def test_hermite_poly(): raises(ValueError, "hermite_poly(-1, x)") assert hermite_poly(1, x, polys=True) == Poly(2 * x) assert hermite_poly(0, x) == 1 assert hermite_poly(1, x) == 2 * x assert hermite_poly(2, x) == 4 * x**2 - 2 assert hermite_poly(3, x) == 8 * x**3 - 12 * x assert hermite_poly(4, x) == 16 * x**4 - 48 * x**2 + 12 assert hermite_poly(5, x) == 32 * x**5 - 160 * x**3 + 120 * x assert hermite_poly(6, x) == 64 * x**6 - 480 * x**4 + 720 * x**2 - 120 assert hermite_poly(1).dummy_eq(2 * x) assert hermite_poly(1, polys=True) == Poly(2 * x)
def test_hermite_poly(): raises(ValueError, "hermite_poly(-1, x)") assert hermite_poly(1, x, polys=True) == Poly(2*x) assert hermite_poly(0, x) == 1 assert hermite_poly(1, x) == 2*x assert hermite_poly(2, x) == 4*x**2 - 2 assert hermite_poly(3, x) == 8*x**3 - 12*x assert hermite_poly(4, x) == 16*x**4 - 48*x**2 + 12 assert hermite_poly(5, x) == 32*x**5 - 160*x**3 + 120*x assert hermite_poly(6, x) == 64*x**6 - 480*x**4 + 720*x**2 - 120 assert hermite_poly(1).dummy_eq(2*x) assert hermite_poly(1, polys=True) == Poly(2*x)
def gauss_hermite(n, n_digits): r""" Computes the Gauss-Hermite quadrature [1]_ points and weights. The Gauss-Hermite quadrature approximates the integral: .. math:: \int_{-\infty}^{\infty} e^{-x^2} f(x)\,dx \approx \sum_{i=1}^n w_i f(x_i) The nodes `x_i` of an order `n` quadrature rule are the roots of `H_n` and the weights `w_i` are given by: .. math:: w_i = \frac{2^{n-1} n! \sqrt{\pi}}{n^2 \left(H_{n-1}(x_i)\right)^2} Parameters ========== n : the order of quadrature n_digits : number of significant digits of the points and weights to return Returns ======= (x, w) : the ``x`` and ``w`` are lists of points and weights as Floats. The points `x_i` and weights `w_i` are returned as ``(x, w)`` tuple of lists. Examples ======== >>> from sympy.integrals.quadrature import gauss_hermite >>> x, w = gauss_hermite(3, 5) >>> x [-1.2247, 0, 1.2247] >>> w [0.29541, 1.1816, 0.29541] >>> x, w = gauss_hermite(6, 5) >>> x [-2.3506, -1.3358, -0.43608, 0.43608, 1.3358, 2.3506] >>> w [0.00453, 0.15707, 0.72463, 0.72463, 0.15707, 0.00453] See Also ======== gauss_legendre, gauss_laguerre, gauss_gen_laguerre, gauss_chebyshev_t, gauss_chebyshev_u, gauss_jacobi, gauss_lobatto References ========== .. [1] https://en.wikipedia.org/wiki/Gauss-Hermite_Quadrature .. [2] http://people.sc.fsu.edu/~jburkardt/cpp_src/hermite_rule/hermite_rule.html .. [3] http://people.sc.fsu.edu/~jburkardt/cpp_src/gen_hermite_rule/gen_hermite_rule.html """ x = Dummy("x") p = hermite_poly(n, x, polys=True) p1 = hermite_poly(n - 1, x, polys=True) xi = [] w = [] for r in p.real_roots(): if isinstance(r, RootOf): r = r.eval_rational(S(1) / 10**(n_digits + 2)) xi.append(r.n(n_digits)) w.append(((2**(n - 1) * factorial(n) * sqrt(pi)) / (n**2 * p1.subs(x, r)**2)).n(n_digits)) return xi, w
def gauss_hermite(n, n_digits): r""" Computes the Gauss-Hermite quadrature [1]_ points and weights. The Gauss-Hermite quadrature approximates the integral: .. math:: \int_{-\infty}^{\infty} e^{-x^2} f(x)\,dx \approx \sum_{i=1}^n w_i f(x_i) The nodes `x_i` of an order `n` quadrature rule are the roots of `H_n` and the weights `w_i` are given by: .. math:: w_i = \frac{2^{n-1} n! \sqrt{\pi}}{n^2 \left(H_{n-1}(x_i)\right)^2} Parameters ========== n : the order of quadrature n_digits : number of significant digits of the points and weights to return Returns ======= (x, w) : the ``x`` and ``w`` are lists of points and weights as Floats. The points `x_i` and weights `w_i` are returned as ``(x, w)`` tuple of lists. Examples ======== >>> from sympy.integrals.quadrature import gauss_hermite >>> x, w = gauss_hermite(3, 5) >>> x [-1.2247, 0, 1.2247] >>> w [0.29541, 1.1816, 0.29541] >>> x, w = gauss_hermite(6, 5) >>> x [-2.3506, -1.3358, -0.43608, 0.43608, 1.3358, 2.3506] >>> w [0.00453, 0.15707, 0.72463, 0.72463, 0.15707, 0.00453] See Also ======== gauss_legendre, gauss_laguerre, gauss_gen_laguerre, gauss_chebyshev_t, gauss_chebyshev_u, gauss_jacobi References ========== .. [1] http://en.wikipedia.org/wiki/Gauss-Hermite_Quadrature .. [2] http://people.sc.fsu.edu/~jburkardt/cpp_src/hermite_rule/hermite_rule.html .. [3] http://people.sc.fsu.edu/~jburkardt/cpp_src/gen_hermite_rule/gen_hermite_rule.html """ x = Dummy("x") p = hermite_poly(n, x, polys=True) p1 = hermite_poly(n-1, x, polys=True) xi = [] w = [] for r in p.real_roots(): if isinstance(r, RootOf): r = r.eval_rational(S(1)/10**(n_digits+2)) xi.append(r.n(n_digits)) w.append(((2**(n-1) * factorial(n) * sqrt(pi))/(n**2 * p1.subs(x, r)**2)).n(n_digits)) return xi, w
binomialpoly.subs(n, 2).expand() == binomialpolyexpand.subs(n, 2).doit() # True binomialpoly.subs(n, 3).expand() == binomialpolyexpand.subs(n, 3).doit() # True binomialpoly.subs(n, 4).expand() == binomialpolyexpand.subs(n, 4).doit() # True # rising factorial a, q = symbols("a q") # a_n = Product(a+k,(k,0,n-1)) a_n = rf(a, n) aq_n = Product(Rat(1) - a * q ** k, (k, 0, n - 1)) # e.g. aq_n.subs(n,3).doit() # Hermite polynomials HermiteF = factorial(n) * (Rat(-1)) ** k * (Rat(2) * x) ** (n - Rat(2) * k) / (factorial(n - Rat(2) * k) * factorial(k)) HermiteP = Sum(HermiteF, (k, 0, floor(n / 2))) hermite_poly(1, x) == HermiteP.subs(n, 1).doit() # True hermite_poly(2, x) == HermiteP.subs(n, 2).doit() # True hermite_poly(3, x) == HermiteP.subs(n, 3).doit() # True hermite_poly(4, x) == HermiteP.subs(n, 4).doit() # True (HermiteF.subs(n, n + 1) / HermiteF).simplify() # 2*x*(n + 1)/(-2*k + n + 1) (HermiteF.subs(k, k + 1) / HermiteF).simplify() # -(2*k - n)*(2*k - n + 1)/(4*x**2*(k + 1)) LaguerreF = binomial(n + alpha, n - k) * (-x) ** k / factorial(k) LaguerreP = Sum(LaguerreF, (k, 0, n)) for alphavar in range(4): for nvar in range(4): print alphavar, nvar, LaguerreP.subs(n, nvar).subs(alpha, alphavar).doit(), LaguerreP.subs(n, nvar).subs( alpha, alphavar ).doit() == laguerre_poly(