Vectorization (1)

Loops over an array run slowly
Vectorization = replace explicit loops by functions calls such that the whole loop is implemented in C (or Fortran)
Explicit loops:
r = zeros(x.shape, x.dtype)
for i in xrange(x.size):
    r[i] = sin(x[i])
Vectorized version:
r = sin(x)
Arithmetic expressions work for both scalars and arrays
Many fundamental functions work for scalars and arrays
Ex: x**2 + abs(x) works for x scalar or array

previousnexttable of contents