Determine if __array_wrap__ is called on a subclass of ndarray from unary ufunc or binary ufunc

Is there a way to find out if a subclass of ndarray __array_wrap__ with a unary function or a binary function? (more reference )

+6
source share
1 answer

This is only a partial answer:

The ufunc arguments are passed as a tuple to the context . The form:

 (ufunc, ufunc_args, ufunc_domain) 

You can check the length of ufunc_args to see if you have 1 argument or 2. As a side note, I have no idea what ufunc_domain (in my tests it always seems 0 ) ....

 import numpy as np class Tester(np.ndarray): def __array_wrap__(self,output,context=None): print context[0].__name__,'is binary' if len(context[1]) > 1 else 'is unary' return np.ndarray.__array_wrap__(self,output,context) a = np.zeros(10) b = a.view(Tester) print (type(b)) -b np.sqrt(b) b+b 

I assume you can tell __array_wrap__ whether it is binary or unary ufunc. Unfortunately, when I asked a question at the beginning, I was hoping to find out if this ufunc call was the result of a unary operator. I did not think of such things as np.abs and np.sqrt as unary functions.

+1
source

All Articles