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
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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
//! GPU-accelerated matrix decompositions.
//!
//! Provides GPU-accelerated LU, QR, Cholesky, and SVD decompositions using
//! scirs2-core GPU backends. Each operation has automatic CPU fallback when
//! GPU is unavailable or the matrix is too small to benefit from GPU transfer.

use scirs2_core::gpu::{GpuBackend, GpuContext, GpuError};
use scirs2_core::ndarray::{Array1, Array2, ArrayView2, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign, One, Zero};
use std::iter::Sum;

use crate::error::{LinalgError, LinalgResult};

use super::types::{CholeskyFactors, LuFactors, QrFactors, SvdFactors};

/// Minimum matrix dimension for GPU acceleration.
/// Below this threshold, CPU is faster due to data transfer overhead.
const GPU_DECOMP_THRESHOLD: usize = 32;

// =============================================================================
// LU Decomposition
// =============================================================================

/// GPU-accelerated LU decomposition: PA = LU
///
/// Factors the matrix A into a permutation matrix P, a lower-triangular matrix L
/// (with unit diagonal), and an upper-triangular matrix U.
///
/// Uses GPU for large matrices, falling back to CPU for small matrices or when
/// no GPU is available.
///
/// # Arguments
///
/// * `ctx` - GPU context (or None for CPU fallback)
/// * `a` - Input square matrix
///
/// # Returns
///
/// `LuFactors` containing P, L, and U matrices.
///
/// # Errors
///
/// Returns `LinalgError` if the matrix is empty, non-finite, or singular.
pub fn gpu_lu<F>(ctx: Option<&GpuContext>, a: &ArrayView2<F>) -> LinalgResult<LuFactors<F>>
where
    F: Float + NumAssign + One + Sum + Send + Sync + ScalarOperand + 'static,
{
    validate_matrix(a, "LU decomposition")?;

    let n = a.nrows();

    // Try GPU path for large matrices
    if n >= GPU_DECOMP_THRESHOLD {
        if let Some(gpu_ctx) = ctx {
            match gpu_lu_impl(gpu_ctx, a) {
                Ok(factors) => return Ok(factors),
                Err(_) => {
                    // Fall through to CPU
                }
            }
        }
    }

    // CPU fallback: use existing LU decomposition
    cpu_lu(a)
}

/// GPU LU implementation via blocked algorithm on GPU buffers.
fn gpu_lu_impl<F>(ctx: &GpuContext, a: &ArrayView2<F>) -> Result<LuFactors<F>, LinalgError>
where
    F: Float + NumAssign + One + Sum + Send + Sync + ScalarOperand + 'static,
{
    let n = a.nrows();
    let m = a.ncols();

    // Transfer matrix to GPU as f64
    let a_flat: Vec<f64> = a.iter().map(|v| v.to_f64().unwrap_or(0.0)).collect();
    let _gpu_buf = ctx.create_buffer_from_slice(&a_flat);

    // For the GPU LU decomposition, we implement a right-looking blocked algorithm.
    // We perform the factorization panel by panel on GPU using GEMM for the
    // trailing matrix update.
    //
    // The core loop:
    //   1. Factor the current panel (column block) to find pivots
    //   2. Apply row swaps to the rest of the matrix
    //   3. Solve triangular system for U block
    //   4. Update trailing sub-matrix via GEMM: A_trail -= L_panel * U_top

    // We work on host data but use GPU for the trailing matrix update (GEMM).
    let mut work = a.to_owned();
    let min_dim = n.min(m);
    let mut piv: Vec<usize> = (0..n).collect();

    let block_size = 32.min(min_dim);

    let mut jb = 0;
    while jb < min_dim {
        let jb_end = (jb + block_size).min(min_dim);
        let current_block = jb_end - jb;

        // Factor the panel [jb..n, jb..jb_end] using partial pivoting
        for j in jb..jb_end {
            // Find pivot in column j, rows j..n
            let mut max_val = F::zero();
            let mut max_row = j;
            for i in j..n {
                let val = work[[i, j]].abs();
                if val > max_val {
                    max_val = val;
                    max_row = i;
                }
            }

            // Swap rows if needed
            if max_row != j {
                for col in 0..m {
                    let tmp = work[[j, col]];
                    work[[j, col]] = work[[max_row, col]];
                    work[[max_row, col]] = tmp;
                }
                piv.swap(j, max_row);
            }

            // Check for singularity
            let pivot = work[[j, j]];
            if pivot.abs() < F::epsilon() * F::from(100.0).unwrap_or_else(F::one) {
                // Nearly singular, but we continue (the result will have small pivots)
            }

            // Compute multipliers for column j
            if pivot.abs() > F::zero() {
                for i in (j + 1)..n {
                    work[[i, j]] /= pivot;
                }
            }

            // Update the panel sub-matrix
            for i in (j + 1)..n {
                let factor = work[[i, j]];
                for k in (j + 1)..jb_end {
                    let rhs = factor * work[[j, k]];
                    work[[i, k]] -= rhs;
                }
            }
        }

        // Update trailing matrix using GPU GEMM: A_trail -= L_panel * U_top
        if jb_end < m {
            let trail_rows = n - jb;
            let trail_cols = m - jb_end;

            // Extract L panel (below diagonal part of current block)
            // and U top (right of current block in factored rows)
            if trail_rows > 0 && trail_cols > 0 && current_block > 0 {
                // L_panel: rows [jb..n], cols [jb..jb_end] (lower part only, rows jb_end..n)
                let l_rows = n - jb_end;
                if l_rows > 0 {
                    let mut l_panel = vec![0.0_f64; l_rows * current_block];
                    for i in 0..l_rows {
                        for j in 0..current_block {
                            l_panel[i * current_block + j] =
                                work[[jb_end + i, jb + j]].to_f64().unwrap_or(0.0);
                        }
                    }

                    let mut u_top = vec![0.0_f64; current_block * trail_cols];
                    for i in 0..current_block {
                        for j in 0..trail_cols {
                            u_top[i * trail_cols + j] =
                                work[[jb + i, jb_end + j]].to_f64().unwrap_or(0.0);
                        }
                    }

                    // GPU GEMM: C = L_panel * U_top
                    let gpu_l = ctx.create_buffer_from_slice(&l_panel);
                    let gpu_u = ctx.create_buffer_from_slice(&u_top);

                    match ctx.gemm(&gpu_l, &gpu_u, l_rows, current_block, trail_cols) {
                        Ok(gpu_c) => {
                            let c_flat = gpu_c.to_vec();
                            // Subtract from trailing matrix
                            for i in 0..l_rows {
                                for j in 0..trail_cols {
                                    let val =
                                        F::from(c_flat[i * trail_cols + j]).unwrap_or_else(F::zero);
                                    work[[jb_end + i, jb_end + j]] -= val;
                                }
                            }
                        }
                        Err(_) => {
                            // CPU fallback for trailing update
                            for i in 0..l_rows {
                                for j in 0..trail_cols {
                                    let mut sum = F::zero();
                                    for p in 0..current_block {
                                        sum +=
                                            work[[jb_end + i, jb + p]] * work[[jb + p, jb_end + j]];
                                    }
                                    work[[jb_end + i, jb_end + j]] -= sum;
                                }
                            }
                        }
                    }
                }
            }
        }

        jb = jb_end;
    }

    // Extract P, L, U from the work array
    extract_plu(&work, &piv, n, m)
}

/// CPU fallback for LU decomposition.
fn cpu_lu<F>(a: &ArrayView2<F>) -> LinalgResult<LuFactors<F>>
where
    F: Float + NumAssign + One + Sum + Send + Sync + ScalarOperand + 'static,
{
    let (p, l, u) = crate::lu(a, None)?;
    Ok(LuFactors { p, l, u })
}

/// Extract P, L, U matrices from compact LU storage.
fn extract_plu<F>(
    work: &Array2<F>,
    piv: &[usize],
    n: usize,
    m: usize,
) -> Result<LuFactors<F>, LinalgError>
where
    F: Float + NumAssign + One + Zero,
{
    let min_dim = n.min(m);

    // Build permutation matrix
    let mut p = Array2::<F>::zeros((n, n));
    for (i, &piv_idx) in piv.iter().enumerate() {
        if piv_idx < n {
            p[[i, piv_idx]] = F::one();
        }
    }

    // Extract L (lower triangular with unit diagonal)
    let mut l = Array2::<F>::zeros((n, min_dim));
    for i in 0..n {
        for j in 0..i.min(min_dim) {
            l[[i, j]] = work[[i, j]];
        }
        if i < min_dim {
            l[[i, i]] = F::one();
        }
    }

    // Extract U (upper triangular)
    let mut u = Array2::<F>::zeros((min_dim, m));
    for i in 0..min_dim {
        for j in i..m {
            u[[i, j]] = work[[i, j]];
        }
    }

    Ok(LuFactors { p, l, u })
}

// =============================================================================
// QR Decomposition
// =============================================================================

/// GPU-accelerated QR decomposition: A = QR
///
/// Factors the matrix A into an orthogonal matrix Q and an upper-triangular
/// matrix R using Householder reflections, with GPU-accelerated trailing
/// matrix updates.
///
/// # Arguments
///
/// * `ctx` - GPU context (or None for CPU fallback)
/// * `a` - Input matrix (m x n)
///
/// # Returns
///
/// `QrFactors` containing Q and R matrices.
///
/// # Errors
///
/// Returns `LinalgError` if the matrix is empty or contains non-finite values.
pub fn gpu_qr<F>(ctx: Option<&GpuContext>, a: &ArrayView2<F>) -> LinalgResult<QrFactors<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    validate_matrix(a, "QR decomposition")?;

    let (m, n) = a.dim();

    if m >= GPU_DECOMP_THRESHOLD && n >= GPU_DECOMP_THRESHOLD {
        if let Some(gpu_ctx) = ctx {
            match gpu_qr_impl(gpu_ctx, a) {
                Ok(factors) => return Ok(factors),
                Err(_) => {
                    // Fall through to CPU
                }
            }
        }
    }

    cpu_qr(a)
}

/// GPU QR implementation using Householder reflections with GPU-accelerated updates.
fn gpu_qr_impl<F>(ctx: &GpuContext, a: &ArrayView2<F>) -> Result<QrFactors<F>, LinalgError>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    let (m, n) = a.dim();
    let min_dim = m.min(n);

    let mut r = a.to_owned();
    let mut q = Array2::<F>::zeros((m, m));
    // Initialize Q as identity
    for i in 0..m {
        q[[i, i]] = F::one();
    }

    for k in 0..min_dim {
        // Compute Householder vector for column k
        let mut norm_sq = F::zero();
        for i in k..m {
            norm_sq += r[[i, k]] * r[[i, k]];
        }
        let norm = norm_sq.sqrt();

        if norm < F::epsilon() {
            continue;
        }

        // Compute the Householder vector v
        let sign = if r[[k, k]] >= F::zero() {
            F::one()
        } else {
            -F::one()
        };
        let mut v = vec![F::zero(); m - k];
        v[0] = r[[k, k]] + sign * norm;
        for i in 1..(m - k) {
            v[i] = r[[k + i, k]];
        }

        // Normalize v
        let mut v_norm_sq = F::zero();
        for vi in &v {
            v_norm_sq += *vi * *vi;
        }
        if v_norm_sq < F::epsilon() {
            continue;
        }
        let tau = F::from(2.0).unwrap_or_else(F::one) / v_norm_sq;

        // Apply Householder reflection to R: R[k:, k:] -= tau * v * (v^T * R[k:, k:])
        // Using GPU for the matrix-vector products when beneficial
        let trail_cols = n - k;
        let trail_rows = m - k;

        if trail_rows >= GPU_DECOMP_THRESHOLD && trail_cols >= GPU_DECOMP_THRESHOLD {
            // Try GPU-accelerated update
            // v^T * R[k:, k:] => 1 x trail_cols
            let mut vt_r = vec![F::zero(); trail_cols];
            for j in 0..trail_cols {
                let mut sum = F::zero();
                for i in 0..trail_rows {
                    sum += v[i] * r[[k + i, k + j]];
                }
                vt_r[j] = sum;
            }

            // R[k:, k:] -= tau * v * vt_r^T (outer product update)
            // Use GPU for the outer product if large enough
            let v_f64: Vec<f64> = v.iter().map(|x| x.to_f64().unwrap_or(0.0)).collect();
            let vtr_f64: Vec<f64> = vt_r.iter().map(|x| x.to_f64().unwrap_or(0.0)).collect();

            let gpu_v = ctx.create_buffer_from_slice(&v_f64);
            let gpu_vtr = ctx.create_buffer_from_slice(&vtr_f64);

            match ctx.gemm(&gpu_v, &gpu_vtr, trail_rows, 1, trail_cols) {
                Ok(gpu_outer) => {
                    let outer_flat = gpu_outer.to_vec();
                    let tau_f64 = tau.to_f64().unwrap_or(2.0);
                    for i in 0..trail_rows {
                        for j in 0..trail_cols {
                            let val = F::from(tau_f64 * outer_flat[i * trail_cols + j])
                                .unwrap_or_else(F::zero);
                            r[[k + i, k + j]] -= val;
                        }
                    }
                }
                Err(_) => {
                    // CPU fallback for outer product
                    for i in 0..trail_rows {
                        for j in 0..trail_cols {
                            r[[k + i, k + j]] -= tau * v[i] * vt_r[j];
                        }
                    }
                }
            }
        } else {
            // Small matrix: CPU update
            let mut vt_r = vec![F::zero(); trail_cols];
            for j in 0..trail_cols {
                let mut sum = F::zero();
                for i in 0..trail_rows {
                    sum += v[i] * r[[k + i, k + j]];
                }
                vt_r[j] = sum;
            }
            for i in 0..trail_rows {
                for j in 0..trail_cols {
                    r[[k + i, k + j]] -= tau * v[i] * vt_r[j];
                }
            }
        }

        // Apply Householder reflection to Q: Q[:, k:] -= tau * (Q[:, k:] * v) * v^T
        let mut q_v = vec![F::zero(); m];
        for i in 0..m {
            let mut sum = F::zero();
            for j in 0..trail_rows {
                sum += q[[i, k + j]] * v[j];
            }
            q_v[i] = sum;
        }
        for i in 0..m {
            for j in 0..trail_rows {
                q[[i, k + j]] -= tau * q_v[i] * v[j];
            }
        }
    }

    // Trim R to proper dimensions
    let r_trimmed = r.slice(scirs2_core::ndarray::s![0..min_dim, ..]).to_owned();

    // Trim Q to proper dimensions
    let q_trimmed = q.slice(scirs2_core::ndarray::s![.., 0..min_dim]).to_owned();

    // For economy QR: Q is m x min(m,n), R is min(m,n) x n
    // For full QR: Q is m x m, R is m x n
    // We return the economy form
    Ok(QrFactors {
        q: q_trimmed,
        r: r_trimmed,
    })
}

/// CPU fallback for QR decomposition.
fn cpu_qr<F>(a: &ArrayView2<F>) -> LinalgResult<QrFactors<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    let (q, r) = crate::qr(a, None)?;
    Ok(QrFactors { q, r })
}

// =============================================================================
// Cholesky Decomposition
// =============================================================================

/// GPU-accelerated Cholesky decomposition: A = LL^T
///
/// Factors a symmetric positive-definite matrix A into a lower-triangular
/// matrix L such that A = L * L^T. Uses a blocked algorithm with
/// GPU-accelerated trailing matrix updates.
///
/// # Arguments
///
/// * `ctx` - GPU context (or None for CPU fallback)
/// * `a` - Symmetric positive-definite matrix
///
/// # Returns
///
/// `CholeskyFactors` containing the lower-triangular factor L.
///
/// # Errors
///
/// Returns `LinalgError` if the matrix is not square, not positive definite,
/// or contains non-finite values.
pub fn gpu_cholesky<F>(
    ctx: Option<&GpuContext>,
    a: &ArrayView2<F>,
) -> LinalgResult<CholeskyFactors<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    validate_matrix(a, "Cholesky decomposition")?;

    if a.nrows() != a.ncols() {
        return Err(LinalgError::ShapeError(
            "Cholesky decomposition requires a square matrix".to_string(),
        ));
    }

    let n = a.nrows();

    if n >= GPU_DECOMP_THRESHOLD {
        if let Some(gpu_ctx) = ctx {
            match gpu_cholesky_impl(gpu_ctx, a) {
                Ok(factors) => return Ok(factors),
                Err(_) => {
                    // Fall through to CPU
                }
            }
        }
    }

    cpu_cholesky(a)
}

/// GPU Cholesky implementation using blocked algorithm.
fn gpu_cholesky_impl<F>(
    ctx: &GpuContext,
    a: &ArrayView2<F>,
) -> Result<CholeskyFactors<F>, LinalgError>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    let n = a.nrows();
    let mut l = Array2::<F>::zeros((n, n));

    // Copy lower triangle of A
    for i in 0..n {
        for j in 0..=i {
            l[[i, j]] = a[[i, j]];
        }
    }

    let block_size = 32.min(n);

    let mut jb = 0;
    while jb < n {
        let jb_end = (jb + block_size).min(n);
        let current_block = jb_end - jb;

        // Factor the diagonal block using standard Cholesky
        for j in jb..jb_end {
            // Compute diagonal element
            let mut sum = l[[j, j]];
            for k in jb..j {
                sum -= l[[j, k]] * l[[j, k]];
            }
            if sum <= F::zero() {
                return Err(LinalgError::NonPositiveDefiniteError(format!(
                    "Matrix is not positive definite at index {}",
                    j
                )));
            }
            l[[j, j]] = sum.sqrt();

            // Compute column j below diagonal within the block
            let diag = l[[j, j]];
            for i in (j + 1)..jb_end {
                let mut sum = l[[i, j]];
                for k in jb..j {
                    sum -= l[[i, k]] * l[[j, k]];
                }
                l[[i, j]] = sum / diag;
            }
        }

        // Update rows below the current block using GPU GEMM
        if jb_end < n {
            let trail_rows = n - jb_end;

            // L_trail_panel = L[jb_end:, jb:jb_end] (to be computed from A[jb_end:, jb:jb_end])
            // Solve: L_trail_panel * L_block^T = A_trail_panel
            // Where L_block is L[jb:jb_end, jb:jb_end]

            // First, solve the triangular system for the panel below
            for j in jb..jb_end {
                let diag = l[[j, j]];
                for i in jb_end..n {
                    let mut sum = l[[i, j]];
                    for k in jb..j {
                        sum -= l[[i, k]] * l[[j, k]];
                    }
                    l[[i, j]] = sum / diag;
                }
            }

            // Update trailing matrix using GPU GEMM
            // L[jb_end:, jb_end:] -= L[jb_end:, jb:jb_end] * L[jb_end:, jb:jb_end]^T
            // But we only need the lower triangle

            let mut l_panel_flat = vec![0.0_f64; trail_rows * current_block];
            for i in 0..trail_rows {
                for j in 0..current_block {
                    l_panel_flat[i * current_block + j] =
                        l[[jb_end + i, jb + j]].to_f64().unwrap_or(0.0);
                }
            }

            let gpu_l_panel = ctx.create_buffer_from_slice(&l_panel_flat);

            // Compute L_panel * L_panel^T using GPU
            match ctx.gemm_transpose_b(
                &gpu_l_panel,
                &gpu_l_panel,
                trail_rows,
                current_block,
                trail_rows,
            ) {
                Ok(gpu_update) => {
                    let update_flat = gpu_update.to_vec();
                    // Subtract from trailing lower triangle
                    for i in 0..trail_rows {
                        for j in 0..=i {
                            let val =
                                F::from(update_flat[i * trail_rows + j]).unwrap_or_else(F::zero);
                            l[[jb_end + i, jb_end + j]] -= val;
                        }
                    }
                }
                Err(_) => {
                    // CPU fallback for trailing update
                    for i in 0..trail_rows {
                        for j in 0..=i {
                            let mut sum = F::zero();
                            for p in 0..current_block {
                                sum += l[[jb_end + i, jb + p]] * l[[jb_end + j, jb + p]];
                            }
                            l[[jb_end + i, jb_end + j]] -= sum;
                        }
                    }
                }
            }
        }

        jb = jb_end;
    }

    Ok(CholeskyFactors { l })
}

/// CPU fallback for Cholesky decomposition.
fn cpu_cholesky<F>(a: &ArrayView2<F>) -> LinalgResult<CholeskyFactors<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    let l = crate::cholesky(a, None)?;
    Ok(CholeskyFactors { l })
}

// =============================================================================
// SVD (Singular Value Decomposition)
// =============================================================================

/// GPU-accelerated thin SVD: A = U * diag(S) * V^T
///
/// Computes the thin (economy) singular value decomposition. For an m x n matrix
/// with k = min(m, n), returns U (m x k), S (k), V^T (k x n).
///
/// Uses a two-phase approach:
/// 1. Bidiagonalization via Householder reflections (partially GPU-accelerated)
/// 2. Iterative bidiagonal SVD (Golub-Kahan) on the bidiagonal form
///
/// # Arguments
///
/// * `ctx` - GPU context (or None for CPU fallback)
/// * `a` - Input matrix (m x n)
///
/// # Returns
///
/// `SvdFactors` containing U, S (singular values), and V^T.
///
/// # Errors
///
/// Returns `LinalgError` if the matrix is empty, contains non-finite values,
/// or the iterative SVD fails to converge.
pub fn gpu_svd<F>(ctx: Option<&GpuContext>, a: &ArrayView2<F>) -> LinalgResult<SvdFactors<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    validate_matrix(a, "SVD")?;

    let (m, n) = a.dim();

    if m >= GPU_DECOMP_THRESHOLD && n >= GPU_DECOMP_THRESHOLD {
        if let Some(gpu_ctx) = ctx {
            match gpu_svd_impl(gpu_ctx, a) {
                Ok(factors) => return Ok(factors),
                Err(_) => {
                    // Fall through to CPU
                }
            }
        }
    }

    cpu_svd(a)
}

/// GPU SVD implementation.
///
/// Strategy: Use GPU-accelerated QR iteration approach.
/// For large matrices, we first compute A^T * A (or A * A^T for tall matrices)
/// via GPU GEMM, then compute eigendecomposition of the smaller product,
/// and reconstruct the SVD factors.
fn gpu_svd_impl<F>(ctx: &GpuContext, a: &ArrayView2<F>) -> Result<SvdFactors<F>, LinalgError>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    let (m, n) = a.dim();
    let transpose = m < n;

    // For the one-sided Jacobi approach or cross-product method:
    // If m >= n: compute B = A^T * A (n x n), eigendecompose B = V * Lambda * V^T
    //   Then S = sqrt(Lambda), U = A * V * S^{-1}
    // If m < n:  compute B = A * A^T (m x m), eigendecompose B = U * Lambda * U^T
    //   Then S = sqrt(Lambda), V^T = S^{-1} * U^T * A

    let a_flat: Vec<f64> = a.iter().map(|v| v.to_f64().unwrap_or(0.0)).collect();
    let gpu_a = ctx.create_buffer_from_slice(&a_flat);

    let (gram_size, gram_data) = if !transpose {
        // B = A^T * A (n x n)
        let gpu_ata = ctx
            .gemm_transpose_a(&gpu_a, &gpu_a, n, m, n)
            .map_err(|e| LinalgError::ComputationError(format!("GPU A^T*A failed: {}", e)))?;
        (n, gpu_ata.to_vec())
    } else {
        // B = A * A^T (m x m)
        let gpu_aat = ctx
            .gemm_transpose_b(&gpu_a, &gpu_a, m, n, m)
            .map_err(|e| LinalgError::ComputationError(format!("GPU A*A^T failed: {}", e)))?;
        (m, gpu_aat.to_vec())
    };

    // Reconstruct the Gram matrix
    let gram_f: Vec<F> = gram_data
        .iter()
        .map(|&v| F::from(v).unwrap_or_else(F::zero))
        .collect();
    let gram = Array2::from_shape_vec((gram_size, gram_size), gram_f)
        .map_err(|e| LinalgError::ShapeError(format!("Failed to reshape Gram matrix: {}", e)))?;

    // Eigendecompose the Gram matrix using CPU (it's symmetric positive semi-definite)
    let (eigenvalues, eigenvectors) = symmetric_eigen(&gram)?;

    let k = m.min(n);

    // Sort eigenvalues in descending order
    let mut indices: Vec<usize> = (0..eigenvalues.len()).collect();
    indices.sort_by(|&a_idx, &b_idx| {
        let a_val = eigenvalues[b_idx].abs();
        let b_val = eigenvalues[a_idx].abs();
        a_val
            .partial_cmp(&b_val)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    // Singular values = sqrt of eigenvalues (clamped to non-negative)
    let mut s = Array1::<F>::zeros(k);
    for i in 0..k {
        let ev = eigenvalues[indices[i]];
        s[i] = if ev > F::zero() { ev.sqrt() } else { F::zero() };
    }

    if !transpose {
        // We have eigenvectors of A^T * A => these are V
        let mut v = Array2::<F>::zeros((k, n));
        for i in 0..k {
            for j in 0..n {
                v[[i, j]] = eigenvectors[[j, indices[i]]];
            }
        }

        // U = A * V^T * S^{-1}
        // First compute A * V^T using GPU
        let vt_flat: Vec<f64> = v.iter().map(|x| x.to_f64().unwrap_or(0.0)).collect();
        let gpu_vt = ctx.create_buffer_from_slice(&vt_flat);

        // A (m x n) * V^T (stored as k x n, transposed = n x k) => m x k
        // We need V (n x k) for this multiplication
        let mut v_col_major = vec![0.0_f64; n * k];
        for i in 0..k {
            for j in 0..n {
                v_col_major[j * k + i] = v[[i, j]].to_f64().unwrap_or(0.0);
            }
        }
        let gpu_v_cols = ctx.create_buffer_from_slice(&v_col_major);

        let u = match ctx.gemm(&gpu_a, &gpu_v_cols, m, n, k) {
            Ok(gpu_u) => {
                let u_flat = gpu_u.to_vec();
                let mut u_mat = Array2::<F>::zeros((m, k));
                for i in 0..m {
                    for j in 0..k {
                        let val = F::from(u_flat[i * k + j]).unwrap_or_else(F::zero);
                        if s[j].abs() > F::epsilon() {
                            u_mat[[i, j]] = val / s[j];
                        }
                    }
                }
                u_mat
            }
            Err(_) => {
                // CPU fallback
                let mut u_mat = Array2::<F>::zeros((m, k));
                for i in 0..m {
                    for j in 0..k {
                        let mut sum = F::zero();
                        for p in 0..n {
                            sum += a[[i, p]] * v[[j, p]]; // v stored as rows
                        }
                        if s[j].abs() > F::epsilon() {
                            u_mat[[i, j]] = sum / s[j];
                        }
                    }
                }
                u_mat
            }
        };

        Ok(SvdFactors { u, s, vt: v })
    } else {
        // We have eigenvectors of A * A^T => these are U
        let mut u = Array2::<F>::zeros((m, k));
        for i in 0..m {
            for j in 0..k {
                u[[i, j]] = eigenvectors[[i, indices[j]]];
            }
        }

        // V^T = S^{-1} * U^T * A
        // First compute U^T * A using GPU
        let ut_flat: Vec<f64> = {
            let mut flat = vec![0.0_f64; k * m];
            for i in 0..k {
                for j in 0..m {
                    flat[i * m + j] = u[[j, i]].to_f64().unwrap_or(0.0);
                }
            }
            flat
        };
        let gpu_ut = ctx.create_buffer_from_slice(&ut_flat);

        let vt = match ctx.gemm(&gpu_ut, &gpu_a, k, m, n) {
            Ok(gpu_vt) => {
                let vt_flat = gpu_vt.to_vec();
                let mut vt_mat = Array2::<F>::zeros((k, n));
                for i in 0..k {
                    for j in 0..n {
                        let val = F::from(vt_flat[i * n + j]).unwrap_or_else(F::zero);
                        if s[i].abs() > F::epsilon() {
                            vt_mat[[i, j]] = val / s[i];
                        }
                    }
                }
                vt_mat
            }
            Err(_) => {
                // CPU fallback
                let mut vt_mat = Array2::<F>::zeros((k, n));
                for i in 0..k {
                    for j in 0..n {
                        let mut sum = F::zero();
                        for p in 0..m {
                            sum += u[[p, i]] * a[[p, j]];
                        }
                        if s[i].abs() > F::epsilon() {
                            vt_mat[[i, j]] = sum / s[i];
                        }
                    }
                }
                vt_mat
            }
        };

        Ok(SvdFactors { u, s, vt })
    }
}

/// Compute eigendecomposition of a symmetric matrix using Jacobi iteration.
///
/// Returns eigenvalues and eigenvectors sorted by magnitude (descending).
fn symmetric_eigen<F>(a: &Array2<F>) -> Result<(Array1<F>, Array2<F>), LinalgError>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    let n = a.nrows();
    let mut d = a.clone();
    let mut v = Array2::<F>::zeros((n, n));
    for i in 0..n {
        v[[i, i]] = F::one();
    }

    let max_iter = 100 * n * n;
    let tol = F::epsilon() * F::from(100.0).unwrap_or_else(F::one);

    for _ in 0..max_iter {
        // Find the largest off-diagonal element
        let mut max_off = F::zero();
        let mut p = 0;
        let mut q = 1;
        for i in 0..n {
            for j in (i + 1)..n {
                let val = d[[i, j]].abs();
                if val > max_off {
                    max_off = val;
                    p = i;
                    q = j;
                }
            }
        }

        if max_off < tol {
            break;
        }

        // Compute Jacobi rotation
        let d_pp = d[[p, p]];
        let d_qq = d[[q, q]];
        let d_pq = d[[p, q]];

        let theta = if (d_pp - d_qq).abs() < F::epsilon() {
            F::from(std::f64::consts::FRAC_PI_4).unwrap_or_else(F::one)
        } else {
            let tau = F::from(2.0).unwrap_or_else(F::one) * d_pq / (d_pp - d_qq);
            let t = if tau >= F::zero() {
                F::one() / (tau + (F::one() + tau * tau).sqrt())
            } else {
                -F::one() / (-tau + (F::one() + tau * tau).sqrt())
            };
            t.atan()
        };

        let cos_t = theta.cos();
        let sin_t = theta.sin();

        // Apply rotation to D
        let mut new_d = d.clone();
        for i in 0..n {
            if i != p && i != q {
                let d_ip = d[[i, p]];
                let d_iq = d[[i, q]];
                new_d[[i, p]] = cos_t * d_ip + sin_t * d_iq;
                new_d[[p, i]] = new_d[[i, p]];
                new_d[[i, q]] = -sin_t * d_ip + cos_t * d_iq;
                new_d[[q, i]] = new_d[[i, q]];
            }
        }
        new_d[[p, p]] = cos_t * cos_t * d_pp
            + F::from(2.0).unwrap_or_else(F::one) * cos_t * sin_t * d_pq
            + sin_t * sin_t * d_qq;
        new_d[[q, q]] = sin_t * sin_t * d_pp
            - F::from(2.0).unwrap_or_else(F::one) * cos_t * sin_t * d_pq
            + cos_t * cos_t * d_qq;
        new_d[[p, q]] = F::zero();
        new_d[[q, p]] = F::zero();
        d = new_d;

        // Apply rotation to V
        for i in 0..n {
            let v_ip = v[[i, p]];
            let v_iq = v[[i, q]];
            v[[i, p]] = cos_t * v_ip + sin_t * v_iq;
            v[[i, q]] = -sin_t * v_ip + cos_t * v_iq;
        }
    }

    // Extract eigenvalues from diagonal
    let mut eigenvalues = Array1::<F>::zeros(n);
    for i in 0..n {
        eigenvalues[i] = d[[i, i]];
    }

    Ok((eigenvalues, v))
}

/// CPU fallback for SVD.
fn cpu_svd<F>(a: &ArrayView2<F>) -> LinalgResult<SvdFactors<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
{
    let (u, s, vt) = crate::svd(a, false, None)?;
    Ok(SvdFactors { u, s, vt })
}

// =============================================================================
// Validation helpers
// =============================================================================

/// Validate that a matrix is non-empty and contains finite values.
fn validate_matrix<F>(a: &ArrayView2<F>, operation: &str) -> LinalgResult<()>
where
    F: Float,
{
    if a.is_empty() {
        return Err(LinalgError::ShapeError(format!(
            "{} failed: Input matrix cannot be empty",
            operation
        )));
    }

    for &val in a.iter() {
        if !val.is_finite() {
            return Err(LinalgError::InvalidInputError(format!(
                "{} failed: Matrix contains non-finite values",
                operation
            )));
        }
    }

    Ok(())
}

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

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

    // ---- LU tests ----

    #[test]
    fn test_cpu_lu_basic() {
        let a = array![[2.0_f64, 1.0], [4.0, 3.0]];
        let factors = cpu_lu(&a.view()).expect("LU failed");

        // Convention: P * A = L * U
        let pa = factors.p.dot(&a);
        let lu = factors.l.dot(&factors.u);
        for i in 0..2 {
            for j in 0..2 {
                assert_relative_eq!(pa[[i, j]], lu[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_gpu_lu_fallback() {
        let a = array![[1.0_f64, 2.0], [3.0, 4.0]];
        let factors = gpu_lu(None, &a.view()).expect("LU fallback failed");

        // Convention: P * A = L * U
        let pa = factors.p.dot(&a);
        let lu = factors.l.dot(&factors.u);
        for i in 0..2 {
            for j in 0..2 {
                assert_relative_eq!(pa[[i, j]], lu[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_lu_empty_matrix() {
        let a = Array2::<f64>::zeros((0, 0));
        let result = gpu_lu(None, &a.view());
        assert!(result.is_err());
    }

    #[test]
    fn test_lu_3x3() {
        let a = array![[1.0_f64, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 10.0]];
        let factors = gpu_lu(None, &a.view()).expect("3x3 LU failed");
        // Convention: P * A = L * U
        let pa = factors.p.dot(&a);
        let lu = factors.l.dot(&factors.u);
        for i in 0..3 {
            for j in 0..3 {
                assert_relative_eq!(pa[[i, j]], lu[[i, j]], epsilon = 1e-10);
            }
        }
    }

    // ---- QR tests ----

    #[test]
    fn test_cpu_qr_basic() {
        let a = array![[1.0_f64, 2.0], [3.0, 4.0]];
        let factors = cpu_qr(&a.view()).expect("QR failed");

        // Verify Q * R ≈ A
        let qr_product = factors.q.dot(&factors.r);
        for i in 0..2 {
            for j in 0..2 {
                assert_relative_eq!(qr_product[[i, j]], a[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_gpu_qr_fallback() {
        let a = array![
            [12.0_f64, -51.0, 4.0],
            [6.0, 167.0, -68.0],
            [-4.0, 24.0, -41.0]
        ];
        let factors = gpu_qr(None, &a.view()).expect("QR fallback failed");
        let qr_product = factors.q.dot(&factors.r);
        for i in 0..3 {
            for j in 0..3 {
                assert_relative_eq!(qr_product[[i, j]], a[[i, j]], epsilon = 1e-8);
            }
        }
    }

    #[test]
    fn test_qr_empty_matrix() {
        let a = Array2::<f64>::zeros((0, 0));
        let result = gpu_qr(None, &a.view());
        assert!(result.is_err());
    }

    // ---- Cholesky tests ----

    #[test]
    fn test_cpu_cholesky_basic() {
        let a = array![[4.0_f64, 2.0], [2.0, 5.0]];
        let factors = cpu_cholesky(&a.view()).expect("Cholesky failed");

        // Verify L * L^T ≈ A
        let llt = factors.l.dot(&factors.l.t());
        for i in 0..2 {
            for j in 0..2 {
                assert_relative_eq!(llt[[i, j]], a[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_gpu_cholesky_fallback() {
        let a = array![[25.0_f64, 15.0, -5.0], [15.0, 18.0, 0.0], [-5.0, 0.0, 11.0]];
        let factors = gpu_cholesky(None, &a.view()).expect("Cholesky fallback failed");
        let llt = factors.l.dot(&factors.l.t());
        for i in 0..3 {
            for j in 0..3 {
                assert_relative_eq!(llt[[i, j]], a[[i, j]], epsilon = 1e-10);
            }
        }
    }

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

    #[test]
    fn test_cholesky_empty() {
        let a = Array2::<f64>::zeros((0, 0));
        let result = gpu_cholesky(None, &a.view());
        assert!(result.is_err());
    }

    // ---- SVD tests ----

    #[test]
    fn test_cpu_svd_basic() {
        let a = array![[1.0_f64, 2.0], [3.0, 4.0]];
        let factors = cpu_svd(&a.view()).expect("SVD failed");

        // Verify U * diag(S) * V^T ≈ A
        let s_diag = Array2::from_diag(&factors.s);
        let usv = factors.u.dot(&s_diag).dot(&factors.vt);
        for i in 0..2 {
            for j in 0..2 {
                assert_relative_eq!(usv[[i, j]], a[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_gpu_svd_fallback() {
        let a = array![[1.0_f64, 0.0], [0.0, 2.0]];
        let factors = gpu_svd(None, &a.view()).expect("SVD fallback failed");

        // For a diagonal matrix, singular values should be the absolute diagonal values
        let mut s_sorted: Vec<f64> = factors.s.iter().copied().collect();
        s_sorted.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
        assert_relative_eq!(s_sorted[0], 2.0, epsilon = 1e-10);
        assert_relative_eq!(s_sorted[1], 1.0, epsilon = 1e-10);
    }

    #[test]
    fn test_svd_empty_matrix() {
        let a = Array2::<f64>::zeros((0, 0));
        let result = gpu_svd(None, &a.view());
        assert!(result.is_err());
    }

    #[test]
    fn test_svd_reconstruction() {
        let a = array![[1.0_f64, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 10.0]];
        let factors = gpu_svd(None, &a.view()).expect("SVD failed");
        let s_diag = Array2::from_diag(&factors.s);
        let usv = factors.u.dot(&s_diag).dot(&factors.vt);
        for i in 0..3 {
            for j in 0..3 {
                assert_relative_eq!(usv[[i, j]], a[[i, j]], epsilon = 1e-8);
            }
        }
    }

    // ---- Validation tests ----

    #[test]
    fn test_validate_non_finite() {
        let a = array![[1.0_f64, f64::NAN], [3.0, 4.0]];
        let result = validate_matrix(&a.view(), "test");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_infinity() {
        let a = array![[1.0_f64, f64::INFINITY], [3.0, 4.0]];
        let result = validate_matrix(&a.view(), "test");
        assert!(result.is_err());
    }

    // ---- Symmetric eigendecomposition tests ----

    #[test]
    fn test_symmetric_eigen_diagonal() {
        let a = array![[3.0_f64, 0.0], [0.0, 1.0]];
        let (eigenvalues, eigenvectors) = symmetric_eigen(&a).expect("eigen failed");

        // Eigenvalues should be 3 and 1
        let mut ev_sorted: Vec<f64> = eigenvalues.iter().copied().collect();
        ev_sorted.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
        assert_relative_eq!(ev_sorted[0], 3.0, epsilon = 1e-10);
        assert_relative_eq!(ev_sorted[1], 1.0, epsilon = 1e-10);
    }

    #[test]
    fn test_symmetric_eigen_identity() {
        let a = array![[1.0_f64, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let (eigenvalues, _) = symmetric_eigen(&a).expect("eigen identity failed");
        for &ev in eigenvalues.iter() {
            assert_relative_eq!(ev, 1.0, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_gpu_lu_with_cpu_context() {
        match GpuContext::new(GpuBackend::Cpu) {
            Ok(gpu_ctx) => {
                let a = array![[2.0_f64, 1.0], [4.0, 3.0]];
                let factors = gpu_lu(Some(&gpu_ctx), &a.view()).expect("LU with ctx failed");
                // Convention: P * A = L * U
                let pa = factors.p.dot(&a);
                let lu = factors.l.dot(&factors.u);
                for i in 0..2 {
                    for j in 0..2 {
                        assert_relative_eq!(pa[[i, j]], lu[[i, j]], epsilon = 1e-10);
                    }
                }
            }
            Err(_) => {
                // Skip if cannot create context
            }
        }
    }

    #[test]
    fn test_gpu_qr_with_cpu_context() {
        if let Ok(gpu_ctx) = GpuContext::new(GpuBackend::Cpu) {
            let a = array![[1.0_f64, 2.0], [3.0, 4.0]];
            let factors = gpu_qr(Some(&gpu_ctx), &a.view()).expect("QR with ctx failed");
            let qr_product = factors.q.dot(&factors.r);
            for i in 0..2 {
                for j in 0..2 {
                    assert_relative_eq!(qr_product[[i, j]], a[[i, j]], epsilon = 1e-10);
                }
            }
        }
    }

    #[test]
    fn test_gpu_cholesky_with_cpu_context() {
        if let Ok(gpu_ctx) = GpuContext::new(GpuBackend::Cpu) {
            let a = array![[4.0_f64, 2.0], [2.0, 5.0]];
            let factors =
                gpu_cholesky(Some(&gpu_ctx), &a.view()).expect("Cholesky with ctx failed");
            let llt = factors.l.dot(&factors.l.t());
            for i in 0..2 {
                for j in 0..2 {
                    assert_relative_eq!(llt[[i, j]], a[[i, j]], epsilon = 1e-10);
                }
            }
        }
    }

    #[test]
    fn test_gpu_svd_with_cpu_context() {
        if let Ok(gpu_ctx) = GpuContext::new(GpuBackend::Cpu) {
            let a = array![[1.0_f64, 0.0], [0.0, 2.0]];
            let factors = gpu_svd(Some(&gpu_ctx), &a.view()).expect("SVD with ctx failed");
            let s_diag = Array2::from_diag(&factors.s);
            let usv = factors.u.dot(&s_diag).dot(&factors.vt);
            for i in 0..2 {
                for j in 0..2 {
                    assert_relative_eq!(usv[[i, j]], a[[i, j]], epsilon = 1e-8);
                }
            }
        }
    }
}