A root function

# Goal: compute roots of a parabola, return real when possible,
# otherwise complex

def roots(a, b, c):
    # compute roots of a*x^2 + b*x + c = 0
    from numpy.lib.scimath import sqrt
    q = sqrt(b**2 - 4*a*c)  # q is real or complex
    r1 = (-b + q)/(2*a)
    r2 = (-b - q)/(2*a)
    return r1, r2

>>> a = 1; b = 2; c = 100
>>> roots(a, b, c)                          # complex roots
((-1+9.94987437107j), (-1-9.94987437107j))

>>> a = 1; b = 4; c = 1
>>> roots(a, b, c)                          # real roots
(-0.267949192431, -3.73205080757)

previousnexttable of contents