Obtain opposite polynomial

6

I know it can be silly but I can not find how to do it. I want to know how I can calculate the opposite polynomial created in Numpy regardless of its degree .

Example that does not work for me:

import numpy as np


p0 = np.poly1d([2., 0., 0., -100., 2., -1.])
p1 = np.polyder(p0, 1)
p2 = -1. @ (np.polydiv(p0, p1))[1]
p3 = -1. @ np.polydiv(p1, p2[1])[1]
p4 = -1. @ np.polydiv(p2[1], p3[1])[1]
print(p0)
print(p1)
print(p2[1])
print(p3[1])
print(p4[1])

The output is as follows:

Traceback (most recent call last) <ipython-input-9-9aab292f9c1e> in <module>()
      1 p0 = np.poly1d([2., 0., 0., -100., 2., -1.])
      2 p1 = np.polyder(p0, 1)
----> 3 p2 = -1. @ (np.polydiv(p0, p1))[1]
      4 p3 = -1. @ np.polydiv(p1, p2[1])[1]
      5 p4 = -1. @ np.polydiv(p2[1], p3[1])[1]

TypeError: unsupported operand type(s) for @: 'float' and 'poly1d'
    
asked by Tobal 19.04.2016 в 21:22
source

1 answer

3

Simply multiply by -1.

The operator @, which you use in the example, is for multiplication between matrices and therefore the error TypeError that does not support that operation.

import numpy as np

p0 = np.poly1d([2., 0., 0., -100., 2., -1.]) 
print(p0)
       5       2
2 x - 100 x + 2 x - 1

#polinomio opuesto de p0
p0_opuesto = p0 * -1
print(p0_opuesto)
    5     4     3       2
-2 x - 0 x - 0 x + 100 x - 2 x + 1
    
answered by 19.04.2016 / 23:54
source