Computational Methods in Physics ASU Physics PHY 494

03 Introduction to NumPy

We saw in the Introduction to Python that the Python language has the control and data structures to do numerical calculations. For instance, a vector denoting the position of a particle could be represented as a Python list r = [x, y, z] and a matrix as a list of lists sigma_y = [[0, -1j], [1j, 0]].

When it comes to doing numerical work, Python by itself is rather slow. By slow we mean compared to languages like C and Fortran, which benefit from being compiled languages in which a program is preprocessed into machine code by a compiler. Python by contrast is an interpreted language, in which each line in a program is fed to the Python interpreter in sequence, then executed. The flexiblity and ease of use that come with Python come at the cost of pure performance.

However, though Python code itself may be slow, Python can be used to run code that is written in a compiled language and already compiled. We will use a library (a.k.a., a Python module) that does exactly this underneath the hood to get fast performance for numerical operations on arrays: We load the NumPy module:

import numpy

Class material

The class will be live-coded in a Jupyter notebook. The annotated notebook is available as 03-intro-numpy.ipynb.