def main(): print("Root Finding With Newton's Method") while True: try: string = input("expression > ") guess = input("guess > ") try: guess = float(guess) except Exception: raise Exception() precision = input("precision > ") try: precision = float(precision) except Exception: raise Exception() niter = input("iterations > ") try: niter = int(niter) except Exception: raise Exception() tokens = lex(string) expr = parse(tokens) root = newton(expr, guess, precision, niter) if root == None: print("No root was found.") else: print(root) print() except KeyboardInterrupt: print("Quitting...") exit(0) except Exception: print("Issue in expression or guess. ") continue
def test(): tokens = [ LN, LEFT_PAREN, 4., ADD, 3., RIGHT_PAREN, SUB, LEFT_PAREN, 2., ADD, 5., ADD, 3., ADD, 3., MUL, 2., RIGHT_PAREN ] s = "x + (x * x) * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x" tokens = lex(s) print(parse(tokens))
def test(): s = "-6 - -5 + 4 + 3 + 2 + 1 + 0 ^ 3 + x" s = "x + (x * x) * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x * x" s = "((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3) + ((1 + 2) + 3)" s = "1 + 2 * 1 * 3 ^ 4 - 1 * 3" s = "1 + 2 + 3 * 0 + 3 + 3 ^ 1 + 2 + 3 + 2 * 3 + 1 + 3" tokens = lex(s) # tokens = [LN, LEFT_PAREN, 4., ADD, 3., RIGHT_PAREN, SUB, LEFT_PAREN, # 2., ADD, 5., ADD, 3., ADD, 3., MUL, 2., RIGHT_PAREN] print(parse(tokens))
def test(): s = "3 + 4" tokens = lex(s) expr = parse(tokens) deriv = diff(expr) optimized = optimize(deriv) print(tokens) print(expr) print(deriv) print(optimized)
def main(): print("Automatic Differentiation") while True: try: string = input("> ") tokens = lex(string) expr = parse(tokens) deriv = diff(expr) optimized = optimize(deriv) print(optimized) except KeyboardInterrupt: print("Quitting...") exit(0) except Exception: print("Issue in expression. ") continue