Фактор квадратичного полинома в Python - PullRequest
1 голос
/ 15 февраля 2011

Поскольку факторинг квадратного уравнения в моей голове просто случается, и сделал это с тех пор, как я его выучил - как бы я начал писать квадратичный факторинг в Python?

Ответы [ 3 ]

6 голосов
/ 15 февраля 2011

Используйте квадратную формулу .

5 голосов
/ 15 февраля 2011

Улучшение ответа Китса:

Начните с полинома P(x) = a*x^2 + b*x + c.Используйте квадратную формулу (или другой другой метод по вашему выбору), чтобы найти корни от r1 и r2 до P(x) = 0.

Теперь вы можете множить P (x) как a*(x-r1)(x-r2).


Если ваш коэффициент (3x - 4) (x - 9), решение будет 3 * (x - 4/3) (x - 9).Возможно, вы захотите найти способ умножить 3 на факторы, чтобы избавиться от дроби / выглядеть красиво.В этом случае может быть полезно использовать дробную арифметику вместо двойных, чтобы вы могли лучше узнать знаменатели.

1 голос
/ 05 мая 2017

Я попытался реализовать подход Хугомга. Я украл из интернета функции «gcd» и «упростить дробь». Вот мой небрежный подход:

from math import sqrt

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def simplify_fraction(numer, denom):
    if denom == 0:
        return "Division by 0 - result undefined"

    # Remove greatest common divisor:
    common_divisor = gcd(numer, denom)
    (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
    # Note that reduced_den > 0 as documented in the gcd function.

    if common_divisor == 1:
        return (numer, denom)
    else:
        # Bunch of nonsense to make sure denominator is negative if possible
        if (reduced_den > denom):
            if (reduced_den * reduced_num < 0):
                return(-reduced_num, -reduced_den)
            else:
                return (reduced_num, reduced_den)
        else:
            return (reduced_num, reduced_den)

def quadratic_function(a,b,c):
    if (b**2-4*a*c >= 0):
        x1 = (-b+sqrt(b**2-4*a*c))/(2*a)
        x2 = (-b-sqrt(b**2-4*a*c))/(2*a)
        # Added a "-" to these next 2 values because they would be moved to the other side of the equation
        mult1 = -x1 * a
        mult2 = -x2 * a
        (num1,den1) = simplify_fraction(a,mult1)
        (num2,den2) = simplify_fraction(a,mult2)
        if ((num1 > a) or (num2 > a)):
            # simplify fraction will make too large of num and denom to try to make a sqrt work
            print("No factorization")
        else:
            # Getting ready to make the print look nice
            if (den1 > 0):
                sign1 = "+"
            else:
                sign1 = ""
            if (den2 > 0):
                sign2 = "+"
            else:
                sign2 = ""
            print("({}x{}{})({}x{}{})".format(int(num1),sign1,int(den1),int(num2),sign2,int(den2)))
    else:
        # if the part under the sqrt is negative, you have a solution with i
        print("Solutions are imaginary")
    return

# This function takes in a, b, and c from the equation:
# ax^2 + bx + c
# and prints out the factorization if there is one

quadratic_function(7,27,-4)

Если я запускаю это, я получаю вывод:

(7x-1)(1x+4)
...