Divide one Hermite series by another in Python using NumPy


The Hermite series is one of the mathematical techniques, which is used to represent the infinite series of Hermite polynomials. The Hermite polynomials referred as the sequence of orthogonal polynomials which are the solutions of the Hermite differential equation.

Dividing one hermite series by another

The Hermite series is given by the following equation.

f(x) = Σn=0^∞ cn Hn(x)

Where

  • Hn(x) is the nth Hermite polynomial

  • cn is the nth coefficient in the expansion.

The coefficient cn can be determined by using the below formula:

cn = (1/$\mathrm{\surd}$(2^n n!))$\mathrm{\lmoustache}$ f(x) Hn(x) e^(−x^2/2) dx

Example

First let’s create the hermite series by using the function in the numpy library namely, polynomial.hermite.poly2herm().

import numpy as np
from numpy.polynomial import hermite as H
c = np.array([1, 2, 3, 4, 5])
print("Our Array...\n",c)
print("\nDimensions of our Array...\n",c.ndim)
print("\nDatatype of our Array object...\n",c.dtype)
print("\nShape of our Array object...\n",c.shape)
print("\nResult (polynomial to hermite)...\n",H.poly2herm(c))

Output

Our Array...
 [1 2 3 4 5]

Dimensions of our Array...
 1

Datatype of our Array object...
 int32

Shape of our Array object...
 (5,)

Result (polynomial to hermite)...
 [6.25   4.     4.5    0.5    0.3125]

Example

We have a function namely, divide() in Numpy library to divide the hermite series by another hermite series. The following is the example.

import numpy as np
from numpy.polynomial import hermite as H
h = np.array([1, 2, 3, 4, 5])
print("Our Array...\n",h)
print("\nDimensions of our Array...\n",h.ndim)
print("\nDatatype of our Array object...\n",h.dtype)
print("\nShape of our Array object...\n",h.shape)
print("\nResult (polynomial to hermite)...\n",H.poly2herm(h))
divide_hermite = np.divide(h,h)
print("The output of dividing the hermite series by another:",divide_hermite)

Output

Our Array...
 [1 2 3 4 5]

Dimensions of our Array...
 1

Datatype of our Array object...
 int32

Shape of our Array object...
 (5,)

Result (polynomial to hermite)...
 [6.25   4.     4.5    0.5    0.3125]
The output of dividing the hermite series by another: [1. 1. 1. 1. 1.]

Example

Let’s see another example to divide the hermite series by another hermite series using divide() function of the numpy library.

import numpy as np
from numpy.polynomial import hermite as H
h1 = np.array([1, 2, 3, 4, 5])
h2 = np.arange(2,12,2)
print("Array1...\n",h1)
print("Array2...\n",h2)
print("\nDimensions of Array1...\n",h1.ndim)
print("\nDimensions of Array2...\n",h2.ndim)
print("\nDatatype of Array1 object...\n",h1.dtype)
print("\nDatatype of Array2 object...\n",h2.dtype)
print("\nShape of Array1 object...\n",h1.shape)
print("\nShape of Array2 object...\n",h2.shape)
print("\nResult (polynomial to hermite)...\n",H.poly2herm(h1))
print("\nResult (polynomial to hermite)...\n",H.poly2herm(h2))
divide_hermite = np.divide(h1,h2)
print("The output of dividing the hermite series by another:",divide_hermite)

Output

Array1...
 [1 2 3 4 5]
Array2...
 [ 2  4  6  8 10]

Dimensions of Array1...
 1

Dimensions of Array2...
 1

Datatype of Array1 object...
 int32

Datatype of Array2 object...
 int32

Shape of Array1 object...
 (5,)

Shape of Array2 object...
 (5,)

Result (polynomial to hermite)...
 [6.25   4.     4.5    0.5    0.3125]

Result (polynomial to hermite)...
 [12.5    8.     9.     1.     0.625]
The output of dividing the hermite series by another: [0.5 0.5 0.5 0.5 0.5]

Example

In the following example, we are dividing a 1−D array by a 2−D array of hermite series using the divide() function, and error will be raised due to the mismatch in co−efficient dimensions between the two series.

import numpy as np
from numpy.polynomial import hermite as H
h1 = np.array([1, 2, 3, 4, 5])
h2 = np.arange(2,12).reshape(5,2)
print("Array1...\n",h1)
print("Array2...\n",h2)
print("\nDimensions of Array1...\n",h1.ndim)
print("\nDimensions of Array2...\n",h2.ndim)
print("\nDatatype of Array1 object...\n",h1.dtype)
print("\nDatatype of Array2 object...\n",h2.dtype)
print("\nShape of Array1 object...\n",h1.shape)
print("\nShape of Array2 object...\n",h2.shape)
print("\nResult (polynomial to hermite)...\n",H.poly2herm(h1))
print("\nResult (polynomial to hermite)...\n",H.poly2herm(h2))
divide_hermite = np.divide(h1,h2)
print("The output of dividing the hermite series by another:",divide_hermite)

Output

Array1...
 [1 2 3 4 5]
Array2...
 [[ 2  3]
 [ 4  5]
 [ 6  7]
 [ 8  9]
 [10 11]]

Dimensions of Array1...
 1

Dimensions of Array2...
 2

Datatype of Array1 object...
 int32

Datatype of Array2 object...
 int32

Shape of Array1 object...
 (5,)

Shape of Array2 object...
 (5, 2)

Result (polynomial to hermite)...
 [6.25   4.     4.5    0.5    0.3125]
Traceback (most recent call last):
  File "F:\test.py", line 14, in <module>
    print("\nResult (polynomial to hermite)...\n",H.poly2herm(h2))
                                                  ^^^^^^^^^^^^^^^
  File "C:\Users\Krishna\AppData\Roaming\Python\Python311\site-packages\numpy\polynomial\hermite.py", line 134, in poly2herm
    [pol] = pu.as_series([pol])
            ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Krishna\AppData\Roaming\Python\Python311\site-packages\numpy\polynomial\polyutils.py", line 134, in as_series
    raise ValueError("Coefficient array is not 1-d")
ValueError: Coefficient array is not 1-d

Updated on: 02-Nov-2023

31 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements