完成函数FindRealZeros
,该函数将多项式作为列表并以递增的顺序返回多项式根的列表,出现次数与重复次数相同
至此,我已经断言只能将最小阶数为1的多项式传递给函数。 0阶的任何函数都会立即使断言失败。接下来,输出阶次为1的多项式。
我遇到的问题是为多项式的阶数大于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
到目前为止,我已经实现的代码可以完美地工作。问题是我被困在下一步解决的问题上。我可以尝试找出一种可以计算3、4等多项式根的方式,但这对我没有帮助,因为我需要能够为给定的任何n阶多项式生成代码。