Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle row and column major layouts #603

Closed
SuperFluffy opened this issue Mar 21, 2019 · 4 comments
Closed

Toggle row and column major layouts #603

SuperFluffy opened this issue Mar 21, 2019 · 4 comments

Comments

@SuperFluffy
Copy link
Contributor

I have an array in row major layout and I want to transpose its rows and columns such that it ends up in column major layout.

This, however, requires exchanging the elements and memory locations (i,j) and (j,i), which currently does not seem to be possible with ndarray. Or is it and I have overlooked a method?

Take this example, which shows several different ways of creating a column major array:

use ndarray::prelude::*;
use ndarray::ShapeBuilder;

fn main() {
    let v: Vec<_> = (0..9).map(|x| x as f64).collect();

    let a = Array::from_shape_vec((3,3), v.clone()).unwrap();
    let b = Array::from_shape_vec((3,3).f(), v.clone()).unwrap();
    let c = a.t().to_owned();
    let d = Array::from_shape_vec((3,3).f(), a.as_slice_memory_order().unwrap().to_vec()).unwrap();
    dbg!(a);
    dbg!(b);
    dbg!(c);
    dbg!(d);
}

This will give the result:

[src/main.rs:11] a = [[0.0, 1.0, 2.0],
 [3.0, 4.0, 5.0],
 [6.0, 7.0, 8.0]] shape=[3, 3], strides=[3, 1], layout=C (0x1), const ndim=2
[src/main.rs:12] b = [[0.0, 3.0, 6.0],
 [1.0, 4.0, 7.0],
 [2.0, 5.0, 8.0]] shape=[3, 3], strides=[1, 3], layout=F (0x2), const ndim=2
[src/main.rs:13] c = [[0.0, 3.0, 6.0],
 [1.0, 4.0, 7.0],
 [2.0, 5.0, 8.0]] shape=[3, 3], strides=[1, 3], layout=F (0x2), const ndim=2
[src/main.rs:14] d = [[0.0, 3.0, 6.0],
 [1.0, 4.0, 7.0],
 [2.0, 5.0, 8.0]] shape=[3, 3], strides=[1, 3], layout=F (0x2), const ndim=2

What I, however, require is:

[src/main.rs:11] a = [[0.0, 1.0, 2.0],
 [3.0, 4.0, 5.0],
 [6.0, 7.0, 8.0]] shape=[3, 3], strides=[1, 3], layout=F (0x2), const ndim=2
@SuperFluffy
Copy link
Contributor Author

I just realized there is a way, which is however pretty ugly:

let a = Array::from_shape_vec((3,3), v.clone()).unwrap();
let e = Array::from_iter(a.t().iter().cloned()).into_shape((3,3)).unwrap().t().to_owned();
dbg!(a);
dbg!(e);

Giving:

[src/main.rs:12] a = [[0.0, 1.0, 2.0],
 [3.0, 4.0, 5.0],
 [6.0, 7.0, 8.0]] shape=[3, 3], strides=[3, 1], layout=C (0x1), const ndim=2
[src/main.rs:16] e = [[0.0, 1.0, 2.0],
 [3.0, 4.0, 5.0],
 [6.0, 7.0, 8.0]] shape=[3, 3], strides=[1, 3], layout=F (0x2), const ndim=2

This also reveals one major deficiency of into_shape: it requires IntoDimension instead of ShapeBuilder, so that I cannot execute (3,3).f() but need to do the t().to_owned() dance.

@SuperFluffy
Copy link
Contributor Author

And finally a slightly cleaner version of the last example which is still very noisy:

let a = Array::from_shape_vec((3,3), v.clone()).unwrap();
let f = Array::from_shape_vec((3,3).f(), a.t().iter().cloned().collect()).unwrap();

@jturner314
Copy link
Member

I use this extension trait in my own code:

pub trait ToOwnedF<A, D> {
    fn to_owned_f(&self) -> Array<A, D>;
}

impl<A, S, D> ToOwnedF<A, D> for ArrayBase<S, D>
where
    A: Copy + Clone,
    S: Data<Elem = A>,
    D: Dimension,
{
    fn to_owned_f(&self) -> Array<A, D> {
        let mut tmp = unsafe { Array::uninitialized(self.dim().f()) };
        tmp.assign(self);
        tmp
    }
}

There probably should be a built-in way to this kind of thing. You may also be interested in #390, which discusses a closely-related problem. I think we should add a CowArray type at a minimum (the additional mutable version described in #390 would also be useful but is less important) and add operations for changing shape and layout that return CowArray. I don't have time to work on this myself, but would be happy to accept contributions.

@bluss
Copy link
Member

bluss commented Mar 28, 2021

This will be covered by #390.

@bluss bluss closed this as completed Mar 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants