# Evaluating a polynomial on a matrices

Hi, I would like to evaluate a polynomial f(x) with coefficients as integers by letting x to be a matrix T. How do I do this on sage? The context is this: I have a matrix A. sage can tell me the characteristic polynomial of A. It will even factorize it for me, say, as f_1(x)f_2(x)f_3(x). I know how to extract each irreducible factor. Is there a way that I can use f_i(x) to evaluate it on a matrix, using sage? (other than manually writing out the polynomial with x replaced by the matrix I wish to evaluate on)

I am new to sage so apologies if this is a silly question.

edit retag close merge delete

To display blocks of code, either indent them with 4 spaces, or select the corresponding lines and click the "code" button (the icon with '101 010'). Can you edit your question to do that?

To display inline code, surround it within "backticks" or "backquotes" .

( 2016-04-12 08:29:41 -0500 )edit

Sort by » oldest newest most voted

Just evaluate the polynomial on the matrix.

You will need to create the polynomial in an appropriate polynomial ring.

Here is an example.

sage: m = matrix(((0, 1), (1, 1)))
sage: R.<x> = PolynomialRing(QQ, 'x')
sage: p = x^2 - x - 1
sage: p(m)
[0 0]
[0 0]


Starting with a polynomial symbolic expression, first turn it into an element of a polynomial ring.

Here is an example.

sage: x = SR.var('x')
sage: q = x^2 - x - 1
sage: m = matrix(((0, 1), (1, 1)))
sage: q(m)
... DeprecationWarning: ...
TypeError ... Traceback (most recent call last)
...
TypeError: no canonical coercion from Full MatrixSpace of 2 by 2 dense matrices over Integer Ring to Symbolic Ring
sage: R.<x> = PolynomialRing(QQ, 'x')
sage: p = R(q)
sage: p(m)
[0 0]
[0 0]


You can check where your polynomial belongs as follows.

sage: q.parent()
Symbolic Ring
sage: p.parent()
Univariate Polynomial Ring in x over Rational Field

more

Thanks. But I think my problem persists because of the way I am evaluating it. I separated out the irreducible factors of the characteristic polynomial using factor_list(). After that, I am able to evaluate the factors over integers successfully, but an error message is displayed if I try evaluating over matrices.

This is the error message (the last line...): "TypeError: no canonical coercion from Full MatrixSpace of 3 by 3 dense matrices over Rational Field to Symbolic Ring"

Could you please help me with this? Also, how do you copy paste a segment of code here, like the way you showed me an example?

( 2016-04-12 03:52:18 -0500 )edit

@neha: I edited the answer to add more hints. It would still be useful if you can include your code in your question. Knowing how users write code helps improve the documentation and tutorials.

( 2016-04-12 08:37:58 -0500 )edit

@slelievre: Sorry for the delayed response. Thank you, your detailed example helped and I was able to get the code to work! I am unable to insert the code, though. I mean...the commands don't appear like how your example does (with "sage: " appearing before each command). I copy-pasted a few lines from the worksheet I am using on sagemath cloud, selected the lines and clicked on the code icon, but it just appears as a series of commands one after the other, not even in separate lines. :( Sorry to bother you again, but what am I doing wrong?

( 2016-04-14 01:43:47 -0500 )edit

To get the code ready for copy-paste in SageMathCloud, use the terminal. In SageMathCloud, open a new terminal, launch Sage by typing sage`, then paste your input lines one after the other. To input that into your Ask Sage question, click the "edit" button at the bottom of your question, then copy-paste the blocks of code from the SageMathCloud terminal, and click the "code" button to display it properly. Ask Sage shows you a preview of what your question will look like.

( 2016-04-14 06:43:59 -0500 )edit

Thank you so much! I got it now. I was able to see it displayed after the question. (I removed it later so that my question was still coherent.) Thanks again for your help!

( 2016-04-14 08:18:14 -0500 )edit