-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathtruncation.jl
More file actions
301 lines (272 loc) · 10.9 KB
/
truncation.jl
File metadata and controls
301 lines (272 loc) · 10.9 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
# Strategies
# ----------
"""
TruncationSpace(V::ElementarySpace, by::Function, rev::Bool)
Truncation strategy to keep the first values for each sector when sorted according to `by` and `rev`,
such that the resulting vector space is no greater than `V`.
See also [`truncspace`](@ref).
"""
struct TruncationSpace{S <: ElementarySpace, F} <: TruncationStrategy
space::S
by::F
rev::Bool
end
"""
truncspace(space::ElementarySpace; by=abs, rev::Bool=true)
Truncation strategy to keep the first values for each sector when sorted according to `by` and `rev`,
such that the resulting vector space is no greater than `V`.
"""
function truncspace(space::ElementarySpace; by = abs, rev::Bool = true)
isdual(space) && throw(ArgumentError("truncation space should not be dual"))
return TruncationSpace(space, by, rev)
end
# truncate!
# ---------
_blocklength(d::Integer, ind) = _blocklength(Base.OneTo(d), ind)
_blocklength(ax, ind) = length(ax[ind])
function truncate_space(V::ElementarySpace, inds)
return spacetype(V)(c => _blocklength(dim(V, c), ind) for (c, ind) in inds)
end
function truncate_domain!(tdst::AbstractTensorMap, tsrc::AbstractTensorMap, inds)
for (c, b) in blocks(tdst)
I = get(inds, c, nothing)
@assert !isnothing(I)
copy!(b, view(block(tsrc, c), :, I))
end
return tdst
end
function truncate_codomain!(tdst::AbstractTensorMap, tsrc::AbstractTensorMap, inds)
for (c, b) in blocks(tdst)
I = get(inds, c, nothing)
@assert !isnothing(I)
copy!(b, view(block(tsrc, c), I, :))
end
return tdst
end
function truncate_diagonal!(Ddst::DiagonalTensorMap, Dsrc::DiagonalTensorMap, inds)
for (c, b) in blocks(Ddst)
I = get(inds, c, nothing)
@assert !isnothing(I)
copy!(diagview(b), view(diagview(block(Dsrc, c)), I))
end
return Ddst
end
function MAK.truncate(
::typeof(svd_trunc!), (U, S, Vᴴ)::NTuple{3, AbstractTensorMap},
strategy::TruncationStrategy
)
ind = MAK.findtruncated_svd(diagview(S), strategy)
V_truncated = truncate_space(space(S, 1), ind)
Ũ = similar(U, codomain(U) ← V_truncated)
truncate_domain!(Ũ, U, ind)
S̃ = similar_diagonal(S, V_truncated)
truncate_diagonal!(S̃, S, ind)
Ṽᴴ = similar(Vᴴ, V_truncated ← domain(Vᴴ))
truncate_codomain!(Ṽᴴ, Vᴴ, ind)
return (Ũ, S̃, Ṽᴴ), ind
end
function MAK.truncate(
::typeof(left_null!), (U, S)::NTuple{2, AbstractTensorMap}, strategy::TruncationStrategy
)
extended_S = zerovector!(SectorVector{eltype(S)}(undef, fuse(codomain(U))))
for (c, b) in blocks(S)
copyto!(extended_S[c], diagview(b)) # copyto! since `b` might be shorter
end
ind = MAK.findtruncated(extended_S, strategy)
V_truncated = truncate_space(space(S, 1), ind)
Ũ = similar(U, codomain(U) ← V_truncated)
truncate_domain!(Ũ, U, ind)
return Ũ, ind
end
function MAK.truncate(
::typeof(right_null!), (S, Vᴴ)::NTuple{2, AbstractTensorMap}, strategy::TruncationStrategy
)
extended_S = zerovector!(SectorVector{eltype(S)}(undef, fuse(domain(Vᴴ))))
for (c, b) in blocks(S)
copyto!(extended_S[c], diagview(b)) # copyto! since `b` might be shorter
end
ind = MAK.findtruncated(extended_S, strategy)
V_truncated = truncate_space(dual(space(S, 2)), ind)
Ṽᴴ = similar(Vᴴ, V_truncated ← domain(Vᴴ))
truncate_codomain!(Ṽᴴ, Vᴴ, ind)
return Ṽᴴ, ind
end
# special case `NoTruncation` for null: should keep exact zeros due to rectangularity
# need to specialize to avoid ambiguity with special case in MatrixAlgebraKit
function MAK.truncate(
::typeof(left_null!), (U, S)::NTuple{2, AbstractTensorMap}, strategy::NoTruncation
)
ind = SectorDict(c => (size(b, 2) + 1):size(b, 1) for (c, b) in blocks(S))
V_truncated = truncate_space(space(S, 1), ind)
Ũ = similar(U, codomain(U) ← V_truncated)
truncate_domain!(Ũ, U, ind)
return Ũ, ind
end
function MAK.truncate(
::typeof(right_null!), (S, Vᴴ)::NTuple{2, AbstractTensorMap}, strategy::NoTruncation
)
ind = SectorDict(c => (size(b, 1) + 1):size(b, 2) for (c, b) in blocks(S))
V_truncated = truncate_space(dual(space(S, 2)), ind)
Ṽᴴ = similar(Vᴴ, V_truncated ← domain(Vᴴ))
truncate_codomain!(Ṽᴴ, Vᴴ, ind)
return Ṽᴴ, ind
end
for f! in (:eig_trunc!, :eigh_trunc!)
@eval function MAK.truncate(
::typeof($f!),
(D, V)::Tuple{DiagonalTensorMap, AbstractTensorMap},
strategy::TruncationStrategy
)
ind = MAK.findtruncated(diagview(D), strategy)
V_truncated = truncate_space(space(D, 1), ind)
D̃ = similar_diagonal(D, V_truncated)
truncate_diagonal!(D̃, D, ind)
Ṽ = similar(V, codomain(V) ← V_truncated)
truncate_domain!(Ṽ, V, ind)
return (D̃, Ṽ), ind
end
end
# Find truncation
# ---------------
# auxiliary functions
rtol_to_atol(S, p, atol, rtol) = rtol == 0 ? atol : max(atol, norm(S, p) * rtol)
function _compute_truncerr(Σdata, truncdim, p = 2)
I = keytype(Σdata)
S = scalartype(valtype(Σdata))
return TensorKit._norm(
(c => @view(v[(get(truncdim, c, 0) + 1):end]) for (c, v) in Σdata),
p, zero(S)
)
end
function _findnexttruncvalue(
S, truncdim::SectorDict{I, Int}; by = identity, rev::Bool = true
) where {I <: Sector}
# early return
(isempty(S) || all(iszero, values(truncdim))) && return nothing
if rev
σmin, imin = findmin(keys(truncdim)) do c
d = truncdim[c]
return by(S[c][d])
end
return σmin, keys(truncdim)[imin]
else
σmax, imax = findmax(keys(truncdim)) do c
d = truncdim[c]
return by(S[c][d])
end
return σmax, keys(truncdim)[imax]
end
end
function _sort_and_perm(values::SectorVector; by = identity, rev::Bool = false)
values_sorted = similar(values)
perms = SectorDict(
(
begin
p = sortperm(v; by, rev)
vs = values_sorted[c]
vs .= view(v, p)
c => p
end
) for (c, v) in pairs(values)
)
return values_sorted, perms
end
# findtruncated
# -------------
# Generic fallback
function MAK.findtruncated_svd(values::SectorVector, strategy::TruncationStrategy)
return MAK.findtruncated(values, strategy)
end
function MAK.findtruncated(values::SectorVector, ::NoTruncation)
return SectorDict(c => Colon() for c in keys(values))
end
function MAK.findtruncated(values::SectorVector, strategy::TruncationByOrder)
values_sorted, perms = _sort_and_perm(values; strategy.by, strategy.rev)
inds = MAK.findtruncated_svd(values_sorted, truncrank(strategy.howmany))
return SectorDict(c => perms[c][I] for (c, I) in inds)
end
function MAK.findtruncated_svd(values::SectorVector, strategy::TruncationByOrder)
I = keytype(values)
truncdim = SectorDict{I, Int}(c => length(d) for (c, d) in pairs(values))
totaldim = sum(dim(c) * d for (c, d) in truncdim; init = 0)
while totaldim > strategy.howmany
next = _findnexttruncvalue(values, truncdim; strategy.by, strategy.rev)
isnothing(next) && break
_, cmin = next
truncdim[cmin] -= 1
totaldim -= dim(cmin)
truncdim[cmin] == 0 && delete!(truncdim, cmin)
end
return SectorDict(c => Base.OneTo(d) for (c, d) in truncdim)
end
function MAK.findtruncated(values::SectorVector, strategy::TruncationByFilter)
return SectorDict(c => findall(strategy.filter, d) for (c, d) in pairs(values))
end
function MAK.findtruncated(values::SectorVector, strategy::TruncationByValue)
atol = rtol_to_atol(values, strategy.p, strategy.atol, strategy.rtol)
strategy′ = trunctol(; atol, strategy.by, strategy.keep_below)
return SectorDict(c => MAK.findtruncated(d, strategy′) for (c, d) in pairs(values))
end
function MAK.findtruncated_svd(values::SectorVector, strategy::TruncationByValue)
atol = rtol_to_atol(values, strategy.p, strategy.atol, strategy.rtol)
strategy′ = trunctol(; atol, strategy.by, strategy.keep_below)
return SectorDict(c => MAK.findtruncated_svd(d, strategy′) for (c, d) in pairs(values))
end
function MAK.findtruncated(values::SectorVector, strategy::TruncationByError)
values_sorted, perms = _sort_and_perm(values; strategy.by, strategy.rev)
inds = MAK.findtruncated_svd(values_sorted, truncrank(strategy.howmany))
return SectorDict(c => perms[c][I] for (c, I) in inds)
end
function MAK.findtruncated_svd(values::SectorVector, strategy::TruncationByError)
I = keytype(values)
truncdim = SectorDict{I, Int}(c => length(d) for (c, d) in pairs(values))
by(c, v) = abs(v)^strategy.p * dim(c)
Nᵖ = sum(((c, v),) -> sum(Base.Fix1(by, c), v), pairs(values))
ϵᵖ = max(strategy.atol^strategy.p, strategy.rtol^strategy.p * Nᵖ)
truncerrᵖ = zero(real(scalartype(valtype(values))))
next = _findnexttruncvalue(values, truncdim)
while !isnothing(next)
σmin, cmin = next
truncerrᵖ += by(cmin, σmin)
truncerrᵖ >= ϵᵖ && break
(truncdim[cmin] -= 1) == 0 && delete!(truncdim, cmin)
next = _findnexttruncvalue(values, truncdim)
end
return SectorDict{I, Base.OneTo{Int}}(c => Base.OneTo(d) for (c, d) in truncdim)
end
function MAK.findtruncated(values::SectorVector, strategy::TruncationSpace)
blockstrategy(c) = truncrank(dim(strategy.space, c); strategy.by, strategy.rev)
return SectorDict(c => MAK.findtruncated(d, blockstrategy(c)) for (c, d) in values)
end
function MAK.findtruncated_svd(values::SectorVector, strategy::TruncationSpace)
blockstrategy(c) = truncrank(dim(strategy.space, c); strategy.by, strategy.rev)
return SectorDict(c => MAK.findtruncated_svd(d, blockstrategy(c)) for (c, d) in pairs(values))
end
function MAK.findtruncated(values::SectorVector, strategy::TruncationIntersection)
inds = map(Base.Fix1(MAK.findtruncated, values), strategy.components)
return SectorDict(
c => mapreduce(
Base.Fix2(getindex, c), MatrixAlgebraKit._ind_intersect, inds;
init = trues(length(values[c]))
) for c in intersect(map(keys, inds)...)
)
end
function MAK.findtruncated_svd(values::SectorVector, strategy::TruncationIntersection)
inds = map(Base.Fix1(MAK.findtruncated_svd, values), strategy.components)
return SectorDict(
c => mapreduce(
Base.Fix2(getindex, c), MatrixAlgebraKit._ind_intersect, inds;
init = trues(length(values[c]))
) for c in intersect(map(keys, inds)...)
)
end
# Truncation error
# ----------------
MAK.truncation_error(values::SectorVector, ind) = MAK.truncation_error!(copy(values), ind)
function MAK.truncation_error!(values::SectorVector, ind)
for (c, ind_c) in ind
v = values[c]
v[ind_c] .= zero(eltype(v))
end
return norm(values)
end