Polynomial Handling using NumPy
Polynomial Handling using NumPy
February 9, 2025
print(p)
2
1 x - 3 x + 2
Features and Uses:
1. Callable Object: The polynomial object is callable. We can evaluate the polynomial at a
specific value by simply passing that value as an argument.
[2]: value = p(5) # Evaluates p(x) at x = 5.
print(value)
12
2. Arithmetic Operations: The object supports standard arithmetic operations. We can add,
subtract, multiply or divide two polynomial objects.
[3]: q = np.poly1d([2, 0, -1])
print(q)
s = p + q
print(s)
2
2 x - 1
2
3 x - 3 x + 1
1
[4]: d=p-q
print(d)
2
-1 x - 3 x + 3
[5]: prod=p*q
print(prod)
4 3 2
2 x - 6 x + 3 x + 3 x - 2
[6]: div=p/q
print(div)
2 x - 3
[9]: ip = p.integ( )
print(ip)
3 2
0.3333 x - 1.5 x + 2 x
4. Accessing Coefficients and Degree: We can access the polynomial’s coefficients with the
.coeffs attribute and its degree with the .order (or .degree()) attribute
[ 1 -3 2]
2
1 x - 3 x + 2
2
Possible usage of np.poly1d( )
1. Polynomial Interpolation:
In numerical methods, once we have determined the coefficients of an interpolating polynomial
(using methods like Lagrange interpolation), we can use np.poly1d( ) to create a polynomial
object that we can evaluate, differentiate or integrate.
2. Data Fitting and Analysis:
When we fit a polynomial to data (for example, using least squares regression), representing
the result with np.poly1d( ) makes it easy to work with the polynomial in further calculations.