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
use crate::{List, VectorsGeneric};
use na::{
    storage::Storage, ClosedAdd, ClosedDiv, ClosedMul, ClosedSub, Dim, Dynamic, Matrix, Matrix1xX,
    MatrixSlice, RealField, Scalar, U3,
};
use num_traits::{cast, NumCast, Signed, Zero};
use std::cmp::PartialOrd;

/// Get the slice of the whole matrix.
pub fn slice<T, R, C, S>(
    matrix: &Matrix<T, R, C, S>,
) -> MatrixSlice<'_, T, Dynamic, Dynamic, S::RStride, S::CStride>
where
    T: Scalar,
    R: Dim,
    C: Dim,
    S: Storage<T, R, C>,
{
    matrix.slice((0, 0), matrix.shape())
}

/// Get the number of [`Vector`][crate::Vector]s.
pub fn number_vectors<T, S>(vectors: &VectorsGeneric<T, S>) -> usize
where
    T: Scalar,
    S: Storage<T, U3, Dynamic>,
{
    vectors.ncols()
}

/// Compute the required size to go from `start` to `end` with `step`, including the end point (last
/// time step can be smaller).
pub fn size_range_with_step<T>(start: T, end: T, step: T) -> usize
where
    T: Scalar + NumCast + Copy + ClosedAdd + ClosedSub + ClosedDiv + ClosedMul + PartialOrd,
{
    if Zero::is_zero(&cast::<T, f64>(step).unwrap())
        || Zero::is_zero(&cast::<T, f64>(end - start).unwrap())
        || (Signed::is_positive(&cast::<T, f64>(end - start).unwrap())
            && Signed::is_negative(&cast::<T, f64>(step).unwrap()))
        || (Signed::is_negative(&cast::<T, f64>(end - start).unwrap())
            && Signed::is_positive(&cast::<T, f64>(step).unwrap()))
    {
        return 1;
    }
    let mut size: usize = cast((end - start) / step).unwrap();
    if start + cast::<usize, T>(size).unwrap() * step < end {
        size += 1;
    }
    size + 1
}

/// Create a [`List`] from `start` to `end` with `step`. The last step can be smaller to include
/// `end`.
pub fn linspace<T>(start: T, end: T, step: T) -> List<T>
where
    T: Scalar + NumCast + ClosedSub + ClosedDiv + ClosedMul + ClosedAdd + PartialOrd + Copy,
{
    let size = size_range_with_step(start, end, step);
    let mut vector = Matrix1xX::from_fn(size, |_, j| start + step * cast(j).unwrap());
    if vector[size - 1] > end {
        vector[size - 1] = end;
    }
    vector
}

/// Clip all elements of a [`List`] between `min` and `max`. A `None` value indicates no limit.
pub fn clip<T>(list: &List<T>, min: Option<T>, max: Option<T>) -> List<T>
where
    T: RealField,
{
    let mut work_list = list.clone();
    for element in work_list.iter_mut() {
        if let Some(mini) = min {
            if *element < mini {
                *element = mini
            };
        }
        if let Some(max) = max {
            if *element > max {
                *element = max
            };
        }
    }
    work_list
}

/// Compute the element-wise power of a [`List`].
pub fn pows<T>(list: &List<T>, power: i32) -> List<T>
where
    T: RealField,
{
    List::from_iterator(list.len(), list.iter().map(|x| x.powi(power)))
}