hvp
Stable function · Evaluate the Jacobian
Tier: stable · Kind: function · Topic: Evaluate the Jacobian
| Julia | Python | Rust |
|---|---|---|
hvp! | hvp | hvp |
Julia
hvp!(g::AbstractVector, hv::HVPEvaluator, pairs, u::AbstractVector, p, t::Real; form)
hvp!(g, hv::HVPEvaluator, pairs, u, p, t; form = :contract) -> g
The second-order contraction at (u, p, t):
g += ∇_{a2} Σᵢ μᵢᵀ J(u) wᵢ (pair (:states, a2))
g += ∇_{a2} (Σᵢ wᵢ μᵢ)ᵀ f_t (pair (:time, a2); each wᵢ a scalar)
with J = ∂f/∂(a1). pairs is one (μ, w) tuple or a collection of them
(one per Rosenbrock stage); μ is indexed like hv.rownames, w like
hv.colnames and g like hv.col2names. g ACCUMULATES — zero it first
for a single product.
form = :contract (default) sums the stages into M(row, col) once and runs
g[col2] += coef2 · M(row, col) over the points. On a (:states, :states)
pair form = :symmetric uses the symmetry of ∇²(μᵀf) instead,
g[col] += μ[row] · coef2 · w[col2] per stage, which needs no M.
Python
hvp(g, obj, pairs, model_name: str | None = None, wrt=None, states: dict | None = None, params: dict | None = None, t: float = 0.0, forcing=None, form: str = 'contract')
The second-order contraction at a point, as a scatter-add over the second-order band points with no tensor formed:
g += ∇_{a2} Σᵢ μᵢᵀ J wᵢ (pair (states, a2))
g += ∇_{a2} (Σᵢ wᵢ μᵢ)ᵀ f_t (pair (time, a2); each wᵢ a scalar)
with J = ∂f/∂(a1). obj is an HVPEvaluator
(built once
with prepare_hvp
; its pair and forcing set apply) or anything
prepare_hvp
takes, prepared for this one call with wrt
(default ("states", "states")). pairs is one (μ, w) tuple or
a collection of them (one per Rosenbrock stage): μ maps row (state
cell) names to values, w maps column names to values — or is a
number on a time pair. A name a band point needs that they do not bind
raises ValueError.
g is a dict keyed by second-column name, or (on a "parameters" /
"forcing" second axis) a flat sequence over col2names. It
ACCUMULATES — zero it first for a single product; a dict gains a key for
every second column a band point reaches, zero-valued or not.
form="contract" (default) runs g[col2] += coef2 · Σᵢ μᵢ[row] wᵢ[col] over the points. On a ("states", "states") pair
form="symmetric" uses the symmetry of ∇²(μᵀf) instead, g[col] += μ[row] · coef2 · w[col2] per stage (g then keyed by the first
column). Returns g.
Rust
pub fn hvp(file: &EsmFile, model_name: Option<&str>, wrt: (Wrt, Wrt), forcing: Option<&[String]>, env: &Env, pairs: &[HvpPair], form: HvpForm, g: &mut BTreeMap<String, f64>) -> Result<(), BandError>
The second-order contraction at a point (spec §4.4), with no tensor formed:
g += ∇_{a2} Σᵢ μᵢᵀ J(u) wᵢ (pair (states, a2))
g += ∇_{a2} (Σᵢ wᵢ μᵢ)ᵀ f_t (pair (time, a2); wᵢ binds "t")
with J = ∂f/∂(a1). pairs holds one (μ, w) per Rosenbrock stage,
μ keyed by row cell name and w by first-column cell name; a name a
point reads that they do not bind is an error. g is keyed by
second-column cell name ("T[2]", "k3", "F[1,4]"; on
HvpForm::Symmetric by first-column name), gains a key for every
column a band point reaches, and ACCUMULATES — clear it first for a
single product. Parameters env leaves unbound read the document
defaults. This prepares the evaluator for one call; a loop calls
HvpEvaluator::hvp on one prepare_hvp
instead.