≡ ▼
Work in Progress :: Please contact us to report errors, typos, etc.
=L_POLYMULT(a, b)
ArgumentDescriptionExample
aPolynomial 1 defined as a row vector of coefficients{1, 2, 3}
bPolynomial 2 defined as a row vector of coefficients{4, 5, 6}

Description

L_POLYMULT returns a polynomial c as a row vector resulting from the multiplication of two polynomials a and b. The multiplication of two polynomials can be accomplished by multiplying all combinations of terms and adding all the resulting like-power terms.

a = 1*x^2 + 2*x + 3
b = 4*x^2 + 5*x + 6
c = a*b = 4*x^4 + 13*x^3 + 28*x^2 + 27*x + 18

For this function, a polynomial is defined as p(x) = βn*xn + βn-1*xn-1 + … + β2*x2 + β1*x1 + β0. The β coefficients for polynomials a and b should be given as row vectors in decreasing order of power: {βnn-1,...,β10}.

Use of row vectors is consistent with LINEST and the other polynomial functions POLYFIT, POLYVAL, POLYDER, etc.

L_POLYMULT uses the discrete convolution algorithm.

Complex Numbers

L_POLYMULT is used within L_POLYFROMROOTS to create a polynomial of real coefficients. However, intermediate calculations from complex roots may involve the results of L_POLYMULT being a vector of real and/or complex numbers. The complex number arithmetic functions IMSUM and IMPRODUCT are only used within L_POLYMULT if a coefficient of a or b is stored as text (assuming text is a valid complex number). Therefore, no additional loss of precision should occur from the complex number functions if a and b have real coefficients.

Lambda Formula

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

/** 
* Multiply two polynomials a and b, represented as row vectors of coefficients
*/
L_POLYMULT = LAMBDA(a,b,
LET(doc,"https://www.vertex42.com/lambda/polymult.html",
    a,IF(AND(ROWS(a)>1,COLUMNS(a)=1),TRANSPOSE(a),a),
    b,IF(AND(ROWS(b)>1,COLUMNS(b)=1),TRANSPOSE(b),b),
    len_a,COLUMNS(a),len_b,COLUMNS(b),len_c,len_a+len_b-1,
    coeffs,MAKEARRAY(1,len_c,LAMBDA(i,k,
        REDUCE(0,SEQUENCE(MIN(k,len_a)-MAX(1,k+1-len_b)+1,1,MAX(1,k+1-len_b)),
            LAMBDA(acc,j,
                IF(OR(ISTEXT(acc),ISTEXT(INDEX(a,j)),ISTEXT(INDEX(b,k-j+1))),
                    IMSUM(acc,IMPRODUCT(INDEX(a,j),INDEX(b,k-j+1))),
                    acc + INDEX(a,j)*INDEX(b,k-j+1)
                )
            )
        )
    )),
    MAP(coeffs,LAMBDA(val,IF(IMAGINARY(val)=0,IMREAL(val),val)))
));

Named Function for Google Sheets

Name: L_POLYMULT
Description: Multiply two polynomials
Arguments: a, b
Function:

=LET(doc,"https://www.vertex42.com/lambda/polymult.html",
    a,IF(AND(ROWS(a)>1,COLUMNS(a)=1),TRANSPOSE(a),a),
    b,IF(AND(ROWS(b)>1,COLUMNS(b)=1),TRANSPOSE(b),b),
    len_a,COLUMNS(a),len_b,COLUMNS(b),len_c,len_a+len_b-1,
    coeffs,MAKEARRAY(1,len_c,LAMBDA(i,k,
        REDUCE(0,SEQUENCE(MIN(k,len_a)-MAX(1,k+1-len_b)+1,1,MAX(1,k+1-len_b)),
            LAMBDA(acc,j,
            IF(OR(ISTEXT(acc),ISTEXT(INDEX(a,j)),ISTEXT(INDEX(b,k-j+1))),
                IMSUM(acc,IMPRODUCT(INDEX(a,j),INDEX(b,k-j+1))),
                acc + INDEX(a,j)*INDEX(b,k-j+1)
            ))
        )
    )),
    MAP(coeffs,LAMBDA(val,IF(IMAGINARY(val)=0,IMREAL(val),val)))
)

L_POLYMULT Examples

Example 1
Multiply the polynomials a=x2+2x+3 and b=4x2+5x+6.
Test: Copy and Paste this LET function into a cell
=LET(
    a, {1,2,3},
    b, {4,5,6},
    L_POLYMULT(a,b)
)

Result: {4, 13, 28, 27, 18}

The resulting polynomial c=4x4+13x3+28x2+27x+18

Example 2
Create a polynomial from (x+1)(x-4)(x+2). This polynomial has roots {-1,4,-2}.
Test: Copy and Paste this LET function into a cell
=LET(
    a, {1,1},
    b, {1,-4},
    c, {1,2},
    L_POLYMULT(L_POLYMULT(a,b),c)
)

Result: {1, -1, -10, -8}

Change History

3/05/2024 - v1.0.6 - Updated to work with complex numbers.

See Also

POLYVAL, POLYDER, POLYFROMROOTS

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.