Use Numpy polyadd() to add two polynomials

Deus Ex :

I'm trying to add two polynomials using Numpy's polyadd(), but i'm getting the wrong result

a = [60, 51, 64, 38,  9, 79, 96, 81, 11]
e = [1,0,1,0,1,0,1]
b = np.polyadd(a, e)
b
array([60, 51, 65, 38, 10, 79, 97, 81, 12])

Is there an easy way to get the correct result (61, 51, 65, 38, 10, 79, 97, 81, 11) ?

enter image description here

Numpy treats coefficients from lowest to the highest order right? So here it should be (60+51x+64x^2+38x^3+9x^4+79x^5+96x^6+81x^7+11x^8)+(1+x^2+x^4+x^6) = 61+51x+65x^2+38x^3+10x^4+79x^5+97x^6+81x^7+11x^8

Sayandip Dutta :

You are seeing the docs for different function. np.polynomial.polynomial.polyadd uses from lowest order to highest from left to right, whereas, np.polyadd ranks from highest to lowest.

>>> a = [60, 51, 64, 38,  9, 79, 96, 81, 11]
>>> e = [1,0,1,0,1,0,1]
>>> np.polyadd(a, e)
array([60, 51, 65, 38, 10, 79, 97, 81, 12])
>>> np.polynomial.polynomial.polyadd(a,e)
array([61., 51., 65., 38., 10., 79., 97., 81., 11.])

The result you are asking for can be obtained using np.polyadd by reversing the lists and then reversing the obtained result as suggested by @Mad Physicist:

>>> np.polyadd(a[::-1], e[::-1])[::-1]
array([61, 51, 65, 38, 10, 79, 97, 81, 11])

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12473&siteId=1