Ask Your Question
0

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

asked 2012-04-10 03:06:40 +0200

d3banjan gravatar image

updated 2012-04-10 11:55:07 +0200

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

1 Answer

Sort by » oldest newest most voted
1

answered 2012-04-10 10:44:11 +0200

DSM gravatar image

updated 2012-04-10 12:02:15 +0200

The IndexError makes perfect sense:

sage: n = 3
sage: 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: S = matrix(R,3,R.gens())
sage: 
sage: [(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)]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
[...]
IndexError: matrix index out of range

in this line you use both i and j but don't define them. So i is still what it was left at the end of the R list comprehension, and j is undefined:

sage: i
8
sage: j
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
[...]
NameError: name 'j' is not defined

So even if i happened to be a valid index, the lack of j would mean the line wouldn't work.

Incidentally, rather than int(k/n) you can simply use k//n for truncating division:

sage: int(5/2)     
2
sage: parent(_)    
<type 'int'>
sage: 5//2
2
sage: parent(_)
Integer Ring

which has the advantage of staying a Sage Integer. (Not so relevant here, I admit, where the result is being used as an index immediately and then discarded, but it's handy elsewhere.)

Edit:

As for your updated question:

Shot = matrix(RR,n,n, Sh)

tries to make an n x n matrix over a 53-bit real field, but the entries live somewhere else:

sage: Sh(0,0)
2.00000000000000*r_11^2*t_12^2 + 2.00000000000000*r_11^2*t_13^2 + 2.00000000000000*t_12^2*t_13^2
sage: parent(Sh(0,0))
Multivariate Polynomial Ring in r_11, t_12, t_13, t_21, r_22, t_23, t_31, t_32, r_33 over Real Field with 53 bits of precision

i.e. in R, not in RR:

sage: Shot = matrix(R,n,n, Sh)
sage: Shot
[                  2.00000000000000*r_11^2*t_12^2 + 2.00000000000000*r_11^2*t_13^2 + 2.00000000000000*t_12^2*t_13^2 2.00000000000000*r_11*t_12*t_21*r_22 + 2.00000000000000*r_11*t_13*t_21*t_23 + 2.00000000000000*t_12*t_13*r_22*t_23 2.00000000000000*r_11*t_12*t_31*t_32 + 2.00000000000000*r_11*t_13*t_31*r_33 + 2.00000000000000*t_12*t_13*t_32*r_33]
[2.00000000000000*r_11*t_12*t_21*r_22 + 2.00000000000000*r_11*t_13*t_21*t_23 + 2.00000000000000*t_12*t_13*r_22*t_23                   2.00000000000000*t_21^2*r_22^2 + 2.00000000000000*t_21^2*t_23^2 + 2.00000000000000*r_22^2*t_23^2 2.00000000000000*t_21*r_22*t_31*t_32 + 2.00000000000000*t_21*t_23*t_31*r_33 + 2.00000000000000*r_22*t_23*t_32*r_33]
[2.00000000000000*r_11*t_12*t_31*t_32 + 2.00000000000000*r_11*t_13*t_31*r_33 + 2.00000000000000*t_12*t_13*t_32*r_33 2.00000000000000*t_21*r_22*t_31*t_32 + 2.00000000000000*t_21*t_23*t_31*r_33 + 2.00000000000000*r_22*t_23*t_32*r_33                   2.00000000000000*t_31^2*t_32^2 + 2.00000000000000*t_31^2*r_33^2 + 2.00000000000000*t_32^2*r_33^2]

It can take a while to get used to Sage's structures, but eventually it all starts to make sense.

edit flag offensive delete link more

Comments

*awkward silence* well! thank you for clearing this up! seriously, a lot! By the way could you tell me what parent(_) does? i am guessing that _ is the equivalent of last output?

d3banjan gravatar imaged3banjan ( 2012-04-10 11:44:13 +0200 )edit
2

Good guess! _ is the last result (in an interactive console, not during non-interactive execution). And if you set _ to something, like some people do (sometimes you'll see "for _ in range(5)" where _ is used to mean 'some dummy index I don't want to give a name to) this won't work. parent by itself returns where the argument "lives", so to speak.

DSM gravatar imageDSM ( 2012-04-10 11:49:59 +0200 )edit

I will revise the question a bit, since it still does not work. Please answer to it!

d3banjan gravatar imaged3banjan ( 2012-04-10 11:50:29 +0200 )edit

@DSM - thank you SO much. i used the constructors straight from the examples in the documentation - decidedly a wrong move! want you to know, sir, that you are an excellent explainer (i stick with the choice of term!).

d3banjan gravatar imaged3banjan ( 2012-04-11 15:12:49 +0200 )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

1 follower

Stats

Asked: 2012-04-10 03:06:40 +0200

Seen: 471 times

Last updated: Apr 10 '12