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

CopyNode or similar #16

Open
arcondello opened this issue Jun 12, 2024 · 0 comments
Open

CopyNode or similar #16

arcondello opened this issue Jun 12, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@arcondello
Copy link
Member

When reshaping, it's not always possible to return a view. In that case, NumPy makes a copy of the array to be reshaped, and then reshapes the copy.

In [1]: arr = np.arange(40).reshape(10, 4)

In [2]: arr
Out[2]: 
array([[ 0,  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]])

In [3]: arr.base
Out[3]: 
array([ 0,  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])

In [4]: arr[::3, ::2]
Out[4]: 
array([[ 0,  2],
       [12, 14],
       [24, 26],
       [36, 38]])

In [5]: arr[::3, ::2].base
Out[5]: 
array([ 0,  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])

In [6]: arr[::3, ::2].reshape(-1)
Out[6]: array([ 0,  2, 12, 14, 24, 26, 36, 38])

In [7]: arr[::3, ::2].reshape(-1).base
Out[7]: 
array([[ 0,  2],
       [12, 14],
       [24, 26],
       [36, 38]])

In [8]: arr[::3, ::2].reshape(-1).base.base

The CopyNode would take a non-contiguous node and make a contiguous copy.

@arcondello arcondello added the enhancement New feature or request label Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant