Exercises#
The first exercise is to use the mass_numbers
and isotopic abundences
array included in the course text to find the average mass number of tin.
import numpy as np
mass_numbers = np.array([112, 114, 115, 116, 117, 118, 119, 120, 122, 124])
isotopic_abundances = np.array([0.0097, 0.0066, 0.0034, 0.1454, 0.0768, 0.2422, 0.0859, 0.3258, 0.0463, 0.0579])
We can find the average mass number in single line with NumPy
np.sum(mass_numbers * isotopic_abundances)
118.80769999999998
The second exercise is returning to the distance evaluations investigated previously.
atom_1 = [0.1, 0.5, 3.2]
atom_2 = [0.4, 0.5, 2.3]
atom_3 = [-0.3, 0.3, 1.7]
We will use the same nested loops arrangement as previously, but harness NumPy to make the mathematics more simple to read/write.
distances = []
atoms = np.array([atom_1, atom_2, atom_3])
for i, a_i in enumerate(atoms):
for a_j in atoms[i+1:]:
distances.append(np.sqrt(np.sum((a_i - a_j) ** 2)))
print(distances)
[0.9486832980505141, 1.5652475842498532, 0.9433981132056602]
The vector operations in NumPy mean the distance evaluation can be achieved without the inner most loop.