Last active
February 10, 2016 22:44
-
-
Save SYZYGY-DEV333/e63fd88ad860e6b8c401 to your computer and use it in GitHub Desktop.
Factor a polynomial using the roots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Polynomial Root Finder | |
# SYZYGY-DEV333 | |
# Apache 2.0 | |
step = 0.001 # smaller steps are more accurate, but slower | |
start = -10 # these parameters should be fine at -10 and 10 | |
stop = 10 | |
print "Python Polynomial Factoring" | |
print "use ** to mean ^" | |
print "Format: a*x**j + b*x**k + c (not ax^j+bx^k+c)" | |
print "examples: x**2 - 2*x - 24 , 3*x**3 + 2*x**2 + x +3" | |
while 1 == 1: | |
equ = raw_input("0 = ") # asks for your equation | |
f = lambda x: eval(equ,{"__builtins__":None},{"x":x}) # for saftey, won't accept anything other than an equation | |
sign = f(start) > 0 | |
z = start | |
while z <= stop: | |
value = f(z) | |
if value == 0: | |
print "(x +", z, ")" | |
elif (value > 0) != sign: | |
print "(x +", z, ")" | |
sign = value > 0 | |
z += step |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Polynomial Factoring using Python
This script is based on my Rootfinder, but factors it instead. I wrote it for my math class because doing it by hand is a pain. So yeah, this works. Saved me a lot of time.
How to install
Run the following commands in your Unix terminal:
Then you can run the program with
./polyfactor.py
.Have fun!