Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Changing symmetric elements in a matrix

Hello, I hope you are doing fine. I have the matrix below:

def p(*t):
   return SR.var(('p' + '_{}'*len(t)).format(*[str(i) for i in t]))
S = FiniteEnumeratedSet([1,2]) 
P = cartesian_product([S]*2)
M = {}
for (row, (i,j)) in enumerate(P):
   for (col, (k,l)) in enumerate(P):
      if i<=j:
         M[(row, col)] = p(i,j,k,l)
      elif i!=j:
         M[(row, col)] = p(i,j,k,l)
m = matrix(M)

This return the matrix:

m
[p_1_1_1_1 p_1_1_1_2 p_1_1_2_1 p_1_1_2_2]
[p_1_2_1_1 p_1_2_1_2 p_1_2_2_1 p_1_2_2_2]
[p_2_1_1_1 p_2_1_1_2 p_2_1_2_1 p_2_1_2_2]
[p_2_2_1_1 p_2_2_1_2 p_2_2_2_1 p_2_2_2_2]

I would like to apply the symmetry property that I have in the system which guarantees p(i,j,k,l) = p(j,i,l,k). In this case, M = {1,2} it is easy to do manually, but if M = {1,2,3}, for instance, it is much more complicated.

So basically, I would like to exchange the elements in such a way that:

m
[p_1_1_1_1 p_1_1_1_2 p_1_1_1_2 p_1_1_2_2]
[p_1_2_1_1 p_1_2_1_2 p_1_2_2_1 p_1_2_2_2]
[p_1_2_1_1 p_1_2_1_2 p_1_2_1_2 p_1_2_2_2]
[p_2_2_1_1 p_2_2_1_2 p_2_2_1_2 p_2_2_2_2]

Any suggestions?