NumPy Module
Numpy is, besides SciPy, the core library for scientific computing in Python. It provides a high-performance multidimensional array object and tools for working with these arrays. The NumPy array, formally called ndarray in NumPy documentation, is the real workhorse of data structures for scientific and engineering applications. The NumPy array is similar to a list but where all the elements of the list are of the same type. The elements of a NumPy array are usually numbers, but can also be booleans, strings, or other objects. When the elements are numbers, they must all be of the same type.
For physics applications, NumPy is essential because it enables efficient numerical calculations on large datasets, handling of vectors and matrices, and implementation of mathematical models that describe physical phenomena. Whether simulating particle motion, analyzing experimental data, or solving equations of motion, NumPy provides the computational foundation needed for modern physics.
Creating Numpy Arrays
There are a number of ways to initialize new numpy arrays, for example from
- a Python list or tuples
- using functions that are dedicated to generating numpy arrays, such as
arange,linspace, etc. - reading data from files which will be covered in the files section
For example, to create new vector and matrix arrays from Python lists we can use the numpy.array function.
For larger arrays it is impractical to initialize the data manually, using explicit python lists. Instead we can use one of the many functions in numpy that generate arrays of different forms. Some of the more common are:
linspace and logspace
The linspace function creates an array of N evenly spaced points between a starting point and an ending point. The form of the function is linspace(start, stop, N).If the third argument N is omitted,then N=50.
logspace is doing equivalent things with logarithmic spacing. Other types of array creation techniques are listed below. Try around with these commands to get a feeling what they do.
mgrid
mgrid generates a multi-dimensional matrix with increasing value entries, for example in columns and rows:
diag
diag generates a diagonal matrix with the list supplied to it. The values can be also offset from the main diagonal.
zeros and ones
zeros and ones creates a matrix with the dimensions given in the argument and filled with 0 or 1.
Array Attributes
NumPy arrays have several attributes that provide information about their size, shape, and data type. These attributes are essential for understanding and debugging your code.
The shape attribute returns a tuple that gives the size of the array along each dimension.
The size attribute returns the total number of elements in the array.
The dtype attribute returns the data type of the array’s elements.
These attributes are particularly useful when debugging operations between arrays, as many NumPy functions require arrays of specific shapes or compatible data types.
Manipulating NumPy arrays
Slicing is the name for extracting part of an array by the syntax M[lower:upper:step]
Any of the three parameters in M[lower:upper:step] can be ommited.
Negative indices counts from the end of the array (positive index from the begining):
Index slicing works exactly the same way for multidimensional arrays:
Slicing can be effectively used to calculate differences for example for the calculation of derivatives. Here the position \(y_i\) of an object has been measured at times \(t_i\) and stored in an array each. We wish to calculate the average velocity at the times \(t_{i}\) from the arrays by
\[\begin{equation} v_{i}=\frac{y_i-y_{i-1}}{t_{i}-t_{i-1}} \end{equation}\]
Arrays can be reshaped into any form, which contains the same number of elements.
With newaxis, we can insert new dimensions in an array, for example converting a vector to a column or row matrix.
Using function repeat, tile, vstack, hstack, and concatenate we can create larger vectors and matrices from smaller ones. Please try the individual functions yourself in your notebook. We wont discuss them in detail.
Tile and repeat
Concatenate
Hstack and vstack
Applying mathematical functions
All kinds of mathematical operations can be carried out on arrays. Typically these operation act element wise as seen from the examples below.
Vector operations enable efficient element-wise calculations where corresponding elements at matching positions are processed simultaneously. Instead of handling elements one by one, these operations work on entire arrays at once, making them particularly fast. When multiplying two vectors using these operations, the result is not a single number (as in a dot product) but rather a new array where each element is the product of the corresponding elements from the input vectors. This element-wise multiplication is just one example of vector operations, which can include addition, subtraction, and other mathematical functions.
NumPy provides powerful tools for generating random numbers, which are essential for simulations in statistical physics, quantum mechanics, and other fields:
These random number generators are particularly useful for Monte Carlo simulations, modeling thermal noise, or simulating quantum mechanical systems.
Broadcasting
Broadcasting is a powerful mechanism that allows NumPy to work with arrays of different shapes when performing arithmetic operations. The smaller array is “broadcast” across the larger array so that they have compatible shapes.
The rules for broadcasting are:
- If the arrays don’t have the same rank, prepend the shape of the lower rank array with 1s until both shapes have the same length.
- The size in each dimension of the output shape is the maximum of the sizes of the input arrays along that dimension.
- An input can be used in the calculation if its size in a particular dimension matches the output size or if its value is exactly 1.
- If an input has a dimension size of 1, the first element is used for all calculations along that dimension.
Let’s see some examples:
Broadcasting enables efficient computation without the need to create copies of arrays, saving memory and computation time.
Physics Example: Force Calculations
Broadcasting is particularly useful in physics when applying the same operation to multiple objects. For example, when calculating the gravitational force between one massive object and multiple other objects using Newton’s law of universal gravitation:
\[\begin{equation} F = \frac{G M m}{r^2} \end{equation}\]
where \(F\) is the gravitational force, \(G\) is the gravitational constant, \(M\) and \(m\) are the masses of the two objects, and \(r\) is the distance between them.