Multiply one polynomial to another in Python

To multiply one polynomial to another, use the numpy.polynomial.polynomial.polymul() method in Python. This function returns the multiplication of two polynomials represented as coefficient arrays. The arguments are sequences of coefficients from lowest order term to highest, i.e., [1,2,3] represents the polynomial 1 + 2*x + 3*x².

The method returns the coefficient array representing their product. The parameters c1 and c2 are 1-D arrays of coefficients representing polynomials, ordered from lowest order term to highest.

Syntax

numpy.polynomial.polynomial.polymul(c1, c2)

Parameters:

  • c1 ? 1-D array of polynomial coefficients (lowest to highest order)
  • c2 ? 1-D array of polynomial coefficients (lowest to highest order)

Example

Let's multiply two polynomials: 4 + x + 6x² and 2 + 5x + 3x² ?

from numpy.polynomial import polynomial as P

# Declare two polynomials
# p1 represents: 4 + 1*x + 6*x²
p1 = (4, 1, 6)

# p2 represents: 2 + 5*x + 3*x²  
p2 = (2, 5, 3)

# Display the polynomials
print("Polynomial 1:", p1)
print("Polynomial 2:", p2)

# Multiply the polynomials
result = P.polymul(p1, p2)
print("Result (Multiply):", result)
Polynomial 1: (4, 1, 6)
Polynomial 2: (2, 5, 3)
Result (Multiply): [ 8. 22. 29. 33. 18.]

Understanding the Result

The result [8. 22. 29. 33. 18.] represents the polynomial 8 + 22x + 29x² + 33x³ + 18x?. This is the product of our two input polynomials.

Another Example

Let's multiply simpler polynomials to see the pattern ?

from numpy.polynomial import polynomial as P

# Simple polynomials: (1 + x) and (1 + 2x)
poly1 = (1, 1)    # 1 + x
poly2 = (1, 2)    # 1 + 2x

print("Polynomial 1 coefficients:", poly1)
print("Polynomial 2 coefficients:", poly2)

# Multiply them
product = P.polymul(poly1, poly2)
print("Product coefficients:", product)
print("This represents: 1 + 3x + 2x²")
Polynomial 1 coefficients: (1, 1)
Polynomial 2 coefficients: (1, 2)
Product coefficients: [1. 3. 2.]
This represents: 1 + 3x + 2x²

Conclusion

The polymul() function efficiently multiplies polynomials represented as coefficient arrays. Remember that coefficients are ordered from lowest to highest degree terms, making polynomial operations straightforward in NumPy.

Updated on: 2026-03-26T19:26:37+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements