Differentiate a Hermite_e series and set the derivatives in Python


Hermite_e series is also known as probabilist's Hermite polynomial or the physicist's Hermite polynomial the available in mathematics which is used to sum of the weighted hermites polynomials. In some particular cases of the quantum mechanics, the Hermite_e series the weight function is given as e^(−x^2). The following is the formula for Hermite_e series.

H_n(x) = (-1)^n e^(x^2/2) d^n/dx^n(e^(-x^2/2))

Where,

  • H_n(x) is the nth Hermite polynomial of degree n

  • x is the independent variable

  • d^n/dx^n denotes the nth derivative with respect to x.

Defining the coefficients

To perform differentiation of the Hermite_e series first we have to define the coefficients of the series. In Numpy library we have the module called hermite_e in the polynomial package to generate the hermite polynomials with the specified degree then we have can multiply the each and every polynomial with the corresponding coefficient and add them to get the series.

Syntax

The following is the syntax.

np.polynomial.hermite_e.hermval(series, coefficients)
np.polynomial.hermite_e.hermdifn(series, coefficients, derivate_degree)

Example

In this example we will generate the differentiate a hermite_e series using the polynomial.hermite_e.hermval() function by passing the series of elements and coefficients and for calculating the derivative of the series we have the function polynomial.hermite_e.hermdifn().

import numpy as np
from numpy.polynomial import hermite
coefficients = [-2,-4,7,-1,5]
x = np.linspace(-5, 5, 20)
series = hermite.hermval(x, coefficients)
print("The Hermite_e series of the given cofficients and series of elements:",series)
derivatives = hermite.hermder(series, m=2,axis = 0)
print("The derivatives of the generated hermite_e series:",derivatives)

Output

The Hermite_e series of the given cofficients and series of elements: [ 4.57240000e+04  2.85437908e+04  1.66401342e+04  8.82740375e+03
  4.06730185e+03  1.46885923e+03  2.88435202e+02 -7.02824257e+01
 -5.62773152e+01  2.87953898e+01  3.06090653e+01 -5.78343935e+01
 -9.62045718e+01  2.03157465e+02  1.27523917e+03  3.70235652e+03
  8.21415400e+03  1.56876046e+04  2.71470100e+04  4.37640000e+04]
The derivatives of the generated hermite_e series: [ 1.33121073e+05  2.11857690e+05  1.95230489e+05  1.17508739e+05
  3.46122243e+04 -1.18074475e+04 -1.26061186e+04  8.29307228e+03
  1.10192635e+04 -2.54471332e+04 -5.07960139e+04  1.26770258e+05
  9.28374116e+05  3.10997948e+06  7.88558784e+06  1.70681138e+07
  3.32279402e+07  5.98691520e+07]

Example

In the above example, we passed the axis as 0 in the hermder() function as the series of elements are 1−d array but here we are using the 2−d array so the axis can be mentioned as axis = 1 or axis = 0.

import numpy as np
from numpy.polynomial import hermite
coefficients = [-2,-4,7,-1,5]
x = np.linspace(-5, 5, 20).reshape(5,4)
series = hermite.hermval(x, coefficients)
print("The Hermite_e series of the given cofficients and series of elements:",series)
derivatives = hermite.hermder(series, m=2,axis = 1)
print("The derivatives of the generated hermite_e series:",derivatives)

Output

The Hermite_e series of the given cofficients and series of elements: [[ 4.57240000e+04  2.85437908e+04  1.66401342e+04  8.82740375e+03]
 [ 4.06730185e+03  1.46885923e+03  2.88435202e+02 -7.02824257e+01]
 [-5.62773152e+01  2.87953898e+01  3.06090653e+01 -5.78343935e+01]
 [-9.62045718e+01  2.03157465e+02  1.27523917e+03  3.70235652e+03]
 [ 8.21415400e+03  1.56876046e+04  2.71470100e+04  4.37640000e+04]]
The derivatives of the generated hermite_e series: [[ 1.33121073e+05  2.11857690e+05]
 [ 2.30748162e+03 -1.68677822e+03]
 [ 2.44872522e+02 -1.38802544e+03]
 [ 1.02019134e+04  8.88565565e+04]
 [ 2.17176080e+05  1.05033600e+06]]

Example

In the following example, when we pass the argument, axis = 2 in the hermder() function for the series elements of 2−d array, it will raise an axis error as the axis should be either 0 or 1.

import numpy as np
from numpy.polynomial import hermite
coefficients = [-2,-4,7,-1,5]
x = np.linspace(-5, 5, 20).reshape(5,4)
series = hermite.hermval(x, coefficients)
print("The Hermite_e series of the given cofficients and series of elements:",series)
derivatives = hermite.hermder(series, m=2,axis = 2)
print("The derivatives of the generated hermite_e series:",derivatives)

Output

The Hermite_e series of the given cofficients and series of elements: [[ 4.57240000e+04  2.85437908e+04  1.66401342e+04  8.82740375e+03]
 [ 4.06730185e+03  1.46885923e+03  2.88435202e+02 -7.02824257e+01]
 [-5.62773152e+01  2.87953898e+01  3.06090653e+01 -5.78343935e+01]
 [-9.62045718e+01  2.03157465e+02  1.27523917e+03  3.70235652e+03]
 [ 8.21415400e+03  1.56876046e+04  2.71470100e+04  4.37640000e+04]]
---------------------------------------------------------------------------
AxisError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_3956\2241814449.py in <module>
      5 series = hermite.hermval(x, coefficients)
      6 print("The Hermite_e series of the given cofficients and series of elements:",series)
----> 7 derivatives = hermite.hermder(series, m=2,axis = 2)
      8 print("The derivatives of the generated hermite_e series:",derivatives)

~\anaconda3\lib\site-packages\numpy\polynomial\hermite.py in hermder(c, m, scl, axis)
    657     if cnt < 0:
    658         raise ValueError("The order of derivation must be non-negative")
--> 659     iaxis = normalize_axis_index(iaxis, c.ndim)
    660 
    661     if cnt == 0:

AxisError: axis 2 is out of bounds for array of dimension 2

Example

Following is another example −

import numpy as np
from numpy.polynomial import hermite_e as H

# Create an array of coefficients
c = np.array([1,2,3,4])

# Display the array
print("Our Array...\n",c)

# Check the Dimensions
print("\nDimensions of our Array...\n",c.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",c.dtype)

# Get the Shape
print("\nShape of our Array object...\n",c.shape)

# To differentiate a Hermite_e series, use the hermite_e.hermeder() method in Python
print("\nResult...\n",H.hermeder(c, 3))

Output

Our Array...
[1 2 3 4]

Dimensions of our Array...
1

Datatype of our Array object...
int64

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

Result...
[24.]

Updated on: 02-Nov-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements