scirs2-linalg 0.4.0

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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
//! Markov chain analysis and stochastic matrix tools
//!
//! This module provides a comprehensive set of tools for analysing discrete-time
//! Markov chains represented by row-stochastic transition matrices P where each
//! row sums to 1 and all entries are non-negative.
//!
//! ## Functions provided
//!
//! - [`stationary_distribution`] – Power-iteration to find the unique stationary
//!   distribution π satisfying π = π P, ‖π‖₁ = 1.
//! - [`is_stochastic`] / [`is_stochastic_f`] – Validate stochasticity.
//! - [`mean_first_passage_time`] – Matrix M where M[i,j] is the expected steps to
//!   reach state j starting from state i.
//! - [`fundamental_matrix`] – Z = (I − P + Π)⁻¹ where Π = 1·π^T.
//! - [`mixing_time`] – Estimate the ε-mixing time from the spectral gap.
//! - [`analyze_absorbing_chain`] – Detect absorbing and transient states.
//! - [`absorption_probabilities`] – B matrix of transient→absorbing absorption
//!   probabilities.

use scirs2_core::ndarray::{Array1, Array2, ArrayView2, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign};
use std::iter::Sum;

use crate::error::{LinalgError, LinalgResult};
use crate::solve::solve_multiple;
use crate::validation::validate_squarematrix;

// ─────────────────────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────────────────────

/// Euclidean norm of a 1-D array.
fn norm2_1d<F: Float + Sum>(v: &Array1<F>) -> F {
    v.iter().map(|&x| x * x).fold(F::zero(), |a, b| a + b).sqrt()
}

/// L1 norm of a 1-D array (sum of absolute values).
fn norm1_1d<F: Float + Sum>(v: &Array1<F>) -> F {
    v.iter().map(|&x| x.abs()).fold(F::zero(), |a, b| a + b)
}

/// Compute π^T · P (left multiplication of row vector by matrix).
fn left_multiply_row<F>(pi: &Array1<F>, p: &ArrayView2<F>) -> Array1<F>
where
    F: Float + NumAssign + ScalarOperand,
{
    let n = p.ncols();
    let mut result = Array1::zeros(n);
    for j in 0..n {
        let mut sum = F::zero();
        for i in 0..n {
            sum += pi[i] * p[[i, j]];
        }
        result[j] = sum;
    }
    result
}

// ─────────────────────────────────────────────────────────────────────────────
// Stochasticity validation
// ─────────────────────────────────────────────────────────────────────────────

/// Check whether a matrix is row-stochastic (rows sum to 1, all entries ≥ 0).
///
/// Uses an absolute tolerance `tol` around 1.0 for row sums and 0.0 for
/// non-negativity.
///
/// # Examples
///
/// ```
/// use scirs2_linalg::matrix_functions::is_stochastic;
/// use scirs2_core::ndarray::array;
///
/// let p = array![[0.7_f64, 0.3], [0.4, 0.6]];
/// assert!(is_stochastic(&p.view(), 1e-10));
/// ```
pub fn is_stochastic<F>(p: &ArrayView2<F>, tol: F) -> bool
where
    F: Float + Sum + NumAssign + ScalarOperand,
{
    if p.nrows() != p.ncols() {
        return false;
    }
    let n = p.nrows();
    for i in 0..n {
        // Non-negativity
        for j in 0..n {
            if p[[i, j]] < -tol {
                return false;
            }
        }
        // Row sum ≈ 1
        let row_sum: F = (0..n).map(|j| p[[i, j]]).fold(F::zero(), |a, b| a + b);
        if (row_sum - F::one()).abs() > tol {
            return false;
        }
    }
    true
}

/// Convenience overload for `f64` with a hard-coded tolerance check.
///
/// # Examples
///
/// ```
/// use scirs2_linalg::matrix_functions::is_stochastic_f64;
/// use scirs2_core::ndarray::array;
///
/// let p = array![[0.5_f64, 0.5], [0.3, 0.7]];
/// assert!(is_stochastic_f64(&p.view(), 1e-10));
/// ```
pub fn is_stochastic_f64(p: &ArrayView2<f64>, tol: f64) -> bool {
    is_stochastic(p, tol)
}

// ─────────────────────────────────────────────────────────────────────────────
// Stationary distribution
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the stationary distribution of an ergodic Markov chain by power
/// iteration.
///
/// Iterates π^(k+1) = π^(k) · P until ‖π^(k+1) − π^(k)‖₂ < `tol`, then
/// normalises so that ‖π‖₁ = 1.
///
/// # Arguments
///
/// * `transition_matrix` – Row-stochastic square transition matrix P.
/// * `tol` – Convergence tolerance for successive π iterates.
/// * `max_iter` – Maximum number of power iterations.
///
/// # Returns
///
/// Stationary distribution vector π with ∑ πᵢ = 1.
///
/// # Errors
///
/// Returns [`LinalgError::ConvergenceError`] if the iteration does not converge,
/// or [`LinalgError::InvalidInputError`] if P is not square or not stochastic.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions::stationary_distribution;
///
/// let p = array![[0.7_f64, 0.3], [0.4, 0.6]];
/// let pi = stationary_distribution(&p.view(), 1e-12, 10_000)
///     .expect("Must converge for ergodic chain");
/// // For this chain: π = [4/7, 3/7] ≈ [0.5714, 0.4286]
/// assert!((pi[0] - 4.0/7.0).abs() < 1e-8);
/// ```
pub fn stationary_distribution<F>(
    transition_matrix: &ArrayView2<F>,
    tol: F,
    max_iter: usize,
) -> LinalgResult<Array1<F>>
where
    F: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static + std::fmt::Display,
{
    validate_squarematrix(transition_matrix, "Stationary distribution")?;

    let n = transition_matrix.nrows();

    // Validate stochasticity
    if !is_stochastic(transition_matrix, F::from(1e-6).unwrap_or(F::epsilon() * F::from(1000.0).unwrap_or(F::one()))) {
        return Err(LinalgError::InvalidInputError(
            "Transition matrix must be row-stochastic (rows sum to 1, all entries non-negative)".to_string(),
        ));
    }

    if max_iter == 0 {
        return Err(LinalgError::InvalidInputError(
            "max_iter must be positive".to_string(),
        ));
    }

    // Initialise uniform distribution
    let inv_n = F::one() / F::from(n).ok_or_else(|| {
        LinalgError::ComputationError("Cannot convert n to float".to_string())
    })?;
    let mut pi: Array1<F> = Array1::from_elem(n, inv_n);

    for _ in 0..max_iter {
        let pi_new = left_multiply_row(&pi, transition_matrix);

        // Check convergence
        let diff: Array1<F> = Array1::from_iter(
            pi_new.iter().zip(pi.iter()).map(|(&a, &b)| a - b)
        );
        let change = norm2_1d(&diff);

        pi = pi_new;

        // Renormalise to counteract floating-point drift
        let sum: F = pi.iter().copied().fold(F::zero(), |a, b| a + b);
        if sum > F::epsilon() {
            pi.mapv_inplace(|v| v / sum);
        }

        if change < tol {
            return Ok(pi);
        }
    }

    Err(LinalgError::ConvergenceError(format!(
        "Stationary distribution did not converge in {max_iter} iterations"
    )))
}

// ─────────────────────────────────────────────────────────────────────────────
// Fundamental matrix
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the fundamental matrix Z = (I − P + Π)⁻¹ where Π = 1·π^T.
///
/// The fundamental matrix encodes the expected number of times the chain
/// passes through each state. It is used to derive the mean first-passage
/// time and variance of absorption times.
///
/// # Arguments
///
/// * `transition_matrix` – Row-stochastic square transition matrix P.
///
/// # Returns
///
/// The n×n fundamental matrix Z.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions::fundamental_matrix;
///
/// let p = array![[0.7_f64, 0.3], [0.4, 0.6]];
/// let z = fundamental_matrix(&p.view()).expect("Must succeed for ergodic chain");
/// assert_eq!(z.nrows(), 2);
/// ```
pub fn fundamental_matrix<F>(
    transition_matrix: &ArrayView2<F>,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static + std::fmt::Display,
{
    validate_squarematrix(transition_matrix, "Fundamental matrix")?;

    let n = transition_matrix.nrows();

    // First compute stationary distribution
    let pi = stationary_distribution(transition_matrix, F::from(1e-12).unwrap_or(F::epsilon()), 50_000)?;

    // Build A = I - P + Π where Π[i,j] = pi[j]
    let mut a = Array2::zeros((n, n));
    for i in 0..n {
        for j in 0..n {
            let delta_ij = if i == j { F::one() } else { F::zero() };
            a[[i, j]] = delta_ij - transition_matrix[[i, j]] + pi[j];
        }
    }

    // Solve A · Z = I  ⟺  Z = A⁻¹
    // Build identity right-hand side
    let identity = Array2::eye(n);

    let z = solve_multiple(&a.view(), &identity.view(), None)?;

    Ok(z)
}

// ─────────────────────────────────────────────────────────────────────────────
// Mean first passage time
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the mean first passage time (MFPT) matrix from the fundamental
/// matrix.
///
/// M[i,j] = expected number of steps to reach state j for the first time
/// starting from state i. For i = j this is the mean recurrence time 1/πⱼ.
///
/// Formula: M[i,j] = (Z[j,j] − Z[i,j]) / π[j].
///
/// # Arguments
///
/// * `transition_matrix` – Row-stochastic square transition matrix P.
///
/// # Returns
///
/// The n×n mean first passage time matrix M.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions::mean_first_passage_time;
///
/// let p = array![[0.7_f64, 0.3], [0.4, 0.6]];
/// let mfpt = mean_first_passage_time(&p.view())
///     .expect("Must succeed for ergodic chain");
/// assert_eq!(mfpt.nrows(), 2);
/// // Mean recurrence time for state 0: 1/pi[0] = 7/4 ≈ 1.75
/// assert!((mfpt[[0, 0]] - 7.0/4.0).abs() < 1e-6);
/// ```
pub fn mean_first_passage_time<F>(
    transition_matrix: &ArrayView2<F>,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static + std::fmt::Display,
{
    validate_squarematrix(transition_matrix, "Mean first passage time")?;

    let n = transition_matrix.nrows();

    let pi = stationary_distribution(transition_matrix, F::from(1e-12).unwrap_or(F::epsilon()), 50_000)?;
    let z = fundamental_matrix(transition_matrix)?;

    let mut m = Array2::zeros((n, n));
    for i in 0..n {
        for j in 0..n {
            if pi[j] <= F::epsilon() {
                m[[i, j]] = F::infinity();
            } else {
                m[[i, j]] = (z[[j, j]] - z[[i, j]]) / pi[j];
            }
        }
    }

    // Diagonal: mean recurrence time = 1/π[j]
    for j in 0..n {
        if pi[j] > F::epsilon() {
            m[[j, j]] = F::one() / pi[j];
        } else {
            m[[j, j]] = F::infinity();
        }
    }

    Ok(m)
}

// ─────────────────────────────────────────────────────────────────────────────
// Mixing time
// ─────────────────────────────────────────────────────────────────────────────

/// Estimate the ε-mixing time of an ergodic Markov chain from its spectral gap.
///
/// The mixing time is estimated using the bound:
/// τ(ε) ≈ ⌈ln(1/ε) / (1 − |λ₂|)⌉
///
/// where λ₂ is the second-largest eigenvalue modulus. A larger spectral gap
/// (1 − |λ₂|) means faster mixing.
///
/// # Arguments
///
/// * `transition_matrix` – Row-stochastic square transition matrix P.
/// * `epsilon` – Target total variation distance from stationarity.
///
/// # Returns
///
/// Estimated number of steps to achieve ε-close convergence to stationary
/// distribution.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions::mixing_time;
///
/// let p = array![[0.7_f64, 0.3], [0.4, 0.6]];
/// let t_mix = mixing_time(&p.view(), 0.01).expect("Must succeed");
/// assert!(t_mix >= 1);
/// ```
pub fn mixing_time<F>(
    transition_matrix: &ArrayView2<F>,
    epsilon: F,
) -> LinalgResult<usize>
where
    F: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static + std::fmt::Display,
{
    validate_squarematrix(transition_matrix, "Mixing time")?;

    if epsilon <= F::zero() || epsilon >= F::one() {
        return Err(LinalgError::InvalidInputError(
            "epsilon must be in (0, 1)".to_string(),
        ));
    }

    let n = transition_matrix.nrows();

    // Compute eigenvalues of P to find the spectral gap.
    // We use power iteration on P^T to find the dominant eigenvalue (= 1),
    // then deflate and find the sub-dominant one.
    //
    // For ergodic chains the eigenvalue 1 is simple; all others have |λ| < 1.

    // Build matrix P^T P (for use in eigenvalue iteration)
    // Instead use direct power iteration on (P - π 1^T) to find λ₂.

    let pi_res = stationary_distribution(transition_matrix, F::from(1e-12).unwrap_or(F::epsilon()), 50_000);
    let pi = pi_res.unwrap_or_else(|_| {
        Array1::from_elem(n, F::one() / F::from(n).unwrap_or(F::one()))
    });

    // Build P_centered = P - Π  (Π[i,j] = π[j])
    // λ₂ of P_centered = λ₂ of P (shifted to remove eigenvalue 1 component)
    // Actually we estimate |λ₂| via power iteration on (P − 1·π^T).

    // Compute ||P^k v - π||₂ → 0 at rate |λ₂|^k
    // Use inverse iteration proxy: iterate v_{k+1} = P^T v_k / ‖ ‖ and track
    // convergence of Rayleigh quotient.

    // Simple approach: compute the spectral gap using the second-largest
    // singular value of (P - Π).
    let mut p_centered = Array2::zeros((n, n));
    for i in 0..n {
        for j in 0..n {
            p_centered[[i, j]] = transition_matrix[[i, j]] - pi[j];
        }
    }

    // Power iteration on p_centered^T to find |λ₂|
    use crate::decomposition::svd;
    let (_, s, _) = svd(&p_centered.view(), false, None)?;

    let lambda2 = if s.len() > 1 { s[0] } else { F::zero() };

    // spectral gap
    let gap = F::one() - lambda2;

    if gap <= F::epsilon() {
        return Err(LinalgError::ComputationError(
            "Spectral gap is zero or negative; chain may not be ergodic".to_string(),
        ));
    }

    // τ(ε) ≈ ceil(ln(1/ε) / gap)
    let ln_inv_eps = (-epsilon.ln()).max(F::zero());
    let t_f = ln_inv_eps / gap;

    let t = t_f.ceil();
    let t_usize = t.to_usize().unwrap_or(usize::MAX).max(1);

    Ok(t_usize)
}

// ─────────────────────────────────────────────────────────────────────────────
// Absorbing chain analysis
// ─────────────────────────────────────────────────────────────────────────────

/// Describes the canonical partition of an absorbing Markov chain.
#[derive(Debug, Clone)]
pub struct AbsorbingMarkovChain {
    /// Indices of absorbing states (states where P[i,i] = 1).
    pub absorbing_states: Vec<usize>,
    /// Indices of transient states (all non-absorbing states).
    pub transient_states: Vec<usize>,
}

/// Identify absorbing and transient states in a Markov chain.
///
/// A state i is *absorbing* if P[i,i] = 1 (equivalently P[i,j] = 0 for j ≠ i).
/// All other states are *transient*.
///
/// # Arguments
///
/// * `transition_matrix` – Row-stochastic square transition matrix P.
///
/// # Returns
///
/// An [`AbsorbingMarkovChain`] struct listing absorbing and transient states.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions::analyze_absorbing_chain;
///
/// // State 0 and 2 are absorbing; state 1 is transient
/// let p = array![
///     [1.0_f64, 0.0, 0.0],
///     [0.3,     0.4, 0.3],
///     [0.0,     0.0, 1.0],
/// ];
/// let chain = analyze_absorbing_chain(&p.view()).expect("Must succeed");
/// assert_eq!(chain.absorbing_states, vec![0, 2]);
/// assert_eq!(chain.transient_states, vec![1]);
/// ```
pub fn analyze_absorbing_chain<F>(
    transition_matrix: &ArrayView2<F>,
) -> LinalgResult<AbsorbingMarkovChain>
where
    F: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static + std::fmt::Display,
{
    validate_squarematrix(transition_matrix, "Absorbing chain analysis")?;

    let n = transition_matrix.nrows();

    // Validate stochasticity
    let tol_stoch = F::from(1e-6).unwrap_or(F::epsilon());
    for i in 0..n {
        let row_sum: F = (0..n).map(|j| transition_matrix[[i, j]]).fold(F::zero(), |a, b| a + b);
        if (row_sum - F::one()).abs() > tol_stoch {
            return Err(LinalgError::InvalidInputError(format!(
                "Row {i} of transition matrix does not sum to 1 (sum = {row_sum})"
            )));
        }
    }

    let tol = F::from(1e-10).unwrap_or(F::epsilon());

    let mut absorbing = Vec::new();
    let mut transient = Vec::new();

    for i in 0..n {
        // Check if this is an absorbing state: P[i,i] ≈ 1
        if (transition_matrix[[i, i]] - F::one()).abs() <= tol {
            // Verify all off-diagonal entries are zero
            let off_diag_sum: F = (0..n)
                .filter(|&j| j != i)
                .map(|j| transition_matrix[[i, j]].abs())
                .fold(F::zero(), |a, b| a + b);
            if off_diag_sum <= tol {
                absorbing.push(i);
                continue;
            }
        }
        transient.push(i);
    }

    Ok(AbsorbingMarkovChain {
        absorbing_states: absorbing,
        transient_states: transient,
    })
}

/// Compute absorption probabilities for an absorbing Markov chain.
///
/// Returns the matrix B where B[i,j] is the probability that a chain starting
/// in transient state `transient_states[i]` is eventually absorbed by
/// absorbing state `absorbing_states[j]`.
///
/// Formula: B = (I − Q)⁻¹ R  where Q is the transient-to-transient sub-matrix
/// and R is the transient-to-absorbing sub-matrix.
///
/// # Arguments
///
/// * `transition_matrix` – Row-stochastic square transition matrix P.
/// * `absorbing_chain` – Pre-computed [`AbsorbingMarkovChain`] (from
///   [`analyze_absorbing_chain`]).
///
/// # Returns
///
/// Matrix B of shape `(num_transient × num_absorbing)`.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::matrix_functions::{analyze_absorbing_chain, absorption_probabilities};
///
/// let p = array![
///     [1.0_f64, 0.0, 0.0],
///     [0.3,     0.4, 0.3],
///     [0.0,     0.0, 1.0],
/// ];
/// let chain = analyze_absorbing_chain(&p.view()).expect("Must succeed");
/// let b = absorption_probabilities(&p.view(), &chain).expect("Must succeed");
/// // State 1 is absorbed by 0 with prob 0.5 and by 2 with prob 0.5
/// assert!((b[[0, 0]] - 0.5).abs() < 1e-8);
/// assert!((b[[0, 1]] - 0.5).abs() < 1e-8);
/// ```
pub fn absorption_probabilities<F>(
    transition_matrix: &ArrayView2<F>,
    absorbing_chain: &AbsorbingMarkovChain,
) -> LinalgResult<Array2<F>>
where
    F: Float + NumAssign + Sum + ScalarOperand + Send + Sync + 'static + std::fmt::Display,
{
    validate_squarematrix(transition_matrix, "Absorption probabilities")?;

    let t_states = &absorbing_chain.transient_states;
    let a_states = &absorbing_chain.absorbing_states;

    if t_states.is_empty() {
        return Err(LinalgError::InvalidInputError(
            "No transient states found in the chain".to_string(),
        ));
    }
    if a_states.is_empty() {
        return Err(LinalgError::InvalidInputError(
            "No absorbing states found in the chain".to_string(),
        ));
    }

    let nt = t_states.len();
    let na = a_states.len();

    // Q: transient → transient sub-matrix
    let mut q = Array2::zeros((nt, nt));
    for (i, &ti) in t_states.iter().enumerate() {
        for (j, &tj) in t_states.iter().enumerate() {
            q[[i, j]] = transition_matrix[[ti, tj]];
        }
    }

    // R: transient → absorbing sub-matrix
    let mut r = Array2::zeros((nt, na));
    for (i, &ti) in t_states.iter().enumerate() {
        for (j, &aj) in a_states.iter().enumerate() {
            r[[i, j]] = transition_matrix[[ti, aj]];
        }
    }

    // Build (I − Q)
    let mut i_minus_q: Array2<F> = Array2::eye(nt);
    for i in 0..nt {
        for j in 0..nt {
            i_minus_q[[i, j]] -= q[[i, j]];
        }
    }

    // B = (I − Q)⁻¹ · R
    let b = solve_multiple(&i_minus_q.view(), &r.view(), None)?;

    Ok(b)
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    // ── helpers ──

    fn two_state_chain() -> Array2<f64> {
        // P = [[0.7, 0.3], [0.4, 0.6]]
        // Stationary: π = [4/7, 3/7]
        array![[0.7_f64, 0.3], [0.4, 0.6]]
    }

    fn three_state_ergodic() -> Array2<f64> {
        array![
            [0.5_f64, 0.3, 0.2],
            [0.2, 0.6, 0.2],
            [0.3, 0.2, 0.5]
        ]
    }

    fn absorbing_chain_3state() -> Array2<f64> {
        // States 0 and 2 are absorbing; state 1 is transient
        array![
            [1.0_f64, 0.0, 0.0],
            [0.3, 0.4, 0.3],
            [0.0, 0.0, 1.0],
        ]
    }

    // ════════════════════════════════════════════════════
    // is_stochastic
    // ════════════════════════════════════════════════════

    #[test]
    fn test_is_stochastic_valid() {
        let p = two_state_chain();
        assert!(is_stochastic(&p.view(), 1e-10));
    }

    #[test]
    fn test_is_stochastic_invalid_row_sum() {
        let p = array![[0.7_f64, 0.4], [0.4, 0.6]]; // row 0 sums to 1.1
        assert!(!is_stochastic(&p.view(), 1e-10));
    }

    #[test]
    fn test_is_stochastic_negative_entry() {
        let p = array![[0.7_f64, 0.3], [-0.1, 1.1]];
        assert!(!is_stochastic(&p.view(), 1e-10));
    }

    #[test]
    fn test_is_stochastic_non_square() {
        let p = array![[0.5_f64, 0.5, 0.0], [0.3, 0.4, 0.3]];
        assert!(!is_stochastic(&p.view(), 1e-10));
    }

    // ════════════════════════════════════════════════════
    // stationary_distribution
    // ════════════════════════════════════════════════════

    #[test]
    fn test_stationary_distribution_two_state() {
        let p = two_state_chain();
        let pi = stationary_distribution(&p.view(), 1e-12, 10_000)
            .expect("Must converge");
        assert_relative_eq!(pi[0], 4.0 / 7.0, epsilon = 1e-8);
        assert_relative_eq!(pi[1], 3.0 / 7.0, epsilon = 1e-8);
    }

    #[test]
    fn test_stationary_distribution_three_state() {
        let p = three_state_ergodic();
        let pi = stationary_distribution(&p.view(), 1e-12, 10_000)
            .expect("Must converge");
        // Sum must be 1
        let sum: f64 = pi.iter().sum();
        assert_relative_eq!(sum, 1.0, epsilon = 1e-10);
        // All entries must be positive
        for &v in pi.iter() {
            assert!(v > 0.0);
        }
        // Must satisfy π = π P
        let pi_p: Array1<f64> = {
            let n = 3;
            let mut r = Array1::zeros(n);
            for j in 0..n {
                r[j] = (0..n).map(|i| pi[i] * p[[i, j]]).sum::<f64>();
            }
            r
        };
        for i in 0..3 {
            assert_relative_eq!(pi[i], pi_p[i], epsilon = 1e-8);
        }
    }

    #[test]
    fn test_stationary_distribution_uniform() {
        // Doubly stochastic → uniform stationary distribution
        let p = array![[0.5_f64, 0.5], [0.5, 0.5]];
        let pi = stationary_distribution(&p.view(), 1e-12, 10_000)
            .expect("Must converge");
        assert_relative_eq!(pi[0], 0.5, epsilon = 1e-8);
        assert_relative_eq!(pi[1], 0.5, epsilon = 1e-8);
    }

    // ════════════════════════════════════════════════════
    // fundamental_matrix
    // ════════════════════════════════════════════════════

    #[test]
    fn test_fundamental_matrix_two_state() {
        let p = two_state_chain();
        let z = fundamental_matrix(&p.view()).expect("Must succeed");
        assert_eq!(z.nrows(), 2);
        assert_eq!(z.ncols(), 2);
    }

    #[test]
    fn test_fundamental_matrix_identity_row() {
        // Z · (I - P + Π) = I (by construction)
        let p = two_state_chain();
        let z = fundamental_matrix(&p.view()).expect("Must succeed");
        let pi = stationary_distribution(&p.view(), 1e-12, 10_000).expect("failed to create pi");
        let n = 2;
        // Build A = I - P + Π
        let mut a = Array2::<f64>::zeros((n, n));
        for i in 0..n {
            for j in 0..n {
                a[[i, j]] = (if i == j { 1.0 } else { 0.0 }) - p[[i, j]] + pi[j];
            }
        }
        // Z · A should be ≈ I
        let mut za = Array2::<f64>::zeros((n, n));
        for i in 0..n {
            for j in 0..n {
                for k in 0..n {
                    za[[i, j]] += z[[i, k]] * a[[k, j]];
                }
            }
        }
        for i in 0..n {
            for j in 0..n {
                let expected = if i == j { 1.0 } else { 0.0 };
                assert_relative_eq!(za[[i, j]], expected, epsilon = 1e-8);
            }
        }
    }

    // ════════════════════════════════════════════════════
    // mean_first_passage_time
    // ════════════════════════════════════════════════════

    #[test]
    fn test_mfpt_diagonal_is_recurrence_time() {
        let p = two_state_chain();
        let mfpt = mean_first_passage_time(&p.view()).expect("Must succeed");
        let pi = stationary_distribution(&p.view(), 1e-12, 10_000).expect("failed to create pi");
        // Mean recurrence time M[i,i] = 1/π[i]
        assert_relative_eq!(mfpt[[0, 0]], 1.0 / pi[0], epsilon = 1e-6);
        assert_relative_eq!(mfpt[[1, 1]], 1.0 / pi[1], epsilon = 1e-6);
    }

    #[test]
    fn test_mfpt_positive_entries() {
        let p = two_state_chain();
        let mfpt = mean_first_passage_time(&p.view()).expect("Must succeed");
        for i in 0..2 {
            for j in 0..2 {
                assert!(mfpt[[i, j]] > 0.0, "MFPT entry [{i},{j}] must be positive");
            }
        }
    }

    // ════════════════════════════════════════════════════
    // mixing_time
    // ════════════════════════════════════════════════════

    #[test]
    fn test_mixing_time_positive() {
        let p = two_state_chain();
        let t = mixing_time(&p.view(), 0.01).expect("Must succeed");
        assert!(t >= 1);
    }

    #[test]
    fn test_mixing_time_faster_for_large_gap() {
        // Identity permutation matrix has spectral gap = 0 → skip
        // Instead: compare two chains with different gaps
        // Fast chain: nearly uniform
        let p_fast = array![[0.5_f64, 0.5], [0.5, 0.5]];
        // Slow chain: highly persistent
        let p_slow = array![[0.99_f64, 0.01], [0.01, 0.99]];
        let t_fast = mixing_time(&p_fast.view(), 0.05).expect("Must succeed");
        let t_slow = mixing_time(&p_slow.view(), 0.05).expect("Must succeed");
        assert!(t_slow > t_fast, "Slower chain should need more steps: {t_slow} vs {t_fast}");
    }

    #[test]
    fn test_mixing_time_invalid_epsilon() {
        let p = two_state_chain();
        assert!(mixing_time(&p.view(), 0.0).is_err());
        assert!(mixing_time(&p.view(), 1.0).is_err());
        assert!(mixing_time(&p.view(), -0.1).is_err());
    }

    // ════════════════════════════════════════════════════
    // analyze_absorbing_chain
    // ════════════════════════════════════════════════════

    #[test]
    fn test_analyze_absorbing_chain_basic() {
        let p = absorbing_chain_3state();
        let chain = analyze_absorbing_chain(&p.view()).expect("Must succeed");
        assert_eq!(chain.absorbing_states, vec![0, 2]);
        assert_eq!(chain.transient_states, vec![1]);
    }

    #[test]
    fn test_analyze_absorbing_chain_no_absorbing() {
        let p = two_state_chain();
        let chain = analyze_absorbing_chain(&p.view()).expect("Must succeed");
        assert!(chain.absorbing_states.is_empty());
        assert_eq!(chain.transient_states.len(), 2);
    }

    #[test]
    fn test_analyze_absorbing_chain_all_absorbing() {
        // 3×3 identity: all absorbing
        let p: Array2<f64> = Array2::eye(3);
        let chain = analyze_absorbing_chain(&p.view()).expect("Must succeed");
        assert_eq!(chain.absorbing_states.len(), 3);
        assert!(chain.transient_states.is_empty());
    }

    // ════════════════════════════════════════════════════
    // absorption_probabilities
    // ════════════════════════════════════════════════════

    #[test]
    fn test_absorption_probs_symmetric() {
        // Due to symmetry, transient state 1 should be absorbed equally
        let p = absorbing_chain_3state();
        let chain = analyze_absorbing_chain(&p.view()).expect("Must succeed");
        let b = absorption_probabilities(&p.view(), &chain).expect("Must succeed");
        // b[0, 0] = P(absorbed by state 0 | start at state 1)
        // By symmetry (R[0]=R[1]=0.3): each absorbing state gets 0.5
        assert_relative_eq!(b[[0, 0]], 0.5, epsilon = 1e-8);
        assert_relative_eq!(b[[0, 1]], 0.5, epsilon = 1e-8);
    }

    #[test]
    fn test_absorption_probs_row_sum_to_one() {
        // Each row of B must sum to 1 (eventual certain absorption)
        let p = absorbing_chain_3state();
        let chain = analyze_absorbing_chain(&p.view()).expect("Must succeed");
        let b = absorption_probabilities(&p.view(), &chain).expect("Must succeed");
        for i in 0..b.nrows() {
            let row_sum: f64 = b.row(i).iter().sum();
            assert_relative_eq!(row_sum, 1.0, epsilon = 1e-8);
        }
    }

    #[test]
    fn test_absorption_probs_shape() {
        let p = absorbing_chain_3state();
        let chain = analyze_absorbing_chain(&p.view()).expect("Must succeed");
        let b = absorption_probabilities(&p.view(), &chain).expect("Must succeed");
        // 1 transient × 2 absorbing
        assert_eq!(b.nrows(), chain.transient_states.len());
        assert_eq!(b.ncols(), chain.absorbing_states.len());
    }

    #[test]
    fn test_absorption_probs_asymmetric() {
        // State 1 transitions: to 0 with 0.7, to 2 with 0.3 (no self-loop)
        let p = array![
            [1.0_f64, 0.0, 0.0],
            [0.7, 0.0, 0.3],
            [0.0, 0.0, 1.0],
        ];
        let chain = analyze_absorbing_chain(&p.view()).expect("Must succeed");
        let b = absorption_probabilities(&p.view(), &chain).expect("Must succeed");
        assert_relative_eq!(b[[0, 0]], 0.7, epsilon = 1e-8);
        assert_relative_eq!(b[[0, 1]], 0.3, epsilon = 1e-8);
    }
}