11# Product sectors
22# ---------------
3- # FusionTree manipulations for `ProductSector` are done factor-by-factor: transformation matrices are
4- # computed by kronecker product of the individual ones.
5- # For efficiency we separate all `UniqueFusion` factors and handle these together, while handling the
6- # rest one-by-one.
3+ # Fusion tree manipulations of a product sector are computed from those of two parts of the product,
4+ # as the Kronecker product of their recoupling matrices.
75
8- # products of at least two sectors, also with named factors
9- const MultiSectorTuple = Tuple{Sector, Sector, Vararg{Sector}}
6+ # products of at least two sectors, also with names
7+ const _MultiSectorTuple = Tuple{Sector, Sector, Vararg{Sector}}
108const ProductSectorType = Union{
11- ProductSector{<: MultiSectorTuple }, NamedSector{<: NamedTuple{<:Any, <:MultiSectorTuple } },
9+ ProductSector{<: _MultiSectorTuple }, NamedSector{<: NamedTuple{<:Any, <:_MultiSectorTuple } },
1210}
1311const ProductSectorBlock = FusionTreeBlock{<: ProductSectorType }
1412
15- firstfactor (s:: ProductSector ) = first (s. sectors)
16- otherfactors (s:: ProductSector{<:Tuple{Sector, Sector}} ) = last (s. sectors)
17- otherfactors (s:: ProductSector ) = ProductSector (Base. tail (s. sectors))
18- firstfactortype (:: Type{ProductSector{T}} ) where {T} = fieldtype (T, 1 )
19- otherfactorstype (:: Type{ProductSector{Tuple{H, T}}} ) where {H <: Sector , T <: Sector } = T
20- otherfactorstype (:: Type{ProductSector{T}} ) where {T} = ProductSector{Base. tuple_type_tail (T)}
21-
22- Base. @assume_effects :foldable function ismixedproduct (:: Type{ProductSector{T}} ) where {T}
23- isunique = map (S -> FusionStyle (S) isa UniqueFusion, fieldtypes (T))
24- return any (isunique) && ! all (isunique)
25- end
26-
27- # the non-`UniqueFusion` and the `UniqueFusion` factors, with `Trivial` for no factors
28- partitionfactors (s:: Sector ) = FusionStyle (s) isa UniqueFusion ? (Trivial (), s) : (s, Trivial ())
29- function partitionfactors (s:: ProductSector )
30- g, a = partitionfactors (otherfactors (s))
31- f = firstfactor (s)
32- return FusionStyle (f) isa UniqueFusion ? (g, f ⊠ a) : (f ⊠ g, a)
13+ # The two parts of a product are its non-`UniqueFusion` and its `UniqueFusion` sectors if it has both,
14+ # and otherwise its first and its remaining sectors, on which we recurse.
15+ Base. @assume_effects :foldable function _partpositions (:: Type{ProductSector{T}} ) where {T}
16+ positions = ntuple (identity, fieldcount (T))
17+ isunique = map (i -> FusionStyle (fieldtype (T, i)) isa UniqueFusion, positions)
18+ any (isunique) && ! all (isunique) &&
19+ return Base. filter (i -> ! isunique[i], positions), Base. filter (i -> isunique[i], positions)
20+ return (first (positions),), Base. tail (positions)
3321end
34- partitionfactortypes (:: Type{I} ) where {I <: Sector } =
35- FusionStyle (I) isa UniqueFusion ? (Trivial, I) : (I, Trivial)
36- function partitionfactortypes (:: Type{I} ) where {I <: ProductSector }
37- G, A = partitionfactortypes (otherfactorstype (I))
38- F = firstfactortype (I)
39- return FusionStyle (F) isa UniqueFusion ? (G, F ⊠ A) : (F ⊠ G, A)
22+ # parts with a single sector are not wrapped in a `ProductSector`, to share the caches of that sector
23+ _sectorproduct (sectors:: Tuple{Sector} ) = only (sectors)
24+ _sectorproduct (sectors:: Tuple ) = ProductSector (sectors)
25+ _sectortuple (s:: ProductSector ) = s. sectors
26+ _sectortuple (s:: Sector ) = (s,)
27+ # select the entries at compile-time positions, which keeps heterogeneous tuples inferable
28+ @generated _getentries (t:: Tuple , :: Val{P} ) where {P} = Expr (:tuple , (:(t[$ i]) for i in P). .. )
29+
30+ function productparts (s:: ProductSector )
31+ positions₁, positions₂ = _partpositions (typeof (s))
32+ return _sectorproduct (_getentries (s. sectors, Val (positions₁))),
33+ _sectorproduct (_getentries (s. sectors, Val (positions₂)))
4034end
41- # the inverse of `partitionfactors` for the factors of `I`
42- factortuple (s:: ProductSector ) = s. sectors
43- factortuple (:: Trivial ) = ()
44- factortuple (s:: Sector ) = (s,)
45- interleave (:: Type{Tuple{}} , gs, as) = ()
46- function interleave (:: Type{T} , gs, as) where {T <: Tuple }
47- return if FusionStyle (fieldtype (T, 1 )) isa UniqueFusion
48- (first (as), interleave (Base. tuple_type_tail (T), gs, Base. tail (as))... )
49- else
50- (first (gs), interleave (Base. tuple_type_tail (T), Base. tail (gs), as)... )
35+ Base. @assume_effects :foldable function productpartstypes (:: Type{ProductSector{T}} ) where {T}
36+ return map (_partpositions (ProductSector{T})) do positions
37+ return length (positions) == 1 ? fieldtype (T, only (positions)) :
38+ ProductSector{Tuple{map (i -> fieldtype (T, i), positions)... }}
5139 end
5240end
53- unpartition (:: Type{ProductSector{T}} , g, a) where {T} =
54- ProductSector {T} (interleave (T, factortuple (g), factortuple (a)))
55-
56- headsector (s:: ProductSector ) = ismixedproduct (typeof (s)) ? first (partitionfactors (s)) : firstfactor (s)
57- tailsector (s:: ProductSector ) = ismixedproduct (typeof (s)) ? last (partitionfactors (s)) : otherfactors (s)
58- headsectortype (:: Type{I} ) where {I <: ProductSector } = ismixedproduct (I) ? first (partitionfactortypes (I)) : firstfactortype (I)
59- tailsectortype (:: Type{I} ) where {I <: ProductSector } = ismixedproduct (I) ? last (partitionfactortypes (I)) : otherfactorstype (I)
60- joinsectors (:: Type{I} , h, t) where {I <: ProductSector } = ismixedproduct (I) ? unpartition (I, h, t) : h ⊠ t
41+ function _joinparts (:: Type{ProductSector{T}} , s₁:: Sector , s₂:: Sector ) where {T}
42+ sectors = (_sectortuple (s₁)... , _sectortuple (s₂)... )
43+ positions = TupleTools. invperm (TupleTools. vcat (_partpositions (ProductSector{T})... ))
44+ return ProductSector {T} (_getentries (sectors, Val (positions)))
45+ end
6146
6247# named products are split and joined as the corresponding `ProductSector`
63- productsectortype (:: Type{NamedSector{NamedTuple{K, T}}} ) where {K, T} = ProductSector{T}
64- headsector (s:: NamedSector ) = headsector (ProductSector (s))
65- tailsector (s:: NamedSector ) = tailsector (ProductSector (s))
66- headsectortype (:: Type{I} ) where {I <: NamedSector } = headsectortype (productsectortype (I))
67- tailsectortype (:: Type{I} ) where {I <: NamedSector } = tailsectortype (productsectortype (I))
68- joinsectors (:: Type{I} , h, t) where {I <: NamedSector } = I (joinsectors (productsectortype (I), h, t))
69-
70- # the vertex labels of a product are the linear indices of the head and tail labels, with the tail
71- # running fastest, such that a `UniqueFusion` tail leaves the labels of the head unchanged
72- vertexlines (f:: FusionTree ) = (f. uncoupled[1 ], f. innerlines... , f. coupled)
73- vertexmultiplicities (f:: FusionTree ) =
74- ntuple (i -> Nsymbol (vertexlines (f)[i], f. uncoupled[i + 1 ], vertexlines (f)[i + 1 ]), length (f) - 1 )
75-
76- maptree (proj, f:: FusionTree , vertices) = FusionTree (
48+ _productsectortype (:: Type{NamedSector{NamedTuple{K, T}}} ) where {K, T} = ProductSector{T}
49+ productparts (s:: NamedSector ) = productparts (ProductSector (s))
50+ productpartstypes (:: Type{I} ) where {I <: NamedSector } = productpartstypes (_productsectortype (I))
51+ _joinparts (:: Type{I} , s₁:: Sector , s₂:: Sector ) where {I <: NamedSector } =
52+ I (_joinparts (_productsectortype (I), s₁, s₂))
53+
54+ # the vertex labels of a product are the linear indices of the labels of its parts, with the second
55+ # part running fastest, such that a `UniqueFusion` second part leaves the labels of the first unchanged
56+ _vertexlines (f:: FusionTree ) = (f. uncoupled[1 ], f. innerlines... , f. coupled)
57+ _vertexmultiplicities (f:: FusionTree ) =
58+ ntuple (i -> Nsymbol (_vertexlines (f)[i], f. uncoupled[i + 1 ], _vertexlines (f)[i + 1 ]), length (f) - 1 )
59+
60+ _maptree (proj, f:: FusionTree , vertices) = FusionTree (
7761 map (proj, f. uncoupled), proj (f. coupled), f. isdual, map (proj, f. innerlines), vertices
7862)
79- function headtail (f:: FusionTree )
80- vertices_h = vertices_t = f. vertices
81- if FusionStyle (tailsectortype ( sectortype (f))) isa UniqueFusion
82- vertices_t = map (Returns (1 ), f. vertices)
63+ function productparts (f:: FusionTree )
64+ vertices₁ = vertices₂ = f. vertices
65+ if FusionStyle (last ( productpartstypes ( sectortype (f) ))) isa UniqueFusion
66+ vertices₂ = map (Returns (1 ), f. vertices)
8367 elseif FusionStyle (f) isa GenericFusion && length (f) > 1
84- ns = vertexmultiplicities ( maptree (tailsector , f, map (Returns (1 ), f. vertices)))
85- vertices_h = map ((μ, n) -> div (μ - 1 , n) + 1 , f. vertices, ns)
86- vertices_t = map ((μ, n) -> mod (μ - 1 , n) + 1 , f. vertices, ns)
68+ ns = _vertexmultiplicities ( _maptree (last ∘ productparts , f, map (Returns (1 ), f. vertices)))
69+ vertices₁ = map ((μ, n) -> div (μ - 1 , n) + 1 , f. vertices, ns)
70+ vertices₂ = map ((μ, n) -> mod (μ - 1 , n) + 1 , f. vertices, ns)
8771 end
88- return maptree (headsector , f, vertices_h ), maptree (tailsector , f, vertices_t )
72+ return _maptree (first ∘ productparts , f, vertices₁ ), _maptree (last ∘ productparts , f, vertices₂ )
8973end
90- function jointree (:: Type{I} , h :: FusionTree , t :: FusionTree ) where {I}
91- vertices = if FusionStyle (I) isa GenericFusion && length (h ) > 1 && ! (FusionStyle (t ) isa UniqueFusion)
92- map ((μ, ν, n) -> ν + (μ - 1 ) * n, h . vertices, t . vertices, vertexmultiplicities (t ))
74+ function _joinparts (:: Type{I} , f₁ :: FusionTree , f₂ :: FusionTree ) where {I}
75+ vertices = if FusionStyle (I) isa GenericFusion && length (f₁ ) > 1 && ! (FusionStyle (f₂ ) isa UniqueFusion)
76+ map ((μ, ν, n) -> ν + (μ - 1 ) * n, f₁ . vertices, f₂ . vertices, _vertexmultiplicities (f₂ ))
9377 else
94- h . vertices
78+ f₁ . vertices
9579 end
96- lift (a, b) = joinsectors (I, a, b)
97- return fusiontreetype (I, length (h ))(
98- map (lift, h . uncoupled, t . uncoupled), lift (h . coupled, t . coupled), h . isdual,
99- map (lift, h . innerlines, t . innerlines), vertices
80+ join (a, b) = _joinparts (I, a, b)
81+ return fusiontreetype (I, length (f₁ ))(
82+ map (join, f₁ . uncoupled, f₂ . uncoupled), join (f₁ . coupled, f₂ . coupled), f₁ . isdual,
83+ map (join, f₁ . innerlines, f₂ . innerlines), vertices
10084 )
10185end
10286
103- # the canonical order of a product block is that of the Kronecker product of the head and tail
104- # blocks, with the head running fastest
105- function productblock (:: Type{I} , head:: FusionTreeBlock , tail:: FusionTreeBlock ) where {I}
106- trees = [(jointree (I, h₁, t₁), jointree (I, h₂, t₂)) for (h₁, h₂) in fusiontrees (head), (t₁, t₂) in fusiontrees (tail)]
87+ # the canonical order of a product block is that of the Kronecker product of the blocks of its
88+ # parts, with the first part running fastest
89+ function productblock (:: Type{I} , block₁:: FusionTreeBlock , block₂:: FusionTreeBlock ) where {I}
90+ trees = [
91+ (_joinparts (I, f₁, g₁), _joinparts (I, f₂, g₂))
92+ for (f₁, f₂) in fusiontrees (block₁), (g₁, g₂) in fusiontrees (block₂)
93+ ]
10794 return FusionTreeBlock (vec (trees))
10895end
10996
@@ -115,56 +102,57 @@ function FusionTreeBlock{I}(
115102 # blocks of `UniqueFusion` sectors are enumerated directly
116103 FusionStyle (I) isa UniqueFusion && return fusiontreeblock (I, uncoupled, isdual; sizehint)
117104
118- # the tail holds the `UniqueFusion` factors of a mixed product, which are cheaper to construct
119- tail = FusionTreeBlock {tailsectortype(I)} (map .(tailsector, uncoupled), isdual)
120- isempty (tail) && return FusionTreeBlock (Vector {fusiontreetype(I, N₁, N₂)} ())
105+ # the second part holds the `UniqueFusion` sectors of a mixed product, which are cheaper to construct
106+ I₁, I₂ = productpartstypes (I)
107+ block₂ = FusionTreeBlock {I₂} (map .(last ∘ productparts, uncoupled), isdual)
108+ isempty (block₂) && return FusionTreeBlock (Vector {fusiontreetype(I, N₁, N₂)} ())
121109
122- head = FusionTreeBlock {headsectortype(I) } (map .(headsector , uncoupled), isdual)
123- return productblock (I, head, tail )
110+ block₁ = FusionTreeBlock {I₁ } (map .(first ∘ productparts , uncoupled), isdual)
111+ return productblock (I, block₁, block₂ )
124112end
125113
126114# the distinct tree pairs, and the position of every tree pair among them
127- function distincttrees (trees)
115+ function _distincttrees (trees)
128116 distinct = unique (trees)
129117 return distinct, indexin (trees, distinct)
130118end
131- headtrees ((f₁, f₂)) = ( first ( headtail (f₁)), first ( headtail (f₂)))
132- tailtrees ((f₁, f₂)) = ( last ( headtail ( f₁)), last ( headtail (f₂) ))
119+ # the tree pairs of the two parts of a tree pair
120+ _partstrees ((f₁, f₂)) = map (tuple, productparts ( f₁), productparts (f₂ ))
133121
134122# apply `transform` to the block `src`, where every tree pair of a `UniqueFusion` block maps to a
135123# single tree pair
136- function factor_transform (transform:: F , :: Type{T} , src:: FusionTreeBlock ) where {F, T}
124+ function _unique_transform (transform:: F , :: Type{T} , src:: FusionTreeBlock ) where {F, T}
137125 FusionStyle (src) isa UniqueFusion || return transform (src)
138126 dst = map (transform, fusiontrees (src))
139127 return FusionTreeBlock (map (first, dst)) => LinearAlgebra. diagm (T[last (d) for d in dst])
140128end
141129
142- # apply `transform` to the block `src` through its head and tail , with the recoupling matrix with scalar type `T`
130+ # apply `transform` to the block `src` through its parts , with the recoupling matrix with scalar type `T`
143131function product_transform (transform:: F , :: Type{T} , src:: FusionTreeBlock{I} ) where {F, T, I}
144132 if FusionStyle (I) isa UniqueFusion
145- return factor_transform (transform, T, src)
146- elseif FusionStyle (tailsectortype (I )) isa UniqueFusion && numind (src) > 0
147- # the tail holds the `UniqueFusion` factors of a mixed product, with a single tree pair
148- (t ₁, t ₂), coeff = transform (tailtrees ( first (fusiontrees (src))))
149- dst_h, U_h = transform (FusionTreeBlock (map (headtrees , fusiontrees (src))))
133+ return _unique_transform (transform, T, src)
134+ elseif FusionStyle (last ( productpartstypes (I) )) isa UniqueFusion && numind (src) > 0
135+ # the second part holds the `UniqueFusion` sectors of a mixed product, with a single tree pair
136+ (g ₁, g ₂), coeff = transform (last ( _partstrees ( first (fusiontrees (src) ))))
137+ dst₁, U₁ = transform (FusionTreeBlock (map (first ∘ _partstrees , fusiontrees (src))))
150138 dst = FusionTreeBlock (
151- map (((h ₁, h ₂),) -> (jointree (I, h ₁, t ₁), jointree (I, h ₂, t ₂)), fusiontrees (dst_h ))
139+ map (((f ₁, f ₂),) -> (_joinparts (I, f ₁, g ₁), _joinparts (I, f ₂, g ₂)), fusiontrees (dst₁ ))
152140 )
153- U = Matrix {T} (undef, size (U_h ))
154- U .= coeff .* U_h
141+ U = Matrix {T} (undef, size (U₁ ))
142+ U .= coeff .* U₁
155143 return dst => U
156144 end
157- src_h, cols_h = distincttrees (map (headtrees , fusiontrees (src)))
158- src_t, cols_t = distincttrees (map (tailtrees , fusiontrees (src)))
159- dst_h, U_h = factor_transform (transform, T, FusionTreeBlock (src_h ))
160- dst_t, U_t = factor_transform (transform, T, FusionTreeBlock (src_t ))
145+ src₁, cols₁ = _distincttrees (map (first ∘ _partstrees , fusiontrees (src)))
146+ src₂, cols₂ = _distincttrees (map (last ∘ _partstrees , fusiontrees (src)))
147+ dst₁, U₁ = _unique_transform (transform, T, FusionTreeBlock (src₁ ))
148+ dst₂, U₂ = _unique_transform (transform, T, FusionTreeBlock (src₂ ))
161149
162- dst = productblock (I, dst_h, dst_t )
150+ dst = productblock (I, dst₁, dst₂ )
163151 U = Matrix {T} (undef, length (dst), length (src))
164- for (col, i, j) in zip (axes (U, 2 ), cols_h, cols_t )
152+ for (col, i, j) in zip (axes (U, 2 ), cols₁, cols₂ )
165153 row = 0
166- for jt in axes (U_t , 1 ), jh in axes (U_h , 1 )
167- U[row += 1 , col] = U_h[jh , i] * U_t[jt , j]
154+ for j₂ in axes (U₂ , 1 ), j₁ in axes (U₁ , 1 )
155+ U[row += 1 , col] = U₁[j₁ , i] * U₂[j₂ , j]
168156 end
169157 end
170158 return dst => U
0 commit comments