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
//! Tensor Train Decomposition (TT-Decomposition)
//!
//! This module provides functionality for Tensor Train decomposition,
//! which is a type of tensor decomposition that compactly represents a
//! high-dimensional tensor as a sequence of low-dimensional tensors.

use crate::error::{LinalgError, LinalgResult};
use scirs2_core::ndarray::{Array, Array2, Array3, ArrayD, ArrayView, Dimension};
use scirs2_core::numeric::{Float, NumAssign, Zero};
use std::fmt::Debug;
use std::iter::Sum;

/// Represents a tensor in Tensor Train (TT) format.
///
/// A tensor train decomposition represents an N-dimensional tensor as a
/// sequence of 3D tensors (called TT-cores), where the first and last cores
/// have special boundary dimensions.
///
/// # Fields
///
/// * `cores` - Vector of 3D tensors (TT-cores)
/// * `ranks` - Vector of TT-ranks (including boundary ranks which are always 1)
/// * `shape` - Original tensor shape
#[derive(Debug, Clone)]
pub struct TensorTrain<A>
where
    A: Clone + Float + Debug,
{
    /// The TT-cores, each with shape (r_{i-1}, n_i, r_i)
    pub cores: Vec<Array3<A>>,
    /// The TT-ranks (including boundary ranks which are always 1)
    pub ranks: Vec<usize>,
    /// Original tensor shape
    pub shape: Vec<usize>,
}

impl<A> TensorTrain<A>
where
    A: Clone
        + Float
        + NumAssign
        + Zero
        + Debug
        + Sum
        + 'static
        + scirs2_core::ndarray::ScalarOperand
        + Send
        + Sync,
{
    /// Creates a new TensorTrain from the given cores, ranks, and original shape.
    ///
    /// # Arguments
    ///
    /// * `cores` - Vector of 3D tensors (TT-cores)
    /// * `ranks` - Vector of TT-ranks (including boundary ranks which should be 1)
    /// * `shape` - Original tensor shape
    ///
    /// # Returns
    ///
    /// * `TensorTrain` - A new TensorTrain instance
    pub fn new(cores: Vec<Array3<A>>, ranks: Vec<usize>, shape: Vec<usize>) -> LinalgResult<Self> {
        // Validate input
        if cores.is_empty() {
            return Err(LinalgError::ValueError(
                "Cores list cannot be empty".to_string(),
            ));
        }

        if ranks.len() != shape.len() + 1 {
            return Err(LinalgError::ShapeError(format!(
                "Ranks vector length ({}) must be 1 more than shape length ({})",
                ranks.len(),
                shape.len()
            )));
        }

        if ranks[0] != 1 || ranks[ranks.len() - 1] != 1 {
            return Err(LinalgError::ValueError(
                "First and last TT-ranks must be 1".to_string(),
            ));
        }

        if cores.len() != shape.len() {
            return Err(LinalgError::ShapeError(format!(
                "Number of _cores ({}) must match the shape length ({})",
                cores.len(),
                shape.len()
            )));
        }

        // Validate core dimensions
        for (i, core) in cores.iter().enumerate() {
            if core.ndim() != 3 {
                return Err(LinalgError::ShapeError(format!(
                    "Core {} must be 3-dimensional, got {} dimensions",
                    i,
                    core.ndim()
                )));
            }

            if core.shape()[0] != ranks[i] {
                return Err(LinalgError::ShapeError(format!(
                    "Core {} has first dimension {}, expected rank {}",
                    i,
                    core.shape()[0],
                    ranks[i]
                )));
            }

            if core.shape()[1] != shape[i] {
                return Err(LinalgError::ShapeError(format!(
                    "Core {} has second dimension {}, expected shape dimension {}",
                    i,
                    core.shape()[1],
                    shape[i]
                )));
            }

            if core.shape()[2] != ranks[i + 1] {
                return Err(LinalgError::ShapeError(format!(
                    "Core {} has third dimension {}, expected rank {}",
                    i,
                    core.shape()[2],
                    ranks[i + 1]
                )));
            }
        }

        Ok(TensorTrain {
            cores,
            ranks,
            shape,
        })
    }

    /// Reconstructs the full tensor from the TT-decomposition.
    ///
    /// # Returns
    ///
    /// * `ArrayD<A>` - The full tensor
    pub fn to_full(&self) -> LinalgResult<ArrayD<A>> {
        let n_dims = self.shape.len();

        // Start with the first core reshaped to remove the leading dimension of 1
        let mut result =
            ArrayD::zeros(scirs2_core::ndarray::IxDyn(&[self.shape[0], self.ranks[1]]));

        // Copy the first core data
        for i in 0..self.shape[0] {
            for j in 0..self.ranks[1] {
                result[[i, j]] = self.cores[0][[0, i, j]];
            }
        }

        // Multiply by each core in sequence
        for i in 1..n_dims {
            let core = &self.cores[i];

            // Get current result shape and prepare for contraction
            let currentshape = result.shape().to_vec();
            let current_rank = currentshape[currentshape.len() - 1];

            // Reshape result to prepare for contraction
            let result_flat = result
                .into_shape_with_order((
                    currentshape[..currentshape.len() - 1].iter().product(),
                    current_rank,
                ))
                .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;

            // Contract with the current core along the rank dimension
            let mut newshape = currentshape[..currentshape.len() - 1].to_vec();
            newshape.push(self.shape[i]);
            newshape.push(self.ranks[i + 1]);

            // Compute the contraction manually
            let mut result_contracted = Array::zeros(scirs2_core::ndarray::IxDyn(&newshape));

            // Handle different dimensionality cases
            match newshape.len() - 2 {
                0 => {
                    // Special case: no free indices
                    for k in 0..self.shape[i] {
                        for l in 0..self.ranks[i + 1] {
                            let mut sum = A::zero();
                            for r in 0..current_rank {
                                sum += result_flat[[0, r]] * core[[r, k, l]];
                            }
                            result_contracted[[k, l]] = sum;
                        }
                    }
                }
                1 => {
                    // One free index dimension
                    for idx1 in 0..newshape[0] {
                        for k in 0..self.shape[i] {
                            for l in 0..self.ranks[i + 1] {
                                let mut sum = A::zero();
                                for r in 0..current_rank {
                                    sum += result_flat[[idx1, r]] * core[[r, k, l]];
                                }
                                result_contracted[[idx1, k, l]] = sum;
                            }
                        }
                    }
                }
                2 => {
                    // Two free index dimensions
                    for idx1 in 0..newshape[0] {
                        for idx2 in 0..newshape[1] {
                            let flat_idx = idx1 * newshape[1] + idx2;
                            for k in 0..self.shape[i] {
                                for l in 0..self.ranks[i + 1] {
                                    let mut sum = A::zero();
                                    for r in 0..current_rank {
                                        sum += result_flat[[flat_idx, r]] * core[[r, k, l]];
                                    }
                                    result_contracted[[idx1, idx2, k, l]] = sum;
                                }
                            }
                        }
                    }
                }
                3 => {
                    // Three free index dimensions
                    for idx1 in 0..newshape[0] {
                        for idx2 in 0..newshape[1] {
                            for idx3 in 0..newshape[2] {
                                let stride2 = newshape[2];
                                let flat_idx = idx1 * newshape[1] * stride2 + idx2 * stride2 + idx3;
                                for k in 0..self.shape[i] {
                                    for l in 0..self.ranks[i + 1] {
                                        let mut sum = A::zero();
                                        for r in 0..current_rank {
                                            sum += result_flat[[flat_idx, r]] * core[[r, k, l]];
                                        }
                                        result_contracted[[idx1, idx2, idx3, k, l]] = sum;
                                    }
                                }
                            }
                        }
                    }
                }
                _ => {
                    // General case for more dimensions - less efficient but more flexible
                    // Create a helper function to iterate through all possible indices
                    fn visit_indices(
                        shape: &[usize],
                        current_indices: &mut Vec<usize>,
                        depth: usize,
                        callback: &mut dyn FnMut(&[usize]),
                    ) {
                        if depth == shape.len() {
                            callback(current_indices);
                            return;
                        }

                        for i in 0..shape[depth] {
                            current_indices.push(i);
                            visit_indices(shape, current_indices, depth + 1, callback);
                            current_indices.pop();
                        }
                    }

                    // Calculate flat index from multi-index
                    fn flat_index(indices: &[usize], shape: &[usize]) -> usize {
                        let mut idx = 0;
                        let mut stride = 1;

                        for i in (0..indices.len()).rev() {
                            idx += indices[i] * stride;
                            if i > 0 {
                                stride *= shape[i];
                            }
                        }

                        idx
                    }

                    let free_dims = newshape[..newshape.len() - 2].to_vec();
                    let mut indices = Vec::new();

                    let mut callback = |idx: &[usize]| {
                        let flat_idx = flat_index(idx, &free_dims);

                        for k in 0..self.shape[i] {
                            for l in 0..self.ranks[i + 1] {
                                let mut sum = A::zero();
                                for r in 0..current_rank {
                                    sum += result_flat[[flat_idx, r]] * core[[r, k, l]];
                                }

                                // Set the result in the new array
                                let mut idx_full = idx.to_vec();
                                idx_full.push(k);
                                idx_full.push(l);

                                // Convert to dynamic index
                                result_contracted[scirs2_core::ndarray::IxDyn(&idx_full)] = sum;
                            }
                        }
                    };

                    visit_indices(&free_dims, &mut indices, 0, &mut callback);
                }
            }

            result = result_contracted;
        }

        // Convert to the final tensor with the original shape
        let mut final_result = ArrayD::zeros(scirs2_core::ndarray::IxDyn(self.shape.as_slice()));

        // Special case if we only have 1 final rank dimension
        if self.ranks[n_dims] == 1 {
            // Prepare helper function to recursively set values in the final tensor
            fn set_values<A>(
                result: &ArrayD<A>,
                final_result: &mut ArrayD<A>,
                current_idx: &mut Vec<usize>,
                shape: &[usize],
                depth: usize,
            ) where
                A: Clone,
            {
                if depth == shape.len() {
                    // We've reached a leaf node, copy the value
                    let mut source_idx = current_idx.clone();
                    source_idx.push(0); // Add the final dimension which is always 0 when ranks[n_dims] == 1
                    final_result[current_idx.as_slice()] = result[source_idx.as_slice()].clone();
                    return;
                }

                // Recursively process each index at the current depth
                for i in 0..shape[depth] {
                    current_idx.push(i);
                    set_values(result, final_result, current_idx, shape, depth + 1);
                    current_idx.pop();
                }
            }

            let mut current_idx = Vec::new();
            set_values(
                &result,
                &mut final_result,
                &mut current_idx,
                self.shape.as_slice(),
                0,
            );
        } else {
            // Handle the general case (not needed for valid tensor trains where ranks[n_dims] == 1)
            return Err(LinalgError::ComputationError(
                "Last rank dimension must be 1 for a valid tensor train".to_string(),
            ));
        }

        Ok(final_result)
    }

    /// Evaluates the tensor train at a specific multi-index.
    ///
    /// This is more efficient than reconstructing the full tensor when
    /// only specific elements are needed.
    ///
    /// # Arguments
    ///
    /// * `indices` - The multi-index at which to evaluate the tensor
    ///
    /// # Returns
    ///
    /// * The tensor value at the given indices
    pub fn get(&self, indices: &[usize]) -> LinalgResult<A> {
        if indices.len() != self.shape.len() {
            return Err(LinalgError::ShapeError(format!(
                "Index length ({}) must match tensor dimensionality ({})",
                indices.len(),
                self.shape.len()
            )));
        }

        for (i, (&idx, &dim)) in indices.iter().zip(self.shape.iter()).enumerate() {
            if idx >= dim {
                return Err(LinalgError::ShapeError(format!(
                    "Index {} for dimension {} out of bounds ({})",
                    idx, i, dim
                )));
            }
        }

        // Start with the first core slice at the first index
        let mut result = self.cores[0]
            .slice(scirs2_core::ndarray::s![0, indices[0], ..])
            .to_owned();

        // Multiply by each core slice in sequence
        for i in 1..self.shape.len() {
            let core_slice = self.cores[i].slice(scirs2_core::ndarray::s![.., indices[i], ..]);

            // Matrix multiplication: result (1 x r_i) * core_slice (r_i x r_{i+1})
            let mut new_result = Array::zeros((1, core_slice.shape()[1]));

            for j in 0..core_slice.shape()[1] {
                let mut sum = A::zero();
                for k in 0..result.len() {
                    sum += result[k] * core_slice[[k, j]];
                }
                new_result[[0, j]] = sum;
            }

            let shape1 = new_result.shape()[1];
            result = new_result
                .into_shape_with_order(shape1)
                .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;
        }

        // The result should be a scalar
        if result.len() != 1 {
            return Err(LinalgError::ComputationError(
                "Final result should be a scalar".to_string(),
            ));
        }

        Ok(result[0])
    }

    /// Performs rounding (compression) of the tensor train.
    ///
    /// Reduces the TT-ranks of the decomposition to approximate the original
    /// tensor with a specified relative error tolerance.
    ///
    /// # Arguments
    ///
    /// * `epsilon` - Relative error tolerance for the approximation
    ///
    /// # Returns
    ///
    /// * A new `TensorTrain` with reduced ranks
    pub fn round(&self, epsilon: A) -> LinalgResult<Self> {
        if epsilon <= A::zero() {
            return Err(LinalgError::ValueError(
                "Epsilon must be positive".to_string(),
            ));
        }

        // Clone the cores to avoid modifying the original
        let mut cores = self.cores.clone();
        let mut ranks = self.ranks.clone();

        // Forward orthogonalization
        for i in 0..cores.len() - 1 {
            // Reshape core tensor to a matrix
            let core = &cores[i];
            let (r1, n, r2) = (core.shape()[0], core.shape()[1], core.shape()[2]);
            let core_mat = core
                .clone()
                .into_shape_with_order((r1 * n, r2))
                .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;

            // QR decomposition
            let (q, r) = qr_decomposition(&core_mat)?;

            // Get the shape value before moving q
            let qshape1 = q.shape()[1];

            // Update the current core
            cores[i] = q
                .into_shape_with_order((r1, n, qshape1))
                .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;

            // Update the next core
            let next_core = &cores[i + 1];
            let (next_r1, next_n, next_r2) = (
                next_core.shape()[0],
                next_core.shape()[1],
                next_core.shape()[2],
            );

            // Contract R with the next core
            let next_core_mat = next_core
                .clone()
                .into_shape_with_order((next_r1, next_n * next_r2))
                .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;

            let updated_next_core = r.dot(&next_core_mat);
            cores[i + 1] = updated_next_core
                .into_shape_with_order((r.shape()[0], next_n, next_r2))
                .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;
        }

        // Backward truncation and orthogonalization
        for i in (1..cores.len()).rev() {
            // Reshape core tensor to a matrix
            let core = &cores[i];
            let (r1, n, r2) = (core.shape()[0], core.shape()[1], core.shape()[2]);
            let core_mat = core
                .clone()
                .into_shape_with_order((r1, n * r2))
                .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;

            // SVD decomposition with truncation
            let (u, s, vt) = svd_with_truncation(&core_mat, epsilon)?;

            // Update ranks
            ranks[i] = u.shape()[1];

            // Update the current core
            cores[i] = vt
                .into_shape_with_order((u.shape()[1], n, r2))
                .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;

            // Update the previous core
            let prev_core = &cores[i - 1];
            let (prev_r1, prev_n, prev_r2) = (
                prev_core.shape()[0],
                prev_core.shape()[1],
                prev_core.shape()[2],
            );

            // Contract u*s with the previous core
            let u_s = Array2::from_diag(&s).dot(&u.t());
            let prev_core_mat = prev_core
                .clone()
                .into_shape_with_order((prev_r1 * prev_n, prev_r2))
                .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;

            let updated_prev_core = prev_core_mat.dot(&u_s);
            cores[i - 1] = updated_prev_core
                .into_shape_with_order((prev_r1, prev_n, u.shape()[1]))
                .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;
        }

        TensorTrain::new(cores, ranks, self.shape.clone())
    }
}

/// Decomposes a tensor into tensor train format.
///
/// This function implements the TT-SVD algorithm for tensor train decomposition.
/// The algorithm works by sequentially decomposing the tensor along each dimension
/// using SVD.
///
/// # Arguments
///
/// * `tensor` - The input tensor to decompose
/// * `max_rank` - Maximum TT-rank (if None, no rank truncation is performed)
/// * `epsilon` - Relative error tolerance for rank truncation (if None, no truncation is performed)
///
/// # Returns
///
/// * A `TensorTrain` object containing the decomposition
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::{array, ArrayD, IxDyn};
/// use scirs2_linalg::tensor_contraction::tensor_train::tensor_train_decomposition;
///
/// // Create a 2x3x2 tensor
/// let tensor = array![[[1.0_f64, 2.0], [3.0, 4.0], [5.0, 6.0]],
///                      [[7.0, 8.0], [9.0, 10.0], [11.0, 12.0]]];
///
/// // Decompose into tensor train format with maximum rank 2
/// let tt = tensor_train_decomposition(&tensor.view(), Some(2), None).expect("Operation failed");
///
/// // The TT-ranks should be [1, 2, 2, 1] (boundary ranks are always 1)
/// assert_eq!(tt.ranks, vec![1, 2, 2, 1]);
///
/// // Reconstruct the tensor and check the approximation
/// let reconstructed = tt.to_full().expect("Operation failed");
/// for i in 0..2 {
///     for j in 0..3 {
///         for k in 0..2 {
///             assert!((reconstructed[[i, j, k]] - tensor[[i, j, k]]).abs() < 1e-10);
///         }
///     }
/// }
/// ```
#[allow(dead_code)]
pub fn tensor_train_decomposition<A, D>(
    tensor: &ArrayView<A, D>,
    max_rank: Option<usize>,
    epsilon: Option<A>,
) -> LinalgResult<TensorTrain<A>>
where
    A: Clone
        + Float
        + NumAssign
        + Zero
        + Debug
        + Sum
        + 'static
        + scirs2_core::ndarray::ScalarOperand
        + Send
        + Sync,
    D: Dimension,
{
    // Convert to dynamic dimensionality
    let tensor_dyn = tensor.to_owned().into_dyn();
    let shape = tensor.shape().to_vec();
    let ndim = shape.len();

    // Initialize ranks vector (boundary ranks are always 1)
    let mut ranks = vec![1];
    ranks.resize(ndim + 1, 1);

    // Initialize cores vector
    let mut cores = Vec::with_capacity(ndim);

    // Create a copy of the tensor to work with
    let mut curr_tensor = tensor_dyn;

    // For each dimension, perform SVD and extract the core
    for k in 0..ndim - 1 {
        // Reshape tensor to a matrix where rows correspond to all previous dimensions
        // and current dimension, and columns to all remaining dimensions
        let rows = ranks[k] * shape[k];
        let cols: usize = shape.iter().skip(k + 1).product();

        let tensor_mat = curr_tensor
            .clone()
            .into_shape_with_order((rows, cols))
            .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;

        // Perform SVD with truncation if requested
        let (u, s, vt) = match (max_rank, epsilon) {
            (Some(max_r), Some(eps)) => {
                let (u, s, vt) = svd_with_truncation_and_max_rank(&tensor_mat, eps, max_r)?;
                ranks[k + 1] = u.shape()[1];
                (u, s, vt)
            }
            (Some(max_r), None) => {
                let (u, s, vt) = svd_with_max_rank(&tensor_mat, max_r)?;
                ranks[k + 1] = u.shape()[1];
                (u, s, vt)
            }
            (None, Some(eps)) => {
                let (u, s, vt) = svd_with_truncation(&tensor_mat, eps)?;
                ranks[k + 1] = u.shape()[1];
                (u, s, vt)
            }
            (None, None) => {
                let (u, s, vt) = svd(&tensor_mat)?;
                ranks[k + 1] = s.len();
                (u, s, vt)
            }
        };

        // Create the current core tensor
        let core = u
            .into_shape_with_order((ranks[k], shape[k], ranks[k + 1]))
            .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;

        cores.push(core);

        // Update the current tensor for the next iteration
        let s_vt = Array2::from_diag(&s).dot(&vt);
        curr_tensor = s_vt
            .into_shape_with_order(
                std::iter::once(ranks[k + 1])
                    .chain(shape.iter().skip(k + 1).copied())
                    .collect::<Vec<_>>(),
            )
            .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;
    }

    // Add the last core
    let last_core = curr_tensor
        .into_shape_with_order((ranks[ndim - 1], shape[ndim - 1], ranks[ndim]))
        .map_err(|e| LinalgError::ComputationError(format!("Reshape error: {}", e)))?;

    cores.push(last_core);

    // Create the TensorTrain object
    TensorTrain::new(cores, ranks, shape)
}

// Helper function for full SVD decomposition
#[allow(dead_code)]
fn svd<A>(matrix: &Array2<A>) -> LinalgResult<(Array2<A>, Array1<A>, Array2<A>)>
where
    A: Clone
        + Float
        + NumAssign
        + Zero
        + Debug
        + Sum
        + 'static
        + scirs2_core::ndarray::ScalarOperand
        + Send
        + Sync,
{
    use crate::decomposition::svd as svd_decomp;

    // Convert to view and call with full_matrices=false
    let matrix_view = matrix.view();
    svd_decomp(&matrix_view, false, None)
}

// Helper function for SVD with truncation based on relative error
#[allow(dead_code)]
fn svd_with_truncation<A>(
    matrix: &Array2<A>,
    epsilon: A,
) -> LinalgResult<(Array2<A>, Array1<A>, Array2<A>)>
where
    A: Clone
        + Float
        + NumAssign
        + Zero
        + Debug
        + Sum
        + 'static
        + scirs2_core::ndarray::ScalarOperand
        + Send
        + Sync,
{
    let (u, s, vt) = svd(matrix)?;

    // Normalize singular values to get relative importance
    let s_norm = if !s.is_empty() && s[0] > A::zero() {
        s.mapv(|x| x / s[0])
    } else {
        s.clone()
    };

    // Find the number of singular values to keep based on epsilon
    let mut rank = 0;
    for (i, &val) in s_norm.iter().enumerate() {
        if val < epsilon {
            rank = i;
            break;
        }
        rank = i + 1;
    }

    // Ensure at least one singular value is kept
    rank = rank.max(1);

    // Truncate the matrices
    let u_trunc = u.slice(scirs2_core::ndarray::s![.., ..rank]).to_owned();
    let s_trunc = s.slice(scirs2_core::ndarray::s![..rank]).to_owned();
    let vt_trunc = vt.slice(scirs2_core::ndarray::s![..rank, ..]).to_owned();

    Ok((u_trunc, s_trunc, vt_trunc))
}

// Helper function for SVD with maximum rank constraint
#[allow(dead_code)]
fn svd_with_max_rank<A>(
    matrix: &Array2<A>,
    max_rank: usize,
) -> LinalgResult<(Array2<A>, Array1<A>, Array2<A>)>
where
    A: Clone
        + Float
        + NumAssign
        + Zero
        + Debug
        + Sum
        + 'static
        + scirs2_core::ndarray::ScalarOperand
        + Send
        + Sync,
{
    let (u, s, vt) = svd(matrix)?;

    // Compute the _rank to use (minimum of max_rank and the number of singular values)
    let rank = max_rank.min(s.len());

    // Truncate the matrices
    let u_trunc = u.slice(scirs2_core::ndarray::s![.., ..rank]).to_owned();
    let s_trunc = s.slice(scirs2_core::ndarray::s![..rank]).to_owned();
    let vt_trunc = vt.slice(scirs2_core::ndarray::s![..rank, ..]).to_owned();

    Ok((u_trunc, s_trunc, vt_trunc))
}

// Helper function for SVD with both truncation and maximum rank
#[allow(dead_code)]
fn svd_with_truncation_and_max_rank<A>(
    matrix: &Array2<A>,
    epsilon: A,
    max_rank: usize,
) -> LinalgResult<(Array2<A>, Array1<A>, Array2<A>)>
where
    A: Clone
        + Float
        + NumAssign
        + Zero
        + Debug
        + Sum
        + 'static
        + scirs2_core::ndarray::ScalarOperand
        + Send
        + Sync,
{
    let (u, s, vt) = svd(matrix)?;

    // Normalize singular values to get relative importance
    let s_norm = if !s.is_empty() && s[0] > A::zero() {
        s.mapv(|x| x / s[0])
    } else {
        s.clone()
    };

    // Find the number of singular values to keep based on epsilon
    let mut rank = 0;
    for (i, &val) in s_norm.iter().enumerate() {
        if val < epsilon {
            rank = i;
            break;
        }
        rank = i + 1;
    }

    // Ensure at least one singular value is kept
    rank = rank.max(1);

    // Apply max_rank constraint
    rank = rank.min(max_rank);

    // Truncate the matrices
    let u_trunc = u.slice(scirs2_core::ndarray::s![.., ..rank]).to_owned();
    let s_trunc = s.slice(scirs2_core::ndarray::s![..rank]).to_owned();
    let vt_trunc = vt.slice(scirs2_core::ndarray::s![..rank, ..]).to_owned();

    Ok((u_trunc, s_trunc, vt_trunc))
}

// Helper function for QR decomposition
#[allow(dead_code)]
fn qr_decomposition<A>(matrix: &Array2<A>) -> LinalgResult<(Array2<A>, Array2<A>)>
where
    A: Clone
        + Float
        + NumAssign
        + Zero
        + Debug
        + Sum
        + 'static
        + scirs2_core::ndarray::ScalarOperand
        + Send
        + Sync,
{
    use crate::decomposition::qr;

    // Convert to view and call QR
    let matrix_view = matrix.view();
    let (q, r) = qr(&matrix_view, None)?;
    Ok((q, r))
}

/// Helper type for more convenient type definition
pub type Array1<A> = Array<A, scirs2_core::ndarray::Ix1>;

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

    #[test]
    fn test_tensor_train_decomposition_3d() {
        // Create a 2x3x2 tensor
        let tensor = array![
            [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]],
            [[7.0, 8.0], [9.0, 10.0], [11.0, 12.0]]
        ];

        // Decompose with full rank (without truncation)
        let tt = tensor_train_decomposition(&tensor.view(), None, None).expect("Operation failed");

        // Verify the shape and ranks
        assert_eq!(tt.shape, vec![2, 3, 2]);
        assert_eq!(tt.ranks.len(), 4); // n+1 ranks for n-dimensional tensor
        assert_eq!(tt.ranks[0], 1); // First rank is always 1
        assert_eq!(tt.ranks[3], 1); // Last rank is always 1

        // Each core should have the correct shape:
        // G_1: 1 x n_1 x r_1
        // G_i: r_{i-1} x n_i x r_i
        // G_d: r_{d-1} x n_d x 1
        for i in 0..tt.cores.len() {
            let core = &tt.cores[i];
            assert_eq!(core.shape()[0], tt.ranks[i]);
            assert_eq!(core.shape()[1], tt.shape[i]);
            assert_eq!(core.shape()[2], tt.ranks[i + 1]);
        }

        // Reconstruct the tensor
        let reconstructed = tt.to_full().expect("Operation failed");

        // Check that the reconstruction matches the original tensor
        for i in 0..2 {
            for j in 0..3 {
                for k in 0..2 {
                    assert_abs_diff_eq!(
                        reconstructed[[i, j, k]],
                        tensor[[i, j, k]],
                        epsilon = 1e-10
                    );
                }
            }
        }
    }

    #[test]
    #[ignore = "SVD fails for small matrices due to unimplemented eigendecomposition"]
    fn test_tensor_train_decomposition_with_truncation() {
        // Create a 4x3x2x2 tensor with some structure
        let mut tensor = ArrayD::<f64>::zeros(scirs2_core::ndarray::IxDyn(&[4, 3, 2, 2]));

        // Fill with some values (outer product-like pattern for simplicity)
        for i in 0..4 {
            for j in 0..3 {
                for k in 0..2 {
                    for l in 0..2 {
                        tensor[[i, j, k, l]] =
                            (i + 1) as f64 * (j + 1) as f64 * (k + 1) as f64 * (l + 1) as f64;
                    }
                }
            }
        }

        // Decompose with rank truncation (max rank 2)
        let tt =
            tensor_train_decomposition(&tensor.view(), Some(2), None).expect("Operation failed");

        // Check that no rank exceeds 2
        for &r in &tt.ranks {
            assert!(r <= 2);
        }

        // Reconstruct the tensor
        let reconstructed = tt.to_full().expect("Operation failed");

        // For low rank tensors like this one, we should still get a good approximation
        for i in 0..4 {
            for j in 0..3 {
                for k in 0..2 {
                    for l in 0..2 {
                        assert_abs_diff_eq!(
                            reconstructed[[i, j, k, l]],
                            tensor[[i, j, k, l]],
                            epsilon = 1e-6
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn test_get_tensor_element() {
        // Create a 2x3x2 tensor
        let tensor = array![
            [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]],
            [[7.0, 8.0], [9.0, 10.0], [11.0, 12.0]]
        ];

        // Decompose without truncation
        let tt = tensor_train_decomposition(&tensor.view(), None, None).expect("Operation failed");

        // Test getting individual elements
        for i in 0..2 {
            for j in 0..3 {
                for k in 0..2 {
                    let value = tt.get(&[i, j, k]).expect("Operation failed");
                    assert_abs_diff_eq!(value, tensor[[i, j, k]], epsilon = 1e-10);
                }
            }
        }
    }

    #[test]
    #[ignore = "SVD fails for small matrices due to unimplemented eigendecomposition"]
    fn test_round_tensor_train() {
        // Create a 3x4x3x2 tensor
        let mut tensor = ArrayD::<f64>::zeros(scirs2_core::ndarray::IxDyn(&[3, 4, 3, 2]));

        // Fill with values
        for i in 0..3 {
            for j in 0..4 {
                for k in 0..3 {
                    for l in 0..2 {
                        tensor[[i, j, k, l]] =
                            (i + 1) as f64 * (j + 1) as f64 * (k + 1) as f64 * (l + 1) as f64;
                    }
                }
            }
        }

        // Decompose with full rank
        let tt = tensor_train_decomposition(&tensor.view(), None, None).expect("Operation failed");

        // Round the tensor train with various epsilon values
        for epsilon in [1e-8, 1e-4, 1e-2].iter() {
            let rounded_tt = tt.round(*epsilon).expect("Operation failed");

            // Reconstruct the tensor
            let reconstructed = rounded_tt.to_full().expect("Operation failed");

            // Check that the error is within bounds
            let mut max_error = 0.0;
            let norm = tensor.mapv(|x| x * x).sum().sqrt();

            for i in 0..3 {
                for j in 0..4 {
                    for k in 0..3 {
                        for l in 0..2 {
                            let error = (reconstructed[[i, j, k, l]] - tensor[[i, j, k, l]]).abs();
                            max_error = max_error.max(error);
                        }
                    }
                }
            }

            // Relative error should be less than epsilon
            let relative_error = max_error / norm;
            assert!(relative_error <= *epsilon || relative_error <= 1e-10);
        }
    }
}