From 2df89bc22d0e2286dc96f0a4fa067cb42fb0fd24 Mon Sep 17 00:00:00 2001 From: lkdvos Date: Thu, 21 Dec 2023 12:49:14 +0100 Subject: [PATCH] small changes and comment resolutions --- src/operators/multipliedoperator.jl | 29 +++++++++++++---------------- src/operators/timedependence.jl | 5 +++-- test/algorithms.jl | 4 ++-- test/operators.jl | 2 +- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/operators/multipliedoperator.jl b/src/operators/multipliedoperator.jl index 5efff901..0ef2bf8b 100644 --- a/src/operators/multipliedoperator.jl +++ b/src/operators/multipliedoperator.jl @@ -14,40 +14,37 @@ end - An function f that gives the time-dependence according to op(t) = f(t)*op """ const TimedOperator{O} = MultipliedOperator{O,<:Function} +TimedOperator(x) = MultipliedOperator(x, t -> One()) """ Structure representing a time-independent operator that will be multiplied with a constant coefficient. Consists of - An operator (MPO, Hamiltonian, ...) - A number f that gets multiplied with the operator """ -const UntimedOperator{O} = MultipliedOperator{O,<:Union{Real,One}} - -#constructors for (un)TimedOperator -TimedOperator(x::O, f::F) where {F<:Function,O} = MultipliedOperator(x, f) -UntimedOperator(x::O, c::C) where {C<:Union{Real,One},O} = MultipliedOperator(x, c) - -TimedOperator(x) = TimedOperator(x, t -> One()) -UntimedOperator(x) = UntimedOperator(x, One()) +const UntimedOperator{O} = MultipliedOperator{O,<:Number} +UntimedOperator(x) = MultipliedOperator(x, One()) # Holy traits -TimeDependence(x::TimedOperator) = TimeDependent() +TimeDependence(::TimedOperator) = TimeDependent() # For internal use only _eval_at(x::UntimedOperator) = x.f * x.op -_eval_at(x::UntimedOperator, ::Number) = x #_eval_at(x) -_eval_at(x::TimedOperator, t::Number) = UntimedOperator(x.op, x.f(t)) +_eval_at(x::UntimedOperator, ::Number) = x +_eval_at(x::TimedOperator, t::Number) = MultipliedOperator(x.op, x.f(t)) # For users (x::UntimedOperator)() = _eval_at(x) (x::TimedOperator)(t::Number) = _eval_at(x, t) # what to do when we multiply by a scalar -Base.:*(op::UntimedOperator, b::Number) = UntimedOperator(op.op, b * op.f) -Base.:*(op::TimedOperator, b::Number) = TimedOperator(op.op, t -> b * op.f(t)) -Base.:*(b::Number, op::MultipliedOperator) = op * b +Base.:*(op::UntimedOperator, b::Number) = MultipliedOperator(op.op, b * op.f) +Base.:*(op::TimedOperator, b::Number) = MultipliedOperator(op.op, t -> b * op.f(t)) +Base.:*(b, op::MultipliedOperator) = op * b + +# slightly dangerous +Base.:*(op::TimedOperator, g::Function) = MultipliedOperator(op.op, t -> g(t) * op.f(t)) +Base.:*(op::UntimedOperator, g::Function) = MultipliedOperator(op.op, t -> g(t) * op.f) -Base.:*(op::TimedOperator, g::Function) = TimedOperator(op.op, t -> g(t) * op.f(t)) #slightly dangerous -Base.:*(g::Function, op::TimedOperator) = op * g # don't know a better place to put this # environment for MultipliedOperator diff --git a/src/operators/timedependence.jl b/src/operators/timedependence.jl index 14081920..363b9eb2 100644 --- a/src/operators/timedependence.jl +++ b/src/operators/timedependence.jl @@ -1,7 +1,8 @@ # Holy traits to differentiate between time dependent and time independent types -struct TimeDependent end -struct NotTimeDependent end +abstract type TimeDependence end +struct TimeDependent <: TimeDependence end +struct NotTimeDependent <: TimeDependence end TimeDependence(x) = NotTimeDependent() istimed(::TimeDependent) = true diff --git a/test/algorithms.jl b/test/algorithms.jl index 0045cc01..e0bd5a5e 100644 --- a/test/algorithms.jl +++ b/test/algorithms.jl @@ -109,7 +109,7 @@ end @test (3 + 1.55 - 0.1) * sum(E₀) ≈ sum(E) atol = 1e-2 end - Ht = TimedOperator(H, t -> 4) + UntimedOperator(H, 1.45) + Ht = MultipliedOperator(H, t -> 4) + MultipliedOperator(H, 1.45) @testset "Finite TimeDependent LazySum $(alg isa TDVP ? "TDVP" : "TDVP2")" for alg in algs @@ -139,7 +139,7 @@ end @test (3 + 1.55 - 0.1) * sum(E₀) ≈ sum(E) atol = 1e-2 end - Ht = TimedOperator(H, t -> 4) + UntimedOperator(H, 1.45) + Ht = MultipliedOperator(H, t -> 4) + MultipliedOperator(H, 1.45) @testset "Infinite TimeDependent LazySum" begin ψ, envs = timestep(ψ₀, Ht(1.0), 0.0, dt, TDVP()) diff --git a/test/operators.jl b/test/operators.jl index 18f3267d..39d44f7d 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -219,7 +219,7 @@ vspaces = (ℙ^10, Rep[U₁]((0 => 20)), Rep[SU₂](1 => 10, 3 => 5, 5 => 1)) end fs = [t -> 3t, 2, 1] - Hts = [TimedOperator(H1, fs[1]), UntimedOperator(H2, fs[2]), H3] + Hts = [MultipliedOperator(H1, fs[1]), MultipliedOperator(H2, fs[2]), H3] summedH = LazySum(Hts) t = 1.1 summedH_at = summedH(t)