oxirs-core 0.2.4

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
//! SIMD operations abstraction for OxiRS
//!
//! This module provides unified SIMD operations across the OxiRS ecosystem.
//! All SIMD operations must go through this module - direct SIMD usage in other modules is forbidden.

/// Unified SIMD operations trait
pub trait SimdOps {
    /// Add two slices element-wise
    fn add(a: &[Self], b: &[Self]) -> Vec<Self>
    where
        Self: Sized;

    /// Subtract two slices element-wise
    fn sub(a: &[Self], b: &[Self]) -> Vec<Self>
    where
        Self: Sized;

    /// Multiply two slices element-wise
    fn mul(a: &[Self], b: &[Self]) -> Vec<Self>
    where
        Self: Sized;

    /// Compute dot product
    fn dot(a: &[Self], b: &[Self]) -> Self
    where
        Self: Sized;

    /// Compute cosine distance (1 - cosine_similarity)
    fn cosine_distance(a: &[Self], b: &[Self]) -> Self
    where
        Self: Sized;

    /// Compute Euclidean distance
    fn euclidean_distance(a: &[Self], b: &[Self]) -> Self
    where
        Self: Sized;

    /// Compute Manhattan distance
    fn manhattan_distance(a: &[Self], b: &[Self]) -> Self
    where
        Self: Sized;

    /// Compute L2 norm
    fn norm(a: &[Self]) -> Self
    where
        Self: Sized;

    /// Sum all elements
    fn sum(a: &[Self]) -> Self
    where
        Self: Sized;

    /// Compute mean
    fn mean(a: &[Self]) -> Self
    where
        Self: Sized;
}

// Platform-specific SIMD implementations
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
mod x86_simd;

// ARM NEON SIMD support for Apple Silicon and ARM processors
#[cfg(all(target_arch = "aarch64", feature = "simd"))]
mod arm_simd;

// Generic scalar fallback implementation
mod scalar;

// Export the appropriate implementation based on features and platform
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
pub use x86_simd::*;

#[cfg(all(target_arch = "aarch64", feature = "simd"))]
pub use arm_simd::*;

#[cfg(not(feature = "simd"))]
pub use scalar::*;

// Fallback to scalar on unsupported platforms
#[cfg(all(
    feature = "simd",
    not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"))
))]
pub use scalar::*;

/// SIMD implementation for f32
impl SimdOps for f32 {
    fn add(a: &[Self], b: &[Self]) -> Vec<Self> {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::add_f32(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::add_f32(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::add_f32(a, b)
    }

    fn sub(a: &[Self], b: &[Self]) -> Vec<Self> {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::sub_f32(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::sub_f32(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::sub_f32(a, b)
    }

    fn mul(a: &[Self], b: &[Self]) -> Vec<Self> {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::mul_f32(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::mul_f32(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::mul_f32(a, b)
    }

    fn dot(a: &[Self], b: &[Self]) -> Self {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::dot_f32(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::dot_f32(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::dot_f32(a, b)
    }

    fn cosine_distance(a: &[Self], b: &[Self]) -> Self {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::cosine_distance_f32(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::cosine_distance_f32(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::cosine_distance_f32(a, b)
    }

    fn euclidean_distance(a: &[Self], b: &[Self]) -> Self {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::euclidean_distance_f32(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::euclidean_distance_f32(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::euclidean_distance_f32(a, b)
    }

    fn manhattan_distance(a: &[Self], b: &[Self]) -> Self {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::manhattan_distance_f32(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::manhattan_distance_f32(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::manhattan_distance_f32(a, b)
    }

    fn norm(a: &[Self]) -> Self {
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::norm_f32(a)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::norm_f32(a)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::norm_f32(a)
    }

    fn sum(a: &[Self]) -> Self {
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::sum_f32(a)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::sum_f32(a)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::sum_f32(a)
    }

    fn mean(a: &[Self]) -> Self {
        if a.is_empty() {
            return 0.0;
        }
        Self::sum(a) / a.len() as f32
    }
}

/// SIMD implementation for f64
impl SimdOps for f64 {
    fn add(a: &[Self], b: &[Self]) -> Vec<Self> {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::add_f64(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::add_f64(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::add_f64(a, b)
    }

    fn sub(a: &[Self], b: &[Self]) -> Vec<Self> {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::sub_f64(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::sub_f64(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::sub_f64(a, b)
    }

    fn mul(a: &[Self], b: &[Self]) -> Vec<Self> {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::mul_f64(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::mul_f64(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::mul_f64(a, b)
    }

    fn dot(a: &[Self], b: &[Self]) -> Self {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::dot_f64(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::dot_f64(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::dot_f64(a, b)
    }

    fn cosine_distance(a: &[Self], b: &[Self]) -> Self {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::cosine_distance_f64(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::cosine_distance_f64(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::cosine_distance_f64(a, b)
    }

    fn euclidean_distance(a: &[Self], b: &[Self]) -> Self {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::euclidean_distance_f64(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::euclidean_distance_f64(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::euclidean_distance_f64(a, b)
    }

    fn manhattan_distance(a: &[Self], b: &[Self]) -> Self {
        debug_assert_eq!(a.len(), b.len());
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::manhattan_distance_f64(a, b)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::manhattan_distance_f64(a, b)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::manhattan_distance_f64(a, b)
    }

    fn norm(a: &[Self]) -> Self {
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::norm_f64(a)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::norm_f64(a)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::norm_f64(a)
    }

    fn sum(a: &[Self]) -> Self {
        #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"))]
        unsafe {
            x86_simd::sum_f64(a)
        }
        #[cfg(all(target_arch = "aarch64", feature = "simd"))]
        unsafe {
            arm_simd::sum_f64(a)
        }
        #[cfg(not(any(
            all(any(target_arch = "x86", target_arch = "x86_64"), feature = "simd"),
            all(target_arch = "aarch64", feature = "simd")
        )))]
        scalar::sum_f64(a)
    }

    fn mean(a: &[Self]) -> Self {
        if a.is_empty() {
            return 0.0;
        }
        Self::sum(a) / a.len() as f64
    }
}

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

    const EPSILON_F32: f32 = 1e-5;
    const EPSILON_F64: f64 = 1e-10;

    // --- f32 tests ---

    #[test]
    fn test_f32_dot_product_basic() {
        let a = [1.0_f32, 2.0, 3.0];
        let b = [4.0_f32, 5.0, 6.0];
        let result = f32::dot(&a, &b);
        // 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
        assert!(
            (result - 32.0_f32).abs() < EPSILON_F32,
            "Expected 32.0, got {result}"
        );
    }

    #[test]
    fn test_f32_dot_product_zeros() {
        let a = [0.0_f32; 8];
        let b = [1.0_f32; 8];
        let result = f32::dot(&a, &b);
        assert!(
            (result - 0.0_f32).abs() < EPSILON_F32,
            "Expected 0.0, got {result}"
        );
    }

    #[test]
    fn test_f32_cosine_distance_identical_vectors() {
        let a = [1.0_f32, 0.0, 0.0];
        let b = [1.0_f32, 0.0, 0.0];
        // cosine_distance = 1 - cosine_similarity = 1 - 1 = 0
        let result = f32::cosine_distance(&a, &b);
        assert!(
            result.abs() < EPSILON_F32,
            "Identical vectors should have cosine distance 0, got {result}"
        );
    }

    #[test]
    fn test_f32_cosine_distance_orthogonal_vectors() {
        let a = [1.0_f32, 0.0, 0.0];
        let b = [0.0_f32, 1.0, 0.0];
        // cosine_similarity = 0, so cosine_distance = 1
        let result = f32::cosine_distance(&a, &b);
        assert!(
            (result - 1.0_f32).abs() < EPSILON_F32,
            "Orthogonal vectors should have cosine distance 1, got {result}"
        );
    }

    #[test]
    fn test_f32_euclidean_distance() {
        let a = [0.0_f32, 0.0, 0.0];
        let b = [3.0_f32, 4.0, 0.0];
        // sqrt(9 + 16) = 5
        let result = f32::euclidean_distance(&a, &b);
        assert!(
            (result - 5.0_f32).abs() < EPSILON_F32,
            "Expected 5.0, got {result}"
        );
    }

    #[test]
    fn test_f32_manhattan_distance() {
        let a = [1.0_f32, 2.0, 3.0];
        let b = [4.0_f32, 6.0, 8.0];
        // |1-4| + |2-6| + |3-8| = 3 + 4 + 5 = 12
        let result = f32::manhattan_distance(&a, &b);
        assert!(
            (result - 12.0_f32).abs() < EPSILON_F32,
            "Expected 12.0, got {result}"
        );
    }

    #[test]
    fn test_f32_norm_unit_vector() {
        let a = [1.0_f32, 0.0, 0.0];
        let result = f32::norm(&a);
        assert!(
            (result - 1.0_f32).abs() < EPSILON_F32,
            "Unit vector norm should be 1.0, got {result}"
        );
    }

    #[test]
    fn test_f32_norm_3_4_5() {
        let a = [3.0_f32, 4.0, 0.0];
        let result = f32::norm(&a);
        assert!(
            (result - 5.0_f32).abs() < EPSILON_F32,
            "Expected norm 5.0, got {result}"
        );
    }

    #[test]
    fn test_f32_sum_and_mean() {
        let a = [1.0_f32, 2.0, 3.0, 4.0];
        let sum = f32::sum(&a);
        let mean = f32::mean(&a);
        assert!(
            (sum - 10.0_f32).abs() < EPSILON_F32,
            "Expected sum 10.0, got {sum}"
        );
        assert!(
            (mean - 2.5_f32).abs() < EPSILON_F32,
            "Expected mean 2.5, got {mean}"
        );
    }

    #[test]
    fn test_f32_mean_empty_slice() {
        let a: [f32; 0] = [];
        let result = f32::mean(&a);
        assert!(
            (result - 0.0_f32).abs() < EPSILON_F32,
            "Mean of empty slice should be 0.0, got {result}"
        );
    }

    #[test]
    fn test_f32_add_element_wise() {
        let a = [1.0_f32, 2.0, 3.0];
        let b = [4.0_f32, 5.0, 6.0];
        let result = f32::add(&a, &b);
        assert_eq!(result.len(), 3);
        assert!((result[0] - 5.0_f32).abs() < EPSILON_F32);
        assert!((result[1] - 7.0_f32).abs() < EPSILON_F32);
        assert!((result[2] - 9.0_f32).abs() < EPSILON_F32);
    }

    #[test]
    fn test_f32_sub_element_wise() {
        let a = [5.0_f32, 7.0, 9.0];
        let b = [1.0_f32, 2.0, 3.0];
        let result = f32::sub(&a, &b);
        assert_eq!(result.len(), 3);
        assert!((result[0] - 4.0_f32).abs() < EPSILON_F32);
        assert!((result[1] - 5.0_f32).abs() < EPSILON_F32);
        assert!((result[2] - 6.0_f32).abs() < EPSILON_F32);
    }

    #[test]
    fn test_f32_mul_element_wise() {
        let a = [2.0_f32, 3.0, 4.0];
        let b = [5.0_f32, 6.0, 7.0];
        let result = f32::mul(&a, &b);
        assert_eq!(result.len(), 3);
        assert!((result[0] - 10.0_f32).abs() < EPSILON_F32);
        assert!((result[1] - 18.0_f32).abs() < EPSILON_F32);
        assert!((result[2] - 28.0_f32).abs() < EPSILON_F32);
    }

    // --- f64 tests ---

    #[test]
    fn test_f64_dot_product_basic() {
        let a = [1.0_f64, 2.0, 3.0];
        let b = [4.0_f64, 5.0, 6.0];
        let result = f64::dot(&a, &b);
        assert!(
            (result - 32.0_f64).abs() < EPSILON_F64,
            "Expected 32.0, got {result}"
        );
    }

    #[test]
    fn test_f64_euclidean_distance_zero() {
        let a = [1.0_f64, 2.0, 3.0];
        let b = [1.0_f64, 2.0, 3.0];
        let result = f64::euclidean_distance(&a, &b);
        assert!(
            result.abs() < EPSILON_F64,
            "Identical vectors should have distance 0, got {result}"
        );
    }

    #[test]
    fn test_f64_cosine_distance_opposite_vectors() {
        // Opposite vectors: a = [1,0,0], b = [-1,0,0]
        // cosine_similarity = -1, cosine_distance = 1 - (-1) = 2
        let a = [1.0_f64, 0.0, 0.0];
        let b = [-1.0_f64, 0.0, 0.0];
        let result = f64::cosine_distance(&a, &b);
        assert!(
            (result - 2.0_f64).abs() < EPSILON_F64,
            "Opposite vectors should have cosine distance 2.0, got {result}"
        );
    }

    #[test]
    fn test_f64_manhattan_distance_symmetry() {
        let a = [1.0_f64, 2.0, 3.0];
        let b = [4.0_f64, 6.0, 8.0];
        let d_ab = f64::manhattan_distance(&a, &b);
        let d_ba = f64::manhattan_distance(&b, &a);
        assert!(
            (d_ab - d_ba).abs() < EPSILON_F64,
            "Manhattan distance should be symmetric"
        );
    }

    #[test]
    fn test_f64_norm_of_standard_basis() {
        let a = [0.0_f64, 0.0, 1.0, 0.0];
        let result = f64::norm(&a);
        assert!(
            (result - 1.0_f64).abs() < EPSILON_F64,
            "Norm of standard basis vector should be 1.0, got {result}"
        );
    }

    #[test]
    fn test_f64_sum_large_slice() {
        // Verify sum of arithmetic series: 1+2+...+100 = 5050
        let a: Vec<f64> = (1..=100).map(|x| x as f64).collect();
        let result = f64::sum(&a);
        assert!(
            (result - 5050.0_f64).abs() < EPSILON_F64,
            "Expected 5050.0, got {result}"
        );
    }

    #[test]
    fn test_f64_mean_empty_slice() {
        let a: [f64; 0] = [];
        let result = f64::mean(&a);
        assert!(
            result.abs() < EPSILON_F64,
            "Mean of empty slice should be 0.0, got {result}"
        );
    }

    #[test]
    fn test_f64_add_sub_roundtrip() {
        let a = [3.0_f64, 1.0, 4.0, 1.0, 5.0];
        let b = [1.0_f64, 2.0, 3.0, 4.0, 5.0];
        let added = f64::add(&a, &b);
        let subtracted = f64::sub(added.as_slice(), &b);
        for (orig, recovered) in a.iter().zip(subtracted.iter()) {
            assert!(
                (orig - recovered).abs() < EPSILON_F64,
                "Add-sub roundtrip failed: {orig} vs {recovered}"
            );
        }
    }

    // --- Cross-type consistency tests ---

    #[test]
    fn test_euclidean_and_manhattan_triangle_inequality() {
        // For any two vectors, Euclidean distance <= Manhattan distance
        let a = [1.0_f32, 2.0, 3.0];
        let b = [4.0_f32, 6.0, 8.0];
        let euclidean = f32::euclidean_distance(&a, &b);
        let manhattan = f32::manhattan_distance(&a, &b);
        assert!(
            euclidean <= manhattan + EPSILON_F32,
            "Euclidean should be <= Manhattan: {euclidean} vs {manhattan}"
        );
    }

    #[test]
    fn test_cosine_distance_range() {
        // cosine_distance should be in [0, 2] for real vectors
        let a = [1.0_f32, 0.5, 0.25];
        let b = [0.5_f32, 1.0, 2.0];
        let result = f32::cosine_distance(&a, &b);
        assert!(
            result >= 0.0,
            "Cosine distance should be non-negative, got {result}"
        );
        assert!(
            result <= 2.0 + EPSILON_F32,
            "Cosine distance should be <= 2.0, got {result}"
        );
    }
}