Ask Your Question

Evaluating a polynomial on a matrices

asked 2016-04-12 08:43:31 +0200

neha gravatar image

updated 2016-04-14 15:20:47 +0200

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 flag offensive 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" `.

slelievre gravatar imageslelievre ( 2016-04-12 15:29:41 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-04-12 09:00:29 +0200

slelievre gravatar image

updated 2016-04-12 15:36:34 +0200

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

neha gravatar imageneha ( 2016-04-12 10:52:18 +0200 )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.

slelievre gravatar imageslelievre ( 2016-04-12 15:37:58 +0200 )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?

neha gravatar imageneha ( 2016-04-14 08:43:47 +0200 )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.

slelievre gravatar imageslelievre ( 2016-04-14 13:43:59 +0200 )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!

neha gravatar imageneha ( 2016-04-14 15:18:14 +0200 )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

1 follower


Asked: 2016-04-12 08:02:52 +0200

Seen: 790 times

Last updated: Apr 14 '16