def test_legendre(): x, w = gauss_legendre(1, 17) assert [str(r) for r in x] == ['0'] assert [str(r) for r in w] == ['2.0000000000000000'] x, w = gauss_legendre(2, 17) assert [str(r) for r in x] == ['-0.57735026918962576', '0.57735026918962576'] assert [str(r) for r in w] == ['1.0000000000000000', '1.0000000000000000'] x, w = gauss_legendre(3, 17) assert [str(r) for r in x ] == ['-0.77459666924148338', '0', '0.77459666924148338'] assert [str(r) for r in w] == [ '0.55555555555555556', '0.88888888888888889', '0.55555555555555556' ] x, w = gauss_legendre(4, 17) assert [str(r) for r in x] == [ '-0.86113631159405258', '-0.33998104358485626', '0.33998104358485626', '0.86113631159405258' ] assert [str(r) for r in w] == [ '0.34785484513745386', '0.65214515486254614', '0.65214515486254614', '0.34785484513745386' ]
def test_legendre(): x, w = gauss_legendre(1, 17) assert [str(r) for r in x] == ['0'] assert [str(r) for r in w] == ['2.0000000000000000'] x, w = gauss_legendre(2, 17) assert [str(r) for r in x] == [ '-0.57735026918962576', '0.57735026918962576'] assert [str(r) for r in w] == [ '1.0000000000000000', '1.0000000000000000'] x, w = gauss_legendre(3, 17) assert [str(r) for r in x] == [ '-0.77459666924148338', '0', '0.77459666924148338'] assert [str(r) for r in w] == [ '0.55555555555555556', '0.88888888888888889', '0.55555555555555556'] x, w = gauss_legendre(4, 17) assert [str(r) for r in x] == [ '-0.86113631159405258', '-0.33998104358485626', '0.33998104358485626', '0.86113631159405258'] assert [str(r) for r in w] == [ '0.34785484513745386', '0.65214515486254614', '0.65214515486254614', '0.34785484513745386']
def test_legendre(): x, w = gauss_legendre(1, 17) assert [str(r) for r in x] == ["0"] assert [str(r) for r in w] == ["2.0000000000000000"] x, w = gauss_legendre(2, 17) assert [str(r) for r in x] == ["-0.57735026918962576", "0.57735026918962576"] assert [str(r) for r in w] == ["1.0000000000000000", "1.0000000000000000"] x, w = gauss_legendre(3, 17) assert [str(r) for r in x ] == ["-0.77459666924148338", "0", "0.77459666924148338"] assert [str(r) for r in w] == [ "0.55555555555555556", "0.88888888888888889", "0.55555555555555556", ] x, w = gauss_legendre(4, 17) assert [str(r) for r in x] == [ "-0.86113631159405258", "-0.33998104358485626", "0.33998104358485626", "0.86113631159405258", ] assert [str(r) for r in w] == [ "0.34785484513745386", "0.65214515486254614", "0.65214515486254614", "0.34785484513745386", ]
def gaussquad2D(f, var1, var2, n=3, n_digits=8): X, W = gauss_legendre(n, n_digits) res = 0 for i in range(len(X)): for j in range(len(X)): res += W[i] * W[j] * f.subs([(var2, X[i]), (var1, X[j])]) res = sym.simplify(res) return res
def test_legendre_precise(): x, w = gauss_legendre(3, 40) assert [str(r) for r in x] == \ ['-0.7745966692414833770358530799564799221666', '0', '0.7745966692414833770358530799564799221666'] assert [str(r) for r in w] == \ ['0.5555555555555555555555555555555555555556', '0.8888888888888888888888888888888888888889', '0.5555555555555555555555555555555555555556']
def test_legendre_precise(): x, w = gauss_legendre(3, 40) assert [str(r) for r in x] == [ "-0.7745966692414833770358530799564799221666", "0", "0.7745966692414833770358530799564799221666", ] assert [str(r) for r in w] == [ "0.5555555555555555555555555555555555555556", "0.8888888888888888888888888888888888888889", "0.5555555555555555555555555555555555555556", ]
def quadintegration(f, x, a, b, n): t, w = gauss_legendre(n, 8) dt = a - 1 inext = 0. for i in range(0, n): inext += N(f.subs(x, t[i] - dt) * w[i]) iprev = inext * 10 # while(abs(iprev - inext) > eps): # iprev = inext # inext = 0. # n *= 2 # t, w = gauss_legendre(n, 8) # for i in range(0,n): # inext += N(f.subs(x,t[i] - dt) * w[i]) # print(n, inext) return inext
def calculatenorms(originkern): w = originkern n = 20 tg, wg = gauss_legendre(n, 8) dt = -1 inext = 0. for i in range(0, n): inext += N(w.subs(q, tg[i] - dt) * wg[i]) c1D = 1. / (2 * inext) inext = 0. for i in range(0, n): inext += N((2 * pi * q * w).subs(q, tg[i] - dt) * wg[i]) c2D = 1. / inext inext = 0. for i in range(0, n): inext += N((4 * pi * q * q * w).subs(q, tg[i] - dt) * wg[i]) c3D = 1. / inext return [c1D, c2D, c3D]
def _quad_coeffs_hq(M, collocation_type, digits=20): if M == 1: x = np.array([0.0]) w = np.array([2.0]) elif collocation_type == "gauss_legendre": from sympy.integrals.quadrature import gauss_legendre x, w = gauss_legendre(M, digits) elif collocation_type == "gauss_lobatto": from sympy.integrals.quadrature import gauss_lobatto x, w = gauss_lobatto(M, digits) elif collocation_type == "gauss_hermite": from sympy.integrals.quadrature import gauss_hermite x, w = gauss_hermite(M, 30) x = np.array(x, dtype=float) w = np.array(w, dtype=float) elif collocation_type == "gauss_jacobi": from sympy.integrals.quadrature import gauss_jacobi x, w = gauss_jacobi(M, 0, 0, 30) x = np.array(x, dtype=float) w = np.array(w, dtype=float) elif collocation_type == "gauss_chebyshev_u": from sympy.integrals.quadrature import gauss_chebyshev_u x, w = gauss_chebyshev_u(M, 30) x = np.array(x, dtype=float) w = np.array(w, dtype=float) elif collocation_type == "gauss_chebyshev_t": from sympy.integrals.quadrature import gauss_chebyshev_t x, w = gauss_chebyshev_t(M, 30) x = np.array(x, dtype=float) w = np.array(w, dtype=float) else: raise Exception("Unknown collocation method '" + str(collocation_type) + "'") assert len(x) == M return x, w
CoeffMatrix = np.matrix(CoeffMatrix) print linalg.inv(CoeffMatrix) Jacobian = MatrixSymbol('J', len(CarVals), len(Vals)) Jacobian = Matrix(Jacobian) for i in xrange(len(CarVals)): for j in xrange(len(Vals)): exec(("Jacobian[i,j] = diff(" + CarVals[i] + ",'" + Vals[j] + "')")) #print Jacobian GOrder = int(math.ceil((Order + 1) / 2.0)) # now that we have the jacobian, we can use gauss quadrature to evaluate the determinant at discrete points to get the exact answer for up polynomials of order 2n-1, so if we have an element of order n we must have quadrature of at lease (n + 1) / 2 Locations, Weights = gauss_legendre(GOrder, 15) GSize = [] for i in xrange(Dims): GSize.append(GOrder) EvalPts = np.zeros((len(Locations)**len(Vals), len(Vals))) EvalWeights = np.ones(EvalPts.shape[0]) for i in xrange(EvalPts.shape[0]): Sub = np.unravel_index(i, GSize) for j in xrange(len(Vals)): EvalPts[i, j] = Locations[Sub[j]] EvalWeights[i] = EvalWeights[i] * Weights[Sub[j]] print Jacobian
CoeffMatrix = np.matrix(CoeffMatrix) print linalg.inv(CoeffMatrix) Jacobian = MatrixSymbol('J',len(CarVals),len(Vals)) Jacobian = Matrix(Jacobian) for i in xrange(len(CarVals)): for j in xrange(len(Vals)): exec(("Jacobian[i,j] = diff(" + CarVals[i] + ",'" + Vals[j] + "')")) #print Jacobian GOrder = int(math.ceil((Order + 1) / 2.0)) # now that we have the jacobian, we can use gauss quadrature to evaluate the determinant at discrete points to get the exact answer for up polynomials of order 2n-1, so if we have an element of order n we must have quadrature of at lease (n + 1) / 2 Locations, Weights = gauss_legendre(GOrder, 15) GSize = [] for i in xrange(Dims): GSize.append(GOrder) EvalPts = np.zeros((len(Locations) ** len(Vals),len(Vals))) EvalWeights = np.ones(EvalPts.shape[0]) for i in xrange(EvalPts.shape[0]): Sub = np.unravel_index(i,GSize) for j in xrange(len(Vals)): EvalPts[i,j] = Locations[Sub[j]] EvalWeights[i] = EvalWeights[i] * Weights[Sub[j]] print Jacobian