Package: baryrat
Barycentric rational approximationThis 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 theAPI documentationfor an overview of
the available functions.FeaturesBest rational approximation using BRASILThe package implements the novel BRASIL algorithm for best rational approximation;
seethe paperorthe preprintto learn more.The following example computes the best uniform rational approximation of degree 5
to a given function in the interval [0, pi]:importnumpyasnpimportbaryratdeff(x):returnnp.sin(x)*np.exp(x)r=baryrat.brasil(f,[0,np.pi],5)The rational functionrcan then be evaluated at arbitrary nodes, its poles computed,
and more. See thedocumentationfor details.The AAA algorithmThe 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)A MATLAB implementation of this algorithm is contained inChebfun.  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 algorithmsThe package includes functions for polynomial interpolation, rational
interpolation with either fixed poles or fixed interpolation nodes,
Floater-Hormann interpolation, and more.Extended precision arithmeticFrombaryrat2.1 forward, most functions in the package support computing in extended precision
using thegmpy2package; linear algebra routines are provided
through theflamppackage.To enable this, first install theflamppackage:pip install flampThis will automatically installgmpy2as well if it is not yet installed.In your code, first set the desired number of decimal digits to compute with byimportflampflamp.set_dps(100)# compute with 100 decimal digits precisionArrays of numbers should be represented as numpy arrays with the object datatype
containinggmpy2floating point numbers. Some convenience functions to create
such arrays are provided inflamp.
For instance, useflamp.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 ause_mpflag for many functions, but it is only required to force
the use of extended precision even when the inputs are in double precision.Also theBarycentricRationalclass supports having its nodes, values, and
weights stored in extended precision and will operate accordingly, for instance
when computing the poles.InstallationThe 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 baryratUsageHere's an example of how to approximate a function in the interval [0,1]
using the AAA algorithm:importnumpyasnpfrombaryratimportaaaZ=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 termsmmax, it's also possible to specify
the error tolerancetol.  Both arguments work exactly as in the MATLAB
version.The returned objectris an instance of the classbaryrat.BarycentricRationaland can be called like a function. For instance,
you can compute the error onZlike this: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 likepol,res=r.polres()and the zeroes usingzer=r.zeros()Finally, the nodes, values and weights used for interpolation (calledzj,fjandwjin the original implementation) can be accessed as properties:r.nodesr.valuesr.weightsExample: approximating the complex exponential# create 9 interpolation nodes in a circlen=9nodes=exp(arange(n)/n*2j*pi)# interpolate the complex exp function as a degree (4,4) rational functionr=baryrat.interpolate_rat(nodes,exp(nodes))# compute poles and zerospoles,zer=r.poles(),r.zeros()# plot the approximation error and the nodes, poles and zerosfigsize(13.5,5)subplot(1,2,1)Y,X=ogrid[-2:2:100j,-2:2:100j]Z=X+1j*Ypcolormesh(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');CitingbaryratIf 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)