Skip to content

Commit 2a5553f

Browse files
KmolYuanNil Goyette
authored andcommitted
Add common used functions.
1 parent d3334a5 commit 2a5553f

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

src/impl_float_maths.rs

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

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,8 @@ mod impl_internal_constructors;
15061506
mod impl_constructors;
15071507

15081508
mod impl_methods;
1509+
#[cfg(feature = "std")]
1510+
mod impl_float_maths;
15091511
mod impl_owned_array;
15101512
mod impl_special_element_types;
15111513

0 commit comments

Comments
 (0)