-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsettings.jl
More file actions
142 lines (105 loc) · 4.52 KB
/
settings.jl
File metadata and controls
142 lines (105 loc) · 4.52 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
## Linear solver
"""
IterativeLinearSolver
Callable object that can solve linear systems `Ax = b` and `AX = B` in the same way as the built-in `\\`.
# Constructor
IterativeLinearSolver(; kwargs...)
IterativeLinearSolver{package}(; kwargs...)
The type parameter `package` can be either:
- `:Krylov` to use the solver `gmres` or `block_gmres` from [Krylov.jl](https://github.com/JuliaSmoothOptimizers/Krylov.jl) (the default)
- `:IterativeSolvers` to use the solver `gmres` from [IterativeSolvers.jl](https://github.com/JuliaLinearAlgebra/IterativeSolvers.jl)
Keyword arguments are passed on to the respective solver.
# Callable behavior
(::IterativeLinearSolver)(A, b::AbstractVector)
Solve a linear system with a single right-hand side.
(::IterativeLinearSolver)(A, B::AbstractMatrix)
Solve a linear system with multiple right-hand sides.
"""
struct IterativeLinearSolver{package,K}
kwargs::K
function IterativeLinearSolver{package}(; kwargs...) where {package}
@assert package in [:Krylov, :IterativeSolvers]
return new{package,typeof(kwargs)}(kwargs)
end
end
function Base.show(io::IO, linear_solver::IterativeLinearSolver{package}) where {package}
print(io, "IterativeLinearSolver{$(repr(package))}(; ")
for (k, v) in pairs(linear_solver.kwargs)
print(io, "$k=$v, ")
end
return print(io, ")")
end
IterativeLinearSolver(; kwargs...) = IterativeLinearSolver{:Krylov}(; kwargs...)
function (solver::IterativeLinearSolver{:Krylov})(A, b::AbstractVector)
x, stats = Krylov.gmres(A, b; solver.kwargs...)
return x
end
function (solver::IterativeLinearSolver{:Krylov})(A, B::AbstractMatrix)
# TODO: use block_gmres
X = mapreduce(hcat, eachcol(B)) do b
x, _ = Krylov.gmres(A, b; solver.kwargs...)
x
end
return X
end
function (solver::IterativeLinearSolver{:IterativeSolvers})(A, b::AbstractVector)
x = IterativeSolvers.gmres(A, b; solver.kwargs...)
return x
end
function (solver::IterativeLinearSolver{:IterativeSolvers})(A, B::AbstractMatrix)
X = mapreduce(hcat, eachcol(B)) do b
IterativeSolvers.gmres(A, b; solver.kwargs...)
end
return X
end
## Representation
abstract type AbstractRepresentation end
"""
MatrixRepresentation
Specify that the matrix `A` involved in the implicit function theorem should be represented explicitly, with all its coefficients.
# See also
- [`ImplicitFunction`](@ref)
- [`OperatorRepresentation`](@ref)
"""
struct MatrixRepresentation <: AbstractRepresentation end
"""
OperatorRepresentation
Specify that the matrix `A` involved in the implicit function theorem should be represented lazily.
# Constructors
OperatorRepresentation(;
symmetric=false, hermitian=false, posdef=false, keep_input_type=false
)
OperatorRepresentation{package}(;
symmetric=false, hermitian=false, posdef=false, keep_input_type=false
)
The type parameter `package` can be either:
- `:LinearOperators` to use a wrapper from [LinearOperators.jl](https://github.com/JuliaSmoothOptimizers/LinearOperators.jl) (the default)
- `:LinearMaps` to use a wrapper from [LinearMaps.jl](https://github.com/JuliaLinearAlgebra/LinearMaps.jl)
The keyword arguments `symmetric`, `hermitian` and `posdef` give additional properties of the Jacobian of the `conditions` with respect to the solution `y`, which are useful to the solver in case you can prove them.
The keyword argument `keep_input_type` dictates whether to force the linear operator to work with the provided input type, or fall back on a default.
# See also
- [`ImplicitFunction`](@ref)
- [`MatrixRepresentation`](@ref)
"""
struct OperatorRepresentation{package,symmetric,hermitian,posdef,keep_input_type} <:
AbstractRepresentation
function OperatorRepresentation{package}(;
symmetric::Bool=false,
hermitian::Bool=false,
posdef::Bool=false,
keep_input_type::Bool=false,
) where {package}
@assert package in [:LinearOperators, :LinearMaps]
return new{package,symmetric,hermitian,posdef,keep_input_type}()
end
end
function Base.show(
io::IO, ::OperatorRepresentation{package,symmetric,hermitian,posdef,keep_input_type}
) where {package,symmetric,hermitian,posdef,keep_input_type}
return print(
io,
"OperatorRepresentation{$(repr(package))}(; symmetric=$symmetric, hermitian=$hermitian, posdef=$posdef, keep_input_type=$keep_input_type)",
)
end
OperatorRepresentation(; kwargs...) = OperatorRepresentation{:LinearOperators}(; kwargs...)
function chainrules_suggested_backend end