简体   繁体   English

整数 python 中多项式的根

[英]integers numbers Roots of Polynomials in python

I have to write a function that takes a parameter p with the list representing the polynomial p(x) and returns the canonical list of the entire roots of p(x).我必须编写一个 function,它接受一个参数 p,其列表表示多项式 p(x),并返回 p(x) 的整个根的规范列表。 For example, the polynomial with the coefficients [1, -5, 6] must return the roots [2, 3].例如,系数为 [1, -5, 6] 的多项式必须返回根 [2, 3]。 I must use lists and consider that the first coefficient will always be 1.我必须使用列表并考虑第一个系数将始终为 1。

degree = int(input('degree: '))
p=[]
i=0
while i <= degree:     #creates polynomial
  coef = int(input("Coef: "))
  p.append(coef)
  i+=1 
               # creates p(possible roots)

pê=[0]
for x in range(1,p[-1]):
  if b % x == 0:
    pê.append(-x)
    pê.append(x)
for g in pê:

You can use numpy for this.您可以为此使用 numpy。

import numpy as np
coeff=[1,-5,6]
roots=np.roots(coeff)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM