7
7
// except according to those terms.
8
8
9
9
use std:: ops:: { Add , Div , Mul } ;
10
- use libnum:: { self , One , Zero , Float , FromPrimitive } ;
10
+ use libnum:: { self , Zero , Float , FromPrimitive } ;
11
11
use itertools:: free:: enumerate;
12
12
13
13
use imp_prelude:: * ;
@@ -123,8 +123,9 @@ impl<A, S, D> ArrayBase<S, D>
123
123
124
124
/// Return mean along `axis`.
125
125
///
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.
128
129
///
129
130
/// ```
130
131
/// use ndarray::{aview1, arr2, Axis};
@@ -137,16 +138,12 @@ impl<A, S, D> ArrayBase<S, D>
137
138
/// );
138
139
/// ```
139
140
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 > ,
141
142
D : RemoveAxis ,
142
143
{
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." ) ;
144
145
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)
150
147
}
151
148
152
149
/// Return variance along `axis`.
0 commit comments