Skip to content Skip to sidebar Skip to footer

Broadcasting/vectorizing Inner And Outer For Loops In Python/numpy

Purpose I have turned a double for loop into a single for loop using vectorization. I would like to now get rid of the last loop. I want to slice an Nx3 array of coordinates and ca

Solution 1:

Here how you can do it:

R = ri[:,None] - rj[None, :]
R = R - np.rint(R/box)*box
R_sq = np.sum(np.square(R), axis=2)

energy = np.sum(4 * ((1/R_sq)**12 - (1/R_sq)**6))

Post a Comment for "Broadcasting/vectorizing Inner And Outer For Loops In Python/numpy"