19px
CCPP-I · Class 02

NumPy

J/K or →/← jump between sections

Arrays: numbers that do arithmetic

An array holds numbers of one type. An operation applies to every element. Click an operation.

$ conda activate pytccm
$ conda install -c conda-forge numpy
import numpy as np

py_list = [1, 2, 3]
np_array = np.array(py_list)

Every array knows its shape and type

np_array.shape, np_array.dtype, len(np_array)
((3,), dtype('int64'), 3)

np.sqrt(np_array), np.exp, np.sin… also work element by element.

Creating arrays

arange takes a step and stops before the end, like range. linspace takes a number of points and includes the end.

Filled arrays: the shape is one argument, a tuple

Shape: the same numbers, arranged differently

reshape fills rows left to right, top to bottom. -1 means "work it out". Colour follows the value.

vec = np.arange(12)
mat = vec.reshape(3, 4)

mat.flatten() gives back the 12 numbers in one row.

A third axis: a stack of matrices

np.arange(24).reshape(2, 3, 4)

Indexing: mat[row, column]

One index or slice per axis, separated by a comma. : alone means "all".

A comparison gives a boolean array; a boolean array selects

The same on a matrix. The selected elements always come out as a 1-D array.

Reductions along an axis

axis=0 collapses the rows: one result per column. axis=1 collapses the columns: one per row. Hover a result.

mat.std(axis=1), mat.min() work the same way.

Distance between two atoms

atom1 = np.array([0.0, 0.0, 0.0])
atom2 = np.array([1.0, 1.0, 0.0])
np.sqrt(np.sum((atom1 - atom2) ** 2)), np.linalg.norm(atom1 - atom2)
(np.float64(1.4142135623730951), np.float64(1.4142135623730951))
⏸

Exercise 1: Arrays

Calculate the average of all integers below 100 that are divisible by 8, in three ways:

  1. np.arange with a step, and mean
  2. np.arange(1, 97).reshape(12, 8) and one column of it
  3. a boolean mask with % on np.arange(1, 100)
52.0
52.0
52.0

* is element by element, @ is the matrix product

Hover a result element of mat @ vec: a row of the matrix times the vector, summed.

a = np.array([1, 2, 3])
a * a, a @ a
(array([1, 4, 9]), np.int64(14))
mat = np.arange(6).reshape(2, 3)
vec = np.array([1, 0, 1])
mat @ vec
array([2, 8])

The inner sizes must agree

mat @ np.array([1, 0])
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 3)

Inverse and identity

m = np.array([[1, 2], [3, 4]])
m_inv = np.linalg.inv(m)
m @ m_inv
array([[1.0000000e+00, 0.0000000e+00], [8.8817842e-16, 1.0000000e+00]])
np.allclose(m @ m_inv, np.identity(2))
True

allclose: equal up to a tiny tolerance.

Reading and writing files

array.dat holds the pyridine coordinates, one atom per line, x y z in Å.

array.dat -0.180226841 0.360945118 -1.120304970 -0.180226841 1.559292118 -0.407860970 -0.180226841 1.503191118 0.986935030 … 11 lines
xyz = np.loadtxt('array.dat')
xyz.shape
(11, 3)
xyz[:3]
array([[-0.18022684, 0.36094512, -1.12030497], [-0.18022684, 1.55929212, -0.40786097], [-0.18022684, 1.50319112, 0.98693503]])
np.savetxt('xyz_copy.dat', xyz, fmt='%10.4f', header='x y z')
xyz_copy.dat# x y z -0.1802 0.3609 -1.1203 -0.1802 1.5593 -0.4079 …

fmt='%10.4f': width 10, 4 decimals, as {x:10.4f} in an f-string.

⏸

Exercise 2: Reading data

Load array.dat with np.loadtxt. Print its shape, the geometric centre (mean along the right axis) and the distance between atoms 0 and 1 (np.linalg.norm).

(11, 3)
[-0.18022684  0.36094512  0.03922925]
1.3941348519942394

Broadcasting: arrays of different shapes

Shapes are compared from the right. Two sizes fit if they are equal or one of them is 1; a size 1 is stretched by copying.

sizes equalsize 1, stretchedmissing axis, counts as 1copied cell

None in an index adds an axis of length 1

d = np.arange(4)
d[:, None].shape
(4, 1)

Three axes: the same rule

tensor = np.arange(24).reshape(2, 4, 3)
(tensor + vec).shape
(2, 4, 3)

The distance matrix in one line

Every atom minus every atom. Row i repeats atom i, column j repeats atom j. Hover a cell.

(xyz[:, None, :] - xyz).shape
distances = np.linalg.norm(xyz[:, None, :] - xyz, axis=-1)
(11, 11, 3)
pyridine, y–z plane
below 1.6 Å: a bond
⏸

Exercises 3–5: Distances

3. Distance matrix

Build the 11 × 11 matrix of all interatomic distances with two nested for loops (enumerate) filling np.zeros((11, 11)). Check with np.allclose that it agrees with the broadcasting one-liner.

True

4. Counting bonds

Two atoms are bonded if their distance is below 1.6 Å. How many bonds does pyridine have? Count each pair once and ignore the diagonal: np.triu with k=1.

11

5. Writing data

Save the distance matrix to distances.dat with two decimals (np.savetxt, fmt), load it back and print the shape.

(11, 11)

Speed

A Python loop handles one number at a time. An array operation runs the whole loop in compiled C.

%timeit sum([x**2 for x in range(1_000_000)])
%timeit (np.arange(1_000_000)**2).sum()

%timeit is a Jupyter command: it runs the line many times and reports the average.

Text columns: reading an xyz file

dtype=str reads everything as text. Take the columns apart, then convert the numbers. Hover the code.

raw = np.loadtxt('pyridine.xyz', skiprows=2, dtype=str)
elements = raw[:, 0]
coordinates = raw[:, 1:].astype(float)
elements, coordinates.mean(axis=0)
(array(['C', 'C', 'C', 'N', 'C', 'C', 'H', 'H', 'H', 'H', 'H'], dtype='<U12'), array([-0.18022684, 0.36094512, 0.03922925]))
pyridine.xyz11 C -0.180226841 0.360945118 -1.120304970 C -0.180226841 1.559292118 -0.407860970 C -0.180226841 1.503191118 0.986935030 N -0.180226841 0.360945118 1.29018350 …

The same reader works for any xyz file: the homework and the plotting class use it.