Skip to content

Commit a796dc5

Browse files
KmolYuanNil Goyette
authored andcommitted
Apply most of the suggestions.
1 parent 82af40d commit a796dc5

File tree

3 files changed

+74
-64
lines changed

3 files changed

+74
-64
lines changed

src/lib.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,21 @@
112112
//! For conversion between `ndarray`, [`nalgebra`](https://crates.io/crates/nalgebra) and
113113
//! [`image`](https://crates.io/crates/image) check out [`nshare`](https://crates.io/crates/nshare).
114114
115-
116115
extern crate alloc;
117116

118-
#[cfg(feature = "std")]
119-
extern crate std;
120117
#[cfg(not(feature = "std"))]
121118
extern crate core as std;
119+
#[cfg(feature = "std")]
120+
extern crate std;
122121

123122
#[cfg(feature = "blas")]
124123
extern crate cblas_sys;
125124

126125
#[cfg(feature = "docs")]
127126
pub mod doc;
128127

129-
use std::marker::PhantomData;
130128
use alloc::sync::Arc;
129+
use std::marker::PhantomData;
131130

132131
pub use crate::dimension::dim::*;
133132
pub use crate::dimension::{Axis, AxisDescription, Dimension, IntoDimension, RemoveAxis};
@@ -146,16 +145,16 @@ use crate::iterators::Baseiter;
146145
use crate::iterators::{ElementsBase, ElementsBaseMut, Iter, IterMut};
147146

148147
pub use crate::arraytraits::AsArray;
148+
pub use crate::linalg_traits::LinalgScalar;
149149
#[cfg(feature = "std")]
150150
pub use crate::linalg_traits::NdFloat;
151-
pub use crate::linalg_traits::LinalgScalar;
152151

153152
#[allow(deprecated)] // stack_new_axis
154153
pub use crate::stacking::{concatenate, stack, stack_new_axis};
155154

156-
pub use crate::math_cell::MathCell;
157155
pub use crate::impl_views::IndexLonger;
158-
pub use crate::shape_builder::{Shape, ShapeBuilder, ShapeArg, StrideShape};
156+
pub use crate::math_cell::MathCell;
157+
pub use crate::shape_builder::{Shape, ShapeArg, ShapeBuilder, StrideShape};
159158

160159
#[macro_use]
161160
mod macro_utils;
@@ -176,8 +175,7 @@ mod data_traits;
176175
pub use crate::aliases::*;
177176

178177
pub use crate::data_traits::{
179-
Data, DataMut, DataOwned, DataShared, RawData, RawDataClone, RawDataMut,
180-
RawDataSubst,
178+
Data, DataMut, DataOwned, DataShared, RawData, RawDataClone, RawDataMut, RawDataSubst,
181179
};
182180

183181
mod free_functions;
@@ -200,9 +198,9 @@ mod partial;
200198
mod shape_builder;
201199
#[macro_use]
202200
mod slice;
201+
mod low_level_util;
203202
mod split_at;
204203
mod stacking;
205-
mod low_level_util;
206204
#[macro_use]
207205
mod zip;
208206

@@ -1502,12 +1500,10 @@ impl<'a, A> CowRepr<'a, A> {
15021500
// Consider the doc effect of ordering modules here.
15031501
mod impl_clone;
15041502

1505-
mod impl_internal_constructors;
15061503
mod impl_constructors;
1504+
mod impl_internal_constructors;
15071505

15081506
mod impl_methods;
1509-
#[cfg(feature = "std")]
1510-
mod impl_float_maths;
15111507
mod impl_owned_array;
15121508
mod impl_special_element_types;
15131509

@@ -1566,9 +1562,7 @@ where
15661562
let d = self.dim.try_remove_axis(axis);
15671563
let s = self.strides.try_remove_axis(axis);
15681564
// safe because new dimension, strides allow access to a subset of old data
1569-
unsafe {
1570-
self.with_strides_dim(s, d)
1571-
}
1565+
unsafe { self.with_strides_dim(s, d) }
15721566
}
15731567
}
15741568

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
//! Element-wise methods for ndarray
1+
// Element-wise methods for ndarray
22

3+
#[cfg(feature = "std")]
34
use num_traits::Float;
45

56
use crate::imp_prelude::*;
67

7-
macro_rules! boolean_op {
8-
($($(#[$meta1:meta])* fn $id1:ident $(#[$meta2:meta])* fn $id2:ident -> $func:ident)+) => {
9-
$($(#[$meta1])*
8+
#[cfg(feature = "std")]
9+
macro_rules! boolean_ops {
10+
($(#[$meta1:meta])* fn $func:ident
11+
$(#[$meta2:meta])* fn $all:ident
12+
$(#[$meta3:meta])* fn $any:ident) => {
13+
$(#[$meta1])*
1014
#[must_use = "method returns a new array and does not mutate the original value"]
11-
pub fn $id1(&self) -> Array<bool, D> {
15+
pub fn $func(&self) -> Array<bool, D> {
1216
self.mapv(A::$func)
1317
}
1418
$(#[$meta2])*
1519
#[must_use = "method returns a new boolean value and does not mutate the original value"]
16-
pub fn $id2(&self) -> bool {
17-
self.mapv(A::$func).iter().any(|&b| b)
18-
})+
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+
}
1928
};
2029
}
2130

22-
macro_rules! unary_op {
31+
#[cfg(feature = "std")]
32+
macro_rules! unary_ops {
2333
($($(#[$meta:meta])* fn $id:ident)+) => {
2434
$($(#[$meta])*
2535
#[must_use = "method returns a new array and does not mutate the original value"]
@@ -29,7 +39,8 @@ macro_rules! unary_op {
2939
};
3040
}
3141

32-
macro_rules! binary_op {
42+
#[cfg(feature = "std")]
43+
macro_rules! binary_ops {
3344
($($(#[$meta:meta])* fn $id:ident($ty:ty))+) => {
3445
$($(#[$meta])*
3546
#[must_use = "method returns a new array and does not mutate the original value"]
@@ -39,103 +50,87 @@ macro_rules! binary_op {
3950
};
4051
}
4152

42-
/// # Element-wise methods for Float Array
53+
/// # Element-wise methods for float arrays
4354
///
4455
/// Element-wise math functions for any array type that contains float number.
56+
#[cfg(feature = "std")]
4557
impl<A, S, D> ArrayBase<S, D>
4658
where
47-
A: Float,
59+
A: 'static + Float,
4860
S: Data<Elem = A>,
4961
D: Dimension,
5062
{
51-
boolean_op! {
63+
boolean_ops! {
5264
/// If the number is `NaN` (not a number), then `true` is returned for each element.
5365
fn is_nan
66+
/// Return `true` if all elements are `NaN` (not a number).
67+
fn is_all_nan
5468
/// Return `true` if any element is `NaN` (not a number).
55-
fn is_any_nan -> is_nan
56-
69+
fn is_any_nan
70+
}
71+
boolean_ops! {
5772
/// If the number is infinity, then `true` is returned for each element.
5873
fn is_infinite
74+
/// Return `true` if all elements are infinity.
75+
fn is_all_infinite
5976
/// Return `true` if any element is infinity.
60-
fn is_any_infinite -> is_infinite
77+
fn is_any_infinite
6178
}
62-
unary_op! {
79+
unary_ops! {
6380
/// The largest integer less than or equal to each element.
6481
fn floor
65-
6682
/// The smallest integer less than or equal to each element.
6783
fn ceil
68-
6984
/// The nearest integer of each element.
7085
fn round
71-
7286
/// The integer part of each element.
7387
fn trunc
74-
7588
/// The fractional part of each element.
7689
fn fract
77-
7890
/// Absolute of each element.
7991
fn abs
80-
8192
/// Sign number of each element.
8293
///
8394
/// + `1.0` for all positive numbers.
8495
/// + `-1.0` for all negative numbers.
8596
/// + `NaN` for all `NaN` (not a number).
8697
fn signum
87-
8898
/// The reciprocal (inverse) of each element, `1/x`.
8999
fn recip
90-
91100
/// Square root of each element.
92101
fn sqrt
93-
94102
/// `e^x` of each element (exponential function).
95103
fn exp
96-
97104
/// `2^x` of each element.
98105
fn exp2
99-
100106
/// Natural logarithm of each element.
101107
fn ln
102-
103108
/// Base 2 logarithm of each element.
104109
fn log2
105-
106110
/// Base 10 logarithm of each element.
107111
fn log10
108-
109112
/// Cubic root of each element.
110113
fn cbrt
111-
112114
/// Sine of each element (in radians).
113115
fn sin
114-
115116
/// Cosine of each element (in radians).
116117
fn cos
117-
118118
/// Tangent of each element (in radians).
119119
fn tan
120-
121120
/// Converts radians to degrees for each element.
122121
fn to_degrees
123-
124122
/// Converts degrees to radians for each element.
125123
fn to_radians
126124
}
127-
binary_op! {
125+
binary_ops! {
128126
/// Integer power of each element.
129127
///
130128
/// This function is generally faster than using float power.
131129
fn powi(i32)
132-
133130
/// Float power of each element.
134131
fn powf(A)
135-
136132
/// Logarithm of each element with respect to an arbitrary base.
137133
fn log(A)
138-
139134
/// The positive difference between given number and each element.
140135
fn abs_sub(A)
141136
}
@@ -145,18 +140,37 @@ where
145140
pub fn pow2(&self) -> Array<A, D> {
146141
self.mapv(|v: A| v * v)
147142
}
143+
}
148144

149-
/// Limit the values for each element.
145+
impl<A, S, D> ArrayBase<S, D>
146+
where
147+
A: 'static + PartialOrd + Clone,
148+
S: Data<Elem = A>,
149+
D: Dimension,
150+
{
151+
/// Limit the values for each element, similar to NumPy's `clip` function.
150152
///
151153
/// ```
152-
/// use ndarray::{Array1, array};
154+
/// use ndarray::array;
153155
///
154-
/// let a = Array1::range(0., 10., 1.);
155-
/// assert_eq!(a.clip(1., 8.), array![1., 1., 2., 3., 4., 5., 6., 7., 8., 8.]);
156-
/// assert_eq!(a.clip(8., 1.), array![1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]);
157-
/// assert_eq!(a.clip(3., 6.), array![3., 3., 3., 3., 4., 5., 6., 6., 6., 6.]);
156+
/// let a = array![0., 1., 2., 3., 4., 5., 6., 7., 8., 9.];
157+
/// assert_eq!(a.clamp(1., 8.), array![1., 1., 2., 3., 4., 5., 6., 7., 8., 8.]);
158+
/// assert_eq!(a.clamp(3., 6.), array![3., 3., 3., 3., 4., 5., 6., 6., 6., 6.]);
158159
/// ```
159-
pub fn clip(&self, min: A, max: A) -> Array<A, D> {
160-
self.mapv(|v| A::min(A::max(v, min), max))
160+
///
161+
/// # Panics
162+
///
163+
/// Panics if `min > max`, `min` is `NaN`, or `max` is `NaN`.
164+
pub fn clamp(&self, min: A, max: A) -> Array<A, D> {
165+
assert!(min <= max, "min must be less than or equal to max");
166+
self.mapv(|v| {
167+
if v < min {
168+
min.clone()
169+
} else if v > max {
170+
max.clone()
171+
} else {
172+
v
173+
}
174+
})
161175
}
162176
}

src/numeric/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
mod impl_numeric;
2+
3+
mod impl_float_maths;

0 commit comments

Comments
 (0)