trueno 0.17.4

High-performance SIMD compute library with GPU support for matrix operations
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
use super::super::super::*;
use proptest::prelude::*;

// Property test: Addition is commutative (a + b == b + a)
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_add_commutative(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100),
        b in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        // Use minimum length to ensure both vectors have same size
        let len = a.len().min(b.len());
        let a_vec: Vec<f32> = a.into_iter().take(len).collect();
        let b_vec: Vec<f32> = b.into_iter().take(len).collect();

        let va = Vector::from_slice(&a_vec);
        let vb = Vector::from_slice(&b_vec);

        let result1 = va.add(&vb).unwrap();
        let result2 = vb.add(&va).unwrap();

        prop_assert_eq!(result1.as_slice(), result2.as_slice());
    }
}

// Property test: Addition is associative ((a + b) + c == a + (b + c))
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_add_associative(
        a in prop::collection::vec(-100.0f32..100.0, 1..50),
        b in prop::collection::vec(-100.0f32..100.0, 1..50),
        c in prop::collection::vec(-100.0f32..100.0, 1..50)
    ) {
        let len = a.len().min(b.len()).min(c.len());
        let a_vec: Vec<f32> = a.into_iter().take(len).collect();
        let b_vec: Vec<f32> = b.into_iter().take(len).collect();
        let c_vec: Vec<f32> = c.into_iter().take(len).collect();

        let va = Vector::from_slice(&a_vec);
        let vb = Vector::from_slice(&b_vec);
        let vc = Vector::from_slice(&c_vec);

        let ab = va.add(&vb).unwrap();
        let abc = ab.add(&vc).unwrap();

        let bc = vb.add(&vc).unwrap();
        let a_bc = va.add(&bc).unwrap();

        // Use approximate equality for floating point (relaxed for associativity)
        for (x, y) in abc.as_slice().iter().zip(a_bc.as_slice()) {
            prop_assert!((x - y).abs() < 1e-4);
        }
    }
}

// Property test: Subtraction anti-commutativity (a - b == -(b - a))
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_sub_anti_commutative(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100),
        b in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let len = a.len().min(b.len());
        let a_vec: Vec<f32> = a.into_iter().take(len).collect();
        let b_vec: Vec<f32> = b.into_iter().take(len).collect();

        let va = Vector::from_slice(&a_vec);
        let vb = Vector::from_slice(&b_vec);

        let result1 = va.sub(&vb).unwrap();
        let result2 = vb.sub(&va).unwrap();

        // a - b should equal -(b - a)
        for (x, y) in result1.as_slice().iter().zip(result2.as_slice()) {
            prop_assert!((x + y).abs() < 1e-5);
        }
    }
}

// Property test: Subtraction identity (a - 0 == a)
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_sub_identity(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let zero = Vector::from_slice(&vec![0.0; a.len()]);

        let result = va.sub(&zero).unwrap();

        prop_assert_eq!(result.as_slice(), va.as_slice());
    }
}

// Property test: Subtraction inverse (a - a == 0)
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_sub_inverse(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);

        let result = va.sub(&va).unwrap();

        // All elements should be zero (or very close due to floating point)
        for &x in result.as_slice() {
            prop_assert!(x.abs() < 1e-5);
        }
    }
}

// Property test: Multiplication is commutative
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_mul_commutative(
        a in prop::collection::vec(-100.0f32..100.0, 1..100),
        b in prop::collection::vec(-100.0f32..100.0, 1..100)
    ) {
        let len = a.len().min(b.len());
        let a_vec: Vec<f32> = a.into_iter().take(len).collect();
        let b_vec: Vec<f32> = b.into_iter().take(len).collect();

        let va = Vector::from_slice(&a_vec);
        let vb = Vector::from_slice(&b_vec);

        let result1 = va.mul(&vb).unwrap();
        let result2 = vb.mul(&va).unwrap();

        prop_assert_eq!(result1.as_slice(), result2.as_slice());
    }
}

// Property test: Division identity (a / 1 == a)
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_div_identity(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let ones = Vector::from_slice(&vec![1.0; a.len()]);

        let result = va.div(&ones).unwrap();

        for (x, y) in result.as_slice().iter().zip(va.as_slice()) {
            prop_assert!((x - y).abs() < 1e-5);
        }
    }
}

// Property test: Division inverse (a / a == 1, for non-zero a)
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_div_inverse(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        // Filter out zeros to avoid division edge cases
        let a_nonzero: Vec<f32> = a.into_iter()
            .map(|x| if x.abs() < 1e-5 { 1.0 } else { x })
            .collect();

        let va = Vector::from_slice(&a_nonzero);
        let result = va.div(&va).unwrap();

        // All elements should be 1.0 (or very close)
        for &x in result.as_slice() {
            prop_assert!((x - 1.0).abs() < 1e-4);
        }
    }
}

// Property test: Division-multiplication inverse (a / b) * b ≈ a
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_div_mul_inverse(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100),
        b in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let len = a.len().min(b.len());
        let a_vec: Vec<f32> = a.into_iter().take(len).collect();

        // Filter out zeros from b to avoid division by zero edge cases
        let b_vec: Vec<f32> = b.into_iter().take(len)
            .map(|x| if x.abs() < 1e-3 { 1.0 } else { x })
            .collect();

        let va = Vector::from_slice(&a_vec);
        let vb = Vector::from_slice(&b_vec);

        let divided = va.div(&vb).unwrap();
        let restored = divided.mul(&vb).unwrap();

        // Restored should approximately equal original
        for (original, restored_val) in a_vec.iter().zip(restored.as_slice()) {
            prop_assert!((original - restored_val).abs() < 1e-2);
        }
    }
}

// Property test: Dot product is commutative
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_dot_commutative(
        a in prop::collection::vec(-100.0f32..100.0, 1..100),
        b in prop::collection::vec(-100.0f32..100.0, 1..100)
    ) {
        let len = a.len().min(b.len());
        let a_vec: Vec<f32> = a.into_iter().take(len).collect();
        let b_vec: Vec<f32> = b.into_iter().take(len).collect();

        let va = Vector::from_slice(&a_vec);
        let vb = Vector::from_slice(&b_vec);

        let result1 = va.dot(&vb).unwrap();
        let result2 = vb.dot(&va).unwrap();

        prop_assert!((result1 - result2).abs() < 1e-3);
    }
}

// Property test: Identity element for addition (a + 0 == a)
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_add_identity(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let zero = Vector::from_slice(&vec![0.0; a.len()]);

        let result = va.add(&zero).unwrap();

        prop_assert_eq!(result.as_slice(), va.as_slice());
    }
}

// Property test: Identity element for multiplication (a * 1 == a)
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_mul_identity(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let one = Vector::from_slice(&vec![1.0; a.len()]);

        let result = va.mul(&one).unwrap();

        for (x, y) in result.as_slice().iter().zip(va.as_slice()) {
            prop_assert!((x - y).abs() < 1e-5);
        }
    }
}

// Property test: Zero element for multiplication (a * 0 == 0)
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_mul_zero(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let zero = Vector::from_slice(&vec![0.0; a.len()]);

        let result = va.mul(&zero).unwrap();

        for x in result.as_slice() {
            prop_assert_eq!(*x, 0.0);
        }
    }
}

// Property test: Distributive property (a * (b + c) == a * b + a * c)
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_distributive(
        a in prop::collection::vec(-10.0f32..10.0, 1..50),
        b in prop::collection::vec(-10.0f32..10.0, 1..50),
        c in prop::collection::vec(-10.0f32..10.0, 1..50)
    ) {
        let len = a.len().min(b.len()).min(c.len());
        let a_vec: Vec<f32> = a.into_iter().take(len).collect();
        let b_vec: Vec<f32> = b.into_iter().take(len).collect();
        let c_vec: Vec<f32> = c.into_iter().take(len).collect();

        let va = Vector::from_slice(&a_vec);
        let vb = Vector::from_slice(&b_vec);
        let vc = Vector::from_slice(&c_vec);

        // a * (b + c)
        let bc = vb.add(&vc).unwrap();
        let left = va.mul(&bc).unwrap();

        // a * b + a * c
        let ab = va.mul(&vb).unwrap();
        let ac = va.mul(&vc).unwrap();
        let right = ab.add(&ac).unwrap();

        for (x, y) in left.as_slice().iter().zip(right.as_slice()) {
            prop_assert!((x - y).abs() < 1e-3);
        }
    }
}

// Property test: Sum is consistent
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_sum_matches_manual(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let result = va.sum().unwrap();
        let manual_sum: f32 = a.iter().sum();

        // Relaxed tolerance for SIMD vs scalar accumulation order differences
        prop_assert!((result - manual_sum).abs() < 1e-2);
    }

    #[test]
    fn test_sum_kahan_correctness(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let kahan_result = va.sum_kahan().unwrap();
        let manual_sum: f32 = a.iter().sum();

        // Kahan result should be close to manual sum
        // Note: Both use same algorithm (iter().sum() also uses compensated summation)
        // so they should match closely
        prop_assert!((kahan_result - manual_sum).abs() < 1e-2,
            "Kahan sum should match manual sum closely");

        // Verify Kahan produces a reasonable result
        let expected_magnitude = a.iter().map(|x| x.abs()).sum::<f32>();
        prop_assert!(kahan_result.abs() <= expected_magnitude + 1.0,
            "Kahan result magnitude should be reasonable");
    }
}

// Property test: Max is actually maximum
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn test_max_is_maximum(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let result = va.max().unwrap();

        // Verify result is >= all elements
        for &x in a.iter() {
            prop_assert!(result >= x);
        }

        // Verify result is actually in the vector
        prop_assert!(a.contains(&result));
    }

    #[test]
    fn test_min_is_minimum(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let result = va.min().unwrap();

        // Verify result is <= all elements
        for &x in a.iter() {
            prop_assert!(result <= x);
        }

        // Verify result is actually in the vector
        prop_assert!(a.contains(&result));
    }

    #[test]
    fn test_argmax_correctness(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let idx = va.argmax().unwrap();

        // Verify index is in bounds
        prop_assert!(idx < a.len());

        // Verify value at index is >= all other values
        let max_val = a[idx];
        for &x in a.iter() {
            prop_assert!(max_val >= x);
        }

        // Verify it's the first occurrence (no earlier index has this value)
        for &val in a.iter().take(idx) {
            prop_assert!(val < max_val || val != max_val);
        }
    }

    #[test]
    fn test_argmin_correctness(
        a in prop::collection::vec(-1000.0f32..1000.0, 1..100)
    ) {
        let va = Vector::from_slice(&a);
        let idx = va.argmin().unwrap();

        // Verify index is in bounds
        prop_assert!(idx < a.len());

        // Verify value at index is <= all other values
        let min_val = a[idx];
        for &x in a.iter() {
            prop_assert!(min_val <= x);
        }

        // Verify it's the first occurrence (no earlier index has this value)
        for &val in a.iter().take(idx) {
            prop_assert!(val > min_val || val != min_val);
        }
    }
}