Re: [Numpy-discussion] Lookup array

2011-10-11 Thread Andrey N. Sobolev
В Mon, 10 Oct 2011 11:20:08 -0400 Olivier Delalleau sh...@keba.be пишет: The following doesn't use numpy but seems to be about 20x faster: A_rows = {} for i, row in enumerate(A): A_rows[tuple(row)] = i for i, row in enumerate(B): C[i] = A_rows.get(tuple(row),

[Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Christoph Groth
Dear numpy experts, I could not find a satisfying solution to the following problem, so I thought I would ask: In one part of a large program I have to deal a lot with small (2d or 3d) vectors and matrices, performing simple linear algebra operations with them (dot products and matrix

Re: [Numpy-discussion] kind of a matrix multiplication

2011-10-11 Thread Martin Raspaud
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 11/10/11 07:49, Martin Raspaud wrote: Hi all, [...] I'm looking for the operation needed to get the two (stacked) vectors array([[0, 1, 2], [6, 8, 10]])) or its transpose. Hi again, Here is a solution I just found: np.einsum(ik,

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Olivier Delalleau
Here's a version that uses less Python loops and thus is faster. What still takes time is the array creation (np.array(...)), I'm not sure exactly why. It may be possible to speed it up. def points_numpy(radius): rr = radius**2 M = np.identity(2, dtype=int) x_y =

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Christoph Groth
Olivier Delalleau sh...@keba.be writes: Here's a version that uses less Python loops and thus is faster. What still takes time is the array creation (np.array(...)), I'm not sure exactly why. It may be possible to speed it up. Thank you for your suggestion. It doesn't help me however,

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Pauli Virtanen
11.10.2011 14:14, Christoph Groth kirjoitti: [clip] Thank you for your suggestion. It doesn't help me however, because the algorithm I'm _really_ trying to speed up cannot be vectorized with numpy in the way you vectorized my toy example. Any other ideas? Reformulate the problem so that it

Re: [Numpy-discussion] kind of a matrix multiplication

2011-10-11 Thread Olivier Delalleau
I don't really understand the operation you have in mind that should lead to your desired result, so here's a way to get it that discards most of mat's content: (which does not seem needed to compute what you want): (stack.T * mat[0, 0]).T -=- Olivier 2011/10/11 Martin Raspaud

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Jean-Luc Menut
Any other ideas? I'm not an expert at all, but I far as I understand if you cannot vectorize your problem, numpy is not the best tool to use if the speed matter a bit. Of course it's not a realistic example, but a simple loop computing a cosine is 3-4 time slower using numpy cos than python

[Numpy-discussion] genfromtxt

2011-10-11 Thread Nils Wagner
Hi all, How do I use genfromtxt to read a file with the following lines 11 2.2592365264892578D+01 22 2.2592365264892578D+01 13 2.669845581055D+00 33 2.2592365264892578D+01

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Christoph Groth
Pauli Virtanen p...@iki.fi writes: Thank you for your suggestion. It doesn't help me however, because the algorithm I'm _really_ trying to speed up cannot be vectorized with numpy in the way you vectorized my toy example. Any other ideas? Reformulate the problem so that it can be

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Skipper Seabold
On Tue, Oct 11, 2011 at 11:57 AM, Christoph Groth c...@falma.de wrote: Pauli Virtanen p...@iki.fi writes: Thank you for your suggestion.  It doesn't help me however, because the algorithm I'm _really_ trying to speed up cannot be vectorized with numpy in the way you vectorized my toy example.

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Pauli Virtanen
11.10.2011 17:57, Christoph Groth kirjoitti: [clip] My question was about ways to achieve a speedup without modifying the algorithm. I was hoping that there is some numpy-like library for python which for small arrays achieves a performance at least on par with the implementation using

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Olivier Delalleau
2011/10/11 Skipper Seabold jsseab...@gmail.com On Tue, Oct 11, 2011 at 11:57 AM, Christoph Groth c...@falma.de wrote: Pauli Virtanen p...@iki.fi writes: Thank you for your suggestion. It doesn't help me however, because the algorithm I'm _really_ trying to speed up cannot be vectorized

Re: [Numpy-discussion] genfromtxt

2011-10-11 Thread Derek Homeier
Hi Nils, On 11 Oct 2011, at 16:34, Nils Wagner wrote: How do I use genfromtxt to read a file with the following lines 11 2.2592365264892578D+01 22 2.2592365264892578D+01 13 2.669845581055D+00

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Christoph Groth
My question was about ways to achieve a speedup without modifying the algorithm. I was hoping that there is some numpy-like library for python which for small arrays achieves a performance at least on par with the implementation using tuples. This should be possible technically. I'm not

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Christoph Groth
Skipper Seabold jsseab...@gmail.com writes: So it's the dot function being called repeatedly on smallish arrays that's the bottleneck? I've run into this as well. See this thread [1]. (...) Thanks for the links. tokyo is interesting, though I fear the intermediate matrix size regime where

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Skipper Seabold
On Tue, Oct 11, 2011 at 12:41 PM, Christoph Groth c...@falma.de wrote: Skipper Seabold jsseab...@gmail.com writes: So it's the dot function being called repeatedly on smallish arrays that's the bottleneck? I've run into this as well. See this thread [1]. (...) Thanks for the links.  tokyo

Re: [Numpy-discussion] speeding up operations on small vectors

2011-10-11 Thread Bruce Southey
On 10/11/2011 12:06 PM, Skipper Seabold wrote: On Tue, Oct 11, 2011 at 12:41 PM, Christoph Grothc...@falma.de wrote: Skipper Seaboldjsseab...@gmail.com writes: So it's the dot function being called repeatedly on smallish arrays that's the bottleneck? I've run into this as well. See this

[Numpy-discussion] Rounding to next lowest float

2011-10-11 Thread Matthew Brett
Hi, Can anyone think of a clever way to round an integer to the next lowest integer represented in a particular floating point format? For example: In [247]: a = 2**25+3 This is out of range of the continuous integers representable by float32, hence: In [248]: print a, int(np.float32(a))

[Numpy-discussion] Nice float - integer conversion?

2011-10-11 Thread Matthew Brett
Hi, Have I missed a fast way of doing nice float to integer conversion? By nice I mean, rounding to the nearest integer, converting NaN to 0, inf, -inf to the max and min of the integer range? The astype method and cast functions don't do what I need here: In [40]: np.array([1.6, np.nan,

[Numpy-discussion] float128 casting rounding as if it were float64

2011-10-11 Thread Matthew Brett
Hi, While struggling with floating point precision, I ran into this: In [52]: a = 2**54+3 In [53]: a Out[53]: 18014398509481987L In [54]: np.float128(a) Out[54]: 18014398509481988.0 In [55]: np.float128(a)-1 Out[55]: 18014398509481987.0 The line above tells us that float128 can exactly

[Numpy-discussion] abs for max negative integers - desired behavior?

2011-10-11 Thread Matthew Brett
Hi, I recently ran into this: In [68]: arr = np.array(-128, np.int8) In [69]: arr Out[69]: array(-128, dtype=int8) In [70]: np.abs(arr) Out[70]: -128 Of course, I can see why this happens, but it is still surprising, and it seems to me that it would be a confusing source of bugs, because of

[Numpy-discussion] np.finfo().maxexp confusing

2011-10-11 Thread Matthew Brett
Hi, I realize it is probably too late to do anything about this, but: In [72]: info = np.finfo(np.float32) In [73]: info.minexp Out[73]: -126 In [74]: info.maxexp Out[74]: 128 minexp is correct, in that 2**(-126) is the minimum value for the exponent part of float32. But maxexp is not

Re: [Numpy-discussion] Nice float - integer conversion?

2011-10-11 Thread Derek Homeier
On 11 Oct 2011, at 20:06, Matthew Brett wrote: Have I missed a fast way of doing nice float to integer conversion? By nice I mean, rounding to the nearest integer, converting NaN to 0, inf, -inf to the max and min of the integer range? The astype method and cast functions don't do what I

Re: [Numpy-discussion] abs for max negative integers - desired behavior?

2011-10-11 Thread Charles R Harris
On Tue, Oct 11, 2011 at 12:23 PM, Matthew Brett matthew.br...@gmail.comwrote: Hi, I recently ran into this: In [68]: arr = np.array(-128, np.int8) In [69]: arr Out[69]: array(-128, dtype=int8) In [70]: np.abs(arr) Out[70]: -128 This has come up for discussion before, but no consensus

Re: [Numpy-discussion] Nice float - integer conversion?

2011-10-11 Thread josef . pktd
On Tue, Oct 11, 2011 at 3:06 PM, Derek Homeier de...@astro.physik.uni-goettingen.de wrote: On 11 Oct 2011, at 20:06, Matthew Brett wrote: Have I missed a fast way of doing nice float to integer conversion? By nice I mean, rounding to the nearest integer, converting NaN to 0, inf, -inf to the

Re: [Numpy-discussion] Rounding to next lowest float

2011-10-11 Thread Colin J. Williams
If you are using integers, why not use Python's Long? Colin W. On 11/10/2011 2:00 PM, Matthew Brett wrote: Hi, Can anyone think of a clever way to round an integer to the next lowest integer represented in a particular floating point format? For example: In [247]: a = 2**25+3 This is out

Re: [Numpy-discussion] Nice float - integer conversion?

2011-10-11 Thread Matthew Brett
Hi, On Tue, Oct 11, 2011 at 3:06 PM, Derek Homeier de...@astro.physik.uni-goettingen.de wrote: On 11 Oct 2011, at 20:06, Matthew Brett wrote: Have I missed a fast way of doing nice float to integer conversion? By nice I mean, rounding to the nearest integer, converting NaN to 0, inf, -inf

Re: [Numpy-discussion] abs for max negative integers - desired behavior?

2011-10-11 Thread Matthew Brett
Hi, On Tue, Oct 11, 2011 at 3:16 PM, Charles R Harris charlesr.har...@gmail.com wrote: On Tue, Oct 11, 2011 at 12:23 PM, Matthew Brett matthew.br...@gmail.com wrote: Hi, I recently ran into this: In [68]: arr = np.array(-128, np.int8) In [69]: arr Out[69]: array(-128, dtype=int8)

Re: [Numpy-discussion] Rounding to next lowest float

2011-10-11 Thread Matthew Brett
Hi, On Tue, Oct 11, 2011 at 3:20 PM, Colin J. Williams cjwilliam...@gmail.com wrote: If you are using integers, why not use Python's Long? You mean, why do I need to know the next lowest representable integer in a float type? It's because I have a floating point array that I'm converting to

Re: [Numpy-discussion] abs for max negative integers - desired behavior?

2011-10-11 Thread Matthew Brett
Hi On Tue, Oct 11, 2011 at 3:16 PM, Charles R Harris charlesr.har...@gmail.com wrote: On Tue, Oct 11, 2011 at 12:23 PM, Matthew Brett matthew.br...@gmail.com wrote: Hi, I recently ran into this: In [68]: arr = np.array(-128, np.int8) In [69]: arr Out[69]: array(-128, dtype=int8) In

Re: [Numpy-discussion] np.finfo().maxexp confusing

2011-10-11 Thread Matthew Brett
Hi, On Tue, Oct 11, 2011 at 2:39 PM, Matthew Brett matthew.br...@gmail.com wrote: Hi, I realize it is probably too late to do anything about this, but: In [72]: info = np.finfo(np.float32) In [73]: info.minexp Out[73]: -126 In [74]: info.maxexp Out[74]: 128 minexp is correct, in that

Re: [Numpy-discussion] Nice float - integer conversion?

2011-10-11 Thread Derek Homeier
On 11.10.2011, at 9:18PM, josef.p...@gmail.com wrote: In [42]: c = np.zeros(4, np.int16) In [43]: d = np.zeros(4, np.int32) In [44]: np.around([1.6,np.nan,np.inf,-np.inf], out=c) Out[44]: array([2, 0, 0, 0], dtype=int16) In [45]: np.around([1.6,np.nan,np.inf,-np.inf], out=d) Out[45]:

Re: [Numpy-discussion] abs for max negative integers - desired behavior?

2011-10-11 Thread Benjamin Root
On Tue, Oct 11, 2011 at 2:51 PM, Matthew Brett matthew.br...@gmail.comwrote: Hi On Tue, Oct 11, 2011 at 3:16 PM, Charles R Harris charlesr.har...@gmail.com wrote: On Tue, Oct 11, 2011 at 12:23 PM, Matthew Brett matthew.br...@gmail.com wrote: Hi, I recently ran into this:

Re: [Numpy-discussion] Nice float - integer conversion?

2011-10-11 Thread Benjamin Root
On Tue, Oct 11, 2011 at 2:06 PM, Derek Homeier de...@astro.physik.uni-goettingen.de wrote: On 11 Oct 2011, at 20:06, Matthew Brett wrote: Have I missed a fast way of doing nice float to integer conversion? By nice I mean, rounding to the nearest integer, converting NaN to 0, inf, -inf

Re: [Numpy-discussion] abs for max negative integers - desired behavior?

2011-10-11 Thread josef . pktd
On Tue, Oct 11, 2011 at 7:13 PM, Benjamin Root ben.r...@ou.edu wrote: On Tue, Oct 11, 2011 at 2:51 PM, Matthew Brett matthew.br...@gmail.com wrote: Hi On Tue, Oct 11, 2011 at 3:16 PM, Charles R Harris charlesr.har...@gmail.com wrote: On Tue, Oct 11, 2011 at 12:23 PM, Matthew Brett

Re: [Numpy-discussion] abs for max negative integers - desired behavior?

2011-10-11 Thread Benjamin Root
On Tue, Oct 11, 2011 at 6:33 PM, josef.p...@gmail.com wrote: On Tue, Oct 11, 2011 at 7:13 PM, Benjamin Root ben.r...@ou.edu wrote: On Tue, Oct 11, 2011 at 2:51 PM, Matthew Brett matthew.br...@gmail.com wrote: Hi On Tue, Oct 11, 2011 at 3:16 PM, Charles R Harris