-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathcraigmr.jl
347 lines (281 loc) · 11 KB
/
craigmr.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
# An implementation of CRAIG-MR for the solution of the
# (under/over-determined or square) linear system
#
# Ax = b.
#
# The method seeks to solve the minimum-norm problem
#
# min ‖x‖ s.t. x ∈ argmin ‖Ax - b‖,
#
# and is equivalent to applying the conjugate residual method
# to the linear system
#
# AAᴴy = b.
#
# This method is equivalent to CRMR, and is described in
#
# D. Orban and M. Arioli. Iterative Solution of Symmetric Quasi-Definite Linear Systems,
# Volume 3 of Spotlights. SIAM, Philadelphia, PA, 2017.
#
# D. Orban, The Projected Golub-Kahan Process for Constrained
# Linear Least-Squares Problems. Cahier du GERAD G-2014-15,
# GERAD, Montreal QC, Canada, 2014.
#
# Dominique Orban, <[email protected]>
# Montreal, QC, May 2015.
export craigmr, craigmr!
"""
(x, y, stats) = craigmr(A, b::AbstractVector{FC};
M=I, N=I, sqd :: Bool=false, λ :: T=zero(T), atol :: T=√eps(T),
rtol::T=√eps(T), itmax::Int=0, verbose::Int=0, history::Bool=false,
ldiv::Bool=false, callback=solver->false)
`T` is an `AbstractFloat` such as `Float32`, `Float64` or `BigFloat`.
`FC` is `T` or `Complex{T}`.
Solve the consistent linear system
Ax + λ²y = b
of size m × n using the CRAIGMR method, where λ ≥ 0 is a regularization parameter.
This method is equivalent to applying the Conjugate Residuals method
to the normal equations of the second kind
(AAᴴ + λ²I) y = b
but is more stable. When λ = 0, this method solves the minimum-norm problem
min ‖x‖ s.t. x ∈ argmin ‖Ax - b‖.
If `λ > 0`, CRAIGMR solves the symmetric and quasi-definite system
[ -F Aᴴ ] [ x ] [ 0 ]
[ A λ²E ] [ y ] = [ b ],
where E and F are symmetric and positive definite.
Preconditioners M = E⁻¹ ≻ 0 and N = F⁻¹ ≻ 0 may be provided in the form of linear operators.
If `sqd=true`, `λ` is set to the common value `1`.
The system above represents the optimality conditions of
min ‖x‖²_F + λ²‖y‖²_E s.t. Ax + λ²Ey = b.
For a symmetric and positive definite matrix `K`, the K-norm of a vector `x` is `‖x‖²_K = xᴴKx`.
CRAIGMR is then equivalent to applying MINRES to `(AF⁻¹Aᴴ + λ²E)y = b` with `Fx = Aᴴy`.
If `λ = 0`, CRAIGMR solves the symmetric and indefinite system
[ -F Aᴴ ] [ x ] [ 0 ]
[ A 0 ] [ y ] = [ b ].
The system above represents the optimality conditions of
min ‖x‖²_F s.t. Ax = b.
In this case, `M` can still be specified and indicates the weighted norm in which residuals are measured.
CRAIGMR produces monotonic residuals ‖r‖₂.
It is formally equivalent to CRMR, though can be slightly more accurate,
and intricate to implement. Both the x- and y-parts of the solution are
returned.
The callback is called as `callback(solver)` and should return `true` if the main loop should terminate,
and `false` otherwise.
#### Input arguments
* `A`: a linear operator that models a matrix of dimension m × n;
* `b`: a vector of length m.
#### Output arguments
* `x`: a dense vector of length n;
* `y`: a dense vector of length m;
* `stats`: statistics collected on the run in a [`SimpleStats`](@ref) structure.
#### References
* D. Orban and M. Arioli. [*Iterative Solution of Symmetric Quasi-Definite Linear Systems*](https://doi.org/10.1137/1.9781611974737), Volume 3 of Spotlights. SIAM, Philadelphia, PA, 2017.
* D. Orban, [*The Projected Golub-Kahan Process for Constrained, Linear Least-Squares Problems*](https://dx.doi.org/10.13140/RG.2.2.17443.99360). Cahier du GERAD G-2014-15, 2014.
"""
function craigmr end
function craigmr(A, b :: AbstractVector{FC}; kwargs...) where FC <: FloatOrComplex
solver = CraigmrSolver(A, b)
craigmr!(solver, A, b; kwargs...)
return (solver.x, solver.y, solver.stats)
end
"""
solver = craigmr!(solver::CraigmrSolver, A, b; kwargs...)
where `kwargs` are keyword arguments of [`craigmr`](@ref).
See [`CraigmrSolver`](@ref) for more details about the `solver`.
"""
function craigmr! end
function craigmr!(solver :: CraigmrSolver{T,FC,S}, A, b :: AbstractVector{FC};
M=I, N=I, sqd :: Bool=false, λ :: T=zero(T), atol :: T=√eps(T),
rtol :: T=√eps(T), itmax :: Int=0, verbose :: Int=0, history :: Bool=false,
ldiv :: Bool=false, callback = solver -> false) where {T <: AbstractFloat, FC <: FloatOrComplex{T}, S <: DenseVector{FC}}
m, n = size(A)
length(b) == m || error("Inconsistent problem size")
(verbose > 0) && @printf("CRAIGMR: system of %d equations in %d variables\n", m, n)
# Check sqd and λ parameters
sqd && (λ ≠ 0) && error("sqd cannot be set to true if λ ≠ 0 !")
sqd && (λ = one(T))
# Tests M = Iₘ and N = Iₙ
MisI = (M === I)
NisI = (N === I)
# Check type consistency
eltype(A) == FC || error("eltype(A) ≠ $FC")
ktypeof(b) <: S || error("ktypeof(b) is not a subtype of $S")
# Compute the adjoint of A
Aᴴ = A'
# Set up workspace.
allocate_if(!MisI, solver, :u, S, m)
allocate_if(!NisI, solver, :v, S, n)
allocate_if(λ > 0, solver, :q, S, n)
x, Nv, Aᴴu, d, y, Mu = solver.x, solver.Nv, solver.Aᴴu, solver.d, solver.y, solver.Mu
w, wbar, Av, q, stats = solver.w, solver.wbar, solver.Av, solver.q, solver.stats
rNorms, ArNorms = stats.residuals, stats.Aresiduals
reset!(stats)
u = MisI ? Mu : solver.u
v = NisI ? Nv : solver.v
# Compute y such that AAᴴy = b. Then recover x = Aᴴy.
x .= zero(FC)
y .= zero(FC)
Mu .= b
MisI || mulorldiv!(u, M, Mu, ldiv)
β = sqrt(@kdotr(m, u, Mu))
if β == 0
stats.niter = 0
stats.solved, stats.inconsistent = true, false
history && push!(rNorms, β)
history && push!(ArNorms, zero(T))
stats.status = "x = 0 is a zero-residual solution"
return solver
end
# Initialize Golub-Kahan process.
# β₁Mu₁ = b.
@kscal!(m, one(FC)/β, u)
MisI || @kscal!(m, one(FC)/β, Mu)
# α₁Nv₁ = Aᴴu₁.
mul!(Aᴴu, Aᴴ, u)
Nv .= Aᴴu
NisI || mulorldiv!(v, N, Nv, ldiv)
α = sqrt(@kdotr(n, v, Nv))
Anorm² = α * α
iter = 0
itmax == 0 && (itmax = m + n)
(verbose > 0) && @printf("%5s %7s %7s %7s %7s %8s %8s %7s\n", "k", "‖r‖", "‖Aᴴr‖", "β", "α", "cos", "sin", "‖A‖²")
kdisplay(iter, verbose) && @printf("%5d %7.1e %7.1e %7.1e %7.1e %8.1e %8.1e %7.1e\n", iter, β, α, β, α, 0, 1, Anorm²)
# Aᴴb = 0 so x = 0 is a minimum least-squares solution
if α == 0
stats.niter = 0
stats.solved, stats.inconsistent = true, false
history && push!(rNorms, β)
history && push!(ArNorms, zero(T))
stats.status = "x = 0 is a minimum least-squares solution"
return solver
end
@kscal!(n, one(FC)/α, v)
NisI || @kscal!(n, one(FC)/α, Nv)
# Regularization.
λₖ = λ # λ₁ = λ
cpₖ = spₖ = one(T) # Givens sines and cosines used to zero out λₖ
cdₖ = sdₖ = one(T) # Givens sines and cosines used to define λₖ₊₁
λ > 0 && (q .= v) # Additional vector needed to update x, by definition q₀ = 0
if λ > 0
(cpₖ, spₖ, αhat) = sym_givens(α, λₖ)
@kscal!(n, spₖ, q) # q̄₁ = sp₁ * v₁
else
αhat = α
end
# Initialize other constants.
ζbar = β
ρbar = αhat
θ = zero(T)
rNorm = ζbar
history && push!(rNorms, rNorm)
ArNorm = α
history && push!(ArNorms, ArNorm)
ɛ_c = atol + rtol * rNorm # Stopping tolerance for consistent systems.
ɛ_i = atol + rtol * ArNorm # Stopping tolerance for inconsistent systems.
wbar .= u
@kscal!(m, one(FC)/αhat, wbar)
w .= zero(FC)
d .= zero(FC)
status = "unknown"
solved = rNorm ≤ ɛ_c
inconsistent = (rNorm > 100 * ɛ_c) & (ArNorm ≤ ɛ_i)
tired = iter ≥ itmax
user_requested_exit = false
while ! (solved || inconsistent || tired || user_requested_exit)
iter = iter + 1
# Generate next Golub-Kahan vectors.
# 1. βₖ₊₁Muₖ₊₁ = Avₖ - αₖMuₖ
mul!(Av, A, v)
@kaxpby!(m, one(FC), Av, -α, Mu)
MisI || mulorldiv!(u, M, Mu, ldiv)
β = sqrt(@kdotr(m, u, Mu))
if β ≠ 0
@kscal!(m, one(FC)/β, u)
MisI || @kscal!(m, one(FC)/β, Mu)
end
Anorm² = Anorm² + β * β # = ‖B_{k-1}‖²
if λ > 0
βhat = cpₖ * β
λₐᵤₓ = spₖ * β
else
βhat = β
end
# Continue QR factorization
#
# Q [ Lₖ β₁ e₁ ] = [ Rₖ zₖ ] :
# [ β 0 ] [ 0 ζbar ]
#
# k k+1 k k+1 k k+1
# k [ c s ] [ ρbar ] = [ ρ θ⁺ ]
# k+1 [ s -c ] [ β α⁺ ] [ ρbar⁺ ]
#
# so that we obtain
#
# [ c s ] [ ζbar ] = [ ζ ]
# [ s -c ] [ 0 ] [ ζbar⁺ ]
(c, s, ρ) = sym_givens(ρbar, βhat)
ζ = c * ζbar
ζbar = s * ζbar
rNorm = abs(ζbar)
history && push!(rNorms, rNorm)
@kaxpby!(m, one(FC)/ρ, wbar, -θ/ρ, w) # w = (wbar - θ * w) / ρ
@kaxpy!(m, ζ, w, y) # y = y + ζ * w
if λ > 0
# DₖRₖ = V̅ₖ with v̅ₖ = cpₖvₖ + spₖqₖ₋₁
if iter == 1
@kaxpy!(n, one(FC)/ρ, cpₖ * v, d)
else
@kaxpby!(n, one(FC)/ρ, cpₖ * v, -θ/ρ, d)
@kaxpy!(n, one(FC)/ρ, spₖ * q, d)
@kaxpby!(n, spₖ, v, -cpₖ, q) # q̄ₖ ← spₖ * vₖ - cpₖ * qₖ₋₁
end
else
# DₖRₖ = Vₖ
if iter == 1
@kaxpy!(n, one(FC)/ρ, v, d)
else
@kaxpby!(n, one(FC)/ρ, v, -θ/ρ, d)
end
end
# xₖ = Dₖzₖ
@kaxpy!(n, ζ, d, x)
# 2. αₖ₊₁Nvₖ₊₁ = Aᴴuₖ₊₁ - βₖ₊₁Nvₖ
mul!(Aᴴu, Aᴴ, u)
@kaxpby!(n, one(FC), Aᴴu, -β, Nv)
NisI || mulorldiv!(v, N, Nv, ldiv)
α = sqrt(@kdotr(n, v, Nv))
Anorm² = Anorm² + α * α # = ‖Lₖ‖
ArNorm = α * β * abs(ζ/ρ)
history && push!(ArNorms, ArNorm)
kdisplay(iter, verbose) && @printf("%5d %7.1e %7.1e %7.1e %7.1e %8.1e %8.1e %7.1e\n", iter, rNorm, ArNorm, β, α, c, s, Anorm²)
if λ > 0
(cdₖ, sdₖ, λₖ₊₁) = sym_givens(λ, λₐᵤₓ)
@kscal!(n, sdₖ, q) # qₖ ← sdₖ * q̄ₖ
(cpₖ, spₖ, αhat) = sym_givens(α, λₖ₊₁)
else
αhat = α
end
if α ≠ 0
@kscal!(n, one(FC)/α, v)
NisI || @kscal!(n, one(FC)/α, Nv)
@kaxpby!(m, one(T)/αhat, u, -βhat / αhat, wbar) # wbar = (u - beta * wbar) / alpha
end
θ = s * αhat
ρbar = -c * αhat
user_requested_exit = callback(solver) :: Bool
solved = rNorm ≤ ɛ_c
inconsistent = (rNorm > 100 * ɛ_c) & (ArNorm ≤ ɛ_i)
tired = iter ≥ itmax
end
(verbose > 0) && @printf("\n")
tired && (status = "maximum number of iterations exceeded")
solved && (status = "found approximate minimum-norm solution")
!tired && !solved && (status = "found approximate minimum least-squares solution")
user_requested_exit && (status = "user-requested exit")
# Update stats
stats.niter = iter
stats.solved = solved
stats.inconsistent = inconsistent
stats.status = status
return solver
end