我正在使用Python 2和维基百科文章"Cubic函数"中给出的相当简单的方法.这也可能是我必须定义的立方根函数的问题,以便创建标题中提到的函数.
# 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
Run Code Online (Sandbox Code Playgroud)
我发现这个函数能够解决一些简单的三次方程:
print cubic(1, 3, 3, 1)
-1.0
Run Code Online (Sandbox Code Playgroud)
不久之前,我已经把它弄到了可以解决两个方程的方程式.我刚做了一次重写,现在它已经变得干扰了.例如,这些系数是(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)]
Run Code Online (Sandbox Code Playgroud)
这是我正在制作的数学错误还是程序错误?
更新:谢谢大家,感谢你的答案,但是这个函数存在的问题比我到目前为止所遇到的还要多.例如,我经常收到与立方根函数有关的错误:
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
Run Code Online (Sandbox Code Playgroud)
Wolfram Alpha确认了你最后一个立方体的根源确实存在
(-4, -2, 2)
Run Code Online (Sandbox Code Playgroud)
而不是你说的那样
......它应该回归
[4.0, -4.0, -2.0]
不能承认(我推测)拼写错误,你的程序给出了
[(-4+1.4802973661668753e-16j), (2+2.9605947323337506e-16j), (-2.0000000000000004-1.1842378929335002e-15j)]
Run Code Online (Sandbox Code Playgroud)
其中的精度10**(-15)是完全一样的树根作为正确的解决方案.正如其他人所说,这个微小的想象部分可能应该归结为四舍五入.
请注意,如果您使用像Cardano这样的解决方案,则必须使用精确算术才能始终正确取消.这个程序喜欢MAPLE或Mathematica存在的原因之一,往往是从公式到实现的脱节.
要调用纯python中的数字的真实部分.real.例:
a = 3.0+4.0j
print a.real
>> 3.0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2451 次 |
| 最近记录: |