11# Tensor factorization
22# ----------------------
3+ function factorisation_scalartype (t:: AbstractTensorMap )
4+ T = scalartype (t)
5+ return promote_type (Float32, typeof (zero (T) / sqrt (abs2 (one (T)))))
6+ end
7+ factorisation_scalartype (f, t) = factorisation_scalartype (t)
8+
9+ function permutedcopy_oftype (t:: AbstractTensorMap , T:: Type{<:Number} , p:: Index2Tuple )
10+ return permute! (similar (t, T, permute (space (t), p)), t, p)
11+ end
12+ function copy_oftype (t:: AbstractTensorMap , T:: Type{<:Number} )
13+ return copy! (similar (t, T), t)
14+ end
15+
316"""
417 tsvd(t::AbstractTensorMap, (leftind, rightind)::Index2Tuple;
518 trunc::TruncationScheme = notrunc(), p::Real = 2, alg::Union{SVD, SDD} = SDD())
@@ -37,12 +50,12 @@ Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and `tsvd(!)`
3750is currently only implemented for `InnerProductStyle(t) === EuclideanInnerProduct()`.
3851"""
3952function tsvd (t:: AbstractTensorMap , p:: Index2Tuple ; kwargs... )
40- tcopy = permute! ( similar ( t, float ( scalartype (t)), permute ( space (t), p)), t , p)
53+ tcopy = permutedcopy_oftype ( t, factorisation_scalartype (tsvd, t) , p)
4154 return tsvd! (tcopy; kwargs... )
4255end
4356
4457function LinearAlgebra. svdvals (t:: AbstractTensorMap )
45- tcopy = copy! ( similar ( t, float ( scalartype (t))) , t)
58+ tcopy = copy_oftype ( t, factorisation_scalartype (tsvd , t) )
4659 return LinearAlgebra. svdvals! (tcopy)
4760end
4861
@@ -69,7 +82,7 @@ Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
6982 `InnerProductStyle(t) === EuclideanInnerProduct()`.
7083"""
7184function leftorth (t:: AbstractTensorMap , p:: Index2Tuple ; kwargs... )
72- tcopy = permute! ( similar ( t, float ( scalartype (t)), permute ( space (t), p)), t , p)
85+ tcopy = permutedcopy_oftype ( t, factorisation_scalartype (leftorth, t) , p)
7386 return leftorth! (tcopy; kwargs... )
7487end
7588
@@ -98,7 +111,7 @@ Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
98111`InnerProductStyle(t) === EuclideanInnerProduct()`.
99112"""
100113function rightorth (t:: AbstractTensorMap , p:: Index2Tuple ; kwargs... )
101- tcopy = permute! ( similar ( t, float ( scalartype (t)), permute ( space (t), p)), t , p)
114+ tcopy = permutedcopy_oftype ( t, factorisation_scalartype (rightorth, t) , p)
102115 return rightorth! (tcopy; kwargs... )
103116end
104117
@@ -125,7 +138,7 @@ Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
125138`InnerProductStyle(t) === EuclideanInnerProduct()`.
126139"""
127140function leftnull (t:: AbstractTensorMap , p:: Index2Tuple ; kwargs... )
128- tcopy = permute! ( similar ( t, float ( scalartype (t)), permute ( space (t), p)), t , p)
141+ tcopy = permutedcopy_oftype ( t, factorisation_scalartype (leftnull, t) , p)
129142 return leftnull! (tcopy; kwargs... )
130143end
131144
@@ -154,7 +167,7 @@ Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
154167`InnerProductStyle(t) === EuclideanInnerProduct()`.
155168"""
156169function rightnull (t:: AbstractTensorMap , p:: Index2Tuple ; kwargs... )
157- tcopy = permute! ( similar ( t, float ( scalartype (t)), permute ( space (t), p)), t , p)
170+ tcopy = permutedcopy_oftype ( t, factorisation_scalartype (rightnull, t) , p)
158171 return rightnull! (tcopy; kwargs... )
159172end
160173
@@ -178,12 +191,12 @@ matrices. See the corresponding documentation for more information.
178191See also `eig` and `eigh`
179192"""
180193function LinearAlgebra. eigen (t:: AbstractTensorMap , p:: Index2Tuple ; kwargs... )
181- tcopy = permute! ( similar ( t, float ( scalartype (t)), permute ( space (t), p)), t , p)
194+ tcopy = permutedcopy_oftype ( t, factorisation_scalartype (eigen, t) , p)
182195 return eigen! (tcopy; kwargs... )
183196end
184197
185198function LinearAlgebra. eigvals (t:: AbstractTensorMap ; kwargs... )
186- tcopy = copy! ( similar ( t, float ( scalartype (t))) , t)
199+ tcopy = copy_oftype ( t, factorisation_scalartype (eigen , t) )
187200 return LinearAlgebra. eigvals! (tcopy; kwargs... )
188201end
189202
@@ -210,7 +223,7 @@ matrices. See the corresponding documentation for more information.
210223See also `eigen` and `eigh`.
211224"""
212225function eig (t:: AbstractTensorMap , p:: Index2Tuple ; kwargs... )
213- tcopy = permute! ( similar ( t, float ( scalartype (t)), permute ( space (t), p)), t , p)
226+ tcopy = permutedcopy_oftype ( t, factorisation_scalartype (eig, t) , p)
214227 return eig! (tcopy; kwargs... )
215228end
216229
@@ -235,7 +248,7 @@ permute(t, (leftind, rightind)) * V = V * D
235248See also `eigen` and `eig`.
236249"""
237250function eigh (t:: AbstractTensorMap , p:: Index2Tuple ; kwargs... )
238- tcopy = permute! ( similar ( t, float ( scalartype (t)), permute ( space (t), p)), t , p)
251+ tcopy = permutedcopy_oftype ( t, factorisation_scalartype (eigh, t) , p)
239252 return eigh! (tcopy; kwargs... )
240253end
241254
@@ -251,7 +264,7 @@ which `isposdef!` is called should have equal domain and codomain, as otherwise
251264meaningless.
252265"""
253266function LinearAlgebra. isposdef (t:: AbstractTensorMap , (p₁, p₂):: Index2Tuple )
254- tcopy = permute! ( similar ( t, float ( scalartype (t)), permute ( space (t), p)), t , p)
267+ tcopy = permutedcopy_oftype ( t, factorisation_scalartype (isposdef, t) , p)
255268 return isposdef! (tcopy)
256269end
257270
0 commit comments