First time here? Check out the FAQ!

Ask Your Question
1

Applying a two-variable polynomial to matrices

asked 13 years ago

Joel B. gravatar image

A naive question: I have in sage two commuting square matrices X and Y over a ring R, and a polynomial P in two variables x and y over R. How can I ask sage to compute P(X,Y)?

Thanks.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 13 years ago

DSM gravatar image

updated 13 years ago

Couldn't you simply substitute X and Y into the polynomial P as the values of x and y? (Throwing worries about ordering to the wind because we have that they commute.)

sage: # set things up
sage: R.<x,y> = QQ[]
sage: P = R.random_element(10,12)
sage: M = MatrixSpace(QQ, 5)
sage: X = M.random_element()
sage: Y = M.random_element()
sage: 
sage: # check the inputs
sage: P
-x^3*y^7 + 1/2*x^2*y^6 + 1/2*x^7 - 13*x^5 - 1/2*y^5 + 2*x*y^2 + y^3
sage: X
[ 1/2    0   -1    0 -1/2]
[  -2    0 -1/2    1 -1/2]
[  -1   -2   -2    0   -2]
[   1    2    0   -2    2]
[   2    1   -1    0    0]
sage: Y
[  0  -2   0   1  -2]
[  0   0   0   0  -1]
[ -1  -1   0  -1   2]
[  0  -2  -2  -2   1]
[ -1  -1 1/2   0   0]
sage: 
sage: parent(P)
Multivariate Polynomial Ring in x, y over Rational Field
sage: 
sage: # do the substitution
sage: q = P.subs(x=X, y=Y)
sage: q
[-170339/256  221009/128  337381/128    90749/64  -27463/256]
[-131901/128   461983/64   574233/64   226241/32 -367357/128]
[-339165/128   441951/64   663751/64   184439/32  -89985/128]
[ 167421/128  -917935/64 -1076103/64  -456607/32  823585/128]
[    8761/64    -3571/32    -6061/32      -815/8     3597/64]
sage: parent(q)
Full MatrixSpace of 5 by 5 dense matrices over Rational Field
sage: parent(q) is M
True
sage: 
sage: # sanity check that it's what we expected
sage: q2 = sage_eval(str(P).replace('x','X').replace('y','Y'),locals=locals())
sage: q == q2
True
Preview: (hide)
link

Comments

Thank you very much. I didn't know the method "subs".

Joel B. gravatar imageJoel B. ( 13 years ago )

@Joel B. Cool, glad that helped!

DSM gravatar imageDSM ( 13 years ago )

Equivalently, you can just "evaluate" the polynomial at the matrices by calling P(X,Y) . This is potentially even a little more efficient than P.subs(x=X,y=Y) because it is clear from the start that it is not a partial evaluation, e.g., P(x=X)(y=Y) gives a rather incomprehensible result by comparison.

nbruin gravatar imagenbruin ( 10 years ago )

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: 13 years ago

Seen: 440 times

Last updated: Nov 27 '11