[Numpy-discussion] replacing the mechanism for dispatching ufuncs

Mark Wiebe mwwiebe@gmail....
Wed Jun 22 12:31:05 CDT 2011

On Wed, Jun 22, 2011 at 7:34 AM, Lluís <xscript@gmx.net> wrote:

> Darren Dale writes:
> > On Tue, Jun 21, 2011 at 1:57 PM, Mark Wiebe <mwwiebe@gmail.com> wrote:
> >> On Tue, Jun 21, 2011 at 12:36 PM, Charles R Harris
> >> <charlesr.harris@gmail.com> wrote:
> >>> How does the ufunc get called so it doesn't get caught in an endless
> loop?
> > [...]
> >> The function being called needs to ensure this, either by extracting a
> raw
> >> ndarray from instances of its class, or adding a 'subok = False'
> parameter
> >> to the kwargs.
> > I didn't understand, could you please expand on that or show an example?
> As I understood the initial description and examples, the ufunc overload
> will keep being used as long as its arguments are of classes that
> declare ufunc overrides (i.e., classes with the "_numpy_ufunc_"
> attribute).
> Thus Mark's comment saying that you have to either transform the
> arguments into raw ndarrays (either by creating new ones or passing a
> view) or use the "subok = False" kwarg parameter to break a possible
> overloading loop.

The sequence of events is something like this:

1. You call np.sin(x)
2. The np.sin ufunc looks at x, sees the _numpy_ufunc_ attribute, and calls
x._numpy_ufunc_(np.sin, x)
3. _numpy_ufunc_ uses np.sin.name (which is "sin") to get the correct my_sin
function to call
4A. If my_sin called np.sin(x), we would go back to 1. and get an infinite
4B. If x is a subclass of ndarray, my_sin can call np.sin(x, subok=False),
as this disables the subclass overloading mechanism.
4C. If x is not a subclass of ndarray, x needs to produce an ndarray, for
instance it might have an x.arr property. Then it can call np.sin(x.arr)


> Lluis
> --
>  "And it's much the same thing with knowledge, for whenever you learn
>  something new, the whole world becomes that much richer."
>  -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
>  Tollbooth
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110622/ed292798/attachment.html 

More information about the NumPy-Discussion mailing list