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 nn point masses about its centre of mass, from a position array rr of shape (n,3)(n,3) and a mass array mm of shape (n,)(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=13n=13), whose inertia tensor is known to be isotropic, Ijk=IδjkI_{jk} = I\delta_{jk}. ((a)) Is rr 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.

Browse the problem library