---
title: "pyratapprox: Rational Approximation in Python"
kernelspec: 
    name: python3
---

This package provides classes and methods for approximating functions or data using rational functions in Python. It includes algorithms for both discrete and continuous approximation, as well as tools for working with various representations of rational functions. It presents a Pythonic interface to the functionality provided by the Julia package [RationalFunctionApproximation.jl](https://complexvariables.github.io/RationalFunctionApproximation.jl/stable/), which is the computational engine. Julia and the necessary dependencies are installed automatically when you first import `pyratapprox`.

## Examples
Here are some simple examples of how to use the package:

```{python}
from pyratapprox import approximate, unitinterval
import numpy as np
def f(x):
    return x + np.tanh(20 * (x + 0.25))
r = approximate(f, unitinterval)
print("Approximant of rational type", r.degrees())
```

The constructed object can be called like a function to evaluate the approximant:

```{python}
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 1000)
plt.plot(x, r(x))
plt.ylabel('r(x)')
plt.title('Rational approximant');
```

By default, the approximant is constructed to achieve an accuracy of about $10^{-14}$, as seen here:

```{python}
import matplotlib.pyplot as plt
plt.plot(x, f(x) - r(x))
plt.ylabel('f(x) - r(x)')
plt.title('Pointwise error');
```

If no method is specified, the AAA algorithm is used to construct a barycentric rational approximant:

```{python}
r.getfunction()
```

You can instead request a Thiele continued fraction approximant:

```{python}
from pyratapprox import TCF
approximate(f, unitinterval, method=TCF).getfunction()
```

There is often not much to separate the two methods, but as the total degree $n$ increases, AAA is an $O(n^4)$ algorithm, while the Thiele method is $O(n^3)$.

For more examples and information, see the [Julia package documentation](https://complexvariables.github.io/RationalFunctionApproximation.jl/stable/).

