≡ ▼
=L_POLYADD(a, b, [drop_leading_zeros])
=L_POLYSUB(a, b, [drop_leading_zeros])
ArgumentDescriptionExample
aA polynomial defined by coefficients in descending order of power
bA polynomial defined by coefficients in descending order of power
drop_leading_zeros(default=FALSE) If TRUE, drops leading zero termsFALSE

Download the Template

In the template file, navigate to the Polynomials worksheet to see the L_POLYADD function in action.

Description

Adding and subtracting polynomials is trivial when the vectors of coefficients are the same size. But, it requires a bit of logic to match up the correct terms when the polynomials are different degrees.

L_POLYADD(a,b) adds polynomial a and polynomial b, resulting in polynomial c which is a row vector the same length as the longest of the two original vectors. Leading zero terms are not removed from the polynomial, unless you use drop_leading_zeros=TRUE.

L_POLYSUB(a,b) subtracts polynomial b from polynomial a, and is based on L_POLYADD:

L_POLYSUB(a,b) = L_POLYADD(a,-b)

For these polynomial functions, the polynomial is defined as p(x) = βn*xn + βn-1*xn-1 + … + β2*x2 + β1*x1 + β0, and the coefficients should be defined as a row vector: {βnn-1,...,β10}.

If the resulting polynomial c is a zero vector and you have set drop_leading_zeros to TRUE, the function will return a #N/A error.

Lambda Formula

This code for using L_POLYADD 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)

/**
* Add two polynomials a + b = c
*/
L_POLYADD = LAMBDA(a,b,[drop_leading_zeros],
LET(doc,"https://www.vertex42.com/lambda/polyadd.html",
    na,COLUMNS(a),
    nb,COLUMNS(b),
    c,IF(na=nb,a+b,
    IF(na>nb,a+HSTACK(SEQUENCE(1,na-nb,0,0),b),
        b+HSTACK(SEQUENCE(1,nb-na,0,0),a)
    )),
    IF(drop_leading_zeros=TRUE,DROP(c,0,MATCH(TRUE,c<>0,0)-1),c)
));
/**
* Subtract polynomial b from polynomial a: a - b = c
*/
L_POLYSUB = LAMBDA(a,b,[drop_leading_zeros],
LET(doc,"https://www.vertex42.com/lambda/polyadd.html",
    L_POLYADD(a,-b,drop_leading_zeros)
));

Named Function for Google Sheets

Name: L_POLYADD
Description: Add two polynomials: a + b = c
Arguments: a, b
Function:
[in the works]

L_POLYADD Examples

Example 1: Add two Polynomials
Add polynomial \(a(x)=x^4+2x^3+4x+5\) and \(b(x)=-3x^2+3\). Remember to include 0 terms in the vector of coefficients.
Test: Copy and Paste this LET function into a cell
=LET(
    a, {1,2,0,4,5},
    b, {-3,0,3},
    L_POLYADD(a,b)
)

Result: {1,2,-3,4,8}
Example 2: Subtract two Polynomials
Subtract polynomial \(b(x)=x^2+1\) from \(a(x)=x^2+2x+3\). Use the paramater drop_leading_zeros to remove the leading \(0x^2\) term from the result.
=LET(
    a, {1,2,4},
    b, {1,0,1},
    L_POLYSUB(a,b, TRUE)
)

Result: {2,3}
Example 3: Remove Leading Zero Terms
Instead of a separate function for reducing a polynomial to standard form, you can use L_POLYADD to remove the leading zero terms like this:
=LET(
    a, {0,0,1,2,3},
    L_POLYADD(a, 0*a, TRUE)
)

Result: {1,2,3}

See Also

POLYVAL, POLYFIT, POLYMULT, POLYDIV

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.