【问题标题】:Factor a quadratic polynomial in Python在 Python 中分解一个二次多项式
【发布时间】:2011-02-15 01:02:04
【问题描述】:

既然我脑子里刚刚发生了一次因式分解,并且自从我学会了它就这样做了——我将如何开始在 Python 中编写一个二次因式分解器?

【问题讨论】:

    标签: python math quadratic factorization


    【解决方案1】:

    改进基思的回答:

    从多项式 P(x) = a*x^2 + b*x + c 开始。 使用二次公式(或您选择的其他其他方法)找到 r1r2P(x) = 0 的根。

    您现在可以将 P(x) 分解为 a*(x-r1)(x-r2)


    如果您的因数 (3x - 4)(x - 9),则解将是 3*(x - 4/3)(x - 9)。 您可能想找到一种方法将 3 乘以因子以消除分数/看起来很漂亮。在这种情况下,使用分数算术而不是双精度数可能会有所帮助,这样您就可以更好地了解分母。

    【讨论】:

    • 说它导致(x-(2/3))我试图让它说(3x-2)
    • 回答得很好,顺便说一句,虽然不是我想要的
    【解决方案2】:

    【讨论】:

    • 它们本质上是等价的。查看引用页面的二次分解部分。
    • @tekk 求解二次方程得到的结果基本上是因素。根为 3 和 5 的抛物线的分解形式为 (x-3)(x-5) :D
    • 问题是如果它像这样 (3x - 4)(x + 9) 我得到了 x 没有系数的地方
    • 您将从参考页面中看到分解是 a(x-r1)(x-r2) 其中 r1 和 r2 来自二次公式。如果你愿意,你同样可以把它写成 (ax-ar1)(x-r2)。
    • 对根使用此方法,在维基百科页面的底部:stackoverflow.com/questions/4503849/quadratic-equation-in-ada
    【解决方案3】:

    我尝试实施hugomg 的方法。我从网上偷了“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)
    

    【讨论】:

    • 当您手动考虑−8x^2 −15x+2 时,您应该得到(−8x+1)(x+2)quadratic_function(-8, -15, 2) 来自您的代码输出 No factorization。错误在哪里?
    猜你喜欢
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2018-11-06
    • 2020-07-08
    • 2017-07-27
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多