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, D> ArrayRef<A, D>
58where
59 A: 'static + Float,
60 D: Dimension,
61{
62 boolean_ops! {
63 /// If the number is `NaN` (not a number), then `true` is returned for each element.
64 fn is_nan
65 /// Return `true` if all elements are `NaN` (not a number).
66 fn is_all_nan
67 /// Return `true` if any element is `NaN` (not a number).
68 fn is_any_nan
69 }
70 boolean_ops! {
71 /// If the number is infinity, then `true` is returned for each element.
72 fn is_infinite
73 /// Return `true` if all elements are infinity.
74 fn is_all_infinite
75 /// Return `true` if any element is infinity.
76 fn is_any_infinite
77 }
78 unary_ops! {
79 /// The largest integer less than or equal to each element.
80 fn floor
81 /// The smallest integer less than or equal to each element.
82 fn ceil
83 /// The nearest integer of each element.
84 fn round
85 /// The integer part of each element.
86 fn trunc
87 /// The fractional part of each element.
88 fn fract
89 /// Absolute of each element.
90 fn abs
91 /// Sign number of each element.
92 ///
93 /// + `1.0` for all positive numbers.
94 /// + `-1.0` for all negative numbers.
95 /// + `NaN` for all `NaN` (not a number).
96 fn signum
97 /// The reciprocal (inverse) of each element, `1/x`.
98 fn recip
99 /// Square root of each element.
100 fn sqrt
101 /// `e^x` of each element (exponential function).
102 fn exp
103 /// `2^x` of each element.
104 fn exp2
105 /// `e^x - 1` of each element.
106 fn exp_m1
107 /// Natural logarithm of each element.
108 fn ln
109 /// Base 2 logarithm of each element.
110 fn log2
111 /// Base 10 logarithm of each element.
112 fn log10
113 /// `ln(1 + x)` of each element.
114 fn ln_1p
115 /// Cubic root of each element.
116 fn cbrt
117 /// Sine of each element (in radians).
118 fn sin
119 /// Cosine of each element (in radians).
120 fn cos
121 /// Tangent of each element (in radians).
122 fn tan
123 /// Arcsine of each element (return in radians).
124 fn asin
125 /// Arccosine of each element (return in radians).
126 fn acos
127 /// Arctangent of each element (return in radians).
128 fn atan
129 /// Hyperbolic sine of each element.
130 fn sinh
131 /// Hyperbolic cosine of each element.
132 fn cosh
133 /// Hyperbolic tangent of each element.
134 fn tanh
135 /// Inverse hyperbolic sine of each element.
136 fn asinh
137 /// Inverse hyperbolic cosine of each element.
138 fn acosh
139 /// Inverse hyperbolic tangent of each element.
140 fn atanh
141 /// Converts radians to degrees for each element.
142 fn to_degrees
143 /// Converts degrees to radians for each element.
144 fn to_radians
145 }
146 binary_ops! {
147 /// Integer power of each element.
148 ///
149 /// This function is generally faster than using float power.
150 fn powi(i32)
151 /// Float power of each element.
152 fn powf(A)
153 /// Logarithm of each element with respect to an arbitrary base.
154 fn log(A)
155 /// The positive difference between given number and each element.
156 fn abs_sub(A)
157 /// Length of the hypotenuse of a right-angle triangle of each element
158 fn hypot(A)
159 }
160
161 /// Square (two powers) of each element.
162 #[must_use = "method returns a new array and does not mutate the original value"]
163 pub fn pow2(&self) -> Array<A, D>
164 {
165 self.mapv(|v: A| v * v)
166 }
167}
168
169impl<A, D> ArrayRef<A, D>
170where
171 A: 'static + PartialOrd + Clone,
172 D: Dimension,
173{
174 /// Limit the values for each element, similar to NumPy's `clip` function.
175 ///
176 /// ```
177 /// use ndarray::array;
178 ///
179 /// let a = array![0., 1., 2., 3., 4., 5., 6., 7., 8., 9.];
180 /// assert_eq!(a.clamp(1., 8.), array![1., 1., 2., 3., 4., 5., 6., 7., 8., 8.]);
181 /// assert_eq!(a.clamp(3., 6.), array![3., 3., 3., 3., 4., 5., 6., 6., 6., 6.]);
182 /// ```
183 ///
184 /// # Panics
185 ///
186 /// Panics if `!(min <= max)`.
187 pub fn clamp(&self, min: A, max: A) -> Array<A, D>
188 {
189 assert!(min <= max, "min must be less than or equal to max");
190 self.mapv(|a| num_traits::clamp(a, min.clone(), max.clone()))
191 }
192}