@@ -12,26 +12,101 @@ depending only on which sectors appear (not their degeneracy dimensions). Shared
1212
1313## Fields
1414- `blocksectors`: `Indices` of all coupled sectors `c::I`.
15- - `fusiontrees`: `Indices ` of all valid fusion tree pairs `(f₁, f₂)`, in canonical order.
15+ - `fusiontrees`: `FusionTreePairs ` of all valid fusion tree pairs `(f₁, f₂)`, in canonical order.
1616
1717See also [`sectorstructure`](@ref), [`DegeneracyStructure`](@ref).
1818"""
19- struct SectorStructure{I <: Sector , F <: FusionTreePair{I} }
19+ struct SectorStructure{I <: Sector , F <: FusionTreePair{I} , T <: AbstractIndices{F} }
2020 blocksectors:: Indices{I}
21- fusiontrees:: Indices{F}
21+ fusiontrees:: T
2222end
2323
24+ """
25+ FusionTreePairs{F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
26+
27+ The fusion tree pairs `(f₁, f₂)` of a `HomSpace`, ordered by coupled sector, then `f₂`, then `f₁`,
28+ stored as the splitting trees `f₁` and the fusion trees `f₂` of every coupled sector. The token of
29+ a pair is its position.
30+ """
31+ struct FusionTreePairs{F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
32+ trees₁:: Indices{F₁} # splitting trees, grouped by coupled sector
33+ trees₂:: Indices{F₂} # fusion trees, grouped by coupled sector
34+ blocks₁:: Vector{Int} # the coupled sector of every splitting tree
35+ blocks₂:: Vector{Int} # the coupled sector of every fusion tree
36+ ranges₁:: Vector{UnitRange{Int}} # the splitting trees of every coupled sector
37+ ranges₂:: Vector{UnitRange{Int}} # the fusion trees of every coupled sector
38+ offsets:: Vector{Int} # the number of pairs before every coupled sector, and in total
39+ end
40+
41+ # from the trees of both sides of every coupled sector
42+ function FusionTreePairs {F₁, F₂} (sides₁, sides₂) where {F₁, F₂}
43+ lengths (ss) = map (length, ss)
44+ ranges (ss) = map ((n, l) -> (n - l + 1 ): n, cumsum (lengths (ss)), lengths (ss))
45+ blocks (ss) = Int[b for (b, ts) in enumerate (ss) for _ in ts]
46+ offsets = cumsum (vcat (0 , lengths (sides₁) .* lengths (sides₂)))
47+ return FusionTreePairs {F₁, F₂} (
48+ Indices (F₁[f for ts in sides₁ for f in ts]), Indices (F₂[f for ts in sides₂ for f in ts]),
49+ blocks (sides₁), blocks (sides₂), ranges (sides₁), ranges (sides₂), offsets
50+ )
51+ end
52+
53+ Base. length (t:: FusionTreePairs ) = last (t. offsets)
54+ Dictionaries. istokenizable (:: FusionTreePairs ) = true
55+ Dictionaries. tokentype (:: FusionTreePairs ) = Int
56+ Dictionaries. iteratetoken (t:: FusionTreePairs , i:: Int = 0 ) = i < length (t) ? (i + 1 , i + 1 ) : nothing
57+ # tokens as returned by `gettoken` look like those of `Indices`, with the position second
58+ function Dictionaries. gettoken (t:: FusionTreePairs , (f₁, f₂))
59+ found₁, (_, i₁) = gettoken (t. trees₁, f₁)
60+ found₂, (_, i₂) = gettoken (t. trees₂, f₂)
61+ (found₁ && found₂ && t. blocks₁[i₁] == t. blocks₂[i₂]) || return false , (0 , 0 )
62+ b = t. blocks₁[i₁]
63+ r₁, r₂ = t. ranges₁[b], t. ranges₂[b]
64+ return true , (0 , t. offsets[b] + (i₂ - first (r₂)) * length (r₁) + (i₁ - first (r₁)) + 1 )
65+ end
66+ Dictionaries. gettokenvalue (t:: FusionTreePairs , (_, i):: Tuple{Int, Int} ) = gettokenvalue (t, i)
67+ function Dictionaries. gettokenvalue (t:: FusionTreePairs , i:: Int )
68+ b = searchsortedlast (t. offsets, i - 1 )
69+ r₁, r₂ = t. ranges₁[b], t. ranges₂[b]
70+ j₂, j₁ = divrem (i - 1 - t. offsets[b], length (r₁))
71+ return (gettokenvalue (t. trees₁, first (r₁) + j₁), gettokenvalue (t. trees₂, first (r₂) + j₂))
72+ end
73+ Base. iterate (t:: FusionTreePairs ) = iterate (t, (1 , 0 , 1 ))
74+ function Base. iterate (t:: FusionTreePairs , (b, i₁, i₂))
75+ while b <= length (t. ranges₁)
76+ r₁, r₂ = t. ranges₁[b], t. ranges₂[b]
77+ i₁ += 1
78+ i₁ > length (r₁) && (i₁ = 1 ; i₂ += 1 )
79+ i₂ <= length (r₂) && ! isempty (r₁) &&
80+ return (gettokenvalue (t. trees₁, first (r₁) + i₁ - 1 ), gettokenvalue (t. trees₂, first (r₂) + i₂ - 1 )), (b, i₁, i₂)
81+ b, i₁, i₂ = b + 1 , 0 , 1
82+ end
83+ return nothing
84+ end
85+
86+ # values for every fusion tree pair, sharing the tokens of `FusionTreePairs`
87+ struct FusionTreePairDict{K, V, T <: FusionTreePairs } <: AbstractDictionary{K, V}
88+ keys:: T
89+ values:: Vector{V}
90+ end
91+ FusionTreePairDict (keys:: T , values:: Vector{V} ) where {T <: FusionTreePairs , V} =
92+ FusionTreePairDict {eltype(T), V, T} (keys, values)
93+ Base. keys (d:: FusionTreePairDict ) = d. keys
94+ Dictionaries. istokenizable (:: FusionTreePairDict ) = true
95+ Dictionaries. istokenassigned (:: FusionTreePairDict , _) = true
96+ Dictionaries. gettokenvalue (d:: FusionTreePairDict , i:: Int ) = d. values[i]
97+ Dictionaries. gettokenvalue (d:: FusionTreePairDict , (_, i):: Tuple{Int, Int} ) = d. values[i]
98+
2499Base. @assume_effects :foldable function sectorstructuretype (key:: Hashed{S} ) where {S <: HomSpace }
25100 I = sectortype (S)
26- F = fusiontreetype (I, numout (S), numin (S))
27- return SectorStructure{I, F }
101+ F₁, F₂ = fusiontreetype (I, numout (S)), fusiontreetype (I , numin (S))
102+ return SectorStructure{I, Tuple{F₁, F₂}, FusionTreePairs{F₁, F₂} }
28103end
29104
30105"""
31106 sectorstructure(W::HomSpace) -> SectorStructure
32107
33- Return the [`SectorStructure`](@ref) for `W`, containing the coupled sectors and fusion tree
34- pairs as `Indices `. The result is cached based on the sector structure of `W` (ignoring
108+ Return the [`SectorStructure`](@ref) for `W`, containing the coupled sectors as `Indices` and the
109+ fusion tree pairs as `FusionTreePairs `. The result is cached based on the sector structure of `W` (ignoring
35110degeneracy dimensions).
36111
37112See also [`degeneracystructure`](@ref), [`fusiontrees`](@ref), [`blocksectors`](@ref).
@@ -40,35 +115,12 @@ sectorstructure(W::HomSpace) = sectorstructure(Hashed(W, sectorhash, sectorequal
40115
41116@cached function sectorstructure (key:: Hashed{S} ):: sectorstructuretype (key) where {S <: HomSpace }
42117 W = parent (key)
43- codom, dom = codomain (W), domain (W)
44-
45- I = sectortype (S)
46- F = fusiontreetype (I, numout (S), numin (S))
47- bs = Vector {I} ()
48- trees = Vector {F} ()
49-
50- for c in _blocksectors (W)
51- push! (bs, c)
52- offset = length (trees)
53- n₁ = 0
54- for f₂ in fusiontrees (dom, c)
55- if n₁ == 0
56- # First f₂ for this sector: enumerate codomain trees and record how many there are.
57- for f₁ in fusiontrees (codom, c)
58- push! (trees, (f₁, f₂))
59- end
60- n₁ = length (trees) - offset
61- else
62- # Subsequent f₂s: the codomain trees are already in the list at
63- # offset .+ (1:n₁), so read them back instead of recomputing.
64- for j in offset .+ (1 : n₁)
65- push! (trees, (trees[j][1 ], f₂))
66- end
67- end
68- end
69- end
70-
71- return SectorStructure {I, F} (Indices (bs), Indices (trees))
118+ # the trees of both sides of every coupled sector; the pairs are not stored
119+ bs = collect (_blocksectors (W))
120+ F₁, F₂ = fusiontreetype (sectortype (S), numout (S)), fusiontreetype (sectortype (S), numin (S))
121+ sides₁ = map (c -> collect (fusiontrees (codomain (W), c)), bs)
122+ sides₂ = map (c -> collect (fusiontrees (domain (W), c)), bs)
123+ return sectorstructuretype (key)(Indices (bs), FusionTreePairs {F₁, F₂} (sides₁, sides₂))
72124end
73125
74126CacheStyle (:: typeof (sectorstructure), :: Hashed{<:HomSpace} ) = GlobalLRUCache ()
@@ -117,72 +169,38 @@ See also [`sectorstructure`](@ref), [`blockstructure`](@ref), [`subblockstructur
117169 N = length (codom) + length (dom)
118170
119171 ss = sectorstructure (W)
120- treelist = ss. fusiontrees
121- L = length (treelist)
122- structurevalues = sizehint! (Vector {StridedStructure{N}} (), L)
172+ ts = ss. fusiontrees
173+ structurevalues = Vector {StridedStructure{N}} (undef, length (ts))
123174 blockvalues = Vector {Tuple{Tuple{Int, Int}, UnitRange{Int}}} (undef, length (ss. blocksectors))
124175
125- # temporary data structures
126- splittingstructure = Vector {NTuple{numout(W), Int}} ()
127-
176+ # the subblocks of every coupled sector, from the degeneracies of the trees of both sides
128177 blockoffset = 0
129- tree_index = 1
130- block_index = 1
131- while tree_index <= L
132- f₁, f₂ = gettokenvalue (treelist, tree_index)
133- c = f₁. coupled
134-
135- # compute subblock structure
136- # splitting tree data
137- empty! (splittingstructure)
138- offset₁ = 0
139- for i in tree_index: L
140- f₁′, f₂′ = gettokenvalue (treelist, i)
141- f₂′ == f₂ || break
142- s₁ = f₁′. uncoupled
143- d₁s = dims (codom, s₁)
144- d₁ = prod (d₁s)
145- offset₁ += d₁
146- push! (splittingstructure, d₁s)
147- end
148- blockdim₁ = offset₁
149- n₁ = length (splittingstructure)
178+ tree_index = 0
179+ for (block_index, (r₁, r₂)) in enumerate (zip (ts. ranges₁, ts. ranges₂))
180+ d₁ss = map (i -> dims (codom, gettokenvalue (ts. trees₁, i). uncoupled), r₁)
181+ d₂ss = map (i -> dims (dom, gettokenvalue (ts. trees₂, i). uncoupled), r₂)
182+ blockdim₁ = sum (prod, d₁ss; init = 0 )
183+ blockdim₂ = sum (prod, d₂ss; init = 0 )
150184 strides = (1 , blockdim₁)
151-
152- # fusion tree data and combine
153185 offset₂ = 0
154- n₂ = 0
155- for i in tree_index: n₁: L
156- f₁′, f₂′ = gettokenvalue (treelist, i)
157- f₂′. coupled == c || break
158- n₂ += 1
159- s₂ = f₂′. uncoupled
160- d₂s = dims (dom, s₂)
186+ for d₂s in d₂ss
161187 d₂ = prod (d₂s)
162188 offset₁ = 0
163- for d₁s in splittingstructure
189+ for d₁s in d₁ss
164190 d₁ = prod (d₁s)
165191 totaloffset = blockoffset + offset₂ * blockdim₁ + offset₁
166192 subsz = (d₁s... , d₂s... )
167193 @assert ! any (== (0 ), subsz)
168194 substr = _subblock_strides (subsz, (d₁, d₂), strides)
169- push! ( structurevalues, (subsz, substr, totaloffset) )
195+ structurevalues[tree_index += 1 ] = (subsz, substr, totaloffset)
170196 offset₁ += d₁
171197 end
172198 offset₂ += d₂
173199 end
174-
175- # compute block structure
176- blockdim₂ = offset₂
177200 blockrange = (blockoffset + 1 ): (blockoffset + blockdim₁ * blockdim₂)
178201 blockvalues[block_index] = ((blockdim₁, blockdim₂), blockrange)
179-
180- # reset
181202 blockoffset = last (blockrange)
182- tree_index += n₁ * n₂
183- block_index += 1
184203 end
185- @assert length (structurevalues) == L
186204
187205 return DegeneracyStructure (blockoffset, blockvalues, structurevalues)
188206end
0 commit comments