-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1708f6d
commit dd2d9f3
Showing
6 changed files
with
355 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import numpy as np | ||
from polyrat import * | ||
from pgf import PGF | ||
from scipy.special import jv | ||
|
||
data = [] | ||
|
||
# Two real-valued input problems | ||
|
||
# Absolute value test problem | ||
M = int(2e3) | ||
X = np.linspace(-1,1,M).reshape(-1,1) | ||
y = np.abs(X).flatten() | ||
data += [['abs', X, y]] | ||
|
||
# Exponential (NST18: Sec. 6.8) | ||
M = 4000 | ||
X = -np.logspace(-3, 4, M).reshape(-1,1) | ||
y = np.exp(X.flatten()) | ||
data += [['exp', X, y]] | ||
|
||
# Two complex-valued input problems | ||
|
||
# Tangent | ||
M = 1000 | ||
X = np.exp(1j*np.linspace(0, 2*np.pi, M, endpoint = False)).reshape(-1,1) | ||
y = np.tan(256*X.flatten()) | ||
data += [['tan256', X, y]] | ||
|
||
# Bessel function (NST18 Fig 6.5) | ||
# This converges too fast | ||
M = 2000 | ||
np.random.seed(0) | ||
X = 10*np.random.rand(M) + 2j*(np.random.rand(M) - 0.5) | ||
y = 1./jv(0, X) | ||
data += [['bessel', X.reshape(-1,1), y]] | ||
|
||
# Log example: NST18 Fig 6.2; | ||
# note rate is independent of offset, so shinking does not harm convergence | ||
M = int(2e3) | ||
X = np.exp(1j*np.linspace(0, 2*np.pi, M, endpoint = False)).reshape(-1,1) | ||
y = np.log(1.1 - X.flatten()) | ||
data += [['log11', X, y]] | ||
|
||
|
||
data = data[2:3] | ||
|
||
# Range of parameters | ||
mns = [(k,k) for k in range(2, 51, 1)] | ||
|
||
|
||
|
||
for name, X, y in data: | ||
|
||
err_isk = np.zeros(len(mns)) | ||
err_iskr = np.zeros(len(mns)) | ||
err_aaa = np.zeros(len(mns)) | ||
|
||
for k, (m, n) in enumerate(mns): | ||
|
||
print(f'\n======== AAA ({m},{m}) | {name} =======\n') | ||
aaa = AAARationalApproximation(degree = m) | ||
aaa.fit(X, y) | ||
err_aaa[k] = np.linalg.norm(aaa(X) - y, 2)/np.linalg.norm(y,2) | ||
|
||
|
||
print(f'\n======== SK Rebase ({m},{n}) | {name} =======\n') | ||
sk = SKRationalApproximation(m,n, verbose = True, rebase = True, refine = False, maxiter = 10) | ||
sk.fit(X,y) | ||
|
||
err_isk[k] = np.linalg.norm(sk(X) - y, 2)/np.linalg.norm(y, 2) | ||
|
||
sk.refine(X, y) | ||
err_iskr[k] = np.linalg.norm(sk(X) - y, 2)/np.linalg.norm(y, 2) | ||
|
||
print(f"original {err_isk[k]:10.3e}; improved {err_iskr[k]:10.3e}") | ||
|
||
|
||
pgf = PGF() | ||
pgf.add('m', [mn[0] for mn in mns]) | ||
pgf.add('n', [mn[1] for mn in mns]) | ||
pgf.add('err_isk', err_isk) | ||
pgf.add('err_iskr', err_iskr) | ||
pgf.add('err_aaa', err_aaa) | ||
|
||
pgf.write(f'data/fig_scalar_{name}.dat') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import numpy as np | ||
import scipy.linalg | ||
from iterprinter import IterationPrinter | ||
|
||
|
||
def eval_barcentric(xeval, x, y, I, a, b): | ||
""" | ||
Parameters | ||
---------- | ||
xeval: np.array | ||
Locations to evaluate the function | ||
x: np.array | ||
input coordinates for fit | ||
""" | ||
|
||
with np.errstate(divide='ignore',invalid='ignore'): | ||
A = np.hstack([ 1. /(xeval - xi) for xi in zip(x[I])]) | ||
# For the rows that have a divide by zero error, | ||
# we replace with value corresponding to the limit | ||
for row in np.argwhere(~np.all(np.isfinite(A), axis = 1)): | ||
A[row] = np.zeros(A.shape[1]) | ||
A[row, np.argmin(np.abs(xeval[row] - x[I])).flatten()] = 1 | ||
|
||
num = np.einsum('ij,j...->i...', A, a) | ||
denom = np.einsum('ij,j->i', A,b) | ||
reval = np.einsum('i...,i->i...', num, 1./denom) | ||
|
||
return reval | ||
|
||
|
||
|
||
def eval_aaa(xeval, x, y, I, b): | ||
""" | ||
Parameters | ||
---------- | ||
xeval: np.array | ||
Locations to evaluate the function | ||
x: np.array | ||
input coordinates for fit | ||
""" | ||
xeval = xeval.reshape(-1,1) | ||
with np.errstate(divide='ignore',invalid='ignore'): | ||
A = np.hstack([ bi /(xeval - xi) for bi, xi in zip(b, x[I])]) | ||
# For the rows that have a divide by zero error, | ||
# we replace with value corresponding to the limit | ||
for row in np.argwhere(~np.all(np.isfinite(A), axis = 1)): | ||
A[row] = np.zeros(A.shape[1]) | ||
A[row, np.argmin(np.abs(xeval[row] - x[I])).flatten()] = 1 | ||
|
||
num = np.einsum('ij,j...->i...', A, y[I]) | ||
denom = np.sum(A, axis = 1) | ||
reval = np.einsum('i...,i->i...', num, 1./denom) | ||
|
||
return reval | ||
|
||
|
||
|
||
|
||
def _build_cauchy(x,y): | ||
return 1./(np.tile(x.reshape(-1,1), (1,len(y))) - np.tile(y.reshape(1,-1), (len(x),1))) | ||
|
||
|
||
def aaa(x, y, degree = None, tol = None, verbose = True): | ||
r""" A vector-valued Adaptive Anderson-Antoulas implementation | ||
Parameters | ||
---------- | ||
x: np.array (M,) | ||
input coordinates | ||
y: np.array (M,...) | ||
output coordinates | ||
""" | ||
|
||
assert not ((degree is None) and (tol is None)), "One or both of 'degree' and 'tol' must be specified" | ||
|
||
if degree is None: | ||
degree = len(x)//2 - 1 | ||
|
||
|
||
mismatch = y | ||
I = np.zeros(len(y), dtype = np.bool) # Index of x values used as barycentric nodes | ||
|
||
if verbose: | ||
printer = IterationPrinter(it = '4d', res = '20.16e', cond = '8.3e') | ||
printer.print_header(it = 'iter', res = 'Frobenius norm residual', cond = 'condition number') | ||
|
||
for it in range(degree+1): | ||
# TODO: Is sum best? or how about maximum? | ||
residual = np.sum(np.abs(mismatch), axis = tuple(range(1,len(y[0].shape)+1))) | ||
residual[I] = 0 # zero out residual at nodes that have already been selected | ||
|
||
Inew = np.argmax(residual) | ||
I[Inew] = True | ||
|
||
# Construct the Cauchy matrix | ||
C = _build_cauchy(x[~I], x[I]) | ||
|
||
# Build the Loewner matrix associated with each input | ||
Lten = [] | ||
for idx in np.ndindex(y[0].shape): | ||
Lten.append( (C.T * y[(~I,*idx)]).T - C*y[(I,*idx)] ) | ||
L = np.vstack(Lten) | ||
|
||
# Compute coefficients for denominator polynomial | ||
U, s, VH = scipy.linalg.svd(L, full_matrices = False, compute_uv = True, overwrite_a = True) | ||
b = VH.conj().T[:,-1] | ||
if len(s) >= 2: | ||
with np.errstate(divide='ignore',invalid='ignore'): | ||
cond = s[0]*np.sqrt(2)/(s[-2] - s[-1]) | ||
else: | ||
cond = None | ||
|
||
mismatch = y - eval_aaa(x, x, y, I, b) | ||
res_norm = np.sqrt(np.sum(np.abs(mismatch)**2)) | ||
|
||
if verbose: | ||
printer.print_iter(it=it, res = res_norm, cond = cond) | ||
|
||
if tol is not None: | ||
if tol > res_norm: | ||
if verbose: print("terminated due to small residual") | ||
break | ||
|
||
return I, b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.