NumPy: Add, subtract, multiply and divide polynomials

NumPy Mathematics: Exercise-18 with Solution

Write a NumPy program to add one polynomial to another, subtract one polynomial from another, multiply one polynomial by another and divide one polynomial by another.

Sample Solution:

Python Code:

from numpy.polynomial import polynomial as P
x = (10,20,30)
y = (30,40,50)
print("Add one polynomial to another:")
print("Subtract one polynomial from another:")
print("Multiply one polynomial by another:")
print("Divide one polynomial by another:")

Sample Output:

Add one polynomial to another:                                         
[ 40.  60.  80.]                                                       
Subtract one polynomial from another:                                  
[-20. -20. -20.]                                                       
Multiply one polynomial by another:                                    
[  300.  1000.  2200.  2200.  1500.]                                   
Divide one polynomial by another:                                      
(array([ 0.6]), array([-8., -4.]))

Pictorial Presentation:

NumPy Mathematics: Add, subtract, multiply and divide polynomials. NumPy Mathematics: Add, subtract, multiply and divide polynomials.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to compute the following polynomial values.
Next: Write a NumPy program to calculate mean across dimension, in a 2D numpy array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Getting rid of unwanted characters:

You can get rid of whitespaces or any specific character using strip methods in Python. You can use either plain strip for both sides, lstrip for the left side and rstrip for the right side only.

str="+++Welcome python+++"



+++Welcome python+++