@@ -69,16 +69,25 @@ The position of a fusion tree pair in the canonical order of `t`, from its token
6969itself, which must be present.
7070"""
7171pairposition (t:: FusionTreePairs , (i₁, i₂):: Tuple{Int, Int} ) = @inbounds t. positions₂[i₂] + t. positions₁[i₁]
72- function pairposition (t:: FusionTreePairs , f:: FusionTreePair )
72+ pairposition (t:: FusionTreePairs , f:: FusionTreePair ) = pairposition (t, pairtoken (t, f))
73+
74+ """
75+ pairtoken(t::FusionTreePairs, i::Int) -> Tuple{Int, Int}
76+ pairtoken(t::FusionTreePairs, (f₁, f₂)) -> Tuple{Int, Int}
77+
78+ The token of a fusion tree pair of `t`, from its position or from the pair itself, which must be
79+ present.
80+ """
81+ pairtoken (t:: FusionTreePairs , i:: Int ) = _pairtoken (t. ranges₁, t. ranges₂, t. offsets, i)
82+ function pairtoken (t:: FusionTreePairs , f:: FusionTreePair )
7383 found, token = gettoken (t, f)
7484 found || throw (KeyError (f))
75- return pairposition (t, token)
85+ return token
7686end
77- # the token of the pair at position `i`
78- function pairtoken (t:: FusionTreePairs , i:: Int )
79- b = searchsortedlast (t. offsets, i - 1 )
80- r₁, r₂ = t. ranges₁[b], t. ranges₂[b]
81- j₂, j₁ = divrem (i - 1 - t. offsets[b], length (r₁))
87+ function _pairtoken (ranges₁, ranges₂, offsets, i:: Int )
88+ b = searchsortedlast (offsets, i - 1 )
89+ r₁, r₂ = ranges₁[b], ranges₂[b]
90+ j₂, j₁ = divrem (i - 1 - offsets[b], length (r₁))
8291 return (first (r₁) + j₁, first (r₂) + j₂)
8392end
8493
@@ -136,17 +145,18 @@ function Base.iterate(t::FusionTreePairs, state...)
136145end
137146
138147# values for every fusion tree pair, stored by position
139- struct FusionTreePairDict{K, V, T <: FusionTreePairs } <: AbstractDictionary{K, V}
148+ struct FusionTreePairDict{K, V, T <: FusionTreePairs , A <: AbstractVector{V} } <: AbstractDictionary{K, V}
140149 keys:: T
141- values:: Vector{V}
150+ values:: A
142151end
143- FusionTreePairDict (keys:: T , values:: Vector {V} ) where {T <: FusionTreePairs , V} =
144- FusionTreePairDict {eltype(T), V, T} (keys, values)
152+ FusionTreePairDict (keys:: T , values:: AbstractVector {V} ) where {T <: FusionTreePairs , V} =
153+ FusionTreePairDict {eltype(T), V, T, typeof(values) } (keys, values)
145154Base. keys (d:: FusionTreePairDict ) = d. keys
146155Dictionaries. istokenizable (:: FusionTreePairDict ) = true
147156Dictionaries. istokenassigned (:: FusionTreePairDict , _) = true
148157Dictionaries. gettokenvalue (d:: FusionTreePairDict , i:: Int ) = d. values[i]
149- Dictionaries. gettokenvalue (d:: FusionTreePairDict , token:: Tuple{Int, Int} ) = d. values[pairposition (d. keys, token)]
158+ Dictionaries. gettokenvalue (d:: FusionTreePairDict , token:: Tuple{Int, Int} ) = _tokenvalue (d. values, d. keys, token)
159+ _tokenvalue (values:: AbstractVector , keys, token) = values[pairposition (keys, token)]
150160
151161MemoizationKit. cachesize (s:: SectorStructure ) =
152162 sizeof (s) + _cache_payload_size (s. blocksectors) + _cache_payload_size (s. fusiontrees)
@@ -180,6 +190,48 @@ MemoizationKit.cachekey(::typeof(sectorstructure), W::HomSpace) = (Hashed(W, sec
180190
181191# DegeneracyStructure: degeneracy-dependent characterization of HomSpaces
182192# -----------------------------------------------------------------------
193+ """
194+ SubblockStructure{N} <: AbstractVector{StridedStructure{N}}
195+
196+ The [`StridedStructure`](@ref) `(sizes, strides, offset)` of the subblock of every fusion tree pair
197+ of a `HomSpace`, in the canonical order of [`fusiontrees`](@ref). The subblock of `(f₁, f₂)` holds
198+ the rows of `f₁` and the columns of `f₂` within the block of their coupled sector, such that only
199+ the structures of the splitting and fusion trees are stored, and that of a pair is computed when it
200+ is accessed. Besides its position, a pair can also be indexed with its token `(i₁, i₂)`.
201+ """
202+ struct SubblockStructure{N} <: AbstractVector{StridedStructure{N}}
203+ # the codomain legs of every splitting tree, padded with zeros for the domain legs
204+ trees₁:: Vector{StridedStructure{N}}
205+ # the domain legs of every fusion tree, padded with zeros for the codomain legs
206+ trees₂:: Vector{StridedStructure{N}}
207+ # shared with the `FusionTreePairs`, to convert between positions and tokens
208+ ranges₁:: Vector{UnitRange{Int}}
209+ ranges₂:: Vector{UnitRange{Int}}
210+ positions₁:: Vector{Int}
211+ positions₂:: Vector{Int}
212+ offsets:: Vector{Int}
213+ end
214+ SubblockStructure (trees₁, trees₂, t:: FusionTreePairs ) =
215+ SubblockStructure (trees₁, trees₂, t. ranges₁, t. ranges₂, t. positions₁, t. positions₂, t. offsets)
216+
217+ # the token `(i₁, i₂)` of a fusion tree pair, which also addresses its subblock
218+ const SubblockToken = Tuple{Int, Int}
219+
220+ Base. size (s:: SubblockStructure ) = (last (s. offsets),)
221+ Base. IndexStyle (:: Type{<:SubblockStructure} ) = IndexLinear ()
222+ Base. @propagate_inbounds Base. getindex (s:: SubblockStructure , i:: Int ) =
223+ s[_pairtoken (s. ranges₁, s. ranges₂, s. offsets, i)]
224+ Base. @propagate_inbounds function Base. getindex (s:: SubblockStructure , (i₁, i₂):: SubblockToken )
225+ sz₁, str₁, offset₁ = s. trees₁[i₁]
226+ sz₂, str₂, offset₂ = s. trees₂[i₂]
227+ return (sz₁ .+ sz₂, str₁ .+ str₂, offset₁ + offset₂)
228+ end
229+ _tokenvalue (values:: SubblockStructure , keys, token) = values[token]
230+ pairposition (s:: SubblockStructure , (i₁, i₂):: SubblockToken ) = @inbounds s. positions₂[i₂] + s. positions₁[i₁]
231+
232+ # the arrays shared with the `FusionTreePairs` are accounted for in the `sectorstructure` cache
233+ _cache_payload_size (s:: SubblockStructure ) = sizeof (s) + sizeof (s. trees₁) + sizeof (s. trees₂)
234+
183235"""
184236 DegeneracyStructure{N}
185237
@@ -191,18 +243,19 @@ strides that depend on the degeneracy (multiplicity) dimensions. Specific to a g
191243- `totaldim`: total number of elements in the flat data vector.
192244- `blockstructure`: `Vector` of `((d₁, d₂), range)` values, one per coupled sector, in the
193245 same order as [`sectorstructure`](@ref)`.blocksectors`.
194- - `subblockstructure`: `Vector` of [`StridedStructure `](@ref) `(sizes, strides, offset)`
195- values, one per fusion tree pair, in the same order as [`sectorstructure`](@ref)`.fusiontrees`.
246+ - `subblockstructure`: [`SubblockStructure `](@ref) of the `(sizes, strides, offset)` of every
247+ fusion tree pair, in the same order as [`sectorstructure`](@ref)`.fusiontrees`.
196248
197249See also [`degeneracystructure`](@ref), [`SectorStructure`](@ref).
198250"""
199251struct DegeneracyStructure{N}
200252 totaldim:: Int
201253 blockstructure:: Vector{Tuple{Tuple{Int, Int}, UnitRange{Int}}}
202- subblockstructure:: Vector{StridedStructure{N} }
254+ subblockstructure:: SubblockStructure{N }
203255end
204256
205- MemoizationKit. cachesize (s:: DegeneracyStructure ) = sizeof (s) + sizeof (s. blockstructure) + sizeof (s. subblockstructure)
257+ MemoizationKit. cachesize (s:: DegeneracyStructure ) =
258+ sizeof (s) + sizeof (s. blockstructure) + _cache_payload_size (s. subblockstructure)
206259
207260function degeneracystructuretype (W:: HomSpace )
208261 N = length (codomain (W)) + length (domain (W))
@@ -219,57 +272,42 @@ since degeneracy dimensions affect the block sizes and offsets).
219272See also [`sectorstructure`](@ref), [`blockstructure`](@ref), [`subblockstructure`](@ref).
220273""" degeneracystructure (:: HomSpace )
221274@cached function degeneracystructure (W:: HomSpace ):: degeneracystructuretype (W)
222- codom = codomain (W)
223- dom = domain (W )
224- N = length (codom) + length (dom )
275+ codom, dom = codomain (W), domain (W)
276+ N₁, N₂ = length (codom), length (dom )
277+ zeros₁, zeros₂ = ntuple ( Returns ( 0 ), N₁), ntuple ( Returns ( 0 ), N₂ )
225278
226279 ss = sectorstructure (W)
227280 ts = ss. fusiontrees
228- structurevalues = sizehint! (Vector {StridedStructure{N}} (), length (ts))
281+ trees₁ = Vector {StridedStructure{N₁ + N₂}} (undef, length (ts. trees₁))
282+ trees₂ = Vector {StridedStructure{N₁ + N₂}} (undef, length (ts. trees₂))
229283 blockvalues = Vector {Tuple{Tuple{Int, Int}, UnitRange{Int}}} (undef, length (ss. blocksectors))
230284
231- # the subblocks of every coupled sector, from the degeneracies of the trees of both sides;
232- # filling a reused buffer is noticeably faster than `map` for the subblock loop below
233- splittingstructure = Vector {NTuple{numout(W), Int}} ()
285+ # every block is column major, with the rows of its splitting trees and the columns of its
286+ # fusion trees, both in order; the legs of a tree are column major within its rows or columns
234287 blockoffset = 0
235288 for (block_index, (r₁, r₂)) in enumerate (zip (ts. ranges₁, ts. ranges₂))
236- empty! (splittingstructure)
237289 blockdim₁ = 0
238290 for i in r₁
239291 d₁s = dims (codom, gettokenvalue (ts. trees₁, i). uncoupled)
292+ @assert ! any (== (0 ), d₁s)
293+ trees₁[i] = ((d₁s... , zeros₂... ), (_colmajorstrides (d₁s, 1 )... , zeros₂... ), blockdim₁)
240294 blockdim₁ += prod (d₁s)
241- push! (splittingstructure, d₁s)
242295 end
243- strides = (1 , blockdim₁)
244296 blockdim₂ = 0
245297 for i in r₂
246298 d₂s = dims (dom, gettokenvalue (ts. trees₂, i). uncoupled)
247- d₂ = prod (d₂s)
248- offset₁ = 0
249- for d₁s in splittingstructure
250- d₁ = prod (d₁s)
251- totaloffset = blockoffset + blockdim₂ * blockdim₁ + offset₁
252- subsz = (d₁s... , d₂s... )
253- @assert ! any (== (0 ), subsz)
254- substr = _subblock_strides (subsz, (d₁, d₂), strides)
255- push! (structurevalues, (subsz, substr, totaloffset))
256- offset₁ += d₁
257- end
258- blockdim₂ += d₂
299+ @assert ! any (== (0 ), d₂s)
300+ trees₂[i] = ((zeros₁... , d₂s... ), (zeros₁... , _colmajorstrides (d₂s, blockdim₁)... ), blockoffset + blockdim₂ * blockdim₁)
301+ blockdim₂ += prod (d₂s)
259302 end
260303 blockrange = (blockoffset + 1 ): (blockoffset + blockdim₁ * blockdim₂)
261304 blockvalues[block_index] = ((blockdim₁, blockdim₂), blockrange)
262305 blockoffset = last (blockrange)
263306 end
264307
265- return DegeneracyStructure (blockoffset, blockvalues, structurevalues)
266- end
267-
268- function _subblock_strides (subsz, sz, str)
269- sz_simplify = Strided. StridedViews. _simplifydims (sz, str)
270- strides = Strided. StridedViews. _computereshapestrides (subsz, sz_simplify... )
271- isnothing (strides) &&
272- throw (ArgumentError (" unexpected error in computing subblock strides" ))
273- return strides
308+ subblockvalues = SubblockStructure (trees₁, trees₂, ts)
309+ return DegeneracyStructure (blockoffset, blockvalues, subblockvalues)
274310end
275311
312+ # the strides of legs with sizes `ds` that are column major, starting from `stride`
313+ _colmajorstrides (ds:: Tuple , stride:: Int ) = Base. front (cumprod ((stride, ds... )))
0 commit comments