## python : array broadcasting

June 15, 2013 by micropore

It is a common practice to use loops in programming. Now if you have a lot of big loops, you find python rather slow. A very efficient way is to use the so-called array broadcasting. For an introduction, see this page for example:

http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html

The basic concept is to use column and row vectors to compute all intermediate values at the same time resulting in an 2d-array with all results.This way, you can get rid of almost all ordinary Python loops over arrays. The actual loops will be executed by highly optimized C routines instead, with only a small overhead for calling it from inside the Python interpreter. So this should be almost as fast as plain C or Fortran code.

**Example:** I would like to evaluate the Planck function for *N* test data sets, each of them cost four times evaluation of the function (for four wavelengths). In an elementary program, one can do it one by one for N times. This costs about 1.831 seconds runtime. Now if I use array broadcasting technique, I can do it within 0.013 seconds, or some 141 times faster !!!Â The main trick is shown in the following example:

**ordinary loop: This loop should be called N times.**

for i in arange(0,num):

param = fplk(frqs[i], temperature)

**vectorized loop using array broadcasting: one single run, no loop at all.**

frqs_row = frqs[numpy.newaxis]

temp = temperature[:, numpy.newaxis]

output = fplk(frqs_row, temp)

note that the fplk function is identical !

### Like this:

Like Loading...

*Related*

## Leave a Reply