-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcommon.jl
327 lines (287 loc) · 11.1 KB
/
common.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
"""
`OperatorCondition`
Specifies the assumption of matrix conditioning for the default linear solver choices. Condition number
is defined as the ratio of eigenvalues. The numerical stability of many linear solver algorithms
can be dependent on the condition number of the matrix. The condition number can be computed as:
```julia
using LinearAlgebra
cond(rand(100, 100))
```
However, in practice this computation is very expensive and thus not possible for most practical cases.
Therefore, OperatorCondition lets one share to LinearSolve the expected conditioning. The higher the
expected condition number, the safer the algorithm needs to be and thus there is a trade-off between
numerical performance and stability. By default the method assumes the operator may be ill-conditioned
for the standard linear solvers to converge (such as LU-factorization), though more extreme
ill-conditioning or well-conditioning could be the case and specified through this assumption.
"""
EnumX.@enumx OperatorCondition begin
"""
`OperatorCondition.IllConditioned`
The default assumption of LinearSolve. Assumes that the operator can have minor ill-conditioning
and thus needs to use safe algorithms.
"""
IllConditioned
"""
`OperatorCondition.VeryIllConditioned`
Assumes that the operator can have fairly major ill-conditioning and thus the standard linear algebra
algorithms cannot be used.
"""
VeryIllConditioned
"""
`OperatorCondition.SuperIllConditioned`
Assumes that the operator can have fairly extreme ill-conditioning and thus the most stable algorithm
is used.
"""
SuperIllConditioned
"""
`OperatorCondition.WellConditioned`
Assumes that the operator can have fairly contained conditioning and thus the fastest algorithm is
used.
"""
WellConditioned
end
"""
OperatorAssumptions(issquare = nothing; condition::OperatorCondition.T = IllConditioned)
Sets the operator `A` assumptions used as part of the default algorithm
"""
struct OperatorAssumptions{T}
issq::T
condition::OperatorCondition.T
end
function OperatorAssumptions(issquare = nothing;
condition::OperatorCondition.T = OperatorCondition.IllConditioned)
OperatorAssumptions{typeof(issquare)}(issquare, condition)
end
__issquare(assump::OperatorAssumptions) = assump.issq
__conditioning(assump::OperatorAssumptions) = assump.condition
mutable struct LinearCache{TA, Tb, Tu, Tp, Talg, Tc, Tl, Tr, Ttol, issq, S}
A::TA
b::Tb
u::Tu
p::Tp
alg::Talg
cacheval::Tc # store alg cache here
isfresh::Bool # false => cacheval is set wrt A, true => update cacheval wrt A
Pl::Tl # preconditioners
Pr::Tr
abstol::Ttol
reltol::Ttol
maxiters::Int
verbose::Bool
assumptions::OperatorAssumptions{issq}
sensealg::S
end
function Base.setproperty!(cache::LinearCache, name::Symbol, x)
if name === :A
if hasproperty(cache.alg, :precs) && !isnothing(cache.alg.precs)
Pl, Pr = cache.alg.precs(x, cache.p)
setfield!(cache, :Pl, Pl)
setfield!(cache, :Pr, Pr)
end
setfield!(cache, :isfresh, true)
elseif name === :p
if hasproperty(cache.alg, :precs) && !isnothing(cache.alg.precs)
Pl, Pr = cache.alg.precs(cache.A, x)
setfield!(cache, :Pl, Pl)
setfield!(cache, :Pr, Pr)
end
elseif name === :b
# In case there is something that needs to be done when b is updated
update_cacheval!(cache, :b, x)
elseif name === :cacheval && cache.alg isa DefaultLinearSolver
@assert cache.cacheval isa DefaultLinearSolverInit
return __setfield!(cache.cacheval, cache.alg, x)
# return setfield!(cache.cacheval, Symbol(cache.alg.alg), x)
end
setfield!(cache, name, x)
end
function update_cacheval!(cache::LinearCache, name::Symbol, x)
return update_cacheval!(cache, cache.cacheval, name, x)
end
update_cacheval!(cache, cacheval, name::Symbol, x) = cacheval
init_cacheval(alg::SciMLLinearSolveAlgorithm, args...) = nothing
function SciMLBase.init(prob::LinearProblem, args...; kwargs...)
SciMLBase.init(prob, nothing, args...; kwargs...)
end
default_tol(::Type{T}) where {T} = √(eps(T))
default_tol(::Type{Complex{T}}) where {T} = √(eps(T))
default_tol(::Type{<:Rational}) = 0
default_tol(::Type{<:Integer}) = 0
default_tol(::Type{Any}) = 0
default_alias_A(::Any, ::Any, ::Any) = false
default_alias_b(::Any, ::Any, ::Any) = false
# Non-destructive algorithms default to true
default_alias_A(::AbstractKrylovSubspaceMethod, ::Any, ::Any) = true
default_alias_b(::AbstractKrylovSubspaceMethod, ::Any, ::Any) = true
default_alias_A(::AbstractSparseFactorization, ::Any, ::Any) = true
default_alias_b(::AbstractSparseFactorization, ::Any, ::Any) = true
DEFAULT_PRECS(A, p) = IdentityOperator(size(A)[1]), IdentityOperator(size(A)[2])
function __init_u0_from_Ab(A, b)
u0 = similar(b, size(A, 2))
fill!(u0, false)
return u0
end
__init_u0_from_Ab(::SMatrix{S1, S2}, b) where {S1, S2} = zeros(SVector{S2, eltype(b)})
function SciMLBase.init(prob::LinearProblem, alg::SciMLLinearSolveAlgorithm,
args...;
alias_A = default_alias_A(alg, prob.A, prob.b),
alias_b = default_alias_b(alg, prob.A, prob.b),
abstol = default_tol(real(eltype(prob.b))),
reltol = default_tol(real(eltype(prob.b))),
maxiters::Int = length(prob.b),
verbose::Bool = false,
Pl = nothing,
Pr = nothing,
assumptions = OperatorAssumptions(issquare(prob.A)),
sensealg = LinearSolveAdjoint(),
kwargs...)
(;A, b, u0, p) = prob
A = if alias_A || A isa SMatrix
A
elseif A isa Array
copy(A)
elseif A isa AbstractSparseMatrixCSC
SparseMatrixCSC(size(A)..., getcolptr(A), rowvals(A), nonzeros(A))
else
deepcopy(A)
end
b = if b isa SparseArrays.AbstractSparseArray && !(A isa Diagonal)
Array(b) # the solution to a linear solve will always be dense!
elseif alias_b || b isa SVector
b
elseif b isa Array
copy(b)
elseif b isa AbstractSparseMatrixCSC
SparseMatrixCSC(size(b)..., getcolptr(b), rowvals(b), nonzeros(b))
else
deepcopy(b)
end
u0_ = u0 !== nothing ? u0 : __init_u0_from_Ab(A, b)
# Guard against type mismatch for user-specified reltol/abstol
reltol = real(eltype(prob.b))(reltol)
abstol = real(eltype(prob.b))(abstol)
precs = if hasproperty(alg, :precs)
isnothing(alg.precs) ? DEFAULT_PRECS : alg.precs
else
DEFAULT_PRECS
end
_Pl, _Pr = precs(A, p)
if isnothing(Pl)
Pl = _Pl
else
# TODO: deprecate once all docs are updated to the new form
#@warn "passing Preconditioners at `init`/`solve` time is deprecated. Instead add a `precs` function to your algorithm."
end
if isnothing(Pr)
Pr = _Pr
else
# TODO: deprecate once all docs are updated to the new form
#@warn "passing Preconditioners at `init`/`solve` time is deprecated. Instead add a `precs` function to your algorithm."
end
cacheval = init_cacheval(alg, A, b, u0_, Pl, Pr, maxiters, abstol, reltol, verbose,
assumptions)
isfresh = false
Tc = typeof(cacheval)
cache = LinearCache{typeof(A), typeof(b), typeof(u0_), typeof(p), typeof(alg), Tc,
typeof(Pl), typeof(Pr), typeof(reltol), typeof(assumptions.issq),
typeof(sensealg)}(A, b, u0_, p, alg, cacheval, isfresh, Pl, Pr, abstol, reltol,
maxiters, verbose, assumptions, sensealg)
return cache
end
function SciMLBase.reinit!(cache::LinearCache;
A = nothing,
b = cache.b,
u = cache.u,
p = nothing,
reinit_cache = false,)
(; alg, cacheval, abstol, reltol, maxiters, verbose, assumptions, sensealg) = cache
precs = (hasproperty(alg, :precs) && !isnothing(alg.precs)) ? alg.precs : DEFAULT_PRECS
Pl, Pr = if isnothing(A) || isnothing(p)
if isnothing(A)
A = cache.A
end
if isnothing(p)
p = cache.p
end
precs(A, p)
else
(cache.Pl, cache.Pr)
end
isfresh = true
if reinit_cache
return LinearCache{typeof(A), typeof(b), typeof(u), typeof(p), typeof(alg), typeof(cacheval),
typeof(Pl), typeof(Pr), typeof(reltol), typeof(assumptions.issq),
typeof(sensealg)}(A, b, u, p, alg, cacheval, isfresh, Pl, Pr, abstol, reltol,
maxiters, verbose, assumptions, sensealg)
else
cache.A = A
cache.b = b
cache.u = u
cache.p = p
cache.Pl = Pl
cache.Pr = Pr
cache.isfresh = true
end
end
function SciMLBase.solve(prob::LinearProblem, args...; kwargs...)
return solve(prob, nothing, args...; kwargs...)
end
function SciMLBase.solve(prob::LinearProblem, ::Nothing, args...;
assump = OperatorAssumptions(issquare(prob.A)), kwargs...)
return solve(prob, defaultalg(prob.A, prob.b, assump), args...; kwargs...)
end
function SciMLBase.solve(prob::LinearProblem, alg::SciMLLinearSolveAlgorithm,
args...; kwargs...)
solve!(init(prob, alg, args...; kwargs...))
end
function SciMLBase.solve!(cache::LinearCache, args...; kwargs...)
solve!(cache, cache.alg, args...; kwargs...)
end
# Special Case for StaticArrays
const StaticLinearProblem = LinearProblem{uType, iip, <:SMatrix,
<:Union{<:SMatrix, <:SVector}} where {uType, iip}
function SciMLBase.solve(prob::StaticLinearProblem, args...; kwargs...)
return SciMLBase.solve(prob, nothing, args...; kwargs...)
end
function SciMLBase.solve(prob::StaticLinearProblem,
alg::Nothing, args...; kwargs...)
if alg === nothing || alg isa DirectLdiv!
u = prob.A \ prob.b
elseif alg isa LUFactorization
u = lu(prob.A) \ prob.b
elseif alg isa QRFactorization
u = qr(prob.A) \ prob.b
elseif alg isa CholeskyFactorization
u = cholesky(prob.A) \ prob.b
elseif alg isa NormalCholeskyFactorization
u = cholesky(Symmetric(prob.A' * prob.A)) \ (prob.A' * prob.b)
elseif alg isa SVDFactorization
u = svd(prob.A) \ prob.b
else
# Slower Path but handles all cases
cache = init(prob, alg, args...; kwargs...)
return solve!(cache)
end
return SciMLBase.build_linear_solution(alg, u, nothing, prob)
end
function SciMLBase.solve(prob::StaticLinearProblem,
alg::SciMLLinearSolveAlgorithm, args...; kwargs...)
if alg === nothing || alg isa DirectLdiv!
u = prob.A \ prob.b
elseif alg isa LUFactorization
u = lu(prob.A) \ prob.b
elseif alg isa QRFactorization
u = qr(prob.A) \ prob.b
elseif alg isa CholeskyFactorization
u = cholesky(prob.A) \ prob.b
elseif alg isa NormalCholeskyFactorization
u = cholesky(Symmetric(prob.A' * prob.A)) \ (prob.A' * prob.b)
elseif alg isa SVDFactorization
u = svd(prob.A) \ prob.b
else
# Slower Path but handles all cases
cache = init(prob, alg, args...; kwargs...)
return solve!(cache)
end
return SciMLBase.build_linear_solution(alg, u, nothing, prob)
end