Skip to content

Commit dfe026d

Browse files
lkdvosclaude
andcommitted
Use the tokens of the splitting and fusion trees for FusionTreePairs
Replace the per-tree block indices by the coupled sectors, and use the pair of tree positions as the token, with `pairposition` converting it into the position of the pair. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 338fb3c commit dfe026d

3 files changed

Lines changed: 98 additions & 49 deletions

File tree

‎src/spaces/homspace.jl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,11 @@ See also [`degeneracystructure`](@ref), [`subblockstructure`](@ref).
179179
blockstructure(W::HomSpace) = Dictionary(blocksectors(W), degeneracystructure(W).blockstructure)
180180

181181
"""
182-
fusiontrees(W::HomSpace) -> FusionTreePairs{F₁,F₂}
182+
fusiontrees(W::HomSpace) -> FusionTreePairs{I,F₁,F₂}
183183
184184
Return the `FusionTreePairs` of all valid fusion tree pairs `(f₁, f₂)` for `W`, an
185-
`AbstractIndices` providing a bijection to sequential integer positions via
186-
`gettoken`/`gettokenvalue`. The result is
185+
`AbstractIndices` in canonical order. `gettokenvalue` with an integer returns the pair at that
186+
position, and `pairposition` returns the position of a pair. The result is
187187
cached based on the sector structure of `W` (ignoring degeneracy dimensions), so
188188
`HomSpace`s that share the same sectors, dualities, and index count will reuse the same
189189
object.

‎src/spaces/structure.jl‎

Lines changed: 88 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,35 @@ struct SectorStructure{I <: Sector, F <: FusionTreePair{I}, T <: AbstractIndices
2222
end
2323

2424
"""
25-
FusionTreePairs{F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
25+
FusionTreePairs{I, F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
2626
2727
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.
28+
stored as the coupled sectors and the splitting trees `f₁` and fusion trees `f₂` of every coupled
29+
sector. The token of a pair is `(i₁, i₂)`, the positions of `f₁` and `f₂` among all splitting and
30+
fusion trees; `pairposition` converts it into the position of the pair.
3031
"""
31-
struct FusionTreePairs{F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
32+
struct FusionTreePairs{I, F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
33+
blocksectors::Indices{I}
3234
trees₁::Indices{F₁} # splitting trees, grouped by coupled sector
3335
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
3636
ranges₁::Vector{UnitRange{Int}} # the splitting trees of every coupled sector
3737
ranges₂::Vector{UnitRange{Int}} # the fusion trees of every coupled sector
38+
positions₁::Vector{Int} # the position of every splitting tree within its coupled sector
39+
positions₂::Vector{Int} # the number of pairs before the first pair with every fusion tree
3840
offsets::Vector{Int} # the number of pairs before every coupled sector, and in total
3941
end
4042

4143
# from the trees of both sides of every coupled sector
42-
function FusionTreePairs{F₁, F₂}(sides₁, sides₂) where {F₁, F₂}
44+
function FusionTreePairs{I, F₁, F₂}(blocksectors::Indices{I}, sides₁, sides₂) where {I, F₁, F₂}
4345
lengths(ss) = map(length, ss)
4446
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]
4647
offsets = cumsum(vcat(0, lengths(sides₁) .* lengths(sides₂)))
47-
return FusionTreePairs{F₁, F₂}(
48+
positions₁ = Int[j for ts in sides₁ for j in eachindex(ts)]
49+
positions₂ = Int[offsets[b] + (j - 1) * length(sides₁[b]) for (b, ts) in enumerate(sides₂) for j in eachindex(ts)]
50+
return FusionTreePairs{I, F₁, F₂}(
51+
blocksectors,
4852
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
53+
ranges(sides₁), ranges(sides₂), positions₁, positions₂, offsets
5054
)
5155
end
5256

@@ -55,39 +59,83 @@ _cache_payload_size(t::FusionTreePairs) =
5559

5660
Base.length(t::FusionTreePairs) = last(t.offsets)
5761
Dictionaries.istokenizable(::FusionTreePairs) = true
58-
Dictionaries.tokentype(::FusionTreePairs) = Int
59-
Dictionaries.iteratetoken(t::FusionTreePairs, i::Int = 0) = i < length(t) ? (i + 1, i + 1) : nothing
60-
Dictionaries.iteratetoken_reverse(t::FusionTreePairs, i::Int = length(t) + 1) = i > 1 ? (i - 1, i - 1) : nothing
61-
# tokens as returned by `gettoken` look like those of `Indices`, with the position second
62-
function Dictionaries.gettoken(t::FusionTreePairs, (f₁, f₂))
63-
found₁, (_, i₁) = gettoken(t.trees₁, f₁)
64-
found₂, (_, i₂) = gettoken(t.trees₂, f₂)
65-
(found₁ && found₂ && t.blocks₁[i₁] == t.blocks₂[i₂]) || return false, (0, 0)
66-
b = t.blocks₁[i₁]
67-
r₁, r₂ = t.ranges₁[b], t.ranges₂[b]
68-
return true, (0, t.offsets[b] + (i₂ - first(r₂)) * length(r₁) + (i₁ - first(r₁)) + 1)
62+
Dictionaries.tokentype(::FusionTreePairs) = Tuple{Int, Int}
63+
64+
"""
65+
pairposition(t::FusionTreePairs, token) -> Int
66+
pairposition(t::FusionTreePairs, (f₁, f₂)) -> Int
67+
68+
The position of a fusion tree pair in the canonical order of `t`, from its token or from the pair
69+
itself, which must be present.
70+
"""
71+
pairposition(t::FusionTreePairs, (i₁, i₂)::Tuple{Int, Int}) = @inbounds t.positions₂[i₂] + t.positions₁[i₁]
72+
function pairposition(t::FusionTreePairs, f::FusionTreePair)
73+
found, token = gettoken(t, f)
74+
found || throw(KeyError(f))
75+
return pairposition(t, token)
6976
end
70-
Dictionaries.gettokenvalue(t::FusionTreePairs, (_, i)::Tuple{Int, Int}) = gettokenvalue(t, i)
71-
function Dictionaries.gettokenvalue(t::FusionTreePairs, i::Int)
77+
# the token of the pair at position `i`
78+
function pairtoken(t::FusionTreePairs, i::Int)
7279
b = searchsortedlast(t.offsets, i - 1)
7380
r₁, r₂ = t.ranges₁[b], t.ranges₂[b]
7481
j₂, j₁ = divrem(i - 1 - t.offsets[b], length(r₁))
75-
return (gettokenvalue(t.trees₁, first(r₁) + j₁), gettokenvalue(t.trees₂, first(r₂) + j₂))
82+
return (first(r₁) + j₁, first(r₂) + j₂)
7683
end
77-
Base.iterate(t::FusionTreePairs) = iterate(t, (1, 0, 1))
78-
function Base.iterate(t::FusionTreePairs, (b, i₁, i₂))
84+
85+
# iterate over the tokens, with the state `(b, i₁, i₂)`: the coupled sector and the tokens of the
86+
# current pair, or those before the first pair (forward) or after the last pair (reverse)
87+
function Dictionaries.iteratetoken(t::FusionTreePairs, (b, i₁, i₂) = (1, 0, 0))
7988
while b <= length(t.ranges₁)
8089
r₁, r₂ = t.ranges₁[b], t.ranges₂[b]
81-
i₁ += 1
82-
i₁ > length(r₁) && (i₁ = 1; i₂ += 1)
83-
i₂ <= length(r₂) && !isempty(r₁) &&
84-
return (gettokenvalue(t.trees₁, first(r₁) + i₁ - 1), gettokenvalue(t.trees₂, first(r₂) + i₂ - 1)), (b, i₁, i₂)
85-
b, i₁, i₂ = b + 1, 0, 1
90+
if !(isempty(r₁) || isempty(r₂))
91+
if i₁ < first(r₁) # before the first pair of `b`
92+
i₁, i₂ = first(r₁), first(r₂)
93+
return (i₁, i₂), (b, i₁, i₂)
94+
elseif i₁ < last(r₁)
95+
return (i₁ + 1, i₂), (b, i₁ + 1, i₂)
96+
elseif i₂ < last(r₂)
97+
return (first(r₁), i₂ + 1), (b, first(r₁), i₂ + 1)
98+
end
99+
end
100+
b, i₁, i₂ = b + 1, 0, 0
86101
end
87102
return nothing
88103
end
104+
function Dictionaries.iteratetoken_reverse(t::FusionTreePairs, (b, i₁, i₂) = (length(t.ranges₁), typemax(Int), typemax(Int)))
105+
while b >= 1
106+
r₁, r₂ = t.ranges₁[b], t.ranges₂[b]
107+
if !(isempty(r₁) || isempty(r₂))
108+
if i₁ > last(r₁) # after the last pair of `b`
109+
i₁, i₂ = last(r₁), last(r₂)
110+
return (i₁, i₂), (b, i₁, i₂)
111+
elseif i₁ > first(r₁)
112+
return (i₁ - 1, i₂), (b, i₁ - 1, i₂)
113+
elseif i₂ > first(r₂)
114+
return (last(r₁), i₂ - 1), (b, last(r₁), i₂ - 1)
115+
end
116+
end
117+
b, i₁, i₂ = b - 1, typemax(Int), typemax(Int)
118+
end
119+
return nothing
120+
end
121+
function Dictionaries.gettoken(t::FusionTreePairs, (f₁, f₂))
122+
f₁.coupled == f₂.coupled || return false, (0, 0)
123+
found₁, (_, i₁) = gettoken(t.trees₁, f₁)
124+
found₂, (_, i₂) = gettoken(t.trees₂, f₂)
125+
return found₁ & found₂, (i₁, i₂)
126+
end
127+
Dictionaries.gettokenvalue(t::FusionTreePairs, (i₁, i₂)::Tuple{Int, Int}) =
128+
(gettokenvalue(t.trees₁, i₁), gettokenvalue(t.trees₂, i₂))
129+
# the pair at position `i`
130+
Dictionaries.gettokenvalue(t::FusionTreePairs, i::Int) = gettokenvalue(t, pairtoken(t, i))
131+
function Base.iterate(t::FusionTreePairs, state...)
132+
next = Dictionaries.iteratetoken(t, state...)
133+
isnothing(next) && return nothing
134+
token, newstate = next
135+
return gettokenvalue(t, token), newstate
136+
end
89137

90-
# values for every fusion tree pair, sharing the tokens of `FusionTreePairs`
138+
# values for every fusion tree pair, stored by position
91139
struct FusionTreePairDict{K, V, T <: FusionTreePairs} <: AbstractDictionary{K, V}
92140
keys::T
93141
values::Vector{V}
@@ -98,15 +146,15 @@ Base.keys(d::FusionTreePairDict) = d.keys
98146
Dictionaries.istokenizable(::FusionTreePairDict) = true
99147
Dictionaries.istokenassigned(::FusionTreePairDict, _) = true
100148
Dictionaries.gettokenvalue(d::FusionTreePairDict, i::Int) = d.values[i]
101-
Dictionaries.gettokenvalue(d::FusionTreePairDict, (_, i)::Tuple{Int, Int}) = d.values[i]
149+
Dictionaries.gettokenvalue(d::FusionTreePairDict, token::Tuple{Int, Int}) = d.values[pairposition(d.keys, token)]
102150

103151
MemoizationKit.cachesize(s::SectorStructure) =
104152
sizeof(s) + _cache_payload_size(s.blocksectors) + _cache_payload_size(s.fusiontrees)
105153

106154
Base.@assume_effects :foldable function sectorstructuretype(::S) where {S <: HomSpace}
107155
I = sectortype(S)
108156
F₁, F₂ = fusiontreetype(I, numout(S)), fusiontreetype(I, numin(S))
109-
return SectorStructure{I, Tuple{F₁, F₂}, FusionTreePairs{F₁, F₂}}
157+
return SectorStructure{I, Tuple{F₁, F₂}, FusionTreePairs{I, F₁, F₂}}
110158
end
111159

112160
"""
@@ -120,11 +168,12 @@ See also [`degeneracystructure`](@ref), [`fusiontrees`](@ref), [`blocksectors`](
120168
"""
121169
@cached function sectorstructure(W::S)::sectorstructuretype(W) where {S <: HomSpace}
122170
# the trees of both sides of every coupled sector; the pairs are not stored
123-
bs = vec(collect(_blocksectors(W))) # `vec`: the units of a product sector have a shape
124-
F₁, F₂ = fusiontreetype(sectortype(S), numout(S)), fusiontreetype(sectortype(S), numin(S))
125-
sides₁ = map(c -> collect(fusiontrees(codomain(W), c)), bs)
126-
sides₂ = map(c -> collect(fusiontrees(domain(W), c)), bs)
127-
return sectorstructuretype(W)(Indices(bs), FusionTreePairs{F₁, F₂}(sides₁, sides₂))
171+
I = sectortype(S)
172+
bs = Indices(vec(collect(_blocksectors(W)))) # `vec`: the units of a product sector have a shape
173+
F₁, F₂ = fusiontreetype(I, numout(S)), fusiontreetype(I, numin(S))
174+
sides₁ = map(c -> collect(fusiontrees(codomain(W), c)), bs.values)
175+
sides₂ = map(c -> collect(fusiontrees(domain(W), c)), bs.values)
176+
return sectorstructuretype(W)(bs, FusionTreePairs{I, F₁, F₂}(bs, sides₁, sides₂))
128177
end
129178

130179
MemoizationKit.cachekey(::typeof(sectorstructure), W::HomSpace) = (Hashed(W, sectorhash, sectorequal),)

‎src/tensors/treetransformers.jl‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function UniqueTreeTransformer(::Type{A}, transform, p, Vdst::HomSpace, Vsrc::Ho
9797

9898
@timeit_debug GLOBAL_TIMER "symmetry: tree transform" for (isrc, (f₁, f₂)) in enumerate(src_trees)
9999
f_dst, coeff = transform(conjsrc ? (f₂, f₁) : (f₁, f₂))
100-
_, (_, idst) = gettoken(dst_trees, f_dst)
100+
idst = pairposition(dst_trees, f_dst)
101101
data[isrc] = (convert(T, coeff), idst, isrc)
102102
end
103103

@@ -132,13 +132,13 @@ function GenericTreeTransformer(::Type{A}, transform, p, Vdst::HomSpace, Vsrc::H
132132
fs_src = fblocks[i]
133133
fs_dst, U = transform(fs_src)
134134
@timeit_debug GLOBAL_TIMER "bookkeeping: subblock positions" begin
135-
# the token into the fusion tree `Indices` is the subblock position
135+
# the position of a fusion tree pair is that of its subblock
136136
inds_src = map(fusiontrees(fs_src)) do (f₁, f₂)
137-
_, (_, isrc) = gettoken(src_trees, conjsrc ? (f₂, f₁) : (f₁, f₂))
137+
isrc = pairposition(src_trees, conjsrc ? (f₂, f₁) : (f₁, f₂))
138138
return isrc
139139
end
140140
inds_dst = map(fusiontrees(fs_dst)) do f
141-
_, (_, idst) = gettoken(dst_trees, f)
141+
idst = pairposition(dst_trees, f)
142142
return idst
143143
end
144144
end
@@ -262,19 +262,19 @@ function TraceTransformer(::Type{A}, Vdst::HomSpace, Vsrc::HomSpace, p::Index2Tu
262262
for (isrc, f) in enumerate(src_trees)
263263
trace_sectors_match(f, q) || continue
264264
f′, coeff = planar_trace(f, p, q)
265-
_, (_, idst) = gettoken(dst_trees, f′)
265+
idst = pairposition(dst_trees, f′)
266266
addcolumn!([idst], [convert(T, coeff)], isrc)
267267
end
268268
else
269269
for fs_src in fusionblocks(Vsrc)
270270
trace_sectors_match(fs_src, q) || continue
271271
fs_dst, U = planar_trace(fs_src, p, q)
272272
inds_dst = map(fusiontrees(fs_dst)) do f
273-
_, (_, idst) = gettoken(dst_trees, f)
273+
idst = pairposition(dst_trees, f)
274274
return idst
275275
end
276276
for (i, f) in enumerate(fusiontrees(fs_src))
277-
_, (_, isrc) = gettoken(src_trees, f)
277+
isrc = pairposition(src_trees, f)
278278
addcolumn!(inds_dst, convert(Vector{T}, U[:, i]), isrc)
279279
end
280280
end

0 commit comments

Comments
 (0)