@@ -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
use dimension:: stride_offset;
@@ -294,7 +295,7 @@ pub type Ixs = isize;
294
295
/// Subview takes two arguments: `axis` and `index`.
295
296
///
296
297
/// ```
297
- /// use ndarray::{arr3, aview2};
298
+ /// use ndarray::{arr3, aview2, Axis };
298
299
///
299
300
/// // 2 submatrices of 2 rows with 3 elements per row, means a shape of `[2, 2, 3]`.
300
301
///
@@ -310,8 +311,8 @@ pub type Ixs = isize;
310
311
/// // Let’s take a subview along the greatest dimension (axis 0),
311
312
/// // taking submatrix 0, then submatrix 1
312
313
///
313
- /// let sub_0 = a.subview(0 , 0);
314
- /// let sub_1 = a.subview(0 , 1);
314
+ /// let sub_0 = a.subview(Axis(0) , 0);
315
+ /// let sub_1 = a.subview(Axis(0) , 1);
315
316
///
316
317
/// assert_eq!(sub_0, aview2(&[[ 1, 2, 3],
317
318
/// [ 4, 5, 6]]));
@@ -320,7 +321,7 @@ pub type Ixs = isize;
320
321
/// assert_eq!(sub_0.shape(), &[2, 3]);
321
322
///
322
323
/// // This is the subview picking only axis 2, column 0
323
- /// let sub_col = a.subview(2 , 0);
324
+ /// let sub_col = a.subview(Axis(2) , 0);
324
325
///
325
326
/// assert_eq!(sub_col, aview2(&[[ 1, 4],
326
327
/// [ 7, 10]]));
@@ -1336,7 +1337,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1336
1337
/// **Panics** if `axis` or `index` is out of bounds.
1337
1338
///
1338
1339
/// ```
1339
- /// use ndarray::{arr1, arr2};
1340
+ /// use ndarray::{arr1, arr2, Axis };
1340
1341
///
1341
1342
/// let a = arr2(&[[1., 2.], // -- axis 0, row 0
1342
1343
/// [3., 4.], // -- axis 0, row 1
@@ -1345,13 +1346,13 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1345
1346
/// // \ axis 1, column 1
1346
1347
/// // axis 1, column 0
1347
1348
/// assert!(
1348
- /// a.subview(0 , 1) == arr1(&[3., 4.]) &&
1349
- /// a.subview(1 , 1) == arr1(&[2., 4., 6.])
1349
+ /// a.subview(Axis(0) , 1) == arr1(&[3., 4.]) &&
1350
+ /// a.subview(Axis(1) , 1) == arr1(&[2., 4., 6.])
1350
1351
/// );
1351
1352
/// ```
1352
- pub fn subview ( & self , axis : usize , index : Ix )
1353
+ pub fn subview ( & self , axis : Axis , index : Ix )
1353
1354
-> ArrayView < A , <D as RemoveAxis >:: Smaller >
1354
- where D : RemoveAxis
1355
+ where D : RemoveAxis ,
1355
1356
{
1356
1357
self . view ( ) . into_subview ( axis, index)
1357
1358
}
@@ -1362,19 +1363,19 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1362
1363
/// **Panics** if `axis` or `index` is out of bounds.
1363
1364
///
1364
1365
/// ```
1365
- /// use ndarray::{arr2, aview2};
1366
+ /// use ndarray::{arr2, aview2, Axis };
1366
1367
///
1367
1368
/// let mut a = arr2(&[[1., 2.],
1368
1369
/// [3., 4.]]);
1369
1370
///
1370
- /// a.subview_mut(1 , 1).iadd_scalar(&10.);
1371
+ /// a.subview_mut(Axis(1) , 1).iadd_scalar(&10.);
1371
1372
///
1372
1373
/// assert!(
1373
1374
/// a == aview2(&[[1., 12.],
1374
1375
/// [3., 14.]])
1375
1376
/// );
1376
1377
/// ```
1377
- pub fn subview_mut ( & mut self , axis : usize , index : Ix )
1378
+ pub fn subview_mut ( & mut self , axis : Axis , index : Ix )
1378
1379
-> ArrayViewMut < A , D :: Smaller >
1379
1380
where S : DataMut ,
1380
1381
D : RemoveAxis ,
@@ -1386,19 +1387,21 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1386
1387
/// and select the subview of `index` along that axis.
1387
1388
///
1388
1389
/// **Panics** if `index` is past the length of the axis.
1389
- pub fn isubview ( & mut self , axis : usize , index : Ix ) {
1390
- dimension:: do_sub ( & mut self . dim , & mut self . ptr , & self . strides , axis, index)
1390
+ pub fn isubview ( & mut self , axis : Axis , index : Ix ) {
1391
+ dimension:: do_sub ( & mut self . dim , & mut self . ptr , & self . strides ,
1392
+ axis. axis ( ) , index)
1391
1393
}
1392
1394
1393
1395
/// Along `axis`, select the subview `index` and return `self`
1394
1396
/// with that axis removed.
1395
1397
///
1396
1398
/// See [`.subview()`](#method.subview) and [*Subviews*](#subviews) for full documentation.
1397
- pub fn into_subview ( mut self , axis : usize , index : Ix )
1399
+ pub fn into_subview ( mut self , axis : Axis , index : Ix )
1398
1400
-> ArrayBase < S , <D as RemoveAxis >:: Smaller >
1399
- where D : RemoveAxis
1401
+ where D : RemoveAxis ,
1400
1402
{
1401
1403
self . isubview ( axis, index) ;
1404
+ let axis = axis. axis ( ) ;
1402
1405
// don't use reshape -- we always know it will fit the size,
1403
1406
// and we can use remove_axis on the strides as well
1404
1407
ArrayBase {
@@ -1450,15 +1453,16 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1450
1453
/// Iterator element is `ArrayView<A, D::Smaller>` (read-only array view).
1451
1454
///
1452
1455
/// ```
1453
- /// use ndarray::arr3;
1456
+ /// use ndarray::{arr3, Axis};
1457
+ ///
1454
1458
/// let a = arr3(&[[[ 0, 1, 2], // \ axis 0, submatrix 0
1455
1459
/// [ 3, 4, 5]], // /
1456
1460
/// [[ 6, 7, 8], // \ axis 0, submatrix 1
1457
1461
/// [ 9, 10, 11]]]); // /
1458
1462
/// // `outer_iter` yields the two submatrices along axis 0.
1459
1463
/// let mut iter = a.outer_iter();
1460
- /// assert_eq!(iter.next().unwrap(), a.subview(0 , 0));
1461
- /// assert_eq!(iter.next().unwrap(), a.subview(0 , 1));
1464
+ /// assert_eq!(iter.next().unwrap(), a.subview(Axis(0) , 0));
1465
+ /// assert_eq!(iter.next().unwrap(), a.subview(Axis(0) , 1));
1462
1466
/// ```
1463
1467
pub fn outer_iter ( & self ) -> OuterIter < A , D :: Smaller >
1464
1468
where D : RemoveAxis ,
@@ -1489,10 +1493,10 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1489
1493
/// See [*Subviews*](#subviews) for full documentation.
1490
1494
///
1491
1495
/// **Panics** if `axis` is out of bounds.
1492
- pub fn axis_iter ( & self , axis : usize ) -> OuterIter < A , D :: Smaller >
1493
- where D : RemoveAxis
1496
+ pub fn axis_iter ( & self , axis : Axis ) -> OuterIter < A , D :: Smaller >
1497
+ where D : RemoveAxis ,
1494
1498
{
1495
- iterators:: new_axis_iter ( self . view ( ) , axis)
1499
+ iterators:: new_axis_iter ( self . view ( ) , axis. axis ( ) )
1496
1500
}
1497
1501
1498
1502
@@ -1503,11 +1507,11 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1503
1507
/// (read-write array view).
1504
1508
///
1505
1509
/// **Panics** if `axis` is out of bounds.
1506
- pub fn axis_iter_mut ( & mut self , axis : usize ) -> OuterIterMut < A , D :: Smaller >
1510
+ pub fn axis_iter_mut ( & mut self , axis : Axis ) -> OuterIterMut < A , D :: Smaller >
1507
1511
where S : DataMut ,
1508
1512
D : RemoveAxis ,
1509
1513
{
1510
- iterators:: new_axis_iter_mut ( self . view_mut ( ) , axis)
1514
+ iterators:: new_axis_iter_mut ( self . view_mut ( ) , axis. axis ( ) )
1511
1515
}
1512
1516
1513
1517
@@ -1523,20 +1527,22 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1523
1527
///
1524
1528
/// ```
1525
1529
/// use ndarray::OwnedArray;
1526
- /// use ndarray::arr3;
1530
+ /// use ndarray::{ arr3, Axis} ;
1527
1531
///
1528
1532
/// let a = OwnedArray::from_iter(0..28).into_shape((2, 7, 2)).unwrap();
1529
- /// let mut iter = a.axis_chunks_iter(1 , 2);
1533
+ /// let mut iter = a.axis_chunks_iter(Axis(1) , 2);
1530
1534
///
1531
1535
/// // first iteration yields a 2 × 2 × 2 view
1532
1536
/// assert_eq!(iter.next().unwrap(),
1533
- /// arr3(&[[[0, 1], [2, 3]], [[14, 15], [16, 17]]]));
1537
+ /// arr3(&[[[ 0, 1], [ 2, 3]],
1538
+ /// [[14, 15], [16, 17]]]));
1534
1539
///
1535
1540
/// // however the last element is a 2 × 1 × 2 view since 7 % 2 == 1
1536
- /// assert_eq!(iter.next_back().unwrap(), arr3(&[[[12, 13]], [[26, 27]]]));
1541
+ /// assert_eq!(iter.next_back().unwrap(), arr3(&[[[12, 13]],
1542
+ /// [[26, 27]]]));
1537
1543
/// ```
1538
- pub fn axis_chunks_iter ( & self , axis : usize , size : usize ) -> AxisChunksIter < A , D > {
1539
- iterators:: new_chunk_iter ( self . view ( ) , axis, size)
1544
+ pub fn axis_chunks_iter ( & self , axis : Axis , size : usize ) -> AxisChunksIter < A , D > {
1545
+ iterators:: new_chunk_iter ( self . view ( ) , axis. axis ( ) , size)
1540
1546
}
1541
1547
1542
1548
/// Return an iterator that traverses over `axis` by chunks of `size`,
@@ -1545,11 +1551,11 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1545
1551
/// Iterator element is `ArrayViewMut<A, D>`
1546
1552
///
1547
1553
/// **Panics** if `axis` is out of bounds.
1548
- pub fn axis_chunks_iter_mut ( & mut self , axis : usize , size : usize )
1554
+ pub fn axis_chunks_iter_mut ( & mut self , axis : Axis , size : usize )
1549
1555
-> AxisChunksIterMut < A , D >
1550
1556
where S : DataMut
1551
1557
{
1552
- iterators:: new_chunk_iter_mut ( self . view_mut ( ) , axis, size)
1558
+ iterators:: new_chunk_iter_mut ( self . view_mut ( ) , axis. axis ( ) , size)
1553
1559
}
1554
1560
1555
1561
// Return (length, stride) for diagonal
@@ -2301,24 +2307,24 @@ impl<A, S, D> ArrayBase<S, D>
2301
2307
/// Return sum along `axis`.
2302
2308
///
2303
2309
/// ```
2304
- /// use ndarray::{aview0, aview1, arr2};
2310
+ /// use ndarray::{aview0, aview1, arr2, Axis };
2305
2311
///
2306
2312
/// let a = arr2(&[[1., 2.],
2307
2313
/// [3., 4.]]);
2308
2314
/// assert!(
2309
- /// a.sum(0 ) == aview1(&[4., 6.]) &&
2310
- /// a.sum(1 ) == aview1(&[3., 7.]) &&
2315
+ /// a.sum(Axis(0) ) == aview1(&[4., 6.]) &&
2316
+ /// a.sum(Axis(1) ) == aview1(&[3., 7.]) &&
2311
2317
///
2312
- /// a.sum(0) .sum(0 ) == aview0(&10.)
2318
+ /// a.sum(Axis(0)) .sum(Axis(0) ) == aview0(&10.)
2313
2319
/// );
2314
2320
/// ```
2315
2321
///
2316
2322
/// **Panics** if `axis` is out of bounds.
2317
- pub fn sum ( & self , axis : usize ) -> OwnedArray < A , <D as RemoveAxis >:: Smaller >
2323
+ pub fn sum ( & self , axis : Axis ) -> OwnedArray < A , <D as RemoveAxis >:: Smaller >
2318
2324
where A : Clone + Add < Output =A > ,
2319
2325
D : RemoveAxis ,
2320
2326
{
2321
- let n = self . shape ( ) [ axis] ;
2327
+ let n = self . shape ( ) [ axis. axis ( ) ] ;
2322
2328
let mut res = self . subview ( axis, 0 ) . to_owned ( ) ;
2323
2329
for i in 1 ..n {
2324
2330
let view = self . subview ( axis, i) ;
@@ -2355,24 +2361,23 @@ impl<A, S, D> ArrayBase<S, D>
2355
2361
2356
2362
/// Return mean along `axis`.
2357
2363
///
2364
+ /// **Panics** if `axis` is out of bounds.
2365
+ ///
2358
2366
/// ```
2359
- /// use ndarray::{aview1, arr2};
2367
+ /// use ndarray::{aview1, arr2, Axis };
2360
2368
///
2361
2369
/// let a = arr2(&[[1., 2.],
2362
2370
/// [3., 4.]]);
2363
2371
/// assert!(
2364
- /// a.mean(0 ) == aview1(&[2.0, 3.0]) &&
2365
- /// a.mean(1 ) == aview1(&[1.5, 3.5])
2372
+ /// a.mean(Axis(0) ) == aview1(&[2.0, 3.0]) &&
2373
+ /// a.mean(Axis(1) ) == aview1(&[1.5, 3.5])
2366
2374
/// );
2367
2375
/// ```
2368
- ///
2369
- ///
2370
- /// **Panics** if `axis` is out of bounds.
2371
- pub fn mean ( & self , axis : usize ) -> OwnedArray < A , <D as RemoveAxis >:: Smaller >
2376
+ pub fn mean ( & self , axis : Axis ) -> OwnedArray < A , <D as RemoveAxis >:: Smaller >
2372
2377
where A : LinalgScalar ,
2373
2378
D : RemoveAxis ,
2374
2379
{
2375
- let n = self . shape ( ) [ axis] ;
2380
+ let n = self . shape ( ) [ axis. axis ( ) ] ;
2376
2381
let mut sum = self . sum ( axis) ;
2377
2382
let one = libnum:: one :: < A > ( ) ;
2378
2383
let mut cnt = one;
@@ -2485,7 +2490,7 @@ impl<A, S> ArrayBase<S, (Ix, Ix)>
2485
2490
/// **Panics** if `index` is out of bounds.
2486
2491
pub fn row ( & self , index : Ix ) -> ArrayView < A , Ix >
2487
2492
{
2488
- self . subview ( 0 , index)
2493
+ self . subview ( Axis ( 0 ) , index)
2489
2494
}
2490
2495
2491
2496
/// Return a mutable array view of row `index`.
@@ -2494,15 +2499,15 @@ impl<A, S> ArrayBase<S, (Ix, Ix)>
2494
2499
pub fn row_mut ( & mut self , index : Ix ) -> ArrayViewMut < A , Ix >
2495
2500
where S : DataMut
2496
2501
{
2497
- self . subview_mut ( 0 , index)
2502
+ self . subview_mut ( Axis ( 0 ) , index)
2498
2503
}
2499
2504
2500
2505
/// Return an array view of column `index`.
2501
2506
///
2502
2507
/// **Panics** if `index` is out of bounds.
2503
2508
pub fn column ( & self , index : Ix ) -> ArrayView < A , Ix >
2504
2509
{
2505
- self . subview ( 1 , index)
2510
+ self . subview ( Axis ( 1 ) , index)
2506
2511
}
2507
2512
2508
2513
/// Return a mutable array view of column `index`.
@@ -2511,7 +2516,7 @@ impl<A, S> ArrayBase<S, (Ix, Ix)>
2511
2516
pub fn column_mut ( & mut self , index : Ix ) -> ArrayViewMut < A , Ix >
2512
2517
where S : DataMut
2513
2518
{
2514
- self . subview_mut ( 1 , index)
2519
+ self . subview_mut ( Axis ( 1 ) , index)
2515
2520
}
2516
2521
2517
2522
/// Perform matrix multiplication of rectangular arrays `self` and `rhs`.
0 commit comments