diffsol 0.12.2

A library for solving ordinary differential equations (ODEs) in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
use std::collections::HashSet;

use crate::{
    LinearOp, LinearOpTranspose, Matrix, MatrixSparsity, NonLinearOp, NonLinearOpAdjoint,
    NonLinearOpJacobian, NonLinearOpSens, NonLinearOpSensAdjoint, Scalar, Vector, VectorIndex,
};
use num_traits::{One, Zero};

use self::{coloring::nonzeros2graph, greedy_coloring::color_graph_greedy};

pub mod coloring;
pub mod graph;
pub mod greedy_coloring;

macro_rules! gen_find_non_zeros_nonlinear {
    ($name:ident, $op_fn:ident, $op_trait:ident, $nrows:ident, $ncols:ident) => {
        /// Find the non-zero entries of the $name matrix of a non-linear operator.
        /// TODO: This function is not efficient for non-host vectors and could be part of the Vector trait
        ///       to allow for more efficient implementations. It's ok for now since this is only used once
        ///       during the setup phase.
        pub fn $name<F: NonLinearOp + $op_trait + ?Sized>(
            op: &F,
            x: &F::V,
            t: F::T,
        ) -> Vec<(usize, usize)> {
            let mut v = F::V::zeros(op.$ncols(), op.context().clone());
            let mut col = F::V::zeros(op.$nrows(), op.context().clone());
            let mut triplets = Vec::with_capacity(op.nstates());
            for j in 0..op.$ncols() {
                v.set_index(j, F::T::NAN);
                op.$op_fn(x, t, &v, &mut col);
                for i in 0..op.nout() {
                    if col.get_index(i).is_nan() {
                        triplets.push((i, j));
                    }
                    col.set_index(i, F::T::zero());
                }
                // OR:
                //col.clone_as_vec().into_iter().for_each(|v| {
                //    if v.is_nan() {
                //        triplets.push((0, 0));
                //    }
                //});
                col.fill(F::T::zero());
                v.set_index(j, F::T::zero());
            }
            triplets
        }
    };
}

gen_find_non_zeros_nonlinear!(
    find_jacobian_non_zeros,
    jac_mul_inplace,
    NonLinearOpJacobian,
    nout,
    nstates
);
gen_find_non_zeros_nonlinear!(
    find_adjoint_non_zeros,
    jac_transpose_mul_inplace,
    NonLinearOpAdjoint,
    nstates,
    nout
);
gen_find_non_zeros_nonlinear!(
    find_sens_non_zeros,
    sens_mul_inplace,
    NonLinearOpSens,
    nstates,
    nparams
);
gen_find_non_zeros_nonlinear!(
    find_sens_adjoint_non_zeros,
    sens_transpose_mul_inplace,
    NonLinearOpSensAdjoint,
    nparams,
    nstates
);

macro_rules! gen_find_non_zeros_linear {
    ($name:ident, $op_fn:ident $(, $op_trait:tt )?) => {
        /// Find the non-zero entries of the $name matrix of a non-linear operator.
        /// TODO: This function is not efficient for non-host vectors and could be part of the Vector trait
        ///       to allow for more efficient implementations. It's ok for now since this is only used once
        ///       during the setup phase.
        pub fn $name<F: LinearOp + ?Sized $(+ $op_trait)?>(op: &F, t: F::T) -> Vec<(usize, usize)> {
            let mut v = F::V::zeros(op.nstates(), op.context().clone());
            let mut col = F::V::zeros(op.nout(), op.context().clone());
            let mut triplets = Vec::with_capacity(op.nstates());
            for j in 0..op.nstates() {
                v.set_index(j, F::T::NAN);
                op.$op_fn(&v, t, &mut col);
                for i in 0..op.nout() {
                    if col.get_index(i).is_nan() {
                        triplets.push((i, j));
                    }
                    col.set_index(i, F::T::zero());
                }
                // OR:
                //col.clone_as_vec().into_iter().for_each(|v| {
                //    if v.is_nan() {
                //        triplets.push((0, 0));
                //    }
                //});
                v.set_index(j, F::T::zero());
            }
            triplets
        }
    };
}

gen_find_non_zeros_linear!(find_matrix_non_zeros, call_inplace);
gen_find_non_zeros_linear!(
    find_transpose_non_zeros,
    call_transpose_inplace,
    LinearOpTranspose
);

use std::cell::RefCell;

/// 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
pub struct JacobianColoring<M: Matrix> {
    dst_indices_per_color: Vec<<M::V as Vector>::Index>,
    src_indices_per_color: Vec<<M::V as Vector>::Index>,
    input_indices_per_color: Vec<<M::V as Vector>::Index>,
    scratch_v: RefCell<M::V>,
    scratch_col: RefCell<M::V>,
}

impl<M: Matrix> Clone for JacobianColoring<M> {
    fn clone(&self) -> Self {
        Self {
            dst_indices_per_color: self.dst_indices_per_color.clone(),
            src_indices_per_color: self.src_indices_per_color.clone(),
            input_indices_per_color: self.input_indices_per_color.clone(),
            scratch_v: RefCell::new(self.scratch_v.borrow().clone()),
            scratch_col: RefCell::new(self.scratch_col.borrow().clone()),
        }
    }
}

impl<M: Matrix> JacobianColoring<M> {
    /// 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.
    pub fn new(sparsity: &impl MatrixSparsity<M>, non_zeros: &[(usize, usize)], ctx: M::C) -> Self {
        let ncols = sparsity.ncols();
        let graph = nonzeros2graph(non_zeros, ncols);
        let coloring = color_graph_greedy(&graph);
        let max_color = coloring.iter().max().copied().unwrap_or(0);
        let mut dst_indices_per_color = Vec::new();
        let mut src_indices_per_color = Vec::new();
        let mut input_indices_per_color = Vec::new();
        for c in 1..=max_color {
            let mut rows = Vec::new();
            let mut cols = Vec::new();
            let mut indices = Vec::new();
            for (i, j) in non_zeros {
                if coloring[*j] == c {
                    rows.push(*i);
                    cols.push(*j);
                    indices.push((*i, *j));
                }
            }
            let dst_indices = sparsity.get_index(indices.as_slice(), ctx.clone());
            let src_indices = <M::V as Vector>::Index::from_vec(rows, ctx.clone());
            let unique_cols: HashSet<_> = HashSet::from_iter(cols.iter().cloned());
            let unique_cols = unique_cols.into_iter().collect::<Vec<_>>();
            let input_indices = <M::V as Vector>::Index::from_vec(unique_cols, ctx.clone());
            dst_indices_per_color.push(dst_indices);
            src_indices_per_color.push(src_indices);
            input_indices_per_color.push(input_indices);
        }
        let scratch_v = RefCell::new(M::V::zeros(sparsity.ncols(), ctx.clone()));
        let scratch_col = RefCell::new(M::V::zeros(sparsity.nrows(), ctx.clone()));
        Self {
            dst_indices_per_color,
            src_indices_per_color,
            input_indices_per_color,
            scratch_v,
            scratch_col,
        }
    }

    //pub fn new_non_linear<F: NonLinearOp<M = M>>(op: &F) -> Self {
    //    let non_zeros = find_non_zeros_nonlinear(op, &F::V::zeros(op.nstates()), F::T::zero());
    //    Self::new_from_non_zeros(op, non_zeros)
    //}

    //pub fn new_linear<F: LinearOp<M = M>>(op: &F) -> Self {
    //    let non_zeros = find_non_zeros_linear(op, F::T::zero());
    //    Self::new_from_non_zeros(op, non_zeros)
    //}

    /// 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.
    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,
    ) {
        let mut v = self.scratch_v.borrow_mut();
        let mut col = self.scratch_col.borrow_mut();
        for c in 0..self.dst_indices_per_color.len() {
            let input = &self.input_indices_per_color[c];
            let dst_indices = &self.dst_indices_per_color[c];
            let src_indices = &self.src_indices_per_color[c];
            v.assign_at_indices(input, F::T::one());
            op.jac_mul_inplace(x, t, &v, &mut col);
            y.set_data_with_indices(dst_indices, src_indices, &col);
            v.assign_at_indices(input, F::T::zero());
        }
    }

    /// 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)
    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,
    ) {
        let mut v = self.scratch_v.borrow_mut();
        let mut col = self.scratch_col.borrow_mut();
        for c in 0..self.dst_indices_per_color.len() {
            let input = &self.input_indices_per_color[c];
            let dst_indices = &self.dst_indices_per_color[c];
            let src_indices = &self.src_indices_per_color[c];
            v.assign_at_indices(input, F::T::one());
            op.sens_mul_inplace(x, t, &v, &mut col);
            y.set_data_with_indices(dst_indices, src_indices, &col);
            v.assign_at_indices(input, F::T::zero());
        }
    }

    /// 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)
    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,
    ) {
        let mut v = self.scratch_v.borrow_mut();
        let mut col = self.scratch_col.borrow_mut();
        for c in 0..self.dst_indices_per_color.len() {
            let input = &self.input_indices_per_color[c];
            let dst_indices = &self.dst_indices_per_color[c];
            let src_indices = &self.src_indices_per_color[c];
            v.assign_at_indices(input, F::T::one());
            op.jac_transpose_mul_inplace(x, t, &v, &mut col);
            y.set_data_with_indices(dst_indices, src_indices, &col);
            v.assign_at_indices(input, F::T::zero());
        }
    }

    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,
    ) {
        let mut v = self.scratch_v.borrow_mut();
        let mut col = self.scratch_col.borrow_mut();
        for c in 0..self.dst_indices_per_color.len() {
            let input = &self.input_indices_per_color[c];
            let dst_indices = &self.dst_indices_per_color[c];
            let src_indices = &self.src_indices_per_color[c];
            v.assign_at_indices(input, F::T::one());
            op.sens_transpose_mul_inplace(x, t, &v, &mut col);
            y.set_data_with_indices(dst_indices, src_indices, &col);
            v.assign_at_indices(input, F::T::zero());
        }
    }

    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,
    ) {
        let mut v = self.scratch_v.borrow_mut();
        let mut col = self.scratch_col.borrow_mut();
        for c in 0..self.dst_indices_per_color.len() {
            let input = &self.input_indices_per_color[c];
            let dst_indices = &self.dst_indices_per_color[c];
            let src_indices = &self.src_indices_per_color[c];
            v.assign_at_indices(input, F::T::one());
            op.call_inplace(&v, t, &mut col);
            y.set_data_with_indices(dst_indices, src_indices, &col);
            v.assign_at_indices(input, F::T::zero());
        }
    }
}

#[cfg(test)]
mod tests {

    use crate::jacobian::{find_jacobian_non_zeros, JacobianColoring};
    use crate::matrix::dense_nalgebra_serial::NalgebraMat;
    use crate::matrix::Matrix;
    use crate::op::linear_closure::LinearClosure;
    use crate::op::ParameterisedOp;
    use crate::vector::Vector;
    use crate::{
        jacobian::{coloring::nonzeros2graph, greedy_coloring::color_graph_greedy},
        op::closure::Closure,
        LinearOp, Op,
    };
    use crate::{scale, FaerSparseMat, NonLinearOpJacobian};
    use num_traits::{FromPrimitive, One, Zero};
    use std::ops::MulAssign;

    #[allow(clippy::type_complexity)]
    fn helper_triplets2op_nonlinear<'a, M: Matrix + 'a>(
        triplets: &'a [(usize, usize, M::T)],
        p: &'a M::V,
        nrows: usize,
        ncols: usize,
    ) -> Closure<
        M,
        impl Fn(&M::V, &M::V, M::T, &mut M::V) + use<'a, M>,
        impl Fn(&M::V, &M::V, M::T, &M::V, &mut M::V) + use<'a, M>,
    > {
        let nstates = ncols;
        let nout = nrows;
        let f = move |x: &M::V, y: &mut M::V| {
            for (i, j, v) in triplets {
                y.set_index(*i, y.get_index(*i) + x.get_index(*j) * *v);
            }
        };
        let mut ret = Closure::new(
            move |x: &M::V, _p: &M::V, _t, y: &mut M::V| {
                y.fill(M::T::zero());
                f(x, y);
            },
            move |_x: &M::V, _p: &M::V, _t, v, y: &mut M::V| {
                y.fill(M::T::zero());
                f(v, y);
            },
            nstates,
            nout,
            p.len(),
            p.context().clone(),
        );
        let y0 = M::V::zeros(nstates, p.context().clone());
        let t0 = M::T::zero();
        ret.calculate_sparsity(&y0, t0, p);
        ret
    }

    #[allow(clippy::type_complexity)]
    fn helper_triplets2op_linear<'a, M: Matrix + 'a>(
        triplets: &'a [(usize, usize, M::T)],
        p: &'a M::V,
        nrows: usize,
        ncols: usize,
    ) -> LinearClosure<M, impl Fn(&M::V, &M::V, M::T, M::T, &mut M::V) + use<'a, M>> {
        let nstates = ncols;
        let nout = nrows;
        let f = move |x: &M::V, y: &mut M::V| {
            for (i, j, v) in triplets {
                y.set_index(*i, y.get_index(*i) + x.get_index(*j) * *v);
            }
        };
        let mut ret = LinearClosure::new(
            move |x: &M::V, _p: &M::V, _t, beta, y: &mut M::V| {
                y.mul_assign(scale(beta));
                f(x, y);
            },
            nstates,
            nout,
            p.len(),
            p.context().clone(),
        );
        let t0 = M::T::zero();
        ret.calculate_sparsity(t0, p);
        ret
    }

    fn find_non_zeros<M: Matrix>() {
        let test_triplets = vec![
            vec![(0, 0, M::T::one()), (1, 1, M::T::one())],
            vec![
                (0, 0, M::T::one()),
                (0, 1, M::T::one()),
                (1, 1, M::T::one()),
            ],
            vec![(1, 1, M::T::one())],
            vec![
                (0, 0, M::T::one()),
                (1, 0, M::T::one()),
                (0, 1, M::T::one()),
                (1, 1, M::T::one()),
            ],
        ];
        let p = M::V::zeros(0, Default::default());
        for triplets in test_triplets {
            let op = helper_triplets2op_nonlinear::<M>(triplets.as_slice(), &p, 2, 2);
            let op = ParameterisedOp::new(&op, &p);
            let non_zeros =
                find_jacobian_non_zeros(&op, &M::V::zeros(2, p.context().clone()), M::T::zero());
            let expect = triplets
                .iter()
                .map(|(i, j, _v)| (*i, *j))
                .collect::<Vec<_>>();
            assert_eq!(non_zeros, expect);
        }
    }

    #[test]
    fn find_non_zeros_dmatrix() {
        find_non_zeros::<NalgebraMat<f64>>();
    }

    #[test]
    fn find_non_zeros_faer_sparse() {
        find_non_zeros::<FaerSparseMat<f64>>();
    }

    fn build_coloring<M: Matrix>() {
        let test_triplets = [
            vec![(0, 0, M::T::one()), (1, 1, M::T::one())],
            vec![
                (0, 0, M::T::one()),
                (0, 1, M::T::one()),
                (1, 1, M::T::one()),
            ],
            vec![(1, 1, M::T::one())],
            vec![
                (0, 0, M::T::one()),
                (1, 0, M::T::one()),
                (0, 1, M::T::one()),
                (1, 1, M::T::one()),
            ],
        ];
        let expect = vec![vec![1, 1], vec![1, 2], vec![1, 1], vec![1, 2]];
        let p = M::V::zeros(0, Default::default());
        for (triplets, expect) in test_triplets.iter().zip(expect) {
            let op = helper_triplets2op_nonlinear::<M>(triplets.as_slice(), &p, 2, 2);
            let op = ParameterisedOp::new(&op, &p);
            let non_zeros =
                find_jacobian_non_zeros(&op, &M::V::zeros(2, p.context().clone()), M::T::zero());
            let ncols = op.nstates();
            let graph = nonzeros2graph(non_zeros.as_slice(), ncols);
            let coloring = color_graph_greedy(&graph);

            assert_eq!(coloring, expect);
        }
    }

    #[test]
    fn build_coloring_dmatrix() {
        build_coloring::<NalgebraMat<f64>>();
    }

    #[test]
    fn build_coloring_faer_sparse() {
        build_coloring::<FaerSparseMat<f64>>();
    }

    fn matrix_coloring<M: Matrix>() {
        let test_triplets = vec![
            vec![
                (0, 0, M::T::one()),
                (1, 1, M::T::one()),
                (2, 2, M::T::one()),
            ],
            vec![(0, 0, M::T::one()), (1, 1, M::T::one())],
            vec![
                (0, 0, M::T::from_f64(0.9).unwrap()),
                (1, 0, M::T::from_f64(2.0).unwrap()),
                (1, 1, M::T::from_f64(1.1).unwrap()),
                (2, 2, M::T::from_f64(1.4).unwrap()),
            ],
        ];
        let n = 3;

        // test nonlinear functions
        let p = M::V::zeros(0, Default::default());
        for triplets in test_triplets.iter() {
            let op = helper_triplets2op_nonlinear::<M>(triplets.as_slice(), &p, n, n);
            let op = ParameterisedOp::new(&op, &p);
            let y0 = M::V::zeros(n, p.context().clone());
            let t0 = M::T::zero();
            let nonzeros = triplets
                .iter()
                .map(|(i, j, _v)| (*i, *j))
                .collect::<Vec<_>>();
            let coloring = JacobianColoring::new(
                &op.jacobian_sparsity().unwrap(),
                &nonzeros,
                p.context().clone(),
            );
            let mut jac = M::new_from_sparsity(3, 3, op.jacobian_sparsity(), p.context().clone());
            coloring.jacobian_inplace(&op, &y0, t0, &mut jac);
            let mut gemv1 = M::V::zeros(n, p.context().clone());
            let v = M::V::from_element(3, M::T::one(), p.context().clone());
            op.jac_mul_inplace(&y0, t0, &v, &mut gemv1);
            let mut gemv2 = M::V::zeros(n, p.context().clone());
            jac.gemv(M::T::one(), &v, M::T::zero(), &mut gemv2);
            gemv1.assert_eq_st(&gemv2, M::T::from_f64(1e-10).unwrap());
        }

        // test linear functions
        let p = M::V::zeros(0, p.context().clone());
        for triplets in test_triplets {
            let op = helper_triplets2op_linear::<M>(triplets.as_slice(), &p, n, n);
            let op = ParameterisedOp::new(&op, &p);
            let t0 = M::T::zero();
            let nonzeros = triplets
                .iter()
                .map(|(i, j, _v)| (*i, *j))
                .collect::<Vec<_>>();
            let coloring =
                JacobianColoring::new(&op.sparsity().unwrap(), &nonzeros, p.context().clone());
            let mut jac = M::new_from_sparsity(3, 3, op.sparsity(), p.context().clone());
            coloring.matrix_inplace(&op, t0, &mut jac);
            let mut gemv1 = M::V::zeros(n, p.context().clone());
            let v = M::V::from_element(3, M::T::one(), p.context().clone());
            op.gemv_inplace(&v, t0, M::T::zero(), &mut gemv1);
            let mut gemv2 = M::V::zeros(n, p.context().clone());
            jac.gemv(M::T::one(), &v, M::T::zero(), &mut gemv2);
            gemv1.assert_eq_st(&gemv2, M::T::from_f64(1e-10).unwrap());
        }
    }

    #[test]
    fn matrix_coloring_dmatrix() {
        matrix_coloring::<NalgebraMat<f64>>();
    }

    #[test]
    fn matrix_coloring_faer_sparse() {
        matrix_coloring::<FaerSparseMat<f64>>();
    }
}