sparse_jacobian
Sparse Jacobian computation via graph coloring and jax.experimental.sparse.
This module provides utilities to compute Jacobians efficiently when the
sparsity pattern is known at compile time. Instead of computing all n_x
(or n_u) columns via jax.jacfwd, we use column graph coloring to
determine the minimum number of directional derivatives (JVPs) needed,
then reconstruct the sparse Jacobian from the compressed results.
The key entry point is :func:make_sparse_jacobian_fns, which returns
vmapped Jacobian callables (matching the dense jax.vmap(jax.jacfwd(...))
signature) that internally use the sparse path.
color_columns(pattern: np.ndarray) -> np.ndarray
¶
Greedy column coloring of a boolean sparsity pattern.
Two columns i and j can share a color when they have no row
in common where both are nonzero. This function assigns the fewest
colors possible via a greedy (first-fit) algorithm over columns
ordered by decreasing number of nonzeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
ndarray
|
Boolean |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Integer array of length |
ndarray
|
(0-indexed). The number of distinct colors is |
Source code in openscvx/discretization/sparse_utils/sparse_jacobian.py
make_sparse_jacobian_fns(f: Callable, A_c_pattern: Optional[np.ndarray], B_c_pattern: Optional[np.ndarray], n_x: int, n_u: int) -> Tuple[Callable, Callable]
¶
Create vmapped sparse Jacobian functions for df/dx and df/du.
If a sparsity pattern is fully dense or None, falls back to the
standard jax.jacfwd path for that Jacobian.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable
|
Dynamics function |
required |
A_c_pattern
|
Optional[ndarray]
|
Boolean |
required |
B_c_pattern
|
Optional[ndarray]
|
Boolean |
required |
n_x
|
int
|
Number of state dimensions. |
required |
n_u
|
int
|
Number of control dimensions. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
|
Callable
|
signature |