7676 D : Dimension + RemoveAxis ,
7777{
7878 fn swap_index_axis ( & mut self , axis : Axis , idx_source : usize , idx_dest : usize ) {
79- // TODO: Avoid copying both; this is needed currently to avoid mutably
80- // borrowing something which has an outstanding immutable
81- // borrow.
79+ // TODO[perf] : Avoid copying both; this is needed currently to avoid mutably
80+ // borrowing something which has an outstanding immutable
81+ // borrow.
8282 let source = self . index_axis ( axis, idx_source) . clone ( ) . to_owned ( ) ;
8383 let dest = self . index_axis ( axis, idx_dest) . clone ( ) . to_owned ( ) ;
8484 self . index_axis_mut ( axis, idx_source) . assign ( & dest) ;
8888
8989#[ cfg( test) ]
9090mod tests {
91- use super :: ArrayBaseMatrixExt ;
91+ use super :: { ArrayBaseMatrixExt , ArrayBaseRemoveAxisExt } ;
9292 use ndarray:: { array, Array2 } ;
9393
9494 #[ test]
@@ -110,4 +110,20 @@ mod tests {
110110 array![ [ 6.0 , 0.0 , 0.0 ] , [ 2.0 , 12.0 , 0.0 ] , [ 4.0 , 15.0 , 3.0 ] ]
111111 ) ;
112112 }
113+
114+ #[ test]
115+ fn swap_index_axis_swaps_rows_correctly ( ) {
116+ let mut mtx = array ! [ [ 6.0 , 18.0 , 3.0 ] , [ 2.0 , 12.0 , 1.0 ] , [ 4.0 , 15.0 , 3.0 ] ] ;
117+ mtx. swap_index_axis ( ndarray:: Axis ( 0 ) , 1 , 2 ) ;
118+ let expected = array ! [ [ 6.0 , 18.0 , 3.0 ] , [ 4.0 , 15.0 , 3.0 ] , [ 2.0 , 12.0 , 1.0 ] ] ;
119+ assert_eq ! ( mtx, expected) ;
120+ }
121+
122+ #[ test]
123+ fn swap_index_axis_swaps_columns_correctly ( ) {
124+ let mut mtx = array ! [ [ 6.0 , 18.0 , 3.0 ] , [ 2.0 , 12.0 , 1.0 ] , [ 4.0 , 15.0 , 3.0 ] ] ;
125+ mtx. swap_index_axis ( ndarray:: Axis ( 1 ) , 1 , 2 ) ;
126+ let expected = array ! [ [ 6.0 , 3.0 , 18.0 ] , [ 2.0 , 1.0 , 12.0 ] , [ 4.0 , 3.0 , 15.0 ] ] ;
127+ assert_eq ! ( mtx, expected) ;
128+ }
113129}
0 commit comments