You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Krylov.jl provides a generic interface for solving linear systems using (block) Krylov methods.
4
+
The interface is designed to be common for all methods and contains three routines [`krylov_workspace`](@ref krylov_workspace), [`krylov_solve`](@ref krylov_solve) and [`krylov_solve!`](@ref krylov_solve!).
5
+
They allow to build Krylov workspaces and call both in-place and out-of-place variants of the solvers with a unified API.
6
+
7
+
```@docs
8
+
krylov_workspace
9
+
krylov_solve!
10
+
krylov_solve
11
+
```
12
+
13
+
In-place solvers update the workspace, from which solutions and statistics can be retrieved.
14
+
The following functions are available for post-solve analysis:
15
+
16
+
```@docs
17
+
Krylov.results
18
+
Krylov.solution
19
+
Krylov.statistics
20
+
Krylov.nsolution
21
+
Krylov.issolved
22
+
```
23
+
24
+
## Examples
25
+
26
+
```julia
27
+
using Krylov, Random
28
+
29
+
# Define a symmetric positive definite matrix A and a right-hand side vector b
30
+
n =1000
31
+
A =sprandn(n, n, 0.005)
32
+
A = A * A'+ I
33
+
b =randn(n)
34
+
35
+
# Out-of-place interface
36
+
for method in (:cg, :cr, :car)
37
+
x, stats =krylov_solve(Val{method}(), A, b)
38
+
r = b - A * x
39
+
println("Residual norm for $(method): ", norm(r))
40
+
end
41
+
```
42
+
43
+
```julia
44
+
using Krylov, Random
45
+
46
+
# Define a square nonsymmetric matrix A and a right-hand side vector b
47
+
n =100
48
+
A =sprand(n, n, 0.05) + I
49
+
b =rand(n)
50
+
51
+
# In-place interface
52
+
for method in (:bicgstab, :gmres)
53
+
# Create a workspace for the Krylov method
54
+
solver =krylov_workspace(Val(method), A, b)
55
+
56
+
# Solve the system in-place
57
+
krylov_solve!(solver, A, b)
58
+
59
+
# Get the statistics
60
+
stats =statistics(solver)
61
+
62
+
# Retrieve the solution
63
+
x =solution(solver)
64
+
65
+
# Check if the solver converged
66
+
solved =issolved(solver)
67
+
println("Converged $method: ", solved)
68
+
69
+
# Display the number of iterations
70
+
niter =niterations(solver)
71
+
println("Number of iterations for $method: ", niter)
Copy file name to clipboardExpand all lines: docs/src/inplace.md
+5-26Lines changed: 5 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,22 +3,22 @@
3
3
All solvers in Krylov.jl have an in-place variant implemented in a method whose name ends with `!`.
4
4
A workspace (`KrylovSolver`), which contains the storage needed by a Krylov method, can be used to solve multiple linear systems with the same dimensions and the same floating-point precision.
5
5
The section [storage requirements](@ref storage-requirements) specifies the memory needed for each Krylov method.
6
-
Each `KrylovSolver` has three constructors:
6
+
7
+
Each `KrylovSolver` has three constructors with consistent argument patterns:
7
8
8
9
```@constructors
9
10
XyzSolver(A, b)
10
11
XyzSolver(m, n, S)
11
12
XyzSolver(kc::KrylovConstructor)
12
13
```
14
+
The only exceptions are `CgLanczosShiftSolver` and `CglsLanczosShiftSolver`, which require an additional argument `nshifts`.
15
+
Additionally, some constructors accept keyword arguments.
13
16
14
-
`Xyz` represents the name of the Krylov method, written in lowercase except for its first letter (e.g., `Cg`, `Minres`, `Lsmr`, `Bicgstab`, etc.).
17
+
`Xyz` represents the name of the Krylov method, written in lowercase except for its first letter (such as `Cg`, `Minres`, `Lsmr` or `Bicgstab`).
15
18
If the name of the Krylov method contains an underscore (e.g., `minres_qlp` or `cgls_lanczos_shift`), the workspace constructor transforms it by capitalizing each word and removing underscores, resulting in names like `MinresQlpSolver` or `CglsLanczosShiftSolver`.
16
19
17
20
Given an operator `A` and a right-hand side `b`, you can create a `KrylovSolver` based on the size of `A` and the type of `b`, or explicitly provide the dimensions `(m, n)` and the storage type `S`.
18
21
19
-
!!! note
20
-
The constructors of `CgLanczosShiftSolver` and `CglsLanczosShiftSolver` require an additional argument `nshifts`.
21
-
22
22
We assume that `S(undef, 0)`, `S(undef, n)`, and `S(undef, m)` are well-defined for the storage type `S`.
23
23
For more advanced vector types, workspaces can also be created with the help of a `KrylovConstructor`.
24
24
```@docs
@@ -44,27 +44,6 @@ lsqr_solver = LsqrSolver(m, n, CuVector{Float32})
44
44
lsqr!(lsqr_solver, A4, b4)
45
45
```
46
46
47
-
A generic function `solve!` is also available and dispatches to the appropriate Krylov method.
48
-
49
-
```@docs
50
-
Krylov.solve!
51
-
```
52
-
53
-
!!! note
54
-
The function `solve!` is not exported to prevent potential conflicts with other Julia packages.
55
-
56
-
In-place methods return an updated `solver` workspace.
57
-
Solutions and statistics can be recovered via `solver.x`, `solver.y` and `solver.stats`.
58
-
Functions `solution`, `statistics` and `results` can be also used.
59
-
60
-
```@docs
61
-
Krylov.nsolution
62
-
Krylov.issolved
63
-
Krylov.solution
64
-
Krylov.statistics
65
-
Krylov.results
66
-
```
67
-
68
47
## Examples
69
48
70
49
We illustrate the use of in-place Krylov solvers with two well-known optimization methods.
0 commit comments