
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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.