Iterative Solvers & Preconditioners
Preconditioned Conjugate Gradient solver and preconditioner factories for masked-domain elliptic problems.
Conjugate Gradient
finitevolx.solve_cg(matvec, rhs, x0=None, preconditioner=None, rtol=1e-06, atol=1e-06, max_steps=500)
Preconditioned Conjugate Gradient solver for symmetric linear operators.
Solves A(x) = rhs where matvec implements the symmetric linear
operator A. The operator need not be explicitly formed; only
matrix-vector products are required.
The solve is delegated to :class:lineax.CG, which provides a
numerically stabilised PCG implementation with periodic residual
recomputation to guard against floating-point drift.
The operator A is assumed to be negative definite, which is
the case for the Helmholtz operator (nabla^2 - lambda) when
lambda > 0. Note that lambda < 0 yields an indefinite operator
and will cause CG to fail; use lambda = 0 only if the operator is
strictly negative semidefinite via the mask (e.g. the masked Laplacian
with homogeneous Dirichlet BCs on a non-trivial domain).
The preconditioner, if supplied, should approximate A^{-1}
(the negative-definite inverse). The required sign-flip for
lineax.CG is handled internally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matvec
|
callable
|
Function implementing the symmetric linear operator |
required |
rhs
|
Float[Array, '...']
|
Right-hand side of the linear system. |
required |
x0
|
Float[Array, '...'] or None
|
Initial guess. Defaults to zeros. |
None
|
preconditioner
|
callable or None
|
Optional approximate inverse of |
None
|
rtol
|
float
|
Relative convergence tolerance. Default: 1e-6. |
1e-06
|
atol
|
float
|
Absolute convergence tolerance. Default: 1e-6. |
1e-06
|
max_steps
|
int or None
|
Maximum CG iterations. |
500
|
Returns:
| Name | Type | Description |
|---|---|---|
x |
Float[Array, '...']
|
Approximate solution, same shape as rhs. |
info |
CGInfo
|
Named tuple with fields |
Source code in finitevolx/_src/solvers/iterative.py
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 | |
finitevolx.CGInfo
Bases: NamedTuple
Convergence diagnostics returned by :func:solve_cg.
Source code in finitevolx/_src/solvers/iterative.py
converged
instance-attribute
True if the solver converged within the requested tolerances.
iterations
instance-attribute
Number of PCG iterations performed.
residual_norm
instance-attribute
L2 norm of the final residual A(x) - rhs.
Preconditioners
finitevolx.make_spectral_preconditioner(dx, dy, lambda_=0.0, bc='fft')
Return a spectral-solve preconditioner for PCG.
The preconditioner applies the rectangular spectral solver as an
approximate inverse of the Helmholtz operator (∇² − λ). It is
particularly effective when the physical domain is a subset of the
rectangle (masked-domain problems).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dx
|
float
|
Grid spacing in x. |
required |
dy
|
float
|
Grid spacing in y. |
required |
lambda_
|
float
|
Helmholtz parameter. Must match the parameter used in the main problem. Default: 0.0 (Poisson). |
0.0
|
bc
|
('fft', 'dst', 'dct')
|
Spectral solver type to use as the preconditioner.
Default: |
"fft"
|
Returns:
| Type | Description |
|---|---|
Callable
|
A function |
Source code in finitevolx/_src/solvers/preconditioners.py
finitevolx.make_nystrom_preconditioner(matvec, shape, rank=50, key=None)
Build a randomized Nyström preconditioner for a symmetric operator.
Uses random probing vectors to approximate the action of A^{-1} via
a low-rank Nyström factorisation. The resulting preconditioner is a
callable that can be passed to :func:~finitevolx._src.solvers.iterative.solve_cg.
Algorithm
- Draw a Gaussian random matrix
Ω ∈ ℝ^{n × k}(k = rank). - QR-factorize:
Ω = Q Rto get orthonormalQ ∈ ℝ^{n × k}. - Compute
Y = A Q(kmatvec applications). - Form the small matrix
B = Q^T Y ∈ ℝ^{k × k}. - Compute
B = U S U^T(eigendecomposition of the small matrix). - The preconditioner applies
M^{-1} x ≈ (Q U S^{-1} U^T Q^T) x.
The operator A is assumed to be symmetric negative definite.
The eigenvalues of B will be negative; their absolute values are
used internally for inversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matvec
|
callable
|
Function implementing the symmetric linear operator |
required |
shape
|
(int, int)
|
Spatial shape |
required |
rank
|
int
|
Number of probing vectors (approximation rank). Higher values give a better preconditioner but cost more setup time. Default: 50. |
50
|
key
|
Array or None
|
PRNG key for the random probing matrix. If |
None
|
Returns:
| Type | Description |
|---|---|
Callable
|
A function |
Source code in finitevolx/_src/solvers/preconditioners.py
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 | |
finitevolx.make_preconditioner(kind, *, dx=None, dy=None, lambda_=0.0, bc='fft', matvec=None, shape=None, rank=50, key=None, mg_solver=None)
Convenience factory that builds a preconditioner by name.
Dispatches to :func:make_spectral_preconditioner,
:func:make_nystrom_preconditioner, or
:func:make_multigrid_preconditioner based on kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
('spectral', 'nystrom', 'multigrid')
|
Which preconditioner to build. |
"spectral"
|
dx
|
float or None
|
Grid spacings. Required for |
None
|
dy
|
float or None
|
Grid spacings. Required for |
None
|
lambda_
|
float
|
Helmholtz parameter. Used by |
0.0
|
bc
|
('fft', 'dst', 'dct')
|
Spectral solver type. Used by |
"fft"
|
matvec
|
callable or None
|
Operator |
None
|
shape
|
(int, int) or None
|
Spatial shape |
None
|
rank
|
int
|
Approximation rank. Used by |
50
|
key
|
Array or None
|
PRNG key. Used by |
None
|
mg_solver
|
MultigridSolver or None
|
Pre-built multigrid solver. Required for |
None
|
Returns:
| Type | Description |
|---|---|
callable
|
A function |
Raises:
| Type | Description |
|---|---|
ValueError
|
If kind is unknown or required arguments are missing. |
Examples:
>>> pc = make_preconditioner("spectral", dx=dx, dy=dy, lambda_=10.0)
>>> pc = make_preconditioner("nystrom", matvec=A, shape=(64, 64), rank=30)
>>> pc = make_preconditioner("multigrid", mg_solver=mg)
Source code in finitevolx/_src/solvers/preconditioners.py
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 | |