Hi Numpy folks,
When working with floats, I prefer to have exact string
representations in doctests and other reference-based testing; I find it
helps a lot to avoid chasing cross-platform differences that are really
about the string conversion rather than about numerical differences.
Since Python 2.6, the, the hex() method on floats has been available and
it gives an exact representation. Is there any way to have Numpy arrays
of floats printed using this representation? If not, would there be
interest in adding that?
On a somewhat related note, is there a table someplace which shows
which versions of Python are supported in each release of Numpy? I
found an FAQ that mentioned 2.4 and 2.5, but since it didn't mention 2.6
or 2.7 (much less 3.1), I assume it's out of date. This relates to the
above since it would be harder to support a new hex printoption for
Pythons before 2.6.
Thanks,
Ken B.

We recently has a discussion regarding an optimization of NumPy's median
to average O(n) complexity. After some searching, I found out there is a
selection algorithm competitive in speed with Hoare's quick select. It
has the advantage of being a lot simpler to implement. In plain Python:
import numpy as np
def wirthselect(array, k):
""" Niklaus Wirth's selection algortithm """
a = np.ascontiguousarray(array)
if (a is array): a = a.copy()
l = 0
m = a.shape[0] - 1
while l < m:
x = a[k]
i = l
j = m
while 1:
while a[i] < x: i += 1
while x < a[j]: j -= 1
if i <= j:
tmp = a[i]
a[i] = a[j]
a[j] = tmp
i += 1
j -= 1
if i > j: break
if j < k: l = i
if k < i: m = j
return a
Now, the median can be obtained in average O(n) time as:
def median(x):
""" median in average O(n) time """
n = x.shape[0]
k = n >> 1
s = wirthselect(x, k)
if n & 1:
return s[k]
else:
return 0.5*(s[k]+s[:k].max())
The beauty of this is that Wirth select is extremely easy to migrate to
Cython:
import numpy
ctypedef numpy.double_t T # or whatever
def wirthselect(numpy.ndarray[T, ndim=1] array, int k):
cdef int i, j, l, m
cdef T x, tmp
cdef T *a
_array = np.ascontiguousarray(array)
if (_array is array): _array = _array.copy()
a = <T *> _array.data
l = 0
m = <int> a.shape[0] - 1
with nogil:
while l < m:
x = a[k]
i = l
j = m
while 1:
while a[i] < x: i += 1
while x < a[j]: j -= 1
if i <= j:
tmp = a[i]
a[i] = a[j]
a[j] = tmp
i += 1
j -= 1
if i > j: break
if j < k: l = i
if k < i: m = j
return _array
For example, we could have a small script that generates withselect for
all NumPy dtypes (T as template), and use a dict as jump table.
Chad, you can continue to write quick select using NumPy's C quick sort
in numpy/core/src/_sortmodule.c.src. When you are done, it might be
about 10% faster than this. :-)
Reference:
http://ndevilla.free.fr/median/median.pdf
Best regards,
Sturla Molden

On Wed, Dec 1, 2010 at 7:26 AM, John Hornstein
<John.Hornstein(a)nrl.navy.mil>wrote:
> Does NumPy 1.5 work with Python 2.7 or Python 3.x?
>
>
>
Yes, both. NumPy 1.5.1 fixes some small bugs and that is what you should
use.
Chuck

Hello all,
I have a little question about the speed of numpy vs IDL 7.0. I did a
very simple little check by computing just a cosine in a loop. I was
quite surprised to see an order of magnitude of difference between numpy
and IDL, I would have thought that for such a basic function, the speed
would be approximatively the same.
I suppose that some of the difference may come from the default data
type of 64bits in numpy and 32 bits in IDL. Is there a way to change the
numpy default data type (without recompiling) ?
And I'm not an expert at all, maybe there is a better explanation, like
a better use of the several CPU core by IDL ?
I'm working with windows 7 64 bits on a core i7.
any hint is welcome.
Thanks.
Here the IDL code :
Julian1 = SYSTIME( /JULIAN , /UTC )
for j=0,9999 do begin
for i=0,999 do begin
a=cos(2*!pi*i/100.)
endfor
endfor
Julian2 = SYSTIME( /JULIAN , /UTC )
print, (Julian2-Julian1)*86400.0
print,cpt
end
result:
% Compiled module: $MAIN$.
2.9999837
The python code:
from numpy import *
from time import time
time1 = time()
for j in range(10000):
for i in range(1000):
a=cos(2*pi*i/100.)
time2 = time()
print time2-time1
result:
In [2]: run python_test_speed.py
24.1809999943