deimos_numerics 0.16.2

Numerical methods and control systems analysis
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//! Compensated operator adapters for `faer`'s matrix-free decomposition
//! routines.
//!
//! The wrappers in this module do not change `faer`'s Arnoldi / Lanczos
//! orthogonalization. They only change the accuracy of operator application for
//! matrix types that this crate knows how to evaluate with compensated sparse
//! accumulation.
//!
//! This is intentionally a narrow boundary. The goal is not to replace `faer`'s
//! decomposition algorithms, but to reduce the local summation error in sparse
//! matrix-vector products before those values enter the Krylov iteration.
//!
//! # Glossary
//!
//! - **LinOp / BiLinOp:** `faer` traits for matrix-free linear and bilinear
//!   operators.
//! - **Compensated accumulation:** Summation that keeps correction terms to
//!   reduce floating-point loss.
//! - **Krylov iteration:** Iterative eigensolver or SVD process built from
//!   repeated operator applications.

use crate::sparse::compensated::{CompensatedField, CompensatedSum};
use faer::dyn_stack::{DynArray, MemStack, StackReq};
use faer::matrix_free::{BiLinOp, LinOp};
use faer::prelude::ReborrowMut;
use faer::sparse::{SparseColMatRef, SparseRowMatRef};
use faer::{Index, MatMut, MatRef, Par, Unbind};
use faer_traits::Conjugate;
use faer_traits::ext::ComplexFieldExt;
use num_traits::Float;

/// Operator applications that can be evaluated with compensated accumulation.
///
/// The `LinOp` traits from `faer` define the algebraic interface expected by
/// its matrix-free eigendecomposition and SVD routines. This companion trait
/// lets an implementation override just the accumulation strategy for the
/// operator application while keeping the same external operator shape.
pub trait CompensatedApply<T: CompensatedField>: LinOp<T>
where
    T::Real: Float,
{
    /// Computes the extra scratch required by compensated application.
    fn compensated_apply_scratch(&self, rhs_ncols: usize, par: Par) -> StackReq;

    /// Computes `out = self * rhs` with compensated accumulation.
    ///
    /// Intuitively, this replaces one naive running sum per output entry with a
    /// compensated reduction so cancellation in long sparse rows or columns
    /// loses less information.
    fn apply_compensated(
        &self,
        out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        par: Par,
        stack: &mut MemStack,
    );

    /// Computes `out = conj(self) * rhs` with compensated accumulation.
    fn conj_apply_compensated(
        &self,
        out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        par: Par,
        stack: &mut MemStack,
    );
}

/// Bi-directional operator applications that can be evaluated with compensated
/// accumulation.
///
/// This extends [`CompensatedApply`] to operators that also need transpose or
/// adjoint application, as required by matrix-free SVD.
pub trait CompensatedBiApply<T: CompensatedField>: CompensatedApply<T> + BiLinOp<T>
where
    T::Real: Float,
{
    /// Computes the extra scratch required by compensated transpose / adjoint application.
    fn compensated_transpose_apply_scratch(&self, rhs_ncols: usize, par: Par) -> StackReq;

    /// Computes `out = self^T * rhs` with compensated accumulation.
    fn transpose_apply_compensated(
        &self,
        out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        par: Par,
        stack: &mut MemStack,
    );

    /// Computes `out = self^H * rhs` with compensated accumulation.
    fn adjoint_apply_compensated(
        &self,
        out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        par: Par,
        stack: &mut MemStack,
    );
}

/// Wraps a linear operator and routes `LinOp` application through compensated
/// kernels supplied by [`CompensatedApply`].
///
/// The wrapper does not add new algebraic behavior. It only changes how the
/// wrapped operator is evaluated when `faer` calls `apply` or `conj_apply`.
#[derive(Clone, Copy, Debug)]
pub struct CompensatedLinOp<A> {
    inner: A,
}

impl<A> CompensatedLinOp<A> {
    /// Creates a compensated wrapper around `inner`.
    #[must_use]
    pub fn new(inner: A) -> Self {
        Self { inner }
    }

    /// Returns the wrapped operator.
    #[must_use]
    pub fn inner(&self) -> &A {
        &self.inner
    }
}

impl<T, A> LinOp<T> for CompensatedLinOp<A>
where
    T: CompensatedField,
    T::Real: Float,
    A: CompensatedApply<T> + Sync + core::fmt::Debug,
{
    fn apply_scratch(&self, rhs_ncols: usize, par: Par) -> StackReq {
        self.inner.compensated_apply_scratch(rhs_ncols, par)
    }

    fn nrows(&self) -> usize {
        self.inner.nrows()
    }

    fn ncols(&self) -> usize {
        self.inner.ncols()
    }

    fn apply(&self, out: MatMut<'_, T>, rhs: MatRef<'_, T>, par: Par, stack: &mut MemStack) {
        self.inner.apply_compensated(out, rhs, par, stack);
    }

    fn conj_apply(&self, out: MatMut<'_, T>, rhs: MatRef<'_, T>, par: Par, stack: &mut MemStack) {
        self.inner.conj_apply_compensated(out, rhs, par, stack);
    }
}

/// Wraps a bi-directional linear operator and routes `BiLinOp` application
/// through compensated kernels supplied by [`CompensatedBiApply`].
///
/// This is the natural wrapper to use before calling the matrix-free partial
/// SVD routines, since they need both the forward and adjoint operator action.
#[derive(Clone, Copy, Debug)]
pub struct CompensatedBiLinOp<A> {
    inner: A,
}

impl<A> CompensatedBiLinOp<A> {
    /// Creates a compensated wrapper around `inner`.
    #[must_use]
    pub fn new(inner: A) -> Self {
        Self { inner }
    }

    /// Returns the wrapped operator.
    #[must_use]
    pub fn inner(&self) -> &A {
        &self.inner
    }
}

impl<T, A> LinOp<T> for CompensatedBiLinOp<A>
where
    T: CompensatedField,
    T::Real: Float,
    A: CompensatedBiApply<T> + Sync + core::fmt::Debug,
{
    fn apply_scratch(&self, rhs_ncols: usize, par: Par) -> StackReq {
        self.inner.compensated_apply_scratch(rhs_ncols, par)
    }

    fn nrows(&self) -> usize {
        self.inner.nrows()
    }

    fn ncols(&self) -> usize {
        self.inner.ncols()
    }

    fn apply(&self, out: MatMut<'_, T>, rhs: MatRef<'_, T>, par: Par, stack: &mut MemStack) {
        self.inner.apply_compensated(out, rhs, par, stack);
    }

    fn conj_apply(&self, out: MatMut<'_, T>, rhs: MatRef<'_, T>, par: Par, stack: &mut MemStack) {
        self.inner.conj_apply_compensated(out, rhs, par, stack);
    }
}

impl<T, A> BiLinOp<T> for CompensatedBiLinOp<A>
where
    T: CompensatedField,
    T::Real: Float,
    A: CompensatedBiApply<T> + Sync + core::fmt::Debug,
{
    fn transpose_apply_scratch(&self, rhs_ncols: usize, par: Par) -> StackReq {
        self.inner
            .compensated_transpose_apply_scratch(rhs_ncols, par)
    }

    fn transpose_apply(
        &self,
        out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        par: Par,
        stack: &mut MemStack,
    ) {
        self.inner.transpose_apply_compensated(out, rhs, par, stack);
    }

    fn adjoint_apply(
        &self,
        out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        par: Par,
        stack: &mut MemStack,
    ) {
        self.inner.adjoint_apply_compensated(out, rhs, par, stack);
    }
}

#[inline]
fn max_scratch_req(lhs: StackReq, rhs: StackReq) -> StackReq {
    StackReq::any_of(&[lhs, rhs])
}

#[inline]
fn col_ref_slice<T>(col: faer::ColRef<'_, T>) -> &[T] {
    col.try_as_col_major().unwrap().as_slice()
}

#[inline]
fn col_mut_slice<T>(col: faer::ColMut<'_, T>) -> &mut [T] {
    col.try_as_col_major_mut().unwrap().as_slice_mut()
}

#[inline]
fn scratch_acc_len<T: CompensatedField>(req_rows: usize) -> StackReq
where
    T::Real: Float,
{
    StackReq::new::<CompensatedSum<T>>(req_rows)
}

#[inline]
fn init_accum_slice<T: CompensatedField>(
    len: usize,
    stack: &mut MemStack,
) -> (DynArray<'_, CompensatedSum<T>>, &mut MemStack)
where
    T::Real: Float,
{
    stack.collect(core::iter::repeat_n(CompensatedSum::<T>::default(), len))
}

impl<T, I, ViewT> CompensatedApply<T> for SparseRowMatRef<'_, I, ViewT>
where
    T: CompensatedField,
    T::Real: Float,
    I: Index,
    ViewT: Conjugate<Canonical = T>,
{
    fn compensated_apply_scratch(&self, rhs_ncols: usize, par: Par) -> StackReq {
        let _ = rhs_ncols;
        max_scratch_req(self.apply_scratch(rhs_ncols, par), StackReq::EMPTY)
    }

    fn apply_compensated(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        _par: Par,
        _stack: &mut MemStack,
    ) {
        let matrix = self.canonical();
        let nrows = matrix.nrows().unbound();
        let ncols = matrix.ncols().unbound();
        assert_eq!(rhs.nrows(), ncols);
        assert_eq!(out.nrows(), nrows);
        assert_eq!(out.ncols(), rhs.ncols());

        let row_ptr = matrix.row_ptr();
        let col_idx = matrix.col_idx();
        let values = matrix.val();

        for j in 0..rhs.ncols() {
            let rhs = col_ref_slice(rhs.col(j));
            let out = col_mut_slice(out.rb_mut().col_mut(j));
            for row in 0..nrows {
                let start = row_ptr[row].zx();
                let end = row_ptr[row + 1].zx();
                let mut acc = CompensatedSum::<T>::default();
                for idx in start..end {
                    acc.add(values[idx] * rhs[col_idx[idx].zx()]);
                }
                out[row] = acc.finish();
            }
        }
    }

    fn conj_apply_compensated(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        _par: Par,
        _stack: &mut MemStack,
    ) {
        let matrix = self.canonical();
        let nrows = matrix.nrows().unbound();
        let ncols = matrix.ncols().unbound();
        assert_eq!(rhs.nrows(), ncols);
        assert_eq!(out.nrows(), nrows);
        assert_eq!(out.ncols(), rhs.ncols());

        let row_ptr = matrix.row_ptr();
        let col_idx = matrix.col_idx();
        let values = matrix.val();

        for j in 0..rhs.ncols() {
            let rhs = col_ref_slice(rhs.col(j));
            let out = col_mut_slice(out.rb_mut().col_mut(j));
            for row in 0..nrows {
                let start = row_ptr[row].zx();
                let end = row_ptr[row + 1].zx();
                let mut acc = CompensatedSum::<T>::default();
                for idx in start..end {
                    acc.add(values[idx].conj() * rhs[col_idx[idx].zx()]);
                }
                out[row] = acc.finish();
            }
        }
    }
}

impl<T, I, ViewT> CompensatedBiApply<T> for SparseRowMatRef<'_, I, ViewT>
where
    T: CompensatedField,
    T::Real: Float,
    I: Index,
    ViewT: Conjugate<Canonical = T>,
{
    fn compensated_transpose_apply_scratch(&self, rhs_ncols: usize, par: Par) -> StackReq {
        max_scratch_req(
            self.transpose_apply_scratch(rhs_ncols, par),
            scratch_acc_len::<T>(self.ncols()),
        )
    }

    fn transpose_apply_compensated(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        _par: Par,
        stack: &mut MemStack,
    ) {
        let matrix = self.canonical();
        let nrows = matrix.nrows().unbound();
        let ncols = matrix.ncols().unbound();
        assert_eq!(rhs.nrows(), nrows);
        assert_eq!(out.nrows(), ncols);
        assert_eq!(out.ncols(), rhs.ncols());

        let row_ptr = matrix.row_ptr();
        let col_idx = matrix.col_idx();
        let values = matrix.val();

        for j in 0..rhs.ncols() {
            let rhs = col_ref_slice(rhs.col(j));
            let out_col = out.rb_mut().col_mut(j);
            let out = col_mut_slice(out_col);
            // The transpose of a CSR matrix scatters each row into output
            // columns. Accumulate into temporary compensated sums first so the
            // scatter order does not become the final rounding order.
            let (mut acc, _) = init_accum_slice::<T>(ncols, stack);
            for value in acc.iter_mut() {
                *value = CompensatedSum::default();
            }
            for row in 0..nrows {
                let rhs_value = rhs[row];
                let start = row_ptr[row].zx();
                let end = row_ptr[row + 1].zx();
                for idx in start..end {
                    acc[col_idx[idx].zx()].add(values[idx] * rhs_value);
                }
            }
            for (dst, sum) in out.iter_mut().zip(acc.iter().copied()) {
                *dst = sum.finish();
            }
        }
    }

    fn adjoint_apply_compensated(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        _par: Par,
        stack: &mut MemStack,
    ) {
        let matrix = self.canonical();
        let nrows = matrix.nrows().unbound();
        let ncols = matrix.ncols().unbound();
        assert_eq!(rhs.nrows(), nrows);
        assert_eq!(out.nrows(), ncols);
        assert_eq!(out.ncols(), rhs.ncols());

        let row_ptr = matrix.row_ptr();
        let col_idx = matrix.col_idx();
        let values = matrix.val();

        for j in 0..rhs.ncols() {
            let rhs = col_ref_slice(rhs.col(j));
            let out_col = out.rb_mut().col_mut(j);
            let out = col_mut_slice(out_col);
            // The adjoint path has the same scatter pattern as the transpose
            // path, but with conjugated matrix entries.
            let (mut acc, _) = init_accum_slice::<T>(ncols, stack);
            for value in acc.iter_mut() {
                *value = CompensatedSum::default();
            }
            for row in 0..nrows {
                let rhs_value = rhs[row];
                let start = row_ptr[row].zx();
                let end = row_ptr[row + 1].zx();
                for idx in start..end {
                    acc[col_idx[idx].zx()].add(values[idx].conj() * rhs_value);
                }
            }
            for (dst, sum) in out.iter_mut().zip(acc.iter().copied()) {
                *dst = sum.finish();
            }
        }
    }
}

impl<T, I, ViewT> CompensatedApply<T> for SparseColMatRef<'_, I, ViewT>
where
    T: CompensatedField,
    T::Real: Float,
    I: Index,
    ViewT: Conjugate<Canonical = T>,
{
    fn compensated_apply_scratch(&self, rhs_ncols: usize, par: Par) -> StackReq {
        max_scratch_req(
            self.apply_scratch(rhs_ncols, par),
            scratch_acc_len::<T>(self.nrows()),
        )
    }

    fn apply_compensated(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        _par: Par,
        stack: &mut MemStack,
    ) {
        let matrix = self.canonical();
        let nrows = matrix.nrows().unbound();
        let ncols = matrix.ncols().unbound();
        assert_eq!(rhs.nrows(), ncols);
        assert_eq!(out.nrows(), nrows);
        assert_eq!(out.ncols(), rhs.ncols());

        let col_ptr = matrix.col_ptr();
        let row_idx = matrix.row_idx();
        let values = matrix.val();

        for j in 0..rhs.ncols() {
            let rhs = col_ref_slice(rhs.col(j));
            let out_col = out.rb_mut().col_mut(j);
            let out = col_mut_slice(out_col);
            // CSC application also scatters contributions into output rows.
            // Use one compensated accumulator per row and finalize once per
            // output entry.
            let (mut acc, _) = init_accum_slice::<T>(nrows, stack);
            for value in acc.iter_mut() {
                *value = CompensatedSum::default();
            }
            for col in 0..ncols {
                let rhs_value = rhs[col];
                let start = col_ptr[col].zx();
                let end = col_ptr[col + 1].zx();
                for idx in start..end {
                    acc[row_idx[idx].zx()].add(values[idx] * rhs_value);
                }
            }
            for (dst, sum) in out.iter_mut().zip(acc.iter().copied()) {
                *dst = sum.finish();
            }
        }
    }

    fn conj_apply_compensated(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        _par: Par,
        stack: &mut MemStack,
    ) {
        let matrix = self.canonical();
        let nrows = matrix.nrows().unbound();
        let ncols = matrix.ncols().unbound();
        assert_eq!(rhs.nrows(), ncols);
        assert_eq!(out.nrows(), nrows);
        assert_eq!(out.ncols(), rhs.ncols());

        let col_ptr = matrix.col_ptr();
        let row_idx = matrix.row_idx();
        let values = matrix.val();

        for j in 0..rhs.ncols() {
            let rhs = col_ref_slice(rhs.col(j));
            let out_col = out.rb_mut().col_mut(j);
            let out = col_mut_slice(out_col);
            let (mut acc, _) = init_accum_slice::<T>(nrows, stack);
            for value in acc.iter_mut() {
                *value = CompensatedSum::default();
            }
            for col in 0..ncols {
                let rhs_value = rhs[col];
                let start = col_ptr[col].zx();
                let end = col_ptr[col + 1].zx();
                for idx in start..end {
                    acc[row_idx[idx].zx()].add(values[idx].conj() * rhs_value);
                }
            }
            for (dst, sum) in out.iter_mut().zip(acc.iter().copied()) {
                *dst = sum.finish();
            }
        }
    }
}

impl<T, I, ViewT> CompensatedBiApply<T> for SparseColMatRef<'_, I, ViewT>
where
    T: CompensatedField,
    T::Real: Float,
    I: Index,
    ViewT: Conjugate<Canonical = T>,
{
    fn compensated_transpose_apply_scratch(&self, rhs_ncols: usize, par: Par) -> StackReq {
        max_scratch_req(
            self.transpose_apply_scratch(rhs_ncols, par),
            StackReq::EMPTY,
        )
    }

    fn transpose_apply_compensated(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        _par: Par,
        _stack: &mut MemStack,
    ) {
        let matrix = self.canonical();
        let nrows = matrix.nrows().unbound();
        let ncols = matrix.ncols().unbound();
        assert_eq!(rhs.nrows(), nrows);
        assert_eq!(out.nrows(), ncols);
        assert_eq!(out.ncols(), rhs.ncols());

        let col_ptr = matrix.col_ptr();
        let row_idx = matrix.row_idx();
        let values = matrix.val();

        for j in 0..rhs.ncols() {
            let rhs = col_ref_slice(rhs.col(j));
            let out = col_mut_slice(out.rb_mut().col_mut(j));
            for col in 0..ncols {
                let start = col_ptr[col].zx();
                let end = col_ptr[col + 1].zx();
                // The transpose of CSC is row-like, so we can reduce each
                // output entry directly with a single compensated accumulator.
                let mut acc = CompensatedSum::<T>::default();
                for idx in start..end {
                    acc.add(values[idx] * rhs[row_idx[idx].zx()]);
                }
                out[col] = acc.finish();
            }
        }
    }

    fn adjoint_apply_compensated(
        &self,
        mut out: MatMut<'_, T>,
        rhs: MatRef<'_, T>,
        _par: Par,
        _stack: &mut MemStack,
    ) {
        let matrix = self.canonical();
        let nrows = matrix.nrows().unbound();
        let ncols = matrix.ncols().unbound();
        assert_eq!(rhs.nrows(), nrows);
        assert_eq!(out.nrows(), ncols);
        assert_eq!(out.ncols(), rhs.ncols());

        let col_ptr = matrix.col_ptr();
        let row_idx = matrix.row_idx();
        let values = matrix.val();

        for j in 0..rhs.ncols() {
            let rhs = col_ref_slice(rhs.col(j));
            let out = col_mut_slice(out.rb_mut().col_mut(j));
            for col in 0..ncols {
                let start = col_ptr[col].zx();
                let end = col_ptr[col + 1].zx();
                let mut acc = CompensatedSum::<T>::default();
                for idx in start..end {
                    acc.add(values[idx].conj() * rhs[row_idx[idx].zx()]);
                }
                out[col] = acc.finish();
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{CompensatedBiLinOp, CompensatedLinOp};
    use faer::dyn_stack::{MemBuffer, MemStack, StackReq};
    use faer::matrix_free::{BiLinOp, LinOp};
    use faer::sparse::{SparseColMat, SparseRowMat, Triplet};
    use faer::{Mat, Par, Unbind};

    #[test]
    fn compensated_csc_wrapper_matches_plain_apply() {
        let matrix = SparseColMat::<usize, f64>::try_new_from_triplets(
            3,
            3,
            &[
                Triplet::new(0, 0, 2.0),
                Triplet::new(1, 1, -3.0),
                Triplet::new(2, 2, 4.0),
                Triplet::new(0, 2, 1.5),
            ],
        )
        .unwrap();
        let rhs = Mat::from_fn(3, 1, |i, _| [1.0, -2.0, 3.0][i.unbound()]);
        let mut plain = Mat::zeros(3, 1);
        let mut compensated = Mat::zeros(3, 1);
        let op = CompensatedBiLinOp::new(matrix.as_ref());
        let mut scratch = MemBuffer::new(op.apply_scratch(1, Par::Seq));
        let mut plain_mem = MemBuffer::new(StackReq::EMPTY);
        let plain_stack = MemStack::new(&mut plain_mem);
        let compensated_stack = MemStack::new(&mut scratch);
        matrix
            .as_ref()
            .apply(plain.as_mut(), rhs.as_ref(), Par::Seq, plain_stack);
        op.apply(
            compensated.as_mut(),
            rhs.as_ref(),
            Par::Seq,
            compensated_stack,
        );

        assert_eq!(plain, compensated);
    }

    #[test]
    fn compensated_csr_wrapper_matches_plain_adjoint_apply() {
        let matrix = SparseRowMat::<usize, f64>::try_new_from_triplets(
            3,
            3,
            &[
                Triplet::new(0, 0, 2.0),
                Triplet::new(1, 1, -3.0),
                Triplet::new(2, 2, 4.0),
                Triplet::new(0, 2, 1.5),
            ],
        )
        .unwrap();
        let rhs = Mat::from_fn(3, 1, |i, _| [1.0, -2.0, 3.0][i.unbound()]);
        let mut plain = Mat::zeros(3, 1);
        let mut compensated = Mat::zeros(3, 1);
        let op = CompensatedBiLinOp::new(matrix.as_ref());
        let mut scratch = MemBuffer::new(op.transpose_apply_scratch(1, Par::Seq));
        let mut plain_mem = MemBuffer::new(StackReq::EMPTY);
        let plain_stack = MemStack::new(&mut plain_mem);
        let compensated_stack = MemStack::new(&mut scratch);
        matrix
            .as_ref()
            .adjoint_apply(plain.as_mut(), rhs.as_ref(), Par::Seq, plain_stack);
        op.adjoint_apply(
            compensated.as_mut(),
            rhs.as_ref(),
            Par::Seq,
            compensated_stack,
        );

        assert_eq!(plain, compensated);
    }

    #[test]
    fn compensated_lin_wrapper_exposes_dimensions() {
        let matrix = SparseRowMat::<usize, f64>::try_new_from_triplets(
            2,
            3,
            &[Triplet::new(0, 1, 1.0), Triplet::new(1, 2, 2.0)],
        )
        .unwrap();
        let op = CompensatedLinOp::new(matrix.as_ref());
        assert_eq!(op.nrows(), 2);
        assert_eq!(op.ncols(), 3);
    }
}