scirs2-autograd 0.3.2

Automatic differentiation module for SciRS2 (scirs2-autograd)
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
//! Reduction tensor operations
//!
//! This module provides operations that reduce tensor dimensions by aggregating
//! values along specified axes:
//! - Basic reductions (sum, mean, product)
//! - Statistical reductions (min, max, variance, standard deviation)
//! - Advanced reductions (norm operations, aggregation functions)
//! - Specialized reductions (all, any, argmin, argmax)

use crate::tensor::{AsTensor, Tensor};
use crate::Float;

// Import internal operation modules
use crate::tensor_ops::{array_ops, math_ops, reduction_ops, shape};

/// Takes sum along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to compute the sum
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::reduce_sum;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[1., 2.], [3., 4.]], g);
///    let s1 = reduce_sum(x, &[0], false); // sum along axis 0
///    assert_eq!(s1.eval(g), Ok(array![4., 6.].into_dyn()));
///
///    let s2 = reduce_sum(x, &[1], true); // sum along axis 1, keep dims
///    assert_eq!(s2.eval(g), Ok(array![[3.], [7.]].into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn reduce_sum<'graph, A, AT, F: Float>(x: A, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .append_input(axes.as_tensor(g), false)
        .build(reduction_ops::ReduceSum {
            keep_dims,
            sparse_axes: false,
        })
}

/// Takes mean along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to compute the mean
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::reduce_mean;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[1., 2.], [3., 4.]], g);
///    let m = reduce_mean(x, &[0], false);
///    assert_eq!(m.eval(g), Ok(array![2., 3.].into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn reduce_mean<'graph, A, AT, F: Float>(x: A, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .append_input(axes.as_tensor(g), false)
        .build(reduction_ops::ReduceMean {
            keep_dims,
            sparse_axes: false,
        })
}

/// Takes product along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to compute the product
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::reduce_prod;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[1., 2.], [3., 4.]], g);
///    let p = reduce_prod(x, &[0], false);
///    assert_eq!(p.eval(g), Ok(array![3., 8.].into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn reduce_prod<'graph, A, AT, F: Float>(x: A, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .append_input(axes.as_tensor(g), false)
        .build(reduction_ops::ReduceProd {
            keep_dims,
            sparse_axes: false,
        })
}

/// Takes min along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to compute the minimum
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::reduce_min;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[1., 4.], [2., 3.]], g);
///    let min_val = reduce_min(x, &[0], false);
///    assert_eq!(min_val.eval(g), Ok(array![1., 3.].into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn reduce_min<'graph, A, AT, F: Float>(x: A, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .append_input(axes.as_tensor(g), false)
        .build(reduction_ops::ReduceMin {
            keep_dims,
            sparse_axes: false,
        })
}

/// Takes max along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to compute the maximum
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::reduce_max;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[1., 4.], [2., 3.]], g);
///    let max_val = reduce_max(x, &[0], false);
///    assert_eq!(max_val.eval(g), Ok(array![2., 4.].into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn reduce_max<'graph, A, AT, F: Float>(x: A, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .append_input(axes.as_tensor(g), false)
        .build(reduction_ops::ReduceMax {
            keep_dims,
            sparse_axes: false,
        })
}

/// Computes variance along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to compute the variance
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::reduce_variance;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[1., 2.], [3., 4.]], g);
///    let var = reduce_variance(x, &[0], false);
///    // Variance of [1,3] and [2,4] along axis 0
///    assert_eq!(var.eval(g), Ok(array![1., 1.].into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn reduce_variance<'graph, A, AT, F: Float>(
    x: A,
    axes: &AT,
    keep_dims: bool,
) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .append_input(axes.as_tensor(g), false)
        .build(reduction_ops::ReduceVariance {
            keep_dims,
            sparse_axes: false,
        })
}

/// Computes standard deviation along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to compute the standard deviation
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
#[allow(dead_code)]
pub fn reduce_std<'graph, A, AT, F: Float>(x: A, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let variance = reduce_variance(x, axes, keep_dims);
    crate::tensor_ops::arithmetic::sqrt(variance)
}

/// Sums all elements in the tensor.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::sum_all;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[1., 2.], [3., 4.]], g);
///    let total = sum_all(x);
///    assert_eq!(total.eval(g), Ok(scirs2_core::ndarray::arr0(10.).into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn sum_all<'graph, A, F: Float>(x: A) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .build(reduction_ops::ReduceSumAll)
}

/// Computes the mean of all elements in the tensor.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::mean_all;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[1., 2.], [3., 4.]], g);
///    let avg = mean_all(x);
///    assert_eq!(avg.eval(g), Ok(scirs2_core::ndarray::arr0(2.5).into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn mean_all<'graph, A, F: Float>(x: A) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .build(reduction_ops::ReduceMeanAll)
}

/// Finds the indices of the maximum values along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axis` - Axis along which to find argmax
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::argmax;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[1., 4.], [2., 3.]], g);
///    let indices = argmax(x, 0, false);
///    assert_eq!(indices.eval(g), Ok(array![1., 0.].into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn argmax<'graph, A, F: Float>(x: A, axis: isize, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .build(reduction_ops::ArgMax {
            axis,
            keep_dim: keep_dims,
        })
}

/// Finds the indices of the minimum values along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axis` - Axis along which to find argmin
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::argmin;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[1., 4.], [2., 3.]], g);
///    let indices = argmin(x, 0, false);
///    assert_eq!(indices.eval(g), Ok(array![0., 1.].into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn argmin<'graph, A, F: Float>(x: A, axis: isize, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .build(reduction_ops::ArgMin {
            axis,
            keep_dim: keep_dims,
        })
}

/// Computes `log(sum(exp(x)))` along specified axis.
///
/// `axis` can be negative.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::reduce_logsumexp;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![1., 2., 3.], g);
///    let lse = reduce_logsumexp(x, 0, false);
///    // Should be approximately log(e^1 + e^2 + e^3) ≈ 3.407
///    let result = lse.eval(g).expect("Operation failed");
///    assert!((result[scirs2_core::ndarray::IxDyn(&[])] - 3.407_f64).abs() < 0.01);
/// });
/// ```
#[allow(dead_code)]
pub fn reduce_logsumexp<'graph, A, F: Float>(x: A, axis: isize, keep_dim: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
{
    let x = x.as_ref();
    let g = x.graph();
    let op = math_ops::LogSumExp {
        axis,
        keep_dims: keep_dim,
    };
    Tensor::builder(g).append_input(x.as_ref(), false).build(op)
}

/// Adds all input tensors, element-wise.
///
/// All the input tensors must have same shapes.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::add_n;
///
/// ag::run(|g| {
///    let a = ag::tensor_ops::ones((&[2, 2]), g);
///    let b = ag::tensor_ops::ones((&[2, 2]), g);
///    let c = ag::tensor_ops::ones((&[2, 2]), g);
///    let d = add_n(&[a, b, c]);
///
///    assert_eq!(d.eval(g).expect("Operation failed").shape(), &[2, 2]);
///    assert_eq!(d.eval(g), Ok(array![[3., 3.], [3., 3.]].into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn add_n<'graph, A, F: Float>(xs: &[A]) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
{
    let len = xs.len();
    assert_ne!(len, 0);
    if len == 1 {
        *xs[0].as_ref()
    } else {
        let g = xs[0].as_ref().graph();
        let mut b = Tensor::builder(g);
        for x in xs {
            b = b.append_input(x.as_ref(), false);
        }
        b.setshape(&shape(xs[0])).build(array_ops::AddN)
    }
}

/// Computes L1 norm along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to compute the norm
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
#[allow(dead_code)]
pub fn l1_norm<'graph, A, AT, F: Float>(x: A, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let abs_x = crate::tensor_ops::arithmetic::abs(x);
    reduce_sum(abs_x, axes, keep_dims)
}

/// Computes L2 norm along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to compute the norm
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
#[allow(dead_code)]
pub fn l2_norm<'graph, A, AT, F: Float>(x: A, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let square_x = crate::tensor_ops::arithmetic::square(x);
    let sum_square = reduce_sum(square_x, axes, keep_dims);
    crate::tensor_ops::arithmetic::sqrt(sum_square)
}

/// Computes Lp norm along specified axes.
///
/// # Arguments
/// * `x` - Input tensor
/// * `p` - The order of the norm
/// * `axes` - Axes along which to compute the norm
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
#[allow(dead_code)]
pub fn lp_norm<'graph, A, AT, F: Float>(x: A, p: F, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let abs_x = crate::tensor_ops::arithmetic::abs(x);
    let pow_x = crate::tensor_ops::arithmetic::pow(abs_x, p);
    let sum_pow = reduce_sum(pow_x, axes, keep_dims);
    let one_over_p = F::one() / p;
    crate::tensor_ops::arithmetic::pow(sum_pow, one_over_p)
}

/// Computes the Frobenius norm (L2 norm of flattened tensor).
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_autograd as ag;
/// use ag::tensor_ops::reduction::frobenius_norm;
///
/// ag::run(|g| {
///    let x = ag::tensor_ops::convert_to_tensor(array![[3., 4.], [0., 0.]], g);
///    let norm = frobenius_norm(x);
///    assert_eq!(norm.eval(g), Ok(scirs2_core::ndarray::arr0(5.).into_dyn()));
/// });
/// ```
#[allow(dead_code)]
pub fn frobenius_norm<'graph, A, F: Float>(x: A) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
{
    let x = x.as_ref();
    let square_x = crate::tensor_ops::arithmetic::square(x);
    let sum_square = sum_all(square_x);
    crate::tensor_ops::arithmetic::sqrt(sum_square)
}

/// Tests if all elements evaluate to true.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to test
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
#[allow(dead_code)]
pub fn reduce_all<'graph, A, AT, F: Float>(x: A, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .append_input(axes.as_tensor(g), false)
        .build(reduction_ops::ReduceAll { keep_dims })
}

/// Tests if any element evaluates to true.
///
/// # Arguments
/// * `x` - Input tensor
/// * `axes` - Axes along which to test
/// * `keep_dims` - If true, keeps reduced dimensions as size 1
#[allow(dead_code)]
pub fn reduce_any<'graph, A, AT, F: Float>(x: A, axes: &AT, keep_dims: bool) -> Tensor<'graph, F>
where
    A: AsRef<Tensor<'graph, F>> + Copy,
    AT: AsTensor<'graph, F>,
{
    let x = x.as_ref();
    let g = x.graph();
    Tensor::builder(g)
        .append_input(x.as_ref(), false)
        .append_input(axes.as_tensor(g), false)
        .build(reduction_ops::ReduceAny { keep_dims })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tensor_ops::convert_to_tensor;
    #[allow(unused_imports)]
    use approx::assert_relative_eq;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_basic_reductions() {
        crate::run(|g| {
            let x = convert_to_tensor(array![[1.0_f32, 2.0], [3.0, 4.0]], g);

            // Test sum reduction
            let sum_result = reduce_sum(x, &[0], false);
            let expected_sum = array![4.0_f32, 6.0];
            assert_eq!(
                sum_result.eval(g).expect("Operation failed"),
                expected_sum.into_dyn()
            );

            // Test mean reduction
            let mean_result = reduce_mean(x, &[0], false);
            let expected_mean = array![2.0_f32, 3.0];
            assert_eq!(
                mean_result.eval(g).expect("Operation failed"),
                expected_mean.into_dyn()
            );

            // Test product reduction
            let prod_result = reduce_prod(x, &[0], false);
            let expected_prod = array![3.0_f32, 8.0];
            assert_eq!(
                prod_result.eval(g).expect("Operation failed"),
                expected_prod.into_dyn()
            );
        });
    }

    #[test]
    fn test_min_max_reductions() {
        crate::run(|g| {
            let x = convert_to_tensor(array![[1.0_f32, 4.0], [2.0, 3.0]], g);

            // Test min reduction
            let min_result = reduce_min(x, &[0], false);
            let expected_min = array![1.0_f32, 3.0];
            assert_eq!(
                min_result.eval(g).expect("Operation failed"),
                expected_min.into_dyn()
            );

            // Test max reduction
            let max_result = reduce_max(x, &[0], false);
            let expected_max = array![2.0_f32, 4.0];
            assert_eq!(
                max_result.eval(g).expect("Operation failed"),
                expected_max.into_dyn()
            );
        });
    }

    #[test]
    fn test_statistical_reductions() {
        crate::run(|g| {
            let x = convert_to_tensor(array![[1.0_f32, 2.0], [3.0, 4.0]], g);

            // Test variance reduction
            let var_result = reduce_variance(x, &[0], false);
            let expected_var = array![1.0_f32, 1.0]; // Variance of [1,3] and [2,4]
            assert_eq!(
                var_result.eval(g).expect("Operation failed"),
                expected_var.into_dyn()
            );

            // Test standard deviation
            let std_result = reduce_std(x, &[0], false);
            let expected_std = array![1.0_f32, 1.0];
            assert_eq!(
                std_result.eval(g).expect("Operation failed"),
                expected_std.into_dyn()
            );
        });
    }

    #[test]
    fn test_global_reductions() {
        crate::run(|g| {
            let x = convert_to_tensor(array![[1.0_f32, 2.0], [3.0, 4.0]], g);

            // Test sum all
            let sum_all_result = sum_all(x);
            assert_eq!(
                sum_all_result.eval(g).expect("Operation failed"),
                scirs2_core::ndarray::arr0(10.0).into_dyn()
            );

            // Test mean all
            let mean_all_result = mean_all(x);
            assert_eq!(
                mean_all_result.eval(g).expect("Operation failed"),
                scirs2_core::ndarray::arr0(2.5).into_dyn()
            );
        });
    }

    #[test]
    fn test_norm_operations() {
        crate::run(|g| {
            let x = convert_to_tensor(array![[3.0_f32, 4.0], [0.0, 0.0]], g);

            // Test Frobenius norm
            let frob_norm = frobenius_norm(x);
            assert_eq!(
                frob_norm.eval(g).expect("Operation failed"),
                scirs2_core::ndarray::arr0(5.0).into_dyn()
            );

            // Test L1 norm
            let l1_result = l1_norm(x, &[0], false);
            let expected_l1 = array![3.0_f32, 4.0];
            assert_eq!(
                l1_result.eval(g).expect("Operation failed"),
                expected_l1.into_dyn()
            );

            // Test L2 norm
            let l2_result = l2_norm(x, &[0], false);
            let expected_l2 = array![3.0_f32, 4.0];
            assert_eq!(
                l2_result.eval(g).expect("Operation failed"),
                expected_l2.into_dyn()
            );
        });
    }

    #[test]
    fn test_add_n() {
        crate::run(|g| {
            let a = convert_to_tensor(array![1.0_f32, 2.0], g);
            let b = convert_to_tensor(array![3.0_f32, 4.0], g);
            let c = convert_to_tensor(array![5.0_f32, 6.0], g);

            let result = add_n(&[a, b, c]);
            let expected = array![9.0_f32, 12.0];
            assert_eq!(
                result.eval(g).expect("Operation failed"),
                expected.into_dyn()
            );
        });
    }

    #[test]
    fn test_keep_dims() {
        crate::run(|g| {
            let x = convert_to_tensor(array![[1.0_f32, 2.0], [3.0, 4.0]], g);

            // Test with keep_dims=true
            let sum_result = reduce_sum(x, &[0], true);
            assert_eq!(
                sum_result.eval(g).expect("Operation failed").shape(),
                &[1, 2]
            );

            // Test with keep_dims=false
            let sum_result = reduce_sum(x, &[0], false);
            assert_eq!(sum_result.eval(g).expect("Operation failed").shape(), &[2]);
        });
    }
}