Print polynomial in variable format in python

别说谁变了你拦得住时间么 提交于 2019-12-13 02:36:12

问题


from numpy import linalg,dot
import numpy.polynomial.polynomial as poly
x7=poly.Polynomial([1,2])
print x7

according to above code in python it should print 1 + 2x^2, but it is printing poly [1. 2.]. Please help.


回答1:


I'd recommend using numpy.poly1d and numpy.polymul, where the coefficients are a0*x2 + a1*x + a2.

For example, to represent 3*x**2 + 2*x + 1:

p1 = numpy.poly1d([3,2,1])

therefor for your problem you could use:

p2= numpy.poly1d([2,0,1])
print p2

and printing p2 will represent: 1 + 2x^2



来源:https://stackoverflow.com/questions/33304178/print-polynomial-in-variable-format-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!