Skip to main content

JacobianColoring

Struct JacobianColoring 

Source
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:

  1. Construct a graph where each column is a node, and edges connect columns that share at least one non-zero row
  2. Apply greedy graph coloring to assign colors to columns such that no two adjacent columns share the same color
  3. 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>

Source

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 matrix
  • non_zeros - A list of (row, column) pairs indicating non-zero entries in the Jacobian
  • ctx - The context for creating vectors and indices
§Returns

A new JacobianColoring instance ready to compute Jacobians efficiently.

Source

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 computed
  • x - The state vector at which to evaluate the Jacobian
  • t - The time at which to evaluate the Jacobian
  • y - 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.

Source

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 computed
  • x - The state vector at which to evaluate the sensitivities
  • t - The time at which to evaluate the sensitivities
  • y - The matrix to store the computed sensitivity matrix (modified in-place)
Source

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 computed
  • x - The state vector at which to evaluate the transposed Jacobian
  • t - The time at which to evaluate the transposed Jacobian
  • y - The matrix to store the computed transposed Jacobian (modified in-place)
Source

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, )

Source

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§

Source§

impl<M: Matrix> Clone for JacobianColoring<M>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<M> !Freeze for JacobianColoring<M>

§

impl<M> !RefUnwindSafe for JacobianColoring<M>

§

impl<M> Send for JacobianColoring<M>
where <M as MatrixCommon>::V: Send, <<M as MatrixCommon>::V as Vector>::Index: Send,

§

impl<M> !Sync for JacobianColoring<M>

§

impl<M> Unpin for JacobianColoring<M>
where <M as MatrixCommon>::V: Unpin, <<M as MatrixCommon>::V as Vector>::Index: Unpin,

§

impl<M> UnsafeUnpin for JacobianColoring<M>
where <M as MatrixCommon>::V: UnsafeUnpin,

§

impl<M> UnwindSafe for JacobianColoring<M>
where <M as MatrixCommon>::V: UnwindSafe, <<M as MatrixCommon>::V as Vector>::Index: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DistributionExt for T
where T: ?Sized,

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,