Skip to content

Commit

Permalink
Modify Formulation and its behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
waltergu committed Dec 17, 2021
1 parent d5a251a commit 5180171
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
21 changes: 5 additions & 16 deletions src/Essentials/Frameworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,31 +170,20 @@ Expand the generator, that is, get the (representations of the) quantum operator
@inline expand!(result, gen::AbstractGenerator) = error("expand! error: not implemented for $(nameof(typeof(gen))).")

"""
Formulation{F<:Function, P<:Parameters, A<:NamedTuple, K<:NamedTuple, V} <: AbstractGenerator
Formulation{F<:Function, P<:Parameters} <: AbstractGenerator
Generator of (representations of the) quantum operators of a quantum lattice system by analytical expressions.
"""
mutable struct Formulation{F<:Function, P<:Parameters, A<:NamedTuple, K<:NamedTuple, V} <: AbstractGenerator
mutable struct Formulation{F<:Function, P<:Parameters} <: AbstractGenerator
formula::F
parameters::P
args::A
kwargs::K
function Formulation(formula::Function, parameters::Parameters, args::NamedTuple; kwargs...)
kwargs = NamedTuple(kwargs)
V = commontype(formula, (fieldtypes(typeof(parameters))..., fieldtypes(typeof(args))...))
new{typeof(formula), typeof(parameters), typeof(args), typeof(kwargs), V}(formula, parameters, args, kwargs)
end
end
@inline Base.eltype(F::Type{<:Formulation}) = valtype(F)
@inline Base.valtype(::Type{<:Formulation{<:Function, <:Parameters, <:NamedTuple, <:NamedTuple, V}}) where V = V
@inline expand(formulation::Formulation) = formulation.formula(values(formulation.parameters)..., formulation.args...; formulation.kwargs...)
@inline function update!(formulation::Formulation; kwargs...)
formulation.parameters = update(formulation.parameters; kwargs...)
formulation.args = update(formulation.args; kwargs...)
formulation.kwargs = update(formulation.kwargs; kwargs...)
@inline function update!(formulation::Formulation; parameters...)
formulation.parameters = update(formulation.parameters; parameters...)
return formulation
end
@inline Parameters(formulation::Formulation) = formulation.parameters
@inline (formulation::Formulation)(; kwargs...) = formulation.formula(values(formulation.parameters)...; kwargs...)

"""
Entry{C, A<:NamedTuple, B<:NamedTuple, P<:Parameters, D<:Boundary} <: AbstractGenerator
Expand Down
11 changes: 5 additions & 6 deletions test/Essentials/Frameworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,16 @@ end
end

@testset "Formulation" begin
A(t, μ, Δ, k) = [
A(t, μ, Δ; k) = [
2t*cos(k[1])+2t*cos(k[2])+μ 2im*Δ*sin(k[1])+2Δ*sin(k[2]);
-2im*Δ*sin(k[1])+2Δ*sin(k[2]) -2t*cos(k[1])-2t*cos(k[2])-μ
]
f = Formulation(A, (t=1.0, μ=0.0, Δ=0.1), (k=[0.0, 0.0],))
@test valtype(f) == valtype(typeof(f)) == eltype(f) == eltype(typeof(f)) == Matrix{ComplexF64}
f = Formulation(A, (t=1.0, μ=0.0, Δ=0.1))
@test Parameters(f) == (t=1.0, μ=0.0, Δ=0.1)
@test expand(f) [4 0; 0 -4]
@test f(k=[0.0, 0.0]) [4 0; 0 -4]

update!(f; μ=0.3, k=[pi/2, pi/2])
@test expand(f) [0.3 0.2+0.2im; 0.2-0.2im -0.3]
update!(f; μ=0.3)
@test f(k=[pi/2, pi/2]) [0.3 0.2+0.2im; 0.2-0.2im -0.3]
end

@testset "Generator" begin
Expand Down

0 comments on commit 5180171

Please sign in to comment.