ImplicitSolvers
ImplicitSolvers provides linear solves, root finding and the linear systems used by implicit-function adjoints. Solves return a solution and a SolverReport. Its residual is the primal Euclidean norm at the returned iterate.
using ImplicitSolvers
u, report = solve_root(u -> u .- cos.(u), [0.5], Picard(damping=0.5))
A = [2.0 1.0; 0.0 3.0]
x, report = linsolve(v -> A * v, [1.0, 2.0], GMRES())Tolerance(; atol, rtol) uses the iterate norm for roots and the right-hand-side norm for linear systems. DenseSolve uses a host factorization, including for device inputs. GMRES keeps the vector operations on the input array backend and uses host storage for the small Krylov least-squares problem. It supports real and complex systems. Pass approximate inverse actions as left=v -> M*v and right=v -> N*v. The same keywords on adjoint_solve and fixed_point_adjoint precondition the system those helpers solve. After each cycle GMRES checks norm(b - A*x) using the original operator and reports that residual. MINRES(; maxiters = 200) uses short recurrences for Hermitian systems, including real symmetric indefinite systems. It also checks the true residual.
Load Krylov to use KrylovJL with :gmres, :minres, or :cg. The backend accepts preconditioners as approximate inverse actions or matrices. With ldiv=true, it accepts objects that apply a preconditioner through ldiv!. MINRES and CG take a Hermitian positive definite left preconditioner. The report uses the true primal residual for all three methods. Krylov.jl 0.10 requires floating-point Krylov workspaces and rejects ForwardDiff dual elements.
solve_columns solves independent root equations with NewtonDescent or TrustRegion. The caller supplies the residual and the scale for each column, along with the Newton system or the trust-region merit, model and stability test. It returns one SolverReport and one stability flag per column.
ForwardDiff can differentiate the steps a solver takes. At a primal root supplied as the initial guess, a zero-step solve returns the initial guess's tangent. Use the adjoint helpers to compute sensitivities of the implicit equation with the caller's Jacobian products and parameter derivatives.
API
ImplicitSolvers.BroydenImplicitSolvers.DenseSolveImplicitSolvers.GMRESImplicitSolvers.KrylovJLImplicitSolvers.LimitedBroydenImplicitSolvers.MINRESImplicitSolvers.NewtonDescentImplicitSolvers.PicardImplicitSolvers.SolverReportImplicitSolvers.ToleranceImplicitSolvers.TrustRegionImplicitSolvers.adjoint_solveImplicitSolvers.fd_jacobianImplicitSolvers.fixed_point_adjointImplicitSolvers.linsolveImplicitSolvers.primalImplicitSolvers.solve_columnsImplicitSolvers.solve_root
ImplicitSolvers.Broyden — Type
Broyden(; init_scale=1)Good inverse Broyden with initial inverse init_scale * I. Stores a dense approximate inverse Jacobian and updates it from successive secant pairs.
ImplicitSolvers.DenseSolve — Type
DenseSolve(; spd=false)Solve by LU, or by Cholesky when spd=true asserts a symmetric positive definite matrix. Factorization uses host arrays. A callable operator is materialized by applying it to the coordinate basis.
The report's iterations is 1 after a successful factorization and 0 when the matrix has nonfinite entries or the factorization fails (the zero vector is then returned). Callers tell a singular system from an unmet tolerance by it (MyceliumLearning's graph adjoint does).
ImplicitSolvers.GMRES — Type
GMRES(; restart=40, maxiters=200)Restarted GMRES with double Gram–Schmidt and Givens rotations. maxiters bounds the number of Arnoldi steps. The residual is checked with extra operator applications, which are included in the report's nfe.
ImplicitSolvers.KrylovJL — Type
KrylovJL(method::Symbol = :gmres; memory=40, maxiters=200, ldiv=false)Use the Krylov.jl backend, which requires using Krylov. Choose :gmres, :minres (Hermitian), or :cg (Hermitian positive definite). memory is the restart length for GMRES. maxiters is the total primal iteration budget.
Pass approximate inverse actions or matrices as left and right to linsolve. With ldiv=true, pass objects supporting ldiv! instead. MINRES and CG accept only a Hermitian positive definite left preconditioner. ForwardDiff inputs use primal Krylov.jl solves and implicit tangent corrections. Dual numbers require ldiv=false.
ImplicitSolvers.LimitedBroyden — Type
LimitedBroyden(; memory=20, init_scale=1)Good inverse Broyden with at most memory rank-one corrections to init_scale * I. Drops the oldest correction when the history fills.
ImplicitSolvers.MINRES — Type
MINRES(; maxiters=200)Paige–Saunders minimum residual iteration for a Hermitian operator (real symmetric or complex Hermitian), including indefinite systems. maxiters bounds the number of Lanczos steps. The solver assumes Hermitian symmetry without checking it. For a non-Hermitian operator the recurrence can yield a wrong iterate. If its true residual misses the tolerance, the solve reports :stalled or :maxiters for finite arithmetic. Preconditioning is unsupported.
ImplicitSolvers.NewtonDescent — Type
NewtonDescent(; step=0.05, armijo=1e-4, min_damping=1e-8)Use a Newton step with Armijo backtracking on the residual norm in stable regions. Else subtract step * residual. If backtracking exhausts the damping budget, take the first step at or below min_damping.
ImplicitSolvers.Picard — Type
Picard(; damping=1)Fixed-point iteration u ← u - damping * F(u). A contractive iteration converges from initial guesses in its contraction region.
ImplicitSolvers.SolverReport — Type
SolverReport(converged, iterations, residual, nfe, reason)Record the status of a solve. residual is the primal Euclidean norm at the returned iterate. nfe counts calls to the residual or linear operator. reason is one of :converged, :maxiters, :nonfinite, and :stalled.
ImplicitSolvers.Tolerance — Type
Tolerance(; atol=nothing, rtol=nothing)Require the residual norm to be at most atol + rtol * scale. Each nothing uses max(1e-9, 30eps(T)) in the primal residual's precision T. Root solves use the iterate norm as their scale and linear solves use the right-hand-side norm.
ImplicitSolvers.TrustRegion — Type
TrustRegion(; radius=nothing, cg_maxiters=200, cg_rtol=1e-8, precision=nothing)Minimize a supplied merit using quadratic models in a ball. Use an eigendecomposition with a matrix Hessian and Steihaug CG with a Hessian action. The default initial radius is 0.1 * sqrt(max(size(U0, 1), 1)). The default arithmetic precision is the machine epsilon of the primal iterate type. Pass precision when the merit is evaluated in different arithmetic.
ImplicitSolvers.adjoint_solve — Method
adjoint_solve(vjp, g::AbstractVector, alg; tol=Tolerance(), left=nothing, right=nothing)Solve J' * λ = g with linsolve. Pass the action v -> J' * v or the adjoint matrix itself. The optional GMRES preconditioners apply to J'. MINRES requires J' to be Hermitian. Return (λ, report).
ImplicitSolvers.fd_jacobian — Method
fd_jacobian(F, u::AbstractVector; step=nothing)Return the dense Jacobian of F by central differences on host vectors. The probe displacement in coordinate j is step * max(1, abs(u[j])). The default multiplier is cbrt(eps(T)) in the primal coordinate type. Divide by the actual separation of the two rounded probes.
ImplicitSolvers.fixed_point_adjoint — Method
fixed_point_adjoint(vjp, g::AbstractVector, alg; tol=Tolerance(), left=nothing, right=nothing)Solve (I - J') * λ = g with linsolve. Pass the action v -> J' * v or the adjoint matrix itself. The optional GMRES preconditioners apply to I - J'. MINRES requires I - J' to be Hermitian. Return (λ, report).
ImplicitSolvers.linsolve — Method
linsolve(A, b::AbstractVector, alg; tol=Tolerance(), left=nothing, right=nothing)Return (x, report) for the square system A*x = b. Supply a square matrix or a callable v -> A*v mapping vectors of length length(b) to vectors of the same length. With GMRES, left and right can apply approximate inverses. KrylovJL also accepts matrix or ldiv! preconditioners. The report's residual and the initial convergence check use the true primal residual norm and the primal norm of b. DenseSolve and MINRES require both preconditioners to be nothing.
With ForwardDiff, Krylov solvers check the tangent residual after primal convergence. Correction solves contribute to the report's iterations and nfe. If any correction fails to converge, the report gives the first failure status while residual still describes the primal solve.
ImplicitSolvers.primal — Method
primal(x)Return the value used for solver decisions and diagnostics. This is the identity by default. With ForwardDiff loaded, it recursively unwraps dual numbers, also inside a complex number.
ImplicitSolvers.solve_columns — Method
solve_columns(problem, U0::AbstractMatrix, alg; tol::Tolerance, maxiters)
-> (U, reports::Vector{SolverReport}, stable::Vector{Bool})Solve independent root equations using NewtonDescent or TrustRegion. Supply problem as a named tuple of callbacks. The matrix U passed to a callback contains just the columns to evaluate, in the order of their original indices in cols. Callbacks should treat matrices passed to them as read-only.
residual(U, cols)returns a residual matrix shaped likeU.scale(U, cols)returns a vector of tolerance scales, one per column.- For Newton descent,
system(U, cols)returns(flags, solves), a Boolean vector and a collection of functionsr -> J⁻¹*r, both in column order. - For the trust region,
merit(U, R, cols)returns a vector of merit values.model(U, R, cols)returns a collection of(g, H)pairs, with gradient vectorgand a symmetric matrix or a callablev -> H*v.stable(U, cols)returns a vector of flags for the reported points.
Optional residual_norms(R) supplies the column Euclidean norms when the caller needs a particular reduction order. Newton descent also accepts trial_residual with the same contract as residual for line searches that discard model state. By default the driver uses primal column norms and the same residual callback at all points. Set round_steps = true to store directions in the residual element type and predicted decreases in the iterate element type before testing trials. By default each direction and prediction retains its computed precision. State, residuals and steps use the input array backend. Callback collections of functions or models are host collections, as are the reports. A trust-region radius below the arithmetic floor reports :stalled when the root tolerance is unmet, including at a non-root Gauss–Newton minimum. nfe counts all residual evaluations, including trials, for each column.
ImplicitSolvers.solve_root — Method
solve_root(F, u0::AbstractVector, alg; tol=Tolerance(), maxiters=100, history=nothing)Return (u, report) for the root equation F(u) = 0. Supply an out-of-place residual with the same length as u0. The report counts the initial evaluation and one evaluation per completed step. With LimitedBroyden, pass a Ref{Any}() as history to receive (; a, b, scale) describing the final inverse approximation scale * I + sum(ai * bi').