(f)
return soln
Returns a list of roots of a cubic polynomial.
Returns a list of roots of a cubic polynomial.
Returns a list of roots of a cubic polynomial .
def roots_cubic(f):
    """Returns a list of roots of a cubic polynomial."""
    _, a, b, c = f.monic().all_coeffs()

    if c is S.Zero:
        x1, x2 = roots([1, a, b], multiple=True)
        return [x1, S.Zero, x2]

    p = b - a**2/3
    q = c - a*b/3 + 2*a**3/27

    pon3 = p/3
    aon3 = a/3

    if p is S.Zero:
        if q is S.Zero:
            return [-aon3]*3
        else:
            if q.is_real:
                if (q > 0) == True:
                    u1 = -q**Rational(1, 3)
                else:
                    u1 = (-q)**Rational(1, 3)
            else:
                u1 = (-q)**Rational(1, 3)
    elif q is S.Zero:
        y1, y2 = roots([1, 0, p], multiple=True)
        return [tmp - aon3 for tmp in [y1, S.Zero, y2]]
    elif q.is_real and q < 0:
        u1 = -(-q/2 + sqrt(q**2/4 + pon3**3))**Rational(1, 3)
    else:
        u1 = (q/2 + sqrt(q**2/4 + pon3**3))**Rational(1, 3)

    coeff = S.ImaginaryUnit*sqrt(3)/2

    u2 = u1*(-S.Half + coeff)
    u3 = u1*(-S.Half - coeff)

    if p is S.Zero:
        return [u1 - aon3, u2 - aon3, u3 - aon3]

    soln = [
        -u1 + pon3/u1 - aon3,
        -u2 + pon3/u2 - aon3,
        -u3 + pon3/u3 - aon3
    ]

    return soln
def roots_cubic ( f ) : _ , a , b , c = f . monic ( ) . all_coeffs ( ) if c is S . Zero : x1 , x2 = roots ( [ 1 , a , b ] , multiple = True ) return [ x1 , S . Zero , x2 ] p = b - a ** 2 / 3 q = c - a * b / 3 + 2 * a ** 3 / 27 pon3 = p / 3 aon3 = a / 3 if p is S . Zero : if q is S . Zero : return [ - aon3 ] * 3 else : if q . is_real : if ( q > 0 ) == True : u1 = - q ** Rational ( 1 , 3 ) else : u1 = ( - q ) ** Rational ( 1 , 3 ) else : u1 = ( - q ) ** Rational ( 1 , 3 ) elif q is S . Zero : y1 , y2 = roots ( [ 1 , 0 , p ] , multiple = True ) return [ tmp - aon3 for tmp in [ y1 , S . Zero , y2 ] ] elif q . is_real and q < 0 : u1 = - ( - q / 2 + sqrt ( q ** 2 / 4 + pon3 ** 3 ) ) ** Rational ( 1 , 3 ) else : u1 = ( q / 2 + sqrt ( q ** 2 / 4 + pon3 ** 3 ) ) ** Rational ( 1 , 3 ) coeff = S . ImaginaryUnit * sqrt ( 3 ) / 2 u2 = u1 * ( - S . Half + coeff ) u3 = u1 * ( - S . Half - coeff ) if p is S . Zero : return [ u1 - aon3 , u2 - aon3 , u3 - aon3 ] soln = [ - u1 + pon3 / u1 - aon3 , - u2 + pon3 / u2 - aon3 , - u3 + pon3 / u3 - aon3 ] return soln