[Numpy-discussion] numpy.fix and subclasses

Darren Dale dsdale24 at gmail.com
Sun Feb 22 22:35:41 EST 2009


I've been finding some numpy functions that could maybe be improved to work
better with ndarray subclasses. For example:

def fix(x, y=None):
    x = nx.asanyarray(x)
    if y is None:
        y = nx.zeros_like(x)
    y1 = nx.floor(x)
    y2 = nx.ceil(x)
    y[...] = nx.where(x >= 0, y1, y2)
    return y

This implementation is a problematic for subclasses, since it does not allow
metadata to propagate using the usual ufunc machinery of __array_wrap__,
like ceil and floor do. nx.zeros_like does yield another instance of
type(x), but y does not get x's metadata (such as units or a mask). Would it
be possible to do something like:

    if y is None:
        y = x*0

"where" is another function that could maybe be improved to work with the
rules established by array_priority, but I'm a lousy C programmer and I
haven't actually looked into how this would work. If "where" respected
array_priority, fix could be implemented as:

def fix(x, y=None):
    x = nx.asanyarray(x)
    y1 = nx.floor(x)
    y2 = nx.ceil(x)
    if y is None:
        return nx.where(x >= 0, y1, y2)
    y[...] = nx.where(x >= 0, y1, y2)
    return y

Darren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090222/39e627c6/attachment.html>


More information about the NumPy-Discussion mailing list