solve equation ok (need to add more steps)

This commit is contained in:
Benjamin Bertrand 2016-03-22 11:33:45 +03:00
parent 05d7d43b39
commit 1b6bd56f8a

View File

@ -2,6 +2,7 @@
# encoding: utf-8 # encoding: utf-8
from .explicable import Explicable
from .expression import Expression from .expression import Expression
from .polynom import Polynom from .polynom import Polynom
from .fraction import Fraction from .fraction import Fraction
@ -96,10 +97,99 @@ class Equation(object):
) )
def solve(self): def solve(self):
"""Solve the equation but yielding each steps r"""Solve the equation but yielding each steps
>>> e = Equation("x + 123 = 0")
>>> for i in e.solve():
... print(i)
[< Step ['x', 123, +]>, < Step [0]>]
[< Step ['x']>, < Step [-123]>]
>>> e = Equation("2x = x + 2")
>>> for i in e.solve():
... print(i)
[< Step [2, 'x', *]>, < Step ['x', 2, +]>]
[< Step ['x']>, < Step [2]>]
>>> e = Equation("2x = 1")
>>> for i in e.solve():
... print(i)
[< Step [2, 'x', *]>, < Step [1]>]
[< Step ['x']>, < Step [1, 2, /]>]
>>> e = Equation("2x + 1 = 4x + 2")
>>> for i in e.solve():
... print(i)
[< Step [2, 'x', *, 1, +]>, < Step [4, 'x', *, 2, +]>]
[< Step [2, 'x', *]>, < Step [4, 'x', *, 1, +]>]
[< Step [-2, 'x', *]>, < Step [1]>]
[< Step ['x']>, < Step [-1, 2, /]>]
>>> e = Equation("2x + 3x + 1 = 4x + 2")
>>> for i in e.solve():
... print(i)
[< Step [2, 'x', *, 3, 'x', *, +, 1, +]>, < Step [4, 'x', *, 2, +]>]
[< Step [2, 3, +, 'x', *, 1, +]>, < Step [4, 'x', *, 2, +]>]
[< Step [5, 'x', *, 1, +]>, < Step [4, 'x', *, 2, +]>]
[< Step [5, 'x', *, 1, +, 1, -]>, < Step [4, 'x', *, 2, +, 1, -]>]
[< Step [5, 'x', *]>, < Step [4, 'x', *, 1, +]>]
[< Step ['x']>, < Step [1]>]
"""
yield from self.gene_smpl_steps()
if self.smpl_l_exp._coef[0] != 0:
yield from Equation(
left_poly = self.smpl_l_exp - self.smpl_l_exp._coef[0],
right_poly = self.smpl_r_exp - self.smpl_l_exp._coef[0]
).solve()
return
try:
poly_r_part = Polynom([0, self.smpl_r_exp._coef[1]])
except IndexError:
pass
else:
if self.smpl_r_exp._coef[1] != 0:
yield from Equation(
left_poly = self.smpl_l_exp - poly_r_part,
right_poly = self.smpl_r_exp - poly_r_part
).solve()
return
if self.smpl_l_exp._coef[1] != 1:
yield from Equation(
left_poly = self.smpl_l_exp / self.smpl_l_exp._coef[1],
right_poly = self.smpl_r_exp / self.smpl_l_exp._coef[1]
).solve()
return
def gene_smpl_steps(self):
r"""Generate simplification steps of the equation
>>> e = Equation("2x + 3x + 1 = 4x + 2")
>>> e.gene_smpl_steps() # doctest:+ELLIPSIS
<generator object Equation.gene_smpl_steps at ...>
>>> for i in e.gene_smpl_steps():
... print(i)
[< Step [2, 'x', *, 3, 'x', *, +, 1, +]>, < Step [4, 'x', *, 2, +]>]
[< Step [2, 3, +, 'x', *, 1, +]>, < Step [4, 'x', *, 2, +]>]
[< Step [5, 'x', *, 1, +]>, < Step [4, 'x', *, 2, +]>]
>>> e = Equation("2x + 3x + 1 = 4x + 2")
>>> for i in e.gene_smpl_steps():
... print(" = ".join([str(j) for j in i]))
2 x + 3 x + 1 = 4 x + 2
( 2 + 3 ) x + 1 = 4 x + 2
5 x + 1 = 4 x + 2
>>> e = Equation("3x / 3 = 5 / 3")
>>> for i in e.gene_smpl_steps():
... print(" = ".join([str(j) for j in i]))
3 \times \frac{ x }{ 3 } = \frac{ 5 }{ 3 }
\frac{ x }{ 3 } \times 3 = \frac{ 5 }{ 3 }
\frac{ x \times 3 }{ 1 \times 3 } = \frac{ 5 }{ 3 }
\frac{ x }{ 1 } = \frac{ 5 }{ 3 }
x = \frac{ 5 }{ 3 }
""" """
pass for s in Explicable.merge_history(
[self.smpl_l_exp, self.smpl_r_exp]
):
yield s
def solution(self): def solution(self):
"""Return the solution of the equation """Return the solution of the equation