Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in 3rd party category for Simplification by pokosasa
import numpy as np
def simplify(expr):
x = np.poly1d([1, 0])
polynomial = eval(expr)
res = ""
order=polynomial.o
for k in range(order,0,-1):
c=int(polynomial[k])
if c == 0:
continue
res += ("-" if c < 0 else "+" if k != order else "") + (str(abs(c)) + "*" if abs(c) != 1 else "") + "x" + ("**" + str(k) if k > 1 else "")
c0=int(polynomial[0])
res += "{:+}".format(c0) if c0!=0 else ""
res="0" if not res else res
return res
if __name__ == "__main__":
#These "asserts" using only for self-checking and not necessary for auto-testing
assert simplify("(x-1)*(x+1)") == "x**2-1", "First and simple"
assert simplify("(x+1)*(x+1)") == "x**2+2*x+1", "Almost the same"
assert simplify("(x+3)*x*2-x*x") == "x**2+6*x", "Different operations"
assert simplify("x+x*x+x*x*x") == "x**3+x**2+x", "Don't forget about order"
assert simplify("(2*x+3)*2-x+x*x*x*x") == "x**4+3*x+6", "All together"
assert simplify("x*x-(x-1)*(x+1)-1") == "0", "Zero"
assert simplify("5-5-x") == "-x", "Negative C1"
assert simplify("x*x*x-x*x*x-1") == "-1", "Negative C0"
Aug. 30, 2020
Comments: