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.
tatva.sparse.pattern_from_energy
¤
pattern_from_energy(
energy_fn: Callable[Concatenate[Array, P], Array],
n_dofs: int,
*static_args,
) -> sps.csr_matrix
Return the sparsity pattern of d²E/du² as a symmetric CSR matrix for a scalar energy function E(u) where u has n_dofs degrees of freedom.
Parameters:
-
(energy_fn¤Callable[Concatenate[Array, P], Array]) –scalar JAX array energy function E(u, *static_args) as a function of input variable u and optional static arguments
-
(n_dofs¤int) –number of DOFs (integer size of flattened input array u)
-
–static_args¤extra args passed to energy_fn, treated as constants
Returns:
-
csr_matrix–A symmetric CSR matrix of shape (n_dofs, n_dofs) with binary entries indicating
-
csr_matrix–the sparsity pattern of the Hessian d²E/du².
tatva.sparse.pattern_from_virtual_work
¤
pattern_from_virtual_work(
virtual_work_fn: Callable[Concatenate[Array, P], Array],
n_dofs: int,
trial_arg: str,
test_arg: str,
*static_args,
) -> sps.csr_matrix
Return the sparsity pattern of the tangent stiffness matrix K = dR/du = d²G/dvdu for a virtual work function virtual_work_fn(*args) as a CSR matrix.
Parameters:
-
(virtual_work_fn¤Callable[Concatenate[Array, P], Array]) –scalar JAX array (virtual work) as a function of trial and test variables (e.g., G(u, v, *static_args))
-
(n_dofs¤int) –number of DOFs (integer size of flattened input arrays u and v)
-
(trial_arg¤str) –parameter name of the trial function in virtual_work_fn
-
(test_arg¤str) –parameter name of the test function in virtual_work_fn
-
–static_args¤extra arguments (e.g., mesh coordinates, parameters) passed to virtual_work_fn, treated as constants
Returns:
-
csr_matrix–A CSR matrix of shape (n_dofs, n_dofs) with binary entries indicating the sparsity
-
csr_matrix–pattern of the tangent stiffness matrix K = dR/du = d²G/dvdu, where G is the
-
csr_matrix–virtual work and u,v are the trial and test functions respectively.
tatva.sparse.pattern_from_compound
¤
pattern_from_compound(
compound_cls: type[Compound], block_wise: bool = False
) -> sps.csr_matrix | list[list[sps.csr_matrix]]
Create a sparsity pattern automatically from a Compound class and its attached mesh.
Nodal fields are fully coupled within elements. All other fields (Local, Shared) are only connected to themselves (diagonal entries).
Parameters:
-
(compound_cls¤type[Compound]) –The Compound class defining the state layout.
-
(block_wise¤bool, default:False) –If True, return the pattern as a list of lists of sparse matrices corresponding to the compound fields/blocks. Stacked fields are one block.