Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Divide one polynomial by another in Python
To divide one polynomial by another in Python, use the numpy.polynomial.polynomial.polydiv() method. This function performs polynomial division and returns both the quotient and remainder. The arguments are sequences of coefficients from lowest order term to highest, e.g., [1,2,3] represents 1 + 2*x + 3*x².
Syntax
numpy.polynomial.polynomial.polydiv(c1, c2)
Parameters
The parameters are ?
- c1 ? 1-D array of coefficients for the dividend polynomial
- c2 ? 1-D array of coefficients for the divisor polynomial
Return Value
Returns a tuple containing two arrays ?
- Quotient ? Array of coefficients representing the quotient
- Remainder ? Array of coefficients representing the remainder
Example
Let's divide two polynomials and examine the quotient and remainder ?
from numpy.polynomial import polynomial as P
# Define two polynomials
# p1 = 4 + 1*x + 6*x²
# p2 = 2 + 5*x + 3*x²
p1 = (4, 1, 6)
p2 = (2, 5, 3)
print("Polynomial 1 coefficients:", p1)
print("Polynomial 2 coefficients:", p2)
# Divide p1 by p2
quotient, remainder = P.polydiv(p1, p2)
print("\nQuotient:", quotient)
print("Remainder:", remainder)
Polynomial 1 coefficients: (4, 1, 6) Polynomial 2 coefficients: (2, 5, 3) Quotient: [2.] Remainder: [ 0. -9.]
Understanding the Result
The division gives us ?
from numpy.polynomial import polynomial as P
# Define polynomials
p1 = (4, 1, 6) # 4 + x + 6x²
p2 = (2, 5, 3) # 2 + 5x + 3x²
quotient, remainder = P.polydiv(p1, p2)
print("Original polynomial p1: 4 + x + 6x²")
print("Divisor polynomial p2: 2 + 5x + 3x²")
print(f"Quotient: {quotient[0]}")
print(f"Remainder: {remainder[1]}x (since remainder[0] = 0)")
# Verification: p1 = quotient * p2 + remainder
verification = P.polyadd(P.polymul(quotient, p2), remainder)
print(f"\nVerification - p1 reconstructed: {verification}")
print(f"Original p1: {p1}")
Original polynomial p1: 4 + x + 6x² Divisor polynomial p2: 2 + 5x + 3x² Quotient: 2.0 Remainder: -9.0x (since remainder[0] = 0) Verification - p1 reconstructed: [ 4. 1. 6.] Original p1: (4, 1, 6)
Conclusion
The polydiv() function divides polynomials and returns both quotient and remainder as coefficient arrays. This is useful for polynomial long division operations in numerical computing and mathematical analysis.
Advertisements
