def test_build_sets(): """ Tests whether the Gaussian quadrature method is properly storing the orders that have already been computed """ x, w = gaussian_quad.gaussxw(3) x2, w2 = gaussian_quad.gaussxw(12) np.testing.assert_almost_equal(x, gaussian_quad.x_set[3]) np.testing.assert_almost_equal(w, gaussian_quad.w_set[3]) np.testing.assert_almost_equal(x2, gaussian_quad.x_set[12]) np.testing.assert_almost_equal(w2, gaussian_quad.w_set[12])
def test_interp_telles(): # The problem I solve: # exact is from mathematica sing_pt = 1.005 denom = lambda x: (sing_pt - x)**2 numer = lambda x: x**3 f = lambda x: numer(x) / denom(x) exact_f = lambda s: (s * (-4 + 6 * s ** 2 - 3 * s * (-1 + s ** 2) * \ (log((-1 - s) / (1 - s))))) / (-1 + s ** 2) exact = exact_f(sing_pt) # Solved with standard Telles quadrature x_nearest = 1.0 D = sing_pt - 1.0 N = 14 [tx, tw] = telles_quasi_singular(N, x_nearest, D) est_telles = np.sum(f(tx) * tw) # Solved with gauss quadrature [gx, gw] = gaussxw(N) est_gauss = np.sum(f(gx) * gw) # Solved with interpolation and Telles quadrature # X = Interpolation Points X = gx # Y = Value of function at the interpolation points Y = f(gx) * denom(gx) # WARNING, WARNING, WARNING: This implementation of lagrange interpolation # is super unstable. I just downloaded it from somewhere online. A # reimplementation using barycentric Lagrange interpolation is necessary to # go above N = appx 20. P = spi.lagrange(X, Y) est_interp_telles = sum(P(tx) / denom(tx) * tw) np.testing.assert_almost_equal(est_telles, est_interp_telles)
def test_interp_telles(): # The problem I solve: # exact is from mathematica sing_pt = 1.005; denom = lambda x: (sing_pt - x) ** 2; numer = lambda x: x ** 3; f = lambda x: numer(x) / denom(x); exact_f = lambda s: (s * (-4 + 6 * s ** 2 - 3 * s * (-1 + s ** 2) * \ (log((-1 - s) / (1 - s))))) / (-1 + s ** 2) exact = exact_f(sing_pt) # Solved with standard Telles quadrature x_nearest = 1.0; D = sing_pt - 1.0; N = 14; [tx, tw] = telles_quasi_singular(N, x_nearest, D); est_telles = np.sum(f(tx) * tw) # Solved with gauss quadrature [gx, gw] = gaussxw(N) est_gauss = np.sum(f(gx) * gw) # Solved with interpolation and Telles quadrature # X = Interpolation Points X = gx; # Y = Value of function at the interpolation points Y = f(gx) * denom(gx); # WARNING, WARNING, WARNING: This implementation of lagrange interpolation # is super unstable. I just downloaded it from somewhere online. A # reimplementation using barycentric Lagrange interpolation is necessary to # go above N = appx 20. P = spi.lagrange(X, Y) est_interp_telles = sum(P(tx) / denom(tx) * tw) np.testing.assert_almost_equal(est_telles, est_interp_telles)
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_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_anotherLogRDouble_G11(): f = lambda x, y: (1 / (3 * np.pi)) *\ np.log(1.0 / np.abs(x - y)) * x * y exact = 1 / (3 * np.pi) gx, gw = gaussxw(75) sum = 0.0 for (pt, wt) in zip(gx, gw): x, w = telles_singular(76, pt) g = lambda x: f(x, pt) sum += np.sum(g(x) * w * wt) np.testing.assert_almost_equal(exact, sum, 5)
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_quasi_singular(): #test telles quasi-singular quadrature with the example on page 966 # sing_pt = 1.08 sing_pt = 1.004 g = lambda x: (sing_pt - x) ** -2 # for sing_pt = 1.08 # exact = 12.0192 # for sing_pt = 1.004 exact = 249.500998 x_nearest = 1.0 D = sing_pt - 1.0 N = 20 [tx, tw] = telles_quasi_singular(N, x_nearest, D) est_telles = np.sum(g(tx) * tw) [gx, gw] = gaussxw(N) est_gauss = np.sum(g(gx) * gw) gauss_error = abs(est_gauss - exact) / exact * 100 telles_error = abs(est_telles - exact) / exact * 100 assert(telles_error < gauss_error / 1000)