vectra 0.2.4

A multi-dimensional array library for Rust, similar to NumPy
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
use crate::{NumExt, core::Array};
use std::fmt::Debug; // Sub currently unused
pub mod matmul;
pub mod ml;
pub mod stat;

use num_traits::{Float, Pow};

/// Matrix multiplication policy for controlling computation backend.
///
/// This enum allows you to choose different algorithms for matrix multiplication
/// based on performance requirements and available features.
///
/// # Variants
///
/// * `Naive` - Simple triple-loop implementation, good for small matrices
/// * `Faer` - Uses the faer library for optimized linear algebra operations
/// * `Blas` - Uses BLAS (Basic Linear Algebra Subprograms) when available
/// * `LoopReorder` - Optimized loop ordering for better cache performance
/// * `LoopRecorderSimd` - SIMD-optimized version (ARM64 only)
/// * `Blocking(usize)` - Block-based algorithm with specified block size
///
/// # Examples
///
/// ```rust
/// use vectra::math::MatmulPolicy;
/// use vectra::prelude::*;
///
/// let a = Array::ones([100, 50]);
/// let b = Array::ones([50, 80]);
///
/// // Use different policies for matrix multiplication
/// let result_faer = a.matmul_policy(&b, MatmulPolicy::Faer);
/// let result_naive = a.matmul_policy(&b, MatmulPolicy::Naive);
/// let result_blocking = a.matmul_policy(&b, MatmulPolicy::Blocking(32));
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MatmulPolicy {
    /// Simple triple-loop matrix multiplication.
    ///
    /// Best for small matrices or when simplicity is preferred over performance.
    Naive,

    /// Use the faer library for matrix multiplication.
    ///
    /// Provides good performance with optimized algorithms and is the default
    /// on most platforms except macOS.
    Faer,

    /// Use BLAS (Basic Linear Algebra Subprograms) for matrix multiplication.
    ///
    /// Typically provides the best performance when available. This is the
    /// default on macOS where Accelerate framework is used.
    #[cfg(feature = "blas")]
    Blas,

    /// Loop reordering optimization for better cache performance.
    ///
    /// Rearranges the computation order to improve memory access patterns.
    LoopReorder,

    /// SIMD-optimized loop reordering (ARM64 only).
    ///
    /// Combines loop reordering with SIMD instructions for maximum performance
    /// on ARM64 architectures.
    #[cfg(target_arch = "aarch64")]
    LoopRecorderSimd,

    /// Block-based matrix multiplication with specified block size.
    ///
    /// Divides the computation into blocks to improve cache locality.
    /// The parameter specifies the block size.
    Blocking(usize),
}

impl Default for MatmulPolicy {
    fn default() -> Self {
        #[cfg(target_os = "macos")]
        return Self::Blas;

        #[cfg(not(target_os = "macos"))]
        return Self::Faer;
    }
}

impl<const D: usize, T: NumExt> Array<D, T> {
    /// Raise each element to the power of the given exponent.
    ///
    /// This method applies the power operation element-wise to the array.
    /// The exponent can be of a different numeric type than the array elements.
    ///
    /// # Arguments
    ///
    /// * `exponent` - The exponent to raise each element to
    ///
    /// # Examples
    ///
    /// ```rust
    /// use vectra::prelude::*;
    ///
    /// // Integer powers
    /// let arr = Array::from_vec(vec![2, 3, 4], [3]);
    /// let squared = arr.pow(2); // [4, 9, 16]
    ///
    /// // Floating-point powers
    /// let arr_f = Array::from_vec(vec![4.0, 9.0, 16.0], [3]);
    /// let sqrt_result = arr_f.pow(0.5); // [2.0, 3.0, 4.0]
    ///
    /// // Mixed types
    /// let arr = Array::from_vec(vec![2.0, 3.0], [2]);
    /// let result = arr.pow(3); // [8.0, 27.0]
    /// ```
    pub fn pow<U, O>(&self, exponent: U) -> Array<D, O>
    where
        T: Pow<U, Output = O>,
        U: NumExt,
        O: NumExt,
    {
        self.map(|x| x.pow(exponent.clone()))
    }
}

macro_rules! unary_ops {
    ($($(#[$meta:meta])* fn $id:ident)+) => {
        $($(#[$meta])*
        #[must_use = "method returns a new array and does not mutate the original value"]
        pub fn $id(&self) -> Array<D, T> {
            self.mapv(T::$id)
        })+
    };
}

macro_rules! binary_ops {
    ($($(#[$meta:meta])* fn $id:ident($ty:ty))+) => {
        $($(#[$meta])*
        #[must_use = "method returns a new array and does not mutate the original value"]
        pub fn $id(&self, rhs: $ty) -> Array<D, T> {
            self.mapv(|v| T::$id(v, rhs))
        })+
    };
}

/// Mathematical functions for floating-point arrays.
///
/// This implementation provides element-wise mathematical operations
/// for arrays containing floating-point numbers.
impl<const D: usize, T: NumExt + Float> Array<D, T> {
    unary_ops! {
        /// The largest integer less than or equal to each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![1.7, 2.3, -1.2], [3]);
        /// let result = arr.floor(); // [1.0, 2.0, -2.0]
        /// ```
        fn floor

        /// The smallest integer greater than or equal to each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![1.2, 2.7, -1.8], [3]);
        /// let result = arr.ceil(); // [2.0, 3.0, -1.0]
        /// ```
        fn ceil

        /// The nearest integer to each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![1.4, 2.6, -1.5], [3]);
        /// let result = arr.round(); // [1.0, 3.0, -2.0]
        /// ```
        fn round

        /// The integer part of each element (truncate towards zero).
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![1.7, -2.3], [2]);
        /// let result = arr.trunc(); // [1.0, -2.0]
        /// ```
        fn trunc

        /// The fractional part of each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![1.7, -2.3], [2]);
        /// let result = arr.fract(); // [0.7, -0.3]
        /// ```
        fn fract

        /// Absolute value of each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![-1.5, 2.3, -4.1], [3]);
        /// let result = arr.abs(); // [1.5, 2.3, 4.1]
        /// ```
        fn abs

        /// Sign of each element.
        ///
        /// Returns:
        /// * `1.0` for positive numbers
        /// * `-1.0` for negative numbers
        /// * `NaN` for `NaN` values
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![-2.5, 0.0, 3.7], [3]);
        /// let result = arr.signum(); // [-1.0, 1.0, 1.0]
        /// ```
        fn signum

        /// Reciprocal (inverse) of each element: `1/x`.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![2.0, 4.0, 0.5], [3]);
        /// let result = arr.recip(); // [0.5, 0.25, 2.0]
        /// ```
        fn recip

        /// Square root of each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![4.0, 9.0, 16.0], [3]);
        /// let result = arr.sqrt(); // [2.0, 3.0, 4.0]
        /// ```
        fn sqrt

        /// Exponential function: `e^x` for each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![0.0, 1.0, 2.0], [3]);
        /// let result = arr.exp(); // [1.0, e, e²]
        /// ```
        fn exp

        /// Base-2 exponential: `2^x` for each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![1.0, 2.0, 3.0], [3]);
        /// let result = arr.exp2(); // [2.0, 4.0, 8.0]
        /// ```
        fn exp2

        /// Exponential minus one: `e^x - 1` for each element.
        ///
        /// More accurate than `exp(x) - 1` for small values of x.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![0.0, 0.1], [2]);
        /// let result = arr.exp_m1();
        /// ```
        fn exp_m1

        /// Natural logarithm of each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![1.0, std::f64::consts::E], [2]);
        /// let result = arr.ln(); // [0.0, 1.0]
        /// ```
        fn ln

        /// Base-2 logarithm of each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![2.0, 4.0, 8.0], [3]);
        /// let result = arr.log2(); // [1.0, 2.0, 3.0]
        /// ```
        fn log2

        /// Base-10 logarithm of each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![10.0, 100.0, 1000.0], [3]);
        /// let result = arr.log10(); // [1.0, 2.0, 3.0]
        /// ```
        fn log10

        /// Natural logarithm of (1 + x) for each element.
        ///
        /// More accurate than `ln(1 + x)` for small values of x.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![0.0, 0.1], [2]);
        /// let result = arr.ln_1p();
        /// ```
        fn ln_1p

        /// Cube root of each element.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use vectra::prelude::*;
        ///
        /// let arr = Array::from_vec(vec![8.0, 27.0, 64.0], [3]);
        /// let result = arr.cbrt(); // [2.0, 3.0, 4.0]
        /// ```
        fn cbrt
        /// Sine of each element (in radians).
        fn sin
        /// Cosine of each element (in radians).
        fn cos
        /// Tangent of each element (in radians).
        fn tan
        /// Arcsine of each element (return in radians).
        fn asin
        /// Arccosine of each element (return in radians).
        fn acos
        /// Arctangent of each element (return in radians).
        fn atan
        /// Hyperbolic sine of each element.
        fn sinh
        /// Hyperbolic cosine of each element.
        fn cosh
        /// Hyperbolic tangent of each element.
        fn tanh
        /// Inverse hyperbolic sine of each element.
        fn asinh
        /// Inverse hyperbolic cosine of each element.
        fn acosh
        /// Inverse hyperbolic tangent of each element.
        fn atanh
        /// Converts radians to degrees for each element.
        fn to_degrees
        /// Converts degrees to radians for each element.
        fn to_radians
    }
    binary_ops! {
        /// Integer power of each element.
        ///
        /// This function is generally faster than using float power.
        fn powi(i32)
        /// Float power of each element.
        fn powf(T)
        /// Logarithm of each element with respect to an arbitrary base.
        fn log(T)
        /// The positive difference between given number and each element.
        fn abs_sub(T)
        /// Length of the hypotenuse of a right-angle triangle of each element
        fn hypot(T)
    }

    pub fn pow2(&self) -> Array<D, T> {
        self.mapv(|v| v * v)
    }
}

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

    #[test]
    fn test_map() {
        let arr = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], [2, 2]);
        let squared = arr.map(|x| x * x);
        assert_eq!(squared[[0, 0]], 1.0);
        assert_eq!(squared[[1, 1]], 16.0);
    }

    #[test]
    fn test_map_type_conversion() {
        let arr = Array::from_vec(vec![1, 2, 3, 4], [2, 2]);
        let result: Array<_, f64> = arr.map(|&x| x as f64 * 1.5);
        assert_eq!(result[[0, 0]], 1.5);
        assert_eq!(result[[1, 1]], 6.0);
    }

    #[test]
    fn test_into_map() {
        let arr = Array::from_vec(vec![1, 2, 3, 4], [2, 2]);
        let result: Array<_, String> = arr.map_into(|x| format!("value_{}", x));
        assert_eq!(result[[0, 0]], "value_1");
        assert_eq!(result[[1, 1]], "value_4");
        assert_eq!(result.shape(), [2, 2]);
    }

    #[test]
    fn test_into_map_type_conversion() {
        let arr = Array::from_vec(vec![1, 2, 3, 4], [2, 2]);
        let result: Array<_, f64> = arr.map_into(|x| x as f64 * 2.5);
        assert_eq!(result[[0, 0]], 2.5);
        assert_eq!(result[[1, 1]], 10.0);
        assert_eq!(result.shape(), [2, 2]);
    }

    #[test]
    fn test_trigonometric_functions() {
        let arr = Array::from_vec(
            vec![
                0.0,
                std::f64::consts::PI / 6.0,
                std::f64::consts::PI / 4.0,
                std::f64::consts::PI / 3.0,
                std::f64::consts::PI / 2.0,
            ],
            [5],
        );

        let sin_result = arr.sin();
        assert!((sin_result[[0]] - 0.0).abs() < 1e-10);
        assert!((sin_result[[1]] - 0.5).abs() < 1e-10);
        assert!((sin_result[[4]] - 1.0).abs() < 1e-10);

        let cos_result = arr.cos();
        assert!((cos_result[[0]] - 1.0).abs() < 1e-10);
        assert!((cos_result[[4]] - 0.0).abs() < 1e-10);

        let _tan_result = arr.tan();

        // Test inverse functions
        let values = Array::from_vec(vec![0.0, 0.5, 1.0], [3]);
        let asin_result = values.asin();
        let acos_result = values.acos();
        let atan_result = values.atan();

        assert!((asin_result[[0]] - 0.0f64).abs() < 1e-10f64);
        assert!((asin_result[[2]] - std::f64::consts::PI / 2.0).abs() < 1e-10);
        assert!((acos_result[[0]] - std::f64::consts::PI / 2.0).abs() < 1e-10);
        assert!((acos_result[[2]] - 0.0).abs() < 1e-10);
        assert!((atan_result[[0]] - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_hyperbolic_functions() {
        let arr = Array::from_vec(vec![0.0, 1.0, -1.0], [3]);

        let sinh_result = arr.sinh();
        let cosh_result = arr.cosh();
        let tanh_result = arr.tanh();

        assert!((sinh_result[[0]] - 0.0f64).abs() < 1e-10f64);
        assert!((cosh_result[[0]] - 1.0f64).abs() < 1e-10f64);
        assert!((tanh_result[[0]] - 0.0f64).abs() < 1e-10f64);
    }

    #[test]
    fn test_logarithmic_functions() {
        let arr = Array::from_vec(vec![1.0, std::f64::consts::E, 10.0, 2.0], [4]);

        // Test natural logarithm
        let ln_result = arr.ln();
        assert!((ln_result[[0]] - 0.0).abs() < 1e-10); // ln(1) = 0
        assert!((ln_result[[1]] - 1.0).abs() < 1e-10); // ln(e) = 1

        // Test base-10 logarithm
        let log10_result = arr.log10();
        assert!((log10_result[[0]] - 0.0).abs() < 1e-10); // log10(1) = 0
        assert!((log10_result[[2]] - 1.0).abs() < 1e-10); // log10(10) = 1

        // Test base-2 logarithm
        let log2_result = arr.log2();
        assert!((log2_result[[0]] - 0.0).abs() < 1e-10); // log2(1) = 0
        assert!((log2_result[[3]] - 1.0).abs() < 1e-10); // log2(2) = 1

        // Test custom base logarithm
        let values = Array::from_vec(vec![1.0, 3.0, 9.0, 27.0], [4]);
        let log3_result = values.log(3.0);
        assert!((log3_result[[0]] - 0.0f64).abs() < 1e-10f64); // log3(1) = 0
        assert!((log3_result[[1]] - 1.0f64).abs() < 1e-10f64); // log3(3) = 1
        assert!((log3_result[[2]] - 2.0f64).abs() < 1e-10f64); // log3(9) = 2
        assert!((log3_result[[3]] - 3.0f64).abs() < 1e-10f64); // log3(27) = 3

        // Test that exp and ln are inverse operations
        let test_values = Array::from_vec(vec![1.0, 2.0, 3.0], [3]);
        let exp_ln = test_values.clone().ln().exp();
        for i in 0..3 {
            assert!(((exp_ln[[i]] - test_values[[i]]) as f64).abs() < 1e-10);
        }
    }

    #[test]
    fn test_exponential_functions() {
        let arr = Array::from_vec(vec![0.0, 1.0, 2.0], [3]);

        // Test exponential function
        let exp_result = arr.exp();
        assert!((exp_result[[0]] - 1.0f64).abs() < 1e-10f64); // exp(0) = 1
        assert!((exp_result[[1]] - std::f64::consts::E).abs() < 1e-10); // exp(1) = e

        // Test base-2 exponential
        let exp2_result = arr.exp2();
        assert!((exp2_result[[0]] - 1.0).abs() < 1e-10); // 2^0 = 1
        assert!((exp2_result[[1]] - 2.0).abs() < 1e-10); // 2^1 = 2
        assert!((exp2_result[[2]] - 4.0).abs() < 1e-10); // 2^2 = 4

        // Test exp_m1 and ln_1p (inverse operations)
        let small_values = Array::from_vec(vec![0.1, 0.01, 0.001], [3]);
        let exp_m1_result = small_values.clone().exp_m1();
        let ln_1p_result = exp_m1_result.ln_1p();

        for i in 0..3 {
            assert!(((ln_1p_result[[i]] - small_values[[i]]) as f64).abs() < 1e-10);
        }

        // Test that exp_m1(0) = 0 and ln_1p(0) = 0
        let zero_arr = Array::from_vec(vec![0.0], [1]);
        assert!((zero_arr.clone().exp_m1()[[0]] - 0.0f64).abs() < 1e-10f64);
        assert!((zero_arr.ln_1p()[[0]] - 0.0f64).abs() < 1e-10f64);
    }
}