ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 14 Apr 2016 15:27:15 +0200Evaluating a polynomial on a matriceshttps://ask.sagemath.org/question/33073/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.
Tue, 12 Apr 2016 08:02:52 +0200https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/Comment by slelievre for <p>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)<em>f_2(x)</em>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)</p>
<p>I am new to sage so apologies if this is a silly question.</p>
https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33078#post-id-33078To 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" `.Tue, 12 Apr 2016 15:29:41 +0200https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33078#post-id-33078Answer by slelievre for <p>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)<em>f_2(x)</em>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)</p>
<p>I am new to sage so apologies if this is a silly question.</p>
https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?answer=33074#post-id-33074Just 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
Tue, 12 Apr 2016 09:00:29 +0200https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?answer=33074#post-id-33074Comment by neha for <p>Just evaluate the polynomial on the matrix.</p>
<p>You will need to create the polynomial in an appropriate polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>Starting with a polynomial symbolic expression,
first turn it into an element of a polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>You can check where your polynomial belongs as follows.</p>
<pre><code>sage: q.parent()
Symbolic Ring
sage: p.parent()
Univariate Polynomial Ring in x over Rational Field
</code></pre>
https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33076#post-id-33076Thanks. 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?Tue, 12 Apr 2016 10:52:18 +0200https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33076#post-id-33076Comment by slelievre for <p>Just evaluate the polynomial on the matrix.</p>
<p>You will need to create the polynomial in an appropriate polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>Starting with a polynomial symbolic expression,
first turn it into an element of a polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>You can check where your polynomial belongs as follows.</p>
<pre><code>sage: q.parent()
Symbolic Ring
sage: p.parent()
Univariate Polynomial Ring in x over Rational Field
</code></pre>
https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33079#post-id-33079@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.Tue, 12 Apr 2016 15:37:58 +0200https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33079#post-id-33079Comment by neha for <p>Just evaluate the polynomial on the matrix.</p>
<p>You will need to create the polynomial in an appropriate polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>Starting with a polynomial symbolic expression,
first turn it into an element of a polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>You can check where your polynomial belongs as follows.</p>
<pre><code>sage: q.parent()
Symbolic Ring
sage: p.parent()
Univariate Polynomial Ring in x over Rational Field
</code></pre>
https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33098#post-id-33098@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?Thu, 14 Apr 2016 08:43:47 +0200https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33098#post-id-33098Comment by slelievre for <p>Just evaluate the polynomial on the matrix.</p>
<p>You will need to create the polynomial in an appropriate polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>Starting with a polynomial symbolic expression,
first turn it into an element of a polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>You can check where your polynomial belongs as follows.</p>
<pre><code>sage: q.parent()
Symbolic Ring
sage: p.parent()
Univariate Polynomial Ring in x over Rational Field
</code></pre>
https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33103#post-id-33103To 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.Thu, 14 Apr 2016 13:43:59 +0200https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33103#post-id-33103Comment by neha for <p>Just evaluate the polynomial on the matrix.</p>
<p>You will need to create the polynomial in an appropriate polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>Starting with a polynomial symbolic expression,
first turn it into an element of a polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>You can check where your polynomial belongs as follows.</p>
<pre><code>sage: q.parent()
Symbolic Ring
sage: p.parent()
Univariate Polynomial Ring in x over Rational Field
</code></pre>
https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33105#post-id-33105Thank 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!Thu, 14 Apr 2016 15:18:14 +0200https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33105#post-id-33105Comment by slelievre for <p>Just evaluate the polynomial on the matrix.</p>
<p>You will need to create the polynomial in an appropriate polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>Starting with a polynomial symbolic expression,
first turn it into an element of a polynomial ring.</p>
<p>Here is an example.</p>
<pre><code>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]
</code></pre>
<p>You can check where your polynomial belongs as follows.</p>
<pre><code>sage: q.parent()
Symbolic Ring
sage: p.parent()
Univariate Polynomial Ring in x over Rational Field
</code></pre>
https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33106#post-id-33106@neha: Why don't you add it at the end of your question? You could clearly mark it as an edit. See question http://ask.sagemath.org/question/32978/solve-irrational-inequality/ for an example of a clearly marked edit.Thu, 14 Apr 2016 15:27:15 +0200https://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/?comment=33106#post-id-33106