Someone writes a function to compute the inertia tensor of a cluster of point masses…
PHYSICS · Multi-part · Fermi University Physics · Mathematical Methods for Physics · Computational Physics · Scientific programming for physicists
Scientific Computing for Physicists, Ch Arrays in memory: layout, cost and the data structures of physical problems
Problem
Someone writes a function to compute the inertia tensor of a cluster of n point masses about its centre of mass, from a position array r of shape (n,3) and a mass array m of shape (n,): def centred_positions(positions, m): return positions - np.average(positions, axis=0, weights=m) def inertia_tensor(r, m): r2 = np.einsum('ij,ij->i', r, r) return np.einsum('i,jk->jk', m * r2, np.eye(3)) \ - np.einsum('i,ji,ik->jk', m, r, r) They call r = centred_positions(positions, m) and then inertia_tensor(r, m) on the positions of the thirteen-atom icosahedral cluster of unit mass (n=13), whose inertia tensor is known to be isotropic, Ijk=Iδjk.
((a)) Is r returned by centred_positions a view or a copy of positions? Justify the answer from how NumPy evaluates positions - np.average(positions, axis=0, weights=m).
((b)) Calling inertia_tensor(r, m) raises a ValueError about operands that could not be broadcast together, before any isotropy check is possible. Identify the error in the einsum labels responsible, explain in words of index sizes why it causes this failure, and give the corrected line.