【问题标题】:Cardano's formula not working with numpy?卡尔达诺的公式不适用于 numpy?
【发布时间】:2016-09-13 15:52:38
【问题描述】:

--- 使用 python 3 ---

按照方程here,我试图找到任意三阶多项式的所有实根。不幸的是,我的实现没有产生正确的结果,我找不到错误。也许你能在眨眼之间发现它并告诉我。

(如您所见,只有绿色曲线的根是错误的。)

致以最诚挚的问候

import numpy as np
def find_cubic_roots(a,b,c,d):
    # with ax³ + bx² + cx + d = 0
    a,b,c,d = a+0j, b+0j, c+0j, d+0j
    all_ = (a != np.pi)

    Q = (3*a*c - b**2)/ (9*a**2)
    R = (9*a*b*c - 27*a**2*d - 2*b**3) / (54 * a**3)
    D = Q**3 + R**2
    S = (R + np.sqrt(D))**(1/3)
    T = (R - np.sqrt(D))**(1/3)

    result = np.zeros(tuple(list(a.shape) + [3])) + 0j
    result[all_,0] = - b / (3*a) + (S+T)
    result[all_,1] = - b / (3*a)  - (S+T) / 2 + 0.5j * np.sqrt(3) * (S - T)
    result[all_,2] = - b / (3*a)  - (S+T) / 2 -  0.5j * np.sqrt(3) * (S - T)

    return result

你看到的例子不起作用:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
a = np.array([2.5])
b = np.array([-5])
c = np.array([0])

x = np.linspace(-2,3,100)
for i, d in enumerate([-8,0,8]):
    d = np.array(d)
    roots = find_cubic_roots(a,b,c,d)
    ax.plot(x, a*x**3 + b*x**2 + c*x + d, label = "a = %.3f, b = %.3f, c = %.3f, d = %.3f"%(a,b,c,d), color = colors[i])
    print(roots)
    ax.plot(x, x*0)
    ax.scatter(roots,roots*0,  s = 80)
ax.legend(loc = 0)
ax.set_xlim(-2,3)
plt.show()

输出:

[[ 2.50852567+0.j        -0.25426283+1.1004545j -0.25426283-1.1004545j]]
[[ 2.+0.j  0.+0.j  0.-0.j]]
[[ 1.51400399+1.46763129j  1.02750817-1.1867528j  -0.54151216-0.28087849j]]

【问题讨论】:

  • 是 python 3 还是 python 2?因为在 python 2 1/3 = 0.
  • python3,我就写在上面了。

标签: python numpy calculus


【解决方案1】:

这是我的解决方案。如果R + np.sqrt(D)R - np.sqrt(D) 为负数,您的代码将失败。原因在this post。基本上,如果你在a 为负数的情况下执行a**(1/3),numpy 会返回一个复数。然而,我们实际上希望ST 是实数,因为负实数的立方根只是一个负实数(让我们暂时忽略De Moivre's 定理并专注于代码而不是数学)。解决它的方法是检查S 是否是真实的,将其转换为真实并将S 传递给函数from scipy.special import cbrtT 也是如此。 示例代码:

import numpy as np
import pdb
import math
from scipy.special import cbrt
def find_cubic_roots(a,b,c,d, bp = False):

    a,b,c,d = a+0j, b+0j, c+0j, d+0j
    all_ = (a != np.pi)

    Q = (3*a*c - b**2)/ (9*a**2)
    R = (9*a*b*c - 27*a**2*d - 2*b**3) / (54 * a**3)
    D = Q**3 + R**2
    S = 0 #NEW CALCULATION FOR S STARTS HERE
    if np.isreal(R + np.sqrt(D)):
        S = cbrt(np.real(R + np.sqrt(D)))
    else:
        S = (R + np.sqrt(D))**(1/3)
    T = 0 #NEW CALCULATION FOR T STARTS HERE
    if np.isreal(R - np.sqrt(D)):
        T = cbrt(np.real(R - np.sqrt(D)))
    else:
        T = (R - np.sqrt(D))**(1/3)

    result = np.zeros(tuple(list(a.shape) + [3])) + 0j
    result[all_,0] = - b / (3*a) + (S+T)
    result[all_,1] = - b / (3*a)  - (S+T) / 2 + 0.5j * np.sqrt(3) * (S - T)
    result[all_,2] = - b / (3*a)  - (S+T) / 2 -  0.5j * np.sqrt(3) * (S - T)
    #if bp:
        #pdb.set_trace()
    return result


import matplotlib.pyplot as plt
fig, ax = plt.subplots()
a = np.array([2.5])
b = np.array([-5])
c = np.array([0])
x = np.linspace(-2,3,100)
for i, d in enumerate([-8,0,8]):
    d = np.array(d)
    if d == 8:
        roots = find_cubic_roots(a,b,c,d, True)
    else:
        roots = find_cubic_roots(a,b,c,d)

    ax.plot(x, a*x**3 + b*x**2 + c*x + d, label = "a = %.3f, b = %.3f, c = %.3f, d = %.3f"%(a,b,c,d))
    print(roots)
    ax.plot(x, x*0)
    ax.scatter(roots,roots*0,  s = 80)
ax.legend(loc = 0)
ax.set_xlim(-2,3)
plt.show()

免责声明:输出根目录给出了一些警告,您可以可能忽略这些警告。输出是正确的。但是,由于某些原因,绘图显示了一个额外的根。这可能是由于您的绘图代码。不过打印出来的根看起来不错。

【讨论】:

  • @varantir 我的荣幸。
猜你喜欢
  • 2022-09-24
  • 2015-07-30
  • 2013-09-21
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多