I have a scalar function f(a,b,c,d)
that has the following permutational symmetry
f(a,b,c,d) = f(c,d,a,b) = -f(b,a,d,c) = -f(d,c,b,a)
I'm using it to fully populate a 4D array. This code (using python/NumPy) below works:
A = np.zeros((N,N,N,N))
for a in range(N):
for b in range(N):
for c in range(N):
for d in range(N):
A[a,b,c,d] = f(a,b,c,d)
But obviously I'd like to exploit symmetry to cut down on the execution time of this section of code. I've tried:
A = np.zeros((N,N,N,N))
ab = 0
for a in range(N):
for b in range(N):
ab += 1
cd = 0
for c in range(N):
for d in range(N):
cd += 1
if ab >= cd:
A[a,b,c,d] = A[c,d,a,b] = f(a,b,c,d)
Which cuts the execution time in half. But for the last symmetry I tried:
A = np.zeros((N,N,N,N))
ab = 0
for a in range(N):
for b in range(N):
ab += 1
cd = 0
for c in range(N):
for d in range(N):
cd += 1
if ab >= cd:
if ((a >= b) or (c >= d)):
A[a,b,c,d] = A[c,d,a,b] = f(a,b,c,d)
A[b,a,d,c] = A[d,c,b,a] = -A[a,b,c,d]
Which works, but doesn't give me near another factor of two speed-up. I don't think it is right for the right reasons, but can't see why.
How can I better exploit this particular permutational symmetry here?