Automatic Differentiation¶
gradient(fn, argnum=0)
¶
Compute the gradient of a scalar-valued function w.r.t. the input at index argnum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
NumericalFunction[I, Tensor]
|
A NumericalFunction with one or more inputs and a scalar output. |
required |
argnum
|
int
|
Index of the input to differentiate w.r.t. (default 0). |
0
|
Returns:
| Type | Description |
|---|---|
NumericalFunction[I, Tensor]
|
A NumericalFunction computing the gradient (same shape as input[argnum]). |
Source code in src/anvil/ad/gradient.py
jacobian(fn, argnum=0)
¶
Compute the Jacobian of fn w.r.t. the input at index argnum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
NumericalFunction[I, Tensor]
|
A NumericalFunction with one or more inputs and a single 1D output. |
required |
argnum
|
int
|
Index of the input to differentiate w.r.t. (default 0). |
0
|
Returns:
| Type | Description |
|---|---|
NumericalFunction[I, Tensor]
|
A NumericalFunction computing the Jacobian matrix (output_dim, input_dim). |
Source code in src/anvil/ad/jacobian.py
spjacobian(fn, argnum=0, unroll=False, sep_uncompression=True, symmetric=False)
¶
Compute the sparse Jacobian of fn w.r.t. the input at index argnum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
NumericalFunction[I, Tensor]
|
A NumericalFunction with one or more inputs and a single 1D output. |
required |
argnum
|
int
|
Index of the input to differentiate w.r.t. (default 0). |
0
|
unroll
|
bool
|
If True, unroll the JVP loop (useful for small problems). |
False
|
sep_uncompression
|
bool
|
If True, separate the uncompression step (often faster). |
True
|
symmetric
|
bool
|
If True, use star coloring for symmetric matrices (e.g., Hessians). |
False
|
Returns:
| Type | Description |
|---|---|
SparseNumericalFunction[I]
|
A SparseNumericalFunction that returns a |
Source code in src/anvil/ad/jacobian.py
hessian(fn, argnum=0)
¶
Compute the Hessian of a scalar-valued function w.r.t. the input at index argnum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
NumericalFunction[I, Tensor]
|
A NumericalFunction with one or more inputs and a scalar output. |
required |
argnum
|
int
|
Index of the input to differentiate w.r.t. (default 0). |
0
|
Returns:
| Type | Description |
|---|---|
NumericalFunction[I, Tensor]
|
A NumericalFunction computing the Hessian matrix (input_dim, input_dim). |
Source code in src/anvil/ad/hessian.py
sphessian(fn, argnum=0, unroll=False)
¶
Compute the sparse Hessian of a scalar-valued function w.r.t. the input at index argnum.
The returned sparse matrix is upper-triangular in CSC format (as required by PIQP).
Implemented as spjacobian(gradient(fn)), using star coloring for symmetry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
NumericalFunction[I, Tensor]
|
A NumericalFunction with one or more inputs and a scalar output. |
required |
argnum
|
int
|
Index of the input to differentiate w.r.t. (default 0). |
0
|
unroll
|
If True, unroll the inner JVP loop (useful for small problems). |
False
|
Returns:
| Type | Description |
|---|---|
SparseNumericalFunction[I]
|
A SparseNumericalFunction that returns a |