Advertisement
WadeRollins2710

Derivative Calculator Ver 2(Still In Development)

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