Skip to content

CoW permute_axes #1026

Answered by jturner314
emmatyping asked this question in Q&A

You must be logged in to vote

I think what you're asking for is already available. CowArray and ArcArray have copy-on-write behavior, and .permuted_axes() doesn't require a copy of the data, so you can just create a CowArray or ArcArray and permute its axes. Here's an example using CowArray:

use ndarray::prelude::*;

fn main() {
    let a = array![[0., 1., 2.], [3., 4., 5.]];
    assert_eq!(a.shape(), &[2, 3]);
    
    let mut b = CowArray::from(a.view()).permuted_axes([1, 0]);
    assert_eq!(b.shape(), &[3, 2]);
    assert_eq!(b, array![[0., 3.], [1., 4.], [2., 5.]]);
    
    assert!(b.is_view());
    b[[0, 0]] = 6.;
    assert!(b.is_owned());
    
    assert_eq!(a, array![[0., 1., 2.], [3., 4., 5.]]);
    assert_eq!(

Replies: 1 comment

You must be logged in to vote
0 replies
Answer selected by emmatyping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1023 on June 05, 2021 01:25.