Array computations

Arithmetic operations can be used with arrays:
b = 3*a - 1    # a is array, b becomes array
1) compute t1 = 3*a, 2) compute t2= t1 - 1, 3) set b = t2
Array operations are much faster than element-wise operations:
>>> import time  # module for measuring CPU time
>>> a = linspace(0, 1, 1E+07)  # create some array
>>> t0 = time.clock()
>>> b = 3*a -1
>>> t1 = time.clock()   # t1-t0 is the CPU time of 3*a-1

>>> for i in xrange(a.size): b[i] = 3*a[i] - 1
>>> t2 = time.clock()
>>> print '3*a-1: %g sec, loop: %g sec' % (t1-t0, t2-t1)
3*a-1: 2.09 sec, loop: 31.27 sec

previousnexttable of contents