# Function of symbols that is drawn from a matrix of symbols - does not work!

Please look at the first cell in this worksheet.

#### The Problem

Wanted to simplify the following symbolic calculation: If S be a matrix, nXn, of symbols - the matrix $A_{ab}=\sum_{c\ne{d}}S_{ac}S_{ad}S_{bd}S_{bc}$.

#### Construction of the matrix of symbols

```
n = 3
R = PolynomialRing(RR,[['r','t'][cmp(int(i/n),i%n)]+'_'+str(1+int(i/n))+str(1+i%n) for i in range(n^2)])
S = matrix(R,3,R.gens())
```

And this works, the matrix is constructed.

#### sanity Check

```
print [S[int(k/n)][k%n] for k in range(n^2)]
```

works perfectly.

#### Roadblock

However this construct does not work:
print [(S[i][int(k/n)]*S[i][k%n]*S[j][k%n]*S[j][int(k/n)])*(int(k/n)!=k%n)for k in range(n^2)]

and returns

```
Traceback (most recent call last):
File "", line 1, in <module>
File "/tmp/tmpQltfcX/___code___.py", line 10, in <module>
print [(S[i][int(k/n)]*S[i][k%n]*S[j][k%n]*S[j][int(k/n)])*(int(k/n)!=k%n)for k in range(n**_sage_const_2 )]
File "matrix0.pyx", line 924, in sage.matrix.matrix0.Matrix.__getitem__ (sage/matrix/matrix0.c:5540)
IndexError: matrix index out of range
```

Consequently the constructor for the matrix $A_{ab}$ doesnt work as well!

#### Check Again

```
print flatten([[[(i,int(k/n),k%n,j,1*(int(k/n)!=k%n))for k in range(n^2)]for i in range(n)]for j in range(n)])
```

returns a list containing 0, 1 and 2 s only. So the IndexError up there does not make sense either!

#### Edit: Addendum

```
n = 3
R = PolynomialRing(RR,[['r','t'][cmp(int(i/n),i%n)]+'_'+str(1+int(i/n))+str(1+i%n) for i in range(n^2)])
S = matrix(R,3,R.gens())
from operator import add
def Sh(i,j):
return reduce(add,[S[i][k//n]*S[i][k%n]*S[j][k%n]*S[j][k//n]*((k//n)!=k%n) for k in range(n^2)])
Shot = matrix(RR,n,n, Sh)
#print Shot
```

sage then returns the following error: Traceback (most recent call last): #print flatten([[[(i,int(k/n),k%n,j,1*(int(k/n)!=k%n))for k in range(n^2)]for i in range(n)]for j in range(n)]) File "", line 1, in <module>

```
File "/tmp/tmpGXJyv5/___code___.py", line 12, in <module>
Shot = matrix(RR,n,n, Sh)
File "/sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/matrix/constructor.py", line 666, in matrix
return matrix_space.MatrixSpace(ring, nrows, ncols, sparse=sparse)(entries)
File "/sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/matrix/matrix_space.py", line 462, in __call__
return self.matrix(entries, copy=copy, coerce=coerce, rows=rows)
File "/sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages ...
```