# Substituting for a matrix of variables 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)


When i tried to substitute one of the variables with zero, it did not work.

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())
f = matrix([[S[i][j] for i in range(n)] for j in range(n)])
f({S:0})


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

edit retag close merge delete

Sort by » oldest newest most voted A few things. First, I find this

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


sage: n = 3
sage: [('r' if i==j else 't') +'_%d%d' % (i,j) for i in [1..n] for j in [1..n]]
['r_11', 't_12', 't_13', 't_21', 'r_22', 't_23', 't_31', 't_32', 'r_33']


instead (or maybe use a CartesianProduct, but with only two it's a toss-up.) I'm not sure what the line

f = matrix([[S[i][j] for i in range(n)] for j in range(n)])


is intended to do. If you simply want the transpose, use .transpose:

sage: S
[r_11 t_12 t_13]
[t_21 r_22 t_23]
[t_31 t_32 r_33]
sage: f0 = matrix([[S[i][j] for i in range(n)] for j in range(n)])
sage: f0
[r_11 t_21 t_31]
[t_12 r_22 t_32]
[t_13 t_23 r_33]
sage: f1 = S.transpose()
sage: f1
[r_11 t_21 t_31]
[t_12 r_22 t_32]
[t_13 t_23 r_33]
sage: f0 == f1
True


To substitute into the matrix, I'd call .subs, so I think my version of your code would look like

sage: n = 3
sage: vnames = [('r' if i==j else 't') +'_%d%d' % (i,j) for i in [1..n] for j in [1..n]]
sage: R = PolynomialRing(RR, vnames)
sage: S = matrix(R, 3, R.gens())
sage: f = S.transpose()
sage: f.subs({S: 0})
[   0 t_21 t_31]
[t_12 r_22 t_32]
[t_13 t_23 r_33]

more

wow! that works!! i personally used the 1*(i==j) version of things because i discovered it on my own one day - and loved how short and LISPy the expression looked. Obviously your way is a lot better.

@DSM - Is there a way to use subs() on the entire matrix? I do understand that forming the dict is a *ONE* way - but is there a shorter expression?

I'm not sure what you mean. Calling .subs this way *does* apply the substitution to each element of the matrix.

i mean something like f.subs({S:matrix([[0,1],[1,0]])}), where S is a $2\times 2$ matrix. Please note the word *like*!

Unfortunately I'm still not sure what you want. f is a three-by-three matrix with entries in R. What would applying a substitution to f from one two-by-two matrix to another two-by-two matrix mean?

def MatrixSubstitutor(SymbolicMatrix,ValueMatrix, Expression,n):
#create dict
SubstitutionDict = dict([(SymbolicMatrix[p//n][p%n],ValueMatrix[p//n][p%n]) for p in range(n^2)])
return Expression.subs(SubstitutionDict) #yay
#for example,
print MatrixSubstitutor(S,FPList,dSdl,n)
# in the words of apache2 - yay! it works!!


@DSM - thanks for all that help!

more