Skip to content

CoW permute_axes #1023

Description

@emmatyping

I believe like to_shape (which is great btw, I really like using it!) it would be really handy to have a copy on write version of permute_axes, which only copies the matrix if it is written to. Would such a method be possible? I'm using permute_axes to do some tensor computations at the moment.

Activity

  1. jturner314 commented on Jun 5, 2021

    @jturner314
    Member

    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!(b, array![[6., 3.], [1., 4.], [2., 5.]]);
    }
  2. locked and limited conversation to collaborators on Jun 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions