-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathmatrixalgebrakit.jl
More file actions
191 lines (171 loc) · 6.64 KB
/
matrixalgebrakit.jl
File metadata and controls
191 lines (171 loc) · 6.64 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
# Algorithm selection
# -------------------
for f in
[
:svd_compact, :svd_full, :svd_vals,
:qr_compact, :qr_full, :qr_null,
:lq_compact, :lq_full, :lq_null,
:eig_full, :eig_vals, :eigh_full, :eigh_vals,
:left_polar, :right_polar,
:project_hermitian, :project_antihermitian, :project_isometric,
]
f! = Symbol(f, :!)
@eval function MAK.default_algorithm(::typeof($f!), ::Type{T}; kwargs...) where {T <: AbstractTensorMap}
return MAK.default_algorithm($f!, blocktype(T); kwargs...)
end
@eval function MAK.copy_input(::typeof($f), t::AbstractTensorMap)
return copy_oftype(t, factorisation_scalartype($f, t))
end
end
_select_truncation(f, ::AbstractTensorMap, trunc::TruncationStrategy) = trunc
function _select_truncation(::typeof(left_null!), ::AbstractTensorMap, trunc::NamedTuple)
return MAK.null_truncation_strategy(; trunc...)
end
# Generic Implementations
# -----------------------
for f! in (
:qr_compact!, :qr_full!, :lq_compact!, :lq_full!,
:eig_full!, :eigh_full!, :svd_compact!, :svd_full!,
:left_polar!, :right_polar!,
)
@eval function MAK.$f!(t::AbstractTensorMap, F, alg::AbstractAlgorithm)
$(f! in (:eig_full!, :eigh_full!) && :(LinearAlgebra.checksquare(t)))
foreachblock(t, F...) do _, (tblock, Fblocks...)
Fblocks′ = $f!(tblock, Fblocks, alg)
# deal with the case where the output is not in-place
for (b′, b) in zip(Fblocks′, Fblocks)
b === b′ || copy!(b, b′)
end
return nothing
end
return F
end
end
# Handle these separately because single output instead of tuple
for f! in (
:qr_null!, :lq_null!,
:svd_vals!, :eig_vals!, :eigh_vals!,
:project_hermitian!, :project_antihermitian!, :project_isometric!,
)
@eval function MAK.$f!(t::AbstractTensorMap, N, alg::AbstractAlgorithm)
$(f! in (:eig_vals!, :eigh_vals!, :project_hermitian!, :project_antihermitian!) && :(LinearAlgebra.checksquare(t)))
foreachblock(t, N) do _, (tblock, Nblock)
Nblock′ = $f!(tblock, Nblock, alg)
# deal with the case where the output is not the same as the input
Nblock === Nblock′ || copy!(Nblock, Nblock′)
return nothing
end
return N
end
end
MAK.zero!(t::AbstractTensorMap) = zerovector!(t)
# Singular value decomposition
# ----------------------------
function MAK.initialize_output(::typeof(svd_full!), t::AbstractTensorMap, ::AbstractAlgorithm)
V_cod = fuse(codomain(t))
V_dom = fuse(domain(t))
U = similar(t, codomain(t) ← V_cod)
S = similar(t, real(scalartype(t)), V_cod ← V_dom)
Vᴴ = similar(t, V_dom ← domain(t))
return U, S, Vᴴ
end
function MAK.initialize_output(::typeof(svd_compact!), t::AbstractTensorMap, ::AbstractAlgorithm)
V_cod = V_dom = infimum(fuse(codomain(t)), fuse(domain(t)))
U = similar(t, codomain(t) ← V_cod)
S = similar_diagonal(t, real(scalartype(t)), V_cod)
Vᴴ = similar(t, V_dom ← domain(t))
return U, S, Vᴴ
end
function MAK.initialize_output(::typeof(svd_vals!), t::AbstractTensorMap, alg::AbstractAlgorithm)
V_cod = infimum(fuse(codomain(t)), fuse(domain(t)))
T = real(scalartype(t))
A = similarstoragetype(t, T)
return SectorVector{T, sectortype(t), A}(undef, V_cod)
end
# Eigenvalue decomposition
# ------------------------
function MAK.initialize_output(::typeof(eigh_full!), t::AbstractTensorMap, ::AbstractAlgorithm)
V_D = fuse(domain(t))
D = similar_diagonal(t, real(scalartype(t)), V_D)
V = similar(t, codomain(t) ← V_D)
return D, V
end
function MAK.initialize_output(::typeof(eig_full!), t::AbstractTensorMap, ::AbstractAlgorithm)
V_D = fuse(domain(t))
Tc = complex(scalartype(t))
D = similar_diagonal(t, Tc, V_D)
V = similar(t, Tc, codomain(t) ← V_D)
return D, V
end
function MAK.initialize_output(::typeof(eigh_vals!), t::AbstractTensorMap, alg::AbstractAlgorithm)
V_D = fuse(domain(t))
T = real(scalartype(t))
A = similarstoragetype(t, T)
return SectorVector{T, sectortype(t), A}(undef, V_D)
end
function MAK.initialize_output(::typeof(eig_vals!), t::AbstractTensorMap, alg::AbstractAlgorithm)
V_D = fuse(domain(t))
Tc = complex(scalartype(t))
A = similarstoragetype(t, Tc)
return SectorVector{Tc, sectortype(t), A}(undef, V_D)
end
# QR decomposition
# ----------------
function MAK.initialize_output(::typeof(qr_full!), t::AbstractTensorMap, ::AbstractAlgorithm)
V_Q = fuse(codomain(t))
Q = similar(t, codomain(t) ← V_Q)
R = similar(t, V_Q ← domain(t))
return Q, R
end
function MAK.initialize_output(::typeof(qr_compact!), t::AbstractTensorMap, ::AbstractAlgorithm)
V_Q = infimum(fuse(codomain(t)), fuse(domain(t)))
Q = similar(t, codomain(t) ← V_Q)
R = similar(t, V_Q ← domain(t))
return Q, R
end
function MAK.initialize_output(::typeof(qr_null!), t::AbstractTensorMap, ::AbstractAlgorithm)
V_Q = infimum(fuse(codomain(t)), fuse(domain(t)))
V_N = ⊖(fuse(codomain(t)), V_Q)
N = similar(t, codomain(t) ← V_N)
return N
end
# LQ decomposition
# ----------------
function MAK.initialize_output(::typeof(lq_full!), t::AbstractTensorMap, ::AbstractAlgorithm)
V_Q = fuse(domain(t))
L = similar(t, codomain(t) ← V_Q)
Q = similar(t, V_Q ← domain(t))
return L, Q
end
function MAK.initialize_output(::typeof(lq_compact!), t::AbstractTensorMap, ::AbstractAlgorithm)
V_Q = infimum(fuse(codomain(t)), fuse(domain(t)))
L = similar(t, codomain(t) ← V_Q)
Q = similar(t, V_Q ← domain(t))
return L, Q
end
function MAK.initialize_output(::typeof(lq_null!), t::AbstractTensorMap, ::AbstractAlgorithm)
V_Q = infimum(fuse(codomain(t)), fuse(domain(t)))
V_N = ⊖(fuse(domain(t)), V_Q)
N = similar(t, V_N ← domain(t))
return N
end
# Polar decomposition
# -------------------
function MAK.initialize_output(::typeof(left_polar!), t::AbstractTensorMap, ::AbstractAlgorithm)
W = similar(t, space(t))
P = similar(t, domain(t) ← domain(t))
return W, P
end
function MAK.initialize_output(::typeof(right_polar!), t::AbstractTensorMap, ::AbstractAlgorithm)
P = similar(t, codomain(t) ← codomain(t))
Wᴴ = similar(t, space(t))
return P, Wᴴ
end
# Projections
# -----------
MAK.initialize_output(::typeof(project_hermitian!), tsrc::AbstractTensorMap, ::AbstractAlgorithm) =
tsrc
MAK.initialize_output(::typeof(project_antihermitian!), tsrc::AbstractTensorMap, ::AbstractAlgorithm) =
tsrc
MAK.initialize_output(::typeof(project_isometric!), tsrc::AbstractTensorMap, ::AbstractAlgorithm) =
similar(tsrc)