Skip to content

ENH: Make swapaxes always return a view. Fixes #5260 - #5468

Merged
charris merged 2 commits into
numpy:masterfrom
jaimefrio:swapaxes_view
Jan 21, 2015
Merged

charris merged 2 commits into
numpy:masterfrom
jaimefrio:swapaxes_view

Conversation

@jaimefrio

Copy link
Copy Markdown
Member

No description provided.

Comment thread numpy/core/src/multiarray/shape.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea what the last argument does? It isn't documented... OK, looks like it is needed for __finalize__, can be NULL.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the code in PyArray_Transpose, which is the next function after this, as a template. We could actually eliminate this fast path altogether and let PyArray_Transpose handle the degenerate case also. There will be some unnecessary overhead, but perhaps simplicity is a better tradeoff than performance here.

@charris

charris commented Jan 21, 2015

Copy link
Copy Markdown
Member

Hmm, looks like PyArray_Transpose always returns a view and will work if a1 == a2, so all that is needed is to avoid returning the original array directly. I wouldn't worry about fast paths here.

This PR affects the C-API, so that should be documented. I'm inclined to think of this as a buglet fix, as the documentation says it returns a view. It's hard to think of a case where it could cause problems that wasn't due to a user bug.

@charris

charris commented Jan 21, 2015

Copy link
Copy Markdown
Member

Apart from that, LGTM ;)

swapaxes now returns a view of the input array when the axes to swap
are both the same, not the input array as it used to do. Fixes numpy#5260
@jaimefrio

Copy link
Copy Markdown
Member Author

Removed the fast path and added a C API remark to the release notes.

charris added a commit that referenced this pull request Jan 21, 2015
ENH: Make swapaxes always return a view. Fixes #5260
@charris
charris merged commit 960433e into numpy:master Jan 21, 2015
@charris

charris commented Jan 21, 2015

Copy link
Copy Markdown
Member

Merged, thanks. BTW, I grepped for PyArray_SetBaseObject and found 35 spots that may be views being created. Could be worth looking at making a function for that. That would make it easy to add a fast path if we wanted to.

@jaimefrio
jaimefrio deleted the swapaxes_view branch January 21, 2015 04:03
@jaimefrio

Copy link
Copy Markdown
Member Author

That would be a new C API function, right?

@charris

charris commented Jan 21, 2015

Copy link
Copy Markdown
Member

Could be. It would be interesting to see how common it is in extension code.

@jaimefrio jaimefrio mentioned this pull request Mar 19, 2016
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

Successfully merging this pull request may close these issues.

2 participants