rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
//! Basic linear algebra for WebAssembly (BLAS-free implementation)
//! WebAssembly向け基本線形代数(BLAS非依存実装)

use wasm_bindgen::prelude::*;

/// WASM-compatible basic linear algebra operations
/// WASM互換基本線形代数演算
#[wasm_bindgen]
pub struct WasmLinearAlgebra {
    max_matrix_size: usize,
}

#[wasm_bindgen]
impl WasmLinearAlgebra {
    /// Create new linear algebra instance with size limits
    /// サイズ制限付きの新しい線形代数インスタンスを作成
    #[wasm_bindgen(constructor)]
    pub fn new(max_size: usize) -> WasmLinearAlgebra {
        WasmLinearAlgebra {
            max_matrix_size: if max_size > 0 && max_size <= 1000 {
                max_size
            } else {
                500
            },
        }
    }

    /// Matrix multiplication (basic O(n³) implementation)
    /// 行列乗算(基本O(n³)実装)
    #[wasm_bindgen]
    pub fn matmul(
        &self,
        a: Vec<f64>,
        a_rows: usize,
        a_cols: usize,
        b: Vec<f64>,
        b_rows: usize,
        b_cols: usize,
    ) -> Result<Vec<f64>, JsValue> {
        if a_rows > self.max_matrix_size
            || a_cols > self.max_matrix_size
            || b_rows > self.max_matrix_size
            || b_cols > self.max_matrix_size
        {
            return Err(JsValue::from_str("Matrix too large for WASM"));
        }

        if a_cols != b_rows {
            return Err(JsValue::from_str("Matrix dimensions incompatible"));
        }

        if a.len() != a_rows * a_cols || b.len() != b_rows * b_cols {
            return Err(JsValue::from_str("Data size doesn't match dimensions"));
        }

        let mut result = vec![0.0; a_rows * b_cols];

        for i in 0..a_rows {
            for j in 0..b_cols {
                let mut sum = 0.0;
                for k in 0..a_cols {
                    sum += a[i * a_cols + k] * b[k * b_cols + j];
                }
                result[i * b_cols + j] = sum;
            }
        }

        Ok(result)
    }

    /// Compute eigenvalues using QR algorithm (small matrices only)
    /// QRアルゴリズムによる固有値計算(小行列のみ)
    #[wasm_bindgen]
    pub fn eigenvalues(&self, matrix: Vec<f64>, n: usize) -> Result<Vec<f64>, JsValue> {
        if n > self.max_matrix_size {
            return Err(JsValue::from_str(
                "Matrix too large for eigenvalue computation",
            ));
        }

        if matrix.len() != n * n {
            return Err(JsValue::from_str("Matrix must be square"));
        }

        // Copy matrix for QR iterations
        let mut a = matrix;
        let max_iterations = 1000;
        let tolerance = 1e-10;

        // Simplified QR algorithm for eigenvalues
        for _ in 0..max_iterations {
            let q = self.qr_q_matrix(&a, n)?;
            let r = self.qr_r_matrix(&a, n)?;
            a = self.matmul(r, n, n, q, n, n)?;

            // Check convergence (off-diagonal elements should be small)
            let mut off_diag_sum = 0.0;
            for i in 0..n {
                for j in 0..n {
                    if i != j {
                        off_diag_sum += a[i * n + j].abs();
                    }
                }
            }

            if off_diag_sum < tolerance {
                break;
            }
        }

        // Extract diagonal elements (eigenvalues)
        let mut eigenvalues = Vec::new();
        for i in 0..n {
            eigenvalues.push(a[i * n + i]);
        }

        Ok(eigenvalues)
    }

    /// QR decomposition Q matrix using Gram-Schmidt process
    /// Gram-Schmidt過程によるQR分解のQ行列
    #[wasm_bindgen]
    pub fn qr_q_matrix(&self, matrix: &[f64], n: usize) -> Result<Vec<f64>, JsValue> {
        if matrix.len() != n * n {
            return Err(JsValue::from_str("Matrix must be square"));
        }

        let mut q = vec![0.0; n * n];
        let mut r = vec![0.0; n * n];

        // Gram-Schmidt orthogonalization
        for j in 0..n {
            // Copy column j from A to Q
            for i in 0..n {
                q[i * n + j] = matrix[i * n + j];
            }

            // Orthogonalize against previous columns
            for k in 0..j {
                // Compute dot product
                let mut dot = 0.0;
                for i in 0..n {
                    dot += q[i * n + j] * q[i * n + k];
                }
                r[k * n + j] = dot;

                // Subtract projection
                for i in 0..n {
                    q[i * n + j] -= dot * q[i * n + k];
                }
            }

            // Normalize column j
            let mut norm = 0.0;
            for i in 0..n {
                norm += q[i * n + j] * q[i * n + j];
            }
            norm = norm.sqrt();

            r[j * n + j] = norm;

            if norm > 1e-15 {
                for i in 0..n {
                    q[i * n + j] /= norm;
                }
            }
        }

        Ok(q)
    }

    /// QR decomposition R matrix using Gram-Schmidt process
    /// Gram-Schmidt過程によるQR分解のR行列
    #[wasm_bindgen]
    pub fn qr_r_matrix(&self, matrix: &[f64], n: usize) -> Result<Vec<f64>, JsValue> {
        if matrix.len() != n * n {
            return Err(JsValue::from_str("Matrix must be square"));
        }

        let mut q = vec![0.0; n * n];
        let mut r = vec![0.0; n * n];

        // Gram-Schmidt orthogonalization (same algorithm)
        for j in 0..n {
            // Copy column j from A to Q
            for i in 0..n {
                q[i * n + j] = matrix[i * n + j];
            }

            // Orthogonalize against previous columns
            for k in 0..j {
                // Compute dot product
                let mut dot = 0.0;
                for i in 0..n {
                    dot += q[i * n + j] * q[i * n + k];
                }
                r[k * n + j] = dot;

                // Subtract projection
                for i in 0..n {
                    q[i * n + j] -= dot * q[i * n + k];
                }
            }

            // Normalize column j
            let mut norm = 0.0;
            for i in 0..n {
                norm += q[i * n + j] * q[i * n + j];
            }
            norm = norm.sqrt();

            r[j * n + j] = norm;

            if norm > 1e-15 {
                for i in 0..n {
                    q[i * n + j] /= norm;
                }
            }
        }

        Ok(r)
    }

    /// Singular Value Decomposition (simplified for small matrices)
    /// 特異値分解(小行列用簡略版)
    #[wasm_bindgen]
    pub fn svd(
        &self,
        matrix: Vec<f64>,
        rows: usize,
        cols: usize,
    ) -> Result<js_sys::Object, JsValue> {
        if rows > self.max_matrix_size || cols > self.max_matrix_size {
            return Err(JsValue::from_str("Matrix too large for SVD"));
        }

        if matrix.len() != rows * cols {
            return Err(JsValue::from_str("Data size doesn't match dimensions"));
        }

        // For SVD, we compute A^T * A for eigenvalues (singular values squared)
        let at = self.transpose(&matrix, rows, cols);
        let ata = self.matmul(at, cols, rows, matrix.clone(), rows, cols)?;

        // Compute eigenvalues of A^T * A
        let eigenvals = self.eigenvalues(ata, cols)?;

        // Singular values are square roots of eigenvalues
        let singular_values: Vec<f64> = eigenvals
            .iter()
            .map(|&x| if x >= 0.0 { x.sqrt() } else { 0.0 })
            .collect();

        // Sort in descending order
        let mut sv_sorted = singular_values.clone();
        sv_sorted.sort_by(|a, b| b.partial_cmp(a).unwrap());

        // Create result object
        let result = js_sys::Object::new();
        js_sys::Reflect::set(
            &result,
            &"singular_values".into(),
            &js_sys::Array::from_iter(sv_sorted.iter().map(|&x| js_sys::Number::from(x))),
        )?;
        js_sys::Reflect::set(
            &result,
            &"rank".into(),
            &JsValue::from(sv_sorted.iter().filter(|&&x| x > 1e-10).count()),
        )?;
        js_sys::Reflect::set(
            &result,
            &"condition_number".into(),
            &JsValue::from(if sv_sorted.last().unwrap_or(&1e-15) > &1e-15 {
                sv_sorted[0] / sv_sorted.last().unwrap()
            } else {
                f64::INFINITY
            }),
        )?;

        Ok(result)
    }

    /// LU decomposition with partial pivoting
    /// 部分ピボッティング付きLU分解
    #[wasm_bindgen]
    pub fn lu_decomposition(&self, matrix: Vec<f64>, n: usize) -> Result<js_sys::Object, JsValue> {
        if n > self.max_matrix_size {
            return Err(JsValue::from_str("Matrix too large for LU decomposition"));
        }

        if matrix.len() != n * n {
            return Err(JsValue::from_str("Matrix must be square"));
        }

        let mut a = matrix;
        let mut p = (0..n).collect::<Vec<usize>>(); // Permutation vector

        // LU decomposition with partial pivoting
        for k in 0..n - 1 {
            // Find pivot
            let mut max_row = k;
            for i in k + 1..n {
                if a[i * n + k].abs() > a[max_row * n + k].abs() {
                    max_row = i;
                }
            }

            // Swap rows
            if max_row != k {
                for j in 0..n {
                    a.swap(k * n + j, max_row * n + j);
                }
                p.swap(k, max_row);
            }

            // Check for singular matrix
            if a[k * n + k].abs() < 1e-15 {
                return Err(JsValue::from_str("Matrix is singular"));
            }

            // Elimination
            for i in k + 1..n {
                let factor = a[i * n + k] / a[k * n + k];
                a[i * n + k] = factor; // Store L factor

                for j in k + 1..n {
                    a[i * n + j] -= factor * a[k * n + j];
                }
            }
        }

        // Separate L and U matrices
        let mut l = vec![0.0; n * n];
        let mut u = vec![0.0; n * n];

        for i in 0..n {
            for j in 0..n {
                if i == j {
                    l[i * n + j] = 1.0; // L diagonal
                    u[i * n + j] = a[i * n + j]; // U diagonal and upper
                } else if i > j {
                    l[i * n + j] = a[i * n + j]; // L lower
                } else {
                    u[i * n + j] = a[i * n + j]; // U upper
                }
            }
        }

        let result = js_sys::Object::new();
        js_sys::Reflect::set(
            &result,
            &"L".into(),
            &js_sys::Array::from_iter(l.iter().map(|&x| js_sys::Number::from(x))),
        )?;
        js_sys::Reflect::set(
            &result,
            &"U".into(),
            &js_sys::Array::from_iter(u.iter().map(|&x| js_sys::Number::from(x))),
        )?;
        js_sys::Reflect::set(
            &result,
            &"P".into(),
            &js_sys::Array::from_iter(p.iter().map(|&x| js_sys::Number::from(x as f64))),
        )?;

        Ok(result)
    }

    /// Compute matrix determinant using LU decomposition
    /// LU分解による行列式計算
    #[wasm_bindgen]
    pub fn determinant(&self, matrix: Vec<f64>, n: usize) -> Result<f64, JsValue> {
        if n > self.max_matrix_size {
            return Err(JsValue::from_str("Matrix too large"));
        }

        let lu_result = self.lu_decomposition(matrix, n)?;
        let u = js_sys::Reflect::get(&lu_result, &"U".into())?;
        let p = js_sys::Reflect::get(&lu_result, &"P".into())?;

        let u_array: js_sys::Array = u.into();
        let p_array: js_sys::Array = p.into();

        // Determinant is product of U diagonal elements
        let mut det = 1.0;
        for i in 0..n {
            let idx = i * n + i;
            let u_val: f64 = u_array.get(idx as u32).as_f64().unwrap();
            det *= u_val;
        }

        // Account for row swaps in permutation
        let mut swap_count = 0;
        let p_vec: Vec<usize> = (0..n)
            .map(|i| p_array.get(i as u32).as_f64().unwrap() as usize)
            .collect();
        for i in 0..n {
            if p_vec[i] != i {
                swap_count += 1;
            }
        }

        // Odd number of swaps changes sign
        if swap_count % 2 == 1 {
            det = -det;
        }

        Ok(det)
    }

    /// Matrix inverse using LU decomposition
    /// LU分解による逆行列計算
    #[wasm_bindgen]
    pub fn inverse(&self, matrix: Vec<f64>, n: usize) -> Result<Vec<f64>, JsValue> {
        if n > self.max_matrix_size {
            return Err(JsValue::from_str("Matrix too large for inversion"));
        }

        let det = self.determinant(matrix.clone(), n)?;
        if det.abs() < 1e-15 {
            return Err(JsValue::from_str("Matrix is singular (determinant ≈ 0)"));
        }

        let lu_result = self.lu_decomposition(matrix, n)?;
        let l = js_sys::Reflect::get(&lu_result, &"L".into())?;
        let u = js_sys::Reflect::get(&lu_result, &"U".into())?;
        let p = js_sys::Reflect::get(&lu_result, &"P".into())?;

        let l_array: js_sys::Array = l.into();
        let u_array: js_sys::Array = u.into();
        let p_array: js_sys::Array = p.into();

        let l_vec: Vec<f64> = (0..n * n)
            .map(|i| l_array.get(i as u32).as_f64().unwrap())
            .collect();
        let u_vec: Vec<f64> = (0..n * n)
            .map(|i| u_array.get(i as u32).as_f64().unwrap())
            .collect();
        let p_vec: Vec<usize> = (0..n)
            .map(|i| p_array.get(i as u32).as_f64().unwrap() as usize)
            .collect();

        let mut inverse = vec![0.0; n * n];

        // Solve for each column of the inverse
        for col in 0..n {
            // Create unit vector for column col
            let mut b = vec![0.0; n];
            b[col] = 1.0;

            // Apply permutation
            let mut pb = vec![0.0; n];
            for i in 0..n {
                pb[i] = b[p_vec[i]];
            }

            // Forward substitution (Ly = Pb)
            let mut y = vec![0.0; n];
            for i in 0..n {
                let mut sum = pb[i];
                for j in 0..i {
                    sum -= l_vec[i * n + j] * y[j];
                }
                y[i] = sum; // L has 1s on diagonal
            }

            // Backward substitution (Ux = y)
            let mut x = vec![0.0; n];
            for i in (0..n).rev() {
                let mut sum = y[i];
                for j in i + 1..n {
                    sum -= u_vec[i * n + j] * x[j];
                }
                x[i] = sum / u_vec[i * n + i];
            }

            // Store column in inverse matrix
            for i in 0..n {
                inverse[i * n + col] = x[i];
            }
        }

        Ok(inverse)
    }

    /// Compute matrix trace (sum of diagonal elements)
    /// 行列のトレース(対角要素の和)
    #[wasm_bindgen]
    pub fn trace(&self, matrix: Vec<f64>, n: usize) -> Result<f64, JsValue> {
        if matrix.len() != n * n {
            return Err(JsValue::from_str("Matrix must be square"));
        }

        let mut trace = 0.0;
        for i in 0..n {
            trace += matrix[i * n + i];
        }

        Ok(trace)
    }

    /// Compute matrix norm (Frobenius norm)
    /// 行列ノルム(フロベニウスノルム)
    #[wasm_bindgen]
    pub fn frobenius_norm(&self, matrix: Vec<f64>) -> f64 {
        matrix.iter().map(|&x| x * x).sum::<f64>().sqrt()
    }

    /// Matrix transpose
    /// 行列転置
    #[wasm_bindgen]
    pub fn transpose(&self, matrix: &[f64], rows: usize, cols: usize) -> Vec<f64> {
        let mut result = vec![0.0; rows * cols];

        for i in 0..rows {
            for j in 0..cols {
                result[j * rows + i] = matrix[i * cols + j];
            }
        }

        result
    }

    /// Solve linear system Ax = b using LU decomposition
    /// LU分解による連立一次方程式の解法
    #[wasm_bindgen]
    pub fn solve(&self, a: Vec<f64>, b: Vec<f64>, n: usize) -> Result<Vec<f64>, JsValue> {
        if n > self.max_matrix_size {
            return Err(JsValue::from_str("Matrix too large"));
        }

        if a.len() != n * n || b.len() != n {
            return Err(JsValue::from_str("Dimension mismatch"));
        }

        let lu_result = self.lu_decomposition(a, n)?;
        let l = js_sys::Reflect::get(&lu_result, &"L".into())?;
        let u = js_sys::Reflect::get(&lu_result, &"U".into())?;
        let p = js_sys::Reflect::get(&lu_result, &"P".into())?;

        let l_array: js_sys::Array = l.into();
        let u_array: js_sys::Array = u.into();
        let p_array: js_sys::Array = p.into();

        let l_vec: Vec<f64> = (0..n * n)
            .map(|i| l_array.get(i as u32).as_f64().unwrap())
            .collect();
        let u_vec: Vec<f64> = (0..n * n)
            .map(|i| u_array.get(i as u32).as_f64().unwrap())
            .collect();
        let p_vec: Vec<usize> = (0..n)
            .map(|i| p_array.get(i as u32).as_f64().unwrap() as usize)
            .collect();

        // Apply permutation to b
        let mut pb = vec![0.0; n];
        for i in 0..n {
            pb[i] = b[p_vec[i]];
        }

        // Forward substitution (Ly = Pb)
        let mut y = vec![0.0; n];
        for i in 0..n {
            let mut sum = pb[i];
            for j in 0..i {
                sum -= l_vec[i * n + j] * y[j];
            }
            y[i] = sum;
        }

        // Backward substitution (Ux = y)
        let mut x = vec![0.0; n];
        for i in (0..n).rev() {
            let mut sum = y[i];
            for j in i + 1..n {
                sum -= u_vec[i * n + j] * x[j];
            }
            x[i] = sum / u_vec[i * n + i];
        }

        Ok(x)
    }

    /// Check if matrix is symmetric
    /// 行列が対称かチェック
    #[wasm_bindgen]
    pub fn is_symmetric(&self, matrix: Vec<f64>, n: usize) -> Result<bool, JsValue> {
        if matrix.len() != n * n {
            return Err(JsValue::from_str("Matrix must be square"));
        }

        let tolerance = 1e-10;
        for i in 0..n {
            for j in 0..n {
                if (matrix[i * n + j] - matrix[j * n + i]).abs() > tolerance {
                    return Ok(false);
                }
            }
        }

        Ok(true)
    }

    /// Check if matrix is positive definite
    /// 行列が正定値かチェック
    #[wasm_bindgen]
    pub fn is_positive_definite(&self, matrix: Vec<f64>, n: usize) -> Result<bool, JsValue> {
        if !self.is_symmetric(matrix.clone(), n)? {
            return Ok(false);
        }

        let eigenvals = self.eigenvalues(matrix, n)?;
        Ok(eigenvals.iter().all(|&x| x > 1e-10))
    }

    /// Compute condition number of matrix
    /// 行列の条件数を計算
    #[wasm_bindgen]
    pub fn condition_number(
        &self,
        matrix: Vec<f64>,
        rows: usize,
        cols: usize,
    ) -> Result<f64, JsValue> {
        let svd_result = self.svd(matrix, rows, cols)?;
        let condition = js_sys::Reflect::get(&svd_result, &"condition_number".into())?;
        Ok(condition.as_f64().unwrap_or(f64::INFINITY))
    }

    /// Matrix rank estimation
    /// 行列ランクの推定
    #[wasm_bindgen]
    pub fn rank(&self, matrix: Vec<f64>, rows: usize, cols: usize) -> Result<usize, JsValue> {
        let svd_result = self.svd(matrix, rows, cols)?;
        let rank = js_sys::Reflect::get(&svd_result, &"rank".into())?;
        Ok(rank.as_f64().unwrap() as usize)
    }

    /// Compute pseudoinverse using SVD
    /// SVDによる擬似逆行列計算
    #[wasm_bindgen]
    pub fn pseudoinverse(
        &self,
        matrix: Vec<f64>,
        rows: usize,
        cols: usize,
    ) -> Result<Vec<f64>, JsValue> {
        if rows > self.max_matrix_size || cols > self.max_matrix_size {
            return Err(JsValue::from_str("Matrix too large"));
        }

        // For small matrices, use a simplified approach
        // In practice, full SVD would be needed for proper pseudoinverse

        if rows == cols {
            // Square matrix - try regular inverse
            match self.inverse(matrix.clone(), rows) {
                Ok(inv) => Ok(inv),
                Err(_) => {
                    // Fallback: return transpose for rank-deficient matrices
                    Ok(self.transpose(&matrix, rows, cols))
                }
            }
        } else {
            // Non-square: return Moore-Penrose approximation
            if rows > cols {
                // Tall matrix: (A^T A)^(-1) A^T
                let at = self.transpose(&matrix, rows, cols);
                let ata = self.matmul(at.clone(), cols, rows, matrix, rows, cols)?;
                let ata_inv = self.inverse(ata, cols)?;
                self.matmul(ata_inv, cols, cols, at, cols, rows)
            } else {
                // Wide matrix: A^T (A A^T)^(-1)
                let at = self.transpose(&matrix, rows, cols);
                let aat = self.matmul(matrix, rows, cols, at.clone(), cols, rows)?;
                let aat_inv = self.inverse(aat, rows)?;
                self.matmul(at, cols, rows, aat_inv, rows, rows)
            }
        }
    }

    /// Power iteration for largest eigenvalue
    /// べき乗法による最大固有値計算
    #[wasm_bindgen]
    pub fn largest_eigenvalue(
        &self,
        matrix: Vec<f64>,
        n: usize,
        max_iter: usize,
    ) -> Result<f64, JsValue> {
        if n > self.max_matrix_size {
            return Err(JsValue::from_str("Matrix too large"));
        }

        if matrix.len() != n * n {
            return Err(JsValue::from_str("Matrix must be square"));
        }

        // Initialize random vector
        let mut v: Vec<f64> = (0..n).map(|_| js_sys::Math::random() - 0.5).collect();

        // Normalize
        let norm = v.iter().map(|&x| x * x).sum::<f64>().sqrt();
        for x in &mut v {
            *x /= norm;
        }

        let mut eigenvalue = 0.0;

        for _ in 0..max_iter {
            // v = A * v
            let mut new_v = vec![0.0; n];
            for i in 0..n {
                for j in 0..n {
                    new_v[i] += matrix[i * n + j] * v[j];
                }
            }

            // Compute eigenvalue estimate (Rayleigh quotient)
            let numerator: f64 = new_v.iter().zip(v.iter()).map(|(a, b)| a * b).sum();
            let denominator: f64 = v.iter().map(|&x| x * x).sum();
            eigenvalue = numerator / denominator;

            // Normalize new_v
            let new_norm = new_v.iter().map(|&x| x * x).sum::<f64>().sqrt();
            for x in &mut new_v {
                *x /= new_norm;
            }

            // Check convergence
            let diff: f64 = new_v
                .iter()
                .zip(v.iter())
                .map(|(a, b)| (a - b) * (a - b))
                .sum::<f64>()
                .sqrt();
            if diff < 1e-10 {
                break;
            }

            v = new_v;
        }

        Ok(eigenvalue)
    }

    /// Matrix-vector product
    /// 行列ベクトル積
    #[wasm_bindgen]
    pub fn matvec(
        &self,
        matrix: Vec<f64>,
        vector: Vec<f64>,
        rows: usize,
        cols: usize,
    ) -> Result<Vec<f64>, JsValue> {
        if matrix.len() != rows * cols || vector.len() != cols {
            return Err(JsValue::from_str("Dimension mismatch"));
        }

        let mut result = vec![0.0; rows];

        for i in 0..rows {
            let mut sum = 0.0;
            for j in 0..cols {
                sum += matrix[i * cols + j] * vector[j];
            }
            result[i] = sum;
        }

        Ok(result)
    }

    /// Vector dot product
    /// ベクトル内積
    #[wasm_bindgen]
    pub fn dot(&self, a: Vec<f64>, b: Vec<f64>) -> Result<f64, JsValue> {
        if a.len() != b.len() {
            return Err(JsValue::from_str("Vector lengths must match"));
        }

        let dot_product = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        Ok(dot_product)
    }

    /// Vector L2 norm
    /// ベクトルL2ノルム
    #[wasm_bindgen]
    pub fn vector_norm(&self, vector: Vec<f64>) -> f64 {
        vector.iter().map(|&x| x * x).sum::<f64>().sqrt()
    }

    /// Normalize vector to unit length
    /// ベクトルを単位長に正規化
    #[wasm_bindgen]
    pub fn normalize_vector(&self, vector: Vec<f64>) -> Vec<f64> {
        let norm = self.vector_norm(vector.clone());
        if norm > 1e-15 {
            vector.iter().map(|&x| x / norm).collect()
        } else {
            vector
        }
    }

    /// Check maximum matrix size limit
    /// 最大行列サイズ制限をチェック
    #[wasm_bindgen]
    pub fn max_size(&self) -> usize {
        self.max_matrix_size
    }

    /// Get recommended chunk size for large operations
    /// 大きな演算の推奨チャンクサイズを取得
    #[wasm_bindgen]
    pub fn recommended_chunk_size(&self, total_elements: usize) -> usize {
        if total_elements <= 10000 {
            total_elements
        } else if total_elements <= 100000 {
            total_elements / 4
        } else {
            25000 // 25K elements max per chunk
        }
    }
}

/// Linear algebra utilities and helper functions
/// 線形代数ユーティリティとヘルパー関数
#[wasm_bindgen]
pub struct WasmLinAlgUtils;

#[wasm_bindgen]
impl WasmLinAlgUtils {
    /// Generate identity matrix
    /// 単位行列を生成
    #[wasm_bindgen]
    pub fn identity(n: usize) -> Vec<f64> {
        let mut matrix = vec![0.0; n * n];
        for i in 0..n {
            matrix[i * n + i] = 1.0;
        }
        matrix
    }

    /// Generate random matrix for testing
    /// テスト用ランダム行列を生成
    #[wasm_bindgen]
    pub fn random_matrix(rows: usize, cols: usize, scale: f64) -> Vec<f64> {
        (0..rows * cols)
            .map(|_| (js_sys::Math::random() - 0.5) * 2.0 * scale)
            .collect()
    }

    /// Generate symmetric random matrix
    /// 対称ランダム行列を生成
    #[wasm_bindgen]
    pub fn random_symmetric(n: usize, scale: f64) -> Vec<f64> {
        let mut matrix = vec![0.0; n * n];

        for i in 0..n {
            for j in i..n {
                let value = (js_sys::Math::random() - 0.5) * 2.0 * scale;
                matrix[i * n + j] = value;
                matrix[j * n + i] = value; // Symmetric
            }
        }

        matrix
    }

    /// Generate positive definite matrix
    /// 正定値行列を生成
    #[wasm_bindgen]
    pub fn random_positive_definite(n: usize, scale: f64) -> Vec<f64> {
        // Generate A and compute A^T * A
        let a = Self::random_matrix(n, n, scale);
        let at = WasmLinearAlgebra::new(1000).transpose(&a, n, n);
        WasmLinearAlgebra::new(1000)
            .matmul(at, n, n, a, n, n)
            .unwrap()
    }

    /// Check if values are approximately equal
    /// 値が近似的に等しいかチェック
    #[wasm_bindgen]
    pub fn approx_equal(a: f64, b: f64, tolerance: f64) -> bool {
        (a - b).abs() < tolerance
    }

    /// Check if matrices are approximately equal
    /// 行列が近似的に等しいかチェック
    #[wasm_bindgen]
    pub fn matrices_approx_equal(a: Vec<f64>, b: Vec<f64>, tolerance: f64) -> bool {
        if a.len() != b.len() {
            return false;
        }

        a.iter()
            .zip(b.iter())
            .all(|(x, y)| (x - y).abs() < tolerance)
    }
}

#[cfg(test)]
#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
mod tests {
    use super::*;
    use wasm_bindgen_test::*;

    #[wasm_bindgen_test]
    fn test_matrix_multiplication() {
        let linalg = WasmLinearAlgebra::new(100);

        let a = vec![1.0, 2.0, 3.0, 4.0]; // 2x2 matrix
        let b = vec![5.0, 6.0, 7.0, 8.0]; // 2x2 matrix

        let result = linalg.matmul(a, 2, 2, b, 2, 2).unwrap();

        // Expected: [19, 22, 43, 50]
        assert!((result[0] - 19.0).abs() < 1e-10);
        assert!((result[1] - 22.0).abs() < 1e-10);
        assert!((result[2] - 43.0).abs() < 1e-10);
        assert!((result[3] - 50.0).abs() < 1e-10);
    }

    #[wasm_bindgen_test]
    fn test_qr_decomposition() {
        let linalg = WasmLinearAlgebra::new(100);

        let matrix = vec![1.0, 2.0, 3.0, 4.0];

        let (q, r) = linalg.qr_decomposition(&matrix, 2).unwrap();

        // Verify Q is orthogonal: Q^T * Q = I
        let qt = linalg.transpose(&q, 2, 2);
        let qtq = linalg.matmul(qt, 2, 2, q.clone(), 2, 2).unwrap();

        // Check if qtq is approximately identity
        assert!((qtq[0] - 1.0).abs() < 1e-10); // [0,0]
        assert!(qtq[1].abs() < 1e-10); // [0,1]
        assert!(qtq[2].abs() < 1e-10); // [1,0]
        assert!((qtq[3] - 1.0).abs() < 1e-10); // [1,1]
    }

    #[wasm_bindgen_test]
    fn test_determinant() {
        let linalg = WasmLinearAlgebra::new(100);

        let matrix = vec![1.0, 2.0, 3.0, 4.0];

        let det = linalg.determinant(matrix, 2).unwrap();
        assert!((det - (-2.0)).abs() < 1e-10);
    }

    #[wasm_bindgen_test]
    fn test_inverse() {
        let linalg = WasmLinearAlgebra::new(100);

        let matrix = vec![2.0, 1.0, 1.0, 1.0];

        let inv = linalg.inverse(matrix.clone(), 2).unwrap();

        // Verify A * A^(-1) = I
        let product = linalg.matmul(matrix, 2, 2, inv, 2, 2).unwrap();
        assert!((product[0] - 1.0).abs() < 1e-10); // [0,0]
        assert!(product[1].abs() < 1e-10); // [0,1]
        assert!(product[2].abs() < 1e-10); // [1,0]
        assert!((product[3] - 1.0).abs() < 1e-10); // [1,1]
    }

    #[wasm_bindgen_test]
    fn test_solve_linear_system() {
        let linalg = WasmLinearAlgebra::new(100);

        let a = vec![2.0, 1.0, 1.0, 1.0];
        let b = vec![3.0, 2.0];

        let x = linalg.solve(a.clone(), b.clone(), 2).unwrap();

        // Verify A * x = b
        let ax = linalg.matvec(a, x, 2, 2).unwrap();
        assert!((ax[0] - b[0]).abs() < 1e-10);
        assert!((ax[1] - b[1]).abs() < 1e-10);
    }

    #[wasm_bindgen_test]
    fn test_utilities() {
        let identity = WasmLinAlgUtils::identity(3);
        assert_eq!(identity.len(), 9);
        assert!((identity[0] - 1.0).abs() < 1e-15);
        assert!((identity[4] - 1.0).abs() < 1e-15);
        assert!((identity[8] - 1.0).abs() < 1e-15);
        assert!(identity[1].abs() < 1e-15);

        let linalg = WasmLinearAlgebra::new(100);
        assert!(linalg.is_symmetric(identity, 3).unwrap());
        assert!(linalg.is_positive_definite(identity, 3).unwrap());
    }
}