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.Mon, 07 Mar 2022 13:59:34 +0100How to get a polynomial by changing basehttps://ask.sagemath.org/question/61439/how-to-get-a-polynomial-by-changing-base/I want to have (x-3)^2 as a polynomial in (x-1) as opposed to x; I want it output (x-1)^2 - 4(x-1) + 4Mon, 07 Mar 2022 12:01:15 +0100https://ask.sagemath.org/question/61439/how-to-get-a-polynomial-by-changing-base/Comment by slelievre for <p>I want to have (x-3)^2 as a polynomial in (x-1) as opposed to x; I want it output (x-1)^2 - 4(x-1) + 4</p>
https://ask.sagemath.org/question/61439/how-to-get-a-polynomial-by-changing-base/?comment=61441#post-id-61441Welcome to Ask Sage! Thank you for your question.Mon, 07 Mar 2022 13:00:00 +0100https://ask.sagemath.org/question/61439/how-to-get-a-polynomial-by-changing-base/?comment=61441#post-id-61441Answer by slelievre for <p>I want to have (x-3)^2 as a polynomial in (x-1) as opposed to x; I want it output (x-1)^2 - 4(x-1) + 4</p>
https://ask.sagemath.org/question/61439/how-to-get-a-polynomial-by-changing-base/?answer=61440#post-id-61440Symbolic expressions have a `taylor` method which is supposed to do that.
Below, `f.taylor(x, 1, 2)` does a Taylor expansion of `f` with respect to `x`
at the point `1` to the order `2`.
Unfortunately it does not keep the linear term in the desired form:
sage: f = (x - 3)^2
sage: f.taylor(x, 1, 2)
(x - 1)^2 - 4*x + 8
There was already a Sage Trac ticket about that:
- [Sage Trac ticket 28638: Incorrect output for `taylor`](https://trac.sagemath.org/ticket/28638)
which was closed because the initial formulation of the ticket
was that the result was incorrect; in fact the result is correct
but the way it is displayed is incorrect, so I reopened that ticket.
**Workaround**
In your case, if you want to get the coefficients that should go
in front of the various powers of `(x - 1)`, you can shift `f` by `1`:
sage: x = polygen(QQ)
sage: f = (x - 3)^2
sage: f(x + 1)
x^2 - 4*x + 4
This tells you `f` is `(x -1)^2 - 4*(x - 1) + 4`.Mon, 07 Mar 2022 12:59:43 +0100https://ask.sagemath.org/question/61439/how-to-get-a-polynomial-by-changing-base/?answer=61440#post-id-61440Comment by mathboy for <p>Symbolic expressions have a <code>taylor</code> method which is supposed to do that.</p>
<p>Below, <code>f.taylor(x, 1, 2)</code> does a Taylor expansion of <code>f</code> with respect to <code>x</code>
at the point <code>1</code> to the order <code>2</code>.</p>
<p>Unfortunately it does not keep the linear term in the desired form:</p>
<pre><code>sage: f = (x - 3)^2
sage: f.taylor(x, 1, 2)
(x - 1)^2 - 4*x + 8
</code></pre>
<p>There was already a Sage Trac ticket about that:</p>
<ul>
<li><a href="https://trac.sagemath.org/ticket/28638">Sage Trac ticket 28638: Incorrect output for <code>taylor</code></a></li>
</ul>
<p>which was closed because the initial formulation of the ticket
was that the result was incorrect; in fact the result is correct
but the way it is displayed is incorrect, so I reopened that ticket.</p>
<p><strong>Workaround</strong></p>
<p>In your case, if you want to get the coefficients that should go
in front of the various powers of <code>(x - 1)</code>, you can shift <code>f</code> by <code>1</code>:</p>
<pre><code>sage: x = polygen(QQ)
sage: f = (x - 3)^2
sage: f(x + 1)
x^2 - 4*x + 4
</code></pre>
<p>This tells you <code>f</code> is <code>(x -1)^2 - 4*(x - 1) + 4</code>.</p>
https://ask.sagemath.org/question/61439/how-to-get-a-polynomial-by-changing-base/?comment=61442#post-id-61442Is it then possible to substitute x - 1 for x in the expression without having it expand?Mon, 07 Mar 2022 13:59:34 +0100https://ask.sagemath.org/question/61439/how-to-get-a-polynomial-by-changing-base/?comment=61442#post-id-61442