I've written a python program do perform synthetic division on a polynomial. There are a few constraints (listed in the module docstring of the program) on the polynomial, mainly because I haven't had the time to work out all the edge cases. I would like feedback on everything possible, as I plan to use this to solve other polynomials. All feedback is welcome, appreciated, and considered!
Constraints
Coefficients must only be one digit, if present
Constant must be one digit, and must be present (even 0)
Constant must be positive
synthetic_division.py
"""
SYNTHETIC DIVISION CALCULATOR
This program accepts a polynomial and returns all the
x intercepts.
Program has some constraints:
- Coefficients must only be one digit, if present
- Constant must be one digit, and must be present (even 0)
- Constant must be positive
EX:
Input: x^3 -4x^2 +x +6
Output: X = [3, -1, 2]
"""
from functools import reduce
def get_coefficients(equation):
"""
Returns the coefficients of the passed polynomial. If no coefficent, then 1 is returned.
Only works for single digit coefficents, if present.
:param equation: The equation to be analyzed
"""
parts = equation.split()
coeffs = []
for part in parts:
if part[0] == "x":
coeffs.append(1)
if part[0] == "-":
coeffs.append(int(part[1]) * -1)
if part[0] == "+" and "x" in part:
if len(part) == 2:
coeffs.append(1)
else:
coeffs.append(int(part[1]))
return coeffs
def get_factors(equation):
"""
Returns a list of factors of the constant
:param n: Number to get factors from
"""
constant = int(equation[len(equation) - 1])
###########################################
"""
===========================================
Equation from StackOverflow user @agf
https://stackoverflow.com/a/6800214/8968906
===========================================
"""
factors = list(
set(
reduce(
list.__add__, (
[i, constant//i] for i in range(1, int(constant**0.5) + 1) if constant % i == 0
)
)
)
)
###########################################
#Add negatives
length = len(factors)
for i in range(length):
factors.append(-factors[i])
return factors
def synthetic_division(coefficients, factors):
"""
Performs synthetic division with the passed coefficients and factors
Returns a list of intercepts
:param coefficients: Coefficients to use in the calculation
:param factors: Factors to test
"""
coeffs = coefficients
facs = factors
ints = []
for fac in facs:
current_sum = 0
for coeff in coeffs:
current_sum += coeff
current_sum *= fac
if current_sum == 0:
ints.append(fac)
return ints
def main(equation):
"""
Gathers the coefficients, factors and intercepts from the equation
:param equation: The polynomial to be solved
"""
coefficients = get_coefficients(equation)
constant = int(equation[len(equation) - 1])
coefficients.append(constant)
factors = get_factors(equation)
intercepts = synthetic_division(coefficients, factors)
return intercepts
if __name__ == '__main__':
EQUATION = "x^3 -4x^2 +1x +6"
print(f"X = {main(EQUATION)}")
EQUATION = "x^2 +0x -1"
– shouldn't the result be[-1, 1]
? \$\endgroup\$main
method. \$\endgroup\$