def test_orthonormality_jacobi_1d(alpha, beta, ebound): """Verify that the Jacobi polymials are orthogonal in 1D.""" from modepy.modes import jacobi from modepy.quadrature.jacobi_gauss import LegendreGaussQuadrature max_n = 10 quad = LegendreGaussQuadrature(4 * max_n) # overkill... class WeightFunction: def __init__(self, alpha, beta): self.alpha = alpha self.beta = beta def __call__(self, x): return (1 - x)**self.alpha * (1 + x)**self.beta from functools import partial jac_f = [partial(jacobi, alpha, beta, n) for n in range(max_n)] wf = WeightFunction(alpha, beta) maxerr = 0 for i, fi in enumerate(jac_f): for j, fj in enumerate(jac_f): result = quad(lambda x: wf(x) * fi(x) * fj(x)) if i == j: true_result = 1 else: true_result = 0 err = abs(result - true_result) maxerr = max(maxerr, err) if abs(result - true_result) > ebound: print(("bad", alpha, beta, i, j, abs(result - true_result))) assert abs(result - true_result) < ebound
def test_gauss_quadrature(backend): from modepy.quadrature.jacobi_gauss import LegendreGaussQuadrature for s in range(9 + 1): quad = LegendreGaussQuadrature(s, backend) for deg in range(quad.exact_to + 1): def f(x): return x**deg i_f = quad(f) i_f_true = 1 / (deg+1) * (1 - (-1)**(deg + 1)) err = abs(i_f - i_f_true) assert err < 2.0e-15, (s, deg, err, i_f, i_f_true)
def test_gauss_quadrature(): from modepy.quadrature.jacobi_gauss import LegendreGaussQuadrature for s in range(9 + 1): cub = LegendreGaussQuadrature(s) for deg in range(2 * s + 1 + 1): def f(x): return x**deg i_f = cub(f) i_f_true = 1 / (deg + 1) * (1 - (-1)**(deg + 1)) err = abs(i_f - i_f_true) assert err < 2e-15
def test_warp(): """Check some assumptions on the node warp factor calculator""" n = 17 from functools import partial def wfc(x): return nd.warp_factor(n, np.array([x]), scaled=False)[0] assert abs(wfc(-1)) < 1e-12 assert abs(wfc(1)) < 1e-12 from modepy.quadrature.jacobi_gauss import LegendreGaussQuadrature lgq = LegendreGaussQuadrature(n) assert abs(lgq(partial(nd.warp_factor, n, scaled=False))) < 6e-14
def test_transformed_quadrature(): """Test 1D quadrature on arbitrary intervals""" def gaussian_density(x, mu, sigma): return 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-(x - mu)**2 / (2 * sigma**2)) from modepy.quadrature import Transformed1DQuadrature from modepy.quadrature.jacobi_gauss import LegendreGaussQuadrature mu = 17 sigma = 12 tq = Transformed1DQuadrature(LegendreGaussQuadrature(20), mu - 6 * sigma, mu + 6 * sigma) result = tq(lambda x: gaussian_density(x, mu, sigma)) assert abs(result - 1) < 1.0e-9
def test_warp(): """Check some assumptions on the node warp factor calculator""" n = 17 from modepy.nodes import warp_factor from functools import partial from modepy.tools import accept_scalar_or_vector wfc = accept_scalar_or_vector(1, 1)(partial(warp_factor, 17, scaled=False)) assert abs(wfc(-1)) < 1e-12 assert abs(wfc(1)) < 1e-12 from modepy.quadrature.jacobi_gauss import LegendreGaussQuadrature lgq = LegendreGaussQuadrature(n) assert abs(lgq(wfc)) < 6e-14
def __init__(self, N, dims, backend=None): # noqa: N803 from modepy.quadrature.jacobi_gauss import LegendreGaussQuadrature super().__init__([LegendreGaussQuadrature(N, backend=backend)] * dims)