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.Tue, 02 Oct 2018 15:38:41 +0200Show correct output of polynomialhttps://ask.sagemath.org/question/43792/show-correct-output-of-polynomial/ Hi, I am new to sage and I am trying to construct a polynomial to try to transform it. However, when writing it I am getting an incorrect output.
x=var("x")
s = (x^2+2*x+1) + 1/(x^2+2*x+1)
s.show()
And this is the output I am getting -
x^2+2x + 1/x^2+2x+1 +1 (I am unsure why the 1 is carrying over all the way to right side)
I am looking to obtain the following output to begin transforming it -
x^2+2x+1 + 1/x^2+2x+1
Appreciate any help!Sat, 29 Sep 2018 21:15:10 +0200https://ask.sagemath.org/question/43792/show-correct-output-of-polynomial/Comment by rburing for <p>Hi, I am new to sage and I am trying to construct a polynomial to try to transform it. However, when writing it I am getting an incorrect output. </p>
<p>x=var("x")
s = (x^2+2<em>x+1) + 1/(x^2+2</em>x+1)
s.show() </p>
<p>And this is the output I am getting - </p>
<p>x^2+2x + 1/x^2+2x+1 +1 (I am unsure why the 1 is carrying over all the way to right side)</p>
<p>I am looking to obtain the following output to begin transforming it - </p>
<p>x^2+2x+1 + 1/x^2+2x+1</p>
<p>Appreciate any help!</p>
https://ask.sagemath.org/question/43792/show-correct-output-of-polynomial/?comment=43795#post-id-43795This is a rational function, and an 'ordering of terms' is not part of the data, so Sage should not be expected to preserve it. Nevertheless the desired output can be obtained; see my answer.Sun, 30 Sep 2018 11:45:44 +0200https://ask.sagemath.org/question/43792/show-correct-output-of-polynomial/?comment=43795#post-id-43795Comment by nevar123 for <p>Hi, I am new to sage and I am trying to construct a polynomial to try to transform it. However, when writing it I am getting an incorrect output. </p>
<p>x=var("x")
s = (x^2+2<em>x+1) + 1/(x^2+2</em>x+1)
s.show() </p>
<p>And this is the output I am getting - </p>
<p>x^2+2x + 1/x^2+2x+1 +1 (I am unsure why the 1 is carrying over all the way to right side)</p>
<p>I am looking to obtain the following output to begin transforming it - </p>
<p>x^2+2x+1 + 1/x^2+2x+1</p>
<p>Appreciate any help!</p>
https://ask.sagemath.org/question/43792/show-correct-output-of-polynomial/?comment=43796#post-id-43796I tried using the QQ command received the following:
R.<x> = QQ[]
s = (x^2 + 2*x + 1) + 1/(x^2 + 2*x + 1)
print s
output-
(x^4 + 4*x^3 + 6*x^2 + 4*x + 2)/(x^2 + 2*x + 1)
Do you know why the polynomial is expanding?Sun, 30 Sep 2018 20:29:01 +0200https://ask.sagemath.org/question/43792/show-correct-output-of-polynomial/?comment=43796#post-id-43796Comment by rburing for <p>Hi, I am new to sage and I am trying to construct a polynomial to try to transform it. However, when writing it I am getting an incorrect output. </p>
<p>x=var("x")
s = (x^2+2<em>x+1) + 1/(x^2+2</em>x+1)
s.show() </p>
<p>And this is the output I am getting - </p>
<p>x^2+2x + 1/x^2+2x+1 +1 (I am unsure why the 1 is carrying over all the way to right side)</p>
<p>I am looking to obtain the following output to begin transforming it - </p>
<p>x^2+2x+1 + 1/x^2+2x+1</p>
<p>Appreciate any help!</p>
https://ask.sagemath.org/question/43792/show-correct-output-of-polynomial/?comment=43819#post-id-43819That is due to the implementation of fraction field elements. To render the expression in the desired way, see my answer.Tue, 02 Oct 2018 15:38:41 +0200https://ask.sagemath.org/question/43792/show-correct-output-of-polynomial/?comment=43819#post-id-43819Answer by rburing for <p>Hi, I am new to sage and I am trying to construct a polynomial to try to transform it. However, when writing it I am getting an incorrect output. </p>
<p>x=var("x")
s = (x^2+2<em>x+1) + 1/(x^2+2</em>x+1)
s.show() </p>
<p>And this is the output I am getting - </p>
<p>x^2+2x + 1/x^2+2x+1 +1 (I am unsure why the 1 is carrying over all the way to right side)</p>
<p>I am looking to obtain the following output to begin transforming it - </p>
<p>x^2+2x+1 + 1/x^2+2x+1</p>
<p>Appreciate any help!</p>
https://ask.sagemath.org/question/43792/show-correct-output-of-polynomial/?answer=43794#post-id-43794The object you want to construct is a rational function (quotient of polynomials), and we can define it as such:
R.<x> = QQ[]
s = (x^2 + 2*x + 1) + 1/(x^2 + 2*x + 1)
We can obtain the two parts you want e.g. as follows (or you could have defined them individually):
sage: q, r = s.numerator().quo_rem(s.denominator())
sage: parts = (q, r/s.denominator())
sage: parts
(x^2 + 2*x + 1, 1/(x^2 + 2*x + 1))
Now we can generate the LaTeX for each part individually and join them by a plus symbol:
sage: latex_code = latex(parts[0]) + '+' + latex(parts[1])
sage: latex_code
x^{2} + 2 x + 1 + \frac{1}{x^{2} + 2 x + 1}
sage: show(latex_code)
Showing the LaTeX gives the desired output: $x^{2} + 2 x + 1 + \frac{1}{x^{2} + 2 x + 1}$.Sun, 30 Sep 2018 11:38:41 +0200https://ask.sagemath.org/question/43792/show-correct-output-of-polynomial/?answer=43794#post-id-43794