Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Newton and Stirling solution in Creative category for Simplification by Juge_Ti
def stirling(n, k): # wikipedia.org/wiki/Stirling_numbers_of_the_first_kind
return stirling(n-1, k-1) - (n-1)*stirling(n-1, k) if n else 0 if k else 1
def divided_diff(x, y): # wikipedia.org/wiki/Divided_differences
return ((divided_diff(x[1:], y[1:]) - divided_diff(x[:-1], y[:-1]))
/ (x[-1]-x[0])) if len(x) > 1 else y[0]
def coeffs_to_poly(coeffs):
if set(coeffs) == {0}:
return '0'
poly = ''.join([(c!=0)*((c>0)*'+'+str(c)+(i>0)*'*x'+(i>1)*('**'+str(i)))
for i, c in enumerate(map(int, coeffs))][::-1])
return poly.replace('+1*', '+').replace('-1*', '-').lstrip('+')
def simplify(expr): # wikipedia.org/wiki/Newton_polynomial
n = expr.count('x')
y = [eval(expr.replace('x', '_')) for _ in range(n+1)]
coeffs = [sum([divided_diff(range(k+1), y[:k+1]) * stirling(k, j)
for k in range(j, n+1)]) for j in range(n+1)]
return coeffs_to_poly(coeffs)
Sept. 10, 2014