-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py~2019-02-24_101107~
311 lines (262 loc) · 10.9 KB
/
solver.py~2019-02-24_101107~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
56
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
from __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
from sympy import sin, pi, cos, tan, asin, acos, atan, Eq, Function, exp, simplify, solveset, S
from sympy.abc import x
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
def heron(A, B, C, showSteps=False):
showStepsList = []
Svar = N((A + B + C)/2,5)
showStepsList.append("S = " + str(Svar))
Area = N(sqrt(Svar*(Svar-A)*(Svar-B)*(Svar-C)),5)
showStepsList.append("Area = " + str(Area))
if (showSteps == True):
for step in showStepsList:
print(step)
return ("Area: " + str(Area) + "\nSemi-Per: " + str(Svar))
def thesolver(A, B, C, a, b, c, method="sin", showSteps=False):
showStepsList = []
nameSolveFor = ""
canUseBoth = []
indexSFO = 0
solvingForAngle = False
solvingForSide = False
bothDict = {"A": A, "B": B, "C": C, "a": a, "b": b, "c": c}
allSides = [a, b, c]
allAngles = [A, B, C]
eq1 = 0
eq2 = 0
for key, val in bothDict.items():
if (val == "?"):
nameSolveFor = key
if(str(key.upper()) == str(key)):
showStepsList.append("We are solving for a angle.")
solvingForAngle = True
else:
showStepsList.append("We are solving for a side.")
solvingForSide = True
for i in range(3):
if (allSides[i] != "X" and allSides[i] != "?"):
if (allAngles[i] != "X" and allAngles[i] != "?"):
if (i == 0):
canUseBoth.append(1)
elif (i == 1):
canUseBoth.append(2)
elif (i == 2):
canUseBoth.append(3)
showStepsList.append("The variable we are solving for is variable " + str(nameSolveFor))
if (method == "sin"):
showStepsList.append("Here are the sets of matching known sides and angles that we can use:")
for name in canUseBoth:
if (int(name) == 1):
eq1 = (sin(int(allAngles[int(name) - 1]))/(int(allSides[int(name) - 1])))
showStepsList.append("A = " + str(allAngles[int(name) - 1]))
showStepsList.append("a = " + str(allSides[int(name) - 1]))
if (int(name) == 2):
eq1 = (sin(int(allAngles[int(name) - 1]))/(int(allSides[int(name) - 1])))
showStepsList.append("B = " + str(allAngles[int(name) - 1]))
showStepsList.append("b = " + str(allSides[int(name) - 1]))
if (int(name) == 3):
eq1 = (sin(int(allAngles[int(name) - 1]))/(int(allSides[int(name) - 1])))
showStepsList.append("C = " + str(allAngles[int(name) - 1]))
showStepsList.append("c = " + str(allSides[int(name) - 1]))
if (method == "cos"):
eq1 = (x**2)
if (str(nameSolveFor) == "A"):
indexSFO = 1
if (str(nameSolveFor) == "B"):
indexSFO = 2
if (str(nameSolveFor) == "C"):
indexSFO = 3
if (str(nameSolveFor) == "a"):
indexSFO = 4
if (str(nameSolveFor) == "b"):
indexSFO = 5
if (str(nameSolveFor) == "c"):
indexSFO = 6
if (method == "sin"):
showStepsList.append("And here is the set containg the value we are looking for:")
if (int(indexSFO) > 3):
indexhere = indexSFO
if (int(indexhere) == 1):
showStepsList.append("A = " + str(allAngles[int(indexhere) - 1]))
showStepsList.append("a = ?")
eq2 = (sin(int(allAngles[int(indexhere) - 1]))/x)
if (int(indexhere) == 2):
showStepsList.append("B = " + str(allAngles[int(indexhere) - 1]))
showStepsList.append("b = ?")
eq2 = (sin(int(allAngles[int(indexhere) - 1]))/x)
if (int(indexhere) == 3):
showStepsList.append("C = " + str(allAngles[int(indexhere) - 1]))
showStepsList.append("c = ?")
eq2 = (sin(int(allAngles[int(indexhere) - 1]))/x)
else:
indexhere = indexSFO
if (int(indexhere) == 1):
showStepsList.append("A = ?")
showStepsList.append("a = " + str(allSides[int(indexSFO) - 1]))
eq2 = (sin(x)/(allSides[int(indexSFO) - 1]))
if (int(indexhere) == 2):
showStepsList.append("B = ?")
showStepsList.append("b = " + str(allSides[int(indexSFO) - 1]))
eq2 = (sin(x)/(allSides[int(indexSFO) - 1]))
if (int(indexhere) == 3):
showStepsList.append("C = ?")
showStepsList.append("c = " + str(allSides[int(indexSFO) - 1]))
eq2 = (sin(x)/(allSides[int(indexSFO) - 1]))
if (method == "cos"):
eq2 = ((allSides[0])**2)+((allSides[1])**2)-(2*(allSides[0])*(allSides[1]))*(cos(rad(allAngles[2])))
showStepsList.append("The 2 equations on each side of the equal sign are as follows:")
showStepsList.append(eq1)
showStepsList.append(eq2)
if (showSteps == True):
for step in showStepsList:
print(step)
solveList = (solve(Eq(eq1,eq2),x))
if (method == "sin"):
return abs((deg(solveList[1])).evalf())
else:
return abs(solveList[1].evalf())
def sendtoSolver():
print("not here yet")
return "sorry"
def demoproblems():
try:
print(thesolver(80, "?", "X", 11, 9, "X", "sin", False))
except:
print("Something went wrong when trying to solve demo problem #1")
try:
print(thesolver("X", "X", 83, 15, 12, "?", "cos", False))
except:
print("Something went wrong when trying to solve demo problem #2")
def dointeractive():
print("Interactive mode activated.")
A = None;
B = None;
C = None;
a = None;
b = None;
c = None;
method = "sin"
steps = False
promptsList = ["\nA >>> ", "\nB >>> ", "\nC >>> ", "\na >>> ", "\nb >>> ", "\nc >>> ", "\nmethod >>> ", "\nsteps >>> ", "\nSolve now?\n>>> "]
while True:
for prompt in promptsList:
currindex = promptsList.index(prompt)
print("A = " + str(A) + " B = " + str(B) + " C = " + str(C) + " a = " + str(a) + " b = " + str(b) + " c = " + str(c))
if (method == "sin"):
print("Solving using the law of sines. ", end="")
else:
print("Solving using the law of cosines. ", end="")
if (steps == True):
print("Showing steps.")
else:
print("Not showing steps.")
if (currindex > 7):
uinput = input(prompt)
if (uinput.lower() == "yes"):
try:
print(thesolver(A, B, C, a, b, c, method, steps))
except:
print("Something went wrong when solving.")
break
elif (uinput.lower() == "quit"):
print("Quitting...")
exit(0)
else:
print("Okay. Returning to normal mode.")
elif (currindex == 6):
uinput = input(prompt)
if (uinput == "sin"):
method = "sin"
elif (uinput == "cos"):
method = "cos"
elif (uinput.lower() == "quit"):
print("Quitting...")
exit(0)
else:
method = "sin"
elif (currindex == 7):
uinput = input(prompt)
if (uinput.lower() == "true" or uinput.lower() == "yes"):
steps = True
elif (uinput.lower() == "false" or uinput.lower() == "no"):
steps = False
elif (uinput.lower() == "quit"):
print("Quitting...")
exit(0)
else:
steps = False
else:
uinput = input(prompt)
if (uinput.lower() == "quit"):
print("Quitting...")
exit(0)
else:
if (currindex == 0):
if (str(uinput) == "X"):
A = "X"
elif (str(uinput) == "?"):
A = "?"
else:
A = int(uinput)
elif (currindex == 1):
if (str(uinput) == "X"):
B = "X"
elif (str(uinput) == "?"):
B = "?"
else:
B = int(uinput)
elif (currindex == 2):
if (str(uinput) == "X"):
C = "X"
elif (str(uinput) == "?"):
C = "?"
else:
C = int(uinput)
elif (currindex == 3):
if (str(uinput) == "X"):
a = "X"
elif (str(uinput) == "?"):
a = "?"
else:
a = int(uinput)
elif (currindex == 4):
if (str(uinput) == "X"):
b = "X"
elif (str(uinput) == "?"):
b = "?"
else:
b = int(uinput)
elif (currindex == 5):
if (str(uinput) == "X"):
c = "X"
elif (str(uinput) == "?"):
c = "?"
else:
c = int(uinput)
def userInputLoop():
print("For now you have to use the notation as follows: ")
print(""">>> thesolver(A, B, C, a, b, c, "sin/cos", True/False)""")
print("""Where the unkown values are denoted by "X" and the variable solving for is "?". True/False = show steps""")
print("Or you can type interactive to try out interactive mode")
print("Or use this:")
print(">>> heron(A, B, C, True/False)")
print("Method and showSteps are optional args")
print("Try it!\n")
while True:
uinput = input(">>> ")
if (uinput == "quit"):
break
elif (uinput == "interactive"):
dointeractive()
else:
try:
print(eval(uinput))
except:
print("Something went wrong when trying to do evaluate the user input.")
userInputLoop()