@@ -71,7 +71,7 @@ impl<T: Num + PartialOrd + Copy> Tensor<T> {
71
71
72
72
// // Reduction operations
73
73
pub fn sum ( & self , axes : Axes ) -> Tensor < T > {
74
- let all_axes = ( 0 ..self . shape . len ( ) ) . collect :: < Vec < _ > > ( ) ;
74
+ let all_axes = ( 0 ..self . shape . order ( ) ) . collect :: < Vec < _ > > ( ) ;
75
75
let remaining_axes = all_axes. clone ( ) . into_iter ( ) . filter ( |& i| !axes. contains ( & i) ) . collect :: < Vec < _ > > ( ) ;
76
76
let remaining_dims = remaining_axes. iter ( ) . map ( |& i| self . shape . dims [ i] ) . collect :: < Vec < _ > > ( ) ;
77
77
let removing_dims = axes. iter ( ) . map ( |& i| self . shape . dims [ i] ) . collect :: < Vec < _ > > ( ) ;
@@ -126,7 +126,7 @@ impl<T: Num + PartialOrd + Copy> Tensor<T> {
126
126
} ) . collect ( ) ;
127
127
let n = removing_dims_t. iter ( ) . fold ( T :: one ( ) , |acc, x| acc * * x) ;
128
128
129
- let all_axes = ( 0 ..self . shape . len ( ) ) . collect :: < Vec < _ > > ( ) ;
129
+ let all_axes = ( 0 ..self . shape . order ( ) ) . collect :: < Vec < _ > > ( ) ;
130
130
let remaining_axes = all_axes. clone ( ) . into_iter ( ) . filter ( |& i| !axes. contains ( & i) ) . collect :: < Vec < _ > > ( ) ;
131
131
let remaining_dims = remaining_axes. iter ( ) . map ( |& i| self . shape . dims [ i] ) . collect :: < Vec < _ > > ( ) ;
132
132
let removing_dims = axes. iter ( ) . map ( |& i| self . shape . dims [ i] ) . collect :: < Vec < _ > > ( ) ;
@@ -162,7 +162,7 @@ impl<T: Num + PartialOrd + Copy> Tensor<T> {
162
162
}
163
163
164
164
pub fn max ( & self , axes : Axes ) -> Tensor < T > {
165
- let all_axes = ( 0 ..self . shape . len ( ) ) . collect :: < Vec < _ > > ( ) ;
165
+ let all_axes = ( 0 ..self . shape . order ( ) ) . collect :: < Vec < _ > > ( ) ;
166
166
let remaining_axes = all_axes. clone ( ) . into_iter ( ) . filter ( |& i| !axes. contains ( & i) ) . collect :: < Vec < _ > > ( ) ;
167
167
let remaining_dims = remaining_axes. iter ( ) . map ( |& i| self . shape . dims [ i] ) . collect :: < Vec < _ > > ( ) ;
168
168
let removing_dims = axes. iter ( ) . map ( |& i| self . shape . dims [ i] ) . collect :: < Vec < _ > > ( ) ;
@@ -196,7 +196,7 @@ impl<T: Num + PartialOrd + Copy> Tensor<T> {
196
196
}
197
197
198
198
pub fn min ( & self , axes : Axes ) -> Tensor < T > {
199
- let all_axes = ( 0 ..self . shape . len ( ) ) . collect :: < Vec < _ > > ( ) ;
199
+ let all_axes = ( 0 ..self . shape . order ( ) ) . collect :: < Vec < _ > > ( ) ;
200
200
let remaining_axes = all_axes. clone ( ) . into_iter ( ) . filter ( |& i| !axes. contains ( & i) ) . collect :: < Vec < _ > > ( ) ;
201
201
let remaining_dims = remaining_axes. iter ( ) . map ( |& i| self . shape . dims [ i] ) . collect :: < Vec < _ > > ( ) ;
202
202
let removing_dims = axes. iter ( ) . map ( |& i| self . shape . dims [ i] ) . collect :: < Vec < _ > > ( ) ;
@@ -249,16 +249,16 @@ impl<T: Num + PartialOrd + Copy> Tensor<T> {
249
249
/// For the maths see: https://bit.ly/3KQjPa3
250
250
fn calculate_index ( & self , indices : & [ usize ] ) -> usize {
251
251
let mut index = 0 ;
252
- for k in 0 ..self . shape . len ( ) {
252
+ for k in 0 ..self . shape . order ( ) {
253
253
let stride = self . shape . dims [ k+1 ..] . iter ( ) . product :: < usize > ( ) ;
254
254
index += indices[ k] * stride;
255
255
}
256
256
index
257
257
}
258
258
259
259
fn assert_indices ( & self , indices : & [ usize ] ) -> Result < ( ) , ShapeError > {
260
- if indices. len ( ) != self . shape . len ( ) {
261
- let msg = format ! ( "incorrect order ({} vs {})." , indices. len( ) , self . shape. len ( ) ) ;
260
+ if indices. len ( ) != self . shape . order ( ) {
261
+ let msg = format ! ( "incorrect order ({} vs {})." , indices. len( ) , self . shape. order ( ) ) ;
262
262
return Err ( ShapeError :: new ( msg. as_str ( ) ) ) ;
263
263
}
264
264
for ( i, & index) in indices. iter ( ) . enumerate ( ) {
@@ -288,15 +288,15 @@ impl<T: Num + PartialOrd + Copy> Mul<Tensor<T>> for Tensor<T> {
288
288
type Output = Tensor < T > ;
289
289
290
290
fn mul ( self , rhs : Tensor < T > ) -> Tensor < T > {
291
- if self . shape . len ( ) == 1 && rhs. shape . len ( ) == 1 {
291
+ if self . shape . order ( ) == 1 && rhs. shape . order ( ) == 1 {
292
292
// Vector-Vector multiplication (dot product)
293
293
assert ! ( self . shape[ 0 ] == rhs. shape[ 0 ] , "Vectors must be of the same length for dot product." ) ;
294
294
let mut result = T :: zero ( ) ;
295
295
for i in 0 ..self . shape [ 0 ] {
296
296
result = result + self . data [ i] * rhs. data [ i] ;
297
297
}
298
298
Tensor :: new ( & shape ! [ 1 ] . unwrap ( ) , & vec ! [ result] ) . unwrap ( )
299
- } else if self . shape . len ( ) == 1 && rhs. shape . len ( ) == 2 {
299
+ } else if self . shape . order ( ) == 1 && rhs. shape . order ( ) == 2 {
300
300
// Vector-Matrix multiplication
301
301
assert ! ( self . shape[ 0 ] == rhs. shape[ 0 ] , "The length of the vector must be equal to the number of rows in the matrix." ) ;
302
302
let mut result = Tensor :: zeros ( & shape ! [ rhs. shape[ 1 ] ] . unwrap ( ) ) ;
@@ -308,7 +308,7 @@ impl<T: Num + PartialOrd + Copy> Mul<Tensor<T>> for Tensor<T> {
308
308
result. data [ j] = sum;
309
309
}
310
310
result
311
- } else if self . shape . len ( ) == 2 && rhs. shape . len ( ) == 1 {
311
+ } else if self . shape . order ( ) == 2 && rhs. shape . order ( ) == 1 {
312
312
// Matrix-Vector multiplication
313
313
assert ! ( self . shape[ 1 ] == rhs. shape[ 0 ] , "The number of columns in the matrix must be equal to the length of the vector." ) ;
314
314
let mut result = Tensor :: zeros ( & shape ! [ self . shape[ 0 ] ] . unwrap ( ) ) ;
@@ -320,7 +320,7 @@ impl<T: Num + PartialOrd + Copy> Mul<Tensor<T>> for Tensor<T> {
320
320
result. data [ i] = sum;
321
321
}
322
322
result
323
- } else if self . shape . len ( ) == 2 && rhs. shape . len ( ) == 2 {
323
+ } else if self . shape . order ( ) == 2 && rhs. shape . order ( ) == 2 {
324
324
// Matrix-Matrix multiplication
325
325
assert ! ( self . shape[ 1 ] == rhs. shape[ 0 ] , "The number of columns in the first matrix must be equal to the number of rows in the second matrix." ) ;
326
326
let mut result = Tensor :: zeros ( & shape ! [ self . shape[ 0 ] , rhs. shape[ 1 ] ] . unwrap ( ) ) ;
0 commit comments