# [Numpy-discussion] How do I use numpy to do this?

Christopher Barker Chris.Barker at noaa.gov
Fri Jun 2 11:57:18 CDT 2006

```Robert Kern wrote:
>> As I need Numeric and numarray compatibility at this point, it seems the

> Ah. It might help if you said that up front.

Yes, it would, but that would mean accepting that I need to keep
backward compatibility -- I'm still hoping!

> x = arange(minx, maxx+step, step)  # oy.
> y = arange(miny, maxy+step, step)
>
> nx = len(x)
> ny = len(y)
>
> x = repeat(x, ny)
> y = concatenate([y] * nx)
> points = transpose([x, y])

Somehow I never think to use repeat. And why use repeat for x and
concatenate for y?

Rob Hooft wrote:
>
>  >>> k=Numeric.repeat(range(0,5+1),Numeric.ones(6)*7)
>  >>> l=Numeric.resize(range(0,6+1),[42])
>  >>>
> zone=Numeric.concatenate((k[:,Numeric.NewAxis],l[:,Numeric.NewAxis]),axis=1)

> This is the same speed as Robert Kern's solution for large arrays, a bit
> slower for small arrays. Both are a little faster than yours.

Did you time them? And yours only handles integers.

This is my timing:

For small arrays:

Using numpy
The Numpy way took: 0.020000 seconds
My way took: 0.010000 seconds
Robert's way took: 0.020000 seconds
Using Numeric
My way took: 0.010000 seconds
Robert's way took: 0.020000 seconds
Using numarray
My way took: 0.070000 seconds
Robert's way took: 0.120000 seconds
Number of X: 4
Number of Y: 3

So my way was faster with all three packages for small arrays.

For Medium arrays ( the size I'm most likely to be using ):
Using numpy
The Numpy way took: 0.120000 seconds
My way took: 0.040000 seconds
Robert's way took: 0.030000 seconds
Using Numeric
My way took: 0.040000 seconds
Robert's way took: 0.030000 seconds
Using numarray
My way took: 0.090000 seconds
Robert's way took: 1.070000 seconds
Number of X: 21
Number of Y: 41

Now we're getting close, with mine faster with numarray, but Robert's
faster with Numeric and numpy.

For Large arrays: (still not very big, but as big as I'm likely to need)

Using numpy
The Numpy way took: 4.200000 seconds
My way took: 0.660000 seconds
Robert's way took: 0.340000 seconds
Using Numeric
My way took: 0.590000 seconds
Robert's way took: 0.500000 seconds
Using numarray
My way took: 0.390000 seconds
Robert's way took: 20.340000 seconds
Number of X: 201
Number of Y: 241

So Robert's way still is faster with Numeric and numpy, but Much slower
with numarray. As it's close with numpy and Numeric, but mine is much
faster with numarray, I think I'll stick with mine.

I note that while the numpy way, using mgrid(), is nice and clean to
write, it is slower across the board. Perhaps mgrid(0 could use some
optimization.

This is exactly why I had suggested that one thing I wanted for numpy
was an as-easy-to-use-as-possible C/C++ API. It would be nice to be able
to write as many as possible of these kinds of utility functions in C as
we could.

In case anyone is interested, I'm using this to draw a grid of dots on
the screen for my wxPython FloatCanvas. Every time the image is changed
or moved or zoomed, I need to re-calculate the points before drawing
them, so it's nice to have it fast.

I've enclosed my test code.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
A non-text attachment was scrubbed...
Name: junk.py
Type: text/x-python
Size: 1915 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060602/ca9a3d10/attachment.py
```