On Mon, Jun 22, 2009 at 7:27 AM, Darren Dale <[email protected]> wrote:
> > > On Mon, Jun 22, 2009 at 3:04 AM, Charles R Harris < > [email protected]> wrote: > >> On Mon, Jun 22, 2009 at 12:18 AM, Pierre GM<[email protected]> wrote: >> > >> > On Jun 21, 2009, at 5:01 AM, David Cournapeau wrote: >> > >> >> (Continuing the discussion initiated in the neighborhood iterator >> >> thread) >> >> >> >> Hi, >> >> >> >> I would like to gather people's opinion on what to target for numpy >> >> 1.4.0. >> > >> > Is this a wish list ? >> > >> > * As Darren mentioned, some __array_prepare__ method called when a >> > ufunc is called on a subclass of ndarray, before any computation takes >> > place. Think of it as a parallel to __array_wrap__ >> > >> >> This is an interesting idea but it need some fleshing out. Working out >> a few example applications would firm things up I think. Things like >> what parameters would be passed need to be specified. > > > I posted an explanation of what parameters would be passed and a simple > example of how the implementation might look and behave. I gave two > application examples, MaskedArray and Quantities. > > >> For instance, if >> units are involved there would be a difference between binary and >> unary functions. And what of things like log and exp which should have >> unitless arguments? There looks to be a lot of preliminary work there. >> > > These considerations are already addressed and many of the ufuncs > (arithmetic, inverse, etc) are already implemented in Quantities-0.5b2 (I'll > post a 0.5b3 that implements log and exp this morning at PyPI). The identity > of the ufunc itself is used as the context for operating on the units or > raising an error if the units are incompatible with the operation. > I just posted sources and installers for quantities-0.5b5 at http://pypi.python.org/pypi . Part of the code that allows quantities to work with standard numpy ufuncs is in quantity.py (Quantity.__array_wrap__) and the rest is at the end of dimensionality.py. So for example, you can do: import numpy as np import quantities as pq np.log(pq.m) # raises an error np.log(pq.m/pq.m) # yields: array(0.0) * dimensionless unrecognized ufuncs return a dimensionless quantity by default, but quantities could raise an error instead: >>> np.logical_and(pq.m,pq.m) ufunc <ufunc 'logical_and'> not implemented, please file a bug report array(True, dtype=bool) * dimensionless Unit handling is all implemented in __array_wrap__, but this occurs too late in the ufunc to catch errors before the ufunc can modify data in-place. For example: q1=1*pq.m q2=1*pq.s np.add(q1,q2) # raises an ValueError np.add(q1,q2, q1) # also raises a ValueError, but too late: print q1 # yields array(2.0) * m Darren
_______________________________________________ Numpy-discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
