sparse_utils
Sparse linear algebra helpers built on jax.experimental.sparse.
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 |
Source code in openscvx/discretization/sparse_utils/sparse_jacobian.py
precompute_sparse_indices(pattern: np.ndarray) -> Tuple[jnp.ndarray, jnp.ndarray, int]
¶
Pre-compute BCOO index arrays from a boolean sparsity pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
ndarray
|
Boolean |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray, int]
|
|
Source code in openscvx/discretization/sparse_utils/bcoo_helpers.py
sparse_matmul_batched(dense_jac: jnp.ndarray, rhs: jnp.ndarray, nz_rows: jnp.ndarray, nz_cols: jnp.ndarray, m: int, n: int) -> jnp.ndarray
¶
Sparse-dense batched matmul: sparse(dense_jac) @ rhs.
Extracts nonzero values from the dense batch using pre-computed indices, constructs a BCOO matrix per batch item, and multiplies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dense_jac
|
ndarray
|
|
required |
rhs
|
ndarray
|
|
required |
nz_rows
|
ndarray
|
Row indices of structural nonzeros. |
required |
nz_cols
|
ndarray
|
Column indices of structural nonzeros. |
required |
m
|
int
|
Number of rows of the sparse matrix. |
required |
n
|
int
|
Number of columns of the sparse matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|