NumPy Polynomials

numpy
Published

October 10, 2024

Representing Polynomials

NumPy represents polynomials using their coefficients. A polynomial like 3x² + 2x + 1 is represented as an array [3, 2, 1], where the index corresponds to the power of x. The highest-order coefficient is placed first.

import numpy as np

coefficients = np.array([3, 2, 1]) 

Polynomial Evaluation

Once you have a polynomial represented as an array of coefficients, NumPy’s polyval() function makes evaluating the polynomial at specific points straightforward.

x = 2
result = np.polyval(coefficients, x)  #result will be 17 (3*2^2 + 2*2 + 1)
print(f"The value of the polynomial at x = {x} is: {result}")


x_values = np.array([0, 1, 2, 3])
results = np.polyval(coefficients, x_values)
print(f"The values of the polynomial at x = {x_values} are: {results}")

Polynomial Roots

Finding the roots (zeros) of a polynomial is crucial in many applications. NumPy’s polyroots() function efficiently calculates the roots of a polynomial given its coefficients.

roots = np.polyroots(coefficients)
print(f"The roots of the polynomial are: {roots}")

Note that the roots might be complex numbers.

Polynomial Multiplication and Division

NumPy allows for straightforward multiplication and division of polynomials using polymul() and polydiv() respectively.

poly1 = np.array([1, 2]) #represents x + 2
poly2 = np.array([2, 1]) #represents 2x + 1
product = np.polymul(poly1, poly2) #result will represent 2x^2 + 5x + 2
print(f"The product of the polynomials is: {product}")


#Divide two polynomials
dividend = np.array([2, 5, 2]) # represents 2x^2 + 5x +2
divisor = np.array([1, 2]) #represents x + 2
quotient, remainder = np.polydiv(dividend, divisor)
print(f"The quotient is: {quotient}") #Represents 2x + 1
print(f"The remainder is: {remainder}") #Represents 0

Polynomial Derivatives

NumPy can compute the derivative of a polynomial using polyder().

derivative = np.polyder(coefficients) #Result will be [6, 2] representing 6x + 2
print(f"The derivative of the polynomial is: {derivative}")

#Calculate second derivative
second_derivative = np.polyder(coefficients, 2) #Result will be [6] representing 6
print(f"The second derivative of the polynomial is: {second_derivative}")

Polynomial Integration

NumPy also provides polyint() for polynomial integration. It requires specifying the integration constant.

integral = np.polyint(coefficients, 0) #Result will be [1, 1, 1] representing x^3 + x^2 + x
print(f"The integral of the polynomial is: {integral}")

These examples illustrate the fundamental operations you can perform with NumPy polynomials. This powerful library simplifies complex polynomial manipulations, making it a crucial tool for anyone working with mathematical modeling and data analysis in Python.