def test_QuadGauss(): f = lambda x: 3 * x ** 2 F = lambda x: x ** 3 exact = F(1) - F(0) x, w = gaussian_quad.gaussxw(2) x, w = map_pts_wts(x, w, 0, 1) est = np.sum(f(x) * w) np.testing.assert_almost_equal(exact, est)
def test_QuadOneOverR_1(): f = lambda x: 1 / (x - 0.4) exact = np.log(3.0 / 2.0) mapped_x0 = map_singular_pt(0.4, 0.0, 1.0) x, w = piessens(2, mapped_x0, nonsingular_N=10) qx, qw = map_pts_wts(x, w, 0.0, 1.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(exact, est)
def test_quadlogr3(): f = lambda x: x ** 2 * np.log(np.abs(x - 3.9)) exact = -18.459 mapped_x0 = map_singular_pt(3.9, 3, 4) tx, tw = telles_singular(50, mapped_x0) tx, tw = map_pts_wts(tx, tw, 3, 4) est = np.sum(f(tx) * tw) np.testing.assert_almost_equal(exact, est, 4)
def test_QuadOneOverR_1(): f = lambda x: 1 / (x - 0.4) exact = np.log(3.0 / 2.0) mapped_x0 = map_singular_pt(0.4, 0.0, 1.0) x, w = piessens(2, mapped_x0, nonsingular_N = 10) qx, qw = map_pts_wts(x, w, 0.0, 1.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(exact, est)
def test_QuadGauss(): f = lambda x: 3 * x**2 F = lambda x: x**3 exact = F(1) - F(0) x, w = gaussian_quad.gaussxw(2) x, w = map_pts_wts(x, w, 0, 1) est = np.sum(f(x) * w) np.testing.assert_almost_equal(exact, est)
def test_QuadOneOverR_4(): f = lambda x: np.exp(x) / (x - 0.8) exact = -1.13761642399 mapped_x0 = map_singular_pt(0.8, 0.0, 1.0) x, w = piessens(2, mapped_x0, nonsingular_N = 20) qx, qw = map_pts_wts(x, w, 0.0, 1.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(exact, est)
def test_QuadOneOverR_4(): f = lambda x: np.exp(x) / (x - 0.8) exact = -1.13761642399 mapped_x0 = map_singular_pt(0.8, 0.0, 1.0) x, w = piessens(2, mapped_x0, nonsingular_N=20) qx, qw = map_pts_wts(x, w, 0.0, 1.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(exact, est)
def test_lobatto(): f = lambda x: 12 * x ** 11 F = lambda x: x ** 12 exact = F(1) - F(0) x, w = lobatto_quad(7) x, w = map_pts_wts(x, w, 0, 1) assert(len(x) == 7) est = np.sum(f(x) * w) np.testing.assert_almost_equal(exact, est)
def test_lobatto(): f = lambda x: 12 * x**11 F = lambda x: x**12 exact = F(1) - F(0) x, w = lobatto_quad(7) x, w = map_pts_wts(x, w, 0, 1) assert (len(x) == 7) est = np.sum(f(x) * w) np.testing.assert_almost_equal(exact, est)
def test_quadlogr2(): f = lambda x: x ** 2 * np.log(np.abs(x - 0.9)) exact = -0.764714 mapped_x0 = map_singular_pt(0.9, 0, 1) tx, tw = telles_singular(40, mapped_x0) tx, tw = map_pts_wts(tx, tw, 0, 1) est = np.sum(f(tx) * tw) np.testing.assert_almost_equal(exact, est, 4)
def test_QuadLogR(): f = lambda x: np.log(np.abs(x - 0.5)) exact = -1.0 - np.log(2.0) mapped_x0 = map_singular_pt(0.5, 0, 1) tx, tw = telles_singular(50, mapped_x0) tx, tw = map_pts_wts(tx, tw, 0, 1) est = np.sum(f(tx) * tw) np.testing.assert_almost_equal(exact, est, 4)
def test_piessens_4_5(): f = lambda x: np.exp(x - 4) / (x - 4.2) exact = 3.139062607254266 mapped_x0 = map_singular_pt(4.2, 4.0, 5.0) x, w = piessens(20, mapped_x0, nonsingular_N=50) qx, qw = map_pts_wts(x, w, 4.0, 5.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(exact, est)
def test_QuadOneOverR_5(): f = lambda x: np.exp(x) / (x - 0.2) exact = 3.139062607254266 mapped_x0 = map_singular_pt(0.2, 0.0, 1.0) x, w = piessens(2, mapped_x0, nonsingular_N = 50) qx, qw = map_pts_wts(x, w, 0.0, 1.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(exact, est)
def test_piessens_4_5(): f = lambda x: np.exp(x - 4) / (x - 4.2) exact = 3.139062607254266 mapped_x0 = map_singular_pt(4.2, 4.0, 5.0) x, w = piessens(20, mapped_x0, nonsingular_N = 50) qx, qw = map_pts_wts(x, w, 4.0, 5.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(exact, est)
def test_QuadOneOverR_5(): f = lambda x: np.exp(x) / (x - 0.2) exact = 3.139062607254266 mapped_x0 = map_singular_pt(0.2, 0.0, 1.0) x, w = piessens(2, mapped_x0, nonsingular_N=50) qx, qw = map_pts_wts(x, w, 0.0, 1.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(exact, est)
def test_QuadOneOverR_2(): # Example 1 from Piessens g = lambda x: np.exp(x) / x f = lambda x: 2 * g((2 * x) - 1) exact = 2.11450175 mapped_x0 = map_singular_pt(0.5, 0.0, 1.0) x, w = piessens(8, mapped_x0, nonsingular_N = 10) qx, qw = map_pts_wts(x, w, 0.0, 1.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(exact, est)
def test_QuadOneOverR_2(): # Example 1 from Piessens g = lambda x: np.exp(x) / x f = lambda x: 2 * g((2 * x) - 1) exact = 2.11450175 mapped_x0 = map_singular_pt(0.5, 0.0, 1.0) x, w = piessens(8, mapped_x0, nonsingular_N=10) qx, qw = map_pts_wts(x, w, 0.0, 1.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(exact, est)
def test_QuadOneOverR_3(): # Example 2 from Piessens g = lambda x: np.exp(x) / (np.sin(x) - np.cos(x)) f = lambda x: np.pi / 2.0 * g(np.pi / 2.0 * x) exact = 2.61398312 # Piessens estimate derived with a two pt rule. piessens_est = 2.61398135 mapped_x0 = map_singular_pt(0.5, 0.0, 1.0) x, w = piessens(2, mapped_x0) qx, qw = map_pts_wts(x, w, 0.0, 1.0) est = np.sum(f(qx) * qw) np.testing.assert_almost_equal(piessens_est, est)
def test_gauss(): """ Exact values retrieved from the wikipedia page on Gaussian Quadrature The main function has been tested by the original author for a wide range of orders. But, this is just to check everything is still working properly """ x, w = gaussian_quad.gaussxw(3) x, w = map_pts_wts(x, w, -1.0, 1.0) np.testing.assert_almost_equal(x[0], sqrt(3.0 / 5.0)) np.testing.assert_almost_equal(x[1], 0.0) np.testing.assert_almost_equal(x[2], -sqrt(3.0 / 5.0)) np.testing.assert_almost_equal(w[0], (5.0 / 9.0)) np.testing.assert_almost_equal(w[1], (8.0 / 9.0)) np.testing.assert_almost_equal(w[2], (5.0 / 9.0))
def test_mapped_recursive_quad2(): mapped_ay = map_singular_pt(0.2, 0.0, 1.0) mapped_by = map_distance_to_interval(0.3, 0.0, 1.0) moments = modified_moments(rl1, 10, mapped_ay, mapped_by) x, w = recursive_quad(moments) x, w = map_pts_wts(x, w, 0.0, 1.0) w = map_weights_by_inv_power(w, 1.0, 0.0, 1.0) exact = [2.332556553293539, 0.9603565576440610, 0.5636909785950152, \ 0.3894662160471265, 0.2949533988361775, 0.2365588120191993, \ 0.1971850086215070, 0.1689379319389373, 0.1477219765628722, \ 0.1312177383160300] for i in range(len(exact)): est = np.sum(w * x ** i) np.testing.assert_almost_equal(exact[i], est, 9)
def test_mapped_recursive_quad2(): mapped_ay = map_singular_pt(0.2, 0.0, 1.0) mapped_by = map_distance_to_interval(0.3, 0.0, 1.0) moments = modified_moments(rl1, 10, mapped_ay, mapped_by) x, w = recursive_quad(moments) x, w = map_pts_wts(x, w, 0.0, 1.0) w = map_weights_by_inv_power(w, 1.0, 0.0, 1.0) exact = [2.332556553293539, 0.9603565576440610, 0.5636909785950152, \ 0.3894662160471265, 0.2949533988361775, 0.2365588120191993, \ 0.1971850086215070, 0.1689379319389373, 0.1477219765628722, \ 0.1312177383160300] for i in range(len(exact)): est = np.sum(w * x**i) np.testing.assert_almost_equal(exact[i], est, 9)
def test_mapped_recursive_quad(): mapped_ay = map_singular_pt(0.5, 1.0, 2.0) mapped_by = map_distance_to_interval(0.5, 1.0, 2.0) moments = modified_moments(rl1, 10, mapped_ay, mapped_by) x, w = recursive_quad(moments) x, w = map_pts_wts(x, w, 1.0, 2.0) w = map_weights_by_inv_power(w, 1.0, 1.0, 2.0) exact = [0.9370728722124352, 1.342568485003826, 2.000243585190683, \ 3.091829672374373, 4.940760583752816, 8.128175556459884, \ 13.70706664710914, 23.59959784014733, 41.33761425072659, \ 73.44841816566141] for i in range(10): est = np.sum(w * x ** i) np.testing.assert_almost_equal(exact[i], est, 10)
def test_mapped_recursive_quad(): mapped_ay = map_singular_pt(0.5, 1.0, 2.0) mapped_by = map_distance_to_interval(0.5, 1.0, 2.0) moments = modified_moments(rl1, 10, mapped_ay, mapped_by) x, w = recursive_quad(moments) x, w = map_pts_wts(x, w, 1.0, 2.0) w = map_weights_by_inv_power(w, 1.0, 1.0, 2.0) exact = [0.9370728722124352, 1.342568485003826, 2.000243585190683, \ 3.091829672374373, 4.940760583752816, 8.128175556459884, \ 13.70706664710914, 23.59959784014733, 41.33761425072659, \ 73.44841816566141] for i in range(10): est = np.sum(w * x**i) np.testing.assert_almost_equal(exact[i], est, 10)