gpt4 book ai didi

python - 这个解三次方程的函数有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:06 25 4
gpt4 key购买 nike

我正在使用 Python 2 和维基百科文章“三次函数”中给出的相当简单的方法。这也可能是我为了创建标题中提到的函数而必须定义的立方根函数的问题。

# Cube root and cubic equation solver
#
# Copyright (c) 2013 user2330618
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, you can obtain one at http://www.mozilla.org/MPL/2.0/.

from __future__ import division
import cmath
from cmath import log, sqrt

def cbrt(x):
"""Computes the cube root of a number."""
if x.imag != 0:
return cmath.exp(log(x) / 3)
else:
if x < 0:
d = (-x) ** (1 / 3)
return -d
elif x >= 0:
return x ** (1 / 3)

def cubic(a, b, c, d):
"""Returns the real roots to cubic equations in expanded form."""
# Define the discriminants
D = (18 * a * b * c * d) - (4 * (b ** 3) * d) + ((b ** 2) * (c ** 2)) - \
(4 * a * (c ** 3)) - (27 * (a ** 2) * d ** 2)
D0 = (b ** 2) - (3 * a * c)
i = 1j # Because I prefer i over j
# Test for some special cases
if D == 0 and D0 == 0:
return -(b / (3 * a))
elif D == 0 and D0 != 0:
return [((b * c) - (9 * a * d)) / (-2 * D0), ((b ** 3) - (4 * a * b * c)
+ (9 * (a ** 2) * d)) / (-a * D0)]
else:
D1 = (2 * (b ** 3)) - (9 * a * b * c) + (27 * (a ** 2) * d)
# More special cases
if D != 0 and D0 == 0 and D1 < 0:
C = cbrt((D1 - sqrt((D1 ** 2) - (4 * (D0 ** 3)))) / 2)
else:
C = cbrt((D1 + sqrt((D1 ** 2) - (4 * (D0 ** 3)))) / 2)
u_2 = (-1 + (i * sqrt(3))) / 2
u_3 = (-1 - (i * sqrt(3))) / 2
x_1 = (-(b + C + (D0 / C))) / (3 * a)
x_2 = (-(b + (u_2 * C) + (D0 / (u_2 * C)))) / (3 * a)
x_3 = (-(b + (u_3 * C) + (D0 / (u_3 * C)))) / (3 * a)
if D > 0:
return [x_1, x_2, x_3]
else:
return x_1

我发现这个函数能够求解一些简单的三次方程:

print cubic(1, 3, 3, 1)
-1.0

不久前,我已经达到可以求解具有两个根的方程式的地步。我刚刚完成了重写,现在它已经失控了。例如,这些系数是 (2x - 4)(x + 4)(x + 2) 的扩展形式,它应该返回 [4.0, -4.0, -2.0] 或类似的东西:

print cubic(2, 8, -8, -32)
[(-4+1.4802973661668753e-16j), (2+2.9605947323337506e-16j), (-2.0000000000000004-1.1842378929335002e-15j)]

这是我犯的数学错误还是编程错误?

更新:谢谢大家的回答,但是这个函数的问题比我到目前为止迭代的要多。例如,我经常遇到与立方根函数相关的错误:

print cubic(1, 2, 3, 4)  # Correct solution: about -1.65
...
if x > 0:
TypeError: no ordering relation is defined for complex numbers
print cubic(1, -3, -3, -1) # Correct solution: about 3.8473
if x > 0:
TypeError: no ordering relation is defined for complex numbers

最佳答案

Wolfram Alpha confirms你最后一个立方体的根确实是

(-4, -2, 2)

不是你说的

... it should return [4.0, -4.0, -2.0]

尽管有那个(我认为)错字,你的程序还是给出了

[(-4+1.4802973661668753e-16j), (2+2.9605947323337506e-16j), (-2.0000000000000004-1.1842378929335002e-15j)]

10**(-15) 的精度是 完全相同的根 作为正确的解决方案。正如其他人所说,微小 虚部可能是由于四舍五入。

请注意,如果您使用类似 Cardano's 的解决方案,则必须使用精确的算法才能始终正确取消。 .这是 MAPLEMathematica 等程序存在的原因之一,公式与实现之间经常存在脱节。

要在纯 Python 中仅获取数字的实部,您可以调用 .real。示例:

a = 3.0+4.0j
print a.real
>> 3.0

关于python - 这个解三次方程的函数有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16270941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com