next up previous
Next: About this document ... Up: A sampling of useful Previous: Filling a vector with

Functions acting on a numpy vector

Numpy mathematical operations on an array are done element by element. So we can construct a cosine wave with frequency 1/8 by discretely sampling $\cos(2\pi t/8)$ using values in the array t above:

>>> print(np.cos(2*np.pi*t/8))
[  1.00000000e+00   7.07106781e-01   6.12323400e-17  -7.07106781e-01
  -1.00000000e+00  -7.07106781e-01  -1.83697020e-16   7.07106781e-01]

Numpy has applied the same operation to each of the elements of the array t and created and printed a new array with the results in the same order. Notice that we must use the numpy version of cosine np.cos. Also, numpy gives us the value of $\pi$ as np.pi, so we don't need to spell it out.

Carleton DeTar 2018-02-12