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

np.add with flat/reshape out argument #7244

Closed
MSeifert04 opened this issue Feb 14, 2016 · 5 comments
Closed

np.add with flat/reshape out argument #7244

MSeifert04 opened this issue Feb 14, 2016 · 5 comments

Comments

@MSeifert04
Copy link
Contributor

There is an issue on SO with the out argument of np.add and I tried almost everything and it's impossible (?) to get the out argument working if it's a view of another array. At least I couldn't work out how it could be done. Even if the behaviour is intended there should be a Warning/Error. Because np.add(b, b, a[1:3, 0:3].flat) (doesn't work at all because out is an iterator not an array and a TypeError is raised.) should be equivalent to a[1:3, 0:3].flat = np.add(b, b) (which works!).

I'm not very familiar with numpy internals so I can only guess what's causing this.

@njsmith
Copy link
Member

njsmith commented Feb 14, 2016

.flat is kind of a weird abandoned corner of numpy that we wish we could get rid of entirely, and a lot has changed in the last ten years since that book was written (assuming that it ever did work).

The problem is entirely with .flat -- in general you can pass array views as out arguments just fine, it's just, they have to be array objects, not the weird .flat object.

Do you have an actual use case here, or is it just that you're frustrated that the book is wrong? Are you aware that there's a second edition of the book available?

@MSeifert04
Copy link
Contributor Author

I got confused about when reshape and ravel return a copy instead of a view. This morning I thought:

a = np.ones(4)
b = np.zeros((10,10))
np.add(a,a,b[2:4,1:3].reshape(4)) # Variant 1
b[2:4,1:3] = np.add(a,a).reshape(2,2) # Variant 2

that these two should be identical. But of course the first one returns a copy during b[2:4,1:3].reshape(4) and therefore it cannot be propagated to b.

I'm very sorry to have bothered you.

@njsmith
Copy link
Member

njsmith commented Feb 14, 2016

Sorry if i sounded curt above -- just writing quickly from my phone :-).

The rules on returning views versus copies are definitely not obvious and easy to get confused by!

Thinking about it actually wouldn't be hard to issue a warning in the case where out= gets passed a temporary copy... Such temporaries are recognizable by having refcnt 1 + the OWNDATA flag set.

@MSeifert04
Copy link
Contributor Author

No problem, I was just emberassed because I missed that it created a temporary copy. :-)

I'll reopen the issue because such a Warning would be very helpful.

@seberg
Copy link
Member

seberg commented Oct 20, 2022

Closing for now, there might be a point in looking it again, but the past PR didn't work out, so it is probably more complicated (and does not seem very pressing).

@seberg seberg closed this as completed Oct 20, 2022
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