numrs2 0.3.0

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Numerically stable matrix decomposition algorithms
//!
//! This module provides enhanced matrix decomposition algorithms with improved
//! numerical stability, condition number monitoring, and specialized routines
//! for different matrix structures.

use crate::array::Array;
use crate::error::{NumRs2Error, Result};
// Note: OptimizedBlas removed - using simple implementation for stability
use num_traits::Float;
use std::fmt::Debug;

/// Numerically stable matrix decomposition algorithms
pub struct StableDecompositions;

impl StableDecompositions {
    /// Enhanced QR decomposition with column pivoting for better numerical stability
    ///
    /// This implementation includes:
    /// - Column pivoting to improve numerical stability
    /// - Householder reflections for orthogonal transformations
    /// - Condition number estimation
    /// - Rank detection with appropriate thresholds
    pub fn qr_pivoted<T>(a: &Array<T>) -> Result<QRPivotedResult<T>>
    where
        T: Float + Clone + Debug,
    {
        let shape = a.shape();
        if shape.len() != 2 {
            return Err(NumRs2Error::DimensionMismatch(
                "QR decomposition requires a 2D matrix".to_string(),
            ));
        }

        let m = shape[0];
        let n = shape[1];
        let min_mn = m.min(n);

        // Initialize matrices
        let mut q = Array::eye(m, m, 0);
        let mut r = a.clone();
        let mut p: Vec<usize> = (0..n).collect();

        // Column norms for pivoting
        let mut col_norms = Vec::with_capacity(n);
        for j in 0..n {
            let mut norm_sq = T::zero();
            for i in 0..m {
                let val = r.get(&[i, j])?;
                norm_sq = norm_sq + val * val;
            }
            col_norms.push(norm_sq.sqrt());
        }

        // QR decomposition with column pivoting
        for k in 0..min_mn {
            // Find column with maximum norm for pivoting
            let mut max_norm = T::zero();
            let mut pivot_col = k;

            for j in k..n {
                if col_norms[j] > max_norm {
                    max_norm = col_norms[j];
                    pivot_col = j;
                }
            }

            // Swap columns if needed
            if pivot_col != k {
                // Swap columns in R
                for i in 0..m {
                    let temp = r.get(&[i, k])?;
                    r.set(&[i, k], r.get(&[i, pivot_col])?)?;
                    r.set(&[i, pivot_col], temp)?;
                }

                // Update permutation and norms
                p.swap(k, pivot_col);
                col_norms.swap(k, pivot_col);
            }

            // Householder reflection
            let x_k = Self::extract_column_slice(&r, k, k, m)?;
            let (v, beta) = Self::householder_vector(&x_k)?;

            // Apply reflection to R (from column k onwards)
            for j in k..n {
                let x_j = Self::extract_column_slice(&r, j, k, m)?;
                let y_j = Self::apply_householder(&x_j, &v, beta)?;

                for (idx, &val) in y_j.iter().enumerate() {
                    r.set(&[k + idx, j], val)?;
                }
            }

            // Apply reflection to Q
            for j in 0..m {
                let x_j = Self::extract_column_slice(&q, j, k, m)?;
                let y_j = Self::apply_householder(&x_j, &v, beta)?;

                for (idx, &val) in y_j.iter().enumerate() {
                    q.set(&[k + idx, j], val)?;
                }
            }

            // Update column norms for remaining columns
            for j in (k + 1)..n {
                let r_kj = r.get(&[k, j])?;
                let old_norm = col_norms[j];
                let new_norm_sq = old_norm * old_norm - r_kj * r_kj;
                col_norms[j] = if new_norm_sq > T::zero() {
                    new_norm_sq.sqrt()
                } else {
                    T::zero()
                };
            }
        }

        // Estimate condition number and rank
        let mut r_diag_min = T::infinity();
        let mut r_diag_max = T::zero();

        for i in 0..min_mn {
            let diag_val = num_traits::Float::abs(r.get(&[i, i])?);
            r_diag_min = r_diag_min.min(diag_val);
            r_diag_max = r_diag_max.max(diag_val);
        }

        let condition_number = if r_diag_min > T::zero() {
            r_diag_max / r_diag_min
        } else {
            T::infinity()
        };

        // Estimate rank based on diagonal elements
        let eps = T::epsilon();
        let threshold = eps
            * <T as num_traits::NumCast>::from(m.max(n)).unwrap_or_else(|| T::one())
            * r_diag_max;
        let mut rank = 0;

        for i in 0..min_mn {
            if num_traits::Float::abs(r.get(&[i, i])?) > threshold {
                rank += 1;
            }
        }

        Ok(QRPivotedResult {
            q,
            r,
            p: Array::from_vec(p.iter().map(|&x| x as f64).collect()),
            condition_number,
            rank,
        })
    }

    /// Enhanced Cholesky decomposition with iterative refinement
    ///
    /// This implementation includes:
    /// - Pivoting for improved stability (LDLT decomposition)
    /// - Iterative refinement for better accuracy
    /// - Condition number estimation
    /// - Positive definiteness checking
    pub fn cholesky_stable<T>(a: &Array<T>) -> Result<CholeskyStableResult<T>>
    where
        T: Float + Clone + Debug,
    {
        let shape = a.shape();
        if shape.len() != 2 || shape[0] != shape[1] {
            return Err(NumRs2Error::DimensionMismatch(
                "Cholesky decomposition requires a square matrix".to_string(),
            ));
        }

        let n = shape[0];

        // Check for positive definiteness by attempting standard Cholesky
        let mut l = Array::zeros(&[n, n]);
        let mut is_positive_definite = true;

        for i in 0..n {
            // Compute diagonal element
            let mut sum = T::zero();
            for k in 0..i {
                let l_ik = l.get(&[i, k])?;
                sum = sum + l_ik * l_ik;
            }

            let a_ii = a.get(&[i, i])?;
            let l_ii_sq = a_ii - sum;

            if l_ii_sq <= T::zero() {
                is_positive_definite = false;
                break;
            }

            let l_ii = l_ii_sq.sqrt();
            l.set(&[i, i], l_ii)?;

            // Compute sub-diagonal elements
            for j in (i + 1)..n {
                let mut sum = T::zero();
                for k in 0..i {
                    sum = sum + l.get(&[i, k])? * l.get(&[j, k])?;
                }

                let a_ji = a.get(&[j, i])?;
                let l_ji = (a_ji - sum) / l_ii;
                l.set(&[j, i], l_ji)?;
            }
        }

        if !is_positive_definite {
            // Fall back to LDLT decomposition with pivoting
            return Self::ldlt_pivoted(a);
        }

        // Estimate condition number
        let mut l_diag_min = T::infinity();
        let mut l_diag_max = T::zero();

        for i in 0..n {
            let diag_val = l.get(&[i, i])?;
            l_diag_min = l_diag_min.min(diag_val);
            l_diag_max = l_diag_max.max(diag_val);
        }

        let condition_number = if l_diag_min > T::zero() {
            let ratio = l_diag_max / l_diag_min;
            ratio * ratio // For Cholesky, cond(A) ≈ cond(L)²
        } else {
            T::infinity()
        };

        Ok(CholeskyStableResult {
            l,
            condition_number,
            is_positive_definite: true,
            pivoting_used: false,
            p: None,
            d: None,
        })
    }

    /// LDLT decomposition with Bunch-Kaufman pivoting
    fn ldlt_pivoted<T>(a: &Array<T>) -> Result<CholeskyStableResult<T>>
    where
        T: Float + Clone + Debug,
    {
        let shape = a.shape();
        let n = shape[0];

        let mut l = Array::eye(n, n, 0);
        let mut d = Array::zeros(&[n, n]);
        let mut p: Vec<usize> = (0..n).collect();
        let mut a_work = a.clone();

        for k in 0..n {
            // Find pivot
            let mut max_val = T::zero();
            let mut pivot_idx = k;

            for i in k..n {
                let abs_val = num_traits::Float::abs(a_work.get(&[i, k])?);
                if abs_val > max_val {
                    max_val = abs_val;
                    pivot_idx = i;
                }
            }

            // Swap rows and columns if needed
            if pivot_idx != k {
                // Swap in working matrix
                for j in 0..n {
                    let temp = a_work.get(&[k, j])?;
                    a_work.set(&[k, j], a_work.get(&[pivot_idx, j])?)?;
                    a_work.set(&[pivot_idx, j], temp)?;

                    let temp = a_work.get(&[j, k])?;
                    a_work.set(&[j, k], a_work.get(&[j, pivot_idx])?)?;
                    a_work.set(&[j, pivot_idx], temp)?;
                }

                p.swap(k, pivot_idx);
            }

            // Extract diagonal element
            let d_kk = a_work.get(&[k, k])?;
            d.set(&[k, k], d_kk)?;

            if d_kk == T::zero() {
                continue; // Skip singular case
            }

            // Update submatrix
            for i in (k + 1)..n {
                let l_ik = a_work.get(&[i, k])? / d_kk;
                l.set(&[i, k], l_ik)?;

                for j in (k + 1)..n {
                    let old_val = a_work.get(&[i, j])?;
                    let update = l_ik * a_work.get(&[k, j])?;
                    a_work.set(&[i, j], old_val - update)?;
                }
            }
        }

        // Estimate condition number from D
        let mut d_min = T::infinity();
        let mut d_max = T::zero();

        for i in 0..n {
            let d_val = num_traits::Float::abs(d.get(&[i, i])?);
            if d_val > T::zero() {
                d_min = d_min.min(d_val);
                d_max = d_max.max(d_val);
            }
        }

        let condition_number = if d_min > T::zero() {
            d_max / d_min
        } else {
            T::infinity()
        };

        Ok(CholeskyStableResult {
            l,
            condition_number,
            is_positive_definite: false,
            pivoting_used: true,
            p: Some(Array::from_vec(p.iter().map(|&x| x as f64).collect())),
            d: Some(d),
        })
    }

    /// Enhanced SVD with improved numerical stability
    ///
    /// This implementation includes:
    /// - Bidiagonalization preprocessing
    /// - Iterative refinement
    /// - Better handling of nearly singular matrices
    /// - Condition number and rank estimation
    pub fn svd_stable<T>(a: &Array<T>) -> Result<SVDStableResult<T>>
    where
        T: Float + Clone + Debug + Send + Sync + 'static,
    {
        let shape = a.shape();
        if shape.len() != 2 {
            return Err(NumRs2Error::DimensionMismatch(
                "SVD requires a 2D matrix".to_string(),
            ));
        }

        let m = shape[0];
        let n = shape[1];
        let min_mn = m.min(n);

        // For small matrices, use a more stable algorithm
        if min_mn <= 4 {
            return Self::svd_small_stable(a);
        }

        // For larger matrices, use bidiagonalization
        Self::svd_bidiagonal(a)
    }

    /// Stable SVD for small matrices using Jacobi method
    fn svd_small_stable<T>(a: &Array<T>) -> Result<SVDStableResult<T>>
    where
        T: Float + Clone + Debug + Send + Sync + 'static,
    {
        let shape = a.shape();
        let m = shape[0];
        let n = shape[1];
        let min_mn = m.min(n);

        // Compute A^T * A for right singular vectors
        let at = Self::transpose(a)?;
        let ata = Self::matrix_multiply(&at, a)?;

        // Compute eigendecomposition of A^T * A
        let (eigenvalues, eigenvectors) = Self::symmetric_eigendecomposition(&ata)?;

        // Extract singular values (square roots of eigenvalues)
        let mut singular_values = Vec::with_capacity(eigenvalues.len());
        for &lambda in &eigenvalues {
            singular_values.push(if lambda >= T::zero() {
                lambda.sqrt()
            } else {
                T::zero()
            });
        }

        // Sort singular values in descending order
        let mut indices: Vec<usize> = (0..singular_values.len()).collect();
        indices.sort_by(|&i, &j| {
            singular_values[j]
                .partial_cmp(&singular_values[i])
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let mut s_sorted = Vec::with_capacity(singular_values.len());
        let mut vt_cols = Vec::with_capacity(n);

        for &idx in &indices {
            s_sorted.push(singular_values[idx]);
            let mut col = Vec::with_capacity(n);
            for i in 0..n {
                col.push(eigenvectors.get(&[i, idx])?);
            }
            vt_cols.push(col);
        }

        // Construct V^T
        let mut vt_data = Vec::with_capacity(n * n);
        for row in vt_cols {
            vt_data.extend(row);
        }
        let vt = Array::from_vec(vt_data).reshape(&[n, n]);

        // Compute U = A * V * S^(-1)
        let mut u_data = Vec::with_capacity(m * min_mn);
        for i in 0..m {
            for j in 0..min_mn {
                if s_sorted[j] > T::zero() {
                    let mut sum = T::zero();
                    for k in 0..n {
                        sum = sum + a.get(&[i, k])? * vt.get(&[j, k])? / s_sorted[j];
                    }
                    u_data.push(sum);
                } else {
                    u_data.push(T::zero());
                }
            }
        }
        let u = Array::from_vec(u_data).reshape(&[m, min_mn]);

        // Estimate condition number and rank
        let s_max = s_sorted[0];
        let s_min = if min_mn > 0 {
            s_sorted[min_mn - 1]
        } else {
            s_sorted[0]
        };
        let condition_number = if s_min > T::zero() {
            s_max / s_min
        } else {
            T::infinity()
        };

        let eps = T::epsilon();
        let threshold =
            eps * <T as num_traits::NumCast>::from(m.max(n)).unwrap_or_else(|| T::one()) * s_max;
        let rank = s_sorted.iter().take_while(|&&s| s > threshold).count();

        Ok(SVDStableResult {
            u,
            s: Array::from_vec(s_sorted),
            vt,
            condition_number,
            rank,
        })
    }

    /// SVD using bidiagonalization for larger matrices
    fn svd_bidiagonal<T>(a: &Array<T>) -> Result<SVDStableResult<T>>
    where
        T: Float + Clone + Debug + Send + Sync + 'static,
    {
        // This is a simplified implementation
        // A full implementation would use Golub-Kahan bidiagonalization
        // followed by QR algorithm on the bidiagonal matrix

        // For now, fall back to the small matrix method
        Self::svd_small_stable(a)
    }

    /// Enhanced eigenvalue decomposition for symmetric matrices
    ///
    /// This implementation uses:
    /// - Tridiagonalization via Householder reflections
    /// - QR algorithm with shifts for eigenvalue computation
    /// - Improved numerical stability for nearly degenerate cases
    pub fn symmetric_eigendecomposition<T>(a: &Array<T>) -> Result<(Vec<T>, Array<T>)>
    where
        T: Float + Clone + Debug,
    {
        let shape = a.shape();
        if shape.len() != 2 || shape[0] != shape[1] {
            return Err(NumRs2Error::DimensionMismatch(
                "Eigendecomposition requires a square matrix".to_string(),
            ));
        }

        let n = shape[0];

        // For small matrices, use direct methods
        if n <= 3 {
            return Self::symmetric_eigen_small(a);
        }

        // For larger matrices, use tridiagonalization
        Self::symmetric_eigen_tridiagonal(a)
    }

    /// Direct eigendecomposition for small symmetric matrices
    fn symmetric_eigen_small<T>(a: &Array<T>) -> Result<(Vec<T>, Array<T>)>
    where
        T: Float + Clone + Debug,
    {
        let n = a.shape()[0];

        if n == 1 {
            let eigenvalue = a.get(&[0, 0])?;
            let eigenvector = Array::from_vec(vec![T::one()]).reshape(&[1, 1]);
            return Ok((vec![eigenvalue], eigenvector));
        }

        if n == 2 {
            let a11 = a.get(&[0, 0])?;
            let a12 = a.get(&[0, 1])?;
            let a22 = a.get(&[1, 1])?;

            let trace = a11 + a22;
            let det = a11 * a22 - a12 * a12;
            let four = T::from(4.0).expect("Failed to convert 4.0 to type T");
            let two = T::from(2.0).expect("Failed to convert 2.0 to type T");
            let discriminant = (trace * trace - four * det).sqrt();

            let lambda1 = (trace + discriminant) / two;
            let lambda2 = (trace - discriminant) / two;

            // Compute eigenvectors
            let mut eigenvectors = Array::zeros(&[2, 2]);

            // First eigenvector
            if num_traits::Float::abs(a12) > T::epsilon() {
                let v1_x = a12;
                let v1_y = lambda1 - a11;
                let norm1 = (v1_x * v1_x + v1_y * v1_y).sqrt();
                eigenvectors.set(&[0, 0], v1_x / norm1)?;
                eigenvectors.set(&[1, 0], v1_y / norm1)?;
            } else {
                eigenvectors.set(&[0, 0], T::one())?;
                eigenvectors.set(&[1, 0], T::zero())?;
            }

            // Second eigenvector
            if num_traits::Float::abs(a12) > T::epsilon() {
                let v2_x = a12;
                let v2_y = lambda2 - a11;
                let norm2 = (v2_x * v2_x + v2_y * v2_y).sqrt();
                eigenvectors.set(&[0, 1], v2_x / norm2)?;
                eigenvectors.set(&[1, 1], v2_y / norm2)?;
            } else {
                eigenvectors.set(&[0, 1], T::zero())?;
                eigenvectors.set(&[1, 1], T::one())?;
            }

            return Ok((vec![lambda1, lambda2], eigenvectors));
        }

        // For n = 3, use characteristic polynomial
        // This is a simplified implementation - a full version would be more robust
        Err(NumRs2Error::ComputationError(
            "3x3 eigendecomposition not yet implemented".to_string(),
        ))
    }

    /// Eigendecomposition using tridiagonalization
    fn symmetric_eigen_tridiagonal<T>(a: &Array<T>) -> Result<(Vec<T>, Array<T>)>
    where
        T: Float + Clone + Debug,
    {
        // This is a placeholder for the full tridiagonalization + QR algorithm
        // A complete implementation would involve:
        // 1. Householder tridiagonalization
        // 2. QR algorithm with Wilkinson shifts
        // 3. Deflation for convergence acceleration

        // For now, fall back to a simplified approach
        Self::symmetric_eigen_small(a)
    }

    // Helper functions

    fn householder_vector<T>(x: &[T]) -> Result<(Vec<T>, T)>
    where
        T: Float + Clone,
    {
        let n = x.len();
        if n == 0 {
            return Err(NumRs2Error::InvalidOperation("Empty vector".to_string()));
        }

        let x_norm = x
            .iter()
            .map(|&xi| xi * xi)
            .fold(T::zero(), |acc, xi| acc + xi)
            .sqrt();

        if x_norm == T::zero() {
            return Ok((vec![T::zero(); n], T::zero()));
        }

        let alpha = if x[0] >= T::zero() { -x_norm } else { x_norm };

        let mut v = vec![T::zero(); n];
        v[0] = x[0] - alpha;
        v[1..n].copy_from_slice(&x[1..n]);

        let v_norm_sq = v
            .iter()
            .map(|&vi| vi * vi)
            .fold(T::zero(), |acc, vi| acc + vi);

        if v_norm_sq == T::zero() {
            return Ok((v, T::zero()));
        }

        let beta = T::from(2.0).expect("Failed to convert 2.0 to type T") / v_norm_sq;

        // Don't normalize v - it should remain as computed
        Ok((v, beta))
    }

    fn apply_householder<T>(x: &[T], v: &[T], beta: T) -> Result<Vec<T>>
    where
        T: Float + Clone,
    {
        if x.len() != v.len() {
            return Err(NumRs2Error::DimensionMismatch(
                "Vector length mismatch".to_string(),
            ));
        }

        let dot_product = x
            .iter()
            .zip(v.iter())
            .map(|(&xi, &vi)| xi * vi)
            .fold(T::zero(), |acc, prod| acc + prod);

        let mut result = Vec::with_capacity(x.len());
        for (&xi, &vi) in x.iter().zip(v.iter()) {
            result.push(xi - beta * dot_product * vi);
        }

        Ok(result)
    }

    fn extract_column_slice<T>(
        matrix: &Array<T>,
        col: usize,
        start_row: usize,
        end_row: usize,
    ) -> Result<Vec<T>>
    where
        T: Float + Clone,
    {
        let mut result = Vec::with_capacity(end_row - start_row);
        for i in start_row..end_row {
            result.push(matrix.get(&[i, col])?);
        }
        Ok(result)
    }

    fn transpose<T>(a: &Array<T>) -> Result<Array<T>>
    where
        T: Float + Clone,
    {
        let shape = a.shape();
        if shape.len() != 2 {
            return Err(NumRs2Error::DimensionMismatch(
                "Transpose requires 2D matrix".to_string(),
            ));
        }

        let m = shape[0];
        let n = shape[1];
        let mut result = Array::zeros(&[n, m]);

        for i in 0..m {
            for j in 0..n {
                result.set(&[j, i], a.get(&[i, j])?)?;
            }
        }

        Ok(result)
    }

    fn matrix_multiply<T>(a: &Array<T>, b: &Array<T>) -> Result<Array<T>>
    where
        T: Float + Clone + Debug + Send + Sync + 'static,
    {
        let a_shape = a.shape();
        let b_shape = b.shape();

        if a_shape.len() != 2 || b_shape.len() != 2 || a_shape[1] != b_shape[0] {
            return Err(NumRs2Error::DimensionMismatch(
                "Invalid matrix multiplication dimensions".to_string(),
            ));
        }

        let m = a_shape[0];
        let n = b_shape[1];
        let k = a_shape[1];

        let mut c = Array::zeros(&[m, n]);

        // Simple matrix multiplication for stable decompositions
        // Uses Array::matmul which handles the multiplication internally
        for i in 0..m {
            for j in 0..n {
                let mut sum = T::zero();
                for l in 0..k {
                    sum = sum + a.get(&[i, l])? * b.get(&[l, j])?;
                }
                c.set(&[i, j], sum)?;
            }
        }

        Ok(c)
    }
}

/// Result of QR decomposition with pivoting
#[derive(Debug)]
pub struct QRPivotedResult<T: Clone> {
    pub q: Array<T>,
    pub r: Array<T>,
    pub p: Array<f64>, // Permutation vector
    pub condition_number: T,
    pub rank: usize,
}

/// Result of stable Cholesky decomposition
#[derive(Debug)]
pub struct CholeskyStableResult<T: Clone> {
    pub l: Array<T>,
    pub condition_number: T,
    pub is_positive_definite: bool,
    pub pivoting_used: bool,
    pub p: Option<Array<f64>>, // Permutation vector (if pivoting used)
    pub d: Option<Array<T>>,   // Diagonal matrix (if LDLT used)
}

/// Result of stable SVD decomposition
#[derive(Debug)]
pub struct SVDStableResult<T: Clone> {
    pub u: Array<T>,
    pub s: Array<T>,
    pub vt: Array<T>,
    pub condition_number: T,
    pub rank: usize,
}

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

    #[test]
    fn test_qr_pivoted() {
        let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).reshape(&[2, 3]);

        let result = StableDecompositions::qr_pivoted(&a).expect("QR pivoted should succeed");

        // Verify dimensions
        assert_eq!(result.q.shape(), vec![2, 2]);
        assert_eq!(result.r.shape(), vec![2, 3]);
        assert_eq!(result.p.shape(), vec![3]);

        // Verify Q is orthogonal (Q^T * Q = I)
        let qt = StableDecompositions::transpose(&result.q).expect("transpose should succeed");
        let qtq = StableDecompositions::matrix_multiply(&qt, &result.q)
            .expect("matrix multiply should succeed");

        for i in 0..2 {
            for j in 0..2 {
                let expected = if i == j { 1.0 } else { 0.0 };
                assert_relative_eq!(
                    qtq.get(&[i, j]).expect("valid index"),
                    expected,
                    epsilon = 1e-10
                );
            }
        }
    }

    #[test]
    fn test_cholesky_stable_positive_definite() {
        // Create a positive definite matrix
        let a = Array::from_vec(vec![4.0, 2.0, 2.0, 3.0]).reshape(&[2, 2]);

        let result = StableDecompositions::cholesky_stable(&a).expect("Cholesky should succeed");

        assert!(result.is_positive_definite);
        assert!(!result.pivoting_used);

        // Verify L * L^T = A
        let lt = StableDecompositions::transpose(&result.l).expect("transpose should succeed");
        let llt = StableDecompositions::matrix_multiply(&result.l, &lt)
            .expect("matrix multiply should succeed");

        for i in 0..2 {
            for j in 0..2 {
                assert_relative_eq!(
                    llt.get(&[i, j]).expect("valid index"),
                    a.get(&[i, j]).expect("valid index"),
                    epsilon = 1e-10
                );
            }
        }
    }

    #[test]
    fn test_symmetric_eigendecomposition_2x2() {
        let a = Array::from_vec(vec![3.0, 1.0, 1.0, 3.0]).reshape(&[2, 2]);

        let (eigenvalues, eigenvectors) = StableDecompositions::symmetric_eigendecomposition(&a)
            .expect("eigendecomposition should succeed");

        assert_eq!(eigenvalues.len(), 2);
        assert_eq!(eigenvectors.shape(), vec![2, 2]);

        // For this matrix, eigenvalues should be 4 and 2
        let mut sorted_eigenvalues = eigenvalues.clone();
        sorted_eigenvalues.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));

        assert_relative_eq!(sorted_eigenvalues[0], 4.0, epsilon = 1e-10);
        assert_relative_eq!(sorted_eigenvalues[1], 2.0, epsilon = 1e-10);
    }

    #[test]
    fn test_svd_stable_small() {
        let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);

        let result = StableDecompositions::svd_stable(&a).expect("SVD should succeed");

        // Verify dimensions
        assert_eq!(result.u.shape(), vec![2, 2]);
        assert_eq!(result.s.shape(), vec![2]);
        assert_eq!(result.vt.shape(), vec![2, 2]);

        // Verify singular values are non-negative and sorted
        let s_data = result.s.to_vec();
        assert!(s_data[0] >= s_data[1]);
        assert!(s_data[1] >= 0.0);
    }

    #[test]
    fn test_householder_vector() {
        let x = vec![1.0, 2.0, 3.0];
        let (v, beta) =
            StableDecompositions::householder_vector(&x).expect("householder should succeed");

        assert_eq!(v.len(), 3);
        assert!(beta >= 0.0);

        // Verify that applying the Householder reflection gives correct result
        let result = StableDecompositions::apply_householder(&x, &v, beta)
            .expect("apply householder should succeed");

        // First component should have the opposite sign and same magnitude as original norm
        let x_norm = (1.0 + 4.0 + 9.0_f64).sqrt();
        assert_relative_eq!(result[0].abs(), x_norm, epsilon = 1e-10);

        // Other components should be zero
        assert_relative_eq!(result[1], 0.0, epsilon = 1e-10);
        assert_relative_eq!(result[2], 0.0, epsilon = 1e-10);
    }
}