[SciPy-user] [newb] how to create arrays

Anne Archibald peridot.faceted@gmail....
Thu Jan 3 02:57:02 CST 2008

On 03/01/2008, Bill Baxter <wbaxter@gmail.com> wrote:
> On Jan 3, 2008 4:56 PM, Anne Archibald <peridot.faceted@gmail.com> wrote:
> > The main reason for this is conceptual clarity: you can start thinking
> > of array operations as single steps in your program, allowing you to
> > do more with the same size of program. Secondarily, using the numpy
> > functions allows the looping to be done in compiled code, which is
> > much faster than python code. If you are filling an array, element by
> > element, with a loop in python, the time the python code takes to run
> > will be much longer than the time spend initializing with zeros. Thus
> > numpy.empty() gets much less use than you might expect. Look into
> > using numpy functions - linspace, arange, zeros, ones,
> > exp/sin/arctan/etc. to create your array.
> That's interesting.  I'd put it almost exactly backwards.  Efficiency
> is the #1 reason for arrays, and as a bonus it happens that *some*
> things are shorter and cleaner expressed as array operations.  I've
> been back to using a compiled language for a stint and it's absolute
> bliss to just be able to write a blasted for-loop when I want to,
> without worrying about my program slowing to a crawl.  As opposed to
> always having to think of some clever way to use the array machinery
> to achieve the same thing efficiently (and sometimes having to think
> of two or three different ways when the first one doesn't turn out to
> be fast enough).

I think I differ in that rather than sweat over how to express
something in an arrayish way, I'm perfectly willing to write a for
loop in python (or use vectorize, which is equivalent). Sure it's
slow, comparatively, but I want to get the code written first, and
worry about fast-enough later.

> But I still fire up matplotlib and hack up some quick NumPy scripts
> whenever I want to take a look at the numbers coming out of my
> compiled code. :-)

Exactly what I mean. Except much of the code I write is closer to this
- in the sense that it only ever needs to run to completion once -
than it is to the problems of classical computer science (sweat blood
finding the most efficient solution because it needs to run a zillion


More information about the SciPy-user mailing list