Skip to content

Commit 9daa777

Browse files
authored
Merge pull request #518 from jturner314/mean-fromprimitive
Change mean_axis to use FromPrimitive
2 parents ad714f8 + 947b52b commit 9daa777

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/numeric/impl_numeric.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88

99
use std::ops::{Add, Div, Mul};
10-
use libnum::{self, One, Zero, Float, FromPrimitive};
10+
use libnum::{self, Zero, Float, FromPrimitive};
1111
use itertools::free::enumerate;
1212

1313
use imp_prelude::*;
@@ -123,8 +123,9 @@ impl<A, S, D> ArrayBase<S, D>
123123

124124
/// Return mean along `axis`.
125125
///
126-
/// **Panics** if `axis` is out of bounds or if the length of the axis is
127-
/// zero and division by zero panics for type `A`.
126+
/// **Panics** if `axis` is out of bounds, if the length of the axis is
127+
/// zero and division by zero panics for type `A`, or if `A::from_usize()`
128+
/// fails for the axis length.
128129
///
129130
/// ```
130131
/// use ndarray::{aview1, arr2, Axis};
@@ -137,16 +138,12 @@ impl<A, S, D> ArrayBase<S, D>
137138
/// );
138139
/// ```
139140
pub fn mean_axis(&self, axis: Axis) -> Array<A, D::Smaller>
140-
where A: Clone + Zero + One + Add<Output=A> + Div<Output=A>,
141+
where A: Clone + Zero + FromPrimitive + Add<Output=A> + Div<Output=A>,
141142
D: RemoveAxis,
142143
{
143-
let n = self.len_of(axis);
144+
let n = A::from_usize(self.len_of(axis)).expect("Converting axis length to `A` must not fail.");
144145
let sum = self.sum_axis(axis);
145-
let mut cnt = A::zero();
146-
for _ in 0..n {
147-
cnt = cnt + A::one();
148-
}
149-
sum / &aview0(&cnt)
146+
sum / &aview0(&n)
150147
}
151148

152149
/// Return variance along `axis`.

0 commit comments

Comments
 (0)