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
//! Comprehensive tests for SIMD-accelerated preprocessing operations
//!
//! Tests normalize_simd and standardize_simd functions with:
//! - Basic correctness (f32/f64)
//! - Edge cases (empty, single element, zero vector, constant)
//! - Large arrays (SIMD path)
//! - SIMD vs scalar equivalence
//! - Numerical properties (unit norm, zero mean, unit variance)

use scirs2_core::ndarray::{array, Array1};
use scirs2_core::ndarray_ext::preprocessing::{clip_simd, normalize_simd, standardize_simd};
#[cfg(feature = "random")]
use scirs2_core::random::{thread_rng, Distribution, Uniform};

// ============================================================================
// Normalize SIMD Tests
// ============================================================================

#[test]
fn test_normalize_simd_f64_basic() {
    let x = array![3.0, 4.0];
    let result = normalize_simd(&x.view());

    // 3-4-5 triangle: norm = 5, so [0.6, 0.8]
    assert!((result[0] - 0.6_f64).abs() < 1e-10);
    assert!((result[1] - 0.8_f64).abs() < 1e-10);

    // Verify unit norm
    let norm: f64 = result.iter().map(|x| x * x).sum::<f64>().sqrt();
    assert!((norm - 1.0).abs() < 1e-10);
}

#[test]
fn test_normalize_simd_f32_basic() {
    let x = array![3.0f32, 4.0];
    let result = normalize_simd(&x.view());

    assert!((result[0] - 0.6).abs() < 1e-6);
    assert!((result[1] - 0.8).abs() < 1e-6);

    // Verify unit norm
    let norm: f32 = result.iter().map(|x| x * x).sum::<f32>().sqrt();
    assert!((norm - 1.0).abs() < 1e-6);
}

#[test]
fn test_normalize_simd_f64_empty() {
    let x: Array1<f64> = array![];
    let result = normalize_simd(&x.view());
    assert_eq!(result.len(), 0);
}

#[test]
fn test_normalize_simd_f32_empty() {
    let x: Array1<f32> = array![];
    let result = normalize_simd(&x.view());
    assert_eq!(result.len(), 0);
}

#[test]
fn test_normalize_simd_f64_single() {
    let x = array![5.0];
    let result = normalize_simd(&x.view());

    // Single element: norm = 5, so result = 1.0
    assert!((result[0] - 1.0_f64).abs() < 1e-10);
}

#[test]
fn test_normalize_simd_f32_single() {
    let x = array![5.0f32];
    let result = normalize_simd(&x.view());

    assert!((result[0] - 1.0).abs() < 1e-6);
}

#[test]
fn test_normalize_simd_f64_zero_vector() {
    let x = array![0.0, 0.0, 0.0];
    let result = normalize_simd(&x.view());

    // Zero norm should return zero array
    assert_eq!(result[0], 0.0);
    assert_eq!(result[1], 0.0);
    assert_eq!(result[2], 0.0);
}

#[test]
fn test_normalize_simd_f32_zero_vector() {
    let x = array![0.0f32, 0.0, 0.0];
    let result = normalize_simd(&x.view());

    assert_eq!(result[0], 0.0);
    assert_eq!(result[1], 0.0);
    assert_eq!(result[2], 0.0);
}

#[test]
fn test_normalize_simd_f64_negative() {
    let x = array![-3.0, 4.0];
    let result = normalize_simd(&x.view());

    // norm = 5, so [-0.6, 0.8]
    assert!((result[0] - (-0.6_f64)).abs() < 1e-10);
    assert!((result[1] - 0.8_f64).abs() < 1e-10);

    // Verify unit norm
    let norm: f64 = result.iter().map(|x| x * x).sum::<f64>().sqrt();
    assert!((norm - 1.0).abs() < 1e-10);
}

#[test]
fn test_normalize_simd_f32_negative() {
    let x = array![-3.0f32, 4.0];
    let result = normalize_simd(&x.view());

    assert!((result[0] - (-0.6)).abs() < 1e-6);
    assert!((result[1] - 0.8).abs() < 1e-6);

    let norm: f32 = result.iter().map(|x| x * x).sum::<f32>().sqrt();
    assert!((norm - 1.0).abs() < 1e-6);
}

#[test]
fn test_normalize_simd_f64_large_array() {
    // Create large array for SIMD path
    let data: Vec<f64> = (0..10000).map(|i| (i as f64).sin()).collect();
    let x = Array1::from_vec(data);
    let result = normalize_simd(&x.view());

    // Verify unit norm
    let norm: f64 = result.iter().map(|x| x * x).sum::<f64>().sqrt();
    assert!(
        (norm - 1.0).abs() < 1e-8,
        "Large array norm should be ~1, got {}",
        norm
    );
}

#[test]
fn test_normalize_simd_f32_large_array() {
    let data: Vec<f32> = (0..10000).map(|i| (i as f32).sin()).collect();
    let x = Array1::from_vec(data);
    let result = normalize_simd(&x.view());

    let norm: f32 = result.iter().map(|x| x * x).sum::<f32>().sqrt();
    assert!(
        (norm - 1.0).abs() < 1e-5,
        "Large array norm should be ~1, got {}",
        norm
    );
}

#[cfg(feature = "random")]
#[test]
fn test_normalize_simd_equivalence_random() {
    let mut rng = thread_rng();
    let uniform = Uniform::new(-100.0, 100.0).expect("Test: operation failed");
    let data: Vec<f64> = (0..5000).map(|_| uniform.sample(&mut rng)).collect();
    let x = Array1::from_vec(data.clone());

    // SIMD result
    let simd_result = normalize_simd(&x.view());

    // Scalar result (manual computation)
    let norm: f64 = data.iter().map(|x| x * x).sum::<f64>().sqrt();
    let expected: Vec<f64> = data.iter().map(|x| x / norm).collect();

    // Compare SIMD vs scalar
    for i in 0..data.len() {
        let diff = (simd_result[i] - expected[i]).abs();
        assert!(
            diff < 1e-10,
            "Mismatch at index {}: SIMD={}, expected={}",
            i,
            simd_result[i],
            expected[i]
        );
    }

    // Verify unit norm
    let result_norm: f64 = simd_result.iter().map(|x| x * x).sum::<f64>().sqrt();
    assert!((result_norm - 1.0).abs() < 1e-10);
}

// ============================================================================
// Standardize SIMD Tests
// ============================================================================

#[test]
fn test_standardize_simd_f64_basic() {
    let x = array![2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0];
    let result = standardize_simd(&x.view());

    // Verify mean ≈ 0
    let mean: f64 = result.iter().sum::<f64>() / result.len() as f64;
    assert!(mean.abs() < 1e-10, "Mean should be ~0, got {}", mean);

    // Verify std ≈ 1 (sample std with ddof=1)
    let variance: f64 = result.iter().map(|&x| x * x).sum::<f64>() / (result.len() - 1) as f64;
    let std = variance.sqrt();
    assert!((std - 1.0).abs() < 1e-10, "Std should be ~1, got {}", std);
}

#[test]
fn test_standardize_simd_f32_basic() {
    let x = array![1.0f32, 2.0, 3.0, 4.0, 5.0];
    let result = standardize_simd(&x.view());

    // Verify mean ≈ 0
    let mean: f32 = result.iter().sum::<f32>() / result.len() as f32;
    assert!(mean.abs() < 1e-5, "Mean should be ~0, got {}", mean);

    // Verify std ≈ 1
    let variance: f32 = result.iter().map(|&x| x * x).sum::<f32>() / (result.len() - 1) as f32;
    let std = variance.sqrt();
    assert!((std - 1.0).abs() < 1e-5, "Std should be ~1, got {}", std);
}

#[test]
fn test_standardize_simd_f64_empty() {
    let x: Array1<f64> = array![];
    let result = standardize_simd(&x.view());
    assert_eq!(result.len(), 0);
}

#[test]
fn test_standardize_simd_f32_empty() {
    let x: Array1<f32> = array![];
    let result = standardize_simd(&x.view());
    assert_eq!(result.len(), 0);
}

#[test]
fn test_standardize_simd_f64_single_element() {
    let x = array![5.0];
    let result = standardize_simd(&x.view());

    // Single element has undefined std, should return zero
    assert_eq!(result[0], 0.0);
}

#[test]
fn test_standardize_simd_f32_single_element() {
    let x = array![5.0f32];
    let result = standardize_simd(&x.view());

    assert_eq!(result[0], 0.0);
}

#[test]
fn test_standardize_simd_f64_constant() {
    let x = array![5.0, 5.0, 5.0, 5.0];
    let result = standardize_simd(&x.view());

    // Constant array has zero std, should return zero array
    assert_eq!(result[0], 0.0);
    assert_eq!(result[1], 0.0);
    assert_eq!(result[2], 0.0);
    assert_eq!(result[3], 0.0);
}

#[test]
fn test_standardize_simd_f32_constant() {
    let x = array![5.0f32, 5.0, 5.0, 5.0];
    let result = standardize_simd(&x.view());

    assert_eq!(result[0], 0.0);
    assert_eq!(result[1], 0.0);
    assert_eq!(result[2], 0.0);
    assert_eq!(result[3], 0.0);
}

#[test]
fn test_standardize_simd_f64_negative() {
    let x = array![-10.0, -5.0, 0.0, 5.0, 10.0];
    let result = standardize_simd(&x.view());

    // Verify mean ≈ 0
    let mean: f64 = result.iter().sum::<f64>() / result.len() as f64;
    assert!(mean.abs() < 1e-10, "Mean should be ~0, got {}", mean);

    // Verify std ≈ 1
    let variance: f64 = result.iter().map(|&x| x * x).sum::<f64>() / (result.len() - 1) as f64;
    let std = variance.sqrt();
    assert!((std - 1.0).abs() < 1e-10, "Std should be ~1, got {}", std);
}

#[test]
fn test_standardize_simd_f32_negative() {
    let x = array![-10.0f32, -5.0, 0.0, 5.0, 10.0];
    let result = standardize_simd(&x.view());

    let mean: f32 = result.iter().sum::<f32>() / result.len() as f32;
    assert!(mean.abs() < 1e-5);

    let variance: f32 = result.iter().map(|&x| x * x).sum::<f32>() / (result.len() - 1) as f32;
    let std = variance.sqrt();
    assert!((std - 1.0).abs() < 1e-5);
}

#[test]
fn test_standardize_simd_f64_large_array() {
    // Create large array for SIMD path
    let data: Vec<f64> = (0..10000).map(|i| (i as f64).sin() * 100.0).collect();
    let x = Array1::from_vec(data);
    let result = standardize_simd(&x.view());

    // Verify mean ≈ 0
    let mean: f64 = result.iter().sum::<f64>() / result.len() as f64;
    assert!(
        mean.abs() < 1e-10,
        "Large array mean should be ~0, got {}",
        mean
    );

    // Verify std ≈ 1
    let variance: f64 = result.iter().map(|&x| x * x).sum::<f64>() / (result.len() - 1) as f64;
    let std = variance.sqrt();
    assert!(
        (std - 1.0).abs() < 1e-8,
        "Large array std should be ~1, got {}",
        std
    );
}

#[test]
fn test_standardize_simd_f32_large_array() {
    let data: Vec<f32> = (0..10000).map(|i| (i as f32).sin() * 100.0).collect();
    let x = Array1::from_vec(data);
    let result = standardize_simd(&x.view());

    let mean: f32 = result.iter().sum::<f32>() / result.len() as f32;
    assert!(mean.abs() < 1e-5);

    let variance: f32 = result.iter().map(|&x| x * x).sum::<f32>() / (result.len() - 1) as f32;
    let std = variance.sqrt();
    assert!((std - 1.0).abs() < 1e-5);
}

#[cfg(feature = "random")]
#[test]
fn test_standardize_simd_equivalence_random() {
    let mut rng = thread_rng();
    let uniform = Uniform::new(-100.0, 100.0).expect("Test: operation failed");
    let data: Vec<f64> = (0..5000).map(|_| uniform.sample(&mut rng)).collect();
    let x = Array1::from_vec(data.clone());

    // SIMD result
    let simd_result = standardize_simd(&x.view());

    // Scalar result (manual computation)
    let n = data.len() as f64;
    let mean: f64 = data.iter().sum::<f64>() / n;
    let variance: f64 = data.iter().map(|x| (x - mean) * (x - mean)).sum::<f64>() / (n - 1.0);
    let std = variance.sqrt();
    let expected: Vec<f64> = data.iter().map(|x| (x - mean) / std).collect();

    // Compare SIMD vs scalar
    for i in 0..data.len() {
        let diff = (simd_result[i] - expected[i]).abs();
        assert!(
            diff < 1e-10,
            "Mismatch at index {}: SIMD={}, expected={}",
            i,
            simd_result[i],
            expected[i]
        );
    }

    // Verify mean ≈ 0
    let result_mean: f64 = simd_result.iter().sum::<f64>() / simd_result.len() as f64;
    assert!(result_mean.abs() < 1e-10);

    // Verify std ≈ 1
    let result_variance: f64 =
        simd_result.iter().map(|&x| x * x).sum::<f64>() / (simd_result.len() - 1) as f64;
    let result_std = result_variance.sqrt();
    assert!((result_std - 1.0).abs() < 1e-10);
}

// ============================================================================
// Edge Case and Property Tests
// ============================================================================

#[test]
fn test_normalize_simd_all_same_sign() {
    let x = array![1.0, 2.0, 3.0, 4.0, 5.0];
    let result = normalize_simd(&x.view());

    // All positive, should still have unit norm
    let norm: f64 = result.iter().map(|x| x * x).sum::<f64>().sqrt();
    assert!((norm - 1.0).abs() < 1e-10);

    // All elements should be positive
    for &val in result.iter() {
        assert!(val > 0.0);
    }
}

#[test]
fn test_standardize_simd_symmetric_distribution() {
    let x = array![-2.0, -1.0, 0.0, 1.0, 2.0];
    let result = standardize_simd(&x.view());

    // Symmetric distribution should maintain symmetry
    let sum_first_last: f64 = result[0] + result[4];
    assert!(sum_first_last.abs() < 1e-10); // First and last should be opposites
    let sum_second_fourth: f64 = result[1] + result[3];
    assert!(sum_second_fourth.abs() < 1e-10); // Second and fourth should be opposites
    let middle: f64 = result[2];
    assert!(middle.abs() < 1e-10); // Middle should be ~0
}

#[test]
fn test_normalize_simd_f64_precision() {
    // Test with very small values to check numerical stability
    let x = array![1e-100, 2e-100, 3e-100];
    let result = normalize_simd(&x.view());

    // Should still have unit norm
    let norm: f64 = result.iter().map(|x| x * x).sum::<f64>().sqrt();
    assert!((norm - 1.0).abs() < 1e-10);
}

#[test]
fn test_standardize_simd_f64_precision() {
    // Test with very large values to check numerical stability
    let x = array![1e100, 2e100, 3e100, 4e100, 5e100];
    let result = standardize_simd(&x.view());

    // Should still have mean ≈ 0 and std ≈ 1
    let mean: f64 = result.iter().sum::<f64>() / result.len() as f64;
    assert!(mean.abs() < 1e-10);

    let variance: f64 = result.iter().map(|&x| x * x).sum::<f64>() / (result.len() - 1) as f64;
    let std = variance.sqrt();
    assert!((std - 1.0).abs() < 1e-10);
}
// ============================================================================
// Clip SIMD Tests
// ============================================================================

#[test]
fn test_clip_simd_f64_basic() {
    let x = array![-10.0, -5.0, 0.0, 5.0, 10.0];
    let result = clip_simd(&x.view(), -3.0, 7.0);

    assert_eq!(result[0], -3.0); // -10 clipped to -3
    assert_eq!(result[1], -3.0); // -5 clipped to -3
    assert_eq!(result[2], 0.0); // 0 unchanged
    assert_eq!(result[3], 5.0); // 5 unchanged
    assert_eq!(result[4], 7.0); // 10 clipped to 7
}

#[test]
fn test_clip_simd_f32_basic() {
    let x = array![-10.0f32, -5.0, 0.0, 5.0, 10.0];
    let result = clip_simd(&x.view(), -3.0, 7.0);

    assert_eq!(result[0], -3.0);
    assert_eq!(result[1], -3.0);
    assert_eq!(result[2], 0.0);
    assert_eq!(result[3], 5.0);
    assert_eq!(result[4], 7.0);
}

#[test]
fn test_clip_simd_f64_empty() {
    let x: Array1<f64> = array![];
    let result = clip_simd(&x.view(), -1.0, 1.0);
    assert_eq!(result.len(), 0);
}

#[test]
fn test_clip_simd_f32_empty() {
    let x: Array1<f32> = array![];
    let result = clip_simd(&x.view(), -1.0, 1.0);
    assert_eq!(result.len(), 0);
}

#[test]
fn test_clip_simd_f64_all_within_range() {
    let x = array![1.0, 2.0, 3.0, 4.0, 5.0];
    let result = clip_simd(&x.view(), 0.0, 10.0);

    // All values should be unchanged
    assert_eq!(result[0], 1.0);
    assert_eq!(result[1], 2.0);
    assert_eq!(result[2], 3.0);
    assert_eq!(result[3], 4.0);
    assert_eq!(result[4], 5.0);
}

#[test]
fn test_clip_simd_f32_all_within_range() {
    let x = array![1.0f32, 2.0, 3.0, 4.0, 5.0];
    let result = clip_simd(&x.view(), 0.0, 10.0);

    assert_eq!(result[0], 1.0);
    assert_eq!(result[1], 2.0);
    assert_eq!(result[2], 3.0);
    assert_eq!(result[3], 4.0);
    assert_eq!(result[4], 5.0);
}

#[test]
fn test_clip_simd_f64_all_below_min() {
    let x = array![-10.0, -8.0, -6.0, -4.0, -2.0];
    let result = clip_simd(&x.view(), 0.0, 10.0);

    // All values should be clipped to min
    for &val in result.iter() {
        assert_eq!(val, 0.0);
    }
}

#[test]
fn test_clip_simd_f32_all_below_min() {
    let x = array![-10.0f32, -8.0, -6.0, -4.0, -2.0];
    let result = clip_simd(&x.view(), 0.0, 10.0);

    for &val in result.iter() {
        assert_eq!(val, 0.0);
    }
}

#[test]
fn test_clip_simd_f64_all_above_max() {
    let x = array![12.0, 14.0, 16.0, 18.0, 20.0];
    let result = clip_simd(&x.view(), 0.0, 10.0);

    // All values should be clipped to max
    for &val in result.iter() {
        assert_eq!(val, 10.0);
    }
}

#[test]
fn test_clip_simd_f32_all_above_max() {
    let x = array![12.0f32, 14.0, 16.0, 18.0, 20.0];
    let result = clip_simd(&x.view(), 0.0, 10.0);

    for &val in result.iter() {
        assert_eq!(val, 10.0);
    }
}

#[test]
fn test_clip_simd_f64_equal_bounds() {
    let x = array![1.0, 2.0, 3.0, 4.0, 5.0];
    let result = clip_simd(&x.view(), 3.0, 3.0);

    // All values should be set to 3.0
    for &val in result.iter() {
        assert_eq!(val, 3.0);
    }
}

#[test]
fn test_clip_simd_f32_equal_bounds() {
    let x = array![1.0f32, 2.0, 3.0, 4.0, 5.0];
    let result = clip_simd(&x.view(), 3.0, 3.0);

    for &val in result.iter() {
        assert_eq!(val, 3.0);
    }
}

#[test]
fn test_clip_simd_f64_negative_range() {
    let x = array![-10.0, -5.0, 0.0, 5.0, 10.0];
    let result = clip_simd(&x.view(), -8.0, -2.0);

    assert_eq!(result[0], -8.0); // -10 clipped to -8
    assert_eq!(result[1], -5.0); // -5 unchanged
    assert_eq!(result[2], -2.0); // 0 clipped to -2
    assert_eq!(result[3], -2.0); // 5 clipped to -2
    assert_eq!(result[4], -2.0); // 10 clipped to -2
}

#[test]
fn test_clip_simd_f32_negative_range() {
    let x = array![-10.0f32, -5.0, 0.0, 5.0, 10.0];
    let result = clip_simd(&x.view(), -8.0, -2.0);

    assert_eq!(result[0], -8.0);
    assert_eq!(result[1], -5.0);
    assert_eq!(result[2], -2.0);
    assert_eq!(result[3], -2.0);
    assert_eq!(result[4], -2.0);
}

#[test]
fn test_clip_simd_f64_large_array() {
    // Test SIMD path with large array
    let data: Vec<f64> = (0..10000).map(|i| (i as f64).sin() * 100.0).collect();
    let x = Array1::from_vec(data.clone());
    let result = clip_simd(&x.view(), -50.0, 50.0);

    // Verify all values are within bounds
    for &val in result.iter() {
        assert!((-50.0..=50.0).contains(&val));
    }

    // Verify clipping actually occurred
    let min_original = data.iter().cloned().fold(f64::INFINITY, f64::min);
    let max_original = data.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
    assert!(min_original < -50.0 || max_original > 50.0);
}

#[test]
fn test_clip_simd_f32_large_array() {
    let data: Vec<f32> = (0..10000).map(|i| (i as f32).sin() * 100.0).collect();
    let x = Array1::from_vec(data);
    let result = clip_simd(&x.view(), -50.0, 50.0);

    for &val in result.iter() {
        assert!((-50.0..=50.0).contains(&val));
    }
}

#[cfg(feature = "random")]
#[test]
fn test_clip_simd_equivalence_random() {
    let mut rng = thread_rng();
    let uniform = Uniform::new(-100.0, 100.0).expect("Test: operation failed");
    let data: Vec<f64> = (0..5000).map(|_| uniform.sample(&mut rng)).collect();
    let x = Array1::from_vec(data.clone());

    let min_val = -30.0;
    let max_val = 40.0;

    // SIMD result
    let simd_result = clip_simd(&x.view(), min_val, max_val);

    // Scalar result (manual computation)
    let expected: Vec<f64> = data
        .iter()
        .map(|&x| {
            if x < min_val {
                min_val
            } else if x > max_val {
                max_val
            } else {
                x
            }
        })
        .collect();

    // Compare SIMD vs scalar
    for i in 0..data.len() {
        let diff = (simd_result[i] - expected[i]).abs();
        assert!(
            diff < 1e-10,
            "Mismatch at index {}: SIMD={}, expected={}",
            i,
            simd_result[i],
            expected[i]
        );
    }

    // Verify bounds
    for &val in simd_result.iter() {
        assert!(val >= min_val && val <= max_val);
    }
}

#[test]
fn test_clip_simd_gradient_clipping_use_case() {
    // Simulate gradient clipping scenario
    let gradients = array![10.5, -8.2, 0.3, 15.7, -12.1, 2.5, -0.9, 7.3];
    let clip_value = 5.0;
    let clipped = clip_simd(&gradients.view(), -clip_value, clip_value);

    // Verify all gradients are within [-5, 5]
    for &val in clipped.iter() {
        assert!(val >= -clip_value && val <= clip_value);
    }

    // Check specific values
    assert_eq!(clipped[0], 5.0); // 10.5 -> 5.0
    assert_eq!(clipped[1], -5.0); // -8.2 -> -5.0
    assert_eq!(clipped[2], 0.3); // unchanged
    assert_eq!(clipped[3], 5.0); // 15.7 -> 5.0
    assert_eq!(clipped[4], -5.0); // -12.1 -> -5.0
    assert_eq!(clipped[5], 2.5); // unchanged
    assert_eq!(clipped[6], -0.9); // unchanged
    assert_eq!(clipped[7], 5.0); // 7.3 -> 5.0
}

#[test]
#[should_panic(expected = "min_val must be <= max_val")]
fn test_clip_simd_invalid_bounds() {
    let x = array![1.0, 2.0, 3.0];
    let _ = clip_simd(&x.view(), 10.0, 5.0); // min > max, should panic
}

#[test]
fn test_clip_simd_zero_centered() {
    let x = array![-5.0, -3.0, -1.0, 0.0, 1.0, 3.0, 5.0];
    let result = clip_simd(&x.view(), -2.0, 2.0);

    assert_eq!(result[0], -2.0);
    assert_eq!(result[1], -2.0);
    assert_eq!(result[2], -1.0);
    assert_eq!(result[3], 0.0);
    assert_eq!(result[4], 1.0);
    assert_eq!(result[5], 2.0);
    assert_eq!(result[6], 2.0);
}