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
//! Extended ndarray operations for scientific computing
//!
//! This module provides additional functionality for ndarray to support
//! the advanced array operations necessary for a complete SciPy port.
//! It implements core `NumPy`-like features that are not available in the
//! base ndarray crate.

/// Re-export essential ndarray types and macros for convenience
/// Use ::ndarray to refer to the external crate directly
pub use ::ndarray::{
    s, Array, ArrayBase, ArrayView, ArrayViewMut, Axis, Data, DataMut, Dim, Dimension, Ix1, Ix2,
    Ix3, Ix4, Ix5, Ix6, IxDyn, OwnedRepr, RemoveAxis, ScalarOperand, ShapeBuilder, ShapeError,
    SliceInfo, ViewRepr, Zip,
};

// Re-export the array! macro for convenient array creation
// This addresses the common issue where users expect array! to be available in scirs2-core
// instead of requiring import from scirs2_autograd::ndarray
pub use ::ndarray::{arr1, arr2, array};

// Re-export type aliases for common array sizes
pub use ::ndarray::{ArcArray1, ArcArray2};
pub use ::ndarray::{Array0, Array1, Array2, Array3, Array4, Array5, Array6, ArrayD};
pub use ::ndarray::{
    ArrayView0, ArrayView1, ArrayView2, ArrayView3, ArrayView4, ArrayView5, ArrayView6, ArrayViewD,
};
pub use ::ndarray::{
    ArrayViewMut0, ArrayViewMut1, ArrayViewMut2, ArrayViewMut3, ArrayViewMut4, ArrayViewMut5,
    ArrayViewMut6, ArrayViewMutD,
};

/// Advanced indexing operations (`NumPy`-like boolean masking, fancy indexing, etc.)
pub mod indexing;

/// Statistical functions for ndarray arrays (mean, median, variance, correlation, etc.)
pub mod stats;

/// Matrix operations (eye, diag, kron, etc.)
pub mod matrix;

/// Array manipulation operations (flip, roll, tile, repeat, etc.)
pub mod manipulation;

/// Array reduction operations with SIMD acceleration (argmin, argmax, etc.)
pub mod reduction;

/// Data preprocessing operations with SIMD acceleration (normalization, standardization)
pub mod preprocessing;

/// Element-wise mathematical operations with SIMD acceleration (abs, sqrt, etc.)
pub mod elementwise;

/// Random array generation with full ndarray-rand compatibility and scientific computing extensions
#[cfg(feature = "random")]
pub mod random;

/// Reshape a 2D array to a new shape without copying data when possible
///
/// # Arguments
///
/// * `array` - The input array
/// * `shape` - The new shape (rows, cols), which must be compatible with the original shape
///
/// # Returns
///
/// A reshaped array view of the input array if possible, or a new array otherwise
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_core::ndarray_ext::reshape_2d;
///
/// let a = array![[1, 2], [3, 4]];
/// let b = reshape_2d(a.view(), (4, 1)).expect("Operation failed");
/// assert_eq!(b.shape(), &[4, 1]);
/// assert_eq!(b[[0, 0]], 1);
/// assert_eq!(b[[3, 0]], 4);
/// ```
#[allow(dead_code)]
pub fn reshape_2d<T>(
    array: ArrayView<T, Ix2>,
    shape: (usize, usize),
) -> Result<Array<T, Ix2>, &'static str>
where
    T: Clone + Default,
{
    let (rows, cols) = shape;
    let total_elements = rows * cols;

    // Check if the new shape is compatible with the original shape
    if total_elements != array.len() {
        return Err("New shape dimensions must match the total number of elements");
    }

    // Create a new array with the specified shape
    let mut result = Array::<T, Ix2>::default(shape);

    // Fill the result array with elements from the input array
    let flat_iter = array.iter();
    for (i, val) in flat_iter.enumerate() {
        let r = i / cols;
        let c = i % cols;
        result[[r, c]] = val.clone();
    }

    Ok(result)
}

/// Stack 2D arrays along a given axis
///
/// # Arguments
///
/// * `arrays` - A slice of 2D arrays to stack
/// * `axis` - The axis along which to stack (0 for rows, 1 for columns)
///
/// # Returns
///
/// A new array containing the stacked arrays
///
/// # Examples
///
/// ```
/// use ndarray::{array, Axis};
/// use scirs2_core::ndarray_ext::stack_2d;
///
/// let a = array![[1, 2], [3, 4]];
/// let b = array![[5, 6], [7, 8]];
/// let c = stack_2d(&[a.view(), b.view()], 0).expect("Operation failed");
/// assert_eq!(c.shape(), &[4, 2]);
/// ```
#[allow(dead_code)]
pub fn stack_2d<T>(arrays: &[ArrayView<T, Ix2>], axis: usize) -> Result<Array<T, Ix2>, &'static str>
where
    T: Clone + Default,
{
    if arrays.is_empty() {
        return Err("No _arrays provided for stacking");
    }

    // Validate that all _arrays have the same shape
    let firstshape = arrays[0].shape();
    for array in arrays.iter().skip(1) {
        if array.shape() != firstshape {
            return Err("All _arrays must have the same shape for stacking");
        }
    }

    let (rows, cols) = (firstshape[0], firstshape[1]);

    // Calculate the new shape
    let (new_rows, new_cols) = match axis {
        0 => (rows * arrays.len(), cols), // Stack vertically
        1 => (rows, cols * arrays.len()), // Stack horizontally
        _ => return Err("Axis must be 0 or 1 for 2D _arrays"),
    };

    // Create a new array to hold the stacked result
    let mut result = Array::<T, Ix2>::default((new_rows, new_cols));

    // Copy data from the input _arrays to the result
    match axis {
        0 => {
            // Stack vertically (along rows)
            for (array_idx, array) in arrays.iter().enumerate() {
                let start_row = array_idx * rows;
                for r in 0..rows {
                    for c in 0..cols {
                        result[[start_row + r, c]] = array[[r, c]].clone();
                    }
                }
            }
        }
        1 => {
            // Stack horizontally (along columns)
            for (array_idx, array) in arrays.iter().enumerate() {
                let start_col = array_idx * cols;
                for r in 0..rows {
                    for c in 0..cols {
                        result[[r, start_col + c]] = array[[r, c]].clone();
                    }
                }
            }
        }
        _ => unreachable!(),
    }

    Ok(result)
}

/// Swap axes (transpose) of a 2D array
///
/// # Arguments
///
/// * `array` - The input 2D array
///
/// # Returns
///
/// A view of the input array with the axes swapped
///
/// # Examples
///
/// ```ignore
/// use crate::ndarray::array;
/// use scirs2_core::ndarray_ext::transpose_2d;
///
/// let a = array![[1, 2, 3], [4, 5, 6]];
/// let b = transpose_2d(a.view());
/// assert_eq!(b.shape(), &[3, 2]);
/// assert_eq!(b[[0, 0]], 1);
/// assert_eq!(b[[0, 1]], 4);
/// ```
#[allow(dead_code)]
pub fn transpose_2d<T>(array: ArrayView<T, Ix2>) -> Array<T, Ix2>
where
    T: Clone,
{
    array.t().to_owned()
}

/// Split a 2D array into multiple sub-arrays along a given axis
///
/// # Arguments
///
/// * `array` - The input 2D array to split
/// * `indices` - Indices where the array should be split
/// * `axis` - The axis along which to split (0 for rows, 1 for columns)
///
/// # Returns
///
/// A vector of arrays resulting from the split
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_core::ndarray_ext::split_2d;
///
/// let a = array![[1, 2, 3, 4], [5, 6, 7, 8]];
/// let result = split_2d(a.view(), &[2], 1).expect("Operation failed");
/// assert_eq!(result.len(), 2);
/// assert_eq!(result[0].shape(), &[2, 2]);
/// assert_eq!(result[1].shape(), &[2, 2]);
/// ```
#[allow(dead_code)]
pub fn split_2d<T>(
    array: ArrayView<T, Ix2>,
    indices: &[usize],
    axis: usize,
) -> Result<Vec<Array<T, Ix2>>, &'static str>
where
    T: Clone + Default,
{
    if indices.is_empty() {
        return Ok(vec![array.to_owned()]);
    }

    let (rows, cols) = (array.shape()[0], array.shape()[1]);
    let axis_len = if axis == 0 { rows } else { cols };

    // Validate indices
    for &idx in indices {
        if idx >= axis_len {
            return Err("Split index out of bounds");
        }
    }

    // Sort indices to ensure they're in ascending order
    let mut sorted_indices = indices.to_vec();
    sorted_indices.sort_unstable();

    // Calculate the sub-array boundaries
    let mut starts = vec![0];
    starts.extend_from_slice(&sorted_indices);

    let mut ends = sorted_indices.clone();
    ends.push(axis_len);

    // Create the split sub-arrays
    let mut result = Vec::with_capacity(starts.len());

    match axis {
        0 => {
            // Split along rows
            for (&start, &end) in starts.iter().zip(ends.iter()) {
                let sub_rows = end - start;
                let mut sub_array = Array::<T, Ix2>::default((sub_rows, cols));

                for r in 0..sub_rows {
                    for c in 0..cols {
                        sub_array[[r, c]] = array[[start + r, c]].clone();
                    }
                }

                result.push(sub_array);
            }
        }
        1 => {
            // Split along columns
            for (&start, &end) in starts.iter().zip(ends.iter()) {
                let sub_cols = end - start;
                let mut sub_array = Array::<T, Ix2>::default((rows, sub_cols));

                for r in 0..rows {
                    for c in 0..sub_cols {
                        sub_array[[r, c]] = array[[r, start + c]].clone();
                    }
                }

                result.push(sub_array);
            }
        }
        _ => return Err("Axis must be 0 or 1 for 2D arrays"),
    }

    Ok(result)
}

/// Take elements from a 2D array along a given axis using indices from another array
///
/// # Arguments
///
/// * `array` - The input 2D array
/// * `indices` - Array of indices to take
/// * `axis` - The axis along which to take values (0 for rows, 1 for columns)
///
/// # Returns
///
/// An array of values at the specified indices along the given axis
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_core::ndarray_ext::take_2d;
///
/// let a = array![[1, 2, 3], [4, 5, 6]];
/// let indices = array![0_usize, 2];
/// let result = take_2d(a.view(), indices.view(), 1).expect("Operation failed");
/// assert_eq!(result.shape(), &[2, 2]);
/// assert_eq!(result[[0, 0]], 1);
/// assert_eq!(result[[0, 1]], 3);
/// ```
#[allow(dead_code)]
pub fn take_2d<T>(
    array: ArrayView<T, Ix2>,
    indices: ArrayView<usize, Ix1>,
    axis: usize,
) -> Result<Array<T, Ix2>, &'static str>
where
    T: Clone + Default,
{
    let (rows, cols) = (array.shape()[0], array.shape()[1]);
    let axis_len = if axis == 0 { rows } else { cols };

    // Check that indices are within bounds
    for &idx in indices.iter() {
        if idx >= axis_len {
            return Err("Index out of bounds");
        }
    }

    // Create the result array with the appropriate shape
    let (result_rows, result_cols) = match axis {
        0 => (indices.len(), cols),
        1 => (rows, indices.len()),
        _ => return Err("Axis must be 0 or 1 for 2D arrays"),
    };

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

    // Fill the result array
    match axis {
        0 => {
            // Take along rows
            for (i, &idx) in indices.iter().enumerate() {
                for j in 0..cols {
                    result[[i, j]] = array[[idx, j]].clone();
                }
            }
        }
        1 => {
            // Take along columns
            for i in 0..rows {
                for (j, &idx) in indices.iter().enumerate() {
                    result[[i, j]] = array[[i, idx]].clone();
                }
            }
        }
        _ => unreachable!(),
    }

    Ok(result)
}

/// Filter an array using a boolean mask
///
/// # Arguments
///
/// * `array` - The input array
/// * `mask` - Boolean mask of the same shape as the array
///
/// # Returns
///
/// A 1D array containing the elements where the mask is true
///
/// # Examples
///
/// ```ignore
/// use crate::ndarray::array;
/// use scirs2_core::ndarray_ext::mask_select;
///
/// let a = array![[1, 2, 3], [4, 5, 6]];
/// let mask = array![[true, false, true], [false, true, false]];
/// let result = mask_select(a.view(), mask.view()).expect("Operation failed");
/// assert_eq!(result.shape(), &[3]);
/// assert_eq!(result[0], 1);
/// assert_eq!(result[1], 3);
/// assert_eq!(result[2], 5);
/// ```
#[allow(dead_code)]
pub fn mask_select<T>(
    array: ArrayView<T, Ix2>,
    mask: ArrayView<bool, Ix2>,
) -> Result<Array<T, Ix1>, &'static str>
where
    T: Clone + Default,
{
    // Check that the mask has the same shape as the array
    if array.shape() != mask.shape() {
        return Err("Mask shape must match array shape");
    }

    // Count the number of true values in the mask
    let true_count = mask.iter().filter(|&&x| x).count();

    // Create the result array
    let mut result = Array::<T, Ix1>::default(true_count);

    // Fill the result array with elements where the mask is true
    let mut idx = 0;
    for (val, &m) in array.iter().zip(mask.iter()) {
        if m {
            result[idx] = val.clone();
            idx += 1;
        }
    }

    Ok(result)
}

/// Index a 2D array with a list of index arrays (fancy indexing)
///
/// # Arguments
///
/// * `array` - The input 2D array
/// * `row_indices` - Array of row indices
/// * `col_indices` - Array of column indices
///
/// # Returns
///
/// A 1D array containing the elements at the specified indices
///
/// # Examples
///
/// ```ignore
/// use crate::ndarray::array;
/// use scirs2_core::ndarray_ext::fancy_index_2d;
///
/// let a = array![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
/// let row_indices = array![0, 2];
/// let col_indices = array![0, 1];
/// let result = fancy_index_2d(a.view(), row_indices.view(), col_indices.view()).expect("Operation failed");
/// assert_eq!(result.shape(), &[2]);
/// assert_eq!(result[0], 1);
/// assert_eq!(result[1], 8);
/// ```
#[allow(dead_code)]
pub fn fancy_index_2d<T>(
    array: ArrayView<T, Ix2>,
    row_indices: ArrayView<usize, Ix1>,
    col_indices: ArrayView<usize, Ix1>,
) -> Result<Array<T, Ix1>, &'static str>
where
    T: Clone + Default,
{
    // Check that all index arrays have the same length
    let result_size = row_indices.len();
    if col_indices.len() != result_size {
        return Err("Row and column index arrays must have the same length");
    }

    let (rows, cols) = (array.shape()[0], array.shape()[1]);

    // Check that _indices are within bounds
    for &idx in row_indices.iter() {
        if idx >= rows {
            return Err("Row index out of bounds");
        }
    }

    for &idx in col_indices.iter() {
        if idx >= cols {
            return Err("Column index out of bounds");
        }
    }

    // Create the result array
    let mut result = Array::<T, Ix1>::default(result_size);

    // Fill the result array
    for i in 0..result_size {
        let row = row_indices[i];
        let col = col_indices[i];
        result[i] = array[[row, col]].clone();
    }

    Ok(result)
}

/// Select elements from an array where a condition is true
///
/// # Arguments
///
/// * `array` - The input array
/// * `condition` - A function that takes a reference to an element and returns a bool
///
/// # Returns
///
/// A 1D array containing the elements where the condition is true
///
/// # Examples
///
/// ```ignore
/// use crate::ndarray::array;
/// use scirs2_core::ndarray_ext::where_condition;
///
/// let a = array![[1, 2, 3], [4, 5, 6]];
/// let result = where_condition(a.view(), |&x| x > 3).expect("Operation failed");
/// assert_eq!(result.shape(), &[3]);
/// assert_eq!(result[0], 4);
/// assert_eq!(result[1], 5);
/// assert_eq!(result[2], 6);
/// ```
#[allow(dead_code)]
pub fn where_condition<T, F>(
    array: ArrayView<T, Ix2>,
    condition: F,
) -> Result<Array<T, Ix1>, &'static str>
where
    T: Clone + Default,
    F: Fn(&T) -> bool,
{
    // Build a boolean mask array based on the condition
    let mask = array.map(condition);

    // Use the mask_select function to select elements
    mask_select(array, mask.view())
}

/// Check if two shapes are broadcast compatible
///
/// # Arguments
///
/// * `shape1` - First shape as a slice
/// * `shape2` - Second shape as a slice
///
/// # Returns
///
/// `true` if the shapes are broadcast compatible, `false` otherwise
///
/// # Examples
///
/// ```ignore
/// use scirs2_core::ndarray_ext::is_broadcast_compatible;
///
/// assert!(is_broadcast_compatible(&[2, 3], &[3]));
/// // This example has dimensions that don't match (5 vs 3 in dimension 0)
/// // and aren't broadcasting compatible (neither is 1)
/// assert!(!is_broadcast_compatible(&[5, 1, 4], &[3, 1, 1]));
/// assert!(!is_broadcast_compatible(&[2, 3], &[4]));
/// ```
#[allow(dead_code)]
pub fn is_broadcast_compatible(shape1: &[usize], shape2: &[usize]) -> bool {
    // Align shapes to have the same dimensionality by prepending with 1s
    let max_dim = shape1.len().max(shape2.len());

    // Fill in 1s for missing dimensions
    let get_dim = |shape: &[usize], i: usize| -> usize {
        let offset = max_dim - shape.len();
        if i < offset {
            1 // Implicit dimension of size 1
        } else {
            shape[i - offset]
        }
    };

    // Check broadcasting rules for each dimension
    for i in 0..max_dim {
        let dim1 = get_dim(shape1, i);
        let dim2 = get_dim(shape2, i);

        // Dimensions must either be the same or one of them must be 1
        if dim1 != dim2 && dim1 != 1 && dim2 != 1 {
            return false;
        }
    }

    true
}

/// Calculate the broadcasted shape from two input shapes
///
/// # Arguments
///
/// * `shape1` - First shape as a slice
/// * `shape2` - Second shape as a slice
///
/// # Returns
///
/// The broadcasted shape as a `Vec<usize>`, or `None` if the shapes are incompatible
#[allow(dead_code)]
pub fn broadcastshape(shape1: &[usize], shape2: &[usize]) -> Option<Vec<usize>> {
    if !is_broadcast_compatible(shape1, shape2) {
        return None;
    }

    // Align shapes to have the same dimensionality
    let max_dim = shape1.len().max(shape2.len());
    let mut result = Vec::with_capacity(max_dim);

    // Fill in 1s for missing dimensions
    let get_dim = |shape: &[usize], i: usize| -> usize {
        let offset = max_dim - shape.len();
        if i < offset {
            1 // Implicit dimension of size 1
        } else {
            shape[i - offset]
        }
    };

    // Calculate the broadcasted shape
    for i in 0..max_dim {
        let dim1 = get_dim(shape1, i);
        let dim2 = get_dim(shape2, i);

        // The broadcasted dimension is the maximum of the two
        result.push(dim1.max(dim2));
    }

    Some(result)
}

/// Broadcast a 1D array to a 2D shape by repeating it along the specified axis
///
/// # Arguments
///
/// * `array` - The input 1D array
/// * `repeats` - Number of times to repeat the array
/// * `axis` - Axis along which to repeat (0 for rows, 1 for columns)
///
/// # Returns
///
/// A 2D array with the input array repeated along the specified axis
///
/// # Examples
///
/// ```ignore
/// use crate::ndarray::array;
/// use scirs2_core::ndarray_ext::broadcast_1d_to_2d;
///
/// let a = array![1, 2, 3];
/// let b = broadcast_1d_to_2d(a.view(), 2, 0).expect("Operation failed");
/// assert_eq!(b.shape(), &[2, 3]);
/// assert_eq!(b[[0, 0]], 1);
/// assert_eq!(b[[1, 0]], 1);
/// ```
#[allow(dead_code)]
pub fn broadcast_1d_to_2d<T>(
    array: ArrayView<T, Ix1>,
    repeats: usize,
    axis: usize,
) -> Result<Array<T, Ix2>, &'static str>
where
    T: Clone + Default,
{
    let len = array.len();

    // Create the result array with the appropriate shape
    let (rows, cols) = match axis {
        0 => (repeats, len), // Broadcast along rows
        1 => (len, repeats), // Broadcast along columns
        _ => return Err("Axis must be 0 or 1"),
    };

    let mut result = Array::<T, Ix2>::default((rows, cols));

    // Fill the result array
    match axis {
        0 => {
            // Broadcast along rows
            for i in 0..repeats {
                for j in 0..len {
                    result[[i, j]] = array[j].clone();
                }
            }
        }
        1 => {
            // Broadcast along columns
            for i in 0..len {
                for j in 0..repeats {
                    result[[i, j]] = array[i].clone();
                }
            }
        }
        _ => unreachable!(),
    }

    Ok(result)
}

/// Apply an element-wise binary operation to two arrays with broadcasting
///
/// # Arguments
///
/// * `a` - First array (2D)
/// * `b` - Second array (can be 1D or 2D)
/// * `op` - Binary operation to apply to each pair of elements
///
/// # Returns
///
/// A 2D array containing the result of the operation applied element-wise
///
/// # Examples
///
/// ```ignore
/// use crate::ndarray::array;
/// use scirs2_core::ndarray_ext::broadcast_apply;
///
/// let a = array![[1, 2, 3], [4, 5, 6]];
/// let b = array![10, 20, 30];
/// let result = broadcast_apply(a.view(), b.view(), |x, y| x + y).expect("Operation failed");
/// assert_eq!(result.shape(), &[2, 3]);
/// assert_eq!(result[[0, 0]], 11);
/// assert_eq!(result[[1, 2]], 36);
/// ```
#[allow(dead_code)]
pub fn broadcast_apply<T, R, F>(
    a: ArrayView<T, Ix2>,
    b: ArrayView<T, Ix1>,
    op: F,
) -> Result<Array<R, Ix2>, &'static str>
where
    T: Clone + Default,
    R: Clone + Default,
    F: Fn(&T, &T) -> R,
{
    let (a_rows, a_cols) = (a.shape()[0], a.shape()[1]);
    let b_len = b.len();

    // Check that the arrays are broadcast compatible
    if a_cols != b_len {
        return Err("Arrays are not broadcast compatible");
    }

    // Create the result array
    let mut result = Array::<R, Ix2>::default((a_rows, a_cols));

    // Apply the operation element-wise
    for i in 0..a_rows {
        for j in 0..a_cols {
            result[[i, j]] = op(&a[[i, j]], &b[j]);
        }
    }

    Ok(result)
}

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

    #[test]
    fn test_reshape_2d() {
        let a = array![[1, 2], [3, 4]];
        let b = reshape_2d(a.view(), (4, 1)).expect("Operation failed");
        assert_eq!(b.shape(), &[4, 1]);
        assert_eq!(b[[0, 0]], 1);
        assert_eq!(b[[1, 0]], 2);
        assert_eq!(b[[2, 0]], 3);
        assert_eq!(b[[3, 0]], 4);

        // Test invalid shape
        let result = reshape_2d(a.view(), (3, 1));
        assert!(result.is_err());
    }

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

        // Stack vertically (along axis 0)
        let c = stack_2d(&[a.view(), b.view()], 0).expect("Operation failed");
        assert_eq!(c.shape(), &[4, 2]);
        assert_eq!(c[[0, 0]], 1);
        assert_eq!(c[[1, 0]], 3);
        assert_eq!(c[[2, 0]], 5);
        assert_eq!(c[[3, 0]], 7);

        // Stack horizontally (along axis 1)
        let d = stack_2d(&[a.view(), b.view()], 1).expect("Operation failed");
        assert_eq!(d.shape(), &[2, 4]);
        assert_eq!(d[[0, 0]], 1);
        assert_eq!(d[[0, 1]], 2);
        assert_eq!(d[[0, 2]], 5);
        assert_eq!(d[[0, 3]], 6);
    }

    #[test]
    fn test_transpose_2d() {
        let a = array![[1, 2, 3], [4, 5, 6]];
        let b = transpose_2d(a.view());
        assert_eq!(b.shape(), &[3, 2]);
        assert_eq!(b[[0, 0]], 1);
        assert_eq!(b[[0, 1]], 4);
        assert_eq!(b[[1, 0]], 2);
        assert_eq!(b[[2, 1]], 6);
    }

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

        // Split along columns at index 2
        let result = split_2d(a.view(), &[2], 1).expect("Operation failed");
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].shape(), &[2, 2]);
        assert_eq!(result[0][[0, 0]], 1);
        assert_eq!(result[0][[0, 1]], 2);
        assert_eq!(result[0][[1, 0]], 5);
        assert_eq!(result[0][[1, 1]], 6);
        assert_eq!(result[1].shape(), &[2, 2]);
        assert_eq!(result[1][[0, 0]], 3);
        assert_eq!(result[1][[0, 1]], 4);
        assert_eq!(result[1][[1, 0]], 7);
        assert_eq!(result[1][[1, 1]], 8);

        // Split along rows at index 1
        let result = split_2d(a.view(), &[1], 0).expect("Operation failed");
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].shape(), &[1, 4]);
        assert_eq!(result[1].shape(), &[1, 4]);
    }

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

        // Take along columns
        let result = take_2d(a.view(), indices.view(), 1).expect("Operation failed");
        assert_eq!(result.shape(), &[2, 2]);
        assert_eq!(result[[0, 0]], 1);
        assert_eq!(result[[0, 1]], 3);
        assert_eq!(result[[1, 0]], 4);
        assert_eq!(result[[1, 1]], 6);
    }

    #[test]
    fn test_mask_select() {
        let a = array![[1, 2, 3], [4, 5, 6]];
        let mask = array![[true, false, true], [false, true, false]];

        let result = mask_select(a.view(), mask.view()).expect("Operation failed");
        assert_eq!(result.shape(), &[3]);
        assert_eq!(result[0], 1);
        assert_eq!(result[1], 3);
        assert_eq!(result[2], 5);
    }

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

        let result = fancy_index_2d(a.view(), row_indices.view(), col_indices.view())
            .expect("Operation failed");
        assert_eq!(result.shape(), &[2]);
        assert_eq!(result[0], 1);
        assert_eq!(result[1], 8);
    }

    #[test]
    fn test_where_condition() {
        let a = array![[1, 2, 3], [4, 5, 6]];
        let result = where_condition(a.view(), |&x| x > 3).expect("Operation failed");
        assert_eq!(result.shape(), &[3]);
        assert_eq!(result[0], 4);
        assert_eq!(result[1], 5);
        assert_eq!(result[2], 6);
    }

    #[test]
    fn test_broadcast_1d_to_2d() {
        let a = array![1, 2, 3];

        // Broadcast along rows (axis 0)
        let b = broadcast_1d_to_2d(a.view(), 2, 0).expect("Operation failed");
        assert_eq!(b.shape(), &[2, 3]);
        assert_eq!(b[[0, 0]], 1);
        assert_eq!(b[[0, 1]], 2);
        assert_eq!(b[[1, 0]], 1);
        assert_eq!(b[[1, 2]], 3);

        // Broadcast along columns (axis 1)
        let c = broadcast_1d_to_2d(a.view(), 2, 1).expect("Operation failed");
        assert_eq!(c.shape(), &[3, 2]);
        assert_eq!(c[[0, 0]], 1);
        assert_eq!(c[[0, 1]], 1);
        assert_eq!(c[[1, 0]], 2);
        assert_eq!(c[[2, 1]], 3);
    }

    #[test]
    fn test_broadcast_apply() {
        let a = array![[1, 2, 3], [4, 5, 6]];
        let b = array![10, 20, 30];

        let result = broadcast_apply(a.view(), b.view(), |x, y| x + y).expect("Operation failed");
        assert_eq!(result.shape(), &[2, 3]);
        assert_eq!(result[[0, 0]], 11);
        assert_eq!(result[[0, 1]], 22);
        assert_eq!(result[[0, 2]], 33);
        assert_eq!(result[[1, 0]], 14);
        assert_eq!(result[[1, 1]], 25);
        assert_eq!(result[[1, 2]], 36);

        let result = broadcast_apply(a.view(), b.view(), |x, y| x * y).expect("Operation failed");
        assert_eq!(result.shape(), &[2, 3]);
        assert_eq!(result[[0, 0]], 10);
        assert_eq!(result[[0, 1]], 40);
        assert_eq!(result[[0, 2]], 90);
        assert_eq!(result[[1, 0]], 40);
        assert_eq!(result[[1, 1]], 100);
        assert_eq!(result[[1, 2]], 180);
    }
}