function [r,pol,res,zer,err,S] = aaai(f,deg,nl,tol,mero,plt)
%AAAI   Rational approximation on imaginary axis or right half-plane.
%   [r,pol,res,zer,err,S] = aaai(f,deg,nlawson,tol,mero)
%Examples:
%   [r,pol] = aaai(@(z) 1./((z+2).*(z+3)))
%   [r,pol] = aaai(@(z) 1./(sqrt(.1+2i+z).*sqrt(.1-2i+z)));
%   r = aaai(@(z) exp(z.^2),150,0,1e-13,1);
 
if nargin<2, deg = 150; end                    % arg 2: (max) degree
if nargin<3, nl = 0; end                       % arg 3: Lawson steps, eg 20
if nargin<4, tol = 1e-13; end                  % arg 4: relative tolerance
if nargin<5, mero = 0; end                     % arg 5: allow poles in RHP
if nargin<6, plt = 1; end                      % arg 6: plt=0 stops plotting
M = 1.207;                                     % arbitrary choice
zw = @(w) min(1e50i,M*(1+w)./(1-w));
wz = @(z) (z-M)./(z+M);
fw = @(w) f(zw(w));
[rw,polw,resw,zerw,err,Sw] = aaaz(fw,deg,nl,tol,mero,1);
r = @(z) rw(wz(z));
pol = zw(polw); zer = zw(zerw);
res = 2*M*resw./(1-polw).^2;
S = 1i*imag(zw(Sw));
