我使用poly1d和polyder来返回简单多项式的导数。
3 2
1 x + 1 x + 1 x + 1
我看到这两个命令的简单组合并没有以正确的顺序使用这些系数。
print(np.poly1d(P.polyder(c)))
我可以像这样使用一个班轮
print(np.poly1d(P.polyder(c)))
以便上述系数的顺序正确吗?
Below is my code and output:
import numpy as np
from numpy.polynomial import polynomial as P
print("")
c = (1, 1, 1, 1)
print("the array of coefficients for the polynomial")
print(c)
print("")
print("polynomial with coefficients and exponents")
print(np.poly1d(c))
print("")
print("array of coefficients of derivative of polynomial: lowest order coming first in the array")
d_c = P.polyder(c)
print(d_c)
print("")
print("reversing the array of coefficients for the derivative of the polynomial")
d_c = d_c[::-1]
print(d_c)
print("")
print("printing the derivative of the polynomial with exponents and coefficients")
print(np.poly1d(d_c))
print("")
print("printing the derivative of the polynomial without reversing the coefficient array after derivation")
print(np.poly1d(P.polyder(c)))
print("")
输出:
the array of coefficients for the polynomial
(1, 1, 1, 1)
polynomial with coefficients and exponents
3 2
1 x + 1 x + 1 x + 1
array of coefficients of derivative of polynomial: lowest order coming first in the array
[ 1. 2. 3.]
reversing the array of coefficients for the derivative of the polynomial
[ 3. 2. 1.]
printing the derivative of the polynomial with exponents and coefficients
2
3 x + 2 x + 1
printing the derivative of the polynomial without reversing the coefficient array after derivation
2
1 x + 2 x + 3
答案 0 :(得分:1)
对deriv
上的对象使用np.poly1d
方法:
import numpy as np
p = np.poly1d([1, 3, 1, 0, 4])
print(p)
print(p.deriv(1))
有关可用方法的完整列表,请参阅the docs。