NumPy - Polynomial Operations



Polynomial Operations in NumPy

Polynomial operations in NumPy refer to various mathematical tasks you can perform on polynomials, such as addition, subtraction, multiplication, division, and evaluation.

NumPy makes it easy to work with polynomials by using arrays to represent their coefficients. You can use functions like numpy.polyadd(), numpy.polysub(), and numpy.polymul() to perform these operations, and methods like numpy.polyval() to evaluate a polynomial at specific values of x.

Adding Polynomials

Polynomials can be added using the numpy.polyadd() function in NumPy. The result is a new polynomial whose coefficients are the sum of the corresponding coefficients of the original polynomials.

Example: Polynomial Addition

In this example, we add two polynomials using the polyadd() function in NumPy −

import numpy as np

# Define two polynomials using their coefficients
# 1 + 2x + 3x
p1 = np.array([1, 2, 3])  
# 0 + 1x + 4x
p2 = np.array([0, 1, 4]) 

# Add the polynomials using numpy.polyadd
result_add = np.polyadd(p1, p2)

print("Result of polynomial addition:", result_add)

The result of adding the two polynomials is −

Result of polynomial addition: [1 3 7]

Subtracting Polynomials

Polynomials can be subtracted using the numpy.polysub() function. The result is a new polynomial whose coefficients are the differences of the corresponding coefficients of the original polynomials.

Example: Polynomial Subtraction

In this example, we subtract one polynomial from another using the polysub() function in NumPy −

import numpy as np

# Define two polynomials using their coefficients
# 1 + 2x + 3x
p1 = np.array([1, 2, 3])  
# 0 + 1x + 4x
p2 = np.array([0, 1, 4]) 

# Subtract the polynomials using numpy.polysub
result_sub = np.polysub(p1, p2)

print("Result of polynomial subtraction:", result_sub)

The result of subtracting the two polynomials is −

Result of polynomial subtraction: [ 1  1 -1]

Multiplying Polynomials

Polynomials can be multiplied using the numpy.polymul() function. The multiplication of two polynomials is carried out by multiplying each term in the first polynomial by each term in the second polynomial and summing the results.

Example: Polynomial Multiplication

In this example, we multiply two polynomials using the polymul() function in NumPy −

import numpy as np

# Define two polynomials using their coefficients
# 1 + 2x + 3x
p1 = np.array([1, 2, 3])  
# 0 + 1x + 4x
p2 = np.array([0, 1, 4]) 

# Multiply the polynomials using numpy.polymul
result_mul = np.polymul(p1, p2)

print("Result of polynomial multiplication:", result_mul)

The result of multiplying the two polynomials is −

Result of polynomial multiplication: [ 1  6 11 12]

Evaluating Polynomials

The numpy.polyval() function is used to evaluate a polynomial at a specific value of x. This can be useful for finding the value of the polynomial for any given x.

Example: Polynomial Evaluation

In this example, we evaluate the polynomial at x = 2 using the polyval() function −

import numpy as np

# Define polynomial using its coefficients
# 1 + 2x + 3x
p1 = np.array([1, 2, 3])  

# Evaluate the polynomial at x = 2 using numpy.polyval
x_value = 2
result_eval = np.polyval(p1, x_value)

print(f"Polynomial evaluated at x = {x_value}:", result_eval)

The result of evaluating the polynomial at x = 2 is −

Polynomial evaluated at x = 2: 11

Polynomial Differentiation

Polynomials can be differentiated using the numpy.polyder() function. The derivative of a polynomial is a new polynomial obtained by differentiating each term of the original polynomial with respect to the variable.

Example: Polynomial Differentiation

In this example, we differentiate the polynomial using the polyder() function in NumPy −

import numpy as np

# Define polynomial using its coefficients
# 1 + 2x + 3x
p1 = np.array([1, 2, 3])  

# Differentiate the polynomial using numpy.polyder
derivative = np.polyder(p1)

print("Derivative of the polynomial:", derivative)

The result of differentiating the polynomial is −

Derivative of the polynomial: [2 2]

Polynomial Integration

Polynomials can be integrated using the numpy.polyint() function. The integral of a polynomial is a new polynomial obtained by integrating each term of the original polynomial with respect to the variable.

Example: Polynomial Integration

In this example, we integrate the polynomial using the polyint() function in NumPy −

import numpy as np

# Define polynomial using its coefficients
# 1 + 2x + 3x
p1 = np.array([1, 2, 3])  

# Integrate the polynomial using numpy.polyint
integral = np.polyint(p1)

print("Integral of the polynomial:", integral)

The result of integrating the polynomial is −

Integral of the polynomial: [0.33333333 1.         3.         0.        ]
Advertisements