8000 Change mean_axis to use FromPrimitive by jturner314 · Pull Request #518 · rust-ndarray/ndarray · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000 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