-1

我有一个多项式方程常数的列表,我想用 print() 函数中的 for 循环编写这个方程。最短的方法是什么(如果存在单行代码会更合适)?

编辑:(原因:添加示例)代码如下:

cnst=list()
degree=int(input("Enter degree of your polynomial: "))
#degree=int(input("Enter degree of your polynomial: "))
# must use degree+1 to include constant term
for i in range(degree+1):
    print(i)
    print("Enter constant for x^" + str(degree-i) + ": ", end='')
    cnst.append(float(input()))
print (cnst)
print("\nFunction created: ")
#print equation code here <<--
4

2 回答 2

1

欢迎来到我们 Pythonic 数学大师和救世主NumPy的世界。

可以加、减、计算导数等。np.polynomial模块功能很多numpy.polynomial现在是处理多项式时推荐的类。查看文档了解更多信息。

from numpy.polynomial import Polynomial

p1 = Polynomial([1,5,2])

p2 = Polynomial([6,1,4,3])

print(p1 * p2)

多项式([6., 31., 21., 25., 23., 6.], [-1., 1.], [-1., 1.])

于 2018-08-01T15:54:43.723 回答
0

我无法正确理解,但我认为这段代码可以帮助你

for x in [-1, 0, 2, 3.4]:
print(x, p(x))
于 2018-08-01T15:51:26.037 回答