remove repetition in equation solving

This commit is contained in:
Benjamin Bertrand 2016-03-26 05:38:21 +03:00
parent 0b3d162a8e
commit 58f447b3b2
2 changed files with 17 additions and 14 deletions

View File

@ -13,6 +13,12 @@ from .random_expression import RdExpression
__all__ = ['Equation']
def equals(new, old):
print("new -> ", new)
print("old -> ", old)
return new, old
class Equation(object):
"""A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown"""
@ -100,8 +106,7 @@ class Equation(object):
r = self.r_exp,
)
#@no_repetition(lambda x, y: (x[0] == y[0]) & (x[1] == y[1]))
@no_repetition()
@no_repetition(lambda x, y: (x[0] == y[0]) & (x[1] == y[1]), ['',''])
def solve(self):
r"""Solve the equation but yielding each steps
@ -110,7 +115,6 @@ class Equation(object):
... print(" = ".join([str(j) for j in i]))
x + 123 = 0
x + 123 - 123 = 0 - 123
x + 123 - 123 = 0 - 123
x + 123 - 123 = -123
x = -123
>>> e = Equation("2x = x + 2")
@ -118,7 +122,6 @@ class Equation(object):
... print(" = ".join([str(j) for j in i]))
2 x = x + 2
2 x - x = x + 2 - x
2 x - x = x + 2 - x
( 2 - 1 ) x = x - x + 2
x = ( 1 - 1 ) x + 2
x = 2
@ -126,7 +129,7 @@ class Equation(object):
>>> for i in e.solve():
... print(" = ".join([str(j) for j in i]))
2 x = 1
2 x \times 2 = 1 \times 2
\frac{ 2 x }{ 2 } = \frac{ 1 }{ 2 }
\frac{ 2 }{ 2 } x = \frac{ 1 }{ 2 }
x = \frac{ 1 }{ 2 }
>>> e = Equation("2x + 1 = 4x + 2")
@ -134,16 +137,13 @@ class Equation(object):
... print(" = ".join([str(j) for j in i]))
2 x + 1 = 4 x + 2
2 x + 1 - 1 = 4 x + 2 - 1
2 x + 1 - 1 = 4 x + 2 - 1
2 x + 1 - 1 = 4 x + 2 - 1
2 x = 4 x + 1
2 x - 4 x = 4 x + 1 - 4 x
2 x - 4 x = 4 x + 1 - 4 x
( 2 - 4 ) x = 4 x - 4 x + 1
-2 x = ( 4 - 4 ) x + 1
-2 x = 1
-2 x \times ( -2 ) = 1 \times ( -2 )
\frac{ -2 }{ -2 } x = \frac{ 1 }{ -2 }
\frac{ -2 x }{ -2 } = \frac{ 1 }{ -2 }
\frac{ -2 }{ -2 } x = \frac{ -1 }{ 2 }
x = \frac{ -1 }{ 2 }
>>> e = Equation("2x + 3x + 1 = 4x + 2")
>>> for i in e.solve():
@ -152,11 +152,8 @@ class Equation(object):
( 2 + 3 ) x + 1 = 4 x + 2
5 x + 1 = 4 x + 2
5 x + 1 - 1 = 4 x + 2 - 1
5 x + 1 - 1 = 4 x + 2 - 1
5 x + 1 - 1 = 4 x + 2 - 1
5 x = 4 x + 1
5 x - 4 x = 4 x + 1 - 4 x
5 x - 4 x = 4 x + 1 - 4 x
( 5 - 4 ) x = 4 x - 4 x + 1
x = ( 4 - 4 ) x + 1
x = 1

View File

@ -74,7 +74,13 @@ class Step(Renderable):
if self.postfix_tokens == other.postfix_tokens:
return True
except AttributeError:
return False
pass
try:
if str(self).replace(' ', '') == str(other).replace(' ',''):
return True
except TypeError:
pass
return False