Eigenproblems, matrix functions and singular values
Matrix functions — what Ax = b cannot express
logdet, trace, applyfun and diagonal touch the operator only through its matvec, so they scale
where a factorization cannot, and they are differentiable. They answer questions a linear solve
cannot: the Bayesian log-evidence of a FEM precision, an effective-degrees-of-freedom count, a
per-DOF uncertainty map, and one exact exponential-integrator step.
A, _ = jno.fem([ui * vi + ui.x * vi.x + ui.y * vi.y - 1.0 * vi, u(xb, yb) - 0.0]).operator
jno.solve.logdet(A, samples=64) # log det A — log-evidence
jno.solve.trace(A, fun=lambda z: 1 / z, samples=64) # tr(A⁻¹) — effective DOFs
jno.solve.trace(A, samples=16, deflate=Z) # tr A, exact on span Z, sampled off it
jno.solve.diagonal(A, fun=lambda z: 1 / z, samples=256) # diag(A⁻¹) as a FIELD, plottable on the mesh
jno.solve.applyfun(A, u0, fun=lambda z: jnp.exp(-dt * z)) # exp(-dt·A)·u₀ — one exact step
Accuracy on a 198-DOF FEM precision operator (cond 45)
| quantity | estimate | exact | rel |
|---|---|---|---|
logdet (samples=64) |
168.99 | 167.22 | 1.1e-02 |
trace(1/z) (samples=64) |
119.65 | 118.20 | 1.2e-02 |
diagonal(1/z) (samples=256) |
— | — | 1.1e-01 (L2 over the field) |
applyfun exp step |
— | — | 1.1e-15 |
The first three are stochastic — Hutchinson probes plus Lanczos quadrature — so a percent or
so is the expected accuracy, not a defect: variance falls with samples, bias with order.
applyfun is deterministic (a Krylov approximation, no probes) and essentially exact.
Differentiating works through the estimator: d(log det cA)/dc came back 152.198 against the
closed form n/c = 152.308.
order must stay below the Krylov dimension — and a pinned FEM operator's is small
Lanczos can only build a subspace as large as the number of distinct eigenvalues the probe sees. A jNO FEM operator has far fewer than it has rows: every Dirichlet-pinned DOF is an identity row, so eigenvalue 1.0 carries the pinned count as its multiplicity.
Measured on 2-D Poisson at mesh 0.25 — n=30, 16 pinned rows, only 15 distinct eigenvalues —
the default order=25 overran that and logdet returned NaN. It now raises instead, naming the
cause. The operator above is fine (198 rows, 151 distinct); a coarse mesh with a large boundary
fraction is not. Lower order, or apply the estimator to the free-DOF operator.
applyfun is not affected: its order is an upper bound, and it stops at the order that has
actually converged (see below). The stochastic three have no such ladder, so order is a real
request there.
applyfun picks its own order
order is an upper bound on the Krylov dimension, not an exact request — the same meaning
maxiter has for every iterative solver here. Running past the dimension a problem supports used
to degrade catastrophically and silently, because the Lanczos sub-diagonal does not collapse to
zero as a textbook "happy breakdown" would; it explodes (0.27 → 2.2 → … → 184.7), the
residual being pure round-off whose normalisation gives basis vectors of noise. On the 30-DOF
operator above, exp(A)·1 against a true 49.02:
order |
before | now |
|---|---|---|
| 15 | 3.35e-15 | 3.26e-15 |
| 20 | 1.44e-10 | 3.26e-15 |
| 25 | 5.11e+35 | 3.17e-15 |
| 29 | 1.85e+74 | 3.17e-15 |
Note order 20 — this is not only a fix for the catastrophic end, it is more accurate wherever round-off has begun to contaminate the basis. The rule is the standard a-posteriori one (Saad, SIAM J. Numer. Anal. 29(1), 1992, §4): accept the first order whose approximation agrees with its predecessor. Every nested approximation comes from the same decomposition, so it costs small dense eigendecompositions and no extra matvecs — measured overhead 0.17 ms at n=513 and 0.41 ms at n=8355.
Eigenproblems at scale
jno.solve.eigs / FEM.eigs have three paths for a symmetric pencil, chosen by the arguments (a fourth,
FEM.eigs(contour=...), is below; a non-symmetric pencil goes to ARPACK). With none of the iterative
arguments the pencil is reduced densely — exact, and right when you want the whole low spectrum of a
small problem, but it materializes the operator (O(N²) memory). Passing precond= selects
preconditioned LOBPCG (Knyazev, SIAM J. Sci. Comput. 23(2), 517–541, 2001), which only applies
K/M as matvecs and so runs where the dense reduction cannot.
Passing sigma= targets the k eigenvalues nearest the shift — interior modes (a cavity resonance
inside a band, a Brillouin-zone point away from the band edge), which no extremal-end iteration can reach
— by shift-invert with a depth-2 block Krylov space per sweep (Ericsson & Ruhe, Math. Comp. 35,
1980; Golub & Underwood, The block Lanczos method, 1977): θ = 1/(λ−σ) makes the near-σ modes dominant
with enormous transformed gaps, so the transformation is its own preconditioner and precond= is
rejected there.
A wide degenerate cluster at the same distance as the wanted modes — the hundreds of exact zeros of a
curl-curl (Maxwell) operator, with σ between 0 and the first cavity mode — converges too: the restart
keeps at most k copies of any one Ritz value, so the cluster cannot crowd out a wanted mode.
The inner solves against K − σM default to a host sparse LU factorized once (every sweep is then
triangular substitutions); linear= swaps in a different inner solver when a factorization is too big.
Constrained pencils (Dirichlet pins, periodic ties) compose: the reduced K − σM is assembled sparsely
through the same triplet remap the periodic solve reduction uses. log=True (or log=N) prints the
LOBPCG or shift-invert iterations while they run; the dense reduction and the non-symmetric ARPACK path
have no loop of their own to report.
K is the source-less jno.fem whose bilinear form is the stiffness; mass= takes the mass form
as a plain term list, which eigs assembles onto the same space for you:
u, v = d.fem_symbols()
xi, yi, _ = d.variable("interior", split=True)
ui, vi = u.bind(x=xi, y=yi), v.bind(x=xi, y=yi)
K = jno.fem([ui.x * vi.x + ui.y * vi.y]) # stiffness — no source term
lam, X = K.eigs(mass=[ui * vi], k=6) # dense; λ = ω² on a Neumann box
lam, X = K.eigs(mass=[ui * vi], k=6, precond=jno.precond.amg()) # LOBPCG, never densified
lam, X = K.eigs(mass=[ui * vi], k=4, sigma=60.0) # the 4 modes nearest λ = 60
jno.solve.eigs(...) is the lower-level form of the same thing and takes two assembled operators
rather than a fem and a term list — jno.solve.eigs(k=6)(K.operator[0], M).
Sweeps warm-start. X0= seeds the LOBPCG block with eigenvector guesses in the full DOF space
(restricted through any constraint elimination for you) — the classic sweep accelerator: a
parameter/frequency/k-point sweep passes each point the previous point's eigenvectors, so the iteration
only tracks the drift instead of re-finding the subspace from random.
Fewer columns than k are padded with the seeded random block. X0= is rejected on the dense path (it
would be silently ignored) and not yet wired into sigma= (the transformation converges from random in a
handful of sweeps anyway):
lam, X = K.eigs(mass=mass, k=6, precond=jno.precond.jacobi()) # first sweep point
lam2, X2 = K2.eigs(mass=mass, k=6, precond=jno.precond.jacobi(), X0=X) # next point: warm
Leaving a known subspace out. deflate= keeps LOBPCG M-orthogonal to a space -- written as formulas,
an (n, m) array or a callable, the values jno.precond.coarse
takes. A free structure's rigid-body modes then skip its zero eigenvalues, and a Neumann problem's
constant its first; with a Y that is not an invariant subspace the result is the constrained
(Courant–Fischer) spectrum on its M-orthogonal complement. LOBPCG path only (precond=):
The Rayleigh–Ritz runs in the M-inner product, so an ordinary FEM form's consistent (non-lumped)
mass matrix is handled directly, and XᵀMX = I holds on both paths. Eigenvalues are differentiable on
both — for simple eigenvalues; a degenerate cluster makes ∂λ/∂θ ill-defined either way (use the
trace of the cluster). LOBPCG freezes the converged eigenvector and differentiates the Rayleigh
quotient, which gives that derivative exactly without differentiating through the sweeps, but its
eigenvectors carry no gradient where the dense path's do.
tol/maxiter tune the iterative paths and are rejected without precond= or sigma=, so a
tolerance can never be silently ignored by the dense path.
Do not set tol near machine precision: on an ill-conditioned pencil the residual floors well above it
(≈4.4e-8 on a singular all-Neumann Laplacian with cond(K) ≈ 2e16), and a tolerance below that floor
burns the budget and NaN-poisons the LOBPCG result — which is the deliberate contract for an
exhausted budget, never a quietly under-converged spectrum.
The shift-invert gate measures the original pencil's residual of the k returned pairs (a θ-space
gate would flatter it), and a shift landing exactly ON an eigenvalue makes K − σM singular — the
garbage its factorization produces fails the same gate; perturb σ off the eigenvalue.
A shift-invert call that fails its gate raises ShiftInvertNotConverged (in
jno.utils.solver.eigen, a RuntimeError) naming the cause — exhausted budget or singular shift —
rather than returning NaN (also under a plain jax.grad); only under jit/vmap, where nothing can be
raised, is its result NaN-poisoned instead.
A non-symmetric pencil goes to ARPACK (Arnoldi), and every pair it returns is checked against the
pencil: a set that does not satisfy it raises RuntimeError instead of returning plausible values
that are not eigenvalues (ARPACK's generalized solve without a shift needs a symmetric M, which a
one-sided coupling block breaks). See the eigs routing note in FEM limitations.
Band structure — a Bloch tie in the pencil
A Bloch tie is an ordinary jno.fem term, u(A) - exp(-i k·a) u(B), and eigs takes it. A band
diagram is then a sweep over k with the pencil rebuilt at each point:
fem = jno.fem([
ui.x * vi.x + ui.y * vi.y, # stiffness
u(xl, yl) - numpy.exp(-1j * kx) * u(xr, yr), # Bloch tie across x
u(xb, yb) - numpy.exp(-1j * ky) * u(xt, yt), # ... and across y
])
lam, X = fem.eigs(mass=[eps * ui * vi], k=6) # λ = ω² at this k-point
A tie factor c ≠ 1 makes the relation complex (the Bloch factor exp(-i k·a) has |c| = 1; it is
c ≠ 1, not the modulus, that matters), so the tied K is assembled on the fused real-equivalent 2n
block over [Re; Im].
mass= is lifted into the same [[M_r, −M_i], [M_i, M_r]] layout — a mass form
assembled on its own carries neither the tie nor the complex literal, and an n × n mass against a 2n ×
2n stiffness is not a pencil — the periodic reduction is taken in that space, and the doubled spectrum
the block carries (every complex pair appears twice, as [Re; Im] and as its i-multiple [−Im; Re])
is thinned back down.
The modes come back complex on the n-space, which is what a band diagram wants.
Sweeps warm-start across k through the same X0=: a complex n-space guess is expanded back into
both of its real-equivalent partners for you.
Every mode in a region — jno.solve.contour
k-smallest and "nearest σ" both name a count. A leaky or lossy mode is naturally specified by
a region instead — everything inside this circle — which is what contour integration answers
(Beyn, Linear Algebra Appl. 436(10), 2012): the spectral projector is a resolvent integral
around the contour, taken by the trapezoid rule on a circle and reduced through an SVD of a random
probing block.
lam, X = fem.eigs(
mass=[eps * ui * vi],
contour=jno.solve.contour(centre=10.0, radius=8.0, points=48, probes=12),
)
Three things follow from the construction rather than from extra machinery:
- Differentiable through the eigenVECTORS. The method is
pointsshifted linear solves, one SVD and one small dense eigenproblem, all pure JAX — sojax.gradreaches the modes, not only the eigenvalues. A mode-shape objective (radiative Q, mode volume, field overlap) differentiates here; on the Arnoldi path it returns NaN by construction. - Non-Hermitian, and even nonlinear pencils, are native. You name a region; whatever is inside
comes back. A frequency-dependent material needs no auxiliary field and no polynomial
linearisation — measured on a Lorentz slab, the dispersive problem solved directly at half the
system size and
3e-14relative. - The shifted solves are independent, so they
vmap:points × probesmatrix-free solves rather than one long serial iteration.
contour= names a region, so it refuses the arguments that only mean something for a count —
which, sigma, precond, X0 — rather than ignoring them.
points sets accuracy; the trapezoid rule on a circle is spectrally accurate for an analytic integrand,
so digits come quickly (points=48 measured at ~1e-5 relative on a near-degenerate Bloch pair,
~1e-14 on a well-separated 1-D pencil). probes is the width of the probing block and must exceed
the number of eigenvalues inside — saturation is detected and raises rather than returning a quietly
wrong answer.
rank defaults to reading the singular-value gap, which is correct but data-dependent; pass it
explicitly when differentiating, and prefer one more than the number of eigenvalues inside: measured
on a pencil with 4 inside, rank 3 is wrong, rank 4 is right, rank 5 is right and differentiable, and
rank 6 still gives the right values but breaks the gradient by differentiating the SVD through
numerically-zero singular values.
Singular values — jno.solve.svd
eigs solves the symmetric pencil Kx = λMx. The two questions that are not eigenproblems need
the SVD of a possibly rectangular map, via Golub–Kahan bidiagonalization (Golub & Kahan,
J. SIAM Numer. Anal. Ser. B 2(2), 1965):
U, s, Vt = jno.solve.svd(snapshots, k=6) # POD basis from a (n_time, n_dofs) trajectory
U, s, Vt = jno.solve.svd(jacobian_op, k=20) # ill-posedness of a parameter-to-observable map
- POD / reduced-order models — the singular vectors are the energy-optimal basis and
ssays how many modes the trajectory actually needs. - Ill-posedness — the singular spectrum of the parameter-to-observable map says which parameter modes are recoverable at all; those below the noise floor are not, whatever the optimizer does.
A is touched only through its matvec, so it can be the JVP of a differentiable FEM solve rather than
an assembled matrix, and s differentiates back to whatever that matvec closes over.
depth (bidiagonalization steps, default 2k+10) must exceed k — the Ritz values converge from
below, so at depth == k only the largest singular value is meaningful (measured 95 % error on the
rest, against ~1e-15 at depth = 2k). Convergence is fast on the decaying spectra that make POD and
ill-posedness analysis worth doing, and slow on clustered ones (~3 % error at depth = 4k on a tight
cluster) — inspect s for a plateau if the spectrum may be flat.