-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtruncation.jl
More file actions
231 lines (198 loc) · 7.71 KB
/
truncation.jl
File metadata and controls
231 lines (198 loc) · 7.71 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
"""
abstract type TruncationStrategy end
Supertype to denote different strategies for truncated decompositions that are implemented via post-truncation.
See also [`truncate!`](@ref)
"""
abstract type TruncationStrategy end
function TruncationStrategy(; atol=nothing, rtol=nothing, maxrank=nothing)
if isnothing(maxrank) && isnothing(atol) && isnothing(rtol)
return NoTruncation()
elseif isnothing(maxrank)
atol = @something atol 0
rtol = @something rtol 0
return TruncationKeepAbove(atol, rtol)
else
if isnothing(atol) && isnothing(rtol)
return truncrank(maxrank)
else
atol = @something atol 0
rtol = @something rtol 0
return truncrank(maxrank) & TruncationKeepAbove(atol, rtol)
end
end
end
"""
NoTruncation()
Trivial truncation strategy that keeps all values, mostly for testing purposes.
"""
struct NoTruncation <: TruncationStrategy end
function select_truncation(trunc)
if isnothing(trunc)
return NoTruncation()
elseif trunc isa NamedTuple
return TruncationStrategy(; trunc...)
elseif trunc isa TruncationStrategy
return trunc
else
return throw(ArgumentError("Unknown truncation strategy: $trunc"))
end
end
# TODO: how do we deal with sorting/filters that treat zeros differently
# since these are implicitly discarded by selecting compact/full
"""
TruncationKeepSorted(howmany::Int, by::Function, rev::Bool)
Truncation strategy to keep the first `howmany` values when sorted according to `by` in increasing (decreasing) order if `rev` is false (true).
"""
struct TruncationKeepSorted{F} <: TruncationStrategy
howmany::Int
by::F
rev::Bool
end
"""
TruncationKeepFiltered(filter::Function)
Truncation strategy to keep the values for which `filter` returns true.
"""
struct TruncationKeepFiltered{F} <: TruncationStrategy
filter::F
end
struct TruncationKeepAbove{T<:Real} <: TruncationStrategy
atol::T
rtol::T
p::Int
end
function TruncationKeepAbove(atol::Real, rtol::Real, p::Int=2)
return TruncationKeepAbove(promote(atol, rtol)..., p)
end
struct TruncationKeepBelow{T<:Real} <: TruncationStrategy
atol::T
rtol::T
p::Int
end
function TruncationKeepBelow(atol::Real, rtol::Real, p::Int=2)
return TruncationKeepBelow(promote(atol, rtol)..., p)
end
# TODO: better names for these functions of the above types
"""
truncrank(howmany::Int; by=abs, rev=true)
Truncation strategy to keep the first `howmany` values when sorted according to `by` or the last `howmany` if `rev` is true.
"""
truncrank(howmany::Int; by=abs, rev=true) = TruncationKeepSorted(howmany, by, rev)
"""
trunctol(atol::Real)
Truncation strategy to discard the values that are smaller than `atol` in absolute value.
"""
trunctol(atol) = TruncationKeepFiltered(≥(atol) ∘ abs)
"""
truncabove(atol::Real)
Truncation strategy to discard the values that are larger than `atol` in absolute value.
"""
truncabove(atol) = TruncationKeepFiltered(≤(atol) ∘ abs)
"""
TruncationIntersection(trunc::TruncationStrategy, truncs::TruncationStrategy...)
Composition of multiple truncation strategies, keeping values common between them.
"""
struct TruncationIntersection{T<:Tuple{Vararg{TruncationStrategy}}} <:
TruncationStrategy
components::T
end
function TruncationIntersection(trunc::TruncationStrategy, truncs::TruncationStrategy...)
return TruncationIntersection((trunc, truncs...))
end
function Base.:&(trunc1::TruncationStrategy, trunc2::TruncationStrategy)
return TruncationIntersection((trunc1, trunc2))
end
function Base.:&(trunc1::TruncationIntersection, trunc2::TruncationIntersection)
return TruncationIntersection((trunc1.components..., trunc2.components...))
end
function Base.:&(trunc1::TruncationIntersection, trunc2::TruncationStrategy)
return TruncationIntersection((trunc1.components..., trunc2))
end
function Base.:&(trunc1::TruncationStrategy, trunc2::TruncationIntersection)
return TruncationIntersection((trunc1, trunc2.components...))
end
# truncate!
# ---------
# Generic implementation: `findtruncated` followed by indexing
@doc """
truncate!(f, out, strategy::TruncationStrategy)
Generic interface for post-truncating a decomposition, specified in `out`.
""" truncate!
# TODO: should we return a view?
function truncate!(::typeof(svd_trunc!), (U, S, Vᴴ), strategy::TruncationStrategy)
ind = findtruncated_sorted(diagview(S), strategy)
return U[:, ind], Diagonal(diagview(S)[ind]), Vᴴ[ind, :]
end
function truncate!(::typeof(eig_trunc!), (D, V), strategy::TruncationStrategy)
ind = findtruncated(diagview(D), strategy)
return Diagonal(diagview(D)[ind]), V[:, ind]
end
function truncate!(::typeof(eigh_trunc!), (D, V), strategy::TruncationStrategy)
ind = findtruncated(diagview(D), strategy)
return Diagonal(diagview(D)[ind]), V[:, ind]
end
function truncate!(::typeof(left_null!), (U, S), strategy::TruncationStrategy)
# TODO: avoid allocation?
extended_S = vcat(diagview(S), zeros(eltype(S), max(0, size(S, 1) - size(S, 2))))
ind = findtruncated(extended_S, strategy)
return U[:, ind]
end
function truncate!(::typeof(right_null!), (S, Vᴴ), strategy::TruncationStrategy)
# TODO: avoid allocation?
extended_S = vcat(diagview(S), zeros(eltype(S), max(0, size(S, 2) - size(S, 1))))
ind = findtruncated(extended_S, strategy)
return Vᴴ[ind, :]
end
# findtruncated
# -------------
# specific implementations for finding truncated values
findtruncated(values::AbstractVector, ::NoTruncation) = Colon()
# TODO: this may also permute the eigenvalues, decide if we want to allow this or not
# can be solved by going to simply sorting the resulting `ind`
function findtruncated(values::AbstractVector, strategy::TruncationKeepSorted)
howmany = min(strategy.howmany, length(values))
return partialsortperm(values, 1:howmany; by=strategy.by, rev=strategy.rev)
end
function findtruncated_sorted(values::AbstractVector, strategy::TruncationKeepSorted)
howmany = min(strategy.howmany, length(values))
return 1:howmany
end
# TODO: consider if worth using that values are sorted when filter is `<` or `>`.
function findtruncated(values::AbstractVector, strategy::TruncationKeepFiltered)
ind = findall(strategy.filter, values)
return ind
end
function findtruncated(values::AbstractVector, strategy::TruncationKeepBelow)
atol = max(strategy.atol, strategy.rtol * norm(values, strategy.p))
return findall(≤(atol), values)
end
function findtruncated_sorted(values::AbstractVector, strategy::TruncationKeepBelow)
atol = max(strategy.atol, strategy.rtol * norm(values, strategy.p))
i = searchsortedfirst(values, atol; by=abs, rev=true)
return i:length(values)
end
function findtruncated(values::AbstractVector, strategy::TruncationKeepAbove)
atol = max(strategy.atol, strategy.rtol * norm(values, strategy.p))
return findall(≥(atol), values)
end
function findtruncated_sorted(values::AbstractVector, strategy::TruncationKeepAbove)
atol = max(strategy.atol, strategy.rtol * norm(values, strategy.p))
i = searchsortedlast(values, atol; by=abs, rev=true)
return 1:i
end
function findtruncated(values::AbstractVector, strategy::TruncationIntersection)
inds = map(Base.Fix1(findtruncated, values), strategy.components)
return intersect(inds...)
end
# Generic fallback.
function findtruncated_sorted(values::AbstractVector, strategy::TruncationStrategy)
return findtruncated(values, strategy)
end
"""
TruncatedAlgorithm(alg::AbstractAlgorithm, trunc::TruncationAlgorithm)
Generic wrapper type for algorithms that consist of first using `alg`, followed by a
truncation through `trunc`.
"""
struct TruncatedAlgorithm{A,T} <: AbstractAlgorithm
alg::A
trunc::T
end