POLYCOMPAN
=POLYCOMPAN(coeffs)
Argument | Description | Example |
---|---|---|
coeffs | Row vector of polynomial coefficients in decreasing order of power | {1, 2, 3} |
In the template file, navigate to the Polynomials worksheet to see the POLYCOMPAN function in action.
Description
Finding the roots of a polynomial involves finding the eigenvalues of the corresponding companion matrix C(p).
Keeping with the same definition for a polynomial as the other functions in the library, p is a row vector of coefficients in decreasing order of power such that p(x) = βn*xn + βn-1*xn-1 + … + β2*x2 + β1*x1 + β0.
The first row of the polynomial companion matrix is the subvector m(x) of length n where the elements are equal to mk=-βk/βn+1 for k from n to 1.
m(x) = [ -βn-1/βn ... -β2/βn -β1/βn -β0/βn ]
$$C(p) = \begin{bmatrix}-{\beta}_{n-1}/{\beta}_n & \cdots & -{\beta}_2/{\beta}_1 & -{\beta}_1/{\beta}_n & -{\beta}_0/{\beta}_n \\ 1 & \cdots & 0 & 0 & 0 \\ \vdots & \ddots & \vdots & \vdots & \vdots \\ 0 & \cdots & 1 & 0 & 0 \\ 0 & \cdots & 0 & 1 & 0 \end{bmatrix}$$
The rest of the companion matrix is assembled using the identity matrix and zeros. This form of the companion matrix is designed to be similar to that used in the scipy.linalg.companion function in SciPy and the compan function in Matlab.
Lambda Formula
This code for using POLYCOMPAN in Excel is provided under the License as part of the LAMBDA Library, but to use just this function, you may copy the following code directly into your spreadsheet.
Code for AFE Workbook Module (Excel Labs Add-in)
/** * Returns the companion matrix for the polynomial defined by coeffs */ POLYCOMPAN = LAMBDA(coeffs, LET(doc,"https://www.vertex42.com/lambda/polycompan.html", coeffs,IF(AND(ROWS(coeffs)>1,COLUMNS(coeffs)=1),TRANSPOSE(coeffs),coeffs), n,COLUMNS(coeffs)-1, m,-CHOOSECOLS(coeffs,SEQUENCE(1,n,2,1))/INDEX(coeffs,1), VSTACK(m,HSTACK(MUNIT(n-1),SEQUENCE(n-1,1,0,0))) ));
Named Function for Google Sheets
Name: POLYCOMPAN Description: Returns the companion matrix for a vector of polynomial coefficients Arguments: coeffs Function: =LET(doc,"https://www.vertex42.com/lambda/polycompan.html", coeffs,IF(AND(ROWS(coeffs)>1,COLUMNS(coeffs)=1),TRANSPOSE(coeffs),coeffs), n,COLUMNS(coeffs)-1, m,ARRAYFORMULA(-CHOOSECOLS(coeffs,SEQUENCE(1,n,2,1))/INDEX(coeffs,1)), VSTACK(m,HSTACK(MUNIT(n-1),SEQUENCE(n-1,1,0,0))) )
POLYCOMPAN Examples
Test: Copy and Paste this LET function into a cell =LET( coeffs, {2,3,-9,10}, POLYCOMPAN(coeffs) ) Result: {-1.5, 4.5, 5; 1, 0, 0; 0, 1, 0}