3次方程式の解

2019/01/02

Python2.7.8

カルダノの公式による3次方程式の解 (実数解のみ)
a.x^3 + b.x^2 + c.x + d = 0

def cubic_equation(a,b,c,d):
    p = -b**2/(9.0*a**2) + c/(3.0*a)
    q = b**3/(27.0*a**3) - b*c/(6.0*a**2) + d/(2.0*a)
    t = complex(q**2+p**3)
    w =(-1.0 +1j*3.0**0.5)/2.0

    u = [0,0,0]
    u[0] = (-q +t**0.5)**(1.0/3.0)
    u[1] = u[0] * w
    u[2] = u[0] * w**2
    v = [0,0,0]
    v[0] = (-q -t**0.5)**(1.0/3.0)
    v[1] = v[0] * w
    v[2] = v[0] * w**2

    x_list = []
    for i in range(3):
      for j in range(3):
        if abs(u[i]*v[j] + p) < 0.0001:
            x = u[i] + v[j]
            if abs(x.imag) < 0.0000001:
                x = x.real - b/(3.0*a)
                x_list.append(x)
    return x_list

if __name__ == '__main__':
    print(cubic_equation(1,-2,-11,12))



3次方程式を解く
https://pgcity.jp/2010/09/05/3%e6%ac%a1%e6%96%b9%e7%a8%8b%e5%bc%8f%e3%82%92%e8%a7%a3%e3%81%8f/

カルダノの公式と例題
https://mathtrain.jp/cardano

三次方程式の解の公式
http://hooktail.sub.jp/algebra/CubicEquation/