pub struct JacobianColoring<M: Matrix> { /* private fields */ }Expand description
A structure for efficiently computing sparse Jacobians using graph coloring.
This struct uses graph coloring to group matrix columns that can be computed simultaneously using finite differences. By identifying columns that don’t share any non-zero rows (i.e., are structurally orthogonal), multiple Jacobian columns can be computed in parallel with a single function evaluation per color.
§Algorithm
The algorithm works as follows:
- Construct a graph where each column is a node, and edges connect columns that share at least one non-zero row
- Apply greedy graph coloring to assign colors to columns such that no two adjacent columns share the same color
- For each color, perturb all columns of that color simultaneously and compute the corresponding Jacobian entries
To enable 3., we pre-calculate for all colors:
- all the indices in the peturbed vector to peterb (i.e. set to 1) which is stored in
input_indices_per_color. - all the indices in the output vector to read the results from which is stored in
src_indices_per_color. - all the indices in the Jacobian matrix data vector to write the results to which is stored in
dst_indices_per_color.
This reduces the number of function evaluations from O(n) to O(χ), where χ is the chromatic number of the graph (typically much smaller than n for sparse matrices).
§Type Parameters
M- The matrix type used to store the Jacobian
Implementations§
Source§impl<M: Matrix> JacobianColoring<M>
impl<M: Matrix> JacobianColoring<M>
Sourcepub fn new(
sparsity: &impl MatrixSparsity<M>,
non_zeros: &[(usize, usize)],
ctx: M::C,
) -> Self
pub fn new( sparsity: &impl MatrixSparsity<M>, non_zeros: &[(usize, usize)], ctx: M::C, ) -> Self
Create a new Jacobian coloring from a sparsity pattern and list of non-zero entries.
This method constructs the graph coloring and organizes the indices for efficient Jacobian computation. The coloring is computed using a greedy algorithm that aims to minimize the number of colors (and thus function evaluations) needed.
§Arguments
sparsity- The sparsity pattern of the Jacobian matrixnon_zeros- A list of (row, column) pairs indicating non-zero entries in the Jacobianctx- The context for creating vectors and indices
§Returns
A new JacobianColoring instance ready to compute Jacobians efficiently.
Sourcepub fn jacobian_inplace<F: NonLinearOpJacobian<M = M, V = M::V, T = M::T, C = M::C>>(
&self,
op: &F,
x: &F::V,
t: F::T,
y: &mut F::M,
)
pub fn jacobian_inplace<F: NonLinearOpJacobian<M = M, V = M::V, T = M::T, C = M::C>>( &self, op: &F, x: &F::V, t: F::T, y: &mut F::M, )
Compute the Jacobian matrix in-place using the coloring scheme.
This method uses the pre-computed coloring to efficiently compute the Jacobian by
evaluating jac_mul once per color instead of once per column. For each color group,
it perturbs multiple columns simultaneously and extracts the corresponding Jacobian entries.
§Arguments
op- The nonlinear operator whose Jacobian is being computedx- The state vector at which to evaluate the Jacobiant- The time at which to evaluate the Jacobiany- The matrix to store the computed Jacobian (modified in-place)
§Note
The sparsity pattern of y must match the sparsity pattern used to create this coloring.
Sourcepub fn sens_inplace<F: NonLinearOpSens<M = M, V = M::V, T = M::T, C = M::C>>(
&self,
op: &F,
x: &F::V,
t: F::T,
y: &mut F::M,
)
pub fn sens_inplace<F: NonLinearOpSens<M = M, V = M::V, T = M::T, C = M::C>>( &self, op: &F, x: &F::V, t: F::T, y: &mut F::M, )
Compute the sensitivity matrix (∂F/∂p) in-place using the coloring scheme.
Similar to jacobian_inplace, but computes the sensitivity of the right-hand side
with respect to parameters rather than the state variables. This is used for forward
sensitivity analysis.
§Arguments
op- The nonlinear operator whose sensitivity matrix is being computedx- The state vector at which to evaluate the sensitivitiest- The time at which to evaluate the sensitivitiesy- The matrix to store the computed sensitivity matrix (modified in-place)
Sourcepub fn adjoint_inplace<F: NonLinearOpAdjoint<M = M, V = M::V, T = M::T, C = M::C>>(
&self,
op: &F,
x: &F::V,
t: F::T,
y: &mut F::M,
)
pub fn adjoint_inplace<F: NonLinearOpAdjoint<M = M, V = M::V, T = M::T, C = M::C>>( &self, op: &F, x: &F::V, t: F::T, y: &mut F::M, )
Compute the transposed Jacobian (adjoint) matrix in-place using the coloring scheme.
This method computes the transpose of the Jacobian, which is used in adjoint sensitivity analysis. The coloring is applied to the columns of the transposed matrix (which correspond to the rows of the original Jacobian).
§Arguments
op- The nonlinear operator whose transposed Jacobian is being computedx- The state vector at which to evaluate the transposed Jacobiant- The time at which to evaluate the transposed Jacobiany- The matrix to store the computed transposed Jacobian (modified in-place)
pub fn sens_adjoint_inplace<F: NonLinearOpSensAdjoint<M = M, V = M::V, T = M::T, C = M::C>>( &self, op: &F, x: &F::V, t: F::T, y: &mut F::M, )
pub fn matrix_inplace<F: LinearOp<M = M, V = M::V, T = M::T, C = M::C>>( &self, op: &F, t: F::T, y: &mut F::M, )
Trait Implementations§
Auto Trait Implementations§
impl<M> !Freeze for JacobianColoring<M>
impl<M> !RefUnwindSafe for JacobianColoring<M>
impl<M> Send for JacobianColoring<M>
impl<M> !Sync for JacobianColoring<M>
impl<M> Unpin for JacobianColoring<M>
impl<M> UnsafeUnpin for JacobianColoring<M>
impl<M> UnwindSafe for JacobianColoring<M>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.