Skip to content

Commit 07ed7d5

Browse files
committed
some more simplifications
1 parent 1a3893d commit 07ed7d5

4 files changed

Lines changed: 60 additions & 85 deletions

File tree

‎src/TensorKit.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const TO = TensorOperations
118118

119119
using MatrixAlgebraKit
120120

121-
using Dictionaries: Dictionaries, Dictionary, Indices, gettoken, gettokenvalue
121+
using Dictionaries: Dictionaries, Dictionary, Indices, gettoken, gettokenvalue, set!
122122
using LRUCache
123123
using OhMyThreads
124124
using ScopedValues

‎src/spaces/homspace.jl‎

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,15 @@ function Base.show(io::IO, W::HomSpace)
6969
)
7070
end
7171

72-
@doc """
72+
"""
7373
blocksectors(W::HomSpace) -> Indices{I}
7474
7575
Return an `Indices` of all coupled sectors for `W`. The result is cached based on the
7676
sector structure of `W` (ignoring degeneracy dimensions).
7777
7878
See also [`hasblock`](@ref), [`blockstructure`](@ref).
79-
""" blocksectors(::HomSpace)
80-
81-
function _blocksectors(W::HomSpace)
82-
sectortype(W) === Trivial &&
83-
return OneOrNoneIterator(dim(domain(W)) != 0 && dim(codomain(W)) != 0, Trivial())
84-
85-
codom = codomain(W)
86-
dom = domain(W)
87-
N₁ = length(codom)
88-
N₂ = length(dom)
89-
I = sectortype(W)
90-
if N₁ == N₂ == 0
91-
return allunits(I)
92-
elseif N₁ == 0
93-
return filter!(isunit, collect(blocksectors(dom))) # module space cannot end in empty space
94-
elseif N₂ == 0
95-
return filter!(isunit, collect(blocksectors(codom)))
96-
elseif N₂ <= N₁
97-
return filter!(c -> hasblock(codom, c), collect(blocksectors(dom)))
98-
else
99-
return filter!(c -> hasblock(dom, c), collect(blocksectors(codom)))
100-
end
101-
end
79+
"""
80+
blocksectors(W::HomSpace) = sectorstructure(W).blocksectors
10281

10382
"""
10483
hasblock(W::HomSpace, c::Sector)
@@ -109,15 +88,29 @@ See also [`blocksectors`](@ref).
10988
"""
11089
hasblock(W::HomSpace, c::Sector) = hasblock(codomain(W), c) && hasblock(domain(W), c)
11190

112-
@doc """
91+
"""
11392
dim(W::HomSpace) -> Int
11493
11594
Return the total dimension of a `HomSpace`, i.e. the number of linearly independent
11695
morphisms that can be constructed within this space.
117-
""" dim(::HomSpace)
96+
"""
97+
dim(W::HomSpace) = degeneracystructure(W).totaldim
11898

11999
dims(W::HomSpace) = (dims(codomain(W))..., dims(domain(W))...)
120100

101+
"""
102+
fusiontrees(W::HomSpace) -> Indices{Tuple{F₁,F₂}}
103+
104+
Return an `Indices` of all valid fusion tree pairs `(f₁, f₂)` for `W`, providing a
105+
bijection to sequential integer positions via `gettoken`/`gettokenvalue`. The result is
106+
cached based on the sector structure of `W` (ignoring degeneracy dimensions), so
107+
`HomSpace`s that share the same sectors, dualities, and index count will reuse the same
108+
object.
109+
110+
See also [`sectorstructure`](@ref), [`subblockstructure`](@ref).
111+
"""
112+
fusiontrees(W::HomSpace) = sectorstructure(W).fusiontrees
113+
121114
# Operations on HomSpaces
122115
# -----------------------
123116
"""

‎src/spaces/productspace.jl‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ sectors(P::ProductSpace) = _sectors(P, sectortype(P))
9696
function _sectors(P::ProductSpace{<:ElementarySpace, N}, ::Type{Trivial}) where {N}
9797
return OneOrNoneIterator(dim(P) != 0, ntuple(n -> Trivial(), N))
9898
end
99+
function _sectors(P::ProductSpace{<:ElementarySpace, 0}, ::Type{I}) where {I <: Sector}
100+
return Iterators.map(u -> (u,), allunits(I))
101+
end
99102
function _sectors(P::ProductSpace{<:ElementarySpace, N}, ::Type{<:Sector}) where {N}
100103
return product(map(sectors, P.spaces)...)
101104
end
@@ -147,23 +150,11 @@ that make up the `ProductSpace` instance.
147150
function blocksectors(P::ProductSpace{S, N}) where {S, N}
148151
I = sectortype(S)
149152
if I == Trivial
150-
return OneOrNoneIterator(dim(P) != 0, Trivial())
153+
return dim(P) != 0 ? Indices([Trivial()]) : Indices(Trivial[])
151154
end
152-
bs = Vector{I}()
153-
if N == 0
154-
append!(bs, allunits(I))
155-
elseif N == 1
156-
for s in sectors(P)
157-
push!(bs, first(s))
158-
end
159-
else
160-
for s in sectors(P)
161-
for c in ⊗(s...)
162-
if !(c in bs)
163-
push!(bs, c)
164-
end
165-
end
166-
end
155+
bs = Indices{I}()
156+
for s in sectors(P), c in ⊗(s...)
157+
set!(bs, c)
167158
end
168159
return sort!(bs)
169160
end

‎src/tensors/tensorstructure.jl‎

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,11 @@ Base.@assume_effects :foldable function sectorstructuretype(key::Hashed{S}) wher
5252
return SectorStructure{I, F₁, F₂}
5353
end
5454

55-
"""
56-
DegeneracyStructure{N}
57-
58-
Degeneracy-dependent structure of a `HomSpace`: the block sizes, ranges, and sub-block
59-
strides that depend on the degeneracy (multiplicity) dimensions. Specific to a given
60-
`HomSpace` instance.
61-
62-
## Fields
63-
- `totaldim`: total number of elements in the flat data vector.
64-
- `blockstructure`: `Vector` of `((d₁, d₂), range)` values, one per coupled sector, in the
65-
same order as [`sectorstructure`](@ref)`.blocksectors`.
66-
- `subblockstructure`: `Vector` of [`StridedStructure`](@ref) `(sizes, strides, offset)`
67-
values, one per fusion tree pair, in the same order as
68-
[`sectorstructure`](@ref)`.fusiontrees`.
55+
function _blocksectors(W::HomSpace)
56+
sectortype(W) === Trivial &&
57+
return OneOrNoneIterator(dim(domain(W)) != 0 && dim(codomain(W)) != 0, Trivial())
6958

70-
See also [`degeneracystructure`](@ref), [`SectorStructure`](@ref).
71-
"""
72-
struct DegeneracyStructure{N}
73-
totaldim::Int
74-
blockstructure::Vector{Tuple{Tuple{Int, Int}, UnitRange{Int}}}
75-
subblockstructure::Vector{StridedStructure{N}}
76-
end
77-
78-
function degeneracystructuretype(W::HomSpace)
79-
N = length(codomain(W)) + length(domain(W))
80-
return DegeneracyStructure{N}
59+
return sort!(intersect(blocksectors(codomain(W)), blocksectors(domain(W))))
8160
end
8261

8362
@doc """
@@ -128,6 +107,35 @@ end
128107

129108
CacheStyle(::typeof(sectorstructure), ::Hashed{S}) where {S <: HomSpace} = GlobalLRUCache()
130109

110+
111+
"""
112+
DegeneracyStructure{N}
113+
114+
Degeneracy-dependent structure of a `HomSpace`: the block sizes, ranges, and sub-block
115+
strides that depend on the degeneracy (multiplicity) dimensions. Specific to a given
116+
`HomSpace` instance.
117+
118+
## Fields
119+
- `totaldim`: total number of elements in the flat data vector.
120+
- `blockstructure`: `Vector` of `((d₁, d₂), range)` values, one per coupled sector, in the
121+
same order as [`sectorstructure`](@ref)`.blocksectors`.
122+
- `subblockstructure`: `Vector` of [`StridedStructure`](@ref) `(sizes, strides, offset)`
123+
values, one per fusion tree pair, in the same order as
124+
[`sectorstructure`](@ref)`.fusiontrees`.
125+
126+
See also [`degeneracystructure`](@ref), [`SectorStructure`](@ref).
127+
"""
128+
struct DegeneracyStructure{N}
129+
totaldim::Int
130+
blockstructure::Vector{Tuple{Tuple{Int, Int}, UnitRange{Int}}}
131+
subblockstructure::Vector{StridedStructure{N}}
132+
end
133+
134+
function degeneracystructuretype(W::HomSpace)
135+
N = length(codomain(W)) + length(domain(W))
136+
return DegeneracyStructure{N}
137+
end
138+
131139
@doc """
132140
degeneracystructure(W::HomSpace) -> DegeneracyStructure
133141
@@ -226,23 +234,6 @@ CacheStyle(::typeof(degeneracystructure), ::HomSpace) = GlobalLRUCache()
226234
# Public API: combining the two caches
227235
#--------------------------------------
228236

229-
"""
230-
fusiontrees(W::HomSpace) -> Indices{Tuple{F₁,F₂}}
231-
232-
Return an `Indices` of all valid fusion tree pairs `(f₁, f₂)` for `W`, providing a
233-
bijection to sequential integer positions via `gettoken`/`gettokenvalue`. The result is
234-
cached based on the sector structure of `W` (ignoring degeneracy dimensions), so
235-
`HomSpace`s that share the same sectors, dualities, and index count will reuse the same
236-
object.
237-
238-
See also [`sectorstructure`](@ref), [`subblockstructure`](@ref).
239-
"""
240-
fusiontrees(W::HomSpace) = sectorstructure(W).fusiontrees
241-
242-
blocksectors(W::HomSpace) = sectorstructure(W).blocksectors
243-
244-
dim(W::HomSpace) = degeneracystructure(W).totaldim
245-
246237
"""
247238
blockstructure(W::HomSpace) -> Dictionary
248239

0 commit comments

Comments
 (0)