numpy.polymul(a1, a2)

The numpy.polymul function finds the product (multiplication) of two polynomials a1 and a2. As an input, use either poly1d objects or one-dimensional sequences of polynomial coefficients. If you use the latter, arange this polynomial sequence naturally from highest to lowest degree.

a1, a2array_like or poly1d objectThe input polynomials to be multiplied
Return Valuendarray or poly1d objectThe polynomial resulting from the multiplication of the inputs. If either inputs is a poly1d object, then the output is also a poly1d object. Otherwise, it is a 1D array of polynomial coefficients from highest to lowest degree.


import numpy as np

print(np.polymul([1, 2, 3], [2, 3, 4]))
# [ 2  7 16 17 12]

You can also use poly1d objects:

import numpy as np

p1 = np.poly1d([1, 2, 3])
p2 = np.poly1d([2, 3, 4])

print(np.polymul(p1, p2))
1 x + 2 x + 3
2 x + 3 x + 4
   4     3      2
2 x + 7 x + 16 x + 17 x + 12

As you see the output looks much like a real polynomial if you use poly1d objects.

Any master coder has a “hands-on” mentality with a bias towards action. Try it yourself—play with the function in the following interactive code shell:

Exercise: Change the parameters of your polynomials. How does the output change? Guess and check!

Master NumPy—and become a data science pro:

Coffee Break NumPy

Related Video