forked from rust-ndarray/ndarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollaxis.rs
37 lines (31 loc) · 839 Bytes
/
rollaxis.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
extern crate ndarray;
use ndarray::prelude::*;
use ndarray::Data;
pub fn roll_axis<A, S, D>(mut a: ArrayBase<S, D>, to: Axis, from: Axis) -> ArrayBase<S, D>
where S: Data<Elem=A>,
D: Dimension,
{
let i = to.index();
let mut j = from.index();
if j > i {
while i != j {
a.swap_axes(i, j);
j -= 1;
}
} else {
while i != j {
a.swap_axes(i, j);
j += 1;
}
}
a
}
fn main() {
let mut data = array![[[-1., 0., -2.], [1., 7., -3.]],
[[ 1., 0., -3.], [1., 7., 5.]],
[[ 1., 0., -3.], [1., 7., 5.]],
[[ 2., 0., 2.], [1., 7., 2.]]];
println!("{:8.4?}", data);
data = roll_axis(data, Axis(2), Axis(0));
println!("{:8.4?}", data);
}