-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtruncation.jl
More file actions
276 lines (227 loc) · 9.04 KB
/
truncation.jl
File metadata and controls
276 lines (227 loc) · 9.04 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
const docs_truncation_kwargs = """
* `atol::Real` : Absolute tolerance for the truncation
* `rtol::Real` : Relative tolerance for the truncation
* `maxrank::Integer` : Maximal rank for the truncation
* `minrank::Integer` : Minimal rank for the truncation
* `maxerror::Real` : Maximal truncation error.
* `filter` : Custom filter to select truncated values.
"""
const docs_truncation_strategies = """
- [`notrunc`](@ref)
- [`truncrank`](@ref)
- [`trunctol`](@ref)
- [`truncerror`](@ref)
- [`truncfilter`](@ref)
"""
const docs_null_truncation_kwargs = """
* `atol::Real` : Absolute tolerance for the truncation
* `rtol::Real` : Relative tolerance for the truncation
* `maxnullity::Real` : Maximal rank for the truncation
"""
"""
TruncationStrategy(; kwargs...)
Select a truncation strategy based on the provided keyword arguments.
## Keyword arguments
The following keyword arguments are all optional, and their default value (`nothing`)
will be ignored. It is also allowed to combine multiple of these, in which case the kept
values will consist of the intersection of the different truncated strategies (except
`minrank`, which uses union semantics to guarantee a lower bound on the number of kept values).
$docs_truncation_kwargs
"""
function TruncationStrategy(;
atol::Union{Real, Nothing} = nothing,
rtol::Union{Real, Nothing} = nothing,
maxrank::Union{Integer, Nothing} = nothing,
minrank::Union{Integer, Nothing} = nothing,
maxerror::Union{Real, Nothing} = nothing,
filter = nothing,
)
strategy = notrunc()
if !isnothing(atol) || !isnothing(rtol)
atol = @something atol 0
rtol = @something rtol 0
strategy &= trunctol(; atol, rtol)
end
isnothing(maxrank) || (strategy &= truncrank(maxrank))
isnothing(maxerror) || (strategy &= truncerror(; atol = maxerror))
isnothing(filter) || (strategy &= truncfilter(filter))
# union constraint: guarantee a lower bound on number of kept values
# special-case NoTruncation: keeping everything already satisfies any minrank
if !isnothing(minrank) && !(strategy isa NoTruncation)
strategy |= truncrank(minrank)
elseif !isnothing(minrank)
strategy = truncrank(minrank)
end
return strategy
end
"""
null_truncation_strategy(; kwargs...)
Select a nullspace truncation strategy based on the provided keyword arguments.
## Keyword arguments
The following keyword arguments are all optional, and their default value (`nothing`)
will be ignored. It is also allowed to combine multiple of these, in which case the
discarded values will consist of the intersection of the different truncated strategies.
$docs_null_truncation_kwargs
"""
function null_truncation_strategy(; atol = nothing, rtol = nothing, maxnullity = nothing)
if isnothing(maxnullity) && isnothing(atol) && isnothing(rtol)
return notrunc()
end
atol = @something atol 0
rtol = @something rtol 0
trunc = trunctol(; atol, rtol, keep_below = true)
return !isnothing(maxnullity) ? trunc & truncrank(maxnullity; rev = false) : trunc
end
"""
NoTruncation()
Trivial truncation strategy that keeps all values, mostly for testing purposes.
See also [`notrunc()`](@ref).
"""
struct NoTruncation <: TruncationStrategy end
"""
notrunc()
Truncation strategy that does nothing, and keeps all the values.
"""
notrunc() = NoTruncation()
# TODO: Base.Ordering?
"""
TruncationByOrder(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).
See also [`truncrank`](@ref).
"""
struct TruncationByOrder{F} <: TruncationStrategy
howmany::Int
by::F
rev::Bool
end
"""
truncrank(howmany::Integer; by=abs, rev::Bool=true)
Truncation strategy to keep the first `howmany` values when sorted according to `by` or the last `howmany` if `rev` is true.
"""
function truncrank(howmany::Integer; by = abs, rev::Bool = true)
return TruncationByOrder(howmany, by, rev)
end
"""
TruncationByFilter(filter::Function)
Truncation strategy to keep the values for which `filter` returns true.
See also [`truncfilter`](@ref).
"""
struct TruncationByFilter{F} <: TruncationStrategy
filter::F
end
"""
truncfilter(filter)
Truncation strategy to keep the values for which `filter` returns true.
"""
truncfilter(f) = TruncationByFilter(f)
"""
TruncationByValue(atol::Real, rtol::Real, p::Real, by, keep_below::Bool=false)
Truncation strategy to keep the values that satisfy `by(val) > max(atol, rtol * norm(values, p))`.
If `keep_below = true`, discard these values instead.
See also [`trunctol`](@ref)
"""
struct TruncationByValue{T <: Real, P <: Real, F} <: TruncationStrategy
atol::T
rtol::T
p::P
by::F
keep_below::Bool
end
function TruncationByValue(atol::Real, rtol::Real, p::Real = 2, by = abs, keep_below::Bool = true)
return TruncationByValue(promote(atol, rtol)..., p, by, keep_below)
end
"""
trunctol(; atol::Real=0, rtol::Real=0, p::Real=2, by=abs, keep_below::Bool=false)
Truncation strategy to keep the values that satisfy `by(val) > max(atol, rtol * norm(values, p))`.
If `keep_below = true`, discard these values instead.
"""
function trunctol(; atol::Real = 0, rtol::Real = 0, p::Real = 2, by = abs, keep_below::Bool = false)
return TruncationByValue(atol, rtol, p, by, keep_below)
end
"""
TruncationByError(; atol::Real, rtol::Real, p::Real)
Truncation strategy to discard values until the error caused by the discarded values exceeds some tolerances.
See also [`truncerror`](@ref).
"""
struct TruncationByError{T <: Real, P <: Real} <: TruncationStrategy
atol::T
rtol::T
p::P
end
function TruncationError(atol::Real, rtol::Real, p::Real = 2)
return TruncationError(promote(atol, rtol)..., p)
end
"""
truncerror(; atol::Real=0, rtol::Real=0, p::Real=2)
Truncation strategy for truncating values such that the error in the factorization
is smaller than `max(atol, rtol * norm)`, where the error is determined using the `p`-norm.
"""
function truncerror(; atol::Real = 0, rtol::Real = 0, p::Real = 2)
return TruncationByError(promote(atol, rtol)..., p)
end
"""
TruncationIntersection(trunc::TruncationStrategy, truncs::TruncationStrategy...)
Truncation strategy that composes multiple truncation strategies, keeping values that are
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
# flatten components
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
# drop notrunc
Base.:&(::NoTruncation, trunc::TruncationStrategy) = trunc
Base.:&(trunc::TruncationStrategy, ::NoTruncation) = trunc
Base.:&(::NoTruncation, ::NoTruncation) = notrunc()
# disambiguate
Base.:&(::NoTruncation, trunc::TruncationIntersection) = trunc
Base.:&(trunc::TruncationIntersection, ::NoTruncation) = trunc
"""
TruncationUnion(trunc::TruncationStrategy, truncs::TruncationStrategy...)
Truncation strategy that composes multiple truncation strategies, keeping values that are
present in any of them.
"""
struct TruncationUnion{T <: Tuple{Vararg{TruncationStrategy}}} <: TruncationStrategy
components::T
end
function TruncationUnion(trunc::TruncationStrategy, truncs::TruncationStrategy...)
return TruncationUnion((trunc, truncs...))
end
function Base.:|(trunc1::TruncationStrategy, trunc2::TruncationStrategy)
return TruncationUnion((trunc1, trunc2))
end
# flatten components
function Base.:|(trunc1::TruncationUnion, trunc2::TruncationUnion)
return TruncationUnion((trunc1.components..., trunc2.components...))
end
function Base.:|(trunc1::TruncationUnion, trunc2::TruncationStrategy)
return TruncationUnion((trunc1.components..., trunc2))
end
function Base.:|(trunc1::TruncationStrategy, trunc2::TruncationUnion)
return TruncationUnion((trunc1, trunc2.components...))
end
# NoTruncation is the absorbing element for | (union with "keep all" = keep all)
Base.:|(::NoTruncation, ::TruncationStrategy) = notrunc()
Base.:|(::TruncationStrategy, ::NoTruncation) = notrunc()
Base.:|(::NoTruncation, ::NoTruncation) = notrunc()
# disambiguate
Base.:|(::NoTruncation, ::TruncationUnion) = notrunc()
Base.:|(::TruncationUnion, ::NoTruncation) = notrunc()
@doc """
truncation_error(values, ind)
Compute the truncation error as the 2-norm of the values that are not kept by `ind`.
""" truncation_error, truncation_error!