Solving a cubic equation

后端 未结 5 1456
后悔当初
后悔当初 2020-12-16 03:24

As part of a program I\'m writing, I need to solve a cubic equation exactly (rather than using a numerical root finder):

a*x**3 + b*x**2 + c*x + d = 0.
         


        
5条回答
  •  情深已故
    2020-12-16 03:59

    Wikipedia's notation (rho^(1/3), theta/3) does not mean that rho^(1/3) is the real part and theta/3 is the imaginary part. Rather, this is in polar coordinates. Thus, if you want the real part, you would take rho^(1/3) * cos(theta/3).

    I made these changes to your code and it worked for me:

    theta = arccos(r/rho)
    s_real = rho**(1./3.) * cos( theta/3)
    t_real = rho**(1./3.) * cos(-theta/3)
    

    (Of course, s_real = t_real here because cos is even.)

提交回复
热议问题