scirs2-core 0.4.4

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
// Copyright (c) 2025, `SciRS2` Team
//
// Licensed under the Apache License, Version 2.0
// (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
//

//! Tests for the grad module — split out to keep grad.rs under 2000 lines.

use super::*;
use ::ndarray::{array, Array2, ArrayD, Ix2, IxDyn};

#[test]
fn test_gradient_tensor_creation() {
    // Create a gradient tensor
    let array = Array2::<f64>::ones((2, 2));
    let tensor = GradientTensor::from_array(array, true);

    // Check properties
    assert!(tensor.requiresgrad());
    assert!(tensor.is_leaf());
    assert!(tensor.grad_2().is_none());
}

#[test]
fn test_gradient_computation_add() {
    // Import will be used when the test is enabled
    #[allow(unused_imports)]
    use ::ndarray::array;

    // Create gradient tensors
    let a_array = Array2::<f64>::ones((2, 2));
    let b_array = Array2::<f64>::ones((2, 2)) * 2.0;

    let a = GradientTensor::from_array(a_array, true);
    let b = GradientTensor::from_array(b_array, true);

    // Perform addition - skip test if operation not implemented
    let c = match grad_add(&a, &b) {
        Ok(c) => c,
        Err(e) => {
            println!("Skipping test_gradient_computationadd: {e}");
            return;
        }
    };

    // Check result
    let c_value = c.value();
    let c_array = match c_value.as_any().downcast_ref::<NdarrayWrapper<f64, Ix2>>() {
        Some(array) => array,
        None => {
            println!("Skipping test_gradient_computationadd: result is not the expected type");
            return;
        }
    };
    assert_eq!(c_array.as_array(), &array![[3.0, 3.0], [3.0, 3.0]]);

    // Compute gradients
    if let Err(e) = c.backward() {
        println!("Skipping test_gradient_computationadd: {e}");
        return;
    }

    // Check gradients
    let a_grad = match a.grad_2() {
        Some(grad) => grad,
        None => {
            println!("Skipping test_gradient_computationadd: no gradient for a");
            return;
        }
    };

    let a_grad_array = match a_grad.as_any().downcast_ref::<NdarrayWrapper<f64, Ix2>>() {
        Some(array) => array,
        None => {
            println!("Skipping test_gradient_computationadd: a_grad is not the expected type");
            return;
        }
    };
    assert_eq!(a_grad_array.as_array(), &array![[1.0, 1.0], [1.0, 1.0]]);

    let b_grad = match b.grad_2() {
        Some(grad) => grad,
        None => {
            println!("Skipping test_gradient_computationadd: no gradient for b");
            return;
        }
    };

    let b_grad_array = match b_grad.as_any().downcast_ref::<NdarrayWrapper<f64, Ix2>>() {
        Some(array) => array,
        None => {
            println!("Skipping test_gradient_computationadd: b_grad is not the expected type");
            return;
        }
    };
    assert_eq!(b_grad_array.as_array(), &array![[1.0, 1.0], [1.0, 1.0]]);
}

#[test]
fn test_gradient_computation_multiply() {
    // Import will be used when the test is enabled
    #[allow(unused_imports)]
    use ::ndarray::array;

    // Create gradient tensors
    let a_array = Array2::<f64>::ones((2, 2)) * 2.0;
    let b_array = Array2::<f64>::ones((2, 2)) * 3.0;

    let a = GradientTensor::from_array(a_array, true);
    let b = GradientTensor::from_array(b_array, true);

    // Perform multiplication - skip test if operation not implemented
    let c = match grad_multiply(&a, &b) {
        Ok(c) => c,
        Err(e) => {
            println!("Skipping test_gradient_computationmultiply: {e}");
            return;
        }
    };

    // Check result
    let c_value = c.value();
    let c_array = match c_value.as_any().downcast_ref::<NdarrayWrapper<f64, Ix2>>() {
        Some(array) => array,
        None => {
            println!(
                "Skipping test_gradient_computation_multiply: result is not the expected type"
            );
            return;
        }
    };
    assert_eq!(c_array.as_array(), &array![[6.0, 6.0], [6.0, 6.0]]);

    // Compute gradients
    if let Err(e) = c.backward() {
        println!("Skipping test_gradient_computationmultiply: {e}");
        return;
    }

    // Check gradients
    let a_grad = match a.grad_2() {
        Some(grad) => grad,
        None => {
            println!("Skipping test_gradient_computationmultiply: no gradient for a");
            return;
        }
    };

    let a_grad_array = match a_grad.as_any().downcast_ref::<NdarrayWrapper<f64, Ix2>>() {
        Some(array) => array,
        None => {
            println!(
                "Skipping test_gradient_computation_multiply: a_grad is not the expected type"
            );
            return;
        }
    };
    assert_eq!(a_grad_array.as_array(), &array![[3.0, 3.0], [3.0, 3.0]]);

    let b_grad = match b.grad_2() {
        Some(grad) => grad,
        None => {
            println!("Skipping test_gradient_computationmultiply: no gradient for b");
            return;
        }
    };

    let b_grad_array = match b_grad.as_any().downcast_ref::<NdarrayWrapper<f64, Ix2>>() {
        Some(array) => array,
        None => {
            println!(
                "Skipping test_gradient_computation_multiply: b_grad is not the expected type"
            );
            return;
        }
    };
    assert_eq!(b_grad_array.as_array(), &array![[2.0, 2.0], [2.0, 2.0]]);
}

#[test]
fn test_sgd_optimizer() {
    // Import will be used when the test is enabled
    #[allow(unused_imports)]
    use ::ndarray::array;

    // Create variables
    let weight_array = Array2::<f64>::ones((2, 2));
    let weight = Variable::new("weight", weight_array);

    let bias_array = Array2::<f64>::zeros((2, 2));
    let bias = Variable::new("bias", bias_array);

    // Create optimizer
    let mut optimizer = SGD::new(0.1, Some(0.9));
    optimizer.add_variable(weight);
    optimizer.add_variable(bias);

    // Manually set gradients for testing
    let weight_grad_array = Array2::<f64>::ones((2, 2));
    let weight_grad = NdarrayWrapper::new(weight_grad_array);
    optimizer.variables()[0].tensor.node.borrow_mut().grad = Some(Rc::new(weight_grad));

    let bias_grad_array = Array2::<f64>::ones((2, 2)) * 2.0;
    let bias_grad = NdarrayWrapper::new(bias_grad_array);
    optimizer.variables()[1].tensor.node.borrow_mut().grad = Some(Rc::new(bias_grad));

    // Take an optimization step
    match optimizer.step() {
        Ok(_) => {
            // Zero gradients
            optimizer.zero_grad();

            // Check that gradients are zeroed
            assert!(optimizer.variables()[0].grad_2().is_none());
            assert!(optimizer.variables()[1].grad_2().is_none());
        }
        Err(e) => {
            println!("Skipping test_sgd_optimizer - step failed: {e}");
        }
    }
}

// ---- integer dtype coverage tests ----

fn make_i32_tensor(values: &[i32]) -> GradientTensor {
    let arr =
        ArrayD::<i32>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    GradientTensor::from_array(arr, true)
}

fn make_i64_tensor(values: &[i64]) -> GradientTensor {
    let arr =
        ArrayD::<i64>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    GradientTensor::from_array(arr, true)
}

fn make_u8_tensor(values: &[u8]) -> GradientTensor {
    let arr =
        ArrayD::<u8>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    GradientTensor::from_array(arr, true)
}

fn make_u16_tensor(values: &[u16]) -> GradientTensor {
    let arr =
        ArrayD::<u16>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    GradientTensor::from_array(arr, true)
}

fn make_u32_tensor(values: &[u32]) -> GradientTensor {
    let arr =
        ArrayD::<u32>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    GradientTensor::from_array(arr, true)
}

fn make_u64_tensor(values: &[u64]) -> GradientTensor {
    let arr =
        ArrayD::<u64>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    GradientTensor::from_array(arr, true)
}

fn make_i32_protocol(values: &[i32]) -> Box<dyn ArrayProtocol> {
    let arr =
        ArrayD::<i32>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    Box::new(NdarrayWrapper::new(arr))
}

fn make_u32_protocol(values: &[u32]) -> Box<dyn ArrayProtocol> {
    let arr =
        ArrayD::<u32>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    Box::new(NdarrayWrapper::new(arr))
}

fn make_u8_protocol(values: &[u8]) -> Box<dyn ArrayProtocol> {
    let arr =
        ArrayD::<u8>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    Box::new(NdarrayWrapper::new(arr))
}

fn make_u16_protocol(values: &[u16]) -> Box<dyn ArrayProtocol> {
    let arr =
        ArrayD::<u16>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    Box::new(NdarrayWrapper::new(arr))
}

fn make_u64_protocol(values: &[u64]) -> Box<dyn ArrayProtocol> {
    let arr =
        ArrayD::<u64>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    Box::new(NdarrayWrapper::new(arr))
}

fn make_i64_protocol(values: &[i64]) -> Box<dyn ArrayProtocol> {
    let arr =
        ArrayD::<i64>::from_shape_vec(IxDyn(&[values.len()]), values.to_vec()).expect("shape vec");
    Box::new(NdarrayWrapper::new(arr))
}

fn extract_f64_result(proto: &dyn ArrayProtocol) -> Vec<f64> {
    proto
        .as_any()
        .downcast_ref::<NdarrayWrapper<f64, IxDyn>>()
        .expect("expected f64 result")
        .as_array()
        .iter()
        .copied()
        .collect()
}

fn extract_i32_result(proto: &dyn ArrayProtocol) -> Vec<i32> {
    proto
        .as_any()
        .downcast_ref::<NdarrayWrapper<i32, IxDyn>>()
        .expect("expected i32 result")
        .as_array()
        .iter()
        .copied()
        .collect()
}

fn extract_u32_result(proto: &dyn ArrayProtocol) -> Vec<u32> {
    proto
        .as_any()
        .downcast_ref::<NdarrayWrapper<u32, IxDyn>>()
        .expect("expected u32 result")
        .as_array()
        .iter()
        .copied()
        .collect()
}

#[test]
fn test_grad_mean_i32() {
    // mean([1,2,3]) == 2.0
    let t = make_i32_tensor(&[1, 2, 3]);
    let result = grad_mean(&t).expect("grad_mean i32");
    let val = result.value();
    let out = extract_f64_result(val.as_ref());
    assert_eq!(out.len(), 1);
    assert!((out[0] - 2.0).abs() < 1e-12, "expected 2.0 got {}", out[0]);
}

#[test]
fn test_grad_mean_i64() {
    // mean([10,20,30]) == 20.0
    let t = make_i64_tensor(&[10, 20, 30]);
    let result = grad_mean(&t).expect("grad_mean i64");
    let val = result.value();
    let out = extract_f64_result(val.as_ref());
    assert!(
        (out[0] - 20.0).abs() < 1e-12,
        "expected 20.0 got {}",
        out[0]
    );
}

#[test]
fn test_grad_mean_u8() {
    // mean([2,4,6]) == 4.0
    let t = make_u8_tensor(&[2, 4, 6]);
    let result = grad_mean(&t).expect("grad_mean u8");
    let val = result.value();
    let out = extract_f64_result(val.as_ref());
    assert!((out[0] - 4.0).abs() < 1e-12, "expected 4.0 got {}", out[0]);
}

#[test]
fn test_grad_mean_u16() {
    // mean([100,200,300]) == 200.0
    let t = make_u16_tensor(&[100, 200, 300]);
    let result = grad_mean(&t).expect("grad_mean u16");
    let val = result.value();
    let out = extract_f64_result(val.as_ref());
    assert!(
        (out[0] - 200.0).abs() < 1e-12,
        "expected 200.0 got {}",
        out[0]
    );
}

#[test]
fn test_grad_mean_u32() {
    // mean([3,6,9]) == 6.0
    let t = make_u32_tensor(&[3, 6, 9]);
    let result = grad_mean(&t).expect("grad_mean u32");
    let val = result.value();
    let out = extract_f64_result(val.as_ref());
    assert!((out[0] - 6.0).abs() < 1e-12, "expected 6.0 got {}", out[0]);
}

#[test]
fn test_grad_mean_u64() {
    // mean([1000,2000,3000]) == 2000.0
    let t = make_u64_tensor(&[1000, 2000, 3000]);
    let result = grad_mean(&t).expect("grad_mean u64");
    let val = result.value();
    let out = extract_f64_result(val.as_ref());
    assert!(
        (out[0] - 2000.0).abs() < 1e-12,
        "expected 2000.0 got {}",
        out[0]
    );
}

#[test]
fn test_multiply_by_scalar_u32() {
    // [2,4,6] * 3.0 -> [6,12,18]
    let a = make_u32_protocol(&[2, 4, 6]);
    let result = multiply_by_scalar(a.as_ref(), 3.0).expect("multiply_by_scalar u32");
    let out = extract_u32_result(result.as_ref());
    assert_eq!(out, vec![6u32, 12, 18]);
}

#[test]
fn test_multiply_by_scalar_u8() {
    // [1,2,3] * 4.0 -> [4,8,12]
    let a = make_u8_protocol(&[1, 2, 3]);
    let result = multiply_by_scalar(a.as_ref(), 4.0).expect("multiply_by_scalar u8");
    let out = result
        .as_any()
        .downcast_ref::<NdarrayWrapper<u8, IxDyn>>()
        .expect("u8 result")
        .as_array()
        .iter()
        .copied()
        .collect::<Vec<u8>>();
    assert_eq!(out, vec![4u8, 8, 12]);
}

#[test]
fn test_multiply_by_scalar_u16() {
    // [10,20,30] * 2.0 -> [20,40,60]
    let a = make_u16_protocol(&[10, 20, 30]);
    let result = multiply_by_scalar(a.as_ref(), 2.0).expect("multiply_by_scalar u16");
    let out = result
        .as_any()
        .downcast_ref::<NdarrayWrapper<u16, IxDyn>>()
        .expect("u16 result")
        .as_array()
        .iter()
        .copied()
        .collect::<Vec<u16>>();
    assert_eq!(out, vec![20u16, 40, 60]);
}

#[test]
fn test_multiply_by_scalar_u64() {
    // [100,200,300] * 5.0 -> [500,1000,1500]
    let a = make_u64_protocol(&[100, 200, 300]);
    let result = multiply_by_scalar(a.as_ref(), 5.0).expect("multiply_by_scalar u64");
    let out = result
        .as_any()
        .downcast_ref::<NdarrayWrapper<u64, IxDyn>>()
        .expect("u64 result")
        .as_array()
        .iter()
        .copied()
        .collect::<Vec<u64>>();
    assert_eq!(out, vec![500u64, 1000, 1500]);
}

#[test]
fn test_subtract_arrays_int_types() {
    // i32: [5,6,7] - [1,2,3] == [4,4,4]
    let a = make_i32_protocol(&[5, 6, 7]);
    let b = make_i32_protocol(&[1, 2, 3]);
    let result = subtract_arrays(a.as_ref(), b.as_ref()).expect("subtract i32");
    let out = extract_i32_result(result.as_ref());
    assert_eq!(out, vec![4i32, 4, 4]);

    // u8: [10,20,30] - [1,2,3] == [9,18,27]
    let ua = make_u8_protocol(&[10, 20, 30]);
    let ub = make_u8_protocol(&[1, 2, 3]);
    let ures = subtract_arrays(ua.as_ref(), ub.as_ref()).expect("subtract u8");
    let uout = ures
        .as_any()
        .downcast_ref::<NdarrayWrapper<u8, IxDyn>>()
        .expect("u8")
        .as_array()
        .iter()
        .copied()
        .collect::<Vec<u8>>();
    assert_eq!(uout, vec![9u8, 18, 27]);

    // u32: [100,200,300] - [10,20,30] == [90,180,270]
    let u32a = make_u32_protocol(&[100, 200, 300]);
    let u32b = make_u32_protocol(&[10, 20, 30]);
    let u32res = subtract_arrays(u32a.as_ref(), u32b.as_ref()).expect("subtract u32");
    let u32out = extract_u32_result(u32res.as_ref());
    assert_eq!(u32out, vec![90u32, 180, 270]);
}

#[test]
fn test_sqrt_i32() {
    // sqrt([4, 9, 16]) -> [2.0, 3.0, 4.0]
    let a = make_i32_protocol(&[4, 9, 16]);
    let result = sqrt(a.as_ref()).expect("sqrt i32");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 2.0).abs() < 1e-12);
    assert!((out[1] - 3.0).abs() < 1e-12);
    assert!((out[2] - 4.0).abs() < 1e-12);
}

#[test]
fn test_sqrt_i64() {
    // sqrt([25, 36, 49]) -> [5.0, 6.0, 7.0]
    let a = make_i64_protocol(&[25, 36, 49]);
    let result = sqrt(a.as_ref()).expect("sqrt i64");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 5.0).abs() < 1e-12);
    assert!((out[1] - 6.0).abs() < 1e-12);
    assert!((out[2] - 7.0).abs() < 1e-12);
}

#[test]
fn test_sqrt_u8() {
    // sqrt([1, 4, 9]) -> [1.0, 2.0, 3.0]
    let a = make_u8_protocol(&[1, 4, 9]);
    let result = sqrt(a.as_ref()).expect("sqrt u8");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 1.0).abs() < 1e-12);
    assert!((out[1] - 2.0).abs() < 1e-12);
    assert!((out[2] - 3.0).abs() < 1e-12);
}

#[test]
fn test_sqrt_u16() {
    // sqrt([100, 225, 400]) -> [10.0, 15.0, 20.0]
    let a = make_u16_protocol(&[100, 225, 400]);
    let result = sqrt(a.as_ref()).expect("sqrt u16");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 10.0).abs() < 1e-12);
    assert!((out[1] - 15.0).abs() < 1e-12);
    assert!((out[2] - 20.0).abs() < 1e-12);
}

#[test]
fn test_sqrt_u32() {
    // sqrt([1, 4, 9]) -> [1.0, 2.0, 3.0]
    let a = make_u32_protocol(&[1, 4, 9]);
    let result = sqrt(a.as_ref()).expect("sqrt u32");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 1.0).abs() < 1e-12);
    assert!((out[1] - 2.0).abs() < 1e-12);
    assert!((out[2] - 3.0).abs() < 1e-12);
}

#[test]
fn test_sqrt_u64() {
    // sqrt([4, 9, 16]) -> [2.0, 3.0, 4.0]
    let a = make_u64_protocol(&[4, 9, 16]);
    let result = sqrt(a.as_ref()).expect("sqrt u64");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 2.0).abs() < 1e-12);
    assert!((out[1] - 3.0).abs() < 1e-12);
    assert!((out[2] - 4.0).abs() < 1e-12);
}

#[test]
fn test_add_scalar_u8() {
    // [1,2,3] + 10 -> [11,12,13]
    let a = make_u8_protocol(&[1, 2, 3]);
    let result = add_scalar(a.as_ref(), 10.0).expect("add_scalar u8");
    let out = result
        .as_any()
        .downcast_ref::<NdarrayWrapper<u8, IxDyn>>()
        .expect("u8")
        .as_array()
        .iter()
        .copied()
        .collect::<Vec<u8>>();
    assert_eq!(out, vec![11u8, 12, 13]);
}

#[test]
fn test_add_scalar_u16() {
    // [10,20,30] + 5 -> [15,25,35]
    let a = make_u16_protocol(&[10, 20, 30]);
    let result = add_scalar(a.as_ref(), 5.0).expect("add_scalar u16");
    let out = result
        .as_any()
        .downcast_ref::<NdarrayWrapper<u16, IxDyn>>()
        .expect("u16")
        .as_array()
        .iter()
        .copied()
        .collect::<Vec<u16>>();
    assert_eq!(out, vec![15u16, 25, 35]);
}

#[test]
fn test_add_scalar_u32() {
    // [100,200,300] + 50 -> [150,250,350]
    let a = make_u32_protocol(&[100, 200, 300]);
    let result = add_scalar(a.as_ref(), 50.0).expect("add_scalar u32");
    let out = extract_u32_result(result.as_ref());
    assert_eq!(out, vec![150u32, 250, 350]);
}

#[test]
fn test_add_scalar_u64() {
    // [1000,2000,3000] + 500 -> [1500,2500,3500]
    let a = make_u64_protocol(&[1000, 2000, 3000]);
    let result = add_scalar(a.as_ref(), 500.0).expect("add_scalar u64");
    let out = result
        .as_any()
        .downcast_ref::<NdarrayWrapper<u64, IxDyn>>()
        .expect("u64")
        .as_array()
        .iter()
        .copied()
        .collect::<Vec<u64>>();
    assert_eq!(out, vec![1500u64, 2500, 3500]);
}

#[test]
fn test_divide_i32() {
    // [6,8,9] / [2,4,3] -> [3.0, 2.0, 3.0]
    let a = make_i32_protocol(&[6, 8, 9]);
    let b = make_i32_protocol(&[2, 4, 3]);
    let result = divide(a.as_ref(), b.as_ref()).expect("divide i32");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 3.0).abs() < 1e-12);
    assert!((out[1] - 2.0).abs() < 1e-12);
    assert!((out[2] - 3.0).abs() < 1e-12);
}

#[test]
fn test_divide_i64() {
    // [100,200,300] / [10,20,30] -> [10.0, 10.0, 10.0]
    let a = make_i64_protocol(&[100, 200, 300]);
    let b = make_i64_protocol(&[10, 20, 30]);
    let result = divide(a.as_ref(), b.as_ref()).expect("divide i64");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 10.0).abs() < 1e-12);
    assert!((out[1] - 10.0).abs() < 1e-12);
    assert!((out[2] - 10.0).abs() < 1e-12);
}

#[test]
fn test_divide_u8() {
    // [4,8,12] / [2,4,3] -> [2.0, 2.0, 4.0]
    let a = make_u8_protocol(&[4, 8, 12]);
    let b = make_u8_protocol(&[2, 4, 3]);
    let result = divide(a.as_ref(), b.as_ref()).expect("divide u8");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 2.0).abs() < 1e-12);
    assert!((out[1] - 2.0).abs() < 1e-12);
    assert!((out[2] - 4.0).abs() < 1e-12);
}

#[test]
fn test_divide_u16() {
    // [100,200,300] / [10,20,30] -> [10.0, 10.0, 10.0]
    let a = make_u16_protocol(&[100, 200, 300]);
    let b = make_u16_protocol(&[10, 20, 30]);
    let result = divide(a.as_ref(), b.as_ref()).expect("divide u16");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 10.0).abs() < 1e-12);
    assert!((out[1] - 10.0).abs() < 1e-12);
    assert!((out[2] - 10.0).abs() < 1e-12);
}

#[test]
fn test_divide_u32() {
    // [6,8,10] / [2,4,5] -> [3.0, 2.0, 2.0]
    let a = make_u32_protocol(&[6, 8, 10]);
    let b = make_u32_protocol(&[2, 4, 5]);
    let result = divide(a.as_ref(), b.as_ref()).expect("divide u32");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 3.0).abs() < 1e-12);
    assert!((out[1] - 2.0).abs() < 1e-12);
    assert!((out[2] - 2.0).abs() < 1e-12);
}

#[test]
fn test_divide_u64() {
    // [1000,2000,3000] / [100,200,300] -> [10.0, 10.0, 10.0]
    let a = make_u64_protocol(&[1000, 2000, 3000]);
    let b = make_u64_protocol(&[100, 200, 300]);
    let result = divide(a.as_ref(), b.as_ref()).expect("divide u64");
    let out = extract_f64_result(result.as_ref());
    assert!((out[0] - 10.0).abs() < 1e-12);
    assert!((out[1] - 10.0).abs() < 1e-12);
    assert!((out[2] - 10.0).abs() < 1e-12);
}