Give a $N \times N$ matrix $M$ in MMA, I am interested in finding a very particular sum using its matrix elements:
$ \sum_{i \neq j \neq k \neq l}^{N} M_{ij}M_{jk}M_{kl}M_{li}$,
which is of some use in studies in Physics. The only problem is that the calculation quickly becomes very tedious, even with small values of $N$, as the number of terms $\sim N^{4}$.
I am approaching the problem in a very straightforward manner as follows. Another thing to note is that my original matrix is Hermitian (or Symmetric for the real case), so maybe one can try to use the structure, but I am not sure how to do that. We use a random GOE matrix as a minimal example.
(*Defining a matrix using seed for reproducibility*)
n = 5;
SeedRandom[1234];
mat = RandomVariate[GaussianOrthogonalMatrixDistribution[2^n]];
Then I am using ParallelSum
and explicitly defining the constraint on the sum, that none of the indices should be the same, using Boole
(there are 6 in all )
ParallelSum[
mat[[i, j]] mat[[j, k]] mat[[k, l]] mat[[l, i]]
Boole[i =!= j ] Boole[j =!= k ] Boole[k =!= l ] Boole[l =!= i ] Boole[j =!= l ] Boole[k =!= i],
{i, 1, 2^n} , {j, 1, 2^n}, {k, 1, 2^n}, {l, 1, 2^n} ]
(*-767.583*)
I would like to know if there is any way to scale the calculation for large $N$. It is already very time-consuming for me to run anything more than $2^6$. For eg for $2^7$ takes around 10 minutes for me, and the time quickly escalates after that. My actual matrices are of the size $2^{16}$ for which I will use a powerful workstation, but still the calculation scales badly with $N$, and it would be useless before doing any further optimisation.
Another solution I tried was using Map
to map all possible indices (pre-generated), but I quickly ran out of memory when I pre-generated all the possible indices.
I tried using ParallelTable
to generate the list and do the sum, but it is slow as well.
Any help or insight on this is appreciated.