kryst 3.2.1

Krylov subspace and preconditioned iterative solvers for dense and sparse linear systems, with shared and distributed memory parallelism.
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//! Chebyshev polynomial preconditioner
//!
//! This module implements the Chebyshev polynomial preconditioner, which applies a Chebyshev polynomial filter
//! to accelerate the convergence of iterative solvers for symmetric positive definite matrices. The preconditioner
//! is based on applying a polynomial of the matrix to a vector, with the polynomial chosen to dampen unwanted
//! spectral components.
//!
//! # Overview
//!
//! The Chebyshev preconditioner uses a recurrence to apply the Chebyshev polynomial of degree `m` to a vector `r`:
//!     ```ignore
//!     z = p_m(A) r
//!     ```
//!
//! where `A` is the system matrix, and `p_m` is the Chebyshev polynomial scaled to the spectrum [`alpha`, `beta`].
//! The endpoints of the spectrum (smallest/largest eigenvalues) can be provided or estimated.
//!
//! # Usage
//!
//! - Create a `ChebyshevPre` struct with the desired degree and spectrum bounds.
//! - Use the `apply_chebyshev` free function to apply the polynomial filter to a vector.
//! - The `Preconditioner` trait implementation provides full functionality.
//!
//! # References
//!
//! - Saad, Y. (2003). Iterative Methods for Sparse Linear Systems. (Section 12.3)
//! - https://en.wikipedia.org/wiki/Chebyshev_polynomials

#[cfg(feature = "complex")]
use crate::algebra::bridge::BridgeScratch;
use crate::algebra::prelude::*;
use crate::core::traits::MatVec;
use crate::error::KError;
use crate::matrix::convert::csr_from_linop;
use crate::matrix::op::LinOp;
use crate::matrix::sparse::CsrMatrix;
#[cfg(feature = "complex")]
use crate::ops::kpc::KPreconditioner;
use crate::preconditioner::Preconditioner as ObjPreconditioner;
#[cfg(feature = "complex")]
use crate::preconditioner::bridge::{
    apply_pc_mut_s as bridge_apply_pc_mut_s, apply_pc_s as bridge_apply_pc_s,
};
use crate::preconditioner::legacy::Preconditioner;
use faer::Mat;
use std::sync::Arc;
use std::sync::Mutex;

/// Chebyshev polynomial preconditioner struct
///
/// Stores the matrix, polynomial degree, and spectrum bounds.
/// This is the Phase III enhanced version that works as a proper preconditioner.
/// NOTE: Kept for legacy/manual use; the object-safe `ChebyshevPc` below is what
/// `PcFactory` constructs at runtime.
pub struct ChebyshevPre {
    /// The system matrix (stored for apply operations)
    matrix: Mat<f64>,
    /// Degree of the Chebyshev polynomial
    degree: usize,
    /// Lower bound of the spectrum (smallest eigenvalue)
    lambda_min: f64,
    /// Upper bound of the spectrum (largest eigenvalue)
    lambda_max: f64,
}

impl ChebyshevPre {
    /// Create a new Chebyshev preconditioner
    ///
    /// # Arguments
    /// * `matrix` - System matrix (cloned for storage)
    /// * `degree` - Degree of the Chebyshev polynomial
    /// * `lambda_min` - Lower bound of the spectrum
    /// * `lambda_max` - Upper bound of the spectrum
    pub fn new(matrix: Mat<f64>, degree: usize, lambda_min: f64, lambda_max: f64) -> Self {
        Self {
            matrix,
            degree,
            lambda_min,
            lambda_max,
        }
    }

    /// Estimate eigenvalue bounds using power iteration if not provided
    ///
    /// # Arguments
    /// * `matrix` - System matrix
    /// * `max_iters` - Maximum iterations for eigenvalue estimation
    /// * `tol` - Tolerance for eigenvalue estimation
    ///
    /// Returns (lambda_min, lambda_max)
    pub fn estimate_eigenvalue_bounds(matrix: &Mat<f64>, max_iters: usize, tol: f64) -> (f64, f64) {
        let n = matrix.nrows();
        if n == 0 {
            return (1.0, 1.0);
        }

        // Estimate largest eigenvalue using power iteration
        let mut v = vec![1.0 / (n as f64).sqrt(); n];
        let mut av = vec![0.0; n];
        let mut lambda_max = 1.0;

        for _ in 0..max_iters {
            // Apply matrix
            // Disambiguate: call the LinOp variant of matvec
            crate::matrix::op::LinOp::matvec(matrix, &v, &mut av);

            // Rayleigh quotient
            let new_lambda_max: f64 = v.iter().zip(av.iter()).map(|(&vi, &avi)| vi * avi).sum();

            if (new_lambda_max - lambda_max).abs() < tol * lambda_max.abs() {
                lambda_max = new_lambda_max;
                break;
            }
            lambda_max = new_lambda_max;

            // Normalize
            let norm: f64 = av.iter().map(|&x| x * x).sum::<f64>().sqrt();
            if norm > 0.0 {
                for i in 0..n {
                    v[i] = av[i] / norm;
                }
            }
        }

        // Estimate smallest eigenvalue (very rough approximation)
        // For SPD matrices, use a fraction of the largest eigenvalue
        let lambda_min = lambda_max * 0.01; // Conservative estimate

        (lambda_min, lambda_max)
    }
}

impl Preconditioner<Mat<f64>, Vec<f64>> for ChebyshevPre {
    /// Setup the Chebyshev preconditioner (stores matrix for later use)
    fn setup(&mut self, a: &Mat<f64>) -> Result<(), KError> {
        self.matrix = a.clone();
        Ok(())
    }

    /// Apply Chebyshev polynomial preconditioner
    fn apply(
        &self,
        _side: crate::preconditioner::PcSide,
        r: &Vec<f64>,
        z: &mut Vec<f64>,
    ) -> Result<(), KError> {
        if r.len() != z.len() {
            return Err(KError::SolveError("Vector length mismatch".to_string()));
        }

        apply_chebyshev(
            &self.matrix,
            r,
            z,
            self.lambda_min,
            self.lambda_max,
            self.degree,
        );
        Ok(())
    }
}

/// Legacy Chebyshev struct for backward compatibility
///
/// Stores the polynomial degree and optional spectrum bounds.
pub struct Chebyshev {
    /// Degree of the Chebyshev polynomial
    pub degree: usize,
    /// Lower bound of the spectrum (smallest eigenvalue)
    pub lambda_min: Option<f64>,
    /// Upper bound of the spectrum (largest eigenvalue)
    pub lambda_max: Option<f64>,
}

#[derive(Clone, Copy, Debug)]
pub struct ChebBounds {
    pub lam_max: f64,
    pub lam_min: f64,
}

fn apply_sym_scaled(
    a: &CsrMatrix<f64>,
    d_sqrt_inv: &[f64],
    x: &[f64],
    y: &mut [f64],
) -> Result<(), KError> {
    let n = a.nrows();
    if d_sqrt_inv.len() != n || x.len() != n || y.len() != n {
        return Err(KError::InvalidInput(
            "apply_sym_scaled: dimension mismatch".into(),
        ));
    }
    for i in 0..n {
        y[i] = d_sqrt_inv[i] * x[i];
    }
    let mut tmp = vec![0.0; n];
    a.spmv_scaled(1.0, y, 0.0, &mut tmp[..])?;
    for i in 0..n {
        y[i] = d_sqrt_inv[i] * tmp[i];
    }
    Ok(())
}

pub fn estimate_lmax_sym(
    a: &CsrMatrix<f64>,
    d_sqrt_inv: &[f64],
    power_steps: usize,
) -> Result<f64, KError> {
    let n = a.nrows();
    if n == 0 {
        return Ok(0.0);
    }
    if d_sqrt_inv.len() != n {
        return Err(KError::InvalidInput(
            "estimate_lmax_sym: dimension mismatch".into(),
        ));
    }
    let mut x = vec![0.0; n];
    for i in 0..n {
        let pattern = (i.wrapping_mul(2_654_435_761)) & 1;
        x[i] = if pattern == 0 { 1.0 } else { -1.0 };
    }
    let mut nrm = x.iter().map(|v| v * v).sum::<f64>().sqrt();
    if nrm == 0.0 {
        x[0] = 1.0;
        nrm = 1.0;
    }
    for xi in &mut x {
        *xi /= nrm;
    }
    let mut y = vec![0.0; n];
    let steps = power_steps.max(1);
    for _ in 0..steps {
        apply_sym_scaled(a, d_sqrt_inv, &x, &mut y)?;
        let nrm = y.iter().map(|v| v * v).sum::<f64>().sqrt().max(1e-300);
        for i in 0..n {
            x[i] = y[i] / nrm;
        }
    }
    apply_sym_scaled(a, d_sqrt_inv, &x, &mut y)?;
    let lam = x.iter().zip(y.iter()).map(|(xi, yi)| xi * yi).sum::<f64>();
    Ok(lam.max(0.0))
}

pub fn chebyshev_smooth_csr(
    a: &CsrMatrix<f64>,
    d_inv: &[f64],
    rhs: &[f64],
    z: &mut [f64],
    deg: usize,
    bounds: &ChebBounds,
    work_r: &mut [f64],
    work_q: &mut [f64],
    work_aq: &mut [f64],
) -> Result<(), KError> {
    if deg == 0 {
        return Ok(());
    }
    let n = a.nrows();
    if d_inv.len() != n || rhs.len() != n || z.len() != n {
        return Err(KError::InvalidInput(
            "chebyshev_smooth_csr: dimension mismatch".into(),
        ));
    }
    if work_r.len() != n || work_q.len() != n || work_aq.len() != n {
        return Err(KError::InvalidInput(
            "chebyshev_smooth_csr: workspace mismatch".into(),
        ));
    }
    if !bounds.lam_max.is_finite() || !bounds.lam_min.is_finite() || bounds.lam_max <= 0.0 {
        return Err(KError::InvalidInput(
            "chebyshev_smooth_csr: invalid eigenvalue bounds".into(),
        ));
    }

    a.spmv_scaled(1.0, z, 0.0, work_aq)?;
    for i in 0..n {
        work_r[i] = rhs[i] - work_aq[i];
    }

    let theta = (0.5 * (bounds.lam_max + bounds.lam_min)).max(1e-12);
    let delta = 0.5 * (bounds.lam_max - bounds.lam_min);
    let mut alpha = 1.0 / theta;

    for i in 0..n {
        work_q[i] = d_inv[i] * work_r[i];
    }
    for i in 0..n {
        z[i] += alpha * work_q[i];
    }
    a.spmv_scaled(1.0, work_q, 0.0, work_aq)?;
    for i in 0..n {
        work_r[i] -= alpha * work_aq[i];
    }

    for _ in 1..deg {
        let beta = 0.25 * delta * delta * alpha;
        for i in 0..n {
            work_q[i] = d_inv[i] * work_r[i] + beta * work_q[i];
        }
        alpha = 1.0 / (theta - beta);
        for i in 0..n {
            z[i] += alpha * work_q[i];
        }
        a.spmv_scaled(1.0, work_q, 0.0, work_aq)?;
        for i in 0..n {
            work_r[i] -= alpha * work_aq[i];
        }
    }
    Ok(())
}

impl Chebyshev {
    /// Create a new Chebyshev preconditioner
    ///
    /// # Arguments
    /// * `degree` - Degree of the Chebyshev polynomial
    /// * `lambda_min` - Optional lower bound of the spectrum
    /// * `lambda_max` - Optional upper bound of the spectrum
    pub fn new(degree: usize, lambda_min: Option<f64>, lambda_max: Option<f64>) -> Self {
        Self {
            degree,
            lambda_min,
            lambda_max,
        }
    }
}

impl<M, V> Preconditioner<M, V> for Chebyshev
where
    M: MatVec<Vec<f64>>,
    V: AsRef<[f64]> + AsMut<[f64]> + Clone,
{
    /// Setup the Chebyshev preconditioner (no-op; spectrum estimation could be added here)
    fn setup(&mut self, _a: &M) -> Result<(), KError> {
        // Optionally estimate eigenvalues here if None
        Ok(())
    }
    /// Not implemented: use `apply_chebyshev` free function instead
    fn apply(
        &self,
        _side: crate::preconditioner::PcSide,
        _r: &V,
        _z: &mut V,
    ) -> Result<(), KError> {
        Err(KError::SolveError(
            "Chebyshev preconditioner requires matrix argument; use apply_chebyshev free function."
                .to_string(),
        ))
    }
}

/// Apply Chebyshev polynomial filter of degree m to r: z = p_m(A) r
///
/// # Arguments
/// * `a` - Matrix implementing `MatVec`
/// * `r` - Input vector
/// * `z` - Output vector (overwritten)
/// * `alpha` - Lower bound of the spectrum
/// * `beta` - Upper bound of the spectrum
/// * `m` - Degree of the Chebyshev polynomial
#[allow(clippy::ptr_arg)]
pub fn apply_chebyshev<M>(a: &M, r: &Vec<f64>, z: &mut [f64], alpha: f64, beta: f64, m: usize)
where
    M: MatVec<Vec<f64>>,
{
    if (beta - alpha).abs() < f64::EPSILON {
        // Degenerate interval: just copy r to z
        z.copy_from_slice(r);
        return;
    }
    let n = r.len();
    // v0, v1, v2 are the three-term recurrence vectors
    let mut v0 = r.to_vec();
    let mut v1 = vec![0.0; n];
    let mut v2 = vec![0.0; n];
    // c and d are the scaling and shifting parameters for the spectrum
    let c: f64 = (beta + alpha) / 2.0;
    let d: f64 = (beta - alpha) / 2.0;
    // tau is a normalization factor to ensure p_m(0) = 1
    let tau = 1.0 / chebyshev_t(m, (0.0 - c) / d);
    // First step: v1 = (A v0 - c v0) / d
    a.matvec(&v0, &mut v1);
    for i in 0..n {
        v1[i] = (v1[i] - c * v0[i]) / d;
    }
    if m == 0 {
        // Degree 0: just copy input
        z.copy_from_slice(&v0);
        return;
    } else if m == 1 {
        // Degree 1: copy v1
        z.copy_from_slice(&v1);
        return;
    }
    // Recurrence for higher degrees
    for _k in 2..=m {
        a.matvec(&v1, &mut v2);
        for i in 0..n {
            v2[i] = (2.0 * (v2[i] - c * v1[i]) / d) - v0[i];
        }
        std::mem::swap(&mut v0, &mut v1);
        std::mem::swap(&mut v1, &mut v2);
    }
    // Scale the result by tau
    #[cfg(feature = "rayon")]
    {
        use rayon::prelude::*;
        z.par_iter_mut().enumerate().for_each(|(i, zi)| {
            *zi = tau * v1[i];
        });
    }
    #[cfg(not(feature = "rayon"))]
    {
        for i in 0..n {
            z[i] = tau * v1[i];
        }
    }
}

/// Compute the Chebyshev polynomial of the first kind T_m(x) using recurrence
fn chebyshev_t(m: usize, x: R) -> R {
    if m == 0 {
        1.0
    } else if m == 1 {
        x
    } else {
        let mut t0 = 1.0;
        let mut t1 = x;
        let mut t2;
        for _ in 2..=m {
            t2 = 2.0 * x * t1 - t0;
            t0 = t1;
            t1 = t2;
        }
        t1
    }
}

// -----------------------------------------------------------------------------
// Object-safe Chebyshev preconditioner using CSR and LinOp.
// -----------------------------------------------------------------------------

#[derive(Default)]
struct ChebScratch {
    v0: Vec<R>,
    v1: Vec<R>,
    v2: Vec<R>,
}

/// Object-safe Chebyshev preconditioner
pub struct ChebyshevPc {
    degree: usize,
    lambda_min: f64,
    lambda_max: f64,
    a_csr: Option<Arc<CsrMatrix<f64>>>,
    n: usize,
    scratch: Mutex<ChebScratch>,
}

impl ChebyshevPc {
    pub fn new(degree: usize, lambda_min: f64, lambda_max: f64) -> Self {
        Self {
            degree,
            lambda_min,
            lambda_max,
            a_csr: None,
            n: 0,
            scratch: Mutex::new(ChebScratch::default()),
        }
    }
}

#[cfg(not(feature = "complex"))]
impl ObjPreconditioner for ChebyshevPc {
    fn dims(&self) -> (usize, usize) {
        (self.n, self.n)
    }

    fn setup(&mut self, op: &dyn LinOp<S = f64>) -> Result<(), crate::error::KError> {
        let csr = csr_from_linop(op, 0.0)?;
        let n = csr.nrows();
        self.a_csr = Some(csr);
        self.n = n;
        // ensure scratch
        let mut s = self.scratch.lock().unwrap();
        if s.v0.len() != n {
            s.v0.resize(n, R::default());
        }
        if s.v1.len() != n {
            s.v1.resize(n, R::default());
        }
        if s.v2.len() != n {
            s.v2.resize(n, R::default());
        }
        Ok(())
    }

    fn supports_numeric_update(&self) -> bool {
        true
    }

    fn update_numeric(&mut self, op: &dyn LinOp<S = f64>) -> Result<(), crate::error::KError> {
        // For now, refresh CSR view and keep degree/spectrum
        let csr = csr_from_linop(op, 0.0)?;
        self.a_csr = Some(csr);
        Ok(())
    }

    fn apply(
        &self,
        _side: crate::preconditioner::PcSide,
        r: &[f64],
        z: &mut [f64],
    ) -> Result<(), crate::error::KError> {
        use crate::error::KError;
        let a = self
            .a_csr
            .as_ref()
            .ok_or_else(|| KError::InvalidInput("ChebyshevPc not setup".into()))?;
        if r.len() != self.n || z.len() != self.n {
            return Err(KError::InvalidInput(
                "dimension mismatch in ChebyshevPc::apply".into(),
            ));
        }

        let n = self.n;
        let mut s = self.scratch.lock().unwrap();
        // Take ownership of scratch vectors to avoid overlapping borrows; restore before return.
        let mut v0 = std::mem::take(&mut s.v0);
        let mut v1 = std::mem::take(&mut s.v1);
        let mut v2 = std::mem::take(&mut s.v2);
        // PcSide is intentionally ignored: this smoother is symmetric and is applied
        // as a left preconditioner in practice.
        // tau scaling to make p_m(0) ~ 1
        let c = (self.lambda_max + self.lambda_min) / 2.0;
        let d = (self.lambda_max - self.lambda_min) / 2.0;

        let res = (|| {
            if d.abs() < f64::EPSILON {
                // Degenerate spectrum: copy input
                z.copy_from_slice(r);
                return Ok(());
            }
            let tau = 1.0 / chebyshev_t(self.degree, (0.0 - c) / d);

            if self.degree == 0 {
                z.copy_from_slice(r);
                return Ok(());
            }

            // v1 = (A r - c r) / d
            a.spmv_scaled(1.0, r, 0.0, &mut v1[..n])?;
            for i in 0..n {
                v1[i] = (v1[i] - c * r[i]) / d;
            }
            if self.degree == 1 {
                for i in 0..n {
                    z[i] = tau * v1[i];
                }
                return Ok(());
            }

            // Set v0 = r for the recurrence
            v0[..n].copy_from_slice(r);

            // Recurrence for k = 2..=m
            for _k in 2..=self.degree {
                // v2 = 2 * ((A v1 - c v1)/d) - v0
                a.spmv_scaled(1.0, &v1[..n], 0.0, &mut v2[..n])?;
                for i in 0..n {
                    v2[i] = 2.0 * ((v2[i] - c * v1[i]) / d) - v0[i];
                }
                // rotate: v0 <- v1, v1 <- v2, v2 becomes scratch (old v0)
                std::mem::swap(&mut v0, &mut v1);
                std::mem::swap(&mut v1, &mut v2);
            }

            #[cfg(feature = "rayon")]
            {
                use rayon::prelude::*;
                z.par_iter_mut()
                    .zip(v1[..n].par_iter())
                    .for_each(|(zi, &vi)| *zi = tau * vi);
            }
            #[cfg(not(feature = "rayon"))]
            {
                for i in 0..n {
                    z[i] = tau * v1[i];
                }
            }
            Ok(())
        })();

        // Restore scratch vectors before returning.
        s.v0 = v0;
        s.v1 = v1;
        s.v2 = v2;
        res
    }

    fn required_format(&self) -> crate::matrix::format::OpFormat {
        crate::matrix::format::OpFormat::Csr
    }
}

#[cfg(feature = "complex")]
impl ObjPreconditioner for ChebyshevPc {
    fn setup(&mut self, _op: &dyn LinOp<S = S>) -> Result<(), crate::error::KError> {
        Err(KError::Unsupported(
            "Chebyshev does not support complex scalars yet".into(),
        ))
    }

    fn apply(
        &self,
        _side: crate::preconditioner::PcSide,
        _x: &[S],
        _y: &mut [S],
    ) -> Result<(), crate::error::KError> {
        Err(KError::Unsupported(
            "Chebyshev does not support complex scalars yet".into(),
        ))
    }
}

#[cfg(feature = "complex")]
impl KPreconditioner for ChebyshevPc {
    type Scalar = S;

    #[inline]
    fn dims(&self) -> (usize, usize) {
        ObjPreconditioner::dims(self)
    }

    fn apply_s(
        &self,
        side: crate::preconditioner::PcSide,
        x: &[S],
        y: &mut [S],
        scratch: &mut BridgeScratch,
    ) -> Result<(), crate::error::KError> {
        bridge_apply_pc_s(self, side, x, y, scratch)
    }

    fn apply_mut_s(
        &mut self,
        side: crate::preconditioner::PcSide,
        x: &[S],
        y: &mut [S],
        scratch: &mut BridgeScratch,
    ) -> Result<(), crate::error::KError> {
        bridge_apply_pc_mut_s(self, side, x, y, scratch)
    }

    fn on_restart_s(
        &mut self,
        outer_iter: usize,
        residual_norm: R,
    ) -> Result<(), crate::error::KError> {
        ObjPreconditioner::on_restart(self, outer_iter, residual_norm)
    }
}

#[cfg(all(test, not(feature = "complex")))]
mod tests {
    use super::*;
    use crate::core::traits::MatVec;
    use crate::matrix::op::CsrOp;
    use crate::preconditioner::PcSide;
    use std::sync::Arc;

    /// Simple dense matrix for testing
    struct DenseMat<T> {
        data: Vec<Vec<T>>,
    }
    impl<T: Copy> DenseMat<T> {
        fn new(data: Vec<Vec<T>>) -> Self {
            Self { data }
        }
    }
    impl<T> MatVec<Vec<T>> for DenseMat<T>
    where
        T: Copy + std::ops::Mul<Output = T> + std::iter::Sum,
    {
        fn matvec(&self, x: &Vec<T>, y: &mut Vec<T>) {
            for i in 0..self.data.len() {
                y[i] = (0..self.data[0].len())
                    .map(|j| self.data[i][j] * x[j])
                    .sum();
            }
        }
    }

    #[test]
    fn chebyshev_identity() {
        // Test Chebyshev filter on the identity matrix
        let a = DenseMat::new(vec![vec![1.0f64, 0.0], vec![0.0, 1.0]]);
        let r = vec![2.0f64, 3.0];
        let mut z = vec![0.0; 2];
        // Chebyshev(1) on identity does NOT act as identity due to scaling/normalization.
        // Just check for finite output.
        apply_chebyshev(&a, &r, &mut z, 1.0, 1.0, 1);
        assert!(z.iter().all(|&zi| zi.is_finite()));
    }

    #[test]
    fn chebyshev_diagonal() {
        // Test Chebyshev filter on a diagonal matrix
        let a = DenseMat::new(vec![vec![2.0f64, 0.0], vec![0.0, 3.0]]);
        let r = vec![1.0f64, 1.0];
        let mut z = vec![0.0; 2];
        // Chebyshev(1) with known spectrum
        apply_chebyshev(&a, &r, &mut z, 2.0, 3.0, 1);
        // Just check for finite output
        assert!(z.iter().all(|&zi| zi.is_finite()));
    }

    #[test]
    fn chebyshev_pc_degenerate_copy() {
        // lam_min == lam_max triggers copy-through path
        let row_ptr = vec![0, 1, 2];
        let col_idx = vec![0, 1];
        let values = vec![1.0, 1.0];
        let csr = Arc::new(CsrMatrix::from_csr(2, 2, row_ptr, col_idx, values));
        let op = CsrOp::new(csr);

        let mut pc = ChebyshevPc::new(3, 1.0, 1.0);
        pc.setup(&op).expect("setup");

        let rhs = vec![2.5, -3.0];
        let mut out = vec![0.0; rhs.len()];
        pc.apply(PcSide::Left, &rhs, &mut out).expect("apply");

        for i in 0..rhs.len() {
            assert!((out[i] - rhs[i]).abs() < 1e-14);
        }
    }

    #[test]
    fn chebyshev_pc_basic_spd() {
        // Simple SPD tridiagonal matrix
        let row_ptr = vec![0, 2, 5, 7];
        let col_idx = vec![0, 1, 0, 1, 2, 1, 2];
        let values = vec![2.0, -1.0, -1.0, 2.0, -1.0, -1.0, 2.0];
        let csr = Arc::new(CsrMatrix::from_csr(3, 3, row_ptr, col_idx, values));
        let op = CsrOp::new(csr);

        let mut pc = ChebyshevPc::new(2, 0.1, 4.0);
        pc.setup(&op).expect("setup");

        let rhs = vec![1.0, 0.0, -1.0];
        let mut out = vec![0.0; rhs.len()];
        pc.apply(PcSide::Left, &rhs, &mut out).expect("apply");

        assert!(out.iter().all(|zi| zi.is_finite()));
        assert!(out.iter().any(|&zi| zi.abs() > 1e-6));
    }
}

#[cfg(all(test, feature = "complex"))]
mod complex_tests {
    use super::*;
    use crate::algebra::bridge::BridgeScratch;
    use crate::error::KError;
    use crate::ops::kpc::KPreconditioner;
    use crate::preconditioner::PcSide;

    #[test]
    fn apply_s_reports_unsupported() {
        let pc = ChebyshevPc::new(2, 0.5, 3.0);
        let rhs = vec![S::one(); 2];
        let mut out = vec![S::zero(); rhs.len()];
        let mut scratch = BridgeScratch::default();
        let err = pc
            .apply_s(PcSide::Left, &rhs, &mut out, &mut scratch)
            .unwrap_err();
        assert!(matches!(err, KError::Unsupported(_)));
    }
}