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
use crate::{
    core::prelude::*,
    errors::prelude::*,
    numeric::prelude::*,
};

/// ArrayTrait - Array Rounding functions
pub trait ArrayRounding<N: Numeric> where Self: Sized + Clone {

    /// Evenly round to the given number of decimals
    ///
    /// # Arguments
    ///
    /// * `decimals` - Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let arr = Array::flat(vec![2.01, 4.6, 8.0010, 22.234]);
    /// assert_eq!(Array::flat(vec![2., 4.6, 8.001, 20.]), arr.round(&Array::flat(vec![0, 1, 3, -1]).unwrap()));
    /// ```
    fn round(&self, decimals: &Array<isize>) -> Result<Array<N>, ArrayError>;

    /// Evenly round to the given number of decimals. alias on `round`
    ///
    /// # Arguments
    ///
    /// * `decimals` - Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let arr = Array::flat(vec![2.01, 4.6, 8.0010, 22.234]);
    /// assert_eq!(Array::flat(vec![2., 4.6, 8.001, 20.]), arr.around(&Array::flat(vec![0, 1, 3, -1]).unwrap()));
    /// ```
    fn around(&self, decimals: &Array<isize>) -> Result<Array<N>, ArrayError>;

    /// Round elements of the array to the nearest integer
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let arr = Array::flat(vec![2.01, 4.6, 8.0010, 22.234]);
    /// assert_eq!(Array::flat(vec![2., 5., 8., 22.]), arr.rint());
    /// ```
    fn rint(&self) -> Result<Array<N>, ArrayError>;

    /// Round to nearest integer towards zero
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let arr = Array::flat(vec![2.01, 4.6, -1.6, -2.2]);
    /// assert_eq!(Array::flat(vec![2., 4., -1., -2.]), arr.fix());
    /// ```
    fn fix(&self) -> Result<Array<N>, ArrayError>;

    /// Round to nearest integer towards zero
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let arr = Array::flat(vec![2.01, 4.6, -1.6, -2.2]);
    /// assert_eq!(Array::flat(vec![2., 4., -1., -2.]), arr.fix());
    /// ```
    fn trunc(&self) -> Result<Array<N>, ArrayError>;

    /// Return the floor of the input, element-wise
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let arr = Array::flat(vec![2.01, 4.6, -1.6, -2.2]);
    /// assert_eq!(Array::flat(vec![2., 4., -2., -3.]), arr.floor());
    /// ```
    fn floor(&self) -> Result<Array<N>, ArrayError>;

    /// Return the ceil of the input, element-wise
    ///
    /// # Examples
    ///
    /// ```
    /// use arr_rs::prelude::*;
    ///
    /// let arr = Array::flat(vec![2.01, 4.6, -1.6, -2.2]);
    /// assert_eq!(Array::flat(vec![3., 5., -1., -2.]), arr.ceil());
    /// ```
    fn ceil(&self) -> Result<Array<N>, ArrayError>;
}

impl <N: Numeric> ArrayRounding<N> for Array<N> {

    fn round(&self, decimals: &Array<isize>) -> Result<Array<N>, ArrayError> {
        let (array, other) = self.broadcast_h2(decimals)?;
        let elements = array.clone().into_iter().zip(&other)
            .map(|tuple| {
                let multiplier = 10_f64.powi(*tuple.1 as i32);
                N::from((tuple.0.to_f64() * multiplier).round() / multiplier)
            })
            .collect();
        Array::new(elements, array.get_shape()?)
    }

    fn around(&self, decimals: &Array<isize>) -> Result<Array<N>, ArrayError> {
        self.round(decimals)
    }

    fn rint(&self) -> Result<Array<N>, ArrayError> {
        self.round(&Array::single(0).unwrap())
    }

    fn fix(&self) -> Result<Array<N>, ArrayError> {
        self.map(|i|
            if *i >= N::zero() { N::from(i.to_f64().floor()) }
            else { N::from(i.to_f64().ceil()) }
        )
    }

    fn trunc(&self) -> Result<Array<N>, ArrayError> {
        self.map(|i| N::from(i.to_f64().trunc()))
    }

    fn floor(&self) -> Result<Array<N>, ArrayError> {
        self.map(|i| N::from(i.to_f64().floor()))
    }

    fn ceil(&self) -> Result<Array<N>, ArrayError> {
        self.map(|i| N::from(i.to_f64().ceil()))
    }
}

impl <N: Numeric> ArrayRounding<N> for Result<Array<N>, ArrayError> {

    fn round(&self, decimals: &Array<isize>) -> Result<Array<N>, ArrayError> {
        self.clone()?.round(decimals)
    }

    fn around(&self, decimals: &Array<isize>) -> Result<Array<N>, ArrayError> {
        self.clone()?.around(decimals)
    }

    fn rint(&self) -> Result<Array<N>, ArrayError> {
        self.clone()?.rint()
    }

    fn fix(&self) -> Result<Array<N>, ArrayError> {
        self.clone()?.fix()
    }

    fn trunc(&self) -> Result<Array<N>, ArrayError> {
        self.clone()?.trunc()
    }

    fn floor(&self) -> Result<Array<N>, ArrayError> {
        self.clone()?.floor()
    }

    fn ceil(&self) -> Result<Array<N>, ArrayError> {
        self.clone()?.ceil()
    }
}