5 Best Ways to Differentiate a Hermite E Series and Set the Derivatives in Python

πŸ’‘ Problem Formulation: When working with Hermite polynomials, specifically the “E” physicists’ version, it can be challenging to calculate their derivatives. This article assists in finding how to differentiate a Hermite E series and capture the derivative coefficients for computational use in Python. For instance, given a Hermite E polynomial like H3(x) = 8x3 – 12x, we want to efficiently calculate its first, second, and higher order derivatives, and represent them for further computational processing.

Method 1: Using NumPy’s polynomial.hermite_e module

This method uses the numpy.polynomial.hermite_e module, which provides a number of functions to work with HermiteE polynomials. To find derivatives, we create a HermiteE object and use the deriv() method, which returns a new HermiteE object representing the derivative.

Here’s an example:

import numpy as np

# Define the Hermite E polynomial coefficients for H3(x)
coeffs = [0, -12, 0, 8]

# Create the HermiteE polynomial
H3 = np.polynomial.hermite_e.HermiteE(coeffs)

# Find the first and second derivatives
first_derivative = H3.deriv(1)
second_derivative = H3.deriv(2)

print("First Derivative:", first_derivative)
print("Second Derivative:", second_derivative)

Output:

First Derivative:     2   1
                 -36   24
Second Derivative: 2
                 48

This code snippet leverages NumPy’s powerful polynomial class to deal with Hermite E polynomials in a more mathematical and object-oriented manner. The deriv method makes it straightforward to calculate derivatives without manual differentiation. Note that the output represents polynomial coefficients in increasing order.

Method 2: Sympy for Symbolic Differentiation

Using sympy, a Python library for symbolic mathematics, we can differentiate any polynomial symbolically. The sympy.hermite function generates a Hermite polynomial, and then sympy.diff can be used to differentiate it.

Here’s an example:

from sympy import symbols, diff, hermite

# Define symbol
x = symbols('x')

# Hermite E polynomial for n=3
H3 = hermite(3, x)

# First and second derivatives
first_derivative = diff(H3, x)
second_derivative = diff(H3, x, 2)

print("H3(x):", H3)
print("First Derivative:", first_derivative)
print("Second Derivative:", second_derivative)

Output:

H3(x): 8*x**3 - 12*x
First Derivative: 24*x**2 - 12
Second Derivative: 48*x

This code snippet showcases the process of creating a Hermite E polynomial and its derivatives symbolically. With sympy, you can easily manipulate expressions and calculate derivatives without numerical approximation, thus providing exact results.

Method 3: Manual Differentiation and Coefficient Extraction

If we want to work directly with coefficients, we can manually differentiate the polynomial by implementing the power rule and then extract the coefficients programmatically. This is a more hands-on approach and allows in-depth understanding of the differentiation process.

Here’s an example:

def differentiate_coeffs(coeffs):
    return [c * i for i, c in enumerate(coeffs)][1:]

# Hermite E polynomial coefficients for H3(x)
coeffs = [0, -12, 0, 8]

# First and second derivatives
first_derivative_coeffs = differentiate_coeffs(coeffs)
second_derivative_coeffs = differentiate_coeffs(first_derivative_coeffs)

print("H3(x) Coefficients:", coeffs)
print("First Derivative Coefficients:", first_derivative_coeffs)
print("Second Derivative Coefficients:", second_derivative_coeffs)

Output:

H3(x) Coefficients: [0, -12, 0, 8]
First Derivative Coefficients: [-12, 0, 24]
Second Derivative Coefficients: [0, 48]

The above code defines a function that applies the power rule for differentiation of polynomials and then applies it to the array of coefficients that define our polynomial. This allows us to differentiate polynomials of any degree straightforwardly and represent the results as arrays of coefficients.

Method 4: scipy.special.hermite Function

In this method, we utilize the scipy.special.hermite which generates Hermite polynomial objects. After generating the polynomial with desired order, we then differentiate using standard polynomial differentiation techniques available in numpy.

Here’s an example:

from scipy.special import hermite
import numpy as np

# Generate Hermite polynomial of order 3
H3 = hermite(3)

# Convert to the polynomial class to differentiate
H3_poly = np.polynomial.Polynomial(H3.c[::-1])  # Reverse coefficients order
first_derivative = H3_poly.deriv()
second_derivative = first_derivative.deriv()

print("First Derivative Coefficients:", first_derivative.coef)
print("Second Derivative Coefficients:", second_derivative.coef)

Output:

First Derivative Coefficients: [ 24. -36.  24.]
Second Derivative Coefficients: [0. 48.]

This snippet utilizes the strength of the SciPy library together with NumPy to handle Hermite polynomials. The hermite function within SciPy creates a polynomial given the order, and subsequent differentiations are performed using the NumPy polynomial class.

Bonus One-Liner Method 5: Lambdify and NumPy Derivative

For a quick and practical application, you can use sympy.lambdify to create a lambda function of a Hermite polynomial and then use numpy.gradient to approximate the derivative numerically.

Here’s an example:

from sympy import symbols, hermite, lambdify
import numpy as np

# Define symbol and Hermite polynomial
x = symbols('x')
H3 = hermite(3, x)

# Lambda function of Hermite polynomial
H3_func = lambdify(x, H3, modules=["numpy"])

# Evaluate on a grid and take derivative
x_vals = np.linspace(-2, 2, 100)
H3_vals = H3_func(x_vals)
first_derivative_vals = np.gradient(H3_vals, x_vals[1] - x_vals[0])

print("Approximate First Derivative at x=0:", first_derivative_vals[50])

Output:

Approximate First Derivative at x=0: -12.000000000000007

In just a few lines, we have a numerical approximation of the first derivative of the Hermite polynomial. This method is useful for quick calculations but sacrifices the exactness of symbolic differentiation for numerical approximation.

Summary/Discussion

  • Method 1: NumPy’s polynomial.hermite_e module. Utilizes an official NumPy module designed for polynomial operations. Simplifies the process with object-oriented practices. May not be as precise as symbolic differentiation for floating-point operations.
  • Method 2: Sympy for Symbolic Differentiation. Offers exact results with symbolic calculus. Suitable for both academic and theoretical applications. May be less efficient for large-scale numerical computations.
  • Method 3: Manual Differentiation and Coefficient Extraction. Provides an understanding of the underlying mathematical process. Simple and direct, with no external library dependencies. Can be cumbersome and error-prone for complex polynomials.
  • Method 4: scipy.special.hermite Function. Benefits from SciPy’s specialized functions for Hermite polynomials. Bridges between symbolic generation and numerical differentiation. Requires familiarity with both SciPy and NumPy libraries.
  • Bonus One-Liner Method 5: Lambdify and NumPy Derivative. Gives a quick numerical approximation. Ideal for on-the-fly calculations. Not as accurate as symbolic methods and sensitive to the choice of discretization in np.linspace().