-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathfactorizations.jl
More file actions
600 lines (529 loc) · 24.2 KB
/
factorizations.jl
File metadata and controls
600 lines (529 loc) · 24.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# Tensor factorization
#----------------------
"""
tsvd(t::AbstractTensorMap, (leftind, rightind)::Index2Tuple;
trunc::TruncationScheme = notrunc(), p::Real = 2, alg::Union{SVD, SDD} = SDD())
-> U, S, V, ϵ
Compute the (possibly truncated) singular value decomposition such that
`norm(permute(t, (leftind, rightind)) - U * S * V) ≈ ϵ`, where `ϵ` thus represents the truncation error.
If `leftind` and `rightind` are not specified, the current partition of left and right
indices of `t` is used. In that case, less memory is allocated if one allows the data in
`t` to be destroyed/overwritten, by using `tsvd!(t, trunc = notrunc(), p = 2)`.
A truncation parameter `trunc` can be specified for the new internal dimension, in which
case a truncated singular value decomposition will be computed. Choices are:
* `notrunc()`: no truncation (default);
* `truncerr(η::Real)`: truncates such that the p-norm of the truncated singular values is
smaller than `η` times the p-norm of all singular values;
* `truncdim(χ::Int)`: truncates such that the equivalent total dimension of the internal
vector space is no larger than `χ`;
* `truncspace(V)`: truncates such that the dimension of the internal vector space is
smaller than that of `V` in any sector.
* `truncbelow(χ::Real)`: truncates such that every singular value is larger then `χ` ;
The method `tsvd` also returns the truncation error `ϵ`, computed as the `p` norm of the
singular values that were truncated.
THe keyword `alg` can be equal to `SVD()` or `SDD()`, corresponding to the underlying LAPACK
algorithm that computes the decomposition (`_gesvd` or `_gesdd`).
Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and `tsvd(!)`
is currently only implemented for `InnerProductStyle(t) === EuclideanProduct()`.
"""
function tsvd(t::AbstractTensorMap, (p₁, p₂)::Index2Tuple; kwargs...)
return tsvd!(permute(t, (p₁, p₂); copy=true); kwargs...)
end
"""
leftorth(t::AbstractTensorMap, (leftind, rightind)::Index2Tuple;
alg::OrthogonalFactorizationAlgorithm = QRpos()) -> Q, R
Create orthonormal basis `Q` for indices in `leftind`, and remainder `R` such that
`permute(t, (leftind, rightind)) = Q*R`.
If `leftind` and `rightind` are not specified, the current partition of left and right
indices of `t` is used. In that case, less memory is allocated if one allows the data in `t`
to be destroyed/overwritten, by using `leftorth!(t, alg = QRpos())`.
Different algorithms are available, namely `QR()`, `QRpos()`, `SVD()` and `Polar()`. `QR()`
and `QRpos()` use a standard QR decomposition, producing an upper triangular matrix `R`.
`Polar()` produces a Hermitian and positive semidefinite `R`. `QRpos()` corrects the
standard QR decomposition such that the diagonal elements of `R` are positive. Only
`QRpos()` and `Polar()` are unique (no residual freedom) so that they always return the same
result for the same input tensor `t`.
Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
`leftorth(!)` is currently only implemented for
`InnerProductStyle(t) === EuclideanProduct()`.
"""
function leftorth(t::AbstractTensorMap, (p₁, p₂)::Index2Tuple; kwargs...)
return leftorth!(permute(t, (p₁, p₂); copy=true); kwargs...)
end
"""
rightorth(t::AbstractTensorMap, (leftind, rightind)::Index2Tuple;
alg::OrthogonalFactorizationAlgorithm = LQpos()) -> L, Q
Create orthonormal basis `Q` for indices in `rightind`, and remainder `L` such that
`permute(t, (leftind, rightind)) = L*Q`.
If `leftind` and `rightind` are not specified, the current partition of left and right
indices of `t` is used. In that case, less memory is allocated if one allows the data in `t`
to be destroyed/overwritten, by using `rightorth!(t, alg = LQpos())`.
Different algorithms are available, namely `LQ()`, `LQpos()`, `RQ()`, `RQpos()`, `SVD()` and
`Polar()`. `LQ()` and `LQpos()` produce a lower triangular matrix `L` and are computed using
a QR decomposition of the transpose. `RQ()` and `RQpos()` produce an upper triangular
remainder `L` and only works if the total left dimension is smaller than or equal to the
total right dimension. `LQpos()` and `RQpos()` add an additional correction such that the
diagonal elements of `L` are positive. `Polar()` produces a Hermitian and positive
semidefinite `L`. Only `LQpos()`, `RQpos()` and `Polar()` are unique (no residual freedom)
so that they always return the same result for the same input tensor `t`.
Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
`rightorth(!)` is currently only implemented for
`InnerProductStyle(t) === EuclideanProduct()`.
"""
function rightorth(t::AbstractTensorMap, (p₁, p₂)::Index2Tuple; kwargs...)
return rightorth!(permute(t, (p₁, p₂); copy=true); kwargs...)
end
"""
leftnull(t::AbstractTensor, (leftind, rightind)::Index2Tuple;
alg::OrthogonalFactorizationAlgorithm = QRpos()) -> N
Create orthonormal basis for the orthogonal complement of the support of the indices in
`leftind`, such that `N' * permute(t, (leftind, rightind)) = 0`.
If `leftind` and `rightind` are not specified, the current partition of left and right
indices of `t` is used. In that case, less memory is allocated if one allows the data in `t`
to be destroyed/overwritten, by using `leftnull!(t, alg = QRpos())`.
Different algorithms are available, namely `QR()` (or equivalently, `QRpos()`), `SVD()` and
`SDD()`. The first assumes that the matrix is full rank and requires `iszero(atol)` and
`iszero(rtol)`. With `SVD()` and `SDD()`, `rightnull` will use the corresponding singular
value decomposition, and one can specify an absolute or relative tolerance for which
singular values are to be considered zero, where `max(atol, norm(t)*rtol)` is used as upper
bound.
Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
`leftnull(!)` is currently only implemented for
`InnerProductStyle(t) === EuclideanProduct()`.
"""
function leftnull(t::AbstractTensorMap, (p₁, p₂)::Index2Tuple; kwargs...)
return leftnull!(permute(t, (p₁, p₂); copy=true); kwargs...)
end
"""
rightnull(t::AbstractTensor, (leftind, rightind)::Index2Tuple;
alg::OrthogonalFactorizationAlgorithm = LQ(),
atol::Real = 0.0,
rtol::Real = eps(real(float(one(scalartype(t)))))*iszero(atol)) -> N
Create orthonormal basis for the orthogonal complement of the support of the indices in
`rightind`, such that `permute(t, (leftind, rightind))*N' = 0`.
If `leftind` and `rightind` are not specified, the current partition of left and right
indices of `t` is used. In that case, less memory is allocated if one allows the data in `t`
to be destroyed/overwritten, by using `rightnull!(t, alg = LQpos())`.
Different algorithms are available, namely `LQ()` (or equivalently, `LQpos`), `SVD()` and
`SDD()`. The first assumes that the matrix is full rank and requires `iszero(atol)` and
`iszero(rtol)`. With `SVD()` and `SDD()`, `rightnull` will use the corresponding singular
value decomposition, and one can specify an absolute or relative tolerance for which
singular values are to be considered zero, where `max(atol, norm(t)*rtol)` is used as upper
bound.
Orthogonality requires `InnerProductStyle(t) <: HasInnerProduct`, and
`rightnull(!)` is currently only implemented for
`InnerProductStyle(t) === EuclideanProduct()`.
"""
function rightnull(t::AbstractTensorMap, (p₁, p₂)::Index2Tuple; kwargs...)
return rightnull!(permute(t, (p₁, p₂); copy=true); kwargs...)
end
"""
eigen(t::AbstractTensor, (leftind, rightind)::Index2Tuple; kwargs...) -> D, V
Compute eigenvalue factorization of tensor `t` as linear map from `rightind` to `leftind`.
If `leftind` and `rightind` are not specified, the current partition of left and right
indices of `t` is used. In that case, less memory is allocated if one allows the data in `t`
to be destroyed/overwritten, by using `eigen!(t)`. Note that the permuted tensor on which
`eigen!` is called should have equal domain and codomain, as otherwise the eigenvalue
decomposition is meaningless and cannot satisfy
```
permute(t, (leftind, rightind)) * V = V * D
```
Accepts the same keyword arguments `scale` and `permute` as `eigen` of dense
matrices. See the corresponding documentation for more information.
See also `eig` and `eigh`
"""
function LinearAlgebra.eigen(t::AbstractTensorMap, (p₁, p₂)::Index2Tuple;
kwargs...)
return eigen!(permute(t, (p₁, p₂); copy=true); kwargs...)
end
"""
eig(t::AbstractTensor, (leftind, rightind)::Index2Tuple; kwargs...) -> D, V
Compute eigenvalue factorization of tensor `t` as linear map from `rightind` to `leftind`.
The function `eig` assumes that the linear map is not hermitian and returns type stable
complex valued `D` and `V` tensors for both real and complex valued `t`. See `eigh` for
hermitian linear maps
If `leftind` and `rightind` are not specified, the current partition of left and right
indices of `t` is used. In that case, less memory is allocated if one allows the data in
`t` to be destroyed/overwritten, by using `eig!(t)`. Note that the permuted tensor on
which `eig!` is called should have equal domain and codomain, as otherwise the eigenvalue
decomposition is meaningless and cannot satisfy
```
permute(t, (leftind, rightind)) * V = V * D
```
Accepts the same keyword arguments `scale` and `permute` as `eigen` of dense
matrices. See the corresponding documentation for more information.
See also `eigen` and `eigh`.
"""
function eig(t::AbstractTensorMap, (p₁, p₂)::Index2Tuple; kwargs...)
return eig!(permute(t, (p₁, p₂); copy=true); kwargs...)
end
"""
eigh(t::AbstractTensorMap, (leftind, rightind)::Index2Tuple) -> D, V
Compute eigenvalue factorization of tensor `t` as linear map from `rightind` to `leftind`.
The function `eigh` assumes that the linear map is hermitian and `D` and `V` tensors with
the same `scalartype` as `t`. See `eig` and `eigen` for non-hermitian tensors. Hermiticity
requires that the tensor acts on inner product spaces, and the current implementation
requires `InnerProductStyle(t) === EuclideanProduct()`.
If `leftind` and `rightind` are not specified, the current partition of left and right
indices of `t` is used. In that case, less memory is allocated if one allows the data in
`t` to be destroyed/overwritten, by using `eigh!(t)`. Note that the permuted tensor on
which `eigh!` is called should have equal domain and codomain, as otherwise the eigenvalue
decomposition is meaningless and cannot satisfy
```
permute(t, (leftind, rightind)) * V = V * D
```
See also `eigen` and `eig`.
"""
function eigh(t::AbstractTensorMap, (p₁, p₂)::Index2Tuple)
return eigh!(permute(t, (p₁, p₂); copy=true))
end
"""
isposdef(t::AbstractTensor, (leftind, rightind)::Index2Tuple) -> ::Bool
Test whether a tensor `t` is positive definite as linear map from `rightind` to `leftind`.
If `leftind` and `rightind` are not specified, the current partition of left and right
indices of `t` is used. In that case, less memory is allocated if one allows the data in
`t` to be destroyed/overwritten, by using `isposdef!(t)`. Note that the permuted tensor on
which `isposdef!` is called should have equal domain and codomain, as otherwise it is
meaningless.
"""
function LinearAlgebra.isposdef(t::AbstractTensorMap, (p₁, p₂)::Index2Tuple)
return isposdef!(permute(t, (p₁, p₂); copy=true))
end
function tsvd(t::AbstractTensorMap; trunc::TruncationScheme=NoTruncation(),
p::Real=2, alg::Union{SVD,SDD}=SDD())
return tsvd!(copy(t); trunc=trunc, p=p, alg=alg)
end
function leftorth(t::AbstractTensorMap; alg::OFA=QRpos(), kwargs...)
return leftorth!(copy(t); alg=alg, kwargs...)
end
function rightorth(t::AbstractTensorMap; alg::OFA=LQpos(), kwargs...)
return rightorth!(copy(t); alg=alg, kwargs...)
end
function leftnull(t::AbstractTensorMap; alg::OFA=QR(), kwargs...)
return leftnull!(copy(t); alg=alg, kwargs...)
end
function rightnull(t::AbstractTensorMap; alg::OFA=LQ(), kwargs...)
return rightnull!(copy(t); alg=alg, kwargs...)
end
LinearAlgebra.eigen(t::AbstractTensorMap; kwargs...) = eigen!(copy(t); kwargs...)
eig(t::AbstractTensorMap; kwargs...) = eig!(copy(t); kwargs...)
eigh(t::AbstractTensorMap; kwargs...) = eigh!(copy(t); kwargs...)
LinearAlgebra.isposdef(t::AbstractTensorMap) = isposdef!(copy(t))
# Orthogonal factorizations (mutation for recycling memory):
# only correct if Euclidean inner product
#------------------------------------------------------------------------------------------
function leftorth!(t::TensorMap;
alg::Union{QR,QRpos,QL,QLpos,SVD,SDD,Polar}=QRpos(),
atol::Real=zero(float(real(scalartype(t)))),
rtol::Real=(alg ∉ (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
eps(real(float(one(scalartype(t))))) * iszero(atol))
InnerProductStyle(t) === EuclideanProduct() || throw_invalid_innerproduct(:leftorth!)
if !iszero(rtol)
atol = max(atol, rtol * norm(t))
end
I = sectortype(t)
S = spacetype(t)
A = storagetype(t)
Qdata = SectorDict{I,A}()
Rdata = SectorDict{I,A}()
dims = SectorDict{I,Int}()
for c in blocksectors(domain(t))
isempty(block(t, c)) && continue
Q, R = MatrixAlgebra.leftorth!(block(t, c), alg, atol)
Qdata[c] = Q
Rdata[c] = R
dims[c] = size(Q, 2)
end
V = S(dims)
if alg isa Polar
@assert V ≅ domain(t)
W = domain(t)
elseif length(domain(t)) == 1 && domain(t) ≅ V
W = domain(t)
elseif length(codomain(t)) == 1 && codomain(t) ≅ V
W = codomain(t)
else
W = ProductSpace(V)
end
return TensorMap(Qdata, codomain(t) ← W), TensorMap(Rdata, W ← domain(t))
end
function leftnull!(t::TensorMap;
alg::Union{QR,QRpos,SVD,SDD}=QRpos(),
atol::Real=zero(float(real(scalartype(t)))),
rtol::Real=(alg ∉ (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
eps(real(float(one(scalartype(t))))) * iszero(atol))
InnerProductStyle(t) === EuclideanProduct() || throw_invalid_innerproduct(:leftnull!)
if !iszero(rtol)
atol = max(atol, rtol * norm(t))
end
I = sectortype(t)
S = spacetype(t)
A = storagetype(t)
V = codomain(t)
Ndata = SectorDict{I,A}()
dims = SectorDict{I,Int}()
for c in blocksectors(V)
N = MatrixAlgebra.leftnull!(block(t, c), alg, atol)
Ndata[c] = N
dims[c] = size(N, 2)
end
W = S(dims)
return TensorMap(Ndata, V ← W)
end
function rightorth!(t::TensorMap;
alg::Union{LQ,LQpos,RQ,RQpos,SVD,SDD,Polar}=LQpos(),
atol::Real=zero(float(real(scalartype(t)))),
rtol::Real=(alg ∉ (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
eps(real(float(one(scalartype(t))))) * iszero(atol))
InnerProductStyle(t) === EuclideanProduct() || throw_invalid_innerproduct(:rightorth!)
if !iszero(rtol)
atol = max(atol, rtol * norm(t))
end
I = sectortype(t)
S = spacetype(t)
A = storagetype(t)
Ldata = SectorDict{I,A}()
Qdata = SectorDict{I,A}()
dims = SectorDict{I,Int}()
for c in blocksectors(codomain(t))
isempty(block(t, c)) && continue
L, Q = MatrixAlgebra.rightorth!(block(t, c), alg, atol)
Ldata[c] = L
Qdata[c] = Q
dims[c] = size(Q, 1)
end
V = S(dims)
if alg isa Polar
@assert V ≅ codomain(t)
W = codomain(t)
elseif length(codomain(t)) == 1 && codomain(t) ≅ V
W = codomain(t)
elseif length(domain(t)) == 1 && domain(t) ≅ V
W = domain(t)
else
W = ProductSpace(V)
end
return TensorMap(Ldata, codomain(t) ← W), TensorMap(Qdata, W ← domain(t))
end
function rightnull!(t::TensorMap;
alg::Union{LQ,LQpos,SVD,SDD}=LQpos(),
atol::Real=zero(float(real(scalartype(t)))),
rtol::Real=(alg ∉ (SVD(), SDD())) ? zero(float(real(scalartype(t)))) :
eps(real(float(one(scalartype(t))))) * iszero(atol))
InnerProductStyle(t) === EuclideanProduct() || throw_invalid_innerproduct(:rightnull!)
if !iszero(rtol)
atol = max(atol, rtol * norm(t))
end
I = sectortype(t)
S = spacetype(t)
A = storagetype(t)
V = domain(t)
Ndata = SectorDict{I,A}()
dims = SectorDict{I,Int}()
for c in blocksectors(V)
N = MatrixAlgebra.rightnull!(block(t, c), alg, atol)
Ndata[c] = N
dims[c] = size(N, 1)
end
W = S(dims)
return TensorMap(Ndata, W ← V)
end
function leftorth!(t::AdjointTensorMap; alg::OFA=QRpos())
InnerProductStyle(t) === EuclideanProduct() || throw_invalid_innerproduct(:leftorth!)
return map(adjoint, reverse(rightorth!(adjoint(t); alg=alg')))
end
function rightorth!(t::AdjointTensorMap; alg::OFA=LQpos())
InnerProductStyle(t) === EuclideanProduct() || throw_invalid_innerproduct(:rightorth!)
return map(adjoint, reverse(leftorth!(adjoint(t); alg=alg')))
end
function leftnull!(t::AdjointTensorMap; alg::OFA=QR(), kwargs...)
InnerProductStyle(t) === EuclideanProduct() || throw_invalid_innerproduct(:leftnull!)
return adjoint(rightnull!(adjoint(t); alg=alg', kwargs...))
end
function rightnull!(t::AdjointTensorMap; alg::OFA=LQ(), kwargs...)
InnerProductStyle(t) === EuclideanProduct() || throw_invalid_innerproduct(:rightnull!)
return adjoint(leftnull!(adjoint(t); alg=alg', kwargs...))
end
#------------------------------#
# Singular value decomposition #
#------------------------------#
function tsvd!(t::AdjointTensorMap;
trunc::TruncationScheme=NoTruncation(),
p::Real=2,
alg::Union{SVD,SDD}=SDD(),
scheduler::Scheduler=default_scheduler(t))
u, s, vt, err = tsvd!(adjoint(t); trunc, p, alg, scheduler)
return adjoint(vt), adjoint(s), adjoint(u), err
end
function tsvd!(t::TensorMap;
trunc::TruncationScheme=NoTruncation(), p::Real=2,
alg::Union{SVD,SDD}=SDD(),
scheduler::Scheduler=default_scheduler(t))
InnerProductStyle(t) === EuclideanProduct() || throw_invalid_innerproduct(:tsvd!)
# early return
if isempty(blocksectors(t))
truncerr = zero(real(scalartype(t)))
return _empty_svdtensors(t)..., truncerr
end
S = spacetype(t)
Udata, Σdata, Vdata, dims = _compute_svddata!(t, alg; scheduler)
if !isa(trunc, NoTruncation)
Σdata, truncerr = _truncate!(Σdata, trunc, p)
Udata, Σdata, Vdata, dims = _implement_svdtruncation!(t, Udata, Σdata, Vdata, dims)
W = S(dims)
else
truncerr = abs(zero(scalartype(t)))
W = S(dims)
if length(domain(t)) == 1 && domain(t)[1] ≅ W
W = domain(t)[1]
elseif length(codomain(t)) == 1 && codomain(t)[1] ≅ W
W = codomain(t)[1]
end
end
return _create_svdtensors(t, Udata, Σdata, Vdata, W)..., truncerr
end
# helper functions
function _compute_svddata!(t::TensorMap, alg::Union{SVD,SDD};
scheduler::Scheduler=default_scheduler(t))
Tdata = blocks(t)
Tkeys = keys(Tdata)
Tvals = values(Tdata)
Uvals = similar(Tvals)
Σtype = Core.Compiler.return_type(similar,
Tuple{eltype(Tvals),Type{real(scalartype(t))},Int})
Σvals = similar(Tvals, Σtype)
Vvals = similar(Tvals)
dimsvals = similar(Tvals, Int)
tforeach(enumerate(Tvals); scheduler) do (i, b)
Uvals[i], Σvals[i], Vvals[i] = MatrixAlgebra.svd!(b, alg)
return dimsvals[i] = length(Σvals[i])
end
# TODO: do we need copys of the keys?
Udata = SectorDict{eltype(Tkeys),eltype(Uvals)}(copy(Tkeys), Uvals)
Σdata = SectorDict{eltype(Tkeys),eltype(Σvals)}(copy(Tkeys), Σvals)
Vdata = SectorDict{eltype(Tkeys),eltype(Vvals)}(copy(Tkeys), Vvals)
dims = SectorDict{eltype(Tkeys),eltype(dimsvals)}(copy(Tkeys), dimsvals)
return Udata, Σdata, Vdata, dims
end
# scheduler ignored for trivial tensormap
function _compute_svddata!(t::TrivialTensorMap, alg::Union{SVD,SDD};
scheduler::Scheduler=default_scheduler(t))
U, S, V = MatrixAlgebra.svd!(t.data, alg)
Udata = SectorDict(Trivial() => U)
Σdata = SectorDict(Trivial() => S)
Vdata = SectorDict(Trivial() => V)
dims = SectorDict(Trivial() => length(S))
return Udata, Σdata, Vdata, dims
end
function _implement_svdtruncation!(t, Udata, Σdata, Vdata, dims)
for c in blocksectors(t)
truncdim = length(Σdata[c])
if truncdim == 0
delete!(Udata, c)
delete!(Vdata, c)
delete!(Σdata, c)
delete!(dims, c)
elseif truncdim < dims[c]
dims[c] = truncdim
Udata[c] = Udata[c][:, 1:truncdim]
Vdata[c] = Vdata[c][1:truncdim, :]
end
end
return Udata, Σdata, Vdata, dims
end
function _empty_svdtensors(t)
I = sectortype(t)
S = spacetype(t)
A = storagetype(t)
Ar = similarstoragetype(t, real(scalartype(t)))
Udata = SectorDict{I,A}()
Σmdata = SectorDict{I,Ar}()
Vdata = SectorDict{I,A}()
dims = SectorDict{I,Int}()
W = S(dims)
return TensorMap(Udata, codomain(t) ← W), TensorMap(Σmdata, W ← W),
TensorMap(Vdata, W ← domain(t))
end
function _create_svdtensors(t, Udata, Σdata, Vdata, W)
I = sectortype(t)
Ar = similarstoragetype(t, real(scalartype(t)))
Σmdata = SectorDict{I,Ar}() # this will contain the singular values as matrix
for (c, Σ) in Σdata
Σmdata[c] = copyto!(similar(Σ, length(Σ), length(Σ)), Diagonal(Σ))
end
return TensorMap(Udata, codomain(t) ← W), TensorMap(Σmdata, W ← W),
TensorMap(Vdata, W ← domain(t))
end
#--------------------------#
# Eigenvalue decomposition #
#--------------------------#
LinearAlgebra.eigen!(t::TensorMap) = ishermitian(t) ? eigh!(t) : eig!(t)
function eigh!(t::TensorMap)
InnerProductStyle(t) === EuclideanProduct() || throw_invalid_innerproduct(:eigh!)
domain(t) == codomain(t) ||
throw(SpaceMismatch("`eigh!` requires domain and codomain to be the same"))
S = spacetype(t)
I = sectortype(t)
A = storagetype(t)
Ar = similarstoragetype(t, real(scalartype(t)))
Ddata = SectorDict{I,Ar}()
Vdata = SectorDict{I,A}()
dims = SectorDict{I,Int}()
for (c, b) in blocks(t)
values, vectors = MatrixAlgebra.eigh!(b)
d = length(values)
Ddata[c] = copyto!(similar(values, (d, d)), Diagonal(values))
Vdata[c] = vectors
dims[c] = d
end
if length(domain(t)) == 1
W = domain(t)[1]
else
W = S(dims)
end
return TensorMap(Ddata, W ← W), TensorMap(Vdata, domain(t) ← W)
end
function eig!(t::TensorMap; kwargs...)
domain(t) == codomain(t) ||
throw(SpaceMismatch("`eig!` requires domain and codomain to be the same"))
S = spacetype(t)
I = sectortype(t)
T = complex(scalartype(t))
Ac = similarstoragetype(t, T)
Ddata = SectorDict{I,Ac}()
Vdata = SectorDict{I,Ac}()
dims = SectorDict{I,Int}()
for (c, b) in blocks(t)
values, vectors = MatrixAlgebra.eig!(b; kwargs...)
d = length(values)
Ddata[c] = copy!(similar(values, T, (d, d)), Diagonal(values))
Vdata[c] = vectors
dims[c] = d
end
if length(domain(t)) == 1
W = domain(t)[1]
else
W = S(dims)
end
return TensorMap(Ddata, W ← W), TensorMap(Vdata, domain(t) ← W)
end
#--------------------------------------------------#
# Checks for hermiticity and positive definiteness #
#--------------------------------------------------#
function LinearAlgebra.ishermitian(t::TensorMap)
domain(t) == codomain(t) || return false
InnerProductStyle(t) === EuclideanProduct() || return false # hermiticity only defined for euclidean
for (c, b) in blocks(t)
ishermitian(b) || return false
end
return true
end
function LinearAlgebra.isposdef!(t::TensorMap)
domain(t) == codomain(t) ||
throw(SpaceMismatch("`isposdef` requires domain and codomain to be the same"))
InnerProductStyle(spacetype(t)) === EuclideanProduct() || return false
for (c, b) in blocks(t)
isposdef!(b) || return false
end
return true
end