|
| 1 | +# Product sectors |
| 2 | +# --------------- |
| 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. |
| 5 | + |
| 6 | +# products of at least two sectors, also with names |
| 7 | +const _MultiSectorTuple = Tuple{Sector, Sector, Vararg{Sector}} |
| 8 | +const ProductSectorType = Union{ |
| 9 | + ProductSector{<:_MultiSectorTuple}, NamedSector{<:NamedTuple{<:Any, <:_MultiSectorTuple}}, |
| 10 | +} |
| 11 | +const ProductSectorBlock = FusionTreeBlock{<:ProductSectorType} |
| 12 | + |
| 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) |
| 21 | +end |
| 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 | + |
| 28 | +function deconstructsector(s::ProductSector) |
| 29 | + positions₁, positions₂ = _partpositions(typeof(s)) |
| 30 | + return _sectorproduct(TupleTools.getindices(s.sectors, positions₁)), |
| 31 | + _sectorproduct(TupleTools.getindices(s.sectors, positions₂)) |
| 32 | +end |
| 33 | +Base.@assume_effects :foldable function deconstructsectortype(::Type{ProductSector{T}}) where {T} |
| 34 | + return map(_partpositions(ProductSector{T})) do positions |
| 35 | + return length(positions) == 1 ? fieldtype(T, only(positions)) : |
| 36 | + ProductSector{Tuple{map(i -> fieldtype(T, i), positions)...}} |
| 37 | + end |
| 38 | +end |
| 39 | +function reconstructsector(::Type{ProductSector{T}}, s₁::Sector, s₂::Sector) where {T} |
| 40 | + sectors = (_sectortuple(s₁)..., _sectortuple(s₂)...) |
| 41 | + positions = TupleTools.invperm(TupleTools.vcat(_partpositions(ProductSector{T})...)) |
| 42 | + return ProductSector{T}(TupleTools.getindices(sectors, positions)) |
| 43 | +end |
| 44 | + |
| 45 | +# named products are split and joined as the corresponding `ProductSector` |
| 46 | +_productsectortype(::Type{NamedSector{NamedTuple{K, T}}}) where {K, T} = ProductSector{T} |
| 47 | +deconstructsector(s::NamedSector) = deconstructsector(ProductSector(s)) |
| 48 | +deconstructsectortype(::Type{I}) where {I <: NamedSector} = deconstructsectortype(_productsectortype(I)) |
| 49 | +reconstructsector(::Type{I}, s₁::Sector, s₂::Sector) where {I <: NamedSector} = |
| 50 | + I(reconstructsector(_productsectortype(I), s₁, s₂)) |
| 51 | + |
| 52 | +# the vertex labels of a product are the linear indices of the labels of its parts, with the second |
| 53 | +# part running fastest, such that a `UniqueFusion` second part leaves the labels of the first unchanged |
| 54 | +_vertexlines(f::FusionTree) = (f.uncoupled[1], f.innerlines..., f.coupled) |
| 55 | +_vertexmultiplicities(f::FusionTree) = |
| 56 | + ntuple(i -> Nsymbol(_vertexlines(f)[i], f.uncoupled[i + 1], _vertexlines(f)[i + 1]), length(f) - 1) |
| 57 | + |
| 58 | +_maptree(proj, f::FusionTree, vertices) = FusionTree( |
| 59 | + map(proj, f.uncoupled), proj(f.coupled), f.isdual, map(proj, f.innerlines), vertices |
| 60 | +) |
| 61 | +function deconstructtree(f::FusionTree) |
| 62 | + vertices₁ = vertices₂ = f.vertices |
| 63 | + if FusionStyle(last(deconstructsectortype(sectortype(f)))) isa UniqueFusion |
| 64 | + vertices₂ = map(Returns(1), f.vertices) |
| 65 | + elseif FusionStyle(f) isa GenericFusion && length(f) > 1 |
| 66 | + ns = _vertexmultiplicities(_maptree(last ∘ deconstructsector, f, map(Returns(1), f.vertices))) |
| 67 | + vertices₁₂ = map((μ, n) -> (divrem(μ - 1, n) .+ 1), f.vertices, ns) |
| 68 | + vertices₁ = first.(vertices₁₂) |
| 69 | + vertices₂ = last.(vertices₁₂) |
| 70 | + end |
| 71 | + return _maptree(first ∘ deconstructsector, f, vertices₁), |
| 72 | + _maptree(last ∘ deconstructsector, f, vertices₂) |
| 73 | +end |
| 74 | +function reconstructtree(::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₂)) |
| 77 | + else |
| 78 | + f₁.vertices |
| 79 | + end |
| 80 | + join(a, b) = reconstructsector(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 |
| 84 | + ) |
| 85 | +end |
| 86 | + |
| 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 = fusiontreetype(I, numout(block₁), numin(block₁))[] |
| 91 | + sizehint!(trees, length(block₁) * length(block₂)) |
| 92 | + for (g₁, g₂) in fusiontrees(block₂) |
| 93 | + for (f₁, f₂) in fusiontrees(block₁) |
| 94 | + push!(trees, (reconstructtree(I, f₁, g₁), reconstructtree(I, f₂, g₂))) |
| 95 | + end |
| 96 | + end |
| 97 | + return FusionTreeBlock(trees) |
| 98 | +end |
| 99 | + |
| 100 | +function FusionTreeBlock{I}( |
| 101 | + uncoupled::Tuple{NTuple{N₁, I}, NTuple{N₂, I}}, |
| 102 | + isdual::Tuple{NTuple{N₁, Bool}, NTuple{N₂, Bool}}; |
| 103 | + sizehint::Int = 0 |
| 104 | + ) where {I <: ProductSectorType, N₁, N₂} |
| 105 | + # blocks of `UniqueFusion` sectors are enumerated directly |
| 106 | + FusionStyle(I) isa UniqueFusion && return fusiontreeblock(I, uncoupled, isdual; sizehint) |
| 107 | + |
| 108 | + # the second part holds the `UniqueFusion` sectors of a mixed product, which are cheaper to construct |
| 109 | + I₁, I₂ = deconstructsectortype(I) |
| 110 | + block₂ = FusionTreeBlock{I₂}(map.(last ∘ deconstructsector, uncoupled), isdual) |
| 111 | + isempty(block₂) && return FusionTreeBlock(Vector{fusiontreetype(I, N₁, N₂)}()) |
| 112 | + |
| 113 | + block₁ = FusionTreeBlock{I₁}(map.(first ∘ deconstructsector, uncoupled), isdual) |
| 114 | + return productblock(I, block₁, block₂) |
| 115 | +end |
| 116 | + |
| 117 | +# the distinct tree pairs, and the position of every tree pair among them |
| 118 | +function _distincttrees(trees) |
| 119 | + distinct = unique(trees) |
| 120 | + return distinct, indexin(trees, distinct) |
| 121 | +end |
| 122 | +# the tree pairs of the two parts of a tree pair |
| 123 | +_deconstructtrees((f₁, f₂)) = map(tuple, deconstructtree(f₁), deconstructtree(f₂)) |
| 124 | + |
| 125 | +# apply `transform` to the block `src`, where every tree pair of a `UniqueFusion` block maps to a |
| 126 | +# single tree pair |
| 127 | +function _unique_transform(transform::F, ::Type{T}, src::FusionTreeBlock) where {F, T} |
| 128 | + FusionStyle(src) isa UniqueFusion || return transform(src) |
| 129 | + dst = map(transform, fusiontrees(src)) |
| 130 | + return FusionTreeBlock(map(first, dst)) => LinearAlgebra.diagm(convert.(T, last.(dst))) |
| 131 | +end |
| 132 | + |
| 133 | +# apply `transform` to the block `src` through its parts, with the recoupling matrix with scalar type `T` |
| 134 | +function product_transform(transform::F, ::Type{T}, src::FusionTreeBlock{I}) where {F, T, I} |
| 135 | + if FusionStyle(I) isa UniqueFusion |
| 136 | + return _unique_transform(transform, T, src) |
| 137 | + elseif FusionStyle(last(deconstructsectortype(I))) isa UniqueFusion && numind(src) > 0 |
| 138 | + # the second part holds the `UniqueFusion` sectors of a mixed product, with a single tree pair |
| 139 | + (g₁, g₂), coeff = transform(last(_deconstructtrees(first(fusiontrees(src))))) |
| 140 | + dst₁, U₁ = transform(FusionTreeBlock(map(first ∘ _deconstructtrees, fusiontrees(src)))) |
| 141 | + dst = FusionTreeBlock( |
| 142 | + map(((f₁, f₂),) -> (reconstructtree(I, f₁, g₁), reconstructtree(I, f₂, g₂)), fusiontrees(dst₁)) |
| 143 | + ) |
| 144 | + U = Matrix{T}(undef, size(U₁)) |
| 145 | + U .= coeff .* U₁ |
| 146 | + return dst => U |
| 147 | + end |
| 148 | + src₁, cols₁ = _distincttrees(map(first ∘ _deconstructtrees, fusiontrees(src))) |
| 149 | + src₂, cols₂ = _distincttrees(map(last ∘ _deconstructtrees, fusiontrees(src))) |
| 150 | + dst₁, U₁ = _unique_transform(transform, T, FusionTreeBlock(src₁)) |
| 151 | + dst₂, U₂ = _unique_transform(transform, T, FusionTreeBlock(src₂)) |
| 152 | + |
| 153 | + dst = productblock(I, dst₁, dst₂) |
| 154 | + U = Matrix{T}(undef, length(dst), length(src)) |
| 155 | + for (col, i, j) in zip(axes(U, 2), cols₁, cols₂) |
| 156 | + row = 0 |
| 157 | + for j₂ in axes(U₂, 1), j₁ in axes(U₁, 1) |
| 158 | + U[row += 1, col] = U₁[j₁, i] * U₂[j₂, j] |
| 159 | + end |
| 160 | + end |
| 161 | + return dst => U |
| 162 | +end |
0 commit comments