From ba579c9ab30656d5f12ef43fe68fbd0888a14e3c Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Tue, 9 Feb 2016 09:23:04 +0300 Subject: [PATCH] add missing step when multiply polynoms --- pymath/calculus/abstract_polynom.py | 54 ++++++++++++++++++----------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/pymath/calculus/abstract_polynom.py b/pymath/calculus/abstract_polynom.py index 5e0bfcd..6faab70 100644 --- a/pymath/calculus/abstract_polynom.py +++ b/pymath/calculus/abstract_polynom.py @@ -156,7 +156,6 @@ class AbstractPolynom(Explicable): ['x', 2, '^'] """ - # TODO: Couille certaine avec txt à qui il fait donner des opérateurs tout beau! |mar. nov. 11 13:08:35 CET 2014 ans =[] if a == [0]: pass @@ -309,37 +308,46 @@ class AbstractPolynom(Explicable): < [3, 12, 6]> >>> for i in Q.explain(): ... print(i) + 6 x^{ 2 } + 3 x + 4 x + 5 x + 1 + 2 6 x^{ 2 } + ( 3 + 4 + 5 ) x + 1 + 2 6 x^{ 2 } + ( 7 + 5 ) x + 3 6 x^{ 2 } + 12 x + 3 >>> Q.steps - [< [< [1, 2, '+'] >, < [3, 4, '+', 5, '+'] >, 6]>, < [3, < [7, 5, '+'] >, 6]>] + [< [[1, 2], [3, 4, 5], 6]>, < [< [1, 2, '+'] >, < [3, 4, '+', 5, '+'] >, 6]>, < [3, < [7, 5, '+'] >, 6]>] """ # TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET 2015 - # gather steps for every coeficients + # gather steps for every coefficients coefs_steps = [] for coef in self._coef: coef_steps = [] - if type(coef) == list: - # On converti en postfix avec une addition - postfix_add = self.postfix_add([i for i in coef if i!=0]) - # On converti en Expression + + if type(coef) ==list: + # Simplify each element before adding them + s = [] + for c in coef: + try: + with Expression.tmp_render(): + s.append(list(c.simplify().explain())) + except AttributeError: + s.append([c]) + + s = list(transpose_fill(s)) + + last = s[-1] + coef_steps += s + + # Convert last element into postfix addition. + postfix_add = self.postfix_add([i for i in last if i!=0]) + # Convert it to an expression coef_exp = Expression(postfix_add) with Expression.tmp_render(): - coef_steps = list(coef_exp.simplify().explain()) + coef_steps += list(coef_exp.simplify().explain()) #print('\t 1.coef_steps -> ', coef_steps) - elif type(coef) == Expression: - - with Expression.tmp_render(): - coef_steps = list(coef.simplify().explain()) - - #print('\t 2.coef_steps -> ', coef_steps) - else: try: with Expression.tmp_render(): @@ -408,8 +416,14 @@ class AbstractPolynom(Explicable): >>> R = P+Q >>> R < [5, 7, 3]> + >>> for i in R.explain(): + ... print(i) + 3 x^{ 2 } + 2 x + 1 + 5 x + 4 + 3 x^{ 2 } + 2 x + 5 x + 1 + 4 + 3 x^{ 2 } + ( 2 + 5 ) x + 1 + 4 + 3 x^{ 2 } + 7 x + 5 >>> R.steps - [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < [< [1, 4, '+'] >, < [2, 5, '+'] >, 3]>] + [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < [[1, 4], [2, 5], 3]>, < [< [1, 4, '+'] >, < [2, 5, '+'] >, 3]>] """ o_poly = self.conv2poly(other) @@ -448,14 +462,15 @@ class AbstractPolynom(Explicable): >>> R = P - Q >>> R < [-3, -3, -3]> - >>> R.steps - [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', 5, 'x', '*', '+', 4, '+', '-'] >, < [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', '-', 5, 'x', '*', '-', 4, '-', '+'] >, < [< [1, -4, '+'] >, < [2, -5, '+'] >, < [3, -6, '+'] >]>] >>> for i in R.explain(): ... print(i) 3 x^{ 2 } + 2 x + 1 - ( 6 x^{ 2 } + 5 x + 4 ) 3 x^{ 2 } + 2 x + 1 - 6 x^{ 2 } - 5 x - 4 + 3 x^{ 2 } - 6 x^{ 2 } + 2 x - 5 x + 1 - 4 ( 3 - 6 ) x^{ 2 } + ( 2 - 5 ) x + 1 - 4 - 3 x^{ 2 } - 3 x - 3 + >>> R.steps + [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', 5, 'x', '*', '+', 4, '+', '-'] >, < [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', '-', 5, 'x', '*', '-', 4, '-', '+'] >, < [[1, -4], [2, -5], [3, -6]]>, < [< [1, -4, '+'] >, < [2, -5, '+'] >, < [3, -6, '+'] >]>] """ o_poly = self.conv2poly(other) ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.sub])] @@ -501,7 +516,6 @@ class AbstractPolynom(Explicable): >>> P*Q < [4, 13, 28, 27, 18]> """ - # TODO: Je trouve qu'elle grille trop d'étapes... |ven. févr. 27 19:08:44 CET 2015 o_poly = self.conv2poly(other) coefs = [0]*(self.degree + o_poly.degree + 1) @@ -549,7 +563,7 @@ class AbstractPolynom(Explicable): >>> p**2 < [1, 4, 4]> >>> (p**2).steps - [< [2, 'x', '*', 1, '+', 2, '^'] >, [< [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < [1, < [2, 2, '+'] >, < [2, 2, '*'] >]>] + [< [2, 'x', '*', 1, '+', 2, '^'] >, [< [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < [1, [2, 2], < [2, 2, '*'] >]>, < [1, < [2, 2, '+'] >, 4]>] >>> p = AbstractPolynom([0,0,1]) >>> p**3 < [0, 0, 0, 0, 0, 0, 1]>