# Substituting for a matrix of variables

 0 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][0]: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][0]:0})  asked Apr 15 '12 d3banjan 73 ● 1 ● 7 DSM 4882 ● 12 ● 65 ● 105 If you click on the "edit" button you'll see there are HTML tags strewn about. If you clean it up and space things it should work.DSM (Apr 15 '12)@DSM - it actually looks worse now! but i have taken out the tags!d3banjan (Apr 15 '12)1".. and space things". You need linebreaks too.DSM (Apr 15 '12)

## 2 Answers:

 1 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)])  hard to read. I'd write 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]: 0}) [ 0 t_21 t_31] [t_12 r_22 t_32] [t_13 t_23 r_33]  posted Apr 15 '12 DSM 4882 ● 12 ● 65 ● 105 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. d3banjan (Apr 15 '12)@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?d3banjan (Apr 15 '12)I'm not sure what you mean. Calling .subs this way does apply the substitution to each element of the matrix.DSM (Apr 15 '12)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!d3banjan (Apr 15 '12)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?DSM (Apr 15 '12) see 3 more comments
 0 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[1],dSdl,n) # in the words of apache2 - yay! it works!!  @DSM - thanks for all that help! posted Apr 17 '12 d3banjan 73 ● 1 ● 7 '//' messes up the syntax highlighting!d3banjan (Apr 17 '12)

## Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
[hide preview]

## Stats:

Asked: Apr 15 '12

Seen: 141 times

Last updated: Apr 17 '12

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.