Ask Your Question
0

Substituting for a matrix of variables

asked 2012-04-15 12:39:09 +0100

d3banjan gravatar image

updated 2012-04-15 14:39:02 +0100

DSM gravatar image

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})
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2012-04-15 13:43:26 +0100

DSM gravatar image

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]
edit flag offensive delete link more

Comments

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 gravatar imaged3banjan ( 2012-04-15 14:42:01 +0100 )edit

@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 gravatar imaged3banjan ( 2012-04-15 14:44:13 +0100 )edit

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

DSM gravatar imageDSM ( 2012-04-15 14:49:23 +0100 )edit

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 gravatar imaged3banjan ( 2012-04-15 15:11:26 +0100 )edit

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 gravatar imageDSM ( 2012-04-15 16:02:50 +0100 )edit
0

answered 2012-04-17 05:29:54 +0100

d3banjan gravatar image
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!

edit flag offensive delete link more

Comments

'//' messes up the syntax highlighting!

d3banjan gravatar imaged3banjan ( 2012-04-17 05:31:44 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-04-15 12:39:09 +0100

Seen: 1,815 times

Last updated: Apr 17 '12