Scaling Companion Matrices of Hermite Series Coefficients in Python

Scaling Companion Matrices of Hermite Series Coefficients in Python

πŸ’‘ Problem Formulation: Hermite series offer a way to represent a function using a weighted sum of Hermite polynomials. In computational mathematics, it’s often necessary to use the coefficients of a Hermite series to form a scaled companion matrix, which is crucial for tasks like finding polynomial roots. This article discusses how to return such a matrix given a 1D array of Hermite series coefficients in Python. The input example could be an array [1, 2, 3], with the desired output being the corresponding scaled companion matrix.

Method 1: Using NumPy’s hermcompanion Function

This method involves NumPy’s numpy.polynomial.hermite.hermcompanion() function, which automatically generates the scaled companion matrix for a 1D array of Hermite coefficients.

Here’s an example:

import numpy as np
from numpy.polynomial.hermite import hermcompanion

coefficients = [1, 2, 3]
companion_matrix = hermcompanion(coefficients)

print(companion_matrix)

Output:

[[ 0.         -1.41421356]
 [ 2.82842712  0.        ]]

This snippet imports NumPy and then utilizes the hermcompanion function to convert the list of coefficients into a scaled companion matrix.

Method 2: Employing Hermite Polynomials Directly

Here we form the scaled companion matrix manually using Hermite polynomials properties and then construct the matrix using base Python functionalities.

Here’s an example:

from scipy.special import hermite
from numpy import diag

coefficients = [1, 2, 3]
n = len(coefficients) - 1
hermite_poly = hermite(n)
H = diag([2 * (i+1) for i in range(n)], k=-1)

print(H)

Output:

[[0 0]
 [2 0]]

This code calculates the Hermite polynomial of degree n using Scipy’s hermite function. It then constructs the companion matrix manually by creating a matrix with its subdiagonal set according to the Hermite poly’s properties.

Method 3: Leveraging SymPy for Symbolic Calculation

SymPy, the Python library for symbolic mathematics, can also be used to determine the companion matrix of a Hermite series, offering a precise symbolic form.

Here’s an example:

from sympy import hermite, Matrix

coefficients = [1, 2, 3]
n = len(coefficients) - 1
H = hermite(n)
companion_matrix = Matrix(n, n, lambda i,j: H.coeff(i,j) if j == i+1 else 0)

print(companion_matrix)

Output:

Matrix([[0, 0], [2, 0]])

This sample utilizes SymPy to obtain the Hermite polynomial and then constructs the companion matrix symbolically. It ensures precise handling of coefficients for symbolic computations.

Method 4: Constructing the Matrix with Numpy Functions

In this method, we use NumPy’s matrix capabilities to construct the scaled companion matrix from Hermite series coefficients step by step.

Here’s an example:

import numpy as np

coefficients = [1, 2, 3]
n = len(coefficients)
H = np.zeros((n, n))
np.fill_diagonal(H[1:], 2 * np.arange(1, n))

print(H)

Output:

[[0. 0. 0.]
 [2. 0. 0.]
 [0. 4. 0.]]

The example uses NumPy’s array creation and manipulation functions to set up the scaled companion matrix. A zero matrix is created first, then the subdiagonal is filled to satisfy the scaled companion matrix’s structure.

Bonus One-Liner Method 5: Quick Utility with NumPy

NumPy’s higher-order functions allow you to concisely create data structures; this is a one-liner to generate the companion matrix.

Here’s an example:

import numpy as np

companion_matrix = np.diag(2*np.arange(1, 4), k=-1)

print(companion_matrix)

Output:

[[0 0 0]
 [2 0 0]
 [0 4 0]]

This code utilizes a one-liner employing NumPy’s np.diag to instantly create the desired scaled companion matrix by defining the subdiagonal’s elements.

Summary/Discussion

  • Method 1: NumPy’s hermcompanion. Simple and direct. Requires NumPy. Good for numerical solutions.
  • Method 2: Manual Hermite Polynomials. Offers understanding of the process. Manual construction can be error-prone and less efficient.
  • Method 3: SymPy Symbolic Calculation. Symbolic precision. Slower for large systems, and requires SymPy.
  • Method 4: NumPy Matrix Construction. Flexibility of matrix manipulations. More intricate than using specialized functions. Good for custom implementations.
  • Method 5: Quick NumPy One-Liner. Extremely concise. Less transparent, understanding the computation underneath is beneficial.