77// except according to those terms.
88
99use std:: ops:: { Add , Div , Mul } ;
10- use libnum:: { self , One , Zero , Float , FromPrimitive } ;
10+ use libnum:: { self , Zero , Float , FromPrimitive } ;
1111use itertools:: free:: enumerate;
1212
1313use 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