Ask Your Question
1

Applying a two-variable polynomial to matrices

asked 2011-11-27 22:25:08 +0100

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.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2011-11-27 23:05:09 +0100

DSM gravatar image

updated 2011-11-27 23:07:55 +0100

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

Comments

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

Joel B. gravatar imageJoel B. ( 2011-11-28 10:17:48 +0100 )edit

@Joel B. Cool, glad that helped!

DSM gravatar imageDSM ( 2011-11-28 11:02:20 +0100 )edit

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 ( 2014-09-30 03:29:27 +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: 2011-11-27 22:25:08 +0100

Seen: 424 times

Last updated: Nov 27 '11