scirs2-core 0.4.3

Core utilities and common functionality for SciRS2 (scirs2-core)
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
//! Matrix operations for ndarray
//!
//! This module provides matrix creation and manipulation operations similar to
//! those found in `NumPy`/SciPy, such as identity, diagonal, block, and other
//! specialized matrix operations.

use ::ndarray::{Array, ArrayView, Ix1, Ix2};
use num_traits::{One, Zero};

/// Create an identity matrix
///
/// # Arguments
///
/// * `n` - Number of rows and columns in the square identity matrix
///
/// # Returns
///
/// An nxn identity matrix
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray_ext::matrix::eye;
///
/// let id3 = eye::<f64>(3);
/// assert_eq!(id3.shape(), &[3, 3]);
/// assert_eq!(id3[[0, 0]], 1.0);
/// assert_eq!(id3[[1, 1]], 1.0);
/// assert_eq!(id3[[2, 2]], 1.0);
/// assert_eq!(id3[[0, 1]], 0.0);
/// ```
#[allow(dead_code)]
pub fn eye<T>(n: usize) -> Array<T, Ix2>
where
    T: Clone + Zero + One,
{
    let mut result = Array::<T, Ix2>::zeros((n, n));

    for i in 0..n {
        result[[i, i]] = T::one();
    }

    result
}

/// Create a matrix with ones on the given diagonal and zeros elsewhere
///
/// # Arguments
///
/// * `n` - Number of rows
/// * `m` - Number of columns
/// * `k` - Diagonal offset (0 for main diagonal, positive for above, negative for below)
///
/// # Returns
///
/// An n x m matrix with ones on the specified diagonal
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray_ext::matrix::eye_offset;
///
/// // 3x3 matrix with ones on the main diagonal (k=0)
/// let id3 = eye_offset::<f64>(3, 3, 0);
/// assert_eq!(id3.shape(), &[3, 3]);
/// assert_eq!(id3[[0, 0]], 1.0);
/// assert_eq!(id3[[1, 1]], 1.0);
/// assert_eq!(id3[[2, 2]], 1.0);
///
/// // 3x4 matrix with ones on the first superdiagonal (k=1)
/// let super_diag = eye_offset::<f64>(3, 4, 1);
/// assert_eq!(super_diag.shape(), &[3, 4]);
/// assert_eq!(super_diag[[0, 1]], 1.0);
/// assert_eq!(super_diag[[1, 2]], 1.0);
/// assert_eq!(super_diag[[2, 3]], 1.0);
/// ```
#[allow(dead_code)]
pub fn eye_offset<T>(n: usize, m: usize, k: isize) -> Array<T, Ix2>
where
    T: Clone + Zero + One,
{
    let mut result = Array::<T, Ix2>::zeros((n, m));

    // Determine the start and end points for the diagonal
    let start_i = if k > 0 { 0 } else { (-k) as usize };
    let start_j = if k < 0 { 0 } else { k as usize };

    let diag_len = std::cmp::min(n - start_i, m - start_j);

    for d in 0..diag_len {
        result[[start_i + d, start_j + d]] = T::one();
    }

    result
}

/// Construct a diagonal matrix from a 1D array
///
/// # Arguments
///
/// * `_diagvalues` - The values to place on the diagonal
///
/// # Returns
///
/// A square matrix with the input values on the main diagonal and zeros elsewhere
///
/// # Examples
///
/// ```
/// use ::ndarray::array;
/// use scirs2_core::ndarray_ext::matrix::_diag;
///
/// let values = array![1, 2, 3];
/// let diagmatrix = _diag(values.view());
/// assert_eq!(diagmatrix.shape(), &[3, 3]);
/// assert_eq!(diagmatrix[[0, 0]], 1);
/// assert_eq!(diagmatrix[[1, 1]], 2);
/// assert_eq!(diagmatrix[[2, 2]], 3);
/// assert_eq!(diagmatrix[[0, 1]], 0);
/// ```
#[allow(dead_code)]
pub fn _diag<T>(_diagvalues: ArrayView<T, Ix1>) -> Array<T, Ix2>
where
    T: Clone + Zero,
{
    let n = _diagvalues.len();
    let mut result = Array::<T, Ix2>::zeros((n, n));

    for i in 0..n {
        result[[i, i]] = _diagvalues[i].clone();
    }

    result
}

/// Extract a diagonal from a 2D array
///
/// # Arguments
///
/// * `array` - The input 2D array
/// * `k` - Diagonal offset (0 for main diagonal, positive for above, negative for below)
///
/// # Returns
///
/// A 1D array containing the specified diagonal
///
/// # Examples
///
/// ```
/// use ::ndarray::array;
/// use scirs2_core::ndarray_ext::matrix::diagonal;
///
/// let a = array![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
///
/// // Extract main diagonal
/// let main_diag = diagonal(a.view(), 0).expect("Operation failed");
/// assert_eq!(main_diag, array![1, 5, 9]);
///
/// // Extract superdiagonal
/// let super_diag = diagonal(a.view(), 1).expect("Operation failed");
/// assert_eq!(super_diag, array![2, 6]);
///
/// // Extract subdiagonal
/// let sub_diag = diagonal(a.view(), -1).expect("Operation failed");
/// assert_eq!(sub_diag, array![4, 8]);
/// ```
#[allow(dead_code)]
pub fn diagonal<T>(array: ArrayView<T, Ix2>, k: isize) -> Result<Array<T, Ix1>, &'static str>
where
    T: Clone + Zero,
{
    let (rows, cols) = (array.shape()[0], array.shape()[1]);

    // Calculate the length of the diagonal
    let diag_len = if k >= 0 {
        std::cmp::min(rows, cols.saturating_sub(k as usize))
    } else {
        std::cmp::min(cols, rows.saturating_sub((-k) as usize))
    };

    if diag_len == 0 {
        return Err("No diagonal elements for the given offset");
    }

    // Create the result array directly
    let mut result = Array::<T, Ix1>::zeros(diag_len);

    // Extract the diagonal elements
    for i in 0..diag_len {
        let row = if k < 0 { i + (-k) as usize } else { i };

        let col = if k > 0 { i + k as usize } else { i };

        result[i] = array[[row, col]].clone();
    }

    Ok(result)
}

/// Create a matrix filled with a given value
///
/// # Arguments
///
/// * `rows` - Number of rows
/// * `cols` - Number of columns
/// * `value` - Value to fill the matrix with
///
/// # Returns
///
/// A matrix filled with the specified value
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray_ext::matrix::full;
///
/// let filled = full(2, 3, 7);
/// assert_eq!(filled.shape(), &[2, 3]);
/// assert_eq!(filled[[0, 0]], 7);
/// assert_eq!(filled[[1, 2]], 7);
/// ```
#[allow(dead_code)]
pub fn full<T>(rows: usize, cols: usize, value: T) -> Array<T, Ix2>
where
    T: Clone,
{
    Array::<T, Ix2>::from_elem((rows, cols), value)
}

/// Create a matrix filled with ones
///
/// # Arguments
///
/// * `rows` - Number of rows
/// * `cols` - Number of columns
///
/// # Returns
///
/// A matrix filled with ones
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray_ext::matrix::ones;
///
/// let ones_mat = ones::<f64>(2, 3);
/// assert_eq!(ones_mat.shape(), &[2, 3]);
/// assert_eq!(ones_mat[[0, 0]], 1.0);
/// assert_eq!(ones_mat[[1, 2]], 1.0);
/// ```
#[allow(dead_code)]
pub fn ones<T>(rows: usize, cols: usize) -> Array<T, Ix2>
where
    T: Clone + One,
{
    Array::<T, Ix2>::from_elem((rows, cols), T::one())
}

/// Create a matrix filled with zeros
///
/// # Arguments
///
/// * `rows` - Number of rows
/// * `cols` - Number of columns
///
/// # Returns
///
/// A matrix filled with zeros
///
/// # Examples
///
/// ```ignore
/// use scirs2_core::ndarray_ext::matrix::zeros;
///
/// let zeros_mat = zeros::<f64>(2, 3);
/// assert_eq!(zeros_mat.shape(), &[2, 3]);
/// assert_eq!(zeros_mat[[0, 0]], 0.0);
/// assert_eq!(zeros_mat[[1, 2]], 0.0);
/// ```
#[allow(dead_code)]
pub fn zeros<T>(rows: usize, cols: usize) -> Array<T, Ix2>
where
    T: Clone + Zero,
{
    Array::<T, Ix2>::zeros((rows, cols))
}

/// Compute the Kronecker product of two 2D arrays
///
/// # Arguments
///
/// * `a` - First input array
/// * `b` - Second input array
///
/// # Returns
///
/// The Kronecker product of the input arrays
///
/// # Examples
///
/// ```
/// use ::ndarray::array;
/// use scirs2_core::ndarray_ext::matrix::kron;
///
/// let a = array![[1, 2], [3, 4]];
/// let b = array![[0, 5], [6, 7]];
///
/// let result = kron(a.view(), b.view());
/// assert_eq!(result.shape(), &[4, 4]);
/// assert_eq!(result, array![
///     [0, 5, 0, 10],
///     [6, 7, 12, 14],
///     [0, 15, 0, 20],
///     [18, 21, 24, 28]
/// ]);
/// ```
#[allow(dead_code)]
pub fn kron<T>(a: ArrayView<T, Ix2>, b: ArrayView<T, Ix2>) -> Array<T, Ix2>
where
    T: Clone + Zero + std::ops::Mul<Output = T>,
{
    let (a_rows, a_cols) = (a.shape()[0], a.shape()[1]);
    let (b_rows, b_cols) = (b.shape()[0], b.shape()[1]);

    let result_rows = a_rows * b_rows;
    let result_cols = a_cols * b_cols;

    let mut result = Array::<T, Ix2>::zeros((result_rows, result_cols));

    for i in 0..a_rows {
        for j in 0..a_cols {
            for k in 0..b_rows {
                for l in 0..b_cols {
                    result[[i * b_rows + k, j * b_cols + l]] =
                        a[[i, j]].clone() * b[[k, l]].clone();
                }
            }
        }
    }

    result
}

/// Create a Toeplitz matrix from first row and first column
///
/// # Arguments
///
/// * `first_row` - First row of the Toeplitz matrix
/// * `first_col` - First column of the Toeplitz matrix (first element must match first row's first element)
///
/// # Returns
///
/// A Toeplitz matrix with the specified first row and column
///
/// # Examples
///
/// ```
/// use ::ndarray::array;
/// use scirs2_core::ndarray_ext::matrix::toeplitz;
///
/// let first_row = array![1, 2, 3];
/// let first_col = array![1, 4, 5];
/// let result = toeplitz(first_row.view(), first_col.view()).expect("Operation failed");
/// assert_eq!(result.shape(), &[3, 3]);
/// assert_eq!(result, array![
///     [1, 2, 3],
///     [4, 1, 2],
///     [5, 4, 1]
/// ]);
/// ```
#[allow(dead_code)]
pub fn toeplitz<T>(
    first_row: ArrayView<T, Ix1>,
    first_col: ArrayView<T, Ix1>,
) -> Result<Array<T, Ix2>, &'static str>
where
    T: Clone + PartialEq + Zero,
{
    // First elements of first_row and first_col must match
    if first_row.is_empty() || first_col.is_empty() {
        return Err("Input arrays must not be empty");
    }

    if first_row[0] != first_col[0] {
        return Err("First element of _row and column must match");
    }

    let n = first_col.len(); // Number of rows
    let m = first_row.len(); // Number of columns

    let mut result = Array::<T, Ix2>::zeros((n, m));

    for i in 0..n {
        for j in 0..m {
            if i <= j {
                // Upper triangle and main diagonal from first_row
                result[[i, j]] = first_row[j - i].clone();
            } else {
                // Lower triangle from first_col
                result[[i, j]] = first_col[i - j].clone();
            }
        }
    }

    Ok(result)
}

/// Create a block diagonal matrix from a sequence of 2D arrays
///
/// # Arguments
///
/// * `arrays` - A slice of 2D arrays to form the blocks on the diagonal
///
/// # Returns
///
/// A block diagonal matrix with the input arrays on the diagonal
///
/// # Examples
///
/// ```
/// use ::ndarray::array;
/// use scirs2_core::ndarray_ext::matrix::block_diag;
///
/// let a = array![[1, 2], [3, 4]];
/// let b = array![[5, 6], [7, 8]];
///
/// let result = block_diag(&[a.view(), b.view()]);
/// assert_eq!(result.shape(), &[4, 4]);
/// assert_eq!(result, array![
///     [1, 2, 0, 0],
///     [3, 4, 0, 0],
///     [0, 0, 5, 6],
///     [0, 0, 7, 8]
/// ]);
/// ```
#[allow(dead_code)]
pub fn block_diag<T>(arrays: &[ArrayView<T, Ix2>]) -> Array<T, Ix2>
where
    T: Clone + Zero,
{
    if arrays.is_empty() {
        return Array::<T, Ix2>::zeros((0, 0));
    }

    // Calculate total dimensions
    let mut total_rows = 0;
    let mut total_cols = 0;

    for array in arrays {
        total_rows += array.shape()[0];
        total_cols += array.shape()[1];
    }

    let mut result = Array::<T, Ix2>::zeros((total_rows, total_cols));

    let mut row_offset = 0;
    let mut col_offset = 0;

    // Place each array on the diagonal
    for array in arrays {
        let (rows, cols) = (array.shape()[0], array.shape()[1]);

        for i in 0..rows {
            for j in 0..cols {
                result[[row_offset + i, col_offset + j]] = array[[i, j]].clone();
            }
        }

        row_offset += rows;
        col_offset += cols;
    }

    result
}

/// Create a tri-diagonal matrix from the three diagonals
///
/// # Arguments
///
/// * `diag` - Main diagonal
/// * `lower_diag` - Lower diagonal
/// * `upper_diag` - Upper diagonal
///
/// # Returns
///
/// A tri-diagonal matrix with the specified diagonals
///
/// # Examples
///
/// ```
/// use ::ndarray::array;
/// use scirs2_core::ndarray_ext::matrix::tridiagonal;
///
/// let diag = array![1, 2, 3];
/// let lower = array![4, 5];
/// let upper = array![6, 7];
///
/// let result = tridiagonal(diag.view(), lower.view(), upper.view()).expect("Operation failed");
/// assert_eq!(result.shape(), &[3, 3]);
/// assert_eq!(result, array![
///     [1, 6, 0],
///     [4, 2, 7],
///     [0, 5, 3]
/// ]);
/// ```
#[allow(dead_code)]
pub fn tridiagonal<T>(
    diag: ArrayView<T, Ix1>,
    lower_diag: ArrayView<T, Ix1>,
    upper_diag: ArrayView<T, Ix1>,
) -> Result<Array<T, Ix2>, &'static str>
where
    T: Clone + Zero,
{
    let n = diag.len();

    // Check that the diagonals have correct sizes
    if lower_diag.len() != n - 1 || upper_diag.len() != n - 1 {
        return Err("Lower and upper diagonals must have length n-1 where n is the length of the main diagonal");
    }

    let mut result = Array::<T, Ix2>::zeros((n, n));

    // Set main diagonal
    for i in 0..n {
        result[[i, i]] = diag[i].clone();
    }

    // Set lower diagonal
    for i in 1..n {
        result[[i, i - 1]] = lower_diag[i - 1].clone();
    }

    // Set upper diagonal
    for i in 0..n - 1 {
        result[[i, i + 1]] = upper_diag[i].clone();
    }

    Ok(result)
}

/// Create a Hankel matrix from its first column and last row
///
/// # Arguments
///
/// * `first_col` - First column of the Hankel matrix
/// * `last_row` - Last row of the Hankel matrix (first element must match last element of first_col)
///
/// # Returns
///
/// A Hankel matrix with the specified first column and last row
///
/// # Examples
///
/// ```
/// use ::ndarray::array;
/// use scirs2_core::ndarray_ext::matrix::hankel;
///
/// let first_col = array![1, 2, 3];
/// let last_row = array![3, 4, 5];
///
/// let result = hankel(first_col.view(), last_row.view()).expect("Operation failed");
/// assert_eq!(result.shape(), &[3, 3]);
/// assert_eq!(result, array![
///     [1, 2, 3],
///     [2, 3, 4],
///     [3, 4, 5]
/// ]);
/// ```
#[allow(dead_code)]
pub fn hankel<T>(
    first_col: ArrayView<T, Ix1>,
    last_row: ArrayView<T, Ix1>,
) -> Result<Array<T, Ix2>, &'static str>
where
    T: Clone + PartialEq + Zero,
{
    if first_col.is_empty() || last_row.is_empty() {
        return Err("Input arrays must not be empty");
    }

    // Last element of first_col must match first element of last_row
    if first_col[first_col.len() - 1] != last_row[0] {
        return Err("Last element of first column must match first element of last _row");
    }

    let n = first_col.len(); // Number of rows
    let m = last_row.len(); // Number of columns

    let mut result = Array::<T, Ix2>::zeros((n, m));

    // Combine first_col and last_row (minus first element of last_row) to form the "data" array
    let data_len = n + m - 1;
    let mut data = Vec::with_capacity(data_len);

    // Fill data with first_col elements
    for i in 0..n {
        data.push(first_col[i].clone());
    }

    // Append last_row elements (skipping first element which should match last element of first_col)
    for i in 1..m {
        data.push(last_row[i].clone());
    }

    // Fill the Hankel matrix
    for i in 0..n {
        for j in 0..m {
            result[[i, j]] = data[i + j].clone();
        }
    }

    Ok(result)
}

/// Calculate the trace of a square matrix (sum of diagonal elements)
///
/// # Arguments
///
/// * `array` - Input square matrix
///
/// # Returns
///
/// The trace of the matrix
///
/// # Examples
///
/// ```
/// use ::ndarray::array;
/// use scirs2_core::ndarray_ext::matrix::trace;
///
/// let a = array![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
/// let tr = trace(a.view()).expect("Operation failed");
/// assert_eq!(tr, 15);  // 1 + 5 + 9 = 15
/// ```
#[allow(dead_code)]
pub fn trace<T>(array: ArrayView<T, Ix2>) -> Result<T, &'static str>
where
    T: Clone + Zero + std::ops::Add<Output = T>,
{
    let (rows, cols) = (array.shape()[0], array.shape()[1]);

    if rows != cols {
        return Err("Trace is only defined for square matrices");
    }

    let mut result = T::zero();

    for i in 0..rows {
        result = result + array[[i, i]].clone();
    }

    Ok(result)
}

/// Create a matrix vander from a 1D array
///
/// # Arguments
///
/// * `x` - Input 1D array
/// * `n` - Optional number of columns in the output (defaults to x.len())
/// * `increasing` - Optional boolean to determine order (defaults to false)
///
/// # Returns
///
/// A Vandermonde matrix where each column is a power of the input array
///
/// # Examples
///
/// ```ignore
/// use ::ndarray::array;
/// use scirs2_core::ndarray_ext::matrix::vander;
///
/// let x = array![1.0, 2.0, 3.0];
///
/// // Default behavior: decreasing powers from n-1 to 0
/// let v1 = vander(x.view(), None, None).expect("Operation failed");
/// assert_eq!(v1.shape(), &[3, 3]);
/// // Powers: x^2, x^1, x^0
/// assert_eq!(v1, array![
///     [1.0, 1.0, 1.0],
///     [4.0, 2.0, 1.0],
///     [9.0, 3.0, 1.0]
/// ]);
///
/// // Increasing powers: 0 to n-1
/// let v2 = vander(x.view(), None, Some(true)).expect("Operation failed");
/// assert_eq!(v2.shape(), &[3, 3]);
/// // Powers: x^0, x^1, x^2
/// assert_eq!(v2, array![
///     [1.0, 1.0, 1.0],
///     [1.0, 2.0, 4.0],
///     [1.0, 3.0, 9.0]
/// ]);
///
/// // Specify 4 columns
/// let v3 = vander(x.view(), Some(4), None).expect("Operation failed");
/// assert_eq!(v3.shape(), &[3, 4]);
/// // Powers: x^3, x^2, x^1, x^0
/// assert_eq!(v3, array![
///     [1.0, 1.0, 1.0, 1.0],
///     [8.0, 4.0, 2.0, 1.0],
///     [27.0, 9.0, 3.0, 1.0]
/// ]);
/// ```
#[allow(dead_code)]
pub fn vander<T>(
    x: ArrayView<T, Ix1>,
    n: Option<usize>,
    increasing: Option<bool>,
) -> Result<Array<T, Ix2>, &'static str>
where
    T: Clone + Zero + One + std::ops::Mul<Output = T> + std::ops::Div<Output = T>,
{
    let x_len = x.len();

    if x_len == 0 {
        return Err("Input array must not be empty");
    }

    let n = n.unwrap_or(x_len);
    let increasing = increasing.unwrap_or(false);

    let mut result = Array::<T, Ix2>::zeros((x_len, n));

    // Fill in the Vandermonde matrix with powers of x elements
    for i in 0..x_len {
        // Initialize accumulator with 1 (x^0)
        let mut power = T::one();

        if increasing {
            // First column is x^0 (all ones)
            for j in 0..n {
                result[[i, j]] = power.clone();

                if j < n - 1 {
                    power = power.clone() * x[i].clone();
                }
            }
        } else {
            // Decreasing powers (last column is x^0)
            // Calculate highest power first: x^(n-1)
            for _p in 0..n - 1 {
                power = power.clone() * x[i].clone();
            }

            for j in 0..n {
                result[[i, j]] = power.clone();

                if j < n - 1 {
                    // For non-increasing powers, we need Div trait for T to handle division
                    // power = power.clone() / x[i].clone(); // This requires Div trait

                    // Use multiplication by reciprocal instead as a safer approach
                    power = power.clone() * (T::one() / x[i].clone());
                }
            }
        }
    }

    Ok(result)
}

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

    #[test]
    fn test_eye() {
        let id3 = eye::<f64>(3);
        assert_eq!(id3.shape(), &[3, 3]);
        assert_eq!(id3[[0, 0]], 1.0);
        assert_eq!(id3[[1, 1]], 1.0);
        assert_eq!(id3[[2, 2]], 1.0);
        assert_eq!(id3[[0, 1]], 0.0);
        assert_eq!(id3[[1, 0]], 0.0);
    }

    #[test]
    fn test_eye_offset() {
        // 3x3 matrix with ones on the main diagonal (k=0)
        let id3 = eye_offset::<f64>(3, 3, 0);
        assert_eq!(id3.shape(), &[3, 3]);
        assert_eq!(id3[[0, 0]], 1.0);
        assert_eq!(id3[[1, 1]], 1.0);
        assert_eq!(id3[[2, 2]], 1.0);

        // 3x4 matrix with ones on the first superdiagonal (k=1)
        let super_diag = eye_offset::<f64>(3, 4, 1);
        assert_eq!(super_diag.shape(), &[3, 4]);
        assert_eq!(super_diag[[0, 1]], 1.0);
        assert_eq!(super_diag[[1, 2]], 1.0);
        assert_eq!(super_diag[[2, 3]], 1.0);

        // 4x3 matrix with ones on the first subdiagonal (k=-1)
        let sub_diag = eye_offset::<f64>(4, 3, -1);
        assert_eq!(sub_diag.shape(), &[4, 3]);
        assert_eq!(sub_diag[[1, 0]], 1.0);
        assert_eq!(sub_diag[[2, 1]], 1.0);
        assert_eq!(sub_diag[[3, 2]], 1.0);
    }

    #[test]
    fn test_diag() {
        let values = array![1, 2, 3];
        let diagmatrix = _diag(values.view());
        assert_eq!(diagmatrix.shape(), &[3, 3]);
        assert_eq!(diagmatrix[[0, 0]], 1);
        assert_eq!(diagmatrix[[1, 1]], 2);
        assert_eq!(diagmatrix[[2, 2]], 3);
        assert_eq!(diagmatrix[[0, 1]], 0);
    }

    #[test]
    fn test_diagonal() {
        let a = array![[1, 2, 3], [4, 5, 6], [7, 8, 9]];

        // Extract main diagonal
        let main_diag = diagonal(a.view(), 0).expect("Operation failed");
        assert_eq!(main_diag, array![1, 5, 9]);

        // Extract superdiagonal
        let super_diag = diagonal(a.view(), 1).expect("Operation failed");
        assert_eq!(super_diag, array![2, 6]);

        // Extract subdiagonal
        let sub_diag = diagonal(a.view(), -1).expect("Operation failed");
        assert_eq!(sub_diag, array![4, 8]);

        // Test out of bounds (should return error)
        assert!(diagonal(a.view(), 3).is_err());
        assert!(diagonal(a.view(), -3).is_err());
    }

    #[test]
    fn test_full_ones_zeros() {
        // Test full
        let filled = full(2, 3, 7);
        assert_eq!(filled.shape(), &[2, 3]);
        assert_eq!(filled[[0, 0]], 7);
        assert_eq!(filled[[1, 2]], 7);

        // Test ones
        let ones_mat = ones::<f64>(2, 3);
        assert_eq!(ones_mat.shape(), &[2, 3]);
        assert_eq!(ones_mat[[0, 0]], 1.0);
        assert_eq!(ones_mat[[1, 2]], 1.0);

        // Test zeros
        let zeros_mat = zeros::<f64>(2, 3);
        assert_eq!(zeros_mat.shape(), &[2, 3]);
        assert_eq!(zeros_mat[[0, 0]], 0.0);
        assert_eq!(zeros_mat[[1, 2]], 0.0);
    }

    #[test]
    fn test_kron() {
        let a = array![[1, 2], [3, 4]];
        let b = array![[0, 5], [6, 7]];

        let result = kron(a.view(), b.view());
        assert_eq!(result.shape(), &[4, 4]);

        // Check results
        assert_eq!(
            result,
            array![
                [0, 5, 0, 10],
                [6, 7, 12, 14],
                [0, 15, 0, 20],
                [18, 21, 24, 28]
            ]
        );
    }

    #[test]
    fn test_toeplitz() {
        let first_row = array![1, 2, 3];
        let first_col = array![1, 4, 5];

        let result = toeplitz(first_row.view(), first_col.view()).expect("Operation failed");
        assert_eq!(result.shape(), &[3, 3]);
        assert_eq!(result, array![[1, 2, 3], [4, 1, 2], [5, 4, 1]]);

        // Test with mismatched first elements (should return error)
        let bad_row = array![9, 2, 3];
        assert!(toeplitz(bad_row.view(), first_col.view()).is_err());
    }

    #[test]
    fn test_block_diag() {
        let a = array![[1, 2], [3, 4]];
        let b = array![[5, 6], [7, 8]];

        let result = block_diag(&[a.view(), b.view()]);
        assert_eq!(result.shape(), &[4, 4]);
        assert_eq!(
            result,
            array![[1, 2, 0, 0], [3, 4, 0, 0], [0, 0, 5, 6], [0, 0, 7, 8]]
        );

        // Test with different size blocks
        let c = array![[9]];
        let result2 = block_diag(&[a.view(), c.view()]);
        assert_eq!(result2.shape(), &[3, 3]);
        assert_eq!(result2, array![[1, 2, 0], [3, 4, 0], [0, 0, 9]]);

        // Test with empty array list
        let empty: [ArrayView<i32, Ix2>; 0] = [];
        let result3 = block_diag(&empty);
        assert_eq!(result3.shape(), &[0, 0]);
    }

    #[test]
    fn test_tridiagonal() {
        let diag = array![1, 2, 3];
        let lower = array![4, 5];
        let upper = array![6, 7];

        let result =
            tridiagonal(diag.view(), lower.view(), upper.view()).expect("Operation failed");
        assert_eq!(result.shape(), &[3, 3]);
        assert_eq!(result, array![[1, 6, 0], [4, 2, 7], [0, 5, 3]]);

        // Test with incorrect diagonals size (should return error)
        let bad_lower = array![4];
        assert!(tridiagonal(diag.view(), bad_lower.view(), upper.view()).is_err());
    }

    #[test]
    fn test_hankel() {
        let first_col = array![1, 2, 3];
        let last_row = array![3, 4, 5];

        let result = hankel(first_col.view(), last_row.view()).expect("Operation failed");
        assert_eq!(result.shape(), &[3, 3]);
        assert_eq!(result, array![[1, 2, 3], [2, 3, 4], [3, 4, 5]]);

        // Test with mismatched elements (should return error)
        let bad_row = array![9, 4, 5];
        assert!(hankel(first_col.view(), bad_row.view()).is_err());
    }

    #[test]
    fn test_trace() {
        let a = array![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
        let tr = trace(a.view()).expect("Operation failed");
        assert_eq!(tr, 15); // 1 + 5 + 9 = 15

        // Test non-square matrix (should return error)
        let b = array![[1, 2, 3], [4, 5, 6]];
        assert!(trace(b.view()).is_err());
    }

    #[test]
    fn test_vander() {
        let x = array![1.0, 2.0, 3.0];

        // Default behavior: decreasing powers from n-1 to 0
        let v1 = vander(x.view(), None, None).expect("Operation failed");
        assert_eq!(v1.shape(), &[3, 3]);
        // Should be equivalent to [x^2, x^1, x^0]
        for i in 0..3 {
            assert_abs_diff_eq!(v1[[0, 0]], x[0] * x[0]);
            assert_abs_diff_eq!(v1[[0, 1]], x[0]);
            assert_abs_diff_eq!(v1[[0, 2]], 1.0);
        }

        // Increasing powers from 0 to n-1
        let v2 = vander(x.view(), None, Some(true)).expect("Operation failed");
        assert_eq!(v2.shape(), &[3, 3]);
        // Should be equivalent to [x^0, x^1, x^2]
        for i in 0..3 {
            assert_abs_diff_eq!(v2[[0, 0]], 1.0);
            assert_abs_diff_eq!(v2[[0, 1]], x[0]);
            assert_abs_diff_eq!(v2[[0, 2]], x[0] * x[0]);
        }

        // Specify 4 columns (decreasing power)
        let v3 = vander(x.view(), Some(4), None).expect("Operation failed");
        assert_eq!(v3.shape(), &[3, 4]);
        // Should be equivalent to [x^3, x^2, x^1, x^0]
        for i in 0..3 {
            assert_abs_diff_eq!(v3[[0, 0]], x[0] * x[0] * x[0]);
            assert_abs_diff_eq!(v3[[0, 1]], x[0] * x[0]);
            assert_abs_diff_eq!(v3[[0, 2]], x[0]);
            assert_abs_diff_eq!(v3[[0, 3]], 1.0);
        }
    }
}