Divide one polynomial by another in Python

PythonNumpyServer Side ProgrammingProgramming

To divide one polynomial by another, use the numpy.polynomial.polynomial.polydiv() method in Python. Returns the quotient-with-remainder of two polynomials c1 / c2. The arguments are sequences of coefficients, from lowest order term to highest, e.g., [1,2,3] represents 1 + 2*x + 3*x**2.

The method returns the array of coefficient series representing the quotient and remainder. The parameters c1 and c2 are the 1-D arrays of coefficients representing a polynomial, relative to the “standard” basis, and ordered from lowest order term to highest.

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 divide one polynomial by another, use the numpy.polynomial.polynomial.polydiv() method in Python −

mulRes = P.polydiv(p1,p2);
print("\nResult (divide)...\n",mulRes)

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 divide one polynomial by another, use the numpy.polynomial.polynomial.polydiv() method in Python.
mulRes = P.polydiv(p1,p2);
print("\nResult (divide)...\n",mulRes)

Output

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

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

Result (divide)...
(array([2.]), array([ 0., -9.]))
raja
Updated on 28-Feb-2022 10:21:40

Advertisements