Using elements from matrix in equation?

57 views
Skip to first unread message

saad khalid

unread,
Jan 1, 2016, 5:41:25 PM1/1/16
to sage-support
Hey everyone:

For what I'm working on, I'm trying to take transform two variables over a matrix A, and then have the transformed variables plug into a function.




reset()
n
= var("n")
x
= var("x")
j
= var("j")
k
= var("k")
y
= var('y')
z
= var('z')

A
= lambda n,j: (matrix([[cos(2*pi/n), -sin(2*pi/n)],[sin(2*pi/n), cos(2*pi/n)]]))^j
C
= matrix([[2],[3]])

AC
= A(3,2)*C

a
= AC[0]
b
= AC[1]

fx1y0
= lambda x,y: x + 11
fx1y1
= lambda x,y: x + y + 11
fx2y1
= lambda x,y: x^2 + y + 11

fx1y0
(a,b)


The error I'm getting when I plug in a and b into fx1y0 is this:
TypeError: unsupported operand parent(s) for '+': 'Vector space of dimension 1 over Symbolic Ring' and 'Integer Ring'


I think the problem is that it is treating a and b as 1 dimensional vectors, when all I want is for a and b to contain the values at the location in the matrix that I specify. Basically, I think I want it to treat the matrix as an array, in terms of me getting values from it. Does anyone know how I could fix this?






Vincent Delecroix

unread,
Jan 1, 2016, 6:27:35 PM1/1/16
to sage-s...@googlegroups.com
AC[0] is the first *row* of the matrix. In other words a vector

sage: M = matrix(3, range(9))
sage: M[0]
(0, 1, 2)
sage: _.parent()
Ambient free module of rank 3 over the principal ideal
domain Integer Ring

Replace AC[0] by AC[0,0] in your code or whatever is appropriate...

Vincent

Nils Bruin

unread,
Jan 1, 2016, 6:29:21 PM1/1/16
to sage-support
On Friday, January 1, 2016 at 2:41:25 PM UTC-8, saad khalid wrote:
The error I'm getting when I plug in a and b into fx1y0 is this:
TypeError: unsupported operand parent(s) for '+': 'Vector space of dimension 1 over Symbolic Ring' and 'Integer Ring'


I think the problem is that it is treating a and b as 1 dimensional vectors, when all I want is for a and b to contain the values at the location in the matrix that I specify. Basically, I think I want it to treat the matrix as an array, in terms of me getting values from it. Does anyone know how I could fix this?

You are correct: AC is a 2-by-1 matrix, and AC[0] selects the first row. You can select the appropriate elements via

a = AC[0,0]
b = AC[1,0]

or

a = AC[0][0]
b = AC[1][0]

Note that vectors in sage are normally row vectors, so you if you rewrite it so that C can be a row vector, you can avoid this problem:

AC = C*A(3,2).transpose()

a = AC[0]
b = AC[1]

(if you go this route, you should rewrite the definition of A so that no transpose is required, of course)

slelievre

unread,
Jan 8, 2016, 4:23:35 AM1/8/16
to sage-support


2016-01-01 15:29:20 -0800 (PST), Nils Bruin:


You are correct: AC is a 2-by-1 matrix, and AC[0] selects the first row. You can select the appropriate elements via

a = AC[0,0]
b = AC[1,0]

or

a = AC[0][0]
b = AC[1][0]

I would recommend the first syntax,

a = AC[0,0]

which just accesses the given element, and so it is faster than


a = AC[0][0]

which would create the vector AC[0] and then get its index 0 coordinate.

Moreover, if you want to change an element of a matrix, writing

AC[0,0] = 9

will work, while

AC[0][0] = 9

will fail.

Reply all
Reply to author
Forward
0 new messages