Thanks to visit codestin.com
Credit goes to github.com

Skip to content

operator.pos does not trigger __array_ufunc__ #9166

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

Closed
ngoldbaum opened this issue May 23, 2017 · 3 comments
Closed

operator.pos does not trigger __array_ufunc__ #9166

ngoldbaum opened this issue May 23, 2017 · 3 comments

Comments

@ngoldbaum
Copy link
Member

ngoldbaum commented May 23, 2017

Consider the following example:

import numpy as np
from operator import abs, neg, pos

class MyArr(np.ndarray):
    def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
        print('calling array_ufunc from %s' % ufunc)
        func = getattr(ufunc, method)
        inp = [i.view(np.ndarray) for i in inputs]
        return func(*inp, **kwargs)

data = np.array([[1, 2, 3], [1, 2, 3]])

arr = data.view(MyArr)

for op in abs, neg, pos:
    print(op(arr))
    print(type(op(arr)))

In numpy 1.13rc2, this prints:

calling array_ufunc from <ufunc 'absolute'>
[[1 2 3]
 [1 2 3]]
calling array_ufunc from <ufunc 'absolute'>
<class 'numpy.ndarray'>
calling array_ufunc from <ufunc 'negative'>
[[-1 -2 -3]
 [-1 -2 -3]]
calling array_ufunc from <ufunc 'negative'>
<class 'numpy.ndarray'>
[[1 2 3]
 [1 2 3]]
<class '__main__.MyArr'>

So it looks like __array_ufunc__ isn't being invoked even though there is now a np.positive ufunc.

@ngoldbaum
Copy link
Member Author

ping @mhvk

@eric-wieser
Copy link
Member

eric-wieser commented May 23, 2017

This is by design - it was decided not to change ndarray.__pos__ to invoke np.positive (yet), due to compatibility concerns (a simple implementation would break -string_array).

I think @mhvk or @shoyer can probably give a more insightful rationale.

Perhaps the existance of __array_ufunc__ in a subclass should force the new behaviour into action?

@shoyer
Copy link
Member

shoyer commented May 23, 2017

Closing this in favor #9081, where we've discussed a few options for what to do here.

This is currently described in a footnote in the NEP: https://github.com/numpy/numpy/blob/master/doc/neps/ufunc-overrides.rst#list-of-operators-and-numpy-ufuncs

@shoyer shoyer closed this as completed May 23, 2017
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