@@ -92,6 +92,7 @@ use itertools::free::enumerate;
92
92
pub use dimension:: {
93
93
Dimension ,
94
94
RemoveAxis ,
95
+ Axis ,
95
96
} ;
96
97
97
98
pub use dimension:: NdIndex ;
@@ -292,7 +293,7 @@ pub type Ixs = isize;
292
293
/// Subview takes two arguments: `axis` and `index`.
293
294
///
294
295
/// ```
295
- /// use ndarray::{arr3, aview2};
296
+ /// use ndarray::{arr3, aview2, Axis };
296
297
///
297
298
/// // 2 submatrices of 2 rows with 3 elements per row, means a shape of `[2, 2, 3]`.
298
299
///
@@ -308,8 +309,8 @@ pub type Ixs = isize;
308
309
/// // Let’s take a subview along the greatest dimension (axis 0),
309
310
/// // taking submatrix 0, then submatrix 1
310
311
///
311
- /// let sub_0 = a.subview(0 , 0);
312
- /// let sub_1 = a.subview(0 , 1);
312
+ /// let sub_0 = a.subview(Axis(0) , 0);
313
+ /// let sub_1 = a.subview(Axis(0) , 1);
313
314
///
314
315
/// assert_eq!(sub_0, aview2(&[[ 1, 2, 3],
315
316
/// [ 4, 5, 6]]));
@@ -318,7 +319,7 @@ pub type Ixs = isize;
318
319
/// assert_eq!(sub_0.shape(), &[2, 3]);
319
320
///
320
321
/// // This is the subview picking only axis 2, column 0
321
- /// let sub_col = a.subview(2 , 0);
322
+ /// let sub_col = a.subview(Axis(2) , 0);
322
323
///
323
324
/// assert_eq!(sub_col, aview2(&[[ 1, 4],
324
325
/// [ 7, 10]]));
@@ -1265,7 +1266,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1265
1266
/// **Panics** if `axis` or `index` is out of bounds.
1266
1267
///
1267
1268
/// ```
1268
- /// use ndarray::{arr1, arr2};
1269
+ /// use ndarray::{arr1, arr2, Axis };
1269
1270
///
1270
1271
/// let a = arr2(&[[1., 2.], // -- axis 0, row 0
1271
1272
/// [3., 4.], // -- axis 0, row 1
@@ -1274,13 +1275,13 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1274
1275
/// // \ axis 1, column 1
1275
1276
/// // axis 1, column 0
1276
1277
/// assert!(
1277
- /// a.subview(0 , 1) == arr1(&[3., 4.]) &&
1278
- /// a.subview(1 , 1) == arr1(&[2., 4., 6.])
1278
+ /// a.subview(Axis(0) , 1) == arr1(&[3., 4.]) &&
1279
+ /// a.subview(Axis(1) , 1) == arr1(&[2., 4., 6.])
1279
1280
/// );
1280
1281
/// ```
1281
- pub fn subview ( & self , axis : usize , index : Ix )
1282
+ pub fn subview ( & self , axis : Axis , index : Ix )
1282
1283
-> ArrayView < A , <D as RemoveAxis >:: Smaller >
1283
- where D : RemoveAxis
1284
+ where D : RemoveAxis ,
1284
1285
{
1285
1286
self . view ( ) . into_subview ( axis, index)
1286
1287
}
@@ -1291,19 +1292,19 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1291
1292
/// **Panics** if `axis` or `index` is out of bounds.
1292
1293
///
1293
1294
/// ```
1294
- /// use ndarray::{arr2, aview2};
1295
+ /// use ndarray::{arr2, aview2, Axis };
1295
1296
///
1296
1297
/// let mut a = arr2(&[[1., 2.],
1297
1298
/// [3., 4.]]);
1298
1299
///
1299
- /// a.subview_mut(1 , 1).iadd_scalar(&10.);
1300
+ /// a.subview_mut(Axis(1) , 1).iadd_scalar(&10.);
1300
1301
///
1301
1302
/// assert!(
1302
1303
/// a == aview2(&[[1., 12.],
1303
1304
/// [3., 14.]])
1304
1305
/// );
1305
1306
/// ```
1306
- pub fn subview_mut ( & mut self , axis : usize , index : Ix )
1307
+ pub fn subview_mut ( & mut self , axis : Axis , index : Ix )
1307
1308
-> ArrayViewMut < A , D :: Smaller >
1308
1309
where S : DataMut ,
1309
1310
D : RemoveAxis ,
@@ -1315,19 +1316,21 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1315
1316
/// and select the subview of `index` along that axis.
1316
1317
///
1317
1318
/// **Panics** if `index` is past the length of the axis.
1318
- pub fn isubview ( & mut self , axis : usize , index : Ix ) {
1319
- dimension:: do_sub ( & mut self . dim , & mut self . ptr , & self . strides , axis, index)
1319
+ pub fn isubview ( & mut self , axis : Axis , index : Ix ) {
1320
+ dimension:: do_sub ( & mut self . dim , & mut self . ptr , & self . strides ,
1321
+ axis. axis ( ) , index)
1320
1322
}
1321
1323
1322
1324
/// Along `axis`, select the subview `index` and return `self`
1323
1325
/// with that axis removed.
1324
1326
///
1325
1327
/// See [`.subview()`](#method.subview) and [*Subviews*](#subviews) for full documentation.
1326
- pub fn into_subview ( mut self , axis : usize , index : Ix )
1328
+ pub fn into_subview ( mut self , axis : Axis , index : Ix )
1327
1329
-> ArrayBase < S , <D as RemoveAxis >:: Smaller >
1328
- where D : RemoveAxis
1330
+ where D : RemoveAxis ,
1329
1331
{
1330
1332
self . isubview ( axis, index) ;
1333
+ let axis = axis. axis ( ) ;
1331
1334
// don't use reshape -- we always know it will fit the size,
1332
1335
// and we can use remove_axis on the strides as well
1333
1336
ArrayBase {
@@ -1379,15 +1382,16 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1379
1382
/// Iterator element is `ArrayView<A, D::Smaller>` (read-only array view).
1380
1383
///
1381
1384
/// ```
1382
- /// use ndarray::arr3;
1385
+ /// use ndarray::{arr3, Axis};
1386
+ ///
1383
1387
/// let a = arr3(&[[[ 0, 1, 2], // \ axis 0, submatrix 0
1384
1388
/// [ 3, 4, 5]], // /
1385
1389
/// [[ 6, 7, 8], // \ axis 0, submatrix 1
1386
1390
/// [ 9, 10, 11]]]); // /
1387
1391
/// // `outer_iter` yields the two submatrices along axis 0.
1388
1392
/// let mut iter = a.outer_iter();
1389
- /// assert_eq!(iter.next().unwrap(), a.subview(0 , 0));
1390
- /// assert_eq!(iter.next().unwrap(), a.subview(0 , 1));
1393
+ /// assert_eq!(iter.next().unwrap(), a.subview(Axis(0) , 0));
1394
+ /// assert_eq!(iter.next().unwrap(), a.subview(Axis(0) , 1));
1391
1395
/// ```
1392
1396
pub fn outer_iter ( & self ) -> OuterIter < A , D :: Smaller >
1393
1397
where D : RemoveAxis ,
@@ -1418,10 +1422,10 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1418
1422
/// See [*Subviews*](#subviews) for full documentation.
1419
1423
///
1420
1424
/// **Panics** if `axis` is out of bounds.
1421
- pub fn axis_iter ( & self , axis : usize ) -> OuterIter < A , D :: Smaller >
1422
- where D : RemoveAxis
1425
+ pub fn axis_iter ( & self , axis : Axis ) -> OuterIter < A , D :: Smaller >
1426
+ where D : RemoveAxis ,
1423
1427
{
1424
- iterators:: new_axis_iter ( self . view ( ) , axis)
1428
+ iterators:: new_axis_iter ( self . view ( ) , axis. axis ( ) )
1425
1429
}
1426
1430
1427
1431
@@ -1432,11 +1436,11 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1432
1436
/// (read-write array view).
1433
1437
///
1434
1438
/// **Panics** if `axis` is out of bounds.
1435
- pub fn axis_iter_mut ( & mut self , axis : usize ) -> OuterIterMut < A , D :: Smaller >
1439
+ pub fn axis_iter_mut ( & mut self , axis : Axis ) -> OuterIterMut < A , D :: Smaller >
1436
1440
where S : DataMut ,
1437
1441
D : RemoveAxis ,
1438
1442
{
1439
- iterators:: new_axis_iter_mut ( self . view_mut ( ) , axis)
1443
+ iterators:: new_axis_iter_mut ( self . view_mut ( ) , axis. axis ( ) )
1440
1444
}
1441
1445
1442
1446
/// Return an iterator that traverses over `axis` by chunks of `size`,
@@ -1451,20 +1455,22 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1451
1455
///
1452
1456
/// ```
1453
1457
/// use ndarray::OwnedArray;
1454
- /// use ndarray::arr3;
1458
+ /// use ndarray::{ arr3, Axis} ;
1455
1459
///
1456
1460
/// let a = OwnedArray::from_iter(0..28).into_shape((2, 7, 2)).unwrap();
1457
- /// let mut iter = a.axis_chunks_iter(1 , 2);
1461
+ /// let mut iter = a.axis_chunks_iter(Axis(1) , 2);
1458
1462
///
1459
1463
/// // first iteration yields a 2 × 2 × 2 view
1460
1464
/// assert_eq!(iter.next().unwrap(),
1461
- /// arr3(&[[[0, 1], [2, 3]], [[14, 15], [16, 17]]]));
1465
+ /// arr3(&[[[ 0, 1], [ 2, 3]],
1466
+ /// [[14, 15], [16, 17]]]));
1462
1467
///
1463
1468
/// // however the last element is a 2 × 1 × 2 view since 7 % 2 == 1
1464
- /// assert_eq!(iter.next_back().unwrap(), arr3(&[[[12, 13]], [[26, 27]]]));
1469
+ /// assert_eq!(iter.next_back().unwrap(), arr3(&[[[12, 13]],
1470
+ /// [[26, 27]]]));
1465
1471
/// ```
1466
- pub fn axis_chunks_iter ( & self , axis : usize , size : usize ) -> AxisChunksIter < A , D > {
1467
- iterators:: new_chunk_iter ( self . view ( ) , axis, size)
1472
+ pub fn axis_chunks_iter ( & self , axis : Axis , size : usize ) -> AxisChunksIter < A , D > {
1473
+ iterators:: new_chunk_iter ( self . view ( ) , axis. axis ( ) , size)
1468
1474
}
1469
1475
1470
1476
/// Return an iterator that traverses over `axis` by chunks of `size`,
@@ -1473,11 +1479,11 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1473
1479
/// Iterator element is `ArrayViewMut<A, D>`
1474
1480
///
1475
1481
/// **Panics** if `axis` is out of bounds.
1476
- pub fn axis_chunks_iter_mut ( & mut self , axis : usize , size : usize )
1482
+ pub fn axis_chunks_iter_mut ( & mut self , axis : Axis , size : usize )
1477
1483
-> AxisChunksIterMut < A , D >
1478
1484
where S : DataMut
1479
1485
{
1480
- iterators:: new_chunk_iter_mut ( self . view_mut ( ) , axis, size)
1486
+ iterators:: new_chunk_iter_mut ( self . view_mut ( ) , axis. axis ( ) , size)
1481
1487
}
1482
1488
1483
1489
// Return (length, stride) for diagonal
@@ -2229,24 +2235,24 @@ impl<A, S, D> ArrayBase<S, D>
2229
2235
/// Return sum along `axis`.
2230
2236
///
2231
2237
/// ```
2232
- /// use ndarray::{aview0, aview1, arr2};
2238
+ /// use ndarray::{aview0, aview1, arr2, Axis };
2233
2239
///
2234
2240
/// let a = arr2(&[[1., 2.],
2235
2241
/// [3., 4.]]);
2236
2242
/// assert!(
2237
- /// a.sum(0 ) == aview1(&[4., 6.]) &&
2238
- /// a.sum(1 ) == aview1(&[3., 7.]) &&
2243
+ /// a.sum(Axis(0) ) == aview1(&[4., 6.]) &&
2244
+ /// a.sum(Axis(1) ) == aview1(&[3., 7.]) &&
2239
2245
///
2240
- /// a.sum(0) .sum(0 ) == aview0(&10.)
2246
+ /// a.sum(Axis(0)) .sum(Axis(0) ) == aview0(&10.)
2241
2247
/// );
2242
2248
/// ```
2243
2249
///
2244
2250
/// **Panics** if `axis` is out of bounds.
2245
- pub fn sum ( & self , axis : usize ) -> OwnedArray < A , <D as RemoveAxis >:: Smaller >
2251
+ pub fn sum ( & self , axis : Axis ) -> OwnedArray < A , <D as RemoveAxis >:: Smaller >
2246
2252
where A : Clone + Add < Output =A > ,
2247
2253
D : RemoveAxis ,
2248
2254
{
2249
- let n = self . shape ( ) [ axis] ;
2255
+ let n = self . shape ( ) [ axis. axis ( ) ] ;
2250
2256
let mut res = self . subview ( axis, 0 ) . to_owned ( ) ;
2251
2257
for i in 1 ..n {
2252
2258
let view = self . subview ( axis, i) ;
@@ -2283,24 +2289,23 @@ impl<A, S, D> ArrayBase<S, D>
2283
2289
2284
2290
/// Return mean along `axis`.
2285
2291
///
2292
+ /// **Panics** if `axis` is out of bounds.
2293
+ ///
2286
2294
/// ```
2287
- /// use ndarray::{aview1, arr2};
2295
+ /// use ndarray::{aview1, arr2, Axis };
2288
2296
///
2289
2297
/// let a = arr2(&[[1., 2.],
2290
2298
/// [3., 4.]]);
2291
2299
/// assert!(
2292
- /// a.mean(0 ) == aview1(&[2.0, 3.0]) &&
2293
- /// a.mean(1 ) == aview1(&[1.5, 3.5])
2300
+ /// a.mean(Axis(0) ) == aview1(&[2.0, 3.0]) &&
2301
+ /// a.mean(Axis(1) ) == aview1(&[1.5, 3.5])
2294
2302
/// );
2295
2303
/// ```
2296
- ///
2297
- ///
2298
- /// **Panics** if `axis` is out of bounds.
2299
- pub fn mean ( & self , axis : usize ) -> OwnedArray < A , <D as RemoveAxis >:: Smaller >
2304
+ pub fn mean ( & self , axis : Axis ) -> OwnedArray < A , <D as RemoveAxis >:: Smaller >
2300
2305
where A : LinalgScalar ,
2301
2306
D : RemoveAxis ,
2302
2307
{
2303
- let n = self . shape ( ) [ axis] ;
2308
+ let n = self . shape ( ) [ axis. axis ( ) ] ;
2304
2309
let mut sum = self . sum ( axis) ;
2305
2310
let one = libnum:: one :: < A > ( ) ;
2306
2311
let mut cnt = one;
@@ -2413,7 +2418,7 @@ impl<A, S> ArrayBase<S, (Ix, Ix)>
2413
2418
/// **Panics** if `index` is out of bounds.
2414
2419
pub fn row ( & self , index : Ix ) -> ArrayView < A , Ix >
2415
2420
{
2416
- self . subview ( 0 , index)
2421
+ self . subview ( Axis ( 0 ) , index)
2417
2422
}
2418
2423
2419
2424
/// Return a mutable array view of row `index`.
@@ -2422,15 +2427,15 @@ impl<A, S> ArrayBase<S, (Ix, Ix)>
2422
2427
pub fn row_mut ( & mut self , index : Ix ) -> ArrayViewMut < A , Ix >
2423
2428
where S : DataMut
2424
2429
{
2425
- self . subview_mut ( 0 , index)
2430
+ self . subview_mut ( Axis ( 0 ) , index)
2426
2431
}
2427
2432
2428
2433
/// Return an array view of column `index`.
2429
2434
///
2430
2435
/// **Panics** if `index` is out of bounds.
2431
2436
pub fn column ( & self , index : Ix ) -> ArrayView < A , Ix >
2432
2437
{
2433
- self . subview ( 1 , index)
2438
+ self . subview ( Axis ( 1 ) , index)
2434
2439
}
2435
2440
2436
2441
/// Return a mutable array view of column `index`.
@@ -2439,7 +2444,7 @@ impl<A, S> ArrayBase<S, (Ix, Ix)>
2439
2444
pub fn column_mut ( & mut self , index : Ix ) -> ArrayViewMut < A , Ix >
2440
2445
where S : DataMut
2441
2446
{
2442
- self . subview_mut ( 1 , index)
2447
+ self . subview_mut ( Axis ( 1 ) , index)
2443
2448
}
2444
2449
2445
2450
/// Perform matrix multiplication of rectangular arrays `self` and `rhs`.
0 commit comments