Example #1
0
def roots_quadratic(f):
    """Returns a list of roots of a quadratic polynomial."""
    a, b, c = f.all_coeffs()
    dom = f.get_domain()
    add_comment('This equation is quadratic')

    def _simplify(expr):
        if dom.is_Composite:
            s = factor(expr)
        else:
            s = simplify(expr)
        return s

    if c is S.Zero:
        add_comment("The equation can be rewritten as")
        add_eq(Mul(f.gen, (a*f.gen + b), evaluate=False), 0)
        r0, r1 = S.Zero, -b/a
        if not dom.is_Numerical:
            r1 = _simplify(r1)
    elif b is S.Zero:
        add_comment("The equation can be rewritten as")
        add_eq(f.gen**2, -c/a)

        r = -c/a

        if not dom.is_Numerical:
            R = sqrt(_simplify(r))
        else:
            R = sqrt(r)

        r0 = R
        r1 = -R
    else:
        d = b**2 - S(4)*a*c
        add_comment('The discriminant is')
        add_eq('D', d.simplify())
        d.clear_repr()

        add_comment("Use the formulas")
        add_eq(f.gen, Mul(Add(-b, Pow(d, S(1)/2, evaluate=False), evaluate=False), Pow(Mul(S(2), a, evaluate=False), -1, evaluate=False), evaluate=False))
        add_eq(f.gen, Mul(Add(-b, Mul(-1, Pow(d, S(1)/2, evaluate=False), evaluate=False), evaluate=False), Pow(Mul(S(2), a, evaluate=False), -1, evaluate=False), evaluate=False))
        if dom.is_Numerical:
            D = sqrt(d)
            r0 = (-b + D) / (S(2)*a)
            r1 = (-b - D) / (S(2)*a)
        else:
            D = sqrt(_simplify(d))
            A = 2*a

            E = _simplify(-b/A)
            F = D/A

            r0 = E + F
            r1 = E - F
        
    add_comment("Therefore the roots of this quadratic equation are")
    add_eq(f.gen, r0)
    add_eq(f.gen, r1)
    return sorted([expand_2arg(i) for i in (r0, r1)], key=default_sort_key)
Example #2
0
def roots_quadratic(f):
    """Returns a list of roots of a quadratic polynomial. If the domain is ZZ
    then the roots will be sorted with negatives coming before positives.
    The ordering will be the same for any numerical coefficients as long as
    the assumptions tested are correct, otherwise the ordering will not be
    sorted (but will be canonical).
    """

    a, b, c = f.all_coeffs()
    dom = f.get_domain()

    def _simplify(expr):
        if dom.is_Composite:
            return factor(expr)
        else:
            return simplify(expr)

    if c is S.Zero:
        r0, r1 = S.Zero, -b/a

        if not dom.is_Numerical:
            r1 = _simplify(r1)
        elif r1.is_negative:
            r0, r1 = r1, r0
    elif b is S.Zero:
        r = -c/a
        if not dom.is_Numerical:
            r = _simplify(r)

        R = sqrt(r)
        r0 = -R
        r1 = R
    else:
        d = b**2 - 4*a*c
        A = 2*a
        B = -b/A

        if not dom.is_Numerical:
            d = _simplify(d)
            B = _simplify(B)

        D = sqrt(d)/A
        r0 = B - D
        r1 = B + D
        if a.is_negative:
            r0, r1 = r1, r0
        elif not dom.is_Numerical:
            r0, r1 = [expand_2arg(i) for i in (r0, r1)]

    return [r0, r1]
Example #3
0
def roots_quadratic(f):
    """Returns a list of roots of a quadratic polynomial. If the domain is ZZ
    then the roots will be sorted with negatives coming before positives.
    The ordering will be the same for any numerical coefficients as long as
    the assumptions tested are correct, otherwise the ordering will not be
    sorted (but will be canonical).
    """

    a, b, c = f.all_coeffs()
    dom = f.get_domain()

    def _simplify(expr):
        if dom.is_Composite:
            return factor(expr)
        else:
            return simplify(expr)

    if c is S.Zero:
        r0, r1 = S.Zero, -b / a

        if not dom.is_Numerical:
            r1 = _simplify(r1)
        elif r1.is_negative:
            r0, r1 = r1, r0
    elif b is S.Zero:
        r = -c / a
        if not dom.is_Numerical:
            r = _simplify(r)

        R = sqrt(r)
        r0 = -R
        r1 = R
    else:
        d = b**2 - 4 * a * c
        A = 2 * a
        B = -b / A

        if not dom.is_Numerical:
            d = _simplify(d)
            B = _simplify(B)

        D = sqrt(d) / A
        r0 = B - D
        r1 = B + D
        if a.is_negative:
            r0, r1 = r1, r0
        elif not dom.is_Numerical:
            r0, r1 = [expand_2arg(i) for i in (r0, r1)]

    return [r0, r1]
Example #4
0
def roots_quadratic(f):
    """Returns a list of roots of a quadratic polynomial."""
    a, b, c = f.all_coeffs()
    dom = f.get_domain()

    def _simplify(expr):
        if dom.is_Composite:
            return factor(expr)
        else:
            return simplify(expr)

    if c is S.Zero:
        r0, r1 = S.Zero, -b / a

        if not dom.is_Numerical:
            r1 = _simplify(r1)
    elif b is S.Zero:
        r = -c / a

        if not dom.is_Numerical:
            R = sqrt(_simplify(r))
        else:
            R = sqrt(r)

        r0 = R
        r1 = -R
    else:
        d = b**2 - 4 * a * c

        if dom.is_Numerical:
            D = sqrt(d)

            r0 = (-b + D) / (2 * a)
            r1 = (-b - D) / (2 * a)
        else:
            D = sqrt(_simplify(d))
            A = 2 * a

            E = _simplify(-b / A)
            F = D / A

            r0 = E + F
            r1 = E - F

    return sorted([expand_2arg(i) for i in (r0, r1)], key=default_sort_key)
def roots_quadratic(f):
    """Returns a list of roots of a quadratic polynomial."""
    a, b, c = f.all_coeffs()
    dom = f.get_domain()

    def _simplify(expr):
        if dom.is_Composite:
            return factor(expr)
        else:
            return simplify(expr)

    if c is S.Zero:
        r0, r1 = S.Zero, -b/a

        if not dom.is_Numerical:
            r1 = _simplify(r1)
    elif b is S.Zero:
        r = -c/a

        if not dom.is_Numerical:
            R = sqrt(_simplify(r))
        else:
            R = sqrt(r)

        r0 = R
        r1 = -R
    else:
        d = b**2 - 4*a*c

        if dom.is_Numerical:
            D = sqrt(d)

            r0 = (-b + D) / (2*a)
            r1 = (-b - D) / (2*a)
        else:
            D = sqrt(_simplify(d))
            A = 2*a

            E = _simplify(-b/A)
            F = D/A

            r0 = E + F
            r1 = E - F

    return sorted([expand_2arg(i) for i in (r0, r1)], key=default_sort_key)
Example #6
0
def roots_quadratic(f):
    """Returns a list of roots of a quadratic polynomial. If the domain is ZZ
    then the roots will be sorted with negatives coming before positives.
    The ordering will be the same for any numerical coefficients as long as
    the assumptions tested are correct, otherwise the ordering will not be
    sorted (but will be canonical).
    """

    a, b, c = f.all_coeffs()
    dom = f.get_domain()

    def _sqrt(d):
        # remove squares from square root since both will be represented
        # in the results; a similar thing is happening in roots() but
        # must be duplicated here because not all quadratics are binomials
        co = []
        other = []
        for di in Mul.make_args(d):
            if di.is_Pow and di.exp.is_Integer and di.exp % 2 == 0:
                co.append(Pow(di.base, di.exp//2))
            else:
                other.append(di)
        if co:
            d = Mul(*other)
            co = Mul(*co)
            return co*sqrt(d)
        return sqrt(d)

    def _simplify(expr):
        if dom.is_Composite:
            return factor(expr)
        else:
            return simplify(expr)

    if c is S.Zero:
        r0, r1 = S.Zero, -b/a

        if not dom.is_Numerical:
            r1 = _simplify(r1)
        elif r1.is_negative:
            r0, r1 = r1, r0
    elif b is S.Zero:
        r = -c/a
        if not dom.is_Numerical:
            r = _simplify(r)

        R = _sqrt(r)
        r0 = -R
        r1 = R
    else:
        d = b**2 - 4*a*c
        A = 2*a
        B = -b/A

        if not dom.is_Numerical:
            d = _simplify(d)
            B = _simplify(B)

        D = factor_terms(_sqrt(d)/A)
        r0 = B - D
        r1 = B + D
        if a.is_negative:
            r0, r1 = r1, r0
        elif not dom.is_Numerical:
            r0, r1 = [expand_2arg(i) for i in (r0, r1)]

    return [r0, r1]
Example #7
0
def roots_quadratic(f):
    """Returns a list of roots of a quadratic polynomial."""
   
    a, b, c = f.all_coeffs()
    dom = f.get_domain()
    add_comment('a * x ** 2 + b * x + c = 0')
    add_eq(f.as_expr(), 0)

    def _simplify(expr):
        if dom.is_Composite:
            s = factor(expr)
        else:
            s = simplify(expr)
        return s

    tmp = str(Poly(f.as_expr()).factor_list()).split(',')[2][1:]

    if c is S.Zero:
        x0 = 0
        x1 = -b/a
        add_eq(tmp + '1', x0)
        add_eq(tmp + '2', x1)
        r0, r1 = S.Zero, -b/a

        if not dom.is_Numerical:
            r1 = _simplify(r1)
    elif b is S.Zero:
        r = -c/a
        add_comment(tmp + ' = +-sqrt(-c/a)')
        if not dom.is_Numerical:
            R = sqrt(_simplify(r))
        else:
            R = sqrt(r)

        r0 = R
        r1 = -R
    else:
        d = b**2 - 4*a*c
        add_eq('d = b ** 2 - 4 * a * c', str(d)) # Look here!
        add_comment(tmp + '1,2 = (-b -+ sqrt(d)) / (2 * a)')
        d.clear_repr()
        
        if dom.is_Numerical:
            d = sqrt(d)

            r0 = (-b + d) / (2*a)
            r1 = (-b - d) / (2*a)
        else:
            d = sqrt(_simplify(d))
            A = 2*a

            E = _simplify(-b/A)
            F = D/A

            r0 = E + F
            r1 = E - F

    Roots = sorted([expand_2arg(i) for i in (r0, r1)], key=default_sort_key)
    x1 = Roots[0]
    x2 = Roots[1]
    add_eq(tmp + '1', x1)
    add_eq(tmp + '2', x2)
    add_comment('')
    return Roots