Sparse¤
tatva.sparse.ColoredMatrix
dataclass
¤
Class to represent the sparsity pattern of a matrix, including the row pointers, column indices, and colors for graph coloring.
Methods:
-
from_csr–Create a SparseMatrix instance from a SciPy CSR matrix and optional colors.
-
to_csr–Convert the sparse matrix to SciPy's CSR format.
-
to_bcoo–Convert the sparse matrix to JAX's BCOO format.
-
to_bcsr–Convert the sparse matrix to JAX's BCSR format.
-
to_dense–Convert the sparse matrix to a dense array.
Attributes:
-
data(Array) –Data values of the sparse matrix
-
indptr(Array) –Row pointers for the original sparsity pattern (CSR format)
-
indices(Array) –Column indices for the original sparsity pattern (CSR format)
-
shape(tuple[int, int]) –Shape of the sparse matrix
-
colors(Array) –Colors assigned to each degree of freedom (DOF) for graph coloring
data
class-attribute
instance-attribute
¤
Data values of the sparse matrix
indptr
class-attribute
instance-attribute
¤
Row pointers for the original sparsity pattern (CSR format)
indices
class-attribute
instance-attribute
¤
Column indices for the original sparsity pattern (CSR format)
shape
class-attribute
instance-attribute
¤
Shape of the sparse matrix
colors
class-attribute
instance-attribute
¤
Colors assigned to each degree of freedom (DOF) for graph coloring
from_csr
classmethod
¤
Create a SparseMatrix instance from a SciPy CSR matrix and optional colors.
tatva.sparse.jacfwd
¤
jacfwd(
fn: Callable[Concatenate[Array, P], Array],
colored_matrix: ColoredMatrix,
*,
color_batch_size: int | None = None,
) -> Callable[Concatenate[Array, P], ColoredMatrix]
Returns a function that computes the Jacobian of fn using forward-mode automatic differentiation
and graph coloring. The returned function takes the same arguments as fn and returns a sparse Jacobian
as a new instance of Sparsity.
Parameters:
-
(fn¤Callable[Concatenate[Array, P], Array]) –Function for which to compute the Jacobian. Must take an Array as its first argument and return an Array. Will be differentiated with respect to the first argument.
-
(colored_matrix¤ColoredMatrix) –An instance of ColoredMatrix representing the sparsity pattern and coloring of the Jacobian.
-
(color_batch_size¤int | None, default:None) –Optional batch size for processing colors. If None, processes all colors at once. If memory usage is a concern, set to a smaller value to process colors in batches.
Returns:
-
Callable[Concatenate[Array, P], ColoredMatrix]–A function that computes the sparse Jacobian of
fnat a given input, returning -
Callable[Concatenate[Array, P], ColoredMatrix]–a new instance of
ColoredMatrixcontaining the Jacobian values in thedatafield.
tatva.sparse.linearized_jacfwd
¤
linearized_jacfwd(
fn: Callable[Concatenate[Array, P], Array],
colored_matrix: ColoredMatrix,
*,
color_batch_size: int | None = None,
) -> Callable[
Concatenate[Array, P], tuple[Array, ColoredMatrix]
]
Like sparse.jacfwd but uses jax.linearize to avoid redundant forward passes. In general that means the memory usage scales with size of the computation.
Parameters:
-
(fn¤Callable[Concatenate[Array, P], Array]) –Function for which to compute the Jacobian. Must take an Array as its first argument and return an Array. Will be differentiated with respect to the first argument.
-
(colored_matrix¤ColoredMatrix) –An instance of ColoredMatrix representing the sparsity pattern and coloring of the Jacobian.
-
(color_batch_size¤int | None, default:None) –Optional batch size for processing colors. If None, processes all colors at once. If memory usage is a concern, set to a smaller value to process colors in batches.
Returns:
-
Callable[Concatenate[Array, P], tuple[Array, ColoredMatrix]]–a function that computes both the primal values and the sparse Jacobian in a single
-
Callable[Concatenate[Array, P], tuple[Array, ColoredMatrix]]–call, sharing the forward pass.