def differentiation(exp,var): if(not(var in exp)): return ["0"] diff=[] if(extract.isOperand(exp[len(exp)-1])==1): if(exp[len(exp)-1]==var): diff.append("1") return diff else: diff.append("0") return diff if(extract.isTrncdDef(exp[len(exp)-1])): op=exp[len(exp)-1] u=exp[0:len(exp)-1] du=differentiation(u,var) if(op=='sin'): diff=u+['cos']+du+['*'] return diff if(op=='cos'): diff=['0']+u+['sin']+du+['*']+['-'] return diff if(op=='exp'): diff=u+['exp']+du+['*'] return diff if(op=='log'): diff=['1']+u+['/']+du+["*"] return diff if(op=='tan'): diff=u+['sec','2','^']+du+['*'] return diff if(op=='cot'): diff=['0']+u+['cosec','2','^']+du+['*','-'] return diff if(op=='sec'): diff=u+['sec']+u+['tan','*']+du+['*'] return diff if(op=='cosec'): diff=['0']+u+['cosec']+u+['cot','*']+du+['*','-'] return diff if(op=='sinh'): diff=u+['cosh']+du+['*'] return diff if(op=='cosh'): diff=u+['sinh']+du+['*'] return diff if(op=='tanh'): diff=['1']+u+['tan','2','^','-']+du+['*'] return diff if(op=='asin'): diff=['1','1']+u+['2','^','-','0.5','^','/']+du+['*'] return diff if(op=='acos'): diff=['0','1','1']+u+['2','^','-','0.5','^','/']+du+['*','-'] return diff if(op=='atan'): diff=['1','1']+u+['2','^','+','/']+du+['*'] return diff if(op=='mod'): diff=u+u+['mod','/']+du+['*'] return diff k=0 i=len(exp)-2 while(k>=0): if(extract.isOperand(exp[i])==1): k=k-1 elif(extract.isOperator(exp[i])==1): k=k+1 i=i-1 op=exp[len(exp)-1] u=exp[0:i+1] v=exp[i+1:len(exp)-1] du=differentiation(u,var) dv=differentiation(v,var) if(op=='+'): if(du==['0']): diff=(dv) elif(dv==["0"]): diff=(du) else: diff=(du+dv+['+']) return diff elif(op=='-'): if(du==["0"]): diff=(['0']+dv+["-"]) elif(dv==["0"]): diff=(du) else: diff=(du+dv+["-"]) return diff elif(op=='*'): if(du==["0"]): diff=(u+dv+["*"]) elif(dv==["0"]): diff=(v+du+["*"]) else: diff=u+dv+["*"]+v+du+["*"]+["+"] return diff elif(op=='/'): if(dv==["0"]): diff=(du+v+["/"]) elif(du=="0"): diff=(["0"]+u+["-"]+dv+["*"]+v+["2","^","/"]) else: diff=(v+du+["*"]+u+dv+["*","-"]+v+["2","^","/"]) return diff else: if(not(var in v)): vs=[] for i in v: vs.append(i) try: vs=eval(extract.post_infix(vs)) if(vs>0): diff=[str(vs)]+u+[str(vs-1)]+['^']+['*']+du+['*'] else: diff=['0']+[str(vs*(-1))]+['-']+u+['0']+[str((vs-1)*-1)]+['-']+['^']+['*']+du+['*'] return diff except: diff=v+u+v+['1']+['-']+['^']+['*'] return diff elif(not(var in u)): diff=u+v+['^']+u+['log']+dv+['*','*'] return diff else: diff=u+v+['^']+v+u+['/']+du+['*']+dv+u+['log','*','+','/'] return diff
def CallDiff(exp,var): res=extract.argumentSeparator(exp) res=differentiation(extract.convert_infix(res),var) res= extract.post_infix(res) return res