@@ -16,6 +16,7 @@ use crate::imp_prelude::*;
1616use crate :: numeric_util;
1717use crate :: ScalarOperand ;
1818use crate :: Slice ;
19+ use crate :: Zip ;
1920
2021/// # Numerical Methods for Arrays
2122impl < A , S , D > ArrayBase < S , D >
@@ -132,36 +133,29 @@ where
132133 A : Clone + One + Mul < Output = A > + ScalarOperand ,
133134 D : Dimension + RemoveAxis ,
134135 {
135- // First check dimensionality
136- if self . ndim ( ) > 1 && axis. is_none ( ) {
137- panic ! ( "axis parameter is required for arrays with more than one dimension" ) ;
138- }
136+ let mut res = Array :: ones ( self . raw_dim ( ) ) ;
139137
140138 match axis {
141139 None => {
142- // This case now only happens for 1D arrays
143- let mut res = Array :: ones ( self . raw_dim ( ) ) ;
140+ // For 1D arrays, use simple iteration
144141 let mut acc = A :: one ( ) ;
145-
146- for ( r, x) in res. iter_mut ( ) . zip ( self . iter ( ) ) {
147- acc = acc * x. clone ( ) ;
142+ Zip :: from ( & mut res) . and ( self ) . for_each ( |r, x| {
143+ acc = acc. clone ( ) * x. clone ( ) ;
148144 * r = acc. clone ( ) ;
149- }
150-
145+ } ) ;
151146 res
152147 }
153148 Some ( axis) => {
154- let mut res: Array < A , D > = Array :: ones ( self . raw_dim ( ) ) ;
149+ // For nD arrays, use fold_axis approach
150+ // Create accumulator array with one less dimension
151+ let mut acc = Array :: ones ( self . raw_dim ( ) . remove_axis ( axis) ) ;
155152
156- // Process each lane independently
157- for ( mut out_lane, in_lane) in res. lanes_mut ( axis) . into_iter ( ) . zip ( self . lanes ( axis) ) {
158- let mut acc = A :: one ( ) ;
159- for ( r, x) in out_lane. iter_mut ( ) . zip ( in_lane. iter ( ) ) {
160- acc = acc * x. clone ( ) ;
161- * r = acc. clone ( ) ;
162- }
153+ for i in 0 ..self . len_of ( axis) {
154+ // Get view of current slice along axis, and update accumulator element-wise multiplication
155+ let view = self . index_axis ( axis, i) ;
156+ acc = acc * & view;
157+ res. index_axis_mut ( axis, i) . assign ( & acc) ;
163158 }
164-
165159 res
166160 }
167161 }
0 commit comments