-
Notifications
You must be signed in to change notification settings - Fork 54
/
krylov_utils.jl
409 lines (335 loc) · 12.5 KB
/
krylov_utils.jl
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
export kstdout
"Default I/O stream for all Krylov methods."
const kstdout = Core.stdout
"""
FloatOrComplex{T}
Union type of `T` and `Complex{T}` where T is an `AbstractFloat`.
"""
const FloatOrComplex{T} = Union{T, Complex{T}} where T <: AbstractFloat
"""
(c, s, ρ) = sym_givens(a, b)
Numerically stable symmetric Givens reflection.
Given `a` and `b` reals, return `(c, s, ρ)` such that
[ c s ] [ a ] = [ ρ ]
[ s -c ] [ b ] = [ 0 ].
"""
function sym_givens(a :: T, b :: T) where T <: AbstractFloat
#
# Modeled after the corresponding Matlab function by M. A. Saunders and S.-C. Choi.
# http://www.stanford.edu/group/SOL/dissertations/sou-cheng-choi-thesis.pdf
# D. Orban, Montreal, May 2015.
if b == 0
if a == 0
c = one(T)
else
c = sign(a) # In Julia, sign(0) = 0.
end
s = zero(T)
ρ = abs(a)
elseif a == 0
c = zero(T)
s = sign(b)
ρ = abs(b)
elseif abs(b) > abs(a)
t = a / b
s = sign(b) / sqrt(one(T) + t * t)
c = s * t
ρ = b / s # Computationally better than ρ = a / c since |c| ≤ |s|.
else
t = b / a
c = sign(a) / sqrt(one(T) + t * t)
s = c * t
ρ = a / c # Computationally better than ρ = b / s since |s| ≤ |c|
end
return (c, s, ρ)
end
"""
Numerically stable symmetric Givens reflection.
Given `a` and `b` complexes, return `(c, s, ρ)` with
c real and (s, ρ) complexes such that
[ c s ] [ a ] = [ ρ ]
[ s̅ -c ] [ b ] = [ 0 ].
"""
function sym_givens(a :: Complex{T}, b :: Complex{T}) where T <: AbstractFloat
#
# Modeled after the corresponding Fortran function by M. A. Saunders and S.-C. Choi.
# A. Montoison, Montreal, March 2020.
abs_a = abs(a)
abs_b = abs(b)
if abs_b == 0
c = one(T)
s = zero(Complex{T})
ρ = a
elseif abs_a == 0
c = zero(T)
s = one(Complex{T})
ρ = b
elseif abs_b > abs_a
t = abs_a / abs_b
c = one(T) / sqrt(one(T) + t * t)
s = c * conj((b / abs_b) / (a / abs_a))
c = c * t
ρ = b / conj(s)
else
t = abs_b / abs_a
c = one(T) / sqrt(one(T) + t * t)
s = c * t * conj((b / abs_b) / (a / abs_a))
ρ = a / c
end
return (c, s, ρ)
end
sym_givens(a :: Complex{T}, b :: T) where T <: AbstractFloat = sym_givens(a, Complex{T}(b))
sym_givens(a :: T, b :: Complex{T}) where T <: AbstractFloat = sym_givens(Complex{T}(a), b)
"""
roots = roots_quadratic(q₂, q₁, q₀; nitref)
Find the real roots of the quadratic
q(x) = q₂ x² + q₁ x + q₀,
where q₂, q₁ and q₀ are real. Care is taken to avoid numerical
cancellation. Optionally, `nitref` steps of iterative refinement
may be performed to improve accuracy. By default, `nitref=1`.
"""
function roots_quadratic(q₂ :: T, q₁ :: T, q₀ :: T;
nitref :: Int=1) where T <: AbstractFloat
# Case where q(x) is linear.
if q₂ == zero(T)
if q₁ == zero(T)
q₀ == zero(T) || error("The quadratic `q` doesn't have real roots.")
root = zero(T)
else
root = -q₀ / q₁
end
return (root, root)
end
# Case where q(x) is indeed quadratic.
rhs = √eps(T) * q₁ * q₁
if abs(q₀ * q₂) > rhs
ρ = q₁ * q₁ - 4 * q₂ * q₀
ρ < 0 && return error("The quadratic `q` doesn't have real roots.")
d = -(q₁ + copysign(sqrt(ρ), q₁)) / 2
root1 = d / q₂
root2 = q₀ / d
else
# Ill-conditioned quadratic.
root1 = -q₁ / q₂
root2 = zero(T)
end
# Perform a few Newton iterations to improve accuracy.
for it = 1 : nitref
q = (q₂ * root1 + q₁) * root1 + q₀
dq = 2 * q₂ * root1 + q₁
dq == zero(T) && continue
root1 = root1 - q / dq
end
for it = 1 : nitref
q = (q₂ * root2 + q₁) * root2 + q₀
dq = 2 * q₂ * root2 + q₁
dq == zero(T) && continue
root2 = root2 - q / dq
end
return (root1, root2)
end
"""
s = vec2str(x; ndisp)
Display an array in the form
[ -3.0e-01 -5.1e-01 1.9e-01 ... -2.3e-01 -4.4e-01 2.4e-01 ]
with (ndisp - 1)/2 elements on each side.
"""
function vec2str(x :: AbstractVector{T}; ndisp :: Int=7) where T <: Union{AbstractFloat, Missing}
n = length(x)
if n ≤ ndisp
ndisp = n
nside = n
else
nside = max(1, div(ndisp - 1, 2))
end
s = "["
i = 1
while i ≤ nside
if x[i] !== missing
s *= @sprintf("%8.1e ", x[i])
else
s *= " ✗✗✗✗ "
end
i += 1
end
if i ≤ div(n, 2)
s *= "... "
end
i = max(i, n - nside + 1)
while i ≤ n
if x[i] !== missing
s *= @sprintf("%8.1e ", x[i])
else
s *= " ✗✗✗✗ "
end
i += 1
end
s *= "]"
return s
end
"""
S = ktypeof(v)
Return the most relevant storage type `S` based on the type of `v`.
"""
function ktypeof end
function ktypeof(v::S) where S <: DenseVector
if S.name.name == :ComponentArray
T = eltype(S)
return Vector{T}
else
return S
end
end
function ktypeof(v::S) where S <: DenseMatrix
return S
end
function ktypeof(v::S) where S <: AbstractVector
if S.name.name == :Zeros || S.name.name == :Ones || S.name.name == :SArray || S.name.name == :MArray || S.name.name == :SizedArray || S.name.name == :FieldArray
T = eltype(S)
return Vector{T} # FillArrays, StaticArrays
else
return S # BlockArrays, PartitionedArrays, etc...
end
end
function ktypeof(v::S) where S <: SparseVector
T = eltype(S)
return Vector{T}
end
function ktypeof(v::S) where S <: AbstractSparseVector
return S.types[2] # return `CuVector` for a `CuSparseVector`
end
function ktypeof(v::S) where S <: SubArray
vp = v.parent
if isa(vp, DenseMatrix)
M = typeof(vp)
return matrix_to_vector(M) # view of a row or a column of a matrix
else
return ktypeof(vp) # view of a vector
end
end
"""
M = vector_to_matrix(S)
Return the dense matrix storage type `M` related to the dense vector storage type `S`.
"""
function vector_to_matrix(::Type{S}) where S <: DenseVector
T = hasproperty(S, :body) ? S.body : S
par = T.parameters
npar = length(par)
(2 ≤ npar ≤ 3) || error("Type $S is not supported.")
if npar == 2
M = T.name.wrapper{par[1], 2}
else
M = T.name.wrapper{par[1], 2, par[3]}
end
return M
end
"""
S = matrix_to_vector(M)
Return the dense vector storage type `S` related to the dense matrix storage type `M`.
"""
function matrix_to_vector(::Type{M}) where M <: DenseMatrix
T = hasproperty(M, :body) ? M.body : M
par = T.parameters
npar = length(par)
(2 ≤ npar ≤ 3) || error("Type $M is not supported.")
if npar == 2
S = T.name.wrapper{par[1], 1}
else
S = T.name.wrapper{par[1], 1, par[3]}
end
return S
end
"""
v = kzeros(S, n)
Create a vector of storage type `S` of length `n` only composed of zero.
"""
kzeros(S, n) = fill!(S(undef, n), zero(eltype(S)))
"""
v = kones(S, n)
Create a vector of storage type `S` of length `n` only composed of one.
"""
kones(S, n) = fill!(S(undef, n), one(eltype(S)))
allocate_if(bool, solver, v, S, n) = bool && isempty(solver.:($v)::S) && (solver.:($v)::S = S(undef, n))
allocate_if(bool, solver, v, S, m, n) = bool && isempty(solver.:($v)::S) && (solver.:($v)::S = S(undef, m, n))
kdisplay(iter, verbose) = (verbose > 0) && (mod(iter, verbose) == 0)
ktimer(start_time::UInt64) = (time_ns() - start_time) / 1e9
mulorldiv!(y, P, x, ldiv::Bool) = ldiv ? ldiv!(y, P, x) : mul!(y, P, x)
kdot(n :: Integer, x :: Vector{T}, y :: Vector{T}) where T <: BLAS.BlasReal = BLAS.dot(n, x, 1, y, 1)
kdot(n :: Integer, x :: Vector{T}, y :: Vector{T}) where T <: BLAS.BlasComplex = BLAS.dotc(n, x, 1, y, 1)
kdot(n :: Integer, x :: AbstractVector{T}, y :: AbstractVector{T}) where T <: FloatOrComplex = dot(x, y)
kdotr(n :: Integer, x :: AbstractVector{T}, y :: AbstractVector{T}) where T <: AbstractFloat = kdot(n, x, y)
kdotr(n :: Integer, x :: AbstractVector{Complex{T}}, y :: AbstractVector{Complex{T}}) where T <: AbstractFloat = real(kdot(n, x, y))
knorm(n :: Integer, x :: Vector{T}) where T <: BLAS.BlasFloat = BLAS.nrm2(n, x, 1)
knorm(n :: Integer, x :: AbstractVector{T}) where T <: FloatOrComplex = norm(x)
knorm_elliptic(n :: Integer, x :: AbstractVector{T}, y :: AbstractVector{T}) where T <: FloatOrComplex = (x === y) ? knorm(n, x) : sqrt(kdotr(n, x, y))
kscal!(n :: Integer, s :: T, x :: Vector{T}) where T <: BLAS.BlasFloat = BLAS.scal!(n, s, x, 1)
kscal!(n :: Integer, s :: T, x :: AbstractVector{T}) where T <: FloatOrComplex = rmul!(x, s)
kscal!(n :: Integer, s :: T, x :: AbstractVector{Complex{T}}) where T <: AbstractFloat = kscal!(n, Complex{T}(s), x)
kaxpy!(n :: Integer, s :: T, x :: Vector{T}, y :: Vector{T}) where T <: BLAS.BlasFloat = BLAS.axpy!(n, s, x, 1, y, 1)
kaxpy!(n :: Integer, s :: T, x :: AbstractVector{T}, y :: AbstractVector{T}) where T <: FloatOrComplex = axpy!(s, x, y)
kaxpy!(n :: Integer, s :: T, x :: AbstractVector{Complex{T}}, y :: AbstractVector{Complex{T}}) where T <: AbstractFloat = kaxpy!(n, Complex{T}(s), x, y)
kaxpby!(n :: Integer, s :: T, x :: Vector{T}, t :: T, y :: Vector{T}) where T <: BLAS.BlasFloat = BLAS.axpby!(n, s, x, 1, t, y, 1)
kaxpby!(n :: Integer, s :: T, x :: AbstractVector{T}, t :: T, y :: AbstractVector{T}) where T <: FloatOrComplex = axpby!(s, x, t, y)
kaxpby!(n :: Integer, s :: T, x :: AbstractVector{Complex{T}}, t :: Complex{T}, y :: AbstractVector{Complex{T}}) where T <: AbstractFloat = kaxpby!(n, Complex{T}(s), x, t, y)
kaxpby!(n :: Integer, s :: Complex{T}, x :: AbstractVector{Complex{T}}, t :: T, y :: AbstractVector{Complex{T}}) where T <: AbstractFloat = kaxpby!(n, s, x, Complex{T}(t), y)
kaxpby!(n :: Integer, s :: T, x :: AbstractVector{Complex{T}}, t :: T, y :: AbstractVector{Complex{T}}) where T <: AbstractFloat = kaxpby!(n, Complex{T}(s), x, Complex{T}(t), y)
kcopy!(n :: Integer, y :: Vector{T}, x :: Vector{T}) where T <: BLAS.BlasFloat = BLAS.blascopy!(n, x, 1, y, 1)
kcopy!(n :: Integer, y :: AbstractVector{T}, x :: AbstractVector{T}) where T <: FloatOrComplex = copyto!(y, x)
kfill!(x :: AbstractArray{T}, val :: T) where T <: FloatOrComplex = fill!(x, val)
kref!(n, x, y, c, s) = reflect!(x, y, c, s)
kgeqrf!(A :: AbstractMatrix{T}, tau :: AbstractVector{T}) where T <: BLAS.BlasFloat = LAPACK.geqrf!(A, tau)
korgqr!(A :: AbstractMatrix{T}, tau :: AbstractVector{T}) where T <: BLAS.BlasFloat = LAPACK.orgqr!(A, tau)
kormqr!(side :: Char, trans :: Char, A :: AbstractMatrix{T}, tau :: AbstractVector{T}, C :: AbstractMatrix{T}) where T <: BLAS.BlasFloat = LAPACK.ormqr!(side, trans, A, tau, C)
macro kswap!(x, y)
quote
local tmp = $(esc(x))
$(esc(x)) = $(esc(y))
$(esc(y)) = tmp
end
end
"""
roots = to_boundary(n, x, d, radius; flip, xNorm2, dNorm2)
Given a trust-region radius `radius`, a vector `x` lying inside the
trust-region and a direction `d`, return `σ1` and `σ2` such that
‖x + σi d‖ = radius, i = 1, 2
in the Euclidean norm.
`n` is the length of vectors `x` and `d`.
If known, ‖x‖² and ‖d‖² may be supplied with `xNorm2` and `dNorm2`.
If `flip` is set to `true`, `σ1` and `σ2` are computed such that
‖x - σi d‖ = radius, i = 1, 2.
"""
function to_boundary(n :: Int, x :: AbstractVector{FC}, d :: AbstractVector{FC}, z::AbstractVector{FC}, radius :: T; flip :: Bool=false, xNorm2 :: T=zero(T), dNorm2 :: T=zero(T), M=I, ldiv :: Bool = false) where {T <: AbstractFloat, FC <: FloatOrComplex{T}}
radius > 0 || error("radius must be positive")
if M === I
# ‖d‖² σ² + (xᴴd + dᴴx) σ + (‖x‖² - Δ²).
rxd = kdotr(n, x, d)
dNorm2 == zero(T) && (dNorm2 = kdotr(n, d, d))
xNorm2 == zero(T) && (xNorm2 = kdotr(n, x, x))
else
# (dᴴMd) σ² + (xᴴMd + dᴴMx) σ + (xᴴMx - Δ²).
mulorldiv!(z, M, x, ldiv)
rxd = kdot(n, z, d)
xNorm2 = kdotr(n, z, x)
mulorldiv!(z, M, d, ldiv)
dNorm2 = kdotr(n, z, d)
end
dNorm2 == zero(T) && error("zero direction")
flip && (rxd = -rxd)
radius2 = radius * radius
(xNorm2 ≤ radius2) || error(@sprintf("outside of the trust region: ‖x‖²=%7.1e, Δ²=%7.1e", xNorm2, radius2))
# q₂ = ‖d‖², q₁ = xᴴd + dᴴx, q₀ = ‖x‖² - Δ²
# ‖x‖² ≤ Δ² ⟹ (q₁)² - 4 * q₂ * q₀ ≥ 0
roots = roots_quadratic(dNorm2, 2 * real(rxd), xNorm2 - radius2)
return roots # `σ1` and `σ2`
end
"""
arguments = extract_parameters(ex::Expr)
Extract the arguments of an expression that is keyword parameter tuple.
Implementation suggested by Mitchell J. O'Sullivan (@mosullivan93).
"""
function extract_parameters(ex::Expr)
Meta.isexpr(ex, :tuple, 1) &&
Meta.isexpr((@inbounds p = ex.args[1]), :parameters) &&
Base.Docs.validcall(p.args[]) || throw(ArgumentError("Given expression is not a kw parameter tuple [e.g. :(; x)]: $ex"))
return p.args[]
end