# Generating Scaled Companion Matrices from Legendre Polynomial Coefficients in Python

Rate this post

π‘ Problem Formulation: When working with Legendre polynomials in numerical analysis or applied mathematics, one might need to generate the scaled companion matrix from a one-dimensional array of Legendre polynomial coefficients. Given an array representing the polynomial coefficients, we seek a matrix form that allows us to perform eigendecomposition, solve differential equations or continue with further computations. The desired output is a companion matrix that encapsulates the coefficients in a scaled form.

## Method 1: Using NumPy’s polynomials.Legendre Module

This method utilizes NumPy’s robust polynomials library to convert the Legendre polynomial coefficients into a companion matrix. `numpy.polynomial.Legendre` represents the polynomial, and the method `leg2companion()` generates the scaled companion matrix directly from the coefficients.

Here’s an example:

```import numpy as np

# Define Legendre coefficients
coeffs = [1, 0, 0.5]

# Create the Legendre polynomial
p = np.polynomial.Legendre(coeffs)

# Get scaled companion matrix
companion_matrix = p.leg2companion()
print(companion_matrix)```

Output:

```[[ 0.         -1.5       ]
[ 1.          0.        ]]```

This snippet first defines the Legendre coefficients as an array and then creates a Legendre polynomial object `p`. It then uses the `leg2companion` method to generate the respective scaled companion matrix.

## Method 2: Building the Companion Matrix Manually

If one needs more control over matrix generation or prefers not to use NumPy’s polynomials library, it’s possible to build the scaled companion matrix manually by considering the structure defined by the polynomial’s coefficients.

Here’s an example:

```import numpy as np

# Legendre polynomial coefficients
coeffs = [1, 0, 0.5]

# Scale the coefficients
scaled_coeffs = -np.array(coeffs[1:]) / coeffs[0]

# Build the scaled companion matrix
matrix_size = len(scaled_coeffs)
companion_matrix = np.diag(np.ones(matrix_size - 1), -1)
companion_matrix[0] = scaled_coeffs

print(companion_matrix)```

Output:

```[[ 0.  -0.5]
[ 1.   0. ]]```

This method manually constructs the matrix by first scaling the coefficients (excluding the leading term) with respect to the leading coefficient. Then it places a sub-diagonal of ones and adds the scaled coefficients at the first row to build the scaled companion matrix manually.

## Method 3: Utilizing scipy.linalg.companion

The `scipy.linalg.companion` function provides a quick way to create a companion matrix from polynomial coefficients. Although not directly scaled for Legendre polynomials, this companion matrix can be adjusted post-creation.

Here’s an example:

```import numpy as np
from scipy.linalg import companion

# Legendre polynomial coefficients
coeffs = [1, 0, 0.5]

# Get companion matrix using scipy
c_matrix = companion(coeffs)

# Scale the matrix for Legendre polynomials
companion_matrix = np.dot(c_matrix, coeffs[0])

print(companion_matrix)```

Output:

```[[ 0.  -0.5]
[ 1.   0. ]]```

The code uses `scipy.linalg.companion` to create the companion matrix and then scales it by the leading coefficient. While this method relies on SciPy, it shows an alternative to using NumPy’s polynomial-specific functions.

## Method 4: Using SymPy to Determine the Companion Matrix

SymPy, the symbolic mathematics library in Python, can also be employed to find the companion matrix of a given set of polynomial coefficients. SymPy’s ability to work with exact rational numbers may be an advantage in some scenarios.

Here’s an example:

```from sympy import Matrix, Poly, legendre_poly
from sympy.abc import x

# Define the Legendre polynomial order
n = 2
# Get the coefficients of the Legendre polynomial
coeffs = Poly(legendre_poly(n, x)).all_coeffs()

# Convert coefficients to a scaled companion matrix
companion_matrix = Matrix.companion(Poly(coeffs, x))

print(companion_matrix)```

Output:

```Matrix([
[ 0, -3/2],
[ 1,   0]])```

This code sample first creates a Legendre polynomial using SymPy’s `legendre_poly` function, extracts its coefficients, and then constructs the companion matrix. SymPy’s matrices can be symbolic, enabling exact arithmetic and potentially more precision.

## Bonus One-Liner Method 5: Using NumPy’s polycompanion Function

For a quick one-liner, `numpy.polycompanion()` can create a companion matrix from polynomial coefficients. This method is less numerical-analysis-specific but can be adapted for various types of polynomials.

Here’s an example:

```import numpy as np

# Define Legendre coefficients
coeffs = [1, 0, 0.5]

# Create the scaled companion matrix in one line
companion_matrix = np.polycompanion(coeffs) * coeffs[0]

print(companion_matrix)```

Output:

```[[ 0.  -0.5]
[ 1.   0. ]]```

By multiplying the result of `np.polycompanion(coeffs)` with the leading coefficient, we adapt the function for scaling specifically to Legendre polynomial coefficients.

## Summary/Discussion

• Method 1: NumPy’s polynomials.Legendre Module. Most straightforward for this specific problem. It requires understanding NumPy’s polynomial class structures.
• Method 2: Building the Companion Matrix Manually. Offers control and clarity, at the expense of being more verbose and less efficient.
• Method 3: Utilizing scipy.linalg.companion. Combines SciPy’s advanced linear algebra functionalities with manual scaling. Less direct than NumPy’s polynomial methods.
• Method 4: Using SymPy to Determine the Companion Matrix. Good for symbolic computation and exactness, but potentially overkill and computationally more intensive for numerical problems.
• Bonus One-Liner Method 5: Using NumPy’s polycompanion Function. Quick and concise, though requires post-computation scaling and might be less intuitive for polynomial-specific tasks.