多项式综合除法

时间:2018-12-05 14:42:31

标签: python python-3.x python-2.7 polynomial-math polynomials

我需要让用户输入系数和除数,并且度数必须为4。

def extended_synthetic_division(dividend, divisor):
    '''Fast polynomial division by using Extended Synthetic Division. Also works with non-monic polynomials.'''
    # dividend and divisor are both polynomials, which are here simply lists of coefficients. Eg: x^2 + 3x + 5 will be represented as [1, 3, 5]


    out = list(dividend) # Copy the dividend
    normalizer = divisor[0]
    for i in xrange(len(dividend)-(len(divisor)-1)):
        out[i] /= normalizer # for general polynomial division (when polynomials are non-monic),
                             # we need to normalize by dividing the coefficient with the divisor's first coefficient
        coef = out[i]
        if coef != 0: # useless to multiply if coef is 0
            for j in xrange(1, len(divisor)): # in synthetic division, we always skip the first coefficient of the divisor,
                                          # because it's only used to normalize the dividend coefficients
                out[i + j] += -divisor[j] * coef

    # The resulting out contains both the quotient and the remainder, the remainder being the size of the divisor (the remainder
    # has necessarily the same degree as the divisor since it's what we couldn't divide from the dividend), so we compute the index
    # where this separation is, and return the quotient and remainder.
    separator = -(len(divisor)-1)
    return out[:separator], out[separator:] # return quotient, remainder.


if __name__ == '__main__':
    print ("POLYNOMIAL SYNTHETIC DIVISION")
    N = [-2, -3, 4, 5]
    D = [1, 2]
    print ("  %s / %s =") % (N,D),
    print (" %s remainder %s") % extended_synthetic_division(N, D)

1 个答案:

答案 0 :(得分:0)

如果我理解正确的话,你可以这样做:

dividends = [int(dividend) for dividend in input("Enter the dividend values separated by comma").split(',')]
divisors = [ int(divisor) for divisor in input("Enter the divisor values seperated by comma").split(',')]