-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a scaling issue when computing roots
- Loading branch information
1 parent
aaa3210
commit f7e49c6
Showing
5 changed files
with
86 additions
and
14 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
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
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,46 @@ | ||
import numpy as np | ||
import pytest | ||
from polyrat import * | ||
|
||
|
||
|
||
@pytest.mark.parametrize("Basis", | ||
[MonomialPolynomialBasis, | ||
LegendrePolynomialBasis, | ||
ChebyshevPolynomialBasis, | ||
HermitePolynomialBasis, | ||
LaguerrePolynomialBasis, | ||
ArnoldiPolynomialBasis]) | ||
@pytest.mark.parametrize("n", [2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20]) | ||
def test_wilkinson_roots(Basis, n): | ||
r""" Check root computation in Arnoldi polynomials | ||
""" | ||
|
||
if Basis in [LaguerrePolynomialBasis, HermitePolynomialBasis] and n>= 8: | ||
# These tests fail due to the ill-conditioning of this basis | ||
return | ||
|
||
true_roots = np.arange(1, n+1) | ||
|
||
def wilkinson(x): | ||
value = np.zeros(x.shape, dtype = np.complex) | ||
for i, xi in enumerate(x): | ||
value[i] = np.prod(xi - true_roots) | ||
return value | ||
|
||
# It is important that we sample at the roots to avoid the large | ||
# values the Wilkinson polynomial takes away from these points. | ||
X = np.arange(0, n+1, step = 0.1, dtype = np.float).reshape(-1,1) | ||
y = wilkinson(X) | ||
poly = PolynomialApproximation(n, Basis = Basis) | ||
poly.fit(X, y) | ||
roots = poly.roots().flatten() | ||
I = hungarian_sort(true_roots, roots) | ||
roots = roots[I] | ||
for tr, r, fr in zip(true_roots, roots, poly(roots.reshape(-1,1))): | ||
print(f'true root: {tr.real:+10.5e} {tr.imag:+10.5e}I \t root: {r.real:+10.5e} {r.imag:+10.5e} I \t abs fun value {np.abs(fr):10.5e}') | ||
|
||
err = sorted_norm(roots, true_roots, np.inf) | ||
print("error", err) | ||
assert err < 1e-7, "Error too large" | ||
|