scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
//! Extended matrix functions: square root, logarithm, sign, p-th root, condition
//!
//! Provides advanced matrix function algorithms beyond the basic set in
//! `matrix_functions`:
//!
//! - **Matrix square root** (Denman-Beavers iteration, Schur method)
//! - **Matrix logarithm** (inverse scaling and squaring + Pade approximation)
//! - **Matrix sign function** (Roberts iteration)
//! - **Matrix p-th root** (Newton-Schulz iteration)
//! - **Condition number** for matrix functions

use crate::error::{LinalgError, LinalgResult};
use scirs2_core::ndarray::{Array2, ArrayView2, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign};
use std::iter::Sum;

// ---------------------------------------------------------------------------
// Trait alias
// ---------------------------------------------------------------------------

/// Trait alias for floating-point bounds used across this module.
pub trait MFFloat: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static {}
impl<T> MFFloat for T where T: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static {}

// ===================================================================
// Helper: matrix Frobenius norm (avoids importing norm module)
// ===================================================================

fn frobenius_norm<F: MFFloat>(m: &Array2<F>) -> F {
    let mut acc = F::zero();
    for &v in m.iter() {
        acc += v * v;
    }
    acc.sqrt()
}

// ===================================================================
// Matrix square root -- Denman-Beavers iteration
// ===================================================================

/// Result of a matrix square root computation.
#[derive(Debug, Clone)]
pub struct MatrixSqrtResult<F> {
    /// The principal square root S such that S*S ~ A.
    pub sqrt: Array2<F>,
    /// Number of iterations used.
    pub iterations: usize,
    /// Final residual ||S*S - A||_F.
    pub residual: F,
}

/// Compute the matrix square root using the Denman-Beavers iteration.
///
/// Given a nonsingular matrix A, this finds S such that S*S = A.
/// The iteration simultaneously converges Y_k -> S and Z_k -> S^{-1}:
///
///   Y_{k+1} = (Y_k + Z_k^{-1}) / 2
///   Z_{k+1} = (Z_k + Y_k^{-1}) / 2
///
/// # Arguments
/// * `a`       - Input nonsingular square matrix
/// * `max_iter`- Maximum iterations (default 100)
/// * `tol`     - Convergence tolerance (default 1e-12)
///
/// # Example
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions_extended::sqrt_denman_beavers;
///
/// let a = array![[4.0_f64, 0.0], [0.0, 9.0]];
/// let res = sqrt_denman_beavers(&a.view(), None, None).expect("sqrt failed");
/// // res.sqrt should be approximately [[2, 0], [0, 3]]
/// assert!((res.sqrt[[0, 0]] - 2.0_f64).abs() < 1e-8);
/// assert!((res.sqrt[[1, 1]] - 3.0_f64).abs() < 1e-8);
/// ```
pub fn sqrt_denman_beavers<F: MFFloat>(
    a: &ArrayView2<F>,
    max_iter: Option<usize>,
    tol: Option<F>,
) -> LinalgResult<MatrixSqrtResult<F>> {
    let n = a.shape()[0];
    if a.shape()[1] != n {
        return Err(LinalgError::ShapeError("Matrix must be square".into()));
    }

    let max_it = max_iter.unwrap_or(100);
    let eps = tol.unwrap_or_else(|| F::from(1e-12).unwrap_or(F::epsilon()));
    let half =
        F::from(0.5).ok_or_else(|| LinalgError::ComputationError("Cannot convert 0.5".into()))?;

    let mut y = a.to_owned();
    let mut z = Array2::<F>::eye(n);

    for iter in 0..max_it {
        let z_inv = crate::inv(&z.view(), None)?;
        let y_inv = crate::inv(&y.view(), None)?;

        let y_new = (&y + &z_inv) * half;
        let z_new = (&z + &y_inv) * half;

        let diff = frobenius_norm(&(&y_new - &y));
        y = y_new;
        z = z_new;

        if diff < eps {
            let residual = frobenius_norm(&(y.dot(&y) - a));
            return Ok(MatrixSqrtResult {
                sqrt: y,
                iterations: iter + 1,
                residual,
            });
        }
    }

    let residual = frobenius_norm(&(y.dot(&y) - a));
    Ok(MatrixSqrtResult {
        sqrt: y,
        iterations: max_it,
        residual,
    })
}

/// Compute the matrix square root using the Schur method.
///
/// 1. Compute Schur decomposition A = Q T Q^T
/// 2. Compute square root of quasi-upper-triangular T element-by-element
/// 3. Back-transform: S = Q * sqrt(T) * Q^T
///
/// More robust than Denman-Beavers for ill-conditioned matrices.
///
/// # Example
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions_extended::sqrt_schur;
///
/// let a = array![[4.0_f64, 0.0], [0.0, 9.0]];
/// let s = sqrt_schur(&a.view()).expect("sqrt failed");
/// assert!((s[[0, 0]] - 2.0_f64).abs() < 1e-6);
/// assert!((s[[1, 1]] - 3.0_f64).abs() < 1e-6);
/// ```
pub fn sqrt_schur<F: MFFloat>(a: &ArrayView2<F>) -> LinalgResult<Array2<F>> {
    let n = a.shape()[0];
    if a.shape()[1] != n {
        return Err(LinalgError::ShapeError("Matrix must be square".into()));
    }
    if n == 0 {
        return Ok(Array2::<F>::zeros((0, 0)));
    }

    let (q, t) = crate::decomposition::schur(a)?;

    // Compute square root of quasi-upper-triangular T
    let mut s = Array2::<F>::zeros((n, n));

    // Diagonal elements first
    for i in 0..n {
        if t[[i, i]] < F::zero() {
            return Err(LinalgError::DomainError(
                "Matrix has negative eigenvalues; principal square root does not exist".into(),
            ));
        }
        s[[i, i]] = t[[i, i]].sqrt();
    }

    // Super-diagonal elements by recurrence:
    // s_{ij} = (t_{ij} - sum_{k=i+1}^{j-1} s_{ik} s_{kj}) / (s_{ii} + s_{jj})
    for j in 1..n {
        for i in (0..j).rev() {
            let mut off_sum = F::zero();
            for k in (i + 1)..j {
                off_sum += s[[i, k]] * s[[k, j]];
            }
            let denom = s[[i, i]] + s[[j, j]];
            if denom.abs() < F::epsilon() {
                // Nearly repeated eigenvalues; use a fallback
                s[[i, j]] = F::zero();
            } else {
                s[[i, j]] = (t[[i, j]] - off_sum) / denom;
            }
        }
    }

    // Back-transform: sqrt(A) = Q S Q^T
    Ok(q.dot(&s).dot(&q.t()))
}

// ===================================================================
// Matrix logarithm -- inverse scaling and squaring + Pade
// ===================================================================

/// Compute the matrix logarithm via inverse scaling and squaring with Pade
/// approximation.
///
/// Algorithm:
/// 1. Scale A by repeated square-rooting until ||A - I|| is small
/// 2. Apply diagonal Pade approximant to log(I + X) where X = A_scaled - I
/// 3. Undo the scaling: log(A) = 2^s * log(A_scaled)
///
/// # Arguments
/// * `a`       - Input square matrix with no eigenvalues on the closed negative real axis
/// * `max_sqrt`- Maximum scaling steps (default 50)
///
/// # Example
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions_extended::logm_iss;
///
/// let a = array![[2.718281828_f64, 0.0], [0.0, 7.389056099]];
/// let l = logm_iss(&a.view(), None).expect("logm failed");
/// // log of diag(e, e^2) ~ diag(1, 2)
/// assert!((l[[0, 0]] - 1.0_f64).abs() < 1e-4);
/// assert!((l[[1, 1]] - 2.0_f64).abs() < 1e-4);
/// ```
pub fn logm_iss<F: MFFloat>(a: &ArrayView2<F>, max_sqrt: Option<usize>) -> LinalgResult<Array2<F>> {
    let n = a.shape()[0];
    if a.shape()[1] != n {
        return Err(LinalgError::ShapeError("Matrix must be square".into()));
    }
    if n == 0 {
        return Ok(Array2::<F>::zeros((0, 0)));
    }

    let max_s = max_sqrt.unwrap_or(50);
    let eye = Array2::<F>::eye(n);

    // Phase 1: repeated square-root scaling until ||A_k - I||_F < 0.5
    let mut a_k = a.to_owned();
    let mut s: usize = 0;
    let threshold = F::from(0.5).unwrap_or(F::one());

    for _ in 0..max_s {
        let diff = frobenius_norm(&(&a_k - &eye));
        if diff < threshold {
            break;
        }
        // a_k <- sqrt(a_k) via Denman-Beavers (quick, low-iter)
        let res = sqrt_denman_beavers(&a_k.view(), Some(40), None)?;
        a_k = res.sqrt;
        s += 1;
    }

    // Phase 2: Pade approximation for log(I + X), X = a_k - I
    let x = &a_k - &eye;

    // Use [m/m] diagonal Pade of degree m = 7 for log(1+x)
    // Coefficients derived from Pade table for log(1+x)
    // We use the recurrence-based approach for the matrix rational function.
    let log_x = pade_log_approximant(&x, 8)?;

    // Phase 3: undo scaling
    let two = F::one() + F::one();
    let scale = two.powi(s as i32);
    Ok(log_x * scale)
}

/// Pade approximant for log(I + X) using partial fractions.
/// Uses the [m/m] diagonal Pade approximant computed via the identity:
///   log(I + X) ~ X * (I + X/2)^{-1}  (order-1 Pade)
/// For higher order, we iterate: use the identity
///   log(I + X) = sum_{k=1}^{m} c_k * X * (I + d_k * X)^{-1}
/// with coefficients from Gauss-Legendre quadrature of integral representation.
fn pade_log_approximant<F: MFFloat>(x: &Array2<F>, order: usize) -> LinalgResult<Array2<F>> {
    let n = x.shape()[0];
    let eye = Array2::<F>::eye(n);

    // Use Gauss-Legendre quadrature nodes/weights for
    //   log(I + X) = integral_0^1 X (I + t X)^{-1} dt
    // For `order` quadrature points:
    let (nodes, weights) = gauss_legendre_nodes(order);

    let mut result = Array2::<F>::zeros((n, n));
    for k in 0..order {
        let t_k = F::from(nodes[k]).unwrap_or(F::zero());
        let w_k = F::from(weights[k]).unwrap_or(F::zero());
        // (I + t_k * X)^{-1}
        let mat = &eye + &(x * t_k);
        let mat_inv = crate::inv(&mat.view(), None)?;
        // accumulate w_k * X * mat_inv
        result = result + x.dot(&mat_inv) * w_k;
    }
    Ok(result)
}

/// Gauss-Legendre quadrature nodes and weights on [0, 1].
/// Returns (nodes, weights) for the given order.
fn gauss_legendre_nodes(order: usize) -> (Vec<f64>, Vec<f64>) {
    // Pre-computed for common orders, transformed from [-1,1] to [0,1]
    match order {
        1 => (vec![0.5], vec![1.0]),
        2 => (vec![0.2113248654, 0.7886751346], vec![0.5, 0.5]),
        4 => (
            vec![0.0694318442, 0.3300094782, 0.6699905218, 0.9305681558],
            vec![0.1739274226, 0.3260725774, 0.3260725774, 0.1739274226],
        ),
        8 => (
            vec![
                0.0198550718,
                0.1016667613,
                0.2372337950,
                0.4082826788,
                0.5917173212,
                0.7627662050,
                0.8983332387,
                0.9801449282,
            ],
            vec![
                0.0506142681,
                0.1111905172,
                0.1568533229,
                0.1813418917,
                0.1813418917,
                0.1568533229,
                0.1111905172,
                0.0506142681,
            ],
        ),
        _ => {
            // Fallback: uniform trapezoid rule
            let mut nodes = Vec::with_capacity(order);
            let mut weights = Vec::with_capacity(order);
            let h = 1.0 / (order as f64);
            for i in 0..order {
                nodes.push((i as f64 + 0.5) * h);
                weights.push(h);
            }
            (nodes, weights)
        }
    }
}

// ===================================================================
// Matrix sign function -- Roberts iteration
// ===================================================================

/// Result of matrix sign function computation.
#[derive(Debug, Clone)]
pub struct MatrixSignResult<F> {
    /// The matrix sign S such that S*S ~ I.
    pub sign: Array2<F>,
    /// Number of iterations used.
    pub iterations: usize,
    /// Final residual ||S*S - I||_F.
    pub residual: F,
}

/// Compute the matrix sign function using the Roberts iteration.
///
/// The Roberts iteration is:
///   S_{k+1} = (S_k + S_k^{-1}) / 2
///
/// starting from S_0 = A. Converges quadratically to sign(A).
///
/// The matrix sign function satisfies sign(A)^2 = I and can be used
/// to split the spectrum of A into stable/unstable parts.
///
/// # Arguments
/// * `a`       - Input nonsingular square matrix
/// * `max_iter`- Maximum iterations (default 100)
/// * `tol`     - Convergence tolerance (default 1e-12)
///
/// # Example
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions_extended::sign_roberts;
///
/// // For a positive definite matrix, sign(A) = I
/// let a = array![[2.0_f64, 0.0], [0.0, 3.0]];
/// let res = sign_roberts(&a.view(), None, None).expect("sign failed");
/// assert!((res.sign[[0, 0]] - 1.0_f64).abs() < 1e-8);
/// assert!((res.sign[[1, 1]] - 1.0_f64).abs() < 1e-8);
/// ```
pub fn sign_roberts<F: MFFloat>(
    a: &ArrayView2<F>,
    max_iter: Option<usize>,
    tol: Option<F>,
) -> LinalgResult<MatrixSignResult<F>> {
    let n = a.shape()[0];
    if a.shape()[1] != n {
        return Err(LinalgError::ShapeError("Matrix must be square".into()));
    }

    let max_it = max_iter.unwrap_or(100);
    let eps = tol.unwrap_or_else(|| F::from(1e-12).unwrap_or(F::epsilon()));
    let half =
        F::from(0.5).ok_or_else(|| LinalgError::ComputationError("Cannot convert 0.5".into()))?;

    let eye = Array2::<F>::eye(n);
    let mut s = a.to_owned();

    for iter in 0..max_it {
        let s_inv = crate::inv(&s.view(), None)?;
        let s_new = (&s + &s_inv) * half;
        let diff = frobenius_norm(&(&s_new - &s));
        s = s_new;

        if diff < eps {
            let residual = frobenius_norm(&(s.dot(&s) - &eye));
            return Ok(MatrixSignResult {
                sign: s,
                iterations: iter + 1,
                residual,
            });
        }
    }

    let residual = frobenius_norm(&(s.dot(&s) - &eye));
    Ok(MatrixSignResult {
        sign: s,
        iterations: max_it,
        residual,
    })
}

// ===================================================================
// Matrix p-th root -- Newton-Schulz iteration
// ===================================================================

/// Result of the matrix p-th root computation.
#[derive(Debug, Clone)]
pub struct MatrixPthRootResult<F> {
    /// The p-th root R such that R^p ~ A.
    pub root: Array2<F>,
    /// Number of iterations used.
    pub iterations: usize,
    /// Final residual.
    pub residual: F,
}

/// Compute the matrix p-th root via a coupled Newton iteration.
///
/// Finds R such that R^p = A using the iteration:
///   R_{k+1} = ((p-1) R_k + R_k^{1-p} A) / p
///
/// with simplification via the inverse iterates:
///   R_{k+1} = R_k * ((p-1)I + M_k) / p
///   M_{k+1} = ((p-1)I + M_k)^{-p} * M_k   (where M_k approximates R_k^{-p} A)
///
/// A simpler formulation is used: coupled Newton for p-th root.
///
/// # Arguments
/// * `a`       - Input nonsingular square matrix
/// * `p`       - Root degree (>= 2)
/// * `max_iter`- Maximum iterations (default 200)
/// * `tol`     - Convergence tolerance (default 1e-10)
///
/// # Example
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions_extended::pth_root;
///
/// // Cube root of diag(8, 27) = diag(2, 3)
/// let a = array![[8.0_f64, 0.0], [0.0, 27.0]];
/// let res = pth_root(&a.view(), 3, None, None).expect("pth_root failed");
/// assert!((res.root[[0, 0]] - 2.0_f64).abs() < 1e-4);
/// assert!((res.root[[1, 1]] - 3.0_f64).abs() < 1e-4);
/// ```
pub fn pth_root<F: MFFloat>(
    a: &ArrayView2<F>,
    p: usize,
    max_iter: Option<usize>,
    tol: Option<F>,
) -> LinalgResult<MatrixPthRootResult<F>> {
    let n = a.shape()[0];
    if a.shape()[1] != n {
        return Err(LinalgError::ShapeError("Matrix must be square".into()));
    }
    if p < 2 {
        return Err(LinalgError::ValueError("Root degree p must be >= 2".into()));
    }

    let max_it = max_iter.unwrap_or(200);
    let eps = tol.unwrap_or_else(|| F::from(1e-10).unwrap_or(F::epsilon()));

    let eye = Array2::<F>::eye(n);
    let p_f = F::from(p)
        .ok_or_else(|| LinalgError::ComputationError("Cannot convert p to float".into()))?;
    let p_minus_1 =
        F::from(p - 1).ok_or_else(|| LinalgError::ComputationError("Cannot convert p-1".into()))?;
    let inv_p = F::one() / p_f;

    // Coupled Newton iteration for p-th root
    // X_{k+1} = ((p-1) X_k + X_k^{1-p} A) / p
    // M_{k+1} = M_k ((p-1) I + M_k)^{-1}... simplified as:
    // Use M_k = X_k^{-1} A approach:
    //   X_{k+1} = X_k * (p_minus_1 * I + M_k) / p
    //   M_{k+1} = (p_minus_1 * I + M_k)^{-(p-1)} * M_k^{p-1} ... too complex.
    //
    // Instead use the DB-style coupled iteration for p-th root:
    //   X_{k+1} = X_k * ((p-1)I + M_k) * inv_p
    //   M_{k+1} = inv_p^p * ((p-1)I + M_k)^{-p} * M_k   -- but simplified:
    //
    // Actually, use the standard coupled iteration (Iannazzo 2006):
    //   X_{k+1} = X_k * (I + (p-1)(I - M_k)/p)
    //            = X_k * ((p-1+p)I - (p-1)M_k) / p   -- no.
    //
    // Simplest correct approach: Newton on f(X) = X^p - A.
    //   X_{k+1} = X_k - (X_k^p - A) * (p * X_k^{p-1})^{-1}
    //           = X_k - X_k * (I - X_k^{-p} * A) / p
    //           = X_k * ((p-1)I + X_k^{-p} * A) / p

    // Start with X_0 = I * ||A||^{1/p}
    let a_norm = frobenius_norm(&a.to_owned());
    let scale = a_norm.powf(F::one() / p_f);
    let mut x_k = &eye * scale;

    for iter in 0..max_it {
        let x_old = x_k.clone();

        // Compute X_k^{-1}
        let x_inv = crate::inv(&x_k.view(), None)?;

        // Compute X_k^{-p} * A iteratively: start with x_inv, multiply (p-1) more times
        let mut x_inv_p_a = x_inv.clone();
        for _ in 1..p {
            x_inv_p_a = x_inv_p_a.dot(&x_inv);
        }
        x_inv_p_a = x_inv_p_a.dot(a);

        // X_{k+1} = X_k * ((p-1)I + X_k^{-p} * A) / p
        let bracket = &eye * p_minus_1 + &x_inv_p_a;
        x_k = x_k.dot(&bracket) * inv_p;

        let diff = frobenius_norm(&(&x_k - &x_old));
        if diff < eps {
            // Compute residual: ||X^p - A||
            let mut x_p = x_k.clone();
            for _ in 1..p {
                x_p = x_p.dot(&x_k);
            }
            let residual = frobenius_norm(&(x_p - a));
            return Ok(MatrixPthRootResult {
                root: x_k,
                iterations: iter + 1,
                residual,
            });
        }
    }

    let mut x_p = x_k.clone();
    for _ in 1..p {
        x_p = x_p.dot(&x_k);
    }
    let residual = frobenius_norm(&(x_p - a));
    Ok(MatrixPthRootResult {
        root: x_k,
        iterations: max_it,
        residual,
    })
}

// ===================================================================
// Condition number for matrix functions
// ===================================================================

/// Estimate the condition number of a matrix function f(A) using
/// finite differences.
///
/// cond_f(A) ~ ||f(A + epsilon*E) - f(A)||_F / (epsilon * ||E||_F) * ||A||_F / ||f(A)||_F
///
/// where E is a random perturbation direction, averaged over several trials.
///
/// # Arguments
/// * `a`    - Input square matrix
/// * `f`    - Matrix function to evaluate
/// * `n_samples` - Number of random perturbation directions (default 5)
///
/// # Example
/// ```
/// use scirs2_core::ndarray::{Array2, ArrayView2};
/// use scirs2_linalg::matrix_functions_extended::matrix_function_condition;
/// use scirs2_linalg::error::LinalgResult;
///
/// fn my_exp(a: &ArrayView2<f64>) -> LinalgResult<Array2<f64>> {
///     scirs2_linalg::matrix_functions::expm(a, None)
/// }
///
/// let a = scirs2_core::ndarray::array![[1.0, 0.1], [0.0, 2.0]];
/// let cond = matrix_function_condition(&a.view(), my_exp, None).expect("cond failed");
/// assert!(cond > 0.0);
/// ```
pub fn matrix_function_condition<F: MFFloat>(
    a: &ArrayView2<F>,
    f: fn(&ArrayView2<F>) -> LinalgResult<Array2<F>>,
    n_samples: Option<usize>,
) -> LinalgResult<F> {
    let n = a.shape()[0];
    if a.shape()[1] != n {
        return Err(LinalgError::ShapeError("Matrix must be square".into()));
    }

    let samples = n_samples.unwrap_or(5);
    let eps_val = F::from(1e-7).unwrap_or(F::epsilon());

    let f_a = f(a)?;
    let norm_a = frobenius_norm(&a.to_owned());
    let norm_fa = frobenius_norm(&f_a);

    if norm_fa < F::epsilon() {
        return Ok(F::infinity());
    }

    let mut max_cond = F::zero();

    // Use deterministic perturbation directions
    for trial in 0..samples {
        let mut e_mat = Array2::<F>::zeros((n, n));
        // Create a structured perturbation
        for i in 0..n {
            for j in 0..n {
                // Simple deterministic pattern: use trial index to vary direction
                let val = F::from((i + j + trial) % 7).unwrap_or(F::one())
                    - F::from(3).unwrap_or(F::zero());
                e_mat[[i, j]] = val;
            }
        }
        let norm_e = frobenius_norm(&e_mat);
        if norm_e < F::epsilon() {
            continue;
        }
        // Normalize
        let e_norm = &e_mat * (F::one() / norm_e);

        let a_pert = a + &(&e_norm * eps_val);
        let f_pert = f(&a_pert.view())?;
        let diff = &f_pert - &f_a;
        let norm_diff = frobenius_norm(&diff);

        let cond_est = (norm_diff / eps_val) * (norm_a / norm_fa);
        if cond_est > max_cond {
            max_cond = cond_est;
        }
    }

    Ok(max_cond)
}

// ===================================================================
// Tests
// ===================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_abs_diff_eq;
    use scirs2_core::ndarray::array;

    // --- Matrix square root (Denman-Beavers) ---

    #[test]
    fn test_sqrt_db_diagonal() {
        let a = array![[4.0, 0.0], [0.0, 9.0]];
        let res = sqrt_denman_beavers(&a.view(), None, None).expect("sqrt failed");
        assert_abs_diff_eq!(res.sqrt[[0, 0]], 2.0, epsilon = 1e-8);
        assert_abs_diff_eq!(res.sqrt[[1, 1]], 3.0, epsilon = 1e-8);
        assert!(res.residual < 1e-8);
    }

    #[test]
    fn test_sqrt_db_identity() {
        let a = array![[1.0, 0.0], [0.0, 1.0]];
        let res = sqrt_denman_beavers(&a.view(), None, None).expect("sqrt failed");
        for i in 0..2 {
            for j in 0..2 {
                let expected = if i == j { 1.0 } else { 0.0 };
                assert_abs_diff_eq!(res.sqrt[[i, j]], expected, epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_sqrt_db_general() {
        let a = array![[2.0, 1.0], [1.0, 3.0]];
        let res = sqrt_denman_beavers(&a.view(), None, None).expect("sqrt failed");
        // Verify S*S ~ A
        let ss = res.sqrt.dot(&res.sqrt);
        for i in 0..2 {
            for j in 0..2 {
                assert_abs_diff_eq!(ss[[i, j]], a[[i, j]], epsilon = 1e-8);
            }
        }
    }

    // --- Matrix square root (Schur) ---

    #[test]
    fn test_sqrt_schur_diagonal() {
        let a = array![[4.0, 0.0], [0.0, 9.0]];
        let s = sqrt_schur(&a.view()).expect("sqrt failed");
        assert_abs_diff_eq!(s[[0, 0]], 2.0, epsilon = 1e-4);
        assert_abs_diff_eq!(s[[1, 1]], 3.0, epsilon = 1e-4);
    }

    #[test]
    fn test_sqrt_schur_identity() {
        let eye = array![[1.0, 0.0], [0.0, 1.0]];
        let s = sqrt_schur(&eye.view()).expect("sqrt failed");
        for i in 0..2 {
            for j in 0..2 {
                let expected = if i == j { 1.0 } else { 0.0 };
                assert_abs_diff_eq!(s[[i, j]], expected, epsilon = 1e-6);
            }
        }
    }

    #[test]
    fn test_sqrt_schur_general_2x2() {
        let a = array![[5.0, 2.0], [2.0, 5.0]];
        let s = sqrt_schur(&a.view()).expect("sqrt failed");
        let ss = s.dot(&s);
        for i in 0..2 {
            for j in 0..2 {
                assert_abs_diff_eq!(ss[[i, j]], a[[i, j]], epsilon = 1e-4);
            }
        }
    }

    // --- Matrix logarithm (ISS) ---

    #[test]
    fn test_logm_iss_identity() {
        let eye = array![[1.0, 0.0], [0.0, 1.0]];
        let l = logm_iss(&eye.view(), None).expect("logm failed");
        for i in 0..2 {
            for j in 0..2 {
                assert_abs_diff_eq!(l[[i, j]], 0.0, epsilon = 1e-8);
            }
        }
    }

    #[test]
    fn test_logm_iss_diagonal() {
        let e_val = std::f64::consts::E;
        let a = array![[e_val, 0.0], [0.0, e_val * e_val]];
        let l = logm_iss(&a.view(), None).expect("logm failed");
        assert_abs_diff_eq!(l[[0, 0]], 1.0, epsilon = 1e-4);
        assert_abs_diff_eq!(l[[1, 1]], 2.0, epsilon = 1e-4);
        assert!(l[[0, 1]].abs() < 1e-4);
        assert!(l[[1, 0]].abs() < 1e-4);
    }

    // --- Matrix sign (Roberts) ---

    #[test]
    fn test_sign_positive_definite() {
        let a = array![[2.0, 0.0], [0.0, 3.0]];
        let res = sign_roberts(&a.view(), None, None).expect("sign failed");
        assert_abs_diff_eq!(res.sign[[0, 0]], 1.0, epsilon = 1e-8);
        assert_abs_diff_eq!(res.sign[[1, 1]], 1.0, epsilon = 1e-8);
        assert!(res.residual < 1e-8);
    }

    #[test]
    fn test_sign_mixed_spectrum() {
        // sign(diag(2, -3)) = diag(1, -1)
        let a = array![[2.0, 0.0], [0.0, -3.0]];
        let res = sign_roberts(&a.view(), None, None).expect("sign failed");
        assert_abs_diff_eq!(res.sign[[0, 0]], 1.0, epsilon = 1e-8);
        assert_abs_diff_eq!(res.sign[[1, 1]], -1.0, epsilon = 1e-8);
    }

    #[test]
    fn test_sign_involutory() {
        // sign(A)^2 = I
        let a = array![[3.0, 1.0], [0.0, -2.0]];
        let res = sign_roberts(&a.view(), None, None).expect("sign failed");
        let ss = res.sign.dot(&res.sign);
        for i in 0..2 {
            for j in 0..2 {
                let expected = if i == j { 1.0 } else { 0.0 };
                assert_abs_diff_eq!(ss[[i, j]], expected, epsilon = 1e-6);
            }
        }
    }

    // --- p-th root ---

    #[test]
    fn test_pth_root_cube_root_diagonal() {
        let a = array![[8.0, 0.0], [0.0, 27.0]];
        let res = pth_root(&a.view(), 3, None, None).expect("pth_root failed");
        assert_abs_diff_eq!(res.root[[0, 0]], 2.0, epsilon = 1e-4);
        assert_abs_diff_eq!(res.root[[1, 1]], 3.0, epsilon = 1e-4);
    }

    #[test]
    fn test_pth_root_square_root() {
        // p=2 should give the same as matrix square root
        let a = array![[4.0, 0.0], [0.0, 16.0]];
        let res = pth_root(&a.view(), 2, None, None).expect("pth_root failed");
        assert_abs_diff_eq!(res.root[[0, 0]], 2.0, epsilon = 1e-4);
        assert_abs_diff_eq!(res.root[[1, 1]], 4.0, epsilon = 1e-4);
    }

    #[test]
    fn test_pth_root_identity() {
        let eye = array![[1.0, 0.0], [0.0, 1.0]];
        let res = pth_root(&eye.view(), 5, None, None).expect("pth_root failed");
        for i in 0..2 {
            for j in 0..2 {
                let expected = if i == j { 1.0 } else { 0.0 };
                assert_abs_diff_eq!(res.root[[i, j]], expected, epsilon = 1e-6);
            }
        }
    }

    // --- Condition number ---

    #[test]
    fn test_condition_number_exp() {
        fn my_exp(a: &ArrayView2<f64>) -> LinalgResult<Array2<f64>> {
            crate::matrix_functions::expm(a, None)
        }
        let a = array![[1.0, 0.0], [0.0, 2.0]];
        let cond = matrix_function_condition(&a.view(), my_exp, Some(3)).expect("cond failed");
        assert!(cond > 0.0);
        assert!(cond < 1e10);
    }

    #[test]
    fn test_sqrt_db_nonsquare_error() {
        let a = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        let res = sqrt_denman_beavers(&a.view(), None, None);
        assert!(res.is_err());
    }

    #[test]
    fn test_pth_root_invalid_p() {
        let a = array![[1.0, 0.0], [0.0, 1.0]];
        let res = pth_root(&a.view(), 1, None, None);
        assert!(res.is_err());
    }
}