[Numpy-discussion] replacing the mechanism for dispatching ufuncs
Wed Jun 22 12:31:05 CDT 2011
On Wed, Jun 22, 2011 at 7:34 AM, Lluís <email@example.com> wrote:
> Darren Dale writes:
> > On Tue, Jun 21, 2011 at 1:57 PM, Mark Wiebe <firstname.lastname@example.org> wrote:
> >> On Tue, Jun 21, 2011 at 12:36 PM, Charles R Harris
> >> <email@example.com> wrote:
> >>> How does the ufunc get called so it doesn't get caught in an endless
> > [...]
> >> The function being called needs to ensure this, either by extracting a
> >> ndarray from instances of its class, or adding a 'subok = False'
> >> 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_"
> 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
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)
> "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
> NumPy-Discussion mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion