Skip to content

Commit

Permalink
Merge pull request #518 from jturner314/mean-fromprimitive
Browse files Browse the repository at this point in the history
Change mean_axis to use FromPrimitive
  • Loading branch information
bluss authored Dec 4, 2018
2 parents ad714f8 + 947b52b commit 9daa777
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/numeric/impl_numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// except according to those terms.

use std::ops::{Add, Div, Mul};
use libnum::{self, One, Zero, Float, FromPrimitive};
use libnum::{self, Zero, Float, FromPrimitive};
use itertools::free::enumerate;

use imp_prelude::*;
Expand Down Expand Up @@ -123,8 +123,9 @@ impl<A, S, D> ArrayBase<S, D>

/// Return mean along `axis`.
///
/// **Panics** if `axis` is out of bounds or if the length of the axis is
/// zero and division by zero panics for type `A`.
/// **Panics** if `axis` is out of bounds, if the length of the axis is
/// zero and division by zero panics for type `A`, or if `A::from_usize()`
/// fails for the axis length.
///
/// ```
/// use ndarray::{aview1, arr2, Axis};
Expand All @@ -137,16 +138,12 @@ impl<A, S, D> ArrayBase<S, D>
/// );
/// ```
pub fn mean_axis(&self, axis: Axis) -> Array<A, D::Smaller>
where A: Clone + Zero + One + Add<Output=A> + Div<Output=A>,
where A: Clone + Zero + FromPrimitive + Add<Output=A> + Div<Output=A>,
D: RemoveAxis,
{
let n = self.len_of(axis);
let n = A::from_usize(self.len_of(axis)).expect("Converting axis length to `A` must not fail.");
let sum = self.sum_axis(axis);
let mut cnt = A::zero();
for _ in 0..n {
cnt = cnt + A::one();
}
sum / &aview0(&cnt)
sum / &aview0(&n)
}

/// Return variance along `axis`.
Expand Down

0 comments on commit 9daa777

Please sign in to comment.