0% found this document useful (0 votes)
13 views3 pages

Polynomial Handling using NumPy

polynomial handling

Uploaded by

reenavinu2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Polynomial Handling using NumPy

polynomial handling

Uploaded by

reenavinu2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Polynomial Handling using NumPy

February 9, 2025

np.poly1d( ) is a convenience constructor in NumPy


• it that creates a one‐dimensional polynomial class object
• it allows us to define a polynomial using an array (or list) of coefficients
• we can work with this polynomial as if it were a function.
• when we print the polynomial, it displays the polynomial in a human-friendly format,
which is useful for debugging and presentation.
[1]: import numpy as np
p = np.poly1d([1, -3, 2])

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)

(poly1d([0.5]), poly1d([-3. , 2.5]))

[7]: np.poly1d([0.5])*np.poly1d([-3. , 2.5])

[7]: poly1d([-1.5 , 1.25])

3. Differentiation and Integration: It provides methods like .deriv( ) to compute the


derivative and .integ( ) to compute an antiderivative (indefinite integral) of the polyno-
mial.
[8]: dp = p.deriv()
print(dp)

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

[10]: coeff = p.coeffs


print(coeff)

[ 1 -3 2]

[11]: deg = p.order


print(p)

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.

You might also like