Skip to content

Commit dc468ec

Browse files
committed
centralize promote_op functions to compute scalartypes
1 parent cb734d2 commit dc468ec

5 files changed

Lines changed: 46 additions & 28 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Random = "1"
5252
SafeTestsets = "0.1"
5353
ScopedValues = "1.3.0"
5454
Strided = "2"
55-
TensorKitSectors = "0.3.3"
55+
TensorKitSectors = "0.3.5"
5656
TensorOperations = "5.1"
5757
Test = "1"
5858
TestExtras = "0.2,0.3"

‎src/tensors/abstracttensor.jl‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ similarstoragetype(::Type{D}, ::Type{T}) where {D <: AbstractDict{<:Sector, <:Ab
102102
# default storage type for numbers
103103
similarstoragetype(::Type{T}) where {T <: Number} = Vector{T}
104104

105-
106-
# helper function to determine the scalartype taking into account that recouplings might happen
107-
recoupled_scalartype(::Type{T}, ::Type{I}) where {T <: Number, I <: Sector} = isreal(I) ? T : complex(T)
108-
recoupled_scalartype(t::AbstractTensorMap) = recoupled_scalartype(typeof(t))
109-
recoupled_scalartype(::Type{T}) where {T <: AbstractTensorMap} = recoupled_scalartype(scalartype(T), sectortype(T))
110-
111105
# tensor characteristics: space and index information
112106
#-----------------------------------------------------
113107
"""

‎src/tensors/diagonal.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ function TO.tensorcontract_type(
261261
::Index2Tuple{1, 1}
262262
)
263263
S = check_spacetype(A, B)
264-
TC′ = recoupled_scalartype(TC, sectortype(S))
264+
TC′ = promote_permute(TC, sectortype(S))
265265
M = promote_storagetype(similarstoragetype(A, TC′), similarstoragetype(B, TC′))
266266
return DiagonalTensorMap{TC, S, M}
267267
end

‎src/tensors/indexmanipulations.jl‎

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
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
"""
1639
function 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
5073
To 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)
6488
end
6589
function 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
125149
To braid into an existing destination, see [braid!](@ref) and [`add_braid!`](@ref)
126150
"""
127151
function 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)
138162
end
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
174198
To permute into an existing destination, see [permute!](@ref) and [`add_permute!`](@ref)
175199
"""
176200
function 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)
186210
end
187211

188212
function LinearAlgebra.transpose(
@@ -294,7 +318,7 @@ See [`twist!`](@ref) for storing the result in place.
294318
"""
295319
function 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)
300324
end

‎src/tensors/tensoroperations.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ end
5454
function TO.tensoradd_type(
5555
TC, A::AbstractTensorMap, ::Index2Tuple{N₁, N₂}, ::Bool
5656
) where {N₁, N₂}
57-
M = similarstoragetype(A, recoupled_scalartype(TC, sectortype(A)))
57+
M = similarstoragetype(A, promote_permute(TC, sectortype(A)))
5858
return tensormaptype(spacetype(A), N₁, N₂, M)
5959
end
6060

@@ -153,7 +153,7 @@ function TO.tensorcontract_type(
153153
::Index2Tuple{N₁, N₂}
154154
) where {N₁, N₂}
155155
S = check_spacetype(A, B)
156-
TC′ = recoupled_scalartype(TC, sectortype(S))
156+
TC′ = promote_permute(TC, sectortype(S))
157157
M = promote_storagetype(similarstoragetype(A, TC′), similarstoragetype(B, TC′))
158158
return tensormaptype(S, N₁, N₂, M)
159159
end

0 commit comments

Comments
 (0)