From cff75c1fecaa3b12148db851ffbe9c00943cd9c4 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Tue, 22 Mar 2016 11:48:29 +0300 Subject: [PATCH] solve eq if know ok --- pymath/calculus/equation.py | 80 ++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 23 deletions(-) diff --git a/pymath/calculus/equation.py b/pymath/calculus/equation.py index ffb91e8..b900ff5 100644 --- a/pymath/calculus/equation.py +++ b/pymath/calculus/equation.py @@ -4,6 +4,8 @@ from .explicable import Explicable from .expression import Expression +from .step import Step +from .decorators import no_repetition from .polynom import Polynom from .fraction import Fraction @@ -71,7 +73,9 @@ class Equation(object): """ Simplify left and right part, transform them into polynom and stock them in smpl_*_exp """ self.smpl_l_exp = self.l_exp.simplify() + self.smpl_l_exp.steal_history(self.l_exp) self.smpl_r_exp = self.r_exp.simplify() + self.smpl_r_exp.steal_history(self.r_exp) try: self.smpl_r_exp = self.smpl_l_exp.conv2poly(self.smpl_r_exp) @@ -96,48 +100,75 @@ class Equation(object): r = self.r_exp, ) + #@no_repetition(lambda x, y: (x[0] == y[0]) & (x[1] == y[1])) + @no_repetition() def solve(self): 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]>] + ... 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") >>> for i in e.solve(): - ... print(i) - [< Step [2, 'x', *]>, < Step ['x', 2, +]>] - [< Step ['x']>, < Step [2]>] + ... 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 >>> e = Equation("2x = 1") >>> for i in e.solve(): - ... print(i) - [< Step [2, 'x', *]>, < Step [1]>] - [< Step ['x']>, < Step [1, 2, /]>] + ... print(" = ".join([str(j) for j in i])) + 2 x = 1 + 2 x \times 2 = 1 \times 2 + \frac{ 2 }{ 2 } x = \frac{ 1 }{ 2 } + x = \frac{ 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, /]>] + ... 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 } + x = \frac{ -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]>] + ... 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 + 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 """ yield from self.gene_smpl_steps() if self.smpl_l_exp._coef[0] != 0: - yield from Equation( + eq = 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() + ) + yield from eq.solve() return try: @@ -159,6 +190,7 @@ class Equation(object): ).solve() return + @no_repetition() def gene_smpl_steps(self): r"""Generate simplification steps of the equation @@ -186,6 +218,8 @@ class Equation(object): x = \frac{ 5 }{ 3 } """ + #yield [Step(self.l_exp), Step(self.r_exp)] + for s in Explicable.merge_history( [self.smpl_l_exp, self.smpl_r_exp] ):