Generate a Hermite_e series with given roots using NumPy in Python


Hermite polynomials are a set of orthogonal polynomials that are useful in a variety of mathematical applications. They are commonly used in the solution of differential equations, probability theory, and quantum mechanics. The Hermite_e series is a variation of the Hermite polynomial that is used to represent a function in terms of its roots. In this article, we will discuss how to generate a Hermite_e series with given roots using NumPy in Python.

Installation and Syntax

NumPy is a Python library that provides support for numerical operations and it can be installed using pip and imported into Python using the statement "import numpy".

pip install numpy

To generate a Hermite_e series with given roots using NumPy, the following syntax can be used −

numpy.polynomial.hermite_e.hermegauss(roots, deg)

roots − A 1-D array containing the roots of the Hermite_e series.

deg − The degree of the Hermite_e series.

Algorithm

The following is the algorithm to generate a Hermite_e series with given roots using NumPy −

  • Import the NumPy library.

  • Define an array containing the roots of the Hermite_e series.

  • Define the degree of the Hermite_e series.

  • Call the numpy.polynomial.hermite_e.hermegauss() function with the roots and degree as arguments.

  • The function returns two arrays, one containing the weights and the other containing the nodes of the Hermite_e series.

  • Use the weights and nodes to construct the Hermite_e series.

Example 1

The following code example generates a Hermite_e series with roots [-1, 0, 1] and degree 2.

import numpy as np
roots = np.array([-1, 0, 1])
deg = 2
weights, nodes = np.polynomial.hermite_e.hermegauss(deg)
print(weights)
print(nodes)

Output

[-1.  1.]
[1.25331414 1.25331414]

Example 2

The following code example generates a Hermite_e series with roots [1, 2, 3, 4] and degree 3.

import numpy as np

# array of roots
roots = np.array([0, 1, 2, 3])

# initialize coefficients array with zeros
coeffs = np.zeros((len(roots), 2 * len(roots) - 1))

# setting up initial values of coefficients
coeffs[:, 0] = roots # setting f(x) values to be the roots
coeffs[1:, 1] = np.diff(coeffs[:, 0]) / np.diff(roots) # setting f'(x) values using finite difference method

# setting up the remaining coefficients using recurrence relation
for j in range(2, 2 * len(roots)):
   for i in range(len(roots)):
      if j % 2 == 0 and i >= j // 2:
         # even-indexed coefficients
         coeffs[i, j // 2] = coeffs[i, j // 2 - 1] * (j - 1) / (j // 2)
      elif j % 2 == 1 and i >= (j + 1) // 2:
         # odd-indexed coefficients
         coeffs[i, (j + 1) // 2 - 1] = (coeffs[i, j // 2] - coeffs[i - 1, j // 2]) / (roots[i] - roots[i - j // 2])

# generating the Hermite series using the calculated coefficients
def hermite_e_series(x):
   res = np.zeros_like(x)
   for i in range(len(roots)):
      term = np.ones_like(x)
      for j in range(i):
         term *= (x - roots[j])
      res += coeffs[i, i] * term
   return res
 

x = np.linspace(-1, 4, 1000)
y = hermite_e_series(x)

# get the first 10 coefficients
print(y[:10])

# plot the function
import matplotlib.pyplot as plt
plt.plot(x, y)
plt.show()

Output

[-5.5  -5.44884884 -5.39799735 -5.34744457 -5.29718957 -5.24723141                                                                                                       
 -5.19756916 -5.14820186 -5.09912858 -5.05034838]

The following code example generates a Hermite_e series with roots [0, 1, 2, 3] and degree 4 and plots the series using Matplotlib.

Applications

The Hermite series produced involving NumPy in Python has various applications. In physics, Hermite polynomials are used to describe the wave functions of the quantum harmonic oscillator while also proving handy for numerical analysis and scientific computing as well as for implementing approximate functions in statistics, such as the normal distribution, because it is frequently implemented with approximate functions with high accuracy.

Conclusion

The Hermite_e series is a powerful tool in scientific computing and numerical analysis. With the help of NumPy in Python, generating the Hermite series has become a simple task. The algorithm for generating the series involves setting up initial coefficients and then using a recurrence relation to determine the remaining coefficients. Once the coefficients have been calculated, the Hermite series can be generated using a simple function. The series has numerous applications in physics, mathematics, and statistics. By using the Hermite_e series, scientists and mathematicians can approximate complex functions with high accuracy, making it a valuable tool in many areas of research.

Updated on: 22-Aug-2023

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements