[Numpy-discussion] endianness ufuncs

Timothy Hochberg tim.hochberg@ieee....
Fri Apr 13 09:08:43 CDT 2007

On 4/13/07, Hugo <numpy@hugo.doemaarwat.nl> wrote:
> Hi,
> As as astronomer I work with big endian arrays (pyfits 1.1rc1 with numpy
> 1.0) on little endian machines. When I try to make a ufunc (e.g. to
> convert from degrees to radians), it doesn't recognize the big endianness:
> In [3]: big = numpy.array([83.4, 83.5, 83.9], dtype='>f8')
> In [4]: uradians = numpy.frompyfunc(math.radians, 1, 1)
> In [6]: uradians(big)
> Out[6]: array([-2.6919063283e-182, 1.26773292198e-318,
> -2.69190632908e-182], dtype=object)
> I cannot figure out whether this is the intended behavior but it sure is
> counterintuitive, since most of the time numpy handles the endianness
> transparently.

I suspect that this is a bug, but I'm not familiar enough with frompyfunc to
be sure. However, I have a separate point I'd like to make about frompyfunc
/ vectorize.

This is the second time in the last week or so where someone has posted an
example using frompyfunc/vectorize that really has no business being
implemented that way. Now it's perfectly possible that these examples are
being used because they are simple and the authors are aware that they will
result in a speed penalty over a pure numpy solution, but just in case, I
felt I should sqawk.

I this case, implementing radians as a pure numpy function such as:
    def nrad(x):
        return x * (np.pi/180.0)
results in a function that's about 20x faster than uradians as defined

Here's the actual timing code that I used:

    import numpy as np, math, timeit
    a= np.arange(100000, dtype=float)
    urad = np.frompyfunc(math.radians, 1, 1)
    def nrad(x):
        return x * (np.pi/180.0)
    assert np.allclose(urad(a), nrad(a))
    if __name__ == "__main__":
        print timeit.Timer("urad(a)", "from scratch import urad, nrad,
        print timeit.Timer("nrad(a)", "from scratch import urad, nrad,

For now I'm happy with just converting everything to native (little)
> endian at the start of my calculations and convert them back in the end.
> (Although I'm not sure what's the best way to do that yet.)
> Hugo
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070413/3df80fc3/attachment.html 

More information about the Numpy-discussion mailing list