I have a non-negative square matrix $M_{ij}$ that denotes weights of a directed graph. Its diagonal is zero (no self-connections). For a node $i$ the (unnormalized) clustering coefficient $C_i$ can be defined as the sum of weights of all the directed triangles that this node is a part of. Given 3 directed edges, the weight of a triangle can be defined as the geometric average of the weights of the edges
$$T_{uv, uw, vw} = \sqrt[3]{M_{uv}M_{uw}M_{vw}}$$
I have written an algorithm to compute this clustering coefficient as follows
$$C_i = \sum_{u=1}^N \sum_{v=u+1}^N \sum_{w=v+1}^N S_{uvw} (\delta_{ui} + \delta_{vi} + \delta_{wi})$$
where $S_{uvw}$ is the sum of weights of 8 directed triangles that can be formed using nodes $u,v,w$
$$ \begin{eqnarray} S_{uvw} &=& T_{uv, uw, vw} \\ &+& T_{uv, uw, wv} \\ &+& T_{uv, wu, vw} \\ &+& T_{uv, wu, wv} \\ &+& T_{vu, uw, vw} \\ &+& T_{vu, uw, wv} \\ &+& T_{vu, wu, vw} \\ &+& T_{vu, wu, wv} \end{eqnarray} $$ The algorithm works and computes exactly what I want. Now I want to make it more readable and optimize the performance of the algorithm. Is there a more compact way to write this sum, but without using implicit objects such as a triangle, perhaps using matrix multiplications?