Issue
I'm trying to write a function that accepts inputs of coefficients for polynomial f(x) and returns the corresponding f(-x). This is what I have so far:
import numpy as np
coeff = [1, -5, 2, -5]
poly = np.poly1d(coeff)
print (poly)
This prints out:
1x³ - 5x² + 2x - 5
I'm stuck here, since using poly(-x) or any values of x calculates the whole equation itself. Is there any workaround here? What I only want is for the code to do f(-x) such that 1(-1)³ - 5(-1)² + 2(-1) + 5 prints out:
-1x³ - 5x² - 2x - 5
Thank you!
Solution
The issue is that you are defining your polynomial by it's coefficients. I would instead define the variable x
and let the module itself handle all the manipulations.
import numpy as np
x = np.poly1d([1,0])
p = x**3 - 5*x**2 + 2*x - 5
print(p(-x))
Answered By - Bobby Ocean
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.