定义一个函数来定位给定多项式的零点

2024-10-03 13:30:40 发布

您现在位置:Python中文网/ 问答频道 /正文

完成函数FindRealZeros,该函数接受一个多项式作为一个列表,并以递增的顺序返回多项式根的列表,重复出现的次数与此相同

到目前为止,我断言只有最小阶数为1的多项式才能传递到函数中。任何顺序为0的函数都会立即使断言失败。接下来,输出一阶多项式的根。你知道吗

我遇到的问题是定义函数的其余部分的阶数大于2的多项式。例如和x^3项。你知道吗


def FindRealZeros(polynomial):



    assert len(polynomial) > 1.  
# Above i have made sure the polynomial is never just a constant or empty list such as [a], or y = a and [].




    assert EnsureStandardForm(polynomial)
# Above ensures that there are never additional zeros that does not satisfy the previous assertion.     



    if len(polynomial) == 2:     
#######################################################################
#   Here i am saying that if given a linear polynomai, ax + b, then return the root #
#               (given by the equation using the members of the list)               #
#######################################################################
        first_member = polynomial[0]
        second_member = polynomial[1]

        root = [(-first_member)/second_member]
        return root

到目前为止,我实现的代码工作得很好。问题是我下一步该怎么解决。我可以试着找出一种方法,在第三,第四,等,多项式根可以计算,但这并没有帮助我,因为我需要能够产生任何n阶多项式给定的代码。你知道吗


Tags: the函数列表lenthat顺序rootassert
1条回答
网友
1楼 · 发布于 2024-10-03 13:30:40

似乎你不知道多项式求根背后的基本数学。概述一下:

  • 一阶:简单,你只要给出解决方案
  • 第二级:很简单,这里有一个formula
  • 第三阶:仍然可行,但越来越棘手(需要多项式除法!)你知道吗
  • 四级及以上:不再琐碎

但幸运的是,有一个numpy函数^{},正好适合您的需要。你知道吗

相关问题 更多 >