≡ ▼
Work in Progress :: Please contact us to report errors, typos, etc.
=L_POLYCOMPAN(coeffs)
ArgumentDescriptionExample
coeffsRow vector of polynomial coefficients in decreasing order of power{1, 2, 3}

Download the Template

In the template file, navigate to the Polynomials worksheet to see the L_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=-βkn+1 for k from n to 1.

m(x) = [ -βn-1n ... -β2n1n0n ]

$$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 L_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
*/
L_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: L_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)))
)

L_POLYCOMPAN Examples

Example 1
Create the companion matrix for the polynomial with coefficients {2,3,-9,10}.
Test: Copy and Paste this LET function into a cell
=LET(
    coeffs, {2,3,-9,10},
    L_POLYCOMPAN(coeffs)
)

Result:
{-1.5, 4.5,  5;
    1,   0,  0;
    0,   1,  0}

See Also

POLYVAL, POLYFIT, EIGENVALUE

Disclaimer: This article is meant for educational purposes only. See the License regarding the LAMBDA code, and the site Terms of Use for the documentation.