I constructed a matrix of variables in the following way:

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())

show(S)

Interestingly though, the following snippet, decidedly the same to me, works with a similar syntax -

xx = [var('alpha_%d'% (i+1)) for i in range(n)] 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()) def kron(i,j): if i==j: return 1 else: return 0 def func(S,xx): return [[(-1/2)*xx[i]*S[i][j]*kron(i,j) for i in range(n)]for j in range(n)] f = matrix(func(S,xx)) diff_S = S*f.conjugate_transpose()*S - f print diff_S({S[0][0]:0})

Can someone please edit this so as the code markdown shows! I tried, but a left indent of 4 doesnt work for me anymore!