forked from QuantumKitHub/MatrixAlgebraKit.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruncation.jl
More file actions
193 lines (164 loc) · 6.48 KB
/
truncation.jl
File metadata and controls
193 lines (164 loc) · 6.48 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
"""
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
# TODO: how do we deal with sorting/filters that treat zeros differently
# since these are implicitly discarded by selecting compact/full
"""
TruncationKeepSorted(howmany::Int, sortby::Function, rev::Bool)
Truncation strategy to keep the first `howmany` values when sorted according to `sortby` or the last `howmany` if `rev` is true.
"""
struct TruncationKeepSorted{F} <: TruncationStrategy
howmany::Int
sortby::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
end
TruncationKeepAbove(atol::Real, rtol::Real) = TruncationKeepAbove(promote(atol, rtol)...)
struct TruncationKeepBelow{T<:Real} <: TruncationStrategy
atol::T
rtol::T
end
TruncationKeepBelow(atol::Real, rtol::Real) = TruncationKeepBelow(promote(atol, rtol)...)
# 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(trunc1::TruncationStrategy, trunc2::TruncationStrategy)
Compose two truncation strategies, keeping values common between the two strategies.
"""
struct TruncationIntersection{T<:Tuple{Vararg{TruncationStrategy}}} <:
TruncationStrategy
components::T
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(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)
sorted = sortperm(values; by=strategy.sortby, rev=strategy.rev)
howmany = min(strategy.howmany, length(sorted))
ind = sorted[1:howmany]
return ind # TODO: consider sort!(ind)
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 * first(values))
i = @something findfirst(≤(atol), values) length(values) + 1
return i:length(values)
end
function findtruncated(values::AbstractVector, strategy::TruncationKeepAbove)
atol = max(strategy.atol, strategy.rtol * first(values))
i = @something findlast(≥(atol), values) 0
return 1:i
end
function findtruncated(values::AbstractVector, strategy::TruncationIntersection)
inds = map(Base.Fix1(findtruncated, values), strategy.components)
return intersect(inds...)
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