Derivatives
Selective coordinate-derivative utilities avoid materializing full Jacobians and Hessians that a PDE does not use. Both use forward-mode differentiation and compute only the derivatives you request.
Write the model as a function of scalar coordinates, in model-input order. The second argument, argnums, holds the
positions of the coordinates to differentiate:
def solutions(t, x):
return model_apply(model_state, jnp.stack((t, x)))[0] # one field
value, (du_dt, du_dx) = value_and_jacobian(solutions, (0, 1))(t, x)
(d2u_dx2,) = hessian_diagonal(solutions, 1)(t, x)
The derivative tuple follows the order of argnums. Each derivative has the same structure as the function output, so
a function returning several fields gives every field's derivative in one call:
def solutions(x, y):
return model_apply(model_state, jnp.stack((x, y))) # [u, v, p]
(u, v, p), (d_x, d_y) = value_and_jacobian(solutions, (0, 1))(x, y)
u_x, v_x, p_x = d_x
(u_xx, v_xx, _), (u_yy, v_yy, _) = hessian_diagonal(solutions, (0, 1))(x, y)
The function closes over model_state, so gradients with respect to the parameters still flow through it under
jax.grad. Evaluate one point at a time and batch the point residual with jax.vmap.
Both helpers raise ValueError when argnums is empty or repeats an index, or when a selected argument is not a
scalar. They raise TypeError when an entry of argnums is not an integer, including a Boolean.
value_and_jacobian
[source]
value_and_jacobian(
function: Callable[..., Any], argnums: ArgumentNumbers
) -> Callable[..., tuple[Any, tuple[Any, ...]]]
Build a forward-mode value-and-Jacobian function for selected scalar arguments.
The returned callable linearizes one primal evaluation and applies one JVP per selected argument. Each selected
argument must be a scalar array. PINN equations can therefore split a point into scalar coordinates, call this
helper at one point, and apply :func:jax.vmap outside it for collocation batches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[..., Any]
|
Pure function differentiated with respect to selected scalar positional arguments. |
required |
argnums
|
ArgumentNumbers
|
Unique positional argument number or numbers, ordered like the requested Jacobian entries. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., tuple[Any, tuple[Any, ...]]]
|
Callable returning the primal output and one derivative PyTree per selected argument. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
hessian_diagonal
[source]
hessian_diagonal(
function: Callable[..., Any], argnums: ArgumentNumbers
) -> Callable[..., tuple[Any, ...]]
Build forward-mode pure second derivatives for selected scalar arguments.
Unlike a complete Hessian, this helper computes only d²function / darg² for each selected argument. It avoids
tracing unused mixed derivatives in PDE operators such as one-dimensional diffusion and coordinate Laplacians.
Every selected argument must be a scalar array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable[..., Any]
|
Pure function differentiated with respect to selected scalar positional arguments. |
required |
argnums
|
ArgumentNumbers
|
Unique positional argument number or numbers, ordered like the requested Hessian diagonal entries. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., tuple[Any, ...]]
|
Callable returning one pure-second-derivative PyTree per selected argument. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |