Thanks to visit codestin.com
Credit goes to docs.rs

ndarray/numeric/
impl_float_maths.rs

1// Element-wise methods for ndarray
2
3#[cfg(feature = "std")]
4use num_traits::Float;
5
6use crate::imp_prelude::*;
7
8#[cfg(feature = "std")]
9macro_rules! boolean_ops {
10    ($(#[$meta1:meta])* fn $func:ident
11    $(#[$meta2:meta])* fn $all:ident
12    $(#[$meta3:meta])* fn $any:ident) => {
13        $(#[$meta1])*
14        #[must_use = "method returns a new array and does not mutate the original value"]
15        pub fn $func(&self) -> Array<bool, D> {
16            self.mapv(A::$func)
17        }
18        $(#[$meta2])*
19        #[must_use = "method returns a new boolean value and does not mutate the original value"]
20        pub fn $all(&self) -> bool {
21            $crate::Zip::from(self).all(|&elt| !elt.$func())
22        }
23        $(#[$meta3])*
24        #[must_use = "method returns a new boolean value and does not mutate the original value"]
25        pub fn $any(&self) -> bool {
26            !self.$all()
27        }
28    };
29}
30
31#[cfg(feature = "std")]
32macro_rules! unary_ops {
33    ($($(#[$meta:meta])* fn $id:ident)+) => {
34        $($(#[$meta])*
35        #[must_use = "method returns a new array and does not mutate the original value"]
36        pub fn $id(&self) -> Array<A, D> {
37            self.mapv(A::$id)
38        })+
39    };
40}
41
42#[cfg(feature = "std")]
43macro_rules! binary_ops {
44    ($($(#[$meta:meta])* fn $id:ident($ty:ty))+) => {
45        $($(#[$meta])*
46        #[must_use = "method returns a new array and does not mutate the original value"]
47        pub fn $id(&self, rhs: $ty) -> Array<A, D> {
48            self.mapv(|v| A::$id(v, rhs))
49        })+
50    };
51}
52
53/// # Element-wise methods for float arrays
54///
55/// Element-wise math functions for any array type that contains float number.
56#[cfg(feature = "std")]
57impl<A, S, D> ArrayBase<S, D>
58where
59    A: 'static + Float,
60    S: Data<Elem = A>,
61    D: Dimension,
62{
63    boolean_ops! {
64        /// If the number is `NaN` (not a number), then `true` is returned for each element.
65        fn is_nan
66        /// Return `true` if all elements are `NaN` (not a number).
67        fn is_all_nan
68        /// Return `true` if any element is `NaN` (not a number).
69        fn is_any_nan
70    }
71    boolean_ops! {
72        /// If the number is infinity, then `true` is returned for each element.
73        fn is_infinite
74        /// Return `true` if all elements are infinity.
75        fn is_all_infinite
76        /// Return `true` if any element is infinity.
77        fn is_any_infinite
78    }
79    unary_ops! {
80        /// The largest integer less than or equal to each element.
81        fn floor
82        /// The smallest integer less than or equal to each element.
83        fn ceil
84        /// The nearest integer of each element.
85        fn round
86        /// The integer part of each element.
87        fn trunc
88        /// The fractional part of each element.
89        fn fract
90        /// Absolute of each element.
91        fn abs
92        /// Sign number of each element.
93        ///
94        /// + `1.0` for all positive numbers.
95        /// + `-1.0` for all negative numbers.
96        /// + `NaN` for all `NaN` (not a number).
97        fn signum
98        /// The reciprocal (inverse) of each element, `1/x`.
99        fn recip
100        /// Square root of each element.
101        fn sqrt
102        /// `e^x` of each element (exponential function).
103        fn exp
104        /// `2^x` of each element.
105        fn exp2
106        /// Natural logarithm of each element.
107        fn ln
108        /// Base 2 logarithm of each element.
109        fn log2
110        /// Base 10 logarithm of each element.
111        fn log10
112        /// Cubic root of each element.
113        fn cbrt
114        /// Sine of each element (in radians).
115        fn sin
116        /// Cosine of each element (in radians).
117        fn cos
118        /// Tangent of each element (in radians).
119        fn tan
120        /// Converts radians to degrees for each element.
121        fn to_degrees
122        /// Converts degrees to radians for each element.
123        fn to_radians
124    }
125    binary_ops! {
126        /// Integer power of each element.
127        ///
128        /// This function is generally faster than using float power.
129        fn powi(i32)
130        /// Float power of each element.
131        fn powf(A)
132        /// Logarithm of each element with respect to an arbitrary base.
133        fn log(A)
134        /// The positive difference between given number and each element.
135        fn abs_sub(A)
136    }
137
138    /// Square (two powers) of each element.
139    #[must_use = "method returns a new array and does not mutate the original value"]
140    pub fn pow2(&self) -> Array<A, D>
141    {
142        self.mapv(|v: A| v * v)
143    }
144}
145
146impl<A, S, D> ArrayBase<S, D>
147where
148    A: 'static + PartialOrd + Clone,
149    S: Data<Elem = A>,
150    D: Dimension,
151{
152    /// Limit the values for each element, similar to NumPy's `clip` function.
153    ///
154    /// ```
155    /// use ndarray::array;
156    ///
157    /// let a = array![0., 1., 2., 3., 4., 5., 6., 7., 8., 9.];
158    /// assert_eq!(a.clamp(1., 8.), array![1., 1., 2., 3., 4., 5., 6., 7., 8., 8.]);
159    /// assert_eq!(a.clamp(3., 6.), array![3., 3., 3., 3., 4., 5., 6., 6., 6., 6.]);
160    /// ```
161    ///
162    /// # Panics
163    ///
164    /// Panics if `!(min <= max)`.
165    pub fn clamp(&self, min: A, max: A) -> Array<A, D>
166    {
167        assert!(min <= max, "min must be less than or equal to max");
168        self.mapv(|a| num_traits::clamp(a, min.clone(), max.clone()))
169    }
170}