Skip to content

Commit

Permalink
Added testing for non-deflating version
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffrey-hokanson committed Sep 8, 2020
1 parent 66911fa commit 3929b6c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion polyrat/lagrange.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import warnings
import numpy as np
from scipy.linalg import eig, eigvals, hessenberg
from .basis import PolynomialBasis



def lagrange_roots(nodes, weights, coef, deflation = True):
r""" Compute the roots of a Lagrange polynomial
Expand Down
4 changes: 2 additions & 2 deletions polyrat/polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ def __init__(self, X, y):
self.basis = LagrangePolynomialBasis(X)
self.coef = np.copy(y)

def roots(self, **kwargs):
return self.basis.roots(self.coef, **kwargs)
def roots(self, *args, **kwargs):
return self.basis.roots(self.coef, *args, **kwargs)
15 changes: 10 additions & 5 deletions tests/test_lagrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

@pytest.mark.parametrize("n", [5, 10, 15, 20])
@pytest.mark.parametrize("ang", [1, (1 +1j)/np.sqrt(2)])
def test_roots_wilkinson(n, ang):
@pytest.mark.parametrize("deflate", [True, False])
def test_roots_wilkinson(n, ang, deflate):
r""" Test computation of barycentric polynomial roots using the Wilkinson polynomial
This test is the first one
"""
if deflate:
tol = 1e-12
else:
tol = 1e-10

true_roots = ang* np.arange(1, n+1)
def wilkinson(x):
value = np.zeros(x.shape, dtype = np.complex)
Expand All @@ -21,7 +27,7 @@ def wilkinson(x):
# First try with basis sampling at nodes
X = ang*np.arange(0,n+1)
lpi = LagrangePolynomialInterpolant(X, wilkinson(X))
roots = lpi.roots()
roots = lpi.roots(deflate)
print(roots)
print(true_roots)
err = sorted_norm(roots, true_roots, np.inf)
Expand All @@ -32,15 +38,14 @@ def wilkinson(x):
#X = (n+1)*np.exp(1j*np.linspace(0,2*np.pi, n+1, endpoint = False))
X = ang*np.arange(0.5, 1.5 +n)
lpi = LagrangePolynomialInterpolant(X, wilkinson(X))
roots = lpi.roots()
roots = lpi.roots(deflate)
print(roots)
print(true_roots)
print(true_roots - roots)
err = sorted_norm(roots, true_roots, np.inf)
print(err)
assert err < 1e-10, "Could not determine roots accurately"



if __name__ == '__main__':
test_roots_wilkinson(20, 1+1j)
test_roots_wilkinson(20, 1, True)

0 comments on commit 3929b6c

Please sign in to comment.