I want to evaluate a sum in Mathematica of the form
Sum[g[[i,j,k,l,m,n]] x g[[o,p,q,r,s,t]] x (complicated function of the indices i,j etc), {i,0,3}, {j,0,3}, {k,0,3}, {l,0,3}, {m,0,3}, {n,0,3}, {o,0,3}, {p,0,3}, {q,0,3}, {r,0,3}, {s,0,3}, {t,0,3}]
But all these indices range from 0 to 3, so the total number of cases to sum over is 4^12, which will take an unforgiving amount of time. However, barely any elements of the array g[[i,j,k,l,m,n]] are nonzero -- there are probably around 8 nonzero entries -- so I would like to restrict the sum over {i,j,k,l,m,n,o,p,q,r,s,t} to precisely those combinations of indices for which both factors of g are nonzero.
I can't find a way to do this for summation over multiple indices, where the allowed index choices are particular combinations of {i,j,k,l,m,n} as opposed to specific values of each particular index. Any help appreciated!


"NonzeroPositions"? See also: What are SparseArray Properties? How and when should they be used? – kglr Mar 04 '22 at 23:04"ExplicitPositions". https://reference.wolfram.com/language/ref/SparseArray.html#1948657 – Greg Hurst Mar 04 '22 at 23:37g[[i,j,k,l,m,n]] x g[[o,p,q,r,s,t]] x ...did you meang[[i,j,k,l,m,n]] + g[[o,p,q,r,s,t]] + ...? – kglr Mar 04 '22 at 23:45g = SparseArray[g]; nzp = s["NonzeroPositions"]; Sum[s[[## & @@ i]] s[[## & @@ j]] FOO[i, j], {i, nzp}, {j, nzp}]– kglr Mar 04 '22 at 23:57Total[s[[## & @@ #]] s[[## & @@ #2]] FOO[##] & @@@ Tuples[nzp, 2]]should give the same result. – kglr Mar 05 '22 at 00:06