Newer
Older
from sympy import *
'''
getUnit '{} () \\' 独立表达模块
getIndepend 'abcd' 独立符号
getContinuous '1234. a^b a_b {x'}' 连续符号
getElement '* / ' 浅连续表达
getAll '+ -' 强连续表达(全部)
'=' 全表达式
'''
syms={
"alpha","beta","gamma","delta","varepsilon","varphi","lambda","mu","rho","sigma","omega","Gamma","Delta","Theta","Lambda","Xi","Pi","Sigma","Upsilon","Phi","Psi","Omega"
}
basicNumber="1234567890."
def warning(warn):
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
res=''
if E[0]=='{' or E[0]=='(':
res,E=getAll(E[1:],dep)
elif E[:5]=='\\frac':
elem1,E=getUnit(E[5:],dep)
elem2,E=getUnit(E,dep)
res=elem1/elem2
elif E[:5]=='\\sqrt':
res,E=getUnit(E[5:],dep)
res=sqrt(res)
elif E[:8]=='\\nthroot':
elem1,E=getUnit(E[8:],dep)
elem2,E=getUnit(E,dep)
res=pow(elem1,1/elem2)
elif E[:5]=='\\sum^':#inf \\sum^{high}_{n=low}elem
high,E=getIndepend(E[5:],dep)
high=int(high)
n,E=getContinuous(E[2:],dep)
n=str(n)
low,E=getAll(E[1:],dep)
low=int(low)
elem,E=getElement(E,dep)
res=0
for i in range(low,high+1):
res+=elem.evalf(subs={n:i})
elif E[:5]=='\\sum_':#inf \\sum_{n=low}^{high}elem
n,E=getContinuous(E[6:],dep)
n=str(n)
low,E=getAll(E[1:],dep)
low=int(low)
high,E=getIndepend(E[1:],dep)
high=int(high)
elem,E=getElement(E,dep)
res=0
for i in range(low,high+1):
res+=elem.evalf(subs={n:i})
elif E[:5]=='\\log_':
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
log_,E=getIndepend(E[5:],dep)
elem,E=getElement(E,dep)
res=log(elem)/log(log_)
elif E[:5]=='\\ln':
elem,E=getContinuous(E,dep)
res=log(elem)
elif E[:4]=='\\cos':
elem,E=getContinuous(E[4:],dep)
res=cos(elem)
elif E[:4]=='\\sin':
elem,E=getContinuous(E[4:],dep)
res=sin(elem)
elif E[:4]=='\\tan':
elem,E=getContinuous(E[4:],dep)
res=tan(elem)
elif E[:4]=='\\cot':
elem,E=getContinuous(E[4:],dep)
res=cot(elem)
elif E[:5]=='\\acos':
elem,E=getContinuous(E[4:],dep)
res=acos(elem)
elif E[:5]=='\\asin':
elem,E=getContinuous(E[4:],dep)
res=asin(elem)
elif E[:5]=='\\atan':
elem,E=getContinuous(E[4:],dep)
res=atan(elem)
elif E[:5]=='\\acot':
elem,E=getContinuous(E[4:],dep)
res=acot(elem)
else:
for s in syms:
if E[:len(s)+1]=='\\'+s:
res=symbols(s)
E=E[len(s)+1:]
break
return res,E
def getIndependStr(E,dep):
cnt=0
s=""
for i in E:
if i=='{':
cnt+=1
elif i=='}':
cnt-=1
s+=i
if cnt==0:
return s,E[len(s):]
def getContinuous(E,dep):
dep=dep+1
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
s=""
for i in E:
if i in basicNumber:
s=s+i
else:
break
if s!='':
elem=s
try:
res=int(elem)
except:
res=float(elem)
E=E[len(elem):]
else:
res,E=getIndepend(E,dep)
elem=str(res)
if E!='':
if E[0]=='^':
elemSuper,E=getIndependStr(E[1:],dep)
if E!='' and E[0]=='_':
elemSub,E=getIndependStr(E[1:],dep)
res=symbols(elem+'_'+elemSub)
try:
elemSuper,tmp=getIndepend(elemSuper)
res=pow(res,elemSuper)
except:
res=symbols(s+'^'+elemSuper)
elif E[0]=='_':
elemSub,E=getIndependStr(E[1:],dep)
res=symbols(elem+'_'+elemSub)
if E!='' and E[0]=='^':
try:
elemSuper,E=getIndepend(E[1:],dep)
res=pow(res,elemSuper)
except:
elemSuper,E=getIndependStr(E[1:],dep)
res=symbols(res+'^'+elemSuper)
res,E=getContinuous(E,dep)
while E!='':
if E[0]=='*':
elem,E=getContinuous(E[1:],dep)
res=res*elem
elif E[:5]=='\\cdot':
elem,E=getContinuous(E[5:],dep)
res=res*elem
elif E[:6]=='\\times':
elem,E=getContinuous(E[6:],dep)
res=res*elem
elif E[0]=='/':
elem,E=getContinuous(E[1:],dep)
try:
res=res/elem
except ZeroDivisionError as e:
print(e)
elif E[:4]=='\\div':
elem,E=getContinuous(E[4:],dep)
try:
res=res/elem
except ZeroDivisionError as e:
print(e)
elif E[0]!='+' and E[0]!='-' and E[0]!='}' and E[0]!=')':
elem,E=getContinuous(E,dep)
res=res*elem
else:
break
if E[0]=='+':
res,E=getElement(E[1:],dep)
elif E[0]=='-':
res,E=getElement(E[1:],dep)
res=-res
else:
res,E=getElement(E,dep)
while E!='':
if E[0]=='+':
elem,E=getElement(E[1:],dep)
res=res+elem
elif E[0]=='-':
elem,E=getElement(E[1:],dep)
res=res-elem
else:
break
E=E[1:]