[Pyrex] Newbie questions: Numeric vs Numpy

Robert Kern robert.kern at gmail.com
Mon Oct 1 18:36:59 CEST 2007


Daniel wrote:
> Hi folks,
> 
> I’m newby in Pyrex. I’m trying to optimize a python application. I’m
> working on Windows XP enviroment, Python 2.5 and the last binary pirex
> version 0.9.3.1
> 
> First I tried, following an example from pirex´s guide using Numeric,
> and it works exactly like my old python version. The problem is I used
> numpy.ndarray so I have to transform into Numeric.array, use the rutin
> and back to numpy. It’s not good.

We have Pyrex examples in the numpy source (numpy/doc/pyrex). You should look at
those instead of using Numeric.

> Looking for a solution, I followed an example in pirex’s code at
> http://www.scipy.org/PerformancePython

I'm afraid that I don't see much similarity between the code there and what you
wrote. It is also a good example of how to use numpy in Pyrex.

> It works with ndarray but the results are not the same. I can not find
> where the error is.

Here's one problem: you've declared `a` as an object, not an ndarray with the
appropriate struct information. When you do this:

     elema = <double *>a.data

what happens is that you get the Python `data` attribute, which is a buffer
object, not a pointer to the start of the data. Casting it to <double *> just
casts the pointer to the buffer object.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Pyrex mailing list