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, 23 Jul 2020 01:51:33 +0200Pade approximationhttps://ask.sagemath.org/question/32722/pade-approximation/ How to use the pade function to calculate the pade approximation of a function.
I have a function (for example arctan(x) ) and would like to calculate the pade approximation of that function. I have found that there exists a function for that in sage, but I was not able to run it, could someone help me with that?
Sat, 05 Mar 2016 19:17:51 +0100https://ask.sagemath.org/question/32722/pade-approximation/Answer by tmonteil for <p>How to use the pade function to calculate the pade approximation of a function.
I have a function (for example arctan(x) ) and would like to calculate the pade approximation of that function. I have found that there exists a function for that in sage, but I was not able to run it, could someone help me with that?</p>
https://ask.sagemath.org/question/32722/pade-approximation/?answer=32723#post-id-32723Let `f` be your function, here a symbolic expression:
sage: f = arctan(x)
sage: f.parent()
Symbolic Ring
If you search for "pade sagemath" you will get this page http://doc.sagemath.org/html/en/reference/power_series/sage/rings/power_series_poly.html so you have first to transform the symbolic expression `arctan(x)` into a power series. As you can see, the symbolic expression has a method named `power_series`, so let us try it:
sage: f.power_series(QQ)
TypeError: unable to convert arctan(x) to a rational
The problem is that, currently, Sage is not able to transform such a symbolic expression into a power series, only the expressions that are polynomials, see the doc of the method by typing `f.power_series`
So, you have first to approximate `f` to a truncated power series, also known as Taylor expansion:
sage: f.taylor(x,0,12)
-1/11*x^11 + 1/9*x^9 - 1/7*x^7 + 1/5*x^5 - 1/3*x^3 + x
Then you can get it as a power series over the rationals:
sage: f.taylor(x,0,12).power_series(QQ)
x - 1/3*x^3 + 1/5*x^5 - 1/7*x^7 + 1/9*x^9 - 1/11*x^11 + O(x^12)
Then you can get the pade approximant of your choice:
sage: f.taylor(x,0,12).power_series(QQ).pade(3,3)
(4/9*x^3 + 5/3*x)/(x^2 + 5/3)
Of course, the higher pade approximant you require, the higer the degree in the Taylor expansion:
sage: f.taylor(x,0,12).power_series(QQ).pade(10,10)
ValueError: the precision of the series is not large enough
sage: f.taylor(x,0,42).power_series(QQ).pade(10,10)
(61567/3969*x^9 + 106964/441*x^7 + 44902/45*x^5 + 281996/189*x^3 + 46189/63*x)/(x^10 + 55*x^8 + 1430/3*x^6 + 1430*x^4 + 12155/7*x^2 + 46189/63)
Sat, 05 Mar 2016 21:06:37 +0100https://ask.sagemath.org/question/32722/pade-approximation/?answer=32723#post-id-32723Comment by Marco Caliari for <p>Let <code>f</code> be your function, here a symbolic expression:</p>
<pre><code>sage: f = arctan(x)
sage: f.parent()
Symbolic Ring
</code></pre>
<p>If you search for "pade sagemath" you will get this page <a href="http://doc.sagemath.org/html/en/reference/power_series/sage/rings/power_series_poly.html">http://doc.sagemath.org/html/en/refer...</a> so you have first to transform the symbolic expression <code>arctan(x)</code> into a power series. As you can see, the symbolic expression has a method named <code>power_series</code>, so let us try it:</p>
<pre><code>sage: f.power_series(QQ)
TypeError: unable to convert arctan(x) to a rational
</code></pre>
<p>The problem is that, currently, Sage is not able to transform such a symbolic expression into a power series, only the expressions that are polynomials, see the doc of the method by typing <code>f.power_series</code></p>
<p>So, you have first to approximate <code>f</code> to a truncated power series, also known as Taylor expansion:</p>
<pre><code>sage: f.taylor(x,0,12)
-1/11*x^11 + 1/9*x^9 - 1/7*x^7 + 1/5*x^5 - 1/3*x^3 + x
</code></pre>
<p>Then you can get it as a power series over the rationals:</p>
<pre><code>sage: f.taylor(x,0,12).power_series(QQ)
x - 1/3*x^3 + 1/5*x^5 - 1/7*x^7 + 1/9*x^9 - 1/11*x^11 + O(x^12)
</code></pre>
<p>Then you can get the pade approximant of your choice:</p>
<pre><code>sage: f.taylor(x,0,12).power_series(QQ).pade(3,3)
(4/9*x^3 + 5/3*x)/(x^2 + 5/3)
</code></pre>
<p>Of course, the higher pade approximant you require, the higer the degree in the Taylor expansion:</p>
<pre><code>sage: f.taylor(x,0,12).power_series(QQ).pade(10,10)
ValueError: the precision of the series is not large enough
sage: f.taylor(x,0,42).power_series(QQ).pade(10,10)
(61567/3969*x^9 + 106964/441*x^7 + 44902/45*x^5 + 281996/189*x^3 + 46189/63*x)/(x^10 + 55*x^8 + 1430/3*x^6 + 1430*x^4 + 12155/7*x^2 + 46189/63)
</code></pre>
https://ask.sagemath.org/question/32722/pade-approximation/?comment=38683#post-id-38683Hi, is it possible to go back to a power series? If I give
sage: f.taylor(x,0,12).power_series(QQ).pade(3,3).taylor(x,0,12)
I get the error: AttributeError: 'FractionFieldElement_1poly_field' object has no attribute 'taylor'Tue, 29 Aug 2017 13:40:52 +0200https://ask.sagemath.org/question/32722/pade-approximation/?comment=38683#post-id-38683Comment by AlexGhitza for <p>Let <code>f</code> be your function, here a symbolic expression:</p>
<pre><code>sage: f = arctan(x)
sage: f.parent()
Symbolic Ring
</code></pre>
<p>If you search for "pade sagemath" you will get this page <a href="http://doc.sagemath.org/html/en/reference/power_series/sage/rings/power_series_poly.html">http://doc.sagemath.org/html/en/refer...</a> so you have first to transform the symbolic expression <code>arctan(x)</code> into a power series. As you can see, the symbolic expression has a method named <code>power_series</code>, so let us try it:</p>
<pre><code>sage: f.power_series(QQ)
TypeError: unable to convert arctan(x) to a rational
</code></pre>
<p>The problem is that, currently, Sage is not able to transform such a symbolic expression into a power series, only the expressions that are polynomials, see the doc of the method by typing <code>f.power_series</code></p>
<p>So, you have first to approximate <code>f</code> to a truncated power series, also known as Taylor expansion:</p>
<pre><code>sage: f.taylor(x,0,12)
-1/11*x^11 + 1/9*x^9 - 1/7*x^7 + 1/5*x^5 - 1/3*x^3 + x
</code></pre>
<p>Then you can get it as a power series over the rationals:</p>
<pre><code>sage: f.taylor(x,0,12).power_series(QQ)
x - 1/3*x^3 + 1/5*x^5 - 1/7*x^7 + 1/9*x^9 - 1/11*x^11 + O(x^12)
</code></pre>
<p>Then you can get the pade approximant of your choice:</p>
<pre><code>sage: f.taylor(x,0,12).power_series(QQ).pade(3,3)
(4/9*x^3 + 5/3*x)/(x^2 + 5/3)
</code></pre>
<p>Of course, the higher pade approximant you require, the higer the degree in the Taylor expansion:</p>
<pre><code>sage: f.taylor(x,0,12).power_series(QQ).pade(10,10)
ValueError: the precision of the series is not large enough
sage: f.taylor(x,0,42).power_series(QQ).pade(10,10)
(61567/3969*x^9 + 106964/441*x^7 + 44902/45*x^5 + 281996/189*x^3 + 46189/63*x)/(x^10 + 55*x^8 + 1430/3*x^6 + 1430*x^4 + 12155/7*x^2 + 46189/63)
</code></pre>
https://ask.sagemath.org/question/32722/pade-approximation/?comment=52628#post-id-52628That's because the rational function in x returned by pade is not a symbolic expression. You can cast it back into the SymbolicRing and then take the Taylor series:
```
sage: SR(f.taylor(x, 0, 12).power_series(QQ).pade(3, 3)).taylor(x, 0, 12)
-27/625*x^11 + 9/125*x^9 - 3/25*x^7 + 1/5*x^5 - 1/3*x^3 + x
```Thu, 23 Jul 2020 01:51:33 +0200https://ask.sagemath.org/question/32722/pade-approximation/?comment=52628#post-id-52628