Advertisement
WadeRollins2710

Derivative Calculator (Still In Development)

Jun 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. operator = ["(", ")", "/", "*", "+", "-", "^"]
  2. priorityDict = {"+": 1, "-": 1, "*": 2, "/": 2, "(": 0, "^": 3}
  3.  
  4.  
  5. def refine(f):
  6.     f = "".join(f.strip().split())
  7.     print(f)
  8.     result = ""
  9.     for char in f:
  10.         if char in operator:
  11.             result += " " + char + " "
  12.         else:
  13.             result += char
  14.     return result.strip()
  15.  
  16.  
  17. def getPriority(operator):
  18.     return priorityDict[operator]
  19.  
  20.  
  21. def toPostfix(f):
  22.     result = []
  23.     stack = []
  24.  
  25.     def processWord(word):
  26.         if word not in operator:
  27.             result.append(word)
  28.         else:
  29.             if word == "(":
  30.                 stack.append(word)
  31.             elif word == ")":
  32.                 char = ""
  33.                 while char != "(":
  34.                     char = stack.pop()
  35.                     if char != "(":
  36.                         result.append(char)
  37.             elif word in ["+", "-", "*", "/", "^"]:
  38.                 while stack and getPriority(word) <= getPriority(stack[-1]):
  39.                     result.append(stack.pop())
  40.                 stack.append(word)
  41.     for word in f.split():
  42.         processWord(word)
  43.     while stack:
  44.         result.append(stack.pop())
  45.     return result
  46.  
  47.  
  48. def toInfix(fStack):
  49.     stack = []
  50.     for index, item in enumerate(fStack):
  51.         if item not in operator:
  52.             stack.append(item)
  53.         else:
  54.             first = stack.pop()
  55.             second = stack.pop()
  56.             stack.append("( " + second + " " + item + " " + first + " )")
  57.     return stack[0]
  58.  
  59.  
  60. def getExp(fStack):
  61.     stack = []
  62.     for index, item in enumerate(fStack):
  63.         if item not in operator:
  64.             stack.append(item)
  65.         else:
  66.             if index == len(fStack) - 1:
  67.                 exp1 = toPostfix(refine(stack.pop()))
  68.                 exp2 = toPostfix(refine(stack.pop()))
  69.                 return (exp2, exp1)
  70.             else:
  71.                 first = stack.pop()
  72.                 second = stack.pop()
  73.                 stack.append("( " + second + " " + item + " " + first + " )")
  74.  
  75.  
  76. def getDerivative(fStack):
  77.     if len(fStack) == 1:
  78.         if fStack[0] == "x":
  79.             return ["1"]
  80.         elif fStack[0].isnumeric:
  81.             return ["0"]
  82.     else:
  83.         _operator = fStack[-1]
  84.         exp1, exp2 = getExp(fStack)
  85.         derivative1, derivative2 = getDerivative(exp1), getDerivative(exp2)
  86.         print("Derivatives: ")
  87.         print(derivative1)
  88.         print(derivative2)
  89.         if _operator == "+":
  90.             return derivative1 + derivative2 + ["+"]
  91.         elif _operator == "-":
  92.             return derivative1 + derivative2 + ["-"]
  93.         elif _operator == "*":
  94.             firstExp = derivative1 + exp2 + ["*"]
  95.             secondExp = derivative2 + exp1 + ["*"]
  96.             return firstExp + secondExp + ["-"]
  97.         elif _operator == "/":
  98.             firstExp = derivative1 + exp2 + ["*"]
  99.             secondExp = derivative2 + exp1 + ["*"]
  100.             numerator = firstExp + secondExp + ["-"]
  101.             denominator = secondExp + ["2"] + ["^"]
  102.  
  103.  
  104. print("Derivative Calculator Beta (default June 14) :: Tran Viet Anh")
  105. print("type 'operator' for help and list of math operation")
  106. print("f(x) = ", end="")
  107. function = input()
  108.  
  109. function = refine(function)
  110. print("Refined function: ", function)
  111. toPostfix(function)
  112. postFixStack = toPostfix(function)
  113. print("Postfix stack: ", postFixStack)
  114. derivativeResult = getDerivative(postFixStack)
  115. print(toInfix(derivativeResult))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement