Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return the companion matrix of a 1-D array of polynomial coefficients in Python
To return the companion matrix of a 1-D array of polynomial coefficients, use the polynomial.polycompanion() method in Python NumPy. The companion matrix for power series cannot be made symmetric by scaling the basis, so this function differs from those for orthogonal polynomials. The method returns a companion matrix of dimensions (deg, deg) where deg is the degree of the polynomial.
Syntax
The syntax for creating a companion matrix is ?
numpy.polynomial.polynomial.polycompanion(c)
Parameters:
-
c: A 1-D array of polynomial coefficients ordered from low to high degree
Basic Example
Let's create a companion matrix from polynomial coefficients ?
import numpy as np
from numpy.polynomial.polynomial import polycompanion
# Create a 1D array of coefficients [1, 2, 3] representing 1 + 2x + 3x^2
coefficients = np.array([1, 2, 3])
print("Polynomial coefficients:", coefficients)
# Get the companion matrix
companion_matrix = polycompanion(coefficients)
print("\nCompanion matrix:")
print(companion_matrix)
Polynomial coefficients: [1 2 3] Companion matrix: [[ 0. -0.33333333] [ 1. -0.66666667]]
Understanding the Result
The companion matrix has dimensions (n-1, n-1) where n is the length of the coefficient array. For a polynomial 1 + 2x + 3x², the companion matrix is 2×2 ?
import numpy as np
from numpy.polynomial.polynomial import polycompanion
# Different polynomial: 2 + 4x + x^2
coefficients = np.array([2, 4, 1])
print("Coefficients:", coefficients)
print("Array shape:", coefficients.shape)
print("Polynomial degree:", len(coefficients) - 1)
companion_matrix = polycompanion(coefficients)
print("\nCompanion matrix shape:", companion_matrix.shape)
print("Companion matrix:")
print(companion_matrix)
Coefficients: [2 4 1] Array shape: (3,) Polynomial degree: 2 Companion matrix shape: (2, 2) Companion matrix: [[ 0. -2.] [ 1. -4.]]
Higher Degree Polynomial
Let's see how the companion matrix looks for a higher degree polynomial ?
import numpy as np
from numpy.polynomial.polynomial import polycompanion
# Cubic polynomial: 1 + x + 2x^2 + x^3
coefficients = np.array([1, 1, 2, 1])
print("Cubic polynomial coefficients:", coefficients)
companion_matrix = polycompanion(coefficients)
print("\nCompanion matrix (3×3):")
print(companion_matrix)
Cubic polynomial coefficients: [1 1 2 1] Companion matrix (3×3): [[ 0. 0. -1.] [ 1. 0. -1.] [ 0. 1. -2.]]
Key Properties
The companion matrix has several important properties:
- The first
n-2rows have 1's on the superdiagonal and 0's elsewhere - The last row contains the negative ratios of coefficients
- Eigenvalues of the companion matrix are the roots of the polynomial
Conclusion
The polycompanion() function creates a companion matrix from polynomial coefficients, which is useful in linear algebra applications. The resulting matrix has dimensions (n-1, n-1) where n is the number of coefficients, and its eigenvalues correspond to the polynomial's roots.
