Ask Your Question
1

Show correct output of polynomial

asked 2018-09-29 21:15:10 +0100

anonymous user

Anonymous

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+2x+1) + 1/(x^2+2x+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!

edit retag flag offensive close merge delete

Comments

This 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.

rburing gravatar imagerburing ( 2018-09-30 11:45:44 +0100 )edit

I tried using the QQ command received the following:

R.<x> = QQ[]

s = (x^2 + 2x + 1) + 1/(x^2 + 2x + 1)

print s

output-

(x^4 + 4x^3 + 6x^2 + 4x + 2)/(x^2 + 2x + 1)

Do you know why the polynomial is expanding?

nevar123 gravatar imagenevar123 ( 2018-09-30 20:29:01 +0100 )edit

That is due to the implementation of fraction field elements. To render the expression in the desired way, see my answer.

rburing gravatar imagerburing ( 2018-10-02 15:38:41 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-09-30 11:38:41 +0100

rburing gravatar image

The 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}$.

edit flag offensive delete link more

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

Stats

Asked: 2018-09-29 21:15:10 +0100

Seen: 300 times

Last updated: Sep 30 '18