Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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]

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.