I was wondering whether its possible to use matrices to calculate the gravitational potential energy of a system. I know it's possible to calculate the acceleration due to gravity on a system by matrices but was wondering if it was possible to implement it with the potential. For gravity, the implementation is as follows, if the position is split into x, y, and z. Then the transpose of those respective elements subtracted by the original matrix finds the distances between all the points. Then if dx, dy and dz are squared
$$ x = \begin{pmatrix} x_1\\ x_2\\ x_3 \end{pmatrix} $$ where x in this case holds all the positions of the respective objects. The numbers indicating different objects. Then $$ dx = x.T - x = \begin{pmatrix} x_1 & x_2 & x_3\\ \end{pmatrix} - \begin{pmatrix} x_1\\ x_2\\ x_3 \end{pmatrix} = \begin{pmatrix} 0 & x_1-x_2 & x_1-x_3\\ x_2-x_1 & 0 & x_2-x_3 \\ x_3-x_1 & x_3-x_2 & 0 \end{pmatrix} $$
The same is true for the y and z axis like the x. The following matrix stores 1/r^3 for all particle pairwise particle separations and raises it to the correct power.
$$ inverse = (dx^2 + dx^2 + dz^2)^{-\frac{3}{2}} $$ therefore leading to
ax = G * (dx * inv_r3)(objects_mass) - sorry didn't know how to nicely display this.
The main question is now is this type of matrix manipulation relevant for Gravitational potential. The equation for which is
$$ U(r) = \frac{Gm_1m_2}{r} $$ This varies from the one of gravity but with the main difference of multiplying by the other mass. So I was wondering if something like the following would be possible. $$ m = \begin{pmatrix} m_1\\ m_2\\ m_3 \end{pmatrix} $$ where m contains all the masses $$ m_m = (m) (m.T) $$ Thereby getting a multiplication of all the masses to each other and then the following
$$ (G\cdot m_m)*(dx^2 + dx^2 + dz^2)^{-1} $$
Is something like this mathematically correct and if not can something similar be done? Thanks in advance