-
Notifications
You must be signed in to change notification settings - Fork 5
/
equationsolvers.py
273 lines (232 loc) · 6.94 KB
/
equationsolvers.py
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
#!/usr/bin/env python3
from sympy import Eq, latex
from sympy.parsing.sympy_parser import (parse_expr, convert_equals_signs,
implicit_multiplication, standard_transformations)
from textwrap import dedent
# Linear Equation Solver
def linear_solver(sub):
r"""Linear Equation Checker/Solver.
Checks whether a given string is a linear equation in one variable,
and if so, returns an explanation of how to solve it.
Parameters
----------
sub : str
The submitted expression, as a math string, to be passed to SymPy.
Returns
-------
explanation:
False if unable to parse as linear,
A worked thorugh $\LaTeX$ explanation otherwise.
Examples
--------
>>> linear_solver("")
False
>>> linear_solver("something abstract")
False
>>> linear_solver("x+1")
False
>>> linear_solver("x**2+1=1")
False
>>> print(linear_solver("x=1"))
Let's solve the equation:
\[
x = 1
\]
The equation is in the form $x = 1$;
That is, the value of $x$ is $1$.
>>> print(linear_solver("x=0"))
Let's solve the equation:
\[
x = 0
\]
The equation is in the form $x = 0$;
That is, the value of $x$ is $0$.
>>> print(linear_solver("a-1=1"))
Let's solve the equation:
\[
a - 1 = 1
\]
First, we subtract -1 from both sides:
\begin{align*}
(a - 1)-(-1) &= 1-(-1) \\
a &= 2
\end{align*}
The equation is in the form $a = 2$;
That is, the value of $a$ is $2$.
>>> print(linear_solver("5/3y=2"))
Let's solve the equation:
\[
\frac{5 y}{3} = 2
\]
We have just one term on the left:
The variable $y$ with coefficient $\frac{5}{3}$.
Divide both sides by $\frac{5}{3}$:
\begin{align*}
\frac{ \frac{5 y}{3} }{ \frac{5}{3} } &=
\frac{ 2 }{ \frac{5}{3} } \\
y &= \frac{6}{5}
\end{align*}
The equation is in the form $y = \frac{6}{5}$;
That is, the value of $y$ is $\frac{6}{5}$.
>>> print(linear_solver("3a-1=1"))
Let's solve the equation:
\[
3 a - 1 = 1
\]
First, we subtract -1 from both sides:
\begin{align*}
(3 a - 1)-(-1) &= 1-(-1) \\
3 a &= 2
\end{align*}
We have just one term on the left:
The variable $a$ with coefficient $3$.
Divide both sides by $3$:
\begin{align*}
\frac{ 3 a }{ 3 } &=
\frac{ 2 }{ 3 } \\
a &= \frac{2}{3}
\end{align*}
The equation is in the form $a = \frac{2}{3}$;
That is, the value of $a$ is $\frac{2}{3}$.
>>> print(linear_solver("a-1=1"))
Let's solve the equation:
\[
a - 1 = 1
\]
First, we subtract -1 from both sides:
\begin{align*}
(a - 1)-(-1) &= 1-(-1) \\
a &= 2
\end{align*}
The equation is in the form $a = 2$;
That is, the value of $a$ is $2$.
"""
# Check if SymPy can parse the expression as an equation
try:
expr = parse_expr(sub,
transformations=(*standard_transformations,
implicit_multiplication,
convert_equals_signs))
except (SyntaxError, ValueError):
return False
# Verify the structure of the equation
# Check if the expression is in 1 variable
variables = expr.free_symbols
if len(variables) != 1:
return False
x, = variables
# Check if it is a linear equation
if not isinstance(expr, Eq):
return False
if not expr.rhs.is_constant():
return False
if not expr.lhs.diff(x).is_constant():
return False
# Now that we know the structure of the equation,
# we can turn it into a worked-through solution.
explanation = dedent("""\
Let's solve the equation:
\\[
{expression}
\\]
""".format(expression=latex(expr)))
lhs = expr.lhs
rhs = expr.rhs
coeff = lhs.coeff(x)
left_constant = lhs - coeff*x
# Use conditional blocks to construct content that only sometimes shows up.
if not left_constant.is_zero:
new_rhs = rhs - left_constant
new_lhs = lhs - left_constant
explanation += dedent("""\
First, we subtract {left_constant} from both sides:
\\begin{{align*}}
({old_lhs})-({left_constant}) &= {old_rhs}-({left_constant}) \\\\
{new_lhs} &= {new_rhs}
\\end{{align*}}
""".format(left_constant = left_constant,
old_lhs = latex(lhs),
old_rhs = latex(rhs),
new_lhs = latex(new_lhs),
new_rhs = latex(new_rhs),
))
lhs = new_lhs
rhs = new_rhs
if not coeff == 1:
new_rhs = rhs/coeff
new_lhs = lhs/coeff
explanation += dedent("""\
We have just one term on the left:
The variable ${variable}$ with coefficient ${coefficient}$.
Divide both sides by ${coefficient}$:
\\begin{{align*}}
\\frac{{ {old_lhs} }}{{ {coefficient} }} &=
\\frac{{ {old_rhs} }}{{ {coefficient} }} \\\\
{new_lhs} &= {new_rhs}
\\end{{align*}}
""".format(coefficient = latex(coeff),
variable = latex(x),
old_lhs = latex(lhs),
old_rhs = latex(rhs),
new_lhs = latex(new_lhs),
new_rhs = latex(new_rhs),
))
lhs = new_lhs
rhs = new_rhs
explanation += dedent("""\
The equation is in the form ${variable} = {value}$;
That is, the value of ${variable}$ is ${value}$.""".format(
variable = latex(x),
value = latex(rhs)))
return explanation
# Exponential
# Examples:
# "e^x-5=2"
# "1+2e^(x-1)=5"
# "1-2e^(x-1)=5"
def exponential_solver(sub):
return False
# Logarithm
# Examples:
# "ln(x)=3"
# "ln(2x)-1=4"
# "ln(3x)+3=2"
# "ln(3a+1)-1=3"
# As a challenge,
# You can also choose to support other formats of log
def logarithm_solver(sub):
return False
# Square Roots
# Examples:
# "sqrt(x+1)=2"
# "2sqrt(2x-3)+3=5"
# "1-2sqrt(2-x)=3"
# As a challenge, you can consider other roots like ^(1/3).
def square_root_solver(sub):
return False
# Quadratic Equation Solver
# Examples:
# "x**2+2x+1=0"
# "y**2+1=0"
# "z**2+3z+2=0"
def quadratic_solver(sub):
return False
# Systems of Linear Equations Solver
# Examples:
# "a+2b = 1,a-b=3"
# "3x+2/3y=5/2,5x-y=2"
# You can do it in only two dimensions if you want,
# or challenge yourself to do it for more.
def system_of_linear_equations_solver(sub):
return False
# Export solvers as a list
equation_solvers = (linear_solver,
quadratic_solver,
logarithm_solver,
exponential_solver,
square_root_solver,
system_of_linear_equations_solver,
)
if __name__ == "__main__":
import doctest
doctest.testmod()