File: rollaxis.rs

package info (click to toggle)
rust-ndarray 0.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,028 kB
  • sloc: sh: 30; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 765 bytes parent folder | download
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
38
39
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);
}