Name | baryrat JSON |
Version |
2.1.2
JSON |
| download |
home_page | None |
Summary | A Python package for barycentric rational approximation |
upload_time | 2025-02-18 23:00:47 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
license | None |
keywords |
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Barycentric rational approximation [](https://github.com/c-f-h/baryrat/actions/workflows/python-package.yml) [](https://badge.fury.io/py/baryrat)
This is a pure Python package which provides routines for rational and
polynomial approximation for real and complex functions through the so-called barycentric representation.
The advantage of this representation is (often significantly) improved
stability over classical approaches.
See the [API documentation](https://baryrat.readthedocs.io/) for an overview of
the available functions.
## Features
### Best rational approximation using BRASIL
The package implements the novel BRASIL algorithm for best rational approximation;
see [the paper](https://doi.org/10.1007/s11075-020-01042-0) or
[the preprint](https://www.ricam.oeaw.ac.at/files/reports/20/rep20-37.pdf)
to learn more.
The following example computes the best uniform rational approximation of degree 5
to a given function in the interval [0, pi]:
```python
import numpy as np
import baryrat
def f(x): return np.sin(x) * np.exp(x)
r = baryrat.brasil(f, [0,np.pi], 5)
```
The rational function `r` can then be evaluated at arbitrary nodes, its poles computed,
and more. See the [documentation](https://baryrat.readthedocs.io/) for details.
### The AAA algorithm
The package includes a Python implementation of the AAA algorithm for rational
approximation described in the paper "The AAA Algorithm for Rational
Approximation" by Yuji Nakatsukasa, Olivier Sète, and Lloyd N. Trefethen, SIAM
Journal on Scientific Computing 2018 40:3, A1494-A1522.
[(doi)](https://doi.org/10.1137/16M1106122)
A MATLAB implementation of this algorithm is contained in
[Chebfun](http://www.chebfun.org/). The present Python version is a more or
less direct port of the MATLAB version.
The "cleanup" feature for spurious poles and zeros is not currently implemented.
### Further algorithms
The package includes functions for polynomial interpolation, rational
interpolation with either fixed poles or fixed interpolation nodes,
Floater-Hormann interpolation, and more.
### Extended precision arithmetic
From ``baryrat`` 2.1 forward, most functions in the package support computing in extended precision
using the [`gmpy2`](https://pypi.org/project/gmpy2/) package; linear algebra routines are provided
through the [`flamp`](https://github.com/c-f-h/flamp) package.
To enable this, first install the `flamp` package:
pip install flamp
This will automatically install `gmpy2` as well if it is not yet installed.
In your code, first set the desired number of decimal digits to compute with by
```python
import flamp
flamp.set_dps(100) # compute with 100 decimal digits precision
```
Arrays of numbers should be represented as numpy arrays with the object datatype
containing `gmpy2` floating point numbers. Some convenience functions to create
such arrays are provided in `flamp`.
For instance, use `flamp.linspace(0, 1, 100)` to create equispaced points in
extended precision.
Most functions will autodetect if you pass such extended precision arrays and
use the corresponding extended precision arithmetic in that case. There is
also a `use_mp` flag for many functions, but it is only required to force
the use of extended precision even when the inputs are in double precision.
Also the `BarycentricRational` class supports having its nodes, values, and
weights stored in extended precision and will operate accordingly, for instance
when computing the poles.
## Installation
The package is implemented in pure Python and depends only on numpy and scipy,
with gmpy2 and flamp as optional dependencies as discussed above.
Install it using pip:
pip install baryrat
## Usage
Here's an example of how to approximate a function in the interval [0,1]
using the AAA algorithm:
```python
import numpy as np
from baryrat import aaa
Z = np.linspace(0.0, 1.0, 1000)
F = np.exp(Z) * np.sin(2*np.pi*Z)
r = aaa(Z, F, mmax=10)
```
Instead of the maximum number of terms `mmax`, it's also possible to specify
the error tolerance `tol`. Both arguments work exactly as in the MATLAB
version.
The returned object `r` is an instance of the class
`baryrat.BarycentricRational` and can be called like a function. For instance,
you can compute the error on `Z` like this:
```python
err = F - r(Z)
print(np.linalg.norm(err, np.inf))
```
If you are interested in the poles and residues of the computed rational function,
you can query them like
```python
pol, res = r.polres()
```
and the zeroes using
```python
zer = r.zeros()
```
Finally, the nodes, values and weights used for interpolation (called `zj`,
`fj` and `wj` in the original implementation) can be accessed as properties:
```python
r.nodes
r.values
r.weights
```
### Example: approximating the complex exponential
```python
# create 9 interpolation nodes in a circle
n = 9
nodes = exp(arange(n) / n * 2j * pi)
# interpolate the complex exp function as a degree (4,4) rational function
r = baryrat.interpolate_rat(nodes, exp(nodes))
# compute poles and zeros
poles, zer = r.poles(), r.zeros()
# plot the approximation error and the nodes, poles and zeros
figsize(13.5, 5)
subplot(1, 2, 1)
Y, X = ogrid[-2:2:100j, -2:2:100j]
Z = X + 1j * Y
pcolormesh(X.flat, Y.flat, abs(r(Z) - exp(Z)), norm=mpl.colors.LogNorm())
colorbar();
axis('equal');
subplot(1, 2, 2)
scatter(real(nodes), imag(nodes))
scatter(real(poles), imag(poles), marker='x', c='r')
scatter(real(zer), imag(zer), marker='.', c='g')
axis('equal');
```

## Citing ``baryrat``
If you use this package in any published research, please cite the following publication where the package was first introduced:
* C. Hofreither. **An algorithm for best rational approximation based on barycentric rational interpolation.**
*Numerical Algorithms*, 88(1):365--388, 2021. [(doi)](https://doi.org/10.1007/s11075-020-01042-0)
Raw data
{
"_id": null,
"home_page": null,
"name": "baryrat",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Clemens Hofreither <chofreither@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/ce/e4/00e84ceb5bb5311e433b41eb8cd5c26061d3a63a5682e102c0161bac5b3c/baryrat-2.1.2.tar.gz",
"platform": null,
"description": "# Barycentric rational approximation [](https://github.com/c-f-h/baryrat/actions/workflows/python-package.yml) [](https://badge.fury.io/py/baryrat)\n\nThis is a pure Python package which provides routines for rational and\npolynomial approximation for real and complex functions through the so-called barycentric representation.\nThe advantage of this representation is (often significantly) improved\nstability over classical approaches.\n\nSee the [API documentation](https://baryrat.readthedocs.io/) for an overview of\nthe available functions.\n\n## Features\n\n### Best rational approximation using BRASIL\n\nThe package implements the novel BRASIL algorithm for best rational approximation;\nsee [the paper](https://doi.org/10.1007/s11075-020-01042-0) or\n[the preprint](https://www.ricam.oeaw.ac.at/files/reports/20/rep20-37.pdf)\nto learn more.\n\nThe following example computes the best uniform rational approximation of degree 5\nto a given function in the interval [0, pi]:\n\n```python\nimport numpy as np\nimport baryrat\n\ndef f(x): return np.sin(x) * np.exp(x)\nr = baryrat.brasil(f, [0,np.pi], 5)\n```\n\nThe rational function `r` can then be evaluated at arbitrary nodes, its poles computed,\nand more. See the [documentation](https://baryrat.readthedocs.io/) for details.\n\n### The AAA algorithm\n\nThe package includes a Python implementation of the AAA algorithm for rational\napproximation described in the paper \"The AAA Algorithm for Rational\nApproximation\" by Yuji Nakatsukasa, Olivier S\u00e8te, and Lloyd N. Trefethen, SIAM\nJournal on Scientific Computing 2018 40:3, A1494-A1522.\n[(doi)](https://doi.org/10.1137/16M1106122)\n\nA MATLAB implementation of this algorithm is contained in\n[Chebfun](http://www.chebfun.org/). The present Python version is a more or\nless direct port of the MATLAB version.\n\nThe \"cleanup\" feature for spurious poles and zeros is not currently implemented.\n\n### Further algorithms\n\nThe package includes functions for polynomial interpolation, rational\ninterpolation with either fixed poles or fixed interpolation nodes,\nFloater-Hormann interpolation, and more.\n\n### Extended precision arithmetic\n\nFrom ``baryrat`` 2.1 forward, most functions in the package support computing in extended precision\nusing the [`gmpy2`](https://pypi.org/project/gmpy2/) package; linear algebra routines are provided\nthrough the [`flamp`](https://github.com/c-f-h/flamp) package.\n\nTo enable this, first install the `flamp` package:\n\n pip install flamp\n\nThis will automatically install `gmpy2` as well if it is not yet installed.\n\nIn your code, first set the desired number of decimal digits to compute with by\n\n```python\nimport flamp\nflamp.set_dps(100) # compute with 100 decimal digits precision\n```\n\nArrays of numbers should be represented as numpy arrays with the object datatype\ncontaining `gmpy2` floating point numbers. Some convenience functions to create\nsuch arrays are provided in `flamp`.\nFor instance, use `flamp.linspace(0, 1, 100)` to create equispaced points in\nextended precision.\n\nMost functions will autodetect if you pass such extended precision arrays and\nuse the corresponding extended precision arithmetic in that case. There is\nalso a `use_mp` flag for many functions, but it is only required to force\nthe use of extended precision even when the inputs are in double precision.\n\nAlso the `BarycentricRational` class supports having its nodes, values, and\nweights stored in extended precision and will operate accordingly, for instance\nwhen computing the poles.\n\n## Installation\n\nThe package is implemented in pure Python and depends only on numpy and scipy,\nwith gmpy2 and flamp as optional dependencies as discussed above.\nInstall it using pip:\n\n pip install baryrat\n\n## Usage\n\nHere's an example of how to approximate a function in the interval [0,1]\nusing the AAA algorithm:\n\n```python\nimport numpy as np\nfrom baryrat import aaa\n\nZ = np.linspace(0.0, 1.0, 1000)\nF = np.exp(Z) * np.sin(2*np.pi*Z)\n\nr = aaa(Z, F, mmax=10)\n```\n\nInstead of the maximum number of terms `mmax`, it's also possible to specify\nthe error tolerance `tol`. Both arguments work exactly as in the MATLAB\nversion.\n\nThe returned object `r` is an instance of the class\n`baryrat.BarycentricRational` and can be called like a function. For instance,\nyou can compute the error on `Z` like this:\n\n```python\nerr = F - r(Z)\nprint(np.linalg.norm(err, np.inf))\n```\n\nIf you are interested in the poles and residues of the computed rational function,\nyou can query them like\n\n```python\npol, res = r.polres()\n```\n\nand the zeroes using\n\n```python\nzer = r.zeros()\n```\n\nFinally, the nodes, values and weights used for interpolation (called `zj`,\n`fj` and `wj` in the original implementation) can be accessed as properties:\n\n```python\nr.nodes\nr.values\nr.weights\n```\n\n### Example: approximating the complex exponential\n\n```python\n# create 9 interpolation nodes in a circle\nn = 9\nnodes = exp(arange(n) / n * 2j * pi)\n\n# interpolate the complex exp function as a degree (4,4) rational function\nr = baryrat.interpolate_rat(nodes, exp(nodes))\n# compute poles and zeros\npoles, zer = r.poles(), r.zeros()\n\n# plot the approximation error and the nodes, poles and zeros\nfigsize(13.5, 5)\n\nsubplot(1, 2, 1)\nY, X = ogrid[-2:2:100j, -2:2:100j]\nZ = X + 1j * Y\npcolormesh(X.flat, Y.flat, abs(r(Z) - exp(Z)), norm=mpl.colors.LogNorm())\ncolorbar();\naxis('equal');\n\nsubplot(1, 2, 2)\nscatter(real(nodes), imag(nodes))\nscatter(real(poles), imag(poles), marker='x', c='r')\nscatter(real(zer), imag(zer), marker='.', c='g')\naxis('equal');\n```\n\n\n\n## Citing ``baryrat``\n\nIf you use this package in any published research, please cite the following publication where the package was first introduced:\n\n* C. Hofreither. **An algorithm for best rational approximation based on barycentric rational interpolation.**\n *Numerical Algorithms*, 88(1):365--388, 2021. [(doi)](https://doi.org/10.1007/s11075-020-01042-0)\n",
"bugtrack_url": null,
"license": null,
"summary": "A Python package for barycentric rational approximation",
"version": "2.1.2",
"project_urls": {
"Homepage": "https://github.com/c-f-h/baryrat",
"Issues": "https://github.com/c-f-h/baryrat/issues"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "8eb9952899429722c2f2fe243d58100b015dc142c8dbeed6824fc84231499654",
"md5": "1756ce8e0b44244821590a29446a30b0",
"sha256": "3eecb9ff7d31421951172a3fb618e26d40f746bc29cfdd02d71cd768927d42ea"
},
"downloads": -1,
"filename": "baryrat-2.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1756ce8e0b44244821590a29446a30b0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 18881,
"upload_time": "2025-02-18T23:00:46",
"upload_time_iso_8601": "2025-02-18T23:00:46.041087Z",
"url": "https://files.pythonhosted.org/packages/8e/b9/952899429722c2f2fe243d58100b015dc142c8dbeed6824fc84231499654/baryrat-2.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "cee400e84ceb5bb5311e433b41eb8cd5c26061d3a63a5682e102c0161bac5b3c",
"md5": "b213bfe8abfac60a954585c6d5e853af",
"sha256": "815de095b163765b7fa35b9850399e987d030671464c7d7732f97f37b8a8df72"
},
"downloads": -1,
"filename": "baryrat-2.1.2.tar.gz",
"has_sig": false,
"md5_digest": "b213bfe8abfac60a954585c6d5e853af",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 23846,
"upload_time": "2025-02-18T23:00:47",
"upload_time_iso_8601": "2025-02-18T23:00:47.925194Z",
"url": "https://files.pythonhosted.org/packages/ce/e4/00e84ceb5bb5311e433b41eb8cd5c26061d3a63a5682e102c0161bac5b3c/baryrat-2.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-18 23:00:47",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "c-f-h",
"github_project": "baryrat",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "baryrat"
}