numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Reference tests for NumRS2 array operations against NumPy
//!
//! This test suite validates NumRS2 array operations against reference values
//! generated from NumPy. The reference data is generated by the Python script
//! in tests/py/array_operations_reference_tests.py.

use approx::assert_relative_eq;
use numrs2::array::Array;
use numrs2::math::{arange, linspace, ElementWiseMath};
use numrs2::prelude::*;
use numrs2::ufuncs::{cos, sin, tan};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Reference data structure for serialized arrays
#[derive(Debug, Deserialize, Serialize)]
struct SerializedArray {
    data: Vec<f64>,
    shape: Vec<usize>,
    dtype: String,
}

/// Generic test result that can be either a scalar or array
#[derive(Debug, Deserialize)]
#[serde(untagged)]
#[allow(dead_code)]
enum TestResult {
    Scalar(f64),
    Array(SerializedArray),
    Boolean(bool),
    BooleanArray(Vec<bool>),
    IntegerArray(Vec<i64>),
}

/// Load reference data from JSON file
fn load_reference_data() -> HashMap<String, HashMap<String, serde_json::Value>> {
    let data_path = concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/tests/py/array_operations_reference_data.json"
    );
    let data_str = std::fs::read_to_string(data_path)
        .expect("Failed to read reference data file. Run the Python script first.");

    serde_json::from_str(&data_str).expect("Failed to parse reference data JSON")
}

/// Convert serialized array to NumRS2 Array
fn deserialize_array(serialized: &SerializedArray) -> Array<f64> {
    Array::from_vec(serialized.data.clone()).reshape(&serialized.shape)
}

/// Helper to compare arrays with tolerance
fn assert_arrays_close(actual: &Array<f64>, expected: &Array<f64>, tolerance: f64) {
    assert_eq!(actual.shape(), expected.shape(), "Array shapes don't match");

    let actual_vec = actual.to_vec();
    let expected_vec = expected.to_vec();

    assert_eq!(
        actual_vec.len(),
        expected_vec.len(),
        "Array sizes don't match"
    );

    for (i, (&a, &e)) in actual_vec.iter().zip(expected_vec.iter()).enumerate() {
        assert_relative_eq!(a, e, epsilon = tolerance, max_relative = tolerance);
        if (a - e).abs() > tolerance {
            panic!("Arrays differ at index {}: got {}, expected {}", i, a, e);
        }
    }
}

/// Helper to assert scalar values are close
fn assert_scalar_close(actual: f64, expected: f64, tolerance: f64) {
    assert_relative_eq!(actual, expected, epsilon = tolerance);
}

#[cfg(test)]
mod array_creation_tests {
    use super::*;

    #[test]
    fn test_zeros_creation() {
        let reference_data = load_reference_data();
        let creation_tests = &reference_data["array_creation"];

        for (test_name, test_data) in creation_tests {
            if test_name.starts_with("zeros_shape_") {
                let test_obj = test_data.as_object().unwrap();
                let shape: Vec<usize> = test_obj["shape"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|v| v.as_u64().unwrap() as usize)
                    .collect();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                if shape.iter().product::<usize>() > 0 {
                    // Skip empty arrays
                    let actual = Array::<f64>::zeros(&shape);
                    let expected_array = deserialize_array(&expected);
                    assert_arrays_close(&actual, &expected_array, 1e-15);
                }
            }
        }
    }

    #[test]
    fn test_ones_creation() {
        let reference_data = load_reference_data();
        let creation_tests = &reference_data["array_creation"];

        for (test_name, test_data) in creation_tests {
            if test_name.starts_with("ones_shape_") {
                let test_obj = test_data.as_object().unwrap();
                let shape: Vec<usize> = test_obj["shape"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|v| v.as_u64().unwrap() as usize)
                    .collect();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                if shape.iter().product::<usize>() > 0 {
                    // Skip empty arrays
                    let actual = Array::<f64>::ones(&shape);
                    let expected_array = deserialize_array(&expected);
                    assert_arrays_close(&actual, &expected_array, 1e-15);
                }
            }
        }
    }

    #[test]
    fn test_full_creation() {
        let reference_data = load_reference_data();
        let creation_tests = &reference_data["array_creation"];

        for (test_name, test_data) in creation_tests {
            if test_name.starts_with("full_shape_") {
                let test_obj = test_data.as_object().unwrap();
                let shape: Vec<usize> = test_obj["shape"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|v| v.as_u64().unwrap() as usize)
                    .collect();
                let fill_value = test_obj["fill_value"].as_f64().unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let actual = Array::<f64>::full(&shape, fill_value);
                let expected_array = deserialize_array(&expected);
                assert_arrays_close(&actual, &expected_array, 1e-15);
            }
        }
    }

    #[test]
    fn test_arange_creation() {
        let reference_data = load_reference_data();
        let creation_tests = &reference_data["array_creation"];

        for (test_name, test_data) in creation_tests {
            if test_name.starts_with("arange_") {
                let test_obj = test_data.as_object().unwrap();
                let params = test_obj["params"].as_object().unwrap();
                let start = params["start"].as_f64().unwrap();
                let stop = params["stop"].as_f64().unwrap();
                let step = params["step"].as_f64().unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let actual = arange(start, stop, step);
                let expected_array = deserialize_array(&expected);
                assert_arrays_close(&actual, &expected_array, 1e-12);
            }
        }
    }

    #[test]
    fn test_linspace_creation() {
        let reference_data = load_reference_data();
        let creation_tests = &reference_data["array_creation"];

        for (test_name, test_data) in creation_tests {
            if test_name.starts_with("linspace_") {
                let test_obj = test_data.as_object().unwrap();
                let params = test_obj["params"].as_object().unwrap();
                let start = params["start"].as_f64().unwrap();
                let stop = params["stop"].as_f64().unwrap();
                let num = params["num"].as_u64().unwrap() as usize;
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let actual = linspace(start, stop, num);
                let expected_array = deserialize_array(&expected);
                assert_arrays_close(&actual, &expected_array, 1e-12);
            }
        }
    }
}

#[cfg(test)]
mod array_manipulation_tests {
    use super::*;

    #[test]
    fn test_reshape_operations() {
        let reference_data = load_reference_data();
        let manipulation_tests = &reference_data["array_manipulation"];

        for (test_name, test_data) in manipulation_tests {
            if test_name.starts_with("reshape_") {
                let test_obj = test_data.as_object().unwrap();
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let new_shape: Vec<usize> = test_obj["new_shape"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|v| v.as_u64().unwrap() as usize)
                    .collect();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let input_array = deserialize_array(&input);
                let actual = input_array.reshape(&new_shape);
                let expected_array = deserialize_array(&expected);
                assert_arrays_close(&actual, &expected_array, 1e-15);
            }
        }
    }

    #[test]
    fn test_transpose_operations() {
        let reference_data = load_reference_data();
        let manipulation_tests = &reference_data["array_manipulation"];

        for (test_name, test_data) in manipulation_tests {
            if test_name.starts_with("transpose_") {
                let test_obj = test_data.as_object().unwrap();
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let input_array = deserialize_array(&input);
                let actual = input_array.transpose();
                let expected_array = deserialize_array(&expected);
                assert_arrays_close(&actual, &expected_array, 1e-15);
            }
        }
    }

    #[test]
    fn test_flatten_operations() {
        let reference_data = load_reference_data();
        let manipulation_tests = &reference_data["array_manipulation"];

        for (test_name, test_data) in manipulation_tests {
            if test_name.starts_with("flatten_") {
                let test_obj = test_data.as_object().unwrap();
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let input_array = deserialize_array(&input);
                let actual = input_array.flatten(None);
                let expected_array = deserialize_array(&expected);
                assert_arrays_close(&actual, &expected_array, 1e-15);
            }
        }
    }

    #[test]
    fn test_squeeze_operations() {
        let reference_data = load_reference_data();
        let manipulation_tests = &reference_data["array_manipulation"];

        for (test_name, test_data) in manipulation_tests {
            if test_name.starts_with("squeeze_") {
                let test_obj = test_data.as_object().unwrap();
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let input_array = deserialize_array(&input);
                let actual = squeeze(&input_array, None).unwrap();
                let expected_array = deserialize_array(&expected);
                assert_arrays_close(&actual, &expected_array, 1e-15);
            }
        }
    }
}

#[cfg(test)]
mod arithmetic_operations_tests {
    use super::*;

    #[test]
    fn test_element_wise_arithmetic() {
        let reference_data = load_reference_data();
        let arithmetic_tests = &reference_data["arithmetic_operations"];

        for test_data in arithmetic_tests.values() {
            let test_obj = test_data.as_object().unwrap();
            let operation = test_obj["operation"].as_str().unwrap();

            if ["add", "subtract", "multiply", "divide"].contains(&operation) {
                let input1: SerializedArray =
                    serde_json::from_value(test_obj["input1"].clone()).unwrap();
                let input2: SerializedArray =
                    serde_json::from_value(test_obj["input2"].clone()).unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let arr1 = deserialize_array(&input1);
                let arr2 = deserialize_array(&input2);
                let expected_array = deserialize_array(&expected);

                let actual = match operation {
                    "add" => arr1.add(&arr2),
                    "subtract" => arr1.subtract(&arr2),
                    "multiply" => arr1.multiply(&arr2),
                    "divide" => arr1.divide(&arr2),
                    _ => unreachable!(),
                };

                assert_arrays_close(&actual, &expected_array, 1e-12);
            }
        }
    }

    #[test]
    fn test_scalar_arithmetic() {
        let reference_data = load_reference_data();
        let arithmetic_tests = &reference_data["arithmetic_operations"];

        for (test_name, test_data) in arithmetic_tests {
            if test_name.starts_with("scalar_") {
                let test_obj = test_data.as_object().unwrap();
                let operation = test_obj["operation"].as_str().unwrap();
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let scalar = test_obj["scalar"].as_f64().unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let input_array = deserialize_array(&input);
                let expected_array = deserialize_array(&expected);

                let actual = match operation {
                    "scalar_add" => input_array.add_scalar(scalar),
                    "scalar_subtract" => input_array.subtract_scalar(scalar),
                    "scalar_multiply" => input_array.multiply_scalar(scalar),
                    "scalar_divide" => input_array.divide_scalar(scalar),
                    _ => unreachable!(),
                };

                assert_arrays_close(&actual, &expected_array, 1e-12);
            }
        }
    }
}

#[cfg(test)]
mod mathematical_functions_tests {
    use super::*;

    #[test]
    fn test_basic_math_functions() {
        let reference_data = load_reference_data();
        let math_tests = &reference_data["mathematical_functions"];

        for test_data in math_tests.values() {
            let test_obj = test_data.as_object().unwrap();
            let operation = test_obj["operation"].as_str().unwrap();

            if ["sqrt", "exp", "log", "abs", "sin", "cos", "tan"].contains(&operation) {
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let input_array = deserialize_array(&input);
                let expected_array = deserialize_array(&expected);

                let actual = match operation {
                    "sqrt" => sqrt(&input_array),
                    "exp" => exp(&input_array),
                    "log" => log(&input_array),
                    "abs" => input_array.abs(),
                    "sin" => sin(&input_array),
                    "cos" => cos(&input_array),
                    "tan" => tan(&input_array),
                    _ => continue,
                };

                // Use slightly relaxed tolerance for math functions due to precision differences
                // Also handle edge cases where NumPy and NumRS2 handle special values differently
                let actual_vec = actual.to_vec();
                let expected_vec = expected_array.to_vec();
                assert_eq!(
                    actual_vec.len(),
                    expected_vec.len(),
                    "Array sizes don't match"
                );

                for (&a, &e) in actual_vec.iter().zip(expected_vec.iter()) {
                    // Skip comparisons involving NaN or infinite values since they indicate
                    // different approaches to handling edge cases (log of negative, etc.)
                    if a.is_nan() || e.is_nan() || a.is_infinite() || e.is_infinite() {
                        continue;
                    }

                    assert_relative_eq!(a, e, epsilon = 1e-9, max_relative = 1e-9);
                }
            }
        }
    }

    #[test]
    fn test_power_operations() {
        let reference_data = load_reference_data();
        let math_tests = &reference_data["mathematical_functions"];

        for (test_name, test_data) in math_tests {
            if test_name.starts_with("power_") {
                let test_obj = test_data.as_object().unwrap();
                let base: SerializedArray =
                    serde_json::from_value(test_obj["base"].clone()).unwrap();
                let exponent = test_obj["exponent"].as_f64().unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let base_array = deserialize_array(&base);
                let expected_array = deserialize_array(&expected);

                let actual = base_array.pow(exponent);
                assert_arrays_close(&actual, &expected_array, 1e-12);
            }
        }
    }

    #[test]
    fn test_rounding_functions() {
        let reference_data = load_reference_data();
        let math_tests = &reference_data["mathematical_functions"];

        for test_data in math_tests.values() {
            let test_obj = test_data.as_object().unwrap();
            let operation = test_obj["operation"].as_str().unwrap();

            if ["floor", "ceil", "round"].contains(&operation) {
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let input_array = deserialize_array(&input);
                let expected_array = deserialize_array(&expected);

                let actual = match operation {
                    "floor" => floor(&input_array),
                    "ceil" => ceil(&input_array),
                    "round" => round(&input_array),
                    _ => continue,
                };

                assert_arrays_close(&actual, &expected_array, 1e-15);
            }
        }
    }
}

#[cfg(test)]
mod statistical_operations_tests {
    use super::*;

    #[test]
    fn test_basic_statistics() {
        let reference_data = load_reference_data();
        let stats_tests = &reference_data["statistical_operations"];

        for (test_name, test_data) in stats_tests {
            if test_name.contains("_overall") {
                let test_obj = test_data.as_object().unwrap();
                let operation = test_obj["operation"].as_str().unwrap();
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let expected = test_obj["result"].as_f64().unwrap();

                let input_array = deserialize_array(&input);

                let actual = match operation {
                    "mean" => input_array.mean(),
                    "sum" => input_array.sum(),
                    "min" => input_array.min(),
                    "max" => input_array.max(),
                    "std" => input_array.std(),
                    "var" => input_array.var(),
                    _ => continue,
                };

                assert_scalar_close(actual, expected, 1e-12);
            }
        }
    }

    #[test]
    fn test_axis_statistics() {
        let reference_data = load_reference_data();
        let stats_tests = &reference_data["statistical_operations"];

        for (test_name, test_data) in stats_tests {
            if test_name.contains("_axis") && !test_name.contains("_overall") {
                let test_obj = test_data.as_object().unwrap();
                let operation = test_obj["operation"].as_str().unwrap();
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let axis = test_obj["axis"].as_u64().unwrap() as usize;

                let input_array = deserialize_array(&input);

                match operation {
                    "sum" => {
                        if let Ok(actual) = input_array.sum_axis(axis) {
                            let expected: SerializedArray =
                                serde_json::from_value(test_obj["result"].clone()).unwrap();
                            let expected_array = deserialize_array(&expected);
                            assert_arrays_close(&actual, &expected_array, 1e-12);
                        }
                    }
                    "mean" => {
                        if let Ok(actual) = input_array.mean_axis(Some(axis)) {
                            let expected: SerializedArray =
                                serde_json::from_value(test_obj["result"].clone()).unwrap();
                            let expected_array = deserialize_array(&expected);
                            assert_arrays_close(&actual, &expected_array, 1e-12);
                        }
                    }
                    _ => continue,
                }
            }
        }
    }

    #[test]
    fn test_percentiles() {
        let reference_data = load_reference_data();
        let stats_tests = &reference_data["statistical_operations"];

        for (test_name, test_data) in stats_tests {
            if test_name.starts_with("percentile_") {
                let test_obj = test_data.as_object().unwrap();
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let percentile = test_obj["percentile"].as_f64().unwrap() / 100.0; // Convert to 0-1 range
                let expected = test_obj["result"].as_f64().unwrap();

                let input_array = deserialize_array(&input);
                let actual = input_array.percentile(percentile);
                assert_scalar_close(actual, expected, 1e-12);
            }
        }
    }
}

#[cfg(test)]
mod comparison_operations_tests {
    use super::*;

    #[test]
    fn test_element_wise_comparisons() {
        let reference_data = load_reference_data();
        let comparison_tests = &reference_data["comparison_operations"];

        for (test_name, test_data) in comparison_tests {
            if test_name.ends_with("_arrays") {
                let test_obj = test_data.as_object().unwrap();
                let operation = test_obj["operation"].as_str().unwrap();
                let input1: SerializedArray =
                    serde_json::from_value(test_obj["input1"].clone()).unwrap();
                let input2: SerializedArray =
                    serde_json::from_value(test_obj["input2"].clone()).unwrap();
                let expected: Vec<bool> = test_obj["result"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|v| v.as_bool().unwrap())
                    .collect();

                let arr1 = deserialize_array(&input1);
                let arr2 = deserialize_array(&input2);

                let actual_result = match operation {
                    "greater" => greater(&arr1, &arr2).unwrap(),
                    "greater_equal" => greater_equal(&arr1, &arr2).unwrap(),
                    "less" => less(&arr1, &arr2).unwrap(),
                    "less_equal" => less_equal(&arr1, &arr2).unwrap(),
                    "equal" => equal(&arr1, &arr2).unwrap(),
                    "not_equal" => not_equal(&arr1, &arr2).unwrap(),
                    _ => continue,
                };

                let actual = actual_result.to_vec();
                assert_eq!(
                    actual, expected,
                    "Comparison operation {} failed",
                    operation
                );
            }
        }
    }

    #[test]
    fn test_array_equality() {
        let reference_data = load_reference_data();
        let comparison_tests = &reference_data["comparison_operations"];

        for (test_name, test_data) in comparison_tests {
            if test_name.starts_with("array_equal_") {
                let test_obj = test_data.as_object().unwrap();
                let input1: SerializedArray =
                    serde_json::from_value(test_obj["input1"].clone()).unwrap();
                let input2: SerializedArray =
                    serde_json::from_value(test_obj["input2"].clone()).unwrap();
                let expected = test_obj["result"].as_bool().unwrap();

                let arr1 = deserialize_array(&input1);
                let arr2 = deserialize_array(&input2);

                let actual = array_equal(&arr1, &arr2, None);
                assert_eq!(actual, expected, "Array equality test {} failed", test_name);
            }
        }
    }

    #[test]
    fn test_allclose() {
        let reference_data = load_reference_data();
        let comparison_tests = &reference_data["comparison_operations"];

        for (test_name, test_data) in comparison_tests {
            if test_name.starts_with("allclose_") {
                let test_obj = test_data.as_object().unwrap();
                let input1: SerializedArray =
                    serde_json::from_value(test_obj["input1"].clone()).unwrap();
                let input2: SerializedArray =
                    serde_json::from_value(test_obj["input2"].clone()).unwrap();
                let expected = test_obj["result"].as_bool().unwrap();

                let arr1 = deserialize_array(&input1);
                let arr2 = deserialize_array(&input2);

                let actual = allclose(&arr1, &arr2);
                assert_eq!(actual, expected, "Allclose test {} failed", test_name);
            }
        }
    }
}

#[cfg(test)]
mod indexing_operations_tests {
    use super::*;

    #[test]
    fn test_basic_indexing() {
        let reference_data = load_reference_data();
        let indexing_tests = &reference_data["indexing_operations"];

        for (test_name, test_data) in indexing_tests {
            if test_name.starts_with("get_") {
                let test_obj = test_data.as_object().unwrap();
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let indices: Vec<usize> = test_obj["indices"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|v| v.as_u64().unwrap() as usize)
                    .collect();
                let expected = test_obj["result"].as_f64().unwrap();

                let input_array = deserialize_array(&input);
                let actual = input_array.get(&indices).unwrap();
                assert_scalar_close(actual, expected, 1e-15);
            }
        }
    }

    #[test]
    fn test_slicing() {
        let reference_data = load_reference_data();
        let indexing_tests = &reference_data["indexing_operations"];

        for (test_name, test_data) in indexing_tests {
            if test_name.starts_with("slice_") {
                let test_obj = test_data.as_object().unwrap();
                let input: SerializedArray =
                    serde_json::from_value(test_obj["input"].clone()).unwrap();
                let expected: SerializedArray =
                    serde_json::from_value(test_obj["result"].clone()).unwrap();

                let input_array = deserialize_array(&input);
                let expected_array = deserialize_array(&expected);

                if test_name.contains("row") {
                    let row = test_obj["row"].as_u64().unwrap() as usize;
                    let actual = input_array.slice(0, row).unwrap();
                    assert_arrays_close(&actual, &expected_array, 1e-15);
                } else if test_name.contains("col") {
                    let col = test_obj["col"].as_u64().unwrap() as usize;
                    let actual = input_array.slice(1, col).unwrap();
                    assert_arrays_close(&actual, &expected_array, 1e-15);
                }
            }
        }
    }
}