Skip to content

Commit

Permalink
Split indextype to internalindextype, indextype and `coordinate…
Browse files Browse the repository at this point in the history
…dindextype` to reduce the ambiguity.
  • Loading branch information
waltergu committed Dec 13, 2024
1 parent ce7f905 commit 22855e7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
32 changes: 16 additions & 16 deletions src/DegreesOfFreedom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ..Toolkit: contentnames, getcontent, parameternames, shape

export AbstractIndex, AllEqual, Boundary, CompositeInternal, ConstrainedInternal, Internal, InternalIndex, InternalIndexProd, InternalPattern, InternalProd, InternalSum, SimpleInternal, SimpleInternalIndex
export Component, CompositeIndex, CoordinatedIndex, Coupling, Hilbert, Index, MatrixCoupling, MatrixCouplingProd, MatrixCouplingSum, Metric, OperatorUnitToTuple, Ordinal, Pattern, Table, Term, TermAmplitude, TermCoupling, TermFunction
export ˢᵗ, ⁿᵈ, ʳᵈ, ᵗʰ, plain, allequalfields, indextype, isdefinite, partition, patternrule, sitestructure, statistics, @pattern
export ˢᵗ, ⁿᵈ, ʳᵈ, ᵗʰ, plain, allequalfields, coordinatedindextype, indextype, internalindextype, isdefinite, partition, patternrule, sitestructure, statistics, @pattern

# AbstractIndex
"""
Expand Down Expand Up @@ -578,7 +578,7 @@ end
@inline parameternames(::Type{<:Index}) = (:internal, :site)
function Base.show(io::IO, index::Index)
internal = String[]
for i = 1:fieldcount(indextype(index))
for i = 1:fieldcount(internalindextype(index))
value = getfield(index.internal, i)
push!(internal, tostr(value))
end
Expand All @@ -605,13 +605,13 @@ Determine whether a tuple of indexes denotes a definite degree of freedom.
@inline isdefinite(::Type{T}) where {T<:Tuple{Vararg{Index}}} = all(map(isdefinite, fieldtypes(T)))

"""
indextype(index::Index)
indextype(::Type{I}) where {I<:Index}
internalindextype(index::Index)
internalindextype(::Type{I}) where {I<:Index}
Get the type of the internal part of an index.
"""
@inline indextype(index::Index) = indextype(typeof(index))
@inline indextype(::Type{I}) where {I<:Index} = parametertype(I, :internal)
@inline internalindextype(index::Index) = internalindextype(typeof(index))
@inline internalindextype(::Type{I}) where {I<:Index} = parametertype(I, :internal)

"""
statistics(index::Index) -> Symbol
Expand All @@ -620,7 +620,7 @@ Get the type of the internal part of an index.
Get the statistics of an index.
"""
@inline statistics(index::Index) = statistics(typeof(index))
@inline statistics(::Type{I}) where {I<:Index} = statistics(indextype(I))
@inline statistics(::Type{I}) where {I<:Index} = statistics(internalindextype(I))

"""
adjoint(index::Index) -> typeof(index)
Expand Down Expand Up @@ -772,11 +772,11 @@ function permute(index₁::CoordinatedIndex, index₂::CoordinatedIndex)
end

"""
indextype(I::Type{<:SimpleInternal}, P::Type{<:Point})
coordinatedindextype(I::Type{<:SimpleInternal}, P::Type{<:Point})
Get the compatible type of the coordinated index based on the type of an internal space and the type of a point.
"""
@inline indextype(I::Type{<:SimpleInternal}, P::Type{<:Point}) = fulltype(CoordinatedIndex, NamedTuple{(:index, :coordination), Tuple{indextype(I), SVector{dimension(P), scalartype(P)}}})
@inline coordinatedindextype(I::Type{<:SimpleInternal}, P::Type{<:Point}) = fulltype(CoordinatedIndex, NamedTuple{(:index, :coordination), Tuple{indextype(I), SVector{dimension(P), scalartype(P)}}})

"""
rcoordinate(opt::Operator{<:Number, <:ID{CoordinatedIndex}}) -> SVector
Expand Down Expand Up @@ -1231,7 +1231,7 @@ end
types = fieldtypes(parametertype(MC, :components))
V = Expr(:call, :promote_type, [:(parametertype($C, :datatype)) for C in types]...)
S = parametertype(MC, :site)
I = Expr(:call, :indextype, parametertype(MC, :internal), [:(parametertype($C, :basistype)) for C in types]...)
I = Expr(:call, :internalindextype, parametertype(MC, :internal), [:(parametertype($C, :basistype)) for C in types]...)
C = :(InternalPattern{Tuple{$I, $I}, (2,), 1, Tuple{typeof(AllEqual($I))}})
return :(Coupling{$V, Pattern{$C, Tuple{$S, $S}}})
end
Expand Down Expand Up @@ -1563,8 +1563,8 @@ Get the compatible `Operator` type from the type of a term, a Hilbert space and
V, V′, V′′ = valtype(T), valtype(C), valtype(fieldtype(T, :amplitude), B)
isconcretetype(V′) && (V = promote_type(V, V′))
isconcretetype(V′′) && (V = promote_type(V, V′′))
indextypes = ntuple(i->indextype(filter(fieldtype(parametertype(parametertype(idtype(C), :internal), :index), i), valtype(H)), eltype(B)), Val(rank(C)))
return fulltype(Operator, NamedTuple{(:value, :id), Tuple{V, Tuple{indextypes...}}})
coordinatedindextypes = ntuple(i->coordinatedindextype(filter(fieldtype(parametertype(parametertype(idtype(C), :internal), :index), i), valtype(H)), eltype(B)), Val(rank(C)))
return fulltype(Operator, NamedTuple{(:value, :id), Tuple{V, Tuple{coordinatedindextypes...}}})
end

"""
Expand Down Expand Up @@ -1677,7 +1677,7 @@ Get the values of the type parameter `Fields`.
Construct the metric rule from the information of the `Index` type.
"""
@inline OperatorUnitToTuple(::Type{I}) where {I<:Index} = OperatorUnitToTuple(:site, (fieldnames(indextype(I)))...)
@inline OperatorUnitToTuple(::Type{I}) where {I<:Index} = OperatorUnitToTuple(:site, (fieldnames(internalindextype(I)))...)

"""
valtype(::Type{<:OperatorUnitToTuple}, ::Type{<:Index})
Expand All @@ -1691,8 +1691,8 @@ Get the valtype of applying an `OperatorUnitToTuple` rule to an `Index`.
push!(types, :(Core.Compiler.return_type($field, Tuple{Type{I}})))
elseif field==:site
push!(types, Int)
elseif hasfield(indextype(I), field)
push!(types, fieldtype(indextype(I), field))
elseif hasfield(internalindextype(I), field)
push!(types, fieldtype(internalindextype(I), field))
end
end
return Expr(:curly, :Tuple, types...)
Expand All @@ -1710,7 +1710,7 @@ Convert an index to a tuple.
push!(exprs, :(($name)(typeof(index))))
elseif name==:site
push!(exprs, :(index.site))
elseif hasfield(indextype(index), name)
elseif hasfield(internalindextype(index), name)
push!(exprs, :(getfield(index.internal, $(QuoteNode(name)))))
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/QuantumLattices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ include("DegreesOfFreedom.jl")
using .DegreesOfFreedom
export AbstractIndex, AllEqual, Boundary, CompositeInternal, ConstrainedInternal, Internal, InternalIndex, InternalIndexProd, InternalPattern, InternalProd, InternalSum, SimpleInternal, SimpleInternalIndex
export Component, CompositeIndex, CoordinatedIndex, Coupling, Hilbert, Index, MatrixCoupling, MatrixCouplingProd, MatrixCouplingSum, Metric, OperatorUnitToTuple, Ordinal, Pattern, Table, Term, TermAmplitude, TermCoupling, TermFunction
export ˢᵗ, ⁿᵈ, ʳᵈ, ᵗʰ, plain, allequalfields, indextype, isdefinite, partition, patternrule, sitestructure, statistics, @pattern
export ˢᵗ, ⁿᵈ, ʳᵈ, ᵗʰ, plain, allequalfields, coordinatedindextype, indextype, internalindextype, isdefinite, partition, patternrule, sitestructure, statistics, @pattern

# QuantumSystems
include("QuantumSystems.jl")
Expand Down
16 changes: 8 additions & 8 deletions src/QuantumSystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using ..QuantumOperators: ID, LaTeX, Operator, OperatorProd, Operators, latexfor
using ..Spatials: Bond, Point, direction, isparallel, rcoordinate
using ..Toolkit: atol, efficientoperations, rtol, Float, VectorSpace, VectorSpaceCartesian, VectorSpaceStyle, delta, getcontent, rawtype, tostr

import ..DegreesOfFreedom: MatrixCoupling, allequalfields, indextype, isdefinite, patternrule, statistics
import ..DegreesOfFreedom: MatrixCoupling, allequalfields, indextype, internalindextype, isdefinite, patternrule, statistics
import ..QuantumLattices: expand, expand!, kind, permute, rank
import ..QuantumNumbers: Graded
import ..QuantumOperators: latexname, matrix, script
Expand Down Expand Up @@ -76,8 +76,8 @@ end
### requested by InternalPattern
@inline allequalfields(::Type{<:FockIndex}) = (:orbital, :spin)
### requested by MatrixCoupling
@inline indextype(::Type{FockIndex}, ::Type{O}, ::Type{S}, ::Type{N}) where {O<:Union{Int, Symbol, Colon}, S<:Union{Rational{Int}, Symbol, Colon}, N<:Union{Int, Symbol, Colon}} = FockIndex{:, O, S, N}
@inline indextype(::Type{FockIndex{T}}, ::Type{O}, ::Type{S}, ::Type{N}) where {T, O<:Union{Int, Symbol, Colon}, S<:Union{Rational{Int}, Symbol, Colon}, N<:Union{Int, Symbol, Colon}} = FockIndex{T, O, S, N}
@inline internalindextype(::Type{FockIndex}, ::Type{O}, ::Type{S}, ::Type{N}) where {O<:Union{Int, Symbol, Colon}, S<:Union{Rational{Int}, Symbol, Colon}, N<:Union{Int, Symbol, Colon}} = FockIndex{:, O, S, N}
@inline internalindextype(::Type{FockIndex{T}}, ::Type{O}, ::Type{S}, ::Type{N}) where {T, O<:Union{Int, Symbol, Colon}, S<:Union{Rational{Int}, Symbol, Colon}, N<:Union{Int, Symbol, Colon}} = FockIndex{T, O, S, N}

"""
FockIndex(orbital::Union{Int, Symbol, Colon}, spin::Union{Rational{Int}, Int, Symbol, Colon}, nambu::Union{Int, Symbol, Colon})
Expand Down Expand Up @@ -582,8 +582,8 @@ end
return :(rawtype(typeof(index)){totalspin(index)}($(exprs...)))
end
### requested by MatrixCoupling
@inline indextype(::Type{SpinIndex}, ::Type{T}) where {T<:Union{Char, Symbol, Colon}} = SpinIndex{:, T}
@inline indextype(::Type{SpinIndex{S}}, ::Type{T}) where {S, T<:Union{Char, Symbol, Colon}} = SpinIndex{S, T}
@inline internalindextype(::Type{SpinIndex}, ::Type{T}) where {T<:Union{Char, Symbol, Colon}} = SpinIndex{:, T}
@inline internalindextype(::Type{SpinIndex{S}}, ::Type{T}) where {S, T<:Union{Char, Symbol, Colon}} = SpinIndex{S, T}

"""
SpinIndex(tag::Union{Char, Symbol, Colon})
Expand Down Expand Up @@ -612,7 +612,7 @@ Get the total spin.
@inline totalspin(index::CompositeIndex{<:Index{<:SpinIndex}}) = totalspin(typeof(index))
@inline totalspin(::Type{<:SpinIndex}) = NaN
@inline totalspin(::Type{<:SpinIndex{S}}) where S = S
@inline totalspin(::Type{I}) where {I<:Index{<:SpinIndex}} = totalspin(indextype(I))
@inline totalspin(::Type{I}) where {I<:Index{<:SpinIndex}} = totalspin(internalindextype(I))
@inline totalspin(::Type{I}) where {I<:CompositeIndex{<:Index{<:SpinIndex}}} = totalspin(indextype(I))

### convenient construction and string representation
Expand Down Expand Up @@ -1172,7 +1172,7 @@ end
return :(rawtype(typeof(index)){kind(index)}($(exprs...)))
end
### requested by MatrixCoupling
@inline indextype(::Type{PhononIndex{K}}, ::Type{D}) where {K, D<:Union{Char, Symbol, Colon}} = PhononIndex{K, D}
@inline internalindextype(::Type{PhononIndex{K}}, ::Type{D}) where {K, D<:Union{Char, Symbol, Colon}} = PhononIndex{K, D}

"""
PhononIndex{K}(direction::Union{Char, Symbol, Colon}) where K
Expand Down Expand Up @@ -1226,7 +1226,7 @@ Get the kind of a phonon index.
@inline kind(index::CoordinatedIndex{<:Index{<:PhononIndex}}) = kind(typeof(index))
@inline kind(::Type{<:PhononIndex}) = Symbol(":")
@inline kind(::Type{<:PhononIndex{K}}) where K = K
@inline kind(::Type{I}) where {I<:Index{<:PhononIndex}} = kind(indextype(I))
@inline kind(::Type{I}) where {I<:Index{<:PhononIndex}} = kind(internalindextype(I))
@inline kind(::Type{I}) where {I<:CoordinatedIndex{<:Index{<:PhononIndex}}} = kind(indextype(I))

## LaTeX format output
Expand Down
8 changes: 4 additions & 4 deletions test/DegreesOfFreedom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using QuantumLattices.Toolkit: Float, contentnames, parameternames, reparameter
using StaticArrays: SVector

import QuantumLattices: permute
import QuantumLattices.DegreesOfFreedom: indextype, isdefinite, statistics
import QuantumLattices.DegreesOfFreedom: internalindextype, isdefinite, statistics
import QuantumLattices.QuantumOperators: latexname, script
import QuantumLattices.Toolkit: shape

Expand All @@ -32,7 +32,7 @@ function Base.angle(id::CoordinatedIndex{Index{DID{Int}, Int}}, vectors::Abstrac
error("angle error: not supported number of input basis vectors.")
return (id.index.internal.nambu == 1) ? phase : -phase
end
@inline indextype(::Type{DID}, ::Type{T}) where {T<:Union{Int, Symbol, Colon}} = DID{T}
@inline internalindextype(::Type{DID}, ::Type{T}) where {T<:Union{Int, Symbol, Colon}} = DID{T}
@inline 𝕕(nambu) = DID(nambu)
@inline 𝕕(site, nambu) = Index(site, DID(nambu))
@inline 𝕕(site, nambu, rcoordinate, icoordinate) = CoordinatedIndex(Index(site, DID(nambu)), rcoordinate, icoordinate)
Expand Down Expand Up @@ -174,7 +174,7 @@ end
@test parameternames(Index) == (:internal, :site)

index = Index(4, 𝕕(1))
@test indextype(index) == indextype(typeof(index)) == DID{Int}
@test internalindextype(index) == internalindextype(typeof(index)) == DID{Int}
@test index' == 𝕕(4, 2)
@test statistics(index) == statistics(typeof(index)) == :f
@test ishermitian(ID(index', index)) == true
Expand Down Expand Up @@ -233,7 +233,7 @@ end
index₂ = 𝕕(1, 2, (1.0, 0.0), (0.0, 0.0))
@test permute(index₁, index₂) == (Operator(1), Operator(-1, index₂, index₁),)

@test indextype(DFock, Point{2, Float}) == CoordinatedIndex{Index{DID{Int}, Int}, SVector{2, Float}}
@test coordinatedindextype(DFock, Point{2, Float}) == CoordinatedIndex{Index{DID{Int}, Int}, SVector{2, Float}}

op = Operator(1.0, 𝕕(1, 2, SVector(0.5, 0.5), SVector(1.0, 1.0)), 𝕕(1, 1, SVector(0.0, 0.5), SVector(0.0, 1.0)))
@test rcoordinate(op) == SVector(-0.5, 0.0)
Expand Down
18 changes: 9 additions & 9 deletions test/QuantumSystems.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using LaTeXStrings: latexstring
using QuantumLattices: , , decompose, expand, kind, permute, rank
using QuantumLattices.DegreesOfFreedom: ˢᵗ, ⁿᵈ, AbstractIndex, CompositeIndex, ConstrainedInternal, CoordinatedIndex, Coupling, Hilbert, Index, MatrixCoupling, allequalfields, indextype, isdefinite, patternrule, statistics, @pattern
using QuantumLattices.DegreesOfFreedom: ˢᵗ, ⁿᵈ, AbstractIndex, CompositeIndex, ConstrainedInternal, CoordinatedIndex, Coupling, Hilbert, Index, MatrixCoupling, allequalfields, internalindextype, isdefinite, patternrule, statistics, @pattern
using QuantumLattices.QuantumNumbers: Graded, ℕ, 𝕊ᶻ, ℤ₁
using QuantumLattices.QuantumOperators: Operator, Operators, latexname, matrix, script
using QuantumLattices.QuantumSystems
Expand Down Expand Up @@ -47,9 +47,9 @@ using StaticArrays: SVector
@test allequalfields(FockIndex) == (:orbital, :spin)
@test isdefinite(FockIndex{:, Int, Rational{Int}, Int})
@test !isdefinite(FockIndex{:f, Symbol, typeof(:), Int})
@test indextype(FockIndex, Int, typeof(:), Int) == FockIndex{:, Int, typeof(:), Int}
@test indextype(FockIndex{:f}, typeof(:), Symbol, Symbol) == FockIndex{:f, typeof(:), Symbol, Symbol}
@test indextype(FockIndex{:b}, typeof(:), Symbol, Symbol) == FockIndex{:b, typeof(:), Symbol, Symbol}
@test internalindextype(FockIndex, Int, typeof(:), Int) == FockIndex{:, Int, typeof(:), Int}
@test internalindextype(FockIndex{:f}, typeof(:), Symbol, Symbol) == FockIndex{:f, typeof(:), Symbol, Symbol}
@test internalindextype(FockIndex{:b}, typeof(:), Symbol, Symbol) == FockIndex{:b, typeof(:), Symbol, Symbol}

@test AbstractIndex[FockIndex{:f}] == AbstractIndex[Index{<:FockIndex{:f}}] == AbstractIndex[CoordinatedIndex{<:Index{<:FockIndex{:f}}}] == 𝕗
@test AbstractIndex[FockIndex{:b}] == AbstractIndex[Index{<:FockIndex{:b}}] == AbstractIndex[CoordinatedIndex{<:Index{<:FockIndex{:b}}}] == 𝕓
Expand Down Expand Up @@ -405,8 +405,8 @@ end
@test isdefinite(SpinIndex{:, Char})
@test !isdefinite(SpinIndex{1//2, Symbol})
@test !isdefinite(SpinIndex{1, Colon})
@test indextype(SpinIndex, Char) == SpinIndex{:, Char}
@test indextype(SpinIndex{1//2}, Symbol) == SpinIndex{1//2, Symbol}
@test internalindextype(SpinIndex, Char) == SpinIndex{:, Char}
@test internalindextype(SpinIndex{1//2}, Symbol) == SpinIndex{1//2, Symbol}

@test AbstractIndex[SpinIndex] == AbstractIndex[Index{<:SpinIndex}] == AbstractIndex[CoordinatedIndex{<:Index{<:SpinIndex}}] == 𝕊
@test AbstractIndex[SpinIndex{:}] == AbstractIndex[Index{<:SpinIndex{:}}] == AbstractIndex[CoordinatedIndex{<:Index{<:SpinIndex{:}}}] == 𝕊
Expand Down Expand Up @@ -761,9 +761,9 @@ end
@test isdefinite(PhononIndex{:u, Char}) == isdefinite(PhononIndex{:p, Char}) == true
@test isdefinite(PhononIndex{:u, Symbol}) == isdefinite(PhononIndex{:p, Symbol}) == false
@test isdefinite(PhononIndex{:u, Colon}) == isdefinite(PhononIndex{:p, Colon}) == false
@test indextype(PhononIndex{:u}, Char) == PhononIndex{:u, Char}
@test indextype(PhononIndex{:p}, Symbol) == PhononIndex{:p, Symbol}
@test indextype(PhononIndex{:}, Colon) == PhononIndex{:, Colon}
@test internalindextype(PhononIndex{:u}, Char) == PhononIndex{:u, Char}
@test internalindextype(PhononIndex{:p}, Symbol) == PhononIndex{:p, Symbol}
@test internalindextype(PhononIndex{:}, Colon) == PhononIndex{:, Colon}

@test AbstractIndex[PhononIndex{:u}] == AbstractIndex[Index{<:PhononIndex{:u}}] == AbstractIndex[CoordinatedIndex{<:Index{<:PhononIndex{:u}}}] == 𝕦
@test AbstractIndex[PhononIndex{:p}] == AbstractIndex[Index{<:PhononIndex{:p}}] == AbstractIndex[CoordinatedIndex{<:Index{<:PhononIndex{:p}}}] == 𝕡
Expand Down

0 comments on commit 22855e7

Please sign in to comment.