SciPy - companion() Function



The scipy.linalg.companion() method creates a companion matrix for a given one-dimensional array 'a'. A companion matrix is a square matrix containing the characteristic polynomial of a linear recurrence relation or polynomial equation. The last row reflects the polynomial coefficients (except the leading term), whereas the subdiagonal contains ones.

This method is commonly used for polynomial root finding, eigenvalue analysis, and system dynamics because it gives a structured matrix representation that improves computer performance.

Errors may arise if the input array is not one-dimensional or has fewer than two members, as the technique requires a suitable polynomial representation. Furthermore, incorrect input shapes or data types can cause computational issues.

The companion() technique is frequently used in combination with eig() to compute the companion matrix's eigenvalues, which correspond to the polynomial roots. It can also be used with solve() to perform dynamic system simulations.

Syntax

The syntax for the SciPy companion() method is as follows −

.companion(a)

Parameters

This method accepts the following parameters −

  • a (N,) array_like) − A 1D array of polynomial coefficients, ordered from the highest degree to the constant term, with at least two elements.

Return Value

The first row of the companion matrix is calculated as a[1:]/a[0], while the first sub-diagonal consists of ones. The matrix has the same data type as the scaled input, 1.0a[0].

Example 1

In the following program, the array a=[1,3,2] is the input representing the polynomial x^23x+2. The companion() function returns the companion matrix C for these coefficients.

The eigenvalues of the matrix C computed by the function eigvals() are the roots of the polynomial, and the given output is evidence that the correct roots are indeed x=2 and x=1.

import numpy as np
from scipy.linalg import companion, eigvals

# Input: Polynomial coefficients
a = [1, -3, 2]

# Compute the companion matrix
C = companion(a)
print("Companion Matrix:\n", C)

# Compute the eigenvalues (roots)
roots = eigvals(C)
print("Polynomial Roots:", roots)

When we run above program, it produces following result

Companion Matrix:
 [[ 3. -2.]
 [ 1.  0.]]
Polynomial Roots: [2.+0.j 1.+0.j]

Example 2

The input vector a=[1,6,11,6] defines the cubic polynomial x^3-6x^2+11x6. A structured matrix is produced by the companion() method with the first row a[1:]/a[0], which corresponds to coefficients of the polynomial (excluding the leading term) and ones in the subdiagonal.

The output reflects the polynomial's structure within the matrix. This method is useful as the eigenvalues of this matrix correspond to the polynomial's roots, making it a powerful tool for solving and analyzing polynomial equations.

import numpy as np
from scipy.linalg import companion, eigvals

# Input: Polynomial coefficients for cubic polynomial
a = [1, -6, 11, -6]

# Compute the companion matrix
C = companion(a)
print("Companion Matrix:\n", C)

Following is an output of the above code

Companion Matrix:
 [[  6. -11.   6.]
 [  1.   0.   0.]
 [  0.   1.   0.]]

Example 3

If the input array contains fewer than two elements, the companion() method returns a ValueError.

The input array a=[1] is invalid, as it does not define a polynomial. The code demonstrates how the procedure validates input dimensions and generates the appropriate error.

import numpy as np
from scipy.linalg import companion, eigvals

a = [1]

try:
    C = companion(a)
except ValueError as e:
    print("Error:", e)

Output of the above code is as follows

Error: The length of `a` must be at least 2.
scipy_special_matrices_functions.htm
Advertisements