[Numpy-discussion] numpy.fix and subclasses

Darren Dale dsdale24@gmail....
Sun Feb 22 21:35:41 CST 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://projects.scipy.org/pipermail/numpy-discussion/attachments/20090222/39e627c6/attachment.html 


More information about the Numpy-discussion mailing list