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, 31 Jan 2019 21:30:24 +0100Factorize characteristic polynomial in SR base ringhttps://ask.sagemath.org/question/45249/factorize-characteristic-polynomial-in-sr-base-ring/ I am total newbie to SAGE so this question might be trivial. How can I factorize the characteristic polynomial obtained by a symbolic matrix in SAGE 8.6? Is there a workaround the fact that `factor()` is not defined on the base ring `SR` which is the one inherited from the symbolic matrix?
For example I have in a SAGE/Jupyter notebook something like:
a,b,c = var('a','b','c')
M = Matrix(SR,3,3)
M[0] = [a, -b, 0]
M[1] = [c, a+b, 0]
M[2] = [0, 0, 1]
e = M.eigenvalues()
f = M.charpoly()
factor(f)
The last instruction raises a `NotImplementedError` as expected from the fact that `factor` is not defined on `SR`...
In my real problem I am computing characteristic polynomials of large (8x8) symbolic matrices and I would like to get at glance all the factors, so as to quickly isolate negative real roots and instead easily discuss conditions for existence and sign of symbolic ones.Thu, 31 Jan 2019 18:26:46 +0100https://ask.sagemath.org/question/45249/factorize-characteristic-polynomial-in-sr-base-ring/Answer by nbruin for <p>I am total newbie to SAGE so this question might be trivial. How can I factorize the characteristic polynomial obtained by a symbolic matrix in SAGE 8.6? Is there a workaround the fact that <code>factor()</code> is not defined on the base ring <code>SR</code> which is the one inherited from the symbolic matrix?</p>
<p>For example I have in a SAGE/Jupyter notebook something like:</p>
<pre><code>a,b,c = var('a','b','c')
M = Matrix(SR,3,3)
M[0] = [a, -b, 0]
M[1] = [c, a+b, 0]
M[2] = [0, 0, 1]
e = M.eigenvalues()
f = M.charpoly()
factor(f)
</code></pre>
<p>The last instruction raises a <code>NotImplementedError</code> as expected from the fact that <code>factor</code> is not defined on <code>SR</code>...
In my real problem I am computing characteristic polynomials of large (8x8) symbolic matrices and I would like to get at glance all the factors, so as to quickly isolate negative real roots and instead easily discuss conditions for existence and sign of symbolic ones.</p>
https://ask.sagemath.org/question/45249/factorize-characteristic-polynomial-in-sr-base-ring/?answer=45250#post-id-45250Actually, it's just that factor isn't defined on *univariate polynomials over SR*. If charpoly were returned as an element of SR, there could be trouble if the matrix already contains an `x`, as in `matrix([SR('x')]).charpoly()`. You can put it in SR and then you can factor it:
sage: factor(f(SR('x')))
(a^2 + a*b + b*c - 2*a*x - b*x + x^2)*(x - 1)
As you can see:
sage: parent(f)
Univariate Polynomial Ring in x over Symbolic Ring
the polynomial `f` doesn't lie in SR, but in a univariate polynomial *over* SR. By evaluating the polynomial in an element of `SR` we can map it into SR, though. With `SR('x')` you create the symbol `x`. You can also use `SR.var('x')` if you prefer. It's different from the variable in which `f` is expressed, even though they print the same:
sage: parent(f).0
x
sage: parent(f).0 == SR('x')
FalseThu, 31 Jan 2019 19:21:28 +0100https://ask.sagemath.org/question/45249/factorize-characteristic-polynomial-in-sr-base-ring/?answer=45250#post-id-45250Comment by maurizio for <p>Actually, it's just that factor isn't defined on <em>univariate polynomials over SR</em>. If charpoly were returned as an element of SR, there could be trouble if the matrix already contains an <code>x</code>, as in <code>matrix([SR('x')]).charpoly()</code>. You can put it in SR and then you can factor it:</p>
<pre><code>sage: factor(f(SR('x')))
(a^2 + a*b + b*c - 2*a*x - b*x + x^2)*(x - 1)
</code></pre>
<p>As you can see:</p>
<pre><code>sage: parent(f)
Univariate Polynomial Ring in x over Symbolic Ring
</code></pre>
<p>the polynomial <code>f</code> doesn't lie in SR, but in a univariate polynomial <em>over</em> SR. By evaluating the polynomial in an element of <code>SR</code> we can map it into SR, though. With <code>SR('x')</code> you create the symbol <code>x</code>. You can also use <code>SR.var('x')</code> if you prefer. It's different from the variable in which <code>f</code> is expressed, even though they print the same:</p>
<pre><code>sage: parent(f).0
x
sage: parent(f).0 == SR('x')
False
</code></pre>
https://ask.sagemath.org/question/45249/factorize-characteristic-polynomial-in-sr-base-ring/?comment=45252#post-id-45252Awesome thanks. @nbruin if you don't mind, I was missing the possibility for this command: `SR('x')`. This tells Sage basically to use the unknown `x` within the SR field? Or how should I interpret it? 'Cause it is not completely immediate for me that I am using a base ring class as a method, unless I think of it as a short way to instantiate a variable of that class (something like `:` in `C++` class definers).Thu, 31 Jan 2019 21:30:24 +0100https://ask.sagemath.org/question/45249/factorize-characteristic-polynomial-in-sr-base-ring/?comment=45252#post-id-45252