11# Index manipulations
22# ---------------------
3+
4+ # find the scalartype after applying operations: take into account fusion and/or braiding
5+ # might need to become Float or Complex to capture complex recoupling coefficients but don't alter precision
6+ for (operation, manipulation) in (
7+ :flip => :sector , :twist => :braiding ,
8+ :transpose => :fusion , :permute => :sector , :braid => :sector ,
9+ )
10+ promote_op = Symbol (:promote_ , operation)
11+ manipulation_scalartype = Symbol (manipulation, :scalartype )
12+
13+ @eval begin
14+ $ promote_op (t:: AbstractTensorMap ) = $ promote_op (typeof (t))
15+ $ promote_op (:: Type{T} ) where {T <: AbstractTensorMap } =
16+ $ promote_op (scalartype (T), sectortype (T))
17+ $ promote_op (:: Type{T} , :: Type{I} ) where {T <: Number , I <: Sector } =
18+ sectorscalartype (I) <: Integer ? T :
19+ sectorscalartype (I) <: Real ? float (T) : complex (T)
20+ # TODO : currently the manipulations all use sectorscalartype, change to:
21+ # $manipulation_scalartype(I) <: Integer ? T :
22+ # $manipulation_scalartype(I) <: Real ? float(T) : complex(T)
23+ end
24+ end
25+
326"""
427 flip(t::AbstractTensorMap, I) -> t′::AbstractTensorMap
528
@@ -15,7 +38,7 @@ Return a new tensor that is isomorphic to `t` but where the arrows on the indice
1538"""
1639function flip (t:: AbstractTensorMap , I; inv:: Bool = false )
1740 P = flip (space (t), I)
18- t′ = similar (t, recoupled_scalartype (t), P)
41+ t′ = similar (t, promote_flip (t), P)
1942 for (f₁, f₂) in fusiontrees (t)
2043 (f₁′, f₂′), factor = only (flip (f₁, f₂, I; inv))
2144 scale! (t′[f₁′, f₂′], t[f₁, f₂], factor)
@@ -49,18 +72,19 @@ Otherwise, a copy is always made.
4972
5073To permute into an existing destination, see [permute!](@ref) and [`add_permute!`](@ref)
5174"""
52- function permute (t:: AbstractTensorMap , (p₁, p₂) :: Index2Tuple ; copy:: Bool = false )
75+ function permute (t:: AbstractTensorMap , p :: Index2Tuple ; copy:: Bool = false )
5376 # share data if possible
5477 if ! copy
55- if p₁ === codomainind (t) && p₂ === domainind (t)
78+ if p == ( codomainind (t), domainind (t) )
5679 return t
57- elseif t isa TensorMap && has_shared_permute (t, (p₁, p₂) )
58- return TensorMap (t. data, permute (space (t), (p₁, p₂) ))
80+ elseif t isa TensorMap && has_shared_permute (t, p )
81+ return TensorMap (t. data, permute (space (t), p ))
5982 end
6083 end
61- tdst = TO . tensoralloc_add ( scalartype (t), t, (p₁, p₂), false , Val ( false ))
84+
6285 # general case
63- return @inbounds permute! (tdst, t, (p₁, p₂))
86+ tdst = similar (t, promote_permute (t), permute (space (t), p))
87+ return @inbounds permute! (tdst, t, p)
6488end
6589function permute (t:: AdjointTensorMap , (p₁, p₂):: Index2Tuple ; copy:: Bool = false )
6690 p₁′ = adjointtensorindices (t, p₂)
@@ -125,16 +149,16 @@ If `copy=false`, `tdst` might share data with `tsrc` whenever possible. Otherwis
125149To braid into an existing destination, see [braid!](@ref) and [`add_braid!`](@ref)
126150"""
127151function braid (
128- t:: AbstractTensorMap , (p₁, p₂) :: Index2Tuple , levels:: IndexTuple ; copy:: Bool = false
152+ t:: AbstractTensorMap , p :: Index2Tuple , levels:: IndexTuple ; copy:: Bool = false
129153 )
130154 length (levels) == numind (t) || throw (ArgumentError (" invalid levels" ))
131155
132- BraidingStyle (sectortype (t)) isa SymmetricBraiding && return permute (t, (p₁, p₂); copy = copy)
133- (! copy && p₁ == codomainind (t) && p₂ == domainind (t)) && return t
156+ BraidingStyle (sectortype (t)) isa SymmetricBraiding && return permute (t, p; copy)
157+ (! copy && p == ( codomainind (t), domainind (t) )) && return t
134158
135159 # general case
136- tdst = TO . tensoralloc_add ( scalartype (t), t, (p₁, p₂ ), false , Val ( false ))
137- return @inbounds braid! (tdst, t, (p₁, p₂) , levels)
160+ tdst = similar (t, promote_braid (t), permute ( space (t ), p ))
161+ return @inbounds braid! (tdst, t, p , levels)
138162end
139163# TODO : braid for `AdjointTensorMap`; think about how to map the `levels` argument.
140164
@@ -174,15 +198,15 @@ If `copy=false`, `tdst` might share data with `tsrc` whenever possible. Otherwis
174198To permute into an existing destination, see [permute!](@ref) and [`add_permute!`](@ref)
175199"""
176200function LinearAlgebra. transpose (
177- t:: AbstractTensorMap , (p₁, p₂) :: Index2Tuple = _transpose_indices (t);
201+ t:: AbstractTensorMap , p :: Index2Tuple = _transpose_indices (t);
178202 copy:: Bool = false
179203 )
180- sectortype (t) === Trivial && return permute (t, (p₁, p₂) ; copy)
181- (! copy && p₁ == codomainind (t) && p₂ == domainind (t)) && return t
204+ sectortype (t) === Trivial && return permute (t, p ; copy)
205+ (! copy && p == ( codomainind (t), domainind (t) )) && return t
182206
183207 # general case
184- tdst = TO . tensoralloc_add ( scalartype (t), t, (p₁, p₂ ), false , Val ( false ))
185- return @inbounds transpose! (tdst, t, (p₁, p₂) )
208+ tdst = similar (t, promote_transpose (t), permute ( space (t ), p ))
209+ return @inbounds transpose! (tdst, t, p )
186210end
187211
188212function LinearAlgebra. transpose (
@@ -294,7 +318,7 @@ See [`twist!`](@ref) for storing the result in place.
294318"""
295319function twist (t:: AbstractTensorMap , inds; inv:: Bool = false , copy:: Bool = false )
296320 ! copy && has_shared_twist (t, inds) && return t
297- tdst = TO . tensoralloc_add ( scalartype (t), t, ( codomainind (t), domainind (t)), false , Val ( false ))
321+ tdst = similar (t, promote_twist (t ))
298322 copy! (tdst, t)
299323 return twist! (tdst, inds; inv)
300324end
0 commit comments