Skip to content

Commit fda00f8

Browse files
committed
Compromise
1 parent 5e38fa6 commit fda00f8

File tree

2 files changed

+81
-65
lines changed

2 files changed

+81
-65
lines changed

docs/src/man/tensors.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,8 @@ the keyword argument `p`. The default value `notrunc()` implies no truncation, a
946946
* `truncdim(χ::Integer)`: finds the optimal truncation such that the equivalent total
947947
dimension of the internal vector space is no larger than `χ`;
948948

949-
* `truncspace(W)`: truncates such that the dimension of the internal vector space is
950-
smaller than or equal to that of `W` in any sector, i.e. with
949+
* `truncspace(W)`: truncates such that the dimension of the internal vector space is no
950+
greater than that of `W` in any sector, i.e. with
951951
`W₀ = min(fuse(codomain(t)), fuse(domain(t)))` this option will result in
952952
`domain(U) == domain(Σ) == codomain(Σ) == codomain(Vᵈ) == min(W, W₀)`;
953953

src/tensors/factorizations.jl

Lines changed: 79 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ case a truncated singular value decomposition will be computed. Choices are:
3232
smaller than `η`;
3333
* `truncdim(χ::Int)`: truncates such that the equivalent total dimension of the internal
3434
vector space is no larger than `χ`;
35-
* `truncspace(V)`: truncates such that the dimension of the internal vector space is
36-
smaller than or equal to that of `V` in any sector.
35+
* `truncspace(V)`: truncates such that the dimension of the internal vector space is no
36+
greater than that of `V` in any sector.
3737
* `truncbelow(η::Real)`: truncates such that every singular value is larger then `η` ;
3838
3939
Truncation options can also be combined using `&`, i.e. `truncbelow(η) & truncdim(χ)` will
@@ -272,21 +272,21 @@ function tsvd(t::AbstractTensorMap; kwargs...)
272272
tcopy = copy_oftype(t, float(scalartype(t)))
273273
return tsvd!(tcopy; kwargs...)
274274
end
275-
function leftorth(t::AbstractTensorMap; alg::OFA=QRpos(), kwargs...)
275+
function leftorth(t::AbstractTensorMap; alg::OFA = QRpos(), kwargs...)
276276
tcopy = copy_oftype(t, float(scalartype(t)))
277-
return leftorth!(tcopy; alg=alg, kwargs...)
277+
return leftorth!(tcopy; alg = alg, kwargs...)
278278
end
279-
function rightorth(t::AbstractTensorMap; alg::OFA=LQpos(), kwargs...)
279+
function rightorth(t::AbstractTensorMap; alg::OFA = LQpos(), kwargs...)
280280
tcopy = copy_oftype(t, float(scalartype(t)))
281-
return rightorth!(tcopy; alg=alg, kwargs...)
281+
return rightorth!(tcopy; alg = alg, kwargs...)
282282
end
283-
function leftnull(t::AbstractTensorMap; alg::OFA=QR(), kwargs...)
283+
function leftnull(t::AbstractTensorMap; alg::OFA = QR(), kwargs...)
284284
tcopy = copy_oftype(t, float(scalartype(t)))
285-
return leftnull!(tcopy; alg=alg, kwargs...)
285+
return leftnull!(tcopy; alg = alg, kwargs...)
286286
end
287-
function rightnull(t::AbstractTensorMap; alg::OFA=LQ(), kwargs...)
287+
function rightnull(t::AbstractTensorMap; alg::OFA = LQ(), kwargs...)
288288
tcopy = copy_oftype(t, float(scalartype(t)))
289-
return rightnull!(tcopy; alg=alg, kwargs...)
289+
return rightnull!(tcopy; alg = alg, kwargs...)
290290
end
291291
function LinearAlgebra.eigen(t::AbstractTensorMap; kwargs...)
292292
tcopy = copy_oftype(t, float(scalartype(t)))
@@ -309,20 +309,22 @@ end
309309
# only possible if scalar type is floating point
310310
# only correct if Euclidean inner product
311311
#------------------------------------------------------------------------------------------
312-
const RealOrComplexFloat = Union{AbstractFloat,Complex{<:AbstractFloat}}
313-
314-
function leftorth!(t::TensorMap{<:RealOrComplexFloat};
315-
alg::Union{QR,QRpos,QL,QLpos,SVD,SDD,Polar}=QRpos(),
316-
atol::Real=zero(float(real(scalartype(t)))),
317-
rtol::Real=(alg (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
318-
eps(real(float(one(scalartype(t))))) * iszero(atol))
312+
const RealOrComplexFloat = Union{AbstractFloat, Complex{<:AbstractFloat}}
313+
314+
function leftorth!(
315+
t::TensorMap{<:RealOrComplexFloat};
316+
alg::Union{QR, QRpos, QL, QLpos, SVD, SDD, Polar} = QRpos(),
317+
atol::Real = zero(float(real(scalartype(t)))),
318+
rtol::Real = (alg (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
319+
eps(real(float(one(scalartype(t))))) * iszero(atol)
320+
)
319321
InnerProductStyle(t) === EuclideanInnerProduct() ||
320322
throw_invalid_innerproduct(:leftorth!)
321323
if !iszero(rtol)
322324
atol = max(atol, rtol * norm(t))
323325
end
324326
I = sectortype(t)
325-
dims = SectorDict{I,Int}()
327+
dims = SectorDict{I, Int}()
326328

327329
# compute QR factorization for each block
328330
if !isempty(blocks(t))
@@ -361,18 +363,20 @@ function leftorth!(t::TensorMap{<:RealOrComplexFloat};
361363
return Q, R
362364
end
363365

364-
function leftnull!(t::TensorMap{<:RealOrComplexFloat};
365-
alg::Union{QR,QRpos,SVD,SDD}=QRpos(),
366-
atol::Real=zero(float(real(scalartype(t)))),
367-
rtol::Real=(alg (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
368-
eps(real(float(one(scalartype(t))))) * iszero(atol))
366+
function leftnull!(
367+
t::TensorMap{<:RealOrComplexFloat};
368+
alg::Union{QR, QRpos, SVD, SDD} = QRpos(),
369+
atol::Real = zero(float(real(scalartype(t)))),
370+
rtol::Real = (alg (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
371+
eps(real(float(one(scalartype(t))))) * iszero(atol)
372+
)
369373
InnerProductStyle(t) === EuclideanInnerProduct() ||
370374
throw_invalid_innerproduct(:leftnull!)
371375
if !iszero(rtol)
372376
atol = max(atol, rtol * norm(t))
373377
end
374378
I = sectortype(t)
375-
dims = SectorDict{I,Int}()
379+
dims = SectorDict{I, Int}()
376380

377381
# compute QR factorization for each block
378382
V = codomain(t)
@@ -400,18 +404,20 @@ function leftnull!(t::TensorMap{<:RealOrComplexFloat};
400404
return N
401405
end
402406

403-
function rightorth!(t::TensorMap{<:RealOrComplexFloat};
404-
alg::Union{LQ,LQpos,RQ,RQpos,SVD,SDD,Polar}=LQpos(),
405-
atol::Real=zero(float(real(scalartype(t)))),
406-
rtol::Real=(alg (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
407-
eps(real(float(one(scalartype(t))))) * iszero(atol))
407+
function rightorth!(
408+
t::TensorMap{<:RealOrComplexFloat};
409+
alg::Union{LQ, LQpos, RQ, RQpos, SVD, SDD, Polar} = LQpos(),
410+
atol::Real = zero(float(real(scalartype(t)))),
411+
rtol::Real = (alg (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
412+
eps(real(float(one(scalartype(t))))) * iszero(atol)
413+
)
408414
InnerProductStyle(t) === EuclideanInnerProduct() ||
409415
throw_invalid_innerproduct(:rightorth!)
410416
if !iszero(rtol)
411417
atol = max(atol, rtol * norm(t))
412418
end
413419
I = sectortype(t)
414-
dims = SectorDict{I,Int}()
420+
dims = SectorDict{I, Int}()
415421

416422
# compute LQ factorization for each block
417423
if !isempty(blocks(t))
@@ -450,18 +456,20 @@ function rightorth!(t::TensorMap{<:RealOrComplexFloat};
450456
return L, Q
451457
end
452458

453-
function rightnull!(t::TensorMap{<:RealOrComplexFloat};
454-
alg::Union{LQ,LQpos,SVD,SDD}=LQpos(),
455-
atol::Real=zero(float(real(scalartype(t)))),
456-
rtol::Real=(alg (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
457-
eps(real(float(one(scalartype(t))))) * iszero(atol))
459+
function rightnull!(
460+
t::TensorMap{<:RealOrComplexFloat};
461+
alg::Union{LQ, LQpos, SVD, SDD} = LQpos(),
462+
atol::Real = zero(float(real(scalartype(t)))),
463+
rtol::Real = (alg (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
464+
eps(real(float(one(scalartype(t))))) * iszero(atol)
465+
)
458466
InnerProductStyle(t) === EuclideanInnerProduct() ||
459467
throw_invalid_innerproduct(:rightnull!)
460468
if !iszero(rtol)
461469
atol = max(atol, rtol * norm(t))
462470
end
463471
I = sectortype(t)
464-
dims = SectorDict{I,Int}()
472+
dims = SectorDict{I, Int}()
465473

466474
# compute LQ factorization for each block
467475
V = domain(t)
@@ -489,28 +497,28 @@ function rightnull!(t::TensorMap{<:RealOrComplexFloat};
489497
return N
490498
end
491499

492-
function leftorth!(t::AdjointTensorMap; alg::OFA=QRpos())
500+
function leftorth!(t::AdjointTensorMap; alg::OFA = QRpos())
493501
InnerProductStyle(t) === EuclideanInnerProduct() ||
494502
throw_invalid_innerproduct(:leftorth!)
495-
return map(adjoint, reverse(rightorth!(adjoint(t); alg=alg')))
503+
return map(adjoint, reverse(rightorth!(adjoint(t); alg = alg')))
496504
end
497505

498-
function rightorth!(t::AdjointTensorMap; alg::OFA=LQpos())
506+
function rightorth!(t::AdjointTensorMap; alg::OFA = LQpos())
499507
InnerProductStyle(t) === EuclideanInnerProduct() ||
500508
throw_invalid_innerproduct(:rightorth!)
501-
return map(adjoint, reverse(leftorth!(adjoint(t); alg=alg')))
509+
return map(adjoint, reverse(leftorth!(adjoint(t); alg = alg')))
502510
end
503511

504-
function leftnull!(t::AdjointTensorMap; alg::OFA=QR(), kwargs...)
512+
function leftnull!(t::AdjointTensorMap; alg::OFA = QR(), kwargs...)
505513
InnerProductStyle(t) === EuclideanInnerProduct() ||
506514
throw_invalid_innerproduct(:leftnull!)
507-
return adjoint(rightnull!(adjoint(t); alg=alg', kwargs...))
515+
return adjoint(rightnull!(adjoint(t); alg = alg', kwargs...))
508516
end
509517

510-
function rightnull!(t::AdjointTensorMap; alg::OFA=LQ(), kwargs...)
518+
function rightnull!(t::AdjointTensorMap; alg::OFA = LQ(), kwargs...)
511519
InnerProductStyle(t) === EuclideanInnerProduct() ||
512520
throw_invalid_innerproduct(:rightnull!)
513-
return adjoint(leftnull!(adjoint(t); alg=alg', kwargs...))
521+
return adjoint(leftnull!(adjoint(t); alg = alg', kwargs...))
514522
end
515523

516524
#------------------------------#
@@ -521,18 +529,22 @@ function LinearAlgebra.svdvals!(t::TensorMap{<:RealOrComplexFloat})
521529
end
522530
LinearAlgebra.svdvals!(t::AdjointTensorMap) = svdvals!(adjoint(t))
523531

524-
function tsvd!(t::TensorMap{<:RealOrComplexFloat};
525-
trunc=NoTruncation(), p::Real=2, alg=SDD())
532+
function tsvd!(
533+
t::TensorMap{<:RealOrComplexFloat};
534+
trunc = NoTruncation(), p::Real = 2, alg = SDD()
535+
)
526536
return _tsvd!(t, alg, trunc, p)
527537
end
528-
function tsvd!(t::AdjointTensorMap; trunc=NoTruncation(), p::Real=2, alg=SDD())
529-
u, s, vt, err = tsvd!(adjoint(t); trunc=trunc, p=p, alg=alg)
538+
function tsvd!(t::AdjointTensorMap; trunc = NoTruncation(), p::Real = 2, alg = SDD())
539+
u, s, vt, err = tsvd!(adjoint(t); trunc = trunc, p = p, alg = alg)
530540
return adjoint(vt), adjoint(s), adjoint(u), err
531541
end
532542

533543
# implementation dispatches on algorithm
534-
function _tsvd!(t::TensorMap{<:RealOrComplexFloat}, alg::Union{SVD,SDD},
535-
trunc::TruncationScheme, p::Real=2)
544+
function _tsvd!(
545+
t::TensorMap{<:RealOrComplexFloat}, alg::Union{SVD, SDD},
546+
trunc::TruncationScheme, p::Real = 2
547+
)
536548
# early return
537549
if isempty(blocksectors(t))
538550
truncerr = zero(real(scalartype(t)))
@@ -552,10 +564,10 @@ function _tsvd!(t::TensorMap{<:RealOrComplexFloat}, alg::Union{SVD,SDD},
552564
end
553565

554566
# helper functions
555-
function _compute_svddata!(t::TensorMap, alg::Union{SVD,SDD})
567+
function _compute_svddata!(t::TensorMap, alg::Union{SVD, SDD})
556568
InnerProductStyle(t) === EuclideanInnerProduct() || throw_invalid_innerproduct(:tsvd!)
557569
I = sectortype(t)
558-
dims = SectorDict{I,Int}()
570+
dims = SectorDict{I, Int}()
559571
generator = Base.Iterators.map(blocks(t)) do (c, b)
560572
U, Σ, V = MatrixAlgebra.svd!(b, alg)
561573
dims[c] = length(Σ)
@@ -572,7 +584,7 @@ function _create_svdtensors(t::TensorMap{<:RealOrComplexFloat}, SVDdata, dims)
572584

573585
Tr = real(T)
574586
A = similarstoragetype(t, Tr)
575-
Σ = DiagonalTensorMap{Tr,S,A}(undef, W)
587+
Σ = DiagonalTensorMap{Tr, S, A}(undef, W)
576588

577589
U = similar(t, codomain(t) W)
578590
V⁺ = similar(t, W domain(t))
@@ -589,12 +601,12 @@ function _empty_svdtensors(t::TensorMap{<:RealOrComplexFloat})
589601
T = scalartype(t)
590602
S = spacetype(t)
591603
I = sectortype(t)
592-
dims = SectorDict{I,Int}()
604+
dims = SectorDict{I, Int}()
593605
W = S(dims)
594606

595607
Tr = real(T)
596608
A = similarstoragetype(t, Tr)
597-
Σ = DiagonalTensorMap{Tr,S,A}(undef, W)
609+
Σ = DiagonalTensorMap{Tr, S, A}(undef, W)
598610

599611
U = similar(t, codomain(t) W)
600612
V⁺ = similar(t, W domain(t))
@@ -609,12 +621,16 @@ function LinearAlgebra.eigen!(t::TensorMap{<:RealOrComplexFloat})
609621
end
610622

611623
function LinearAlgebra.eigvals!(t::TensorMap{<:RealOrComplexFloat}; kwargs...)
612-
return SectorDict(c => complex(LinearAlgebra.eigvals!(b; kwargs...))
613-
for (c, b) in blocks(t))
624+
return SectorDict(
625+
c => complex(LinearAlgebra.eigvals!(b; kwargs...))
626+
for (c, b) in blocks(t)
627+
)
614628
end
615629
function LinearAlgebra.eigvals!(t::AdjointTensorMap{<:RealOrComplexFloat}; kwargs...)
616-
return SectorDict(c => conj!(complex(LinearAlgebra.eigvals!(b; kwargs...)))
617-
for (c, b) in blocks(t))
630+
return SectorDict(
631+
c => conj!(complex(LinearAlgebra.eigvals!(b; kwargs...)))
632+
for (c, b) in blocks(t)
633+
)
618634
end
619635

620636
function eigh!(t::TensorMap{<:RealOrComplexFloat})
@@ -625,12 +641,12 @@ function eigh!(t::TensorMap{<:RealOrComplexFloat})
625641
T = scalartype(t)
626642
I = sectortype(t)
627643
S = spacetype(t)
628-
dims = SectorDict{I,Int}(c => size(b, 1) for (c, b) in blocks(t))
644+
dims = SectorDict{I, Int}(c => size(b, 1) for (c, b) in blocks(t))
629645
W = S(dims)
630646

631647
Tr = real(T)
632648
A = similarstoragetype(t, Tr)
633-
D = DiagonalTensorMap{Tr,S,A}(undef, W)
649+
D = DiagonalTensorMap{Tr, S, A}(undef, W)
634650
V = similar(t, domain(t) W)
635651
for (c, b) in blocks(t)
636652
values, vectors = MatrixAlgebra.eigh!(b)
@@ -647,12 +663,12 @@ function eig!(t::TensorMap{<:RealOrComplexFloat}; kwargs...)
647663
T = scalartype(t)
648664
I = sectortype(t)
649665
S = spacetype(t)
650-
dims = SectorDict{I,Int}(c => size(b, 1) for (c, b) in blocks(t))
666+
dims = SectorDict{I, Int}(c => size(b, 1) for (c, b) in blocks(t))
651667
W = S(dims)
652668

653669
Tc = complex(T)
654670
A = similarstoragetype(t, Tc)
655-
D = DiagonalTensorMap{Tc,S,A}(undef, W)
671+
D = DiagonalTensorMap{Tc, S, A}(undef, W)
656672
V = similar(t, Tc, domain(t) W)
657673
for (c, b) in blocks(t)
658674
values, vectors = MatrixAlgebra.eig!(b; kwargs...)

0 commit comments

Comments
 (0)