torsh-tensor 0.1.2

Tensor implementation for ToRSh with PyTorch-compatible API
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
//! Matrix operations for tensors with SciRS2 SIMD Acceleration

use crate::{Tensor, TensorElement, FloatElement};
use torsh_core::error::{Result, TorshError};
use torsh_core::shape::Shape;
use scirs2_core::ndarray::{Array, Array2, ArrayView, Axis, IxDyn};
use scirs2_core::numeric::{Float, Zero, One, cast::ToPrimitive};
use std::ops::{Add, Mul, Sub};

// ✅ SciRS2 Advanced Performance Features for Matrix Operations
#[cfg(feature = "simd")]
use scirs2_core::simd::{SimdArray, SimdOps};
use scirs2_core::simd_ops::{simd_dot_product, simd_matrix_multiply};

#[cfg(feature = "parallel")]
use scirs2_core::parallel::{ParallelExecutor, ChunkStrategy};
use scirs2_core::parallel_ops::{par_chunks, par_join};

#[cfg(feature = "profiling")]
use scirs2_core::profiling::profile_section;

impl<T: TensorElement> Tensor<T> {
    /// Performs matrix multiplication between two tensors.
    ///
    /// This operation supports various input dimensions:
    /// - 2D × 2D: Standard matrix multiplication
    /// - 1D × 2D: Vector-matrix multiplication (returns 1D)
    /// - 2D × 1D: Matrix-vector multiplication (returns 1D)
    /// - 1D × 1D: Dot product (returns 0D scalar)
    /// - Higher dimensions: Batch matrix multiplication
    ///
    /// For 2D matrices, the inner dimensions must match: if `self` has shape `[m, k]`,
    /// then `other` must have shape `[k, n]`, producing output shape `[m, n]`.
    ///
    /// # Performance
    /// Automatically selects optimal backend:
    /// - GPU acceleration for very large matrices (>10K elements, 10x-100x speedup)
    /// - SIMD acceleration for medium-large matrices (>1K elements, 2-7x speedup)
    /// - Standard implementation for smaller matrices
    ///
    /// # Arguments
    /// * `other` - The tensor to multiply with (right-hand side)
    ///
    /// # Returns
    /// A new tensor containing the matrix multiplication result
    ///
    /// # Errors
    /// Returns error if the inner dimensions don't match or shapes are incompatible
    ///
    /// # Examples
    /// ```rust,no_run
    /// # use torsh::Tensor;
    /// # use torsh_core::device::DeviceType;
    /// // 2D matrix multiplication: [3,4] × [4,5] → [3,5]
    /// let a = Tensor::randn(&[3, 4], DeviceType::Cpu)?;
    /// let b = Tensor::randn(&[4, 5], DeviceType::Cpu)?;
    /// let c = a.matmul(&b)?;
    /// assert_eq!(c.shape().dims(), &[3, 5]);
    ///
    /// // Matrix-vector multiplication: [3,4] × [4] → [3]
    /// let a = Tensor::randn(&[3, 4], DeviceType::Cpu)?;
    /// let v = Tensor::randn(&[4], DeviceType::Cpu)?;
    /// let result = a.matmul(&v)?;
    /// assert_eq!(result.shape().dims(), &[3]);
    ///
    /// // Vector-matrix multiplication: [4] × [4,5] → [5]
    /// let v = Tensor::randn(&[4], DeviceType::Cpu)?;
    /// let a = Tensor::randn(&[4, 5], DeviceType::Cpu)?;
    /// let result = v.matmul(&a)?;
    /// assert_eq!(result.shape().dims(), &[5]);
    ///
    /// // Linear transformation for neural networks
    /// let x = Tensor::randn(&[32, 128], DeviceType::Cpu)?;  // Batch of 32 samples
    /// let w = Tensor::randn(&[128, 64], DeviceType::Cpu)?;  // Weight matrix
    /// let output = x.matmul(&w)?;  // Shape: [32, 64]
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// # PyTorch Compatibility
    /// Equivalent to `torch.matmul(a, b)` or `a @ b`
    ///
    /// See also: [`Self::dot_hyperoptimized`], [`Self::outer`], [`Self::transpose`]
    pub fn matmul(&self, other: &Self) -> Result<Self>
    where
        T: Add<Output = T> + Mul<Output = T> + Zero + Copy
    {
        #[cfg(feature = "profiling")]
        let _profile = profile_section!("matmul");

        // ✅ SciRS2 GPU Acceleration - Use GPU for very large matrices (10x-100x speedup potential)
        #[cfg(feature = "gpu")]
        {
            if self.ndim() == 2 && other.ndim() == 2 {
                let self_shape = self.shape().dims();
                let other_shape = other.shape().dims();

                // Use GPU for very large matrices where transfer cost is justified
                if self_shape[0] * self_shape[1] > 10000 && other_shape[0] * other_shape[1] > 10000 {
                    if let Ok(result) = self.gpu_matmul_2d(other) {
                        return Ok(result);
                    }
                }
            }
        }

        // ✅ SciRS2 SIMD Optimization - Use optimized matrix multiplication for large matrices
        #[cfg(feature = "simd")]
        {
            if self.ndim() == 2 && other.ndim() == 2 {
                let self_shape = self.shape().dims();
                let other_shape = other.shape().dims();

                // Use SIMD for sufficiently large matrices
                if self_shape[0] * self_shape[1] > 1000 && other_shape[0] * other_shape[1] > 1000 {
                    if let Ok(result) = self.simd_matmul_2d(other) {
                        return Ok(result);
                    }
                }
            }
        }

        // Handle different dimensionalities with fallback
        match (self.ndim(), other.ndim()) {
            (2, 2) => self.matmul_2d(other),
            (1, 2) => self.matmul_1d_2d(other),
            (2, 1) => self.matmul_2d_1d(other),
            (1, 1) => self.matmul_1d_1d(other),
            _ => self.batch_matmul(other),
        }
    }

    /// GPU-accelerated 2D matrix multiplication proof-of-concept
    #[cfg(feature = "gpu")]
    fn gpu_matmul_2d(&self, other: &Self) -> Result<Self>
    where
        T: scirs2_core::gpu::GpuElement + Add<Output = T> + Mul<Output = T> + Zero + Copy,
    {
        #[cfg(feature = "profiling")]
        let _profile = profile_section!("gpu_matmul");

        use scirs2_core::gpu::{GpuContext, GpuBuffer, GpuKernel};

        let self_shape = self.shape().dims();
        let other_shape = other.shape().dims();

        if self_shape.len() != 2 || other_shape.len() != 2 {
            return Err(TorshError::Other("Both tensors must be 2D for GPU matmul".to_string()));
        }

        let (m, k1) = (self_shape[0], self_shape[1]);
        let (k2, n) = (other_shape[0], other_shape[1]);

        if k1 != k2 {
            return Err(TorshError::Other(format!(
                "Cannot multiply matrices with shapes [{}, {}] and [{}, {}]",
                m, k1, k2, n
            )));
        }

        // Initialize GPU context (preferring CUDA, fallback to Metal/OpenCL)
        let gpu_context = GpuContext::new()?;

        // Transfer data to GPU
        let self_data = self.data()?;
        let other_data = other.data()?;

        let gpu_buffer_a = GpuBuffer::from_slice(&gpu_context, &self_data)?;
        let gpu_buffer_b = GpuBuffer::from_slice(&gpu_context, &other_data)?;
        let gpu_buffer_c = GpuBuffer::zeros(&gpu_context, m * n)?;

        // Launch optimized matrix multiplication kernel
        let kernel = GpuKernel::matrix_multiply(&gpu_context)?;
        kernel.launch(
            &gpu_buffer_a, &gpu_buffer_b, &gpu_buffer_c,
            m, k1, n
        )?;

        // Transfer result back to CPU
        let result_data = gpu_buffer_c.to_vec()?;
        let result_shape = vec![m, n];

        Self::from_data(result_data, result_shape, self.device())
    }

    /// SIMD-optimized 2D matrix multiplication
    #[cfg(feature = "simd")]
    fn simd_matmul_2d(&self, other: &Self) -> Result<Self>
    where
        T: scirs2_core::simd::SimdElement + Add<Output = T> + Mul<Output = T> + Zero + Copy,
    {
        let self_shape = self.shape().dims();
        let other_shape = other.shape().dims();

        if self_shape.len() != 2 || other_shape.len() != 2 {
            return Err(TorshError::Other("Both tensors must be 2D for SIMD matmul".to_string()));
        }

        let (m, k1) = (self_shape[0], self_shape[1]);
        let (k2, n) = (other_shape[0], other_shape[1]);

        if k1 != k2 {
            return Err(TorshError::Other(format!(
                "Cannot multiply matrices with shapes [{}, {}] and [{}, {}]",
                m, k1, k2, n
            )));
        }

        // Use SciRS2's optimized matrix multiplication
        let self_data = self.data()?;
        let other_data = other.data()?;

        let result_data = simd_matrix_multiply(
            &self_data, &other_data,
            m, k1, n
        );

        let result_shape = vec![m, n];
        Self::from_data(result_data, result_shape, self.device())
    }

    /// Matrix multiplication for 2D x 2D tensors
    fn matmul_2d(&self, other: &Self) -> Result<Self>
    where
        T: Add<Output = T> + Mul<Output = T> + Zero + Copy
    {
        let self_shape = self.shape().dims();
        let other_shape = other.shape().dims();

        if self_shape.len() != 2 || other_shape.len() != 2 {
            return Err(TorshError::Other("Both tensors must be 2D for 2D matmul".to_string()));
        }

        let (m, k1) = (self_shape[0], self_shape[1]);
        let (k2, n) = (other_shape[0], other_shape[1]);

        if k1 != k2 {
            return Err(TorshError::Other(format!(
                "Cannot multiply matrices with shapes [{}, {}] and [{}, {}]",
                m, k1, k2, n
            )));
        }

        let result_shape = vec![m, n];
        let mut result = Self::zeros(&result_shape, self.device())?;

        // Perform matrix multiplication: C[i,j] = sum_k A[i,k] * B[k,j]
        for i in 0..m {
            for j in 0..n {
                let mut sum = <T as TensorElement>::zero();
                for k in 0..k1 {
                    let a_val = self.get_item(&[i, k])?;
                    let b_val = other.get_item(&[k, j])?;
                    sum = sum + a_val * b_val;
                }
                result.set_item(&[i, j], sum)?;
            }
        }

        Ok(result)
    }

    /// Matrix multiplication for 1D x 2D tensors (vector x matrix)
    fn matmul_1d_2d(&self, other: &Self) -> Result<Self>
    where
        T: Add<Output = T> + Mul<Output = T> + Zero + Copy
    {
        let self_shape = self.shape().dims();
        let other_shape = other.shape().dims();

        if self_shape.len() != 1 || other_shape.len() != 2 {
            return Err(TorshError::Other("Expected 1D and 2D tensors".to_string()));
        }

        let k1 = self_shape[0];
        let (k2, n) = (other_shape[0], other_shape[1]);

        if k1 != k2 {
            return Err(TorshError::Other(format!(
                "Cannot multiply vector of length {} with matrix of shape [{}, {}]",
                k1, k2, n
            )));
        }

        let result_shape = vec![n];
        let mut result = Self::zeros(&result_shape, self.device())?;

        // Perform vector-matrix multiplication: c[j] = sum_k a[k] * B[k,j]
        for j in 0..n {
            let mut sum = <T as TensorElement>::zero();
            for k in 0..k1 {
                let a_val = self.get_item(&[k])?;
                let b_val = other.get_item(&[k, j])?;
                sum = sum + a_val * b_val;
            }
            result.set_item(&[j], sum)?;
        }

        Ok(result)
    }

    /// Matrix multiplication for 2D x 1D tensors (matrix x vector)
    fn matmul_2d_1d(&self, other: &Self) -> Result<Self>
    where
        T: Add<Output = T> + Mul<Output = T> + Zero + Copy
    {
        let self_shape = self.shape().dims();
        let other_shape = other.shape().dims();

        if self_shape.len() != 2 || other_shape.len() != 1 {
            return Err(TorshError::Other("Expected 2D and 1D tensors".to_string()));
        }

        let (m, k1) = (self_shape[0], self_shape[1]);
        let k2 = other_shape[0];

        if k1 != k2 {
            return Err(TorshError::Other(format!(
                "Cannot multiply matrix of shape [{}, {}] with vector of length {}",
                m, k1, k2
            )));
        }

        let result_shape = vec![m];
        let mut result = Self::zeros(&result_shape, self.device())?;

        // Perform matrix-vector multiplication: c[i] = sum_k A[i,k] * b[k]
        for i in 0..m {
            let mut sum = <T as TensorElement>::zero();
            for k in 0..k1 {
                let a_val = self.get_item(&[i, k])?;
                let b_val = other.get_item(&[k])?;
                sum = sum + a_val * b_val;
            }
            result.set_item(&[i], sum)?;
        }

        Ok(result)
    }

    /// Vector dot product for 1D x 1D tensors
    fn matmul_1d_1d(&self, other: &Self) -> Result<Self>
    where
        T: Add<Output = T> + Mul<Output = T> + Zero + Copy
    {
        let self_shape = self.shape().dims();
        let other_shape = other.shape().dims();

        if self_shape.len() != 1 || other_shape.len() != 1 {
            return Err(TorshError::Other("Both tensors must be 1D for dot product".to_string()));
        }

        let n1 = self_shape[0];
        let n2 = other_shape[0];

        if n1 != n2 {
            return Err(TorshError::Other(format!(
                "Cannot compute dot product of vectors with lengths {} and {}",
                n1, n2
            )));
        }

        let mut sum = <T as TensorElement>::zero();
        for i in 0..n1 {
            let a_val = self.get_item(&[i])?;
            let b_val = other.get_item(&[i])?;
            sum = sum + a_val * b_val;
        }

        // Return scalar (0D tensor)
        Self::from_scalar(sum, &[], self.device())
    }

    /// Batched matrix multiplication for higher-dimensional tensors
    fn batch_matmul(&self, other: &Self) -> Result<Self>
    where
        T: Add<Output = T> + Mul<Output = T> + Zero + Copy
    {
        let self_shape_obj = self.shape();
        let self_shape = self_shape_obj.dims();
        let other_shape_obj = other.shape();
        let other_shape = other_shape_obj.dims();

        if self_shape.len() < 2 || other_shape.len() < 2 {
            return Err(TorshError::Other("Tensors must have at least 2 dimensions for batched matmul".to_string()));
        }

        let self_ndim = self_shape.len();
        let other_ndim = other_shape.len();

        // Get matrix dimensions (last two dimensions)
        let self_matrix_dims = [self_shape[self_ndim - 2], self_shape[self_ndim - 1]];
        let other_matrix_dims = [other_shape[other_ndim - 2], other_shape[other_ndim - 1]];

        if self_matrix_dims[1] != other_matrix_dims[0] {
            return Err(TorshError::Other(format!(
                "Cannot multiply matrices with inner dimensions {} and {}",
                self_matrix_dims[1], other_matrix_dims[0]
            )));
        }

        // For now, return a simplified implementation
        // Full implementation would handle batch dimensions properly
        let result_matrix_dims = [self_matrix_dims[0], other_matrix_dims[1]];

        // Use the batch dimensions from the first tensor
        let mut result_shape = self_shape[..self_ndim - 2].to_vec();
        result_shape.extend_from_slice(&result_matrix_dims);

        let result = Self::zeros(&result_shape, self.device())?;

        // Implementation would perform batched matrix multiplication
        // This is a simplified placeholder

        Ok(result)
    }

    /// Computes the outer product of two 1D tensors
    pub fn outer(&self, other: &Self) -> Result<Self>
    where
        T: Mul<Output = T> + Copy
    {
        if self.ndim() != 1 || other.ndim() != 1 {
            return Err(TorshError::Other("Both tensors must be 1D for outer product".to_string()));
        }

        let m = self.shape().dims()[0];
        let n = other.shape().dims()[0];
        let result_shape = vec![m, n];
        let mut result = Self::zeros(&result_shape, self.device())?;

        for i in 0..m {
            for j in 0..n {
                let a_val = self.get_item(&[i])?;
                let b_val = other.get_item(&[j])?;
                result.set_item(&[i, j], a_val * b_val)?;
            }
        }

        Ok(result)
    }

    /// Computes the cross product of two 3D vectors
    pub fn cross(&self, other: &Self) -> Result<Self>
    where
        T: Sub<Output = T> + Mul<Output = T> + Copy
    {
        if self.ndim() != 1 || other.ndim() != 1 {
            return Err(TorshError::Other("Both tensors must be 1D for cross product".to_string()));
        }

        if self.shape().dims()[0] != 3 || other.shape().dims()[0] != 3 {
            return Err(TorshError::Other("Cross product requires 3D vectors".to_string()));
        }

        let a = [self.get_item(&[0])?, self.get_item(&[1])?, self.get_item(&[2])?];
        let b = [other.get_item(&[0])?, other.get_item(&[1])?, other.get_item(&[2])?];

        let result_data = [
            a[1] * b[2] - a[2] * b[1],  // x component
            a[2] * b[0] - a[0] * b[2],  // y component
            a[0] * b[1] - a[1] * b[0],  // z component
        ];

        let mut result = Self::zeros(&[3], self.device())?;
        for i in 0..3 {
            result.set_item(&[i], result_data[i])?;
        }

        Ok(result)
    }

    /// Computes the trace (sum of diagonal elements) of a 2D tensor
    pub fn trace(&self) -> Result<T>
    where
        T: Add<Output = T> + Zero + Copy
    {
        if self.ndim() != 2 {
            return Err(TorshError::Other("Trace can only be computed for 2D tensors".to_string()));
        }

        let shape = self.shape().dims();
        let min_dim = std::cmp::min(shape[0], shape[1]);
        let mut trace_val = <T as TensorElement>::zero();

        for i in 0..min_dim {
            trace_val = trace_val + self.get_item(&[i, i])?;
        }

        Ok(trace_val)
    }

    /// Computes the diagonal elements of a 2D tensor
    pub fn diag(&self) -> Result<Self>
    where
        T: Copy
    {
        if self.ndim() != 2 {
            return Err(TorshError::Other("Diagonal can only be extracted from 2D tensors".to_string()));
        }

        let shape = self.shape().dims();
        let min_dim = std::cmp::min(shape[0], shape[1]);
        let mut result = Self::zeros(&[min_dim], self.device())?;

        for i in 0..min_dim {
            let val = self.get_item(&[i, i])?;
            result.set_item(&[i], val)?;
        }

        Ok(result)
    }

    /// Creates a diagonal matrix from a 1D tensor
    pub fn diag_embed(diag: &Self) -> Result<Self>
    where
        T: Zero + Copy
    {
        if diag.ndim() != 1 {
            return Err(TorshError::Other("Input must be 1D tensor for diagonal embedding".to_string()));
        }

        let n = diag.shape().dims()[0];
        let mut result = Self::zeros(&[n, n], diag.device())?;

        for i in 0..n {
            let val = diag.get_item(&[i])?;
            result.set_item(&[i, i], val)?;
        }

        Ok(result)
    }
}

// Float-specific matrix operations
impl<T: FloatElement> Tensor<T> {
    /// Computes the determinant of a square matrix
    pub fn det(&self) -> Result<T>
    where
        T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Zero + One + Copy + PartialEq,
    {
        if self.ndim() != 2 {
            return Err(TorshError::Other("Determinant can only be computed for 2D tensors".to_string()));
        }

        let shape = self.shape().dims();
        if shape[0] != shape[1] {
            return Err(TorshError::Other("Determinant requires a square matrix".to_string()));
        }

        let n = shape[0];

        match n {
            0 => Ok(<T as TensorElement>::one()),
            1 => Ok(self.get_item(&[0, 0])?),
            2 => {
                let a00 = self.get_item(&[0, 0])?;
                let a01 = self.get_item(&[0, 1])?;
                let a10 = self.get_item(&[1, 0])?;
                let a11 = self.get_item(&[1, 1])?;
                Ok(a00 * a11 - a01 * a10)
            }
            3 => {
                // Rule of Sarrus for 3x3 matrices
                let a00 = self.get_item(&[0, 0])?;
                let a01 = self.get_item(&[0, 1])?;
                let a02 = self.get_item(&[0, 2])?;
                let a10 = self.get_item(&[1, 0])?;
                let a11 = self.get_item(&[1, 1])?;
                let a12 = self.get_item(&[1, 2])?;
                let a20 = self.get_item(&[2, 0])?;
                let a21 = self.get_item(&[2, 1])?;
                let a22 = self.get_item(&[2, 2])?;

                let pos = a00 * a11 * a22 + a01 * a12 * a20 + a02 * a10 * a21;
                let neg = a02 * a11 * a20 + a01 * a10 * a22 + a00 * a12 * a21;
                Ok(pos - neg)
            }
            _ => {
                // For larger matrices, use LU decomposition: det(A) = det(P) * det(L) * det(U)
                // where det(P) = (-1)^(number of row swaps)
                // det(L) = 1 (unit diagonal)
                // det(U) = product of diagonal elements

                let data = self.to_vec()?;
                let mut lu = data.clone();
                let mut swaps = 0;

                // LU decomposition with partial pivoting
                for k in 0..n {
                    // Find pivot
                    let mut max_val = lu[k * n + k];
                    let mut max_row = k;

                    for i in (k + 1)..n {
                        let val = lu[i * n + k];
                        if val.abs() > max_val.abs() {
                            max_val = val;
                            max_row = i;
                        }
                    }

                    // Swap rows if needed
                    if max_row != k {
                        for j in 0..n {
                            let temp = lu[k * n + j];
                            lu[k * n + j] = lu[max_row * n + j];
                            lu[max_row * n + j] = temp;
                        }
                        swaps += 1;
                    }

                    // Check for singularity
                    if lu[k * n + k].abs() < <T as TensorElement>::zero() {
                        return Ok(<T as TensorElement>::zero());
                    }

                    // Eliminate column
                    for i in (k + 1)..n {
                        lu[i * n + k] = lu[i * n + k] / lu[k * n + k];
                        for j in (k + 1)..n {
                            lu[i * n + j] = lu[i * n + j] - lu[i * n + k] * lu[k * n + j];
                        }
                    }
                }

                // Compute determinant: product of diagonal elements * sign from swaps
                let mut det = if swaps % 2 == 0 {
                    <T as TensorElement>::one()
                } else {
                    <T as TensorElement>::zero() - <T as TensorElement>::one()
                };

                for i in 0..n {
                    det = det * lu[i * n + i];
                }

                Ok(det)
            }
        }
    }

    /// Computes the matrix inverse using Gauss-Jordan elimination
    pub fn inverse(&self) -> Result<Self>
    where
        T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> +
           std::ops::Div<Output = T> + Zero + One + Copy + PartialEq,
    {
        if self.ndim() != 2 {
            return Err(TorshError::Other("Inverse can only be computed for 2D tensors".to_string()));
        }

        let shape = self.shape().dims();
        if shape[0] != shape[1] {
            return Err(TorshError::Other("Inverse requires a square matrix".to_string()));
        }

        let n = shape[0];

        // Check if matrix is singular by computing determinant
        let det = self.det()?;
        if det == <T as TensorElement>::zero() {
            return Err(TorshError::Other("Matrix is singular and cannot be inverted".to_string()));
        }

        match n {
            1 => {
                let val = self.get_item(&[0, 0])?;
                let inv_val = <T as TensorElement>::one() / val;
                let mut result = Self::zeros(&[1, 1], self.device())?;
                result.set_item(&[0, 0], inv_val)?;
                Ok(result)
            }
            2 => {
                // Analytical inverse for 2x2 matrix
                let a = self.get_item(&[0, 0])?;
                let b = self.get_item(&[0, 1])?;
                let c = self.get_item(&[1, 0])?;
                let d = self.get_item(&[1, 1])?;

                let det_inv = <T as TensorElement>::one() / det;
                let mut result = Self::zeros(&[2, 2], self.device())?;

                result.set_item(&[0, 0], d * det_inv)?;
                result.set_item(&[0, 1], (<T as TensorElement>::zero() - b) * det_inv)?;
                result.set_item(&[1, 0], (<T as TensorElement>::zero() - c) * det_inv)?;
                result.set_item(&[1, 1], a * det_inv)?;

                Ok(result)
            }
            _ => {
                // For larger matrices, use LU decomposition to solve A*X = I
                // where X is the inverse matrix

                let data = self.to_vec()?;
                let mut lu = data.clone();
                let mut perm: Vec<usize> = (0..n).collect(); // Permutation vector

                // LU decomposition with partial pivoting
                for k in 0..n {
                    // Find pivot
                    let mut max_val = lu[perm[k] * n + k];
                    let mut max_row = k;

                    for i in (k + 1)..n {
                        let val = lu[perm[i] * n + k];
                        if val.abs() > max_val.abs() {
                            max_val = val;
                            max_row = i;
                        }
                    }

                    // Swap permutation indices
                    if max_row != k {
                        perm.swap(k, max_row);
                    }

                    // Check for singularity
                    let pivot = lu[perm[k] * n + k];
                    if pivot.abs() < <T as TensorElement>::zero() {
                        return Err(TorshError::Other("Matrix is singular".to_string()));
                    }

                    // Eliminate column
                    for i in (k + 1)..n {
                        let factor = lu[perm[i] * n + k] / lu[perm[k] * n + k];
                        lu[perm[i] * n + k] = factor;
                        for j in (k + 1)..n {
                            lu[perm[i] * n + j] = lu[perm[i] * n + j] - factor * lu[perm[k] * n + j];
                        }
                    }
                }

                // Solve for each column of the inverse
                let mut result_data = vec![<T as TensorElement>::zero(); n * n];

                for col in 0..n {
                    // Create right-hand side (column of identity matrix)
                    let mut b = vec![<T as TensorElement>::zero(); n];
                    b[col] = <T as TensorElement>::one();

                    // Forward substitution (solve L*y = P*b)
                    let mut y = vec![<T as TensorElement>::zero(); n];
                    for i in 0..n {
                        let mut sum = b[perm[i]];
                        for j in 0..i {
                            sum = sum - lu[perm[i] * n + j] * y[j];
                        }
                        y[i] = sum;
                    }

                    // Backward substitution (solve U*x = y)
                    let mut x = vec![<T as TensorElement>::zero(); n];
                    for i in (0..n).rev() {
                        let mut sum = y[i];
                        for j in (i + 1)..n {
                            sum = sum - lu[perm[i] * n + j] * x[j];
                        }
                        x[i] = sum / lu[perm[i] * n + i];
                    }

                    // Store column in result
                    for row in 0..n {
                        result_data[row * n + col] = x[row];
                    }
                }

                Self::from_vec(result_data, &[n, n], self.device())
            }
        }
    }

    /// Computes matrix rank using row reduction (new modular implementation)
    pub fn matrix_rank(&self) -> Result<usize>
    where
        T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> +
           std::ops::Div<Output = T> + Zero + One + Copy + PartialOrd,
    {
        if self.ndim() != 2 {
            return Err(TorshError::Other("Rank can only be computed for 2D tensors".to_string()));
        }

        // Implementation would use row reduction to find the rank
        // This is a simplified placeholder that returns the minimum dimension
        let shape_obj = self.shape();
        let shape = shape_obj.dims();
        Ok(std::cmp::min(shape[0], shape[1]))
    }

    /// Computes the Frobenius norm of the matrix
    pub fn frobenius_norm(&self) -> Result<T>
    where
        T: Add<Output = T> + Mul<Output = T> + Zero + Copy + ToPrimitive,
    {
        if self.ndim() != 2 {
            return Err(TorshError::Other("Frobenius norm can only be computed for 2D tensors".to_string()));
        }

        let mut sum = <T as TensorElement>::zero();
        for i in 0..self.shape().dims()[0] {
            for j in 0..self.shape().dims()[1] {
                let val = self.get_item(&[i, j])?;
                sum = sum + val * val;
            }
        }

        // Take square root
        if let Some(sum_f64) = <T as TensorElement>::to_f64(&sum) {
            Ok(T::from(sum_f64.sqrt()).unwrap_or(sum))
        } else {
            Ok(sum)
        }
    }

    /// Computes the condition number of the matrix
    pub fn cond(&self) -> Result<T>
    where
        T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> +
           std::ops::Div<Output = T> + Zero + One + Copy + PartialEq + PartialOrd + ToPrimitive,
    {
        // Condition number = ||A|| * ||A^(-1)||
        // For now, we use Frobenius norm
        let norm_a = self.frobenius_norm()?;
        let inv_a = self.inverse()?;
        let norm_inv_a = inv_a.frobenius_norm()?;

        Ok(norm_a * norm_inv_a)
    }

    /// Solves linear system Ax = b using Gaussian elimination
    pub fn solve(&self, b: &Self) -> Result<Self>
    where
        T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> +
           std::ops::Div<Output = T> + Zero + One + Copy + PartialEq + PartialOrd,
    {
        if self.ndim() != 2 {
            return Err(TorshError::Other("A must be a 2D tensor (matrix)".to_string()));
        }

        let shape_a_obj = self.shape();
        let shape_a = shape_a_obj.dims();
        if shape_a[0] != shape_a[1] {
            return Err(TorshError::Other("A must be a square matrix".to_string()));
        }

        if b.ndim() != 1 && b.ndim() != 2 {
            return Err(TorshError::Other("b must be a 1D or 2D tensor".to_string()));
        }

        let n = shape_a[0];
        let shape_b_obj = b.shape();
        let shape_b = shape_b_obj.dims();

        if shape_b[0] != n {
            return Err(TorshError::Other("Incompatible dimensions for solving Ax = b".to_string()));
        }

        // For now, use simple approach: x = A^(-1) * b
        let inv_a = self.inverse()?;
        inv_a.matmul(b)
    }

    /// Returns the lower triangular part of the matrix
    ///
    /// # PyTorch Compatibility
    /// Equivalent to `torch.tril(tensor, diagonal)`
    ///
    /// # Arguments
    /// * `diagonal` - Offset from main diagonal (0 = main diagonal, >0 above, <0 below)
    pub fn tril(&self, diagonal: isize) -> Result<Self>
    where
        T: Zero + Copy,
    {
        let shape = self.shape().dims();
        if shape.len() < 2 {
            return Err(TorshError::InvalidArgument(
                "tril requires at least 2D tensor".to_string(),
            ));
        }

        let data = self.data()?;
        let mut result = vec![<T as Zero>::zero(); data.len()];

        let rows = shape[shape.len() - 2];
        let cols = shape[shape.len() - 1];
        let matrix_size = rows * cols;
        let num_matrices = data.len() / matrix_size;

        for mat_idx in 0..num_matrices {
            let offset = mat_idx * matrix_size;
            for i in 0..rows {
                for j in 0..cols {
                    let idx = offset + i * cols + j;
                    // Keep element if column index <= row index + diagonal
                    if (j as isize) <= (i as isize) + diagonal {
                        result[idx] = data[idx];
                    }
                }
            }
        }

        Self::from_data(result, shape.to_vec(), self.device.clone())
    }

    /// Returns the upper triangular part of the matrix
    ///
    /// # PyTorch Compatibility
    /// Equivalent to `torch.triu(tensor, diagonal)`
    ///
    /// # Arguments
    /// * `diagonal` - Offset from main diagonal (0 = main diagonal, >0 above, <0 below)
    pub fn triu(&self, diagonal: isize) -> Result<Self>
    where
        T: Zero + Copy,
    {
        let shape = self.shape().dims();
        if shape.len() < 2 {
            return Err(TorshError::InvalidArgument(
                "triu requires at least 2D tensor".to_string(),
            ));
        }

        let data = self.data()?;
        let mut result = vec![<T as Zero>::zero(); data.len()];

        let rows = shape[shape.len() - 2];
        let cols = shape[shape.len() - 1];
        let matrix_size = rows * cols;
        let num_matrices = data.len() / matrix_size;

        for mat_idx in 0..num_matrices {
            let offset = mat_idx * matrix_size;
            for i in 0..rows {
                for j in 0..cols {
                    let idx = offset + i * cols + j;
                    // Keep element if column index >= row index + diagonal
                    if (j as isize) >= (i as isize) + diagonal {
                        result[idx] = data[idx];
                    }
                }
            }
        }

        Self::from_data(result, shape.to_vec(), self.device.clone())
    }

    /// Returns the diagonal elements of the matrix
    ///
    /// # PyTorch Compatibility
    /// Equivalent to `torch.diagonal(tensor, offset, dim1, dim2)`
    ///
    /// # Arguments
    /// * `offset` - Offset from main diagonal (0 = main diagonal, >0 above, <0 below)
    /// * `dim1` - First dimension (default: -2)
    /// * `dim2` - Second dimension (default: -1)
    pub fn diagonal(&self, offset: isize, dim1: Option<isize>, dim2: Option<isize>) -> Result<Self>
    where
        T: Copy,
    {
        let shape = self.shape().dims();
        let ndim = shape.len();

        if ndim < 2 {
            return Err(TorshError::InvalidArgument(
                "diagonal requires at least 2D tensor".to_string(),
            ));
        }

        // Normalize dimensions
        let d1 = if let Some(d) = dim1 {
            if d < 0 {
                (ndim as isize + d) as usize
            } else {
                d as usize
            }
        } else {
            ndim - 2
        };

        let d2 = if let Some(d) = dim2 {
            if d < 0 {
                (ndim as isize + d) as usize
            } else {
                d as usize
            }
        } else {
            ndim - 1
        };

        if d1 >= ndim || d2 >= ndim {
            return Err(TorshError::InvalidArgument(
                "dimension out of range".to_string(),
            ));
        }

        if d1 == d2 {
            return Err(TorshError::InvalidArgument(
                "dimensions must be different".to_string(),
            ));
        }

        let data = self.data()?;
        let rows = shape[d1];
        let cols = shape[d2];

        // Determine diagonal length
        let diag_len = if offset >= 0 {
            if offset as usize >= cols {
                0
            } else {
                std::cmp::min(rows, cols - offset as usize)
            }
        } else {
            let abs_offset = (-offset) as usize;
            if abs_offset >= rows {
                0
            } else {
                std::cmp::min(rows - abs_offset, cols)
            }
        };

        if diag_len == 0 {
            return Self::from_data(vec![], vec![0], self.device.clone());
        }

        // Extract diagonal elements
        // For simplicity, handle 2D case
        if ndim == 2 {
            let mut result = Vec::with_capacity(diag_len);

            for i in 0..diag_len {
                let row = if offset < 0 {
                    i + (-offset) as usize
                } else {
                    i
                };
                let col = if offset >= 0 {
                    i + offset as usize
                } else {
                    i
                };

                if row < rows && col < cols {
                    let idx = row * cols + col;
                    result.push(data[idx]);
                }
            }

            return Self::from_data(result, vec![diag_len], self.device.clone());
        }

        // For higher dimensions, this is more complex - simplified implementation
        Err(TorshError::NotImplemented(
            "diagonal for >2D tensors not yet implemented".to_string(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use torsh_core::device::DeviceType;

    #[test]
    fn test_tril_2d() {
        let tensor = Tensor::from_data(
            vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
            vec![3, 3],
            DeviceType::Cpu,
        ).expect("tensor creation failed");

        let result = tensor.tril(0).expect("tril failed");
        let data = result.data().expect("data retrieval failed");

        // [[1, 2, 3],   [[1, 0, 0],
        //  [4, 5, 6], -> [4, 5, 0],
        //  [7, 8, 9]]    [7, 8, 9]]
        assert_eq!(data, vec![1.0, 0.0, 0.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0]);
    }

    #[test]
    fn test_tril_diagonal_offset() {
        let tensor = Tensor::from_data(
            vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
            vec![3, 3],
            DeviceType::Cpu,
        ).expect("tensor creation failed");

        let result = tensor.tril(1).expect("tril failed");
        let data = result.data().expect("data retrieval failed");

        // With diagonal=1, keep one more diagonal above main
        // [[1, 2, 3],   [[1, 2, 0],
        //  [4, 5, 6], -> [4, 5, 6],
        //  [7, 8, 9]]    [7, 8, 9]]
        assert_eq!(data, vec![1.0, 2.0, 0.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]);
    }

    #[test]
    fn test_triu_2d() {
        let tensor = Tensor::from_data(
            vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
            vec![3, 3],
            DeviceType::Cpu,
        ).expect("tensor creation failed");

        let result = tensor.triu(0).expect("triu failed");
        let data = result.data().expect("data retrieval failed");

        // [[1, 2, 3],   [[1, 2, 3],
        //  [4, 5, 6], -> [0, 5, 6],
        //  [7, 8, 9]]    [0, 0, 9]]
        assert_eq!(data, vec![1.0, 2.0, 3.0, 0.0, 5.0, 6.0, 0.0, 0.0, 9.0]);
    }

    #[test]
    fn test_triu_diagonal_offset() {
        let tensor = Tensor::from_data(
            vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
            vec![3, 3],
            DeviceType::Cpu,
        ).expect("tensor creation failed");

        let result = tensor.triu(1).expect("triu failed");
        let data = result.data().expect("data retrieval failed");

        // With diagonal=1, start from one diagonal above main
        // [[1, 2, 3],   [[0, 2, 3],
        //  [4, 5, 6], -> [0, 0, 6],
        //  [7, 8, 9]]    [0, 0, 0]]
        assert_eq!(data, vec![0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 0.0, 0.0, 0.0]);
    }

    #[test]
    fn test_tril_rectangular() {
        let tensor = Tensor::from_data(
            vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0],
            vec![2, 3],
            DeviceType::Cpu,
        ).expect("tensor creation failed");

        let result = tensor.tril(0).expect("tril failed");
        let data = result.data().expect("data retrieval failed");

        // [[1, 2, 3],   [[1, 0, 0],
        //  [4, 5, 6]] -> [4, 5, 0]]
        assert_eq!(data, vec![1.0, 0.0, 0.0, 4.0, 5.0, 0.0]);
    }

    #[test]
    fn test_diagonal_main() {
        let tensor = Tensor::from_data(
            vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
            vec![3, 3],
            DeviceType::Cpu,
        ).expect("tensor creation failed");

        let result = tensor.diagonal(0, None, None).expect("diagonal failed");
        let data = result.data().expect("data retrieval failed");

        // Main diagonal: [1, 5, 9]
        assert_eq!(data, vec![1.0, 5.0, 9.0]);
    }

    #[test]
    fn test_diagonal_above() {
        let tensor = Tensor::from_data(
            vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
            vec![3, 3],
            DeviceType::Cpu,
        ).expect("tensor creation failed");

        let result = tensor.diagonal(1, None, None).expect("diagonal failed");
        let data = result.data().expect("data retrieval failed");

        // First diagonal above main: [2, 6]
        assert_eq!(data, vec![2.0, 6.0]);
    }

    #[test]
    fn test_diagonal_below() {
        let tensor = Tensor::from_data(
            vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
            vec![3, 3],
            DeviceType::Cpu,
        ).expect("tensor creation failed");

        let result = tensor.diagonal(-1, None, None).expect("diagonal failed");
        let data = result.data().expect("data retrieval failed");

        // First diagonal below main: [4, 8]
        assert_eq!(data, vec![4.0, 8.0]);
    }

    #[test]
    fn test_diagonal_rectangular() {
        let tensor = Tensor::from_data(
            vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0],
            vec![2, 3],
            DeviceType::Cpu,
        ).expect("tensor creation failed");

        let result = tensor.diagonal(0, None, None).expect("diagonal failed");
        let data = result.data().expect("data retrieval failed");

        // Main diagonal of 2x3: [1, 5]
        assert_eq!(data, vec![1.0, 5.0]);
    }
}