# Add one polynomial to another in Python

PythonNumpyServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

To add one polynomial to another, use the numpy.polynomial.polynomial.polyadd() method in Python. Returns the sum of two polynomials c1 + c2. 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**2.The method returns the coefficient array representing their sum.The parameters c1 and c2 returns 1-D arrays of polynomial coefficients ordered from low to high.

This numpy.polynomial.polynomial module provides a number of objects useful for dealing with polynomials, including a Polynomial class that encapsulates the usual arithmetic operations.

## Steps

At first, import the required libraries-

from numpy.polynomial import polynomial as P

Declare Two Polynomials −

p1 = (4,1,6)
p2 = (2,5,3)

Display the polynomials −

print("Polynomial 1...\n",p1)
print("\nPolynomial 2...\n",p2)

To add one polynomial to another, use the numpy.polynomial.polynomial.polyadd() method in Python. Returns the sum of two polynomials c1 + c2. 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**2 −

sumRes = P.polyadd(p1,p2);
print("\nResult (Sum)...\n",sumRes)

## Example

from numpy.polynomial import polynomial as P

# Declare Two Polynomials
p1 = (4,1,6)
p2 = (2,5,3)

# Display the polynomials
print("Polynomial 1...\n",p1)
print("\nPolynomial 2...\n",p2)

# To add one polynomial to another, use the numpy.polynomial.polynomial.polyadd() method in Python.
print("\nResult (Sum)...\n",sumRes)

## Output

Polynomial 1...
(4, 1, 6)

Polynomial 2...
(2, 5, 3)

Result (Sum)...
[6. 6. 9.]