From a9f1f90e8d96fafb15161e6ad0401a8297c1bd39 Mon Sep 17 00:00:00 2001 From: Lafrite Date: Mon, 6 Apr 2015 17:57:13 +0200 Subject: [PATCH] finaly finish to implement simplify and explain --- pymath/explicable.py | 25 ++++++--- pymath/expression.py | 120 +++++++++++++++++++++++++++++------------- pymath/fraction.py | 60 +++++++++++++++------ pymath/polynom.py | 16 +++--- pymath/polynomDeg2.py | 15 +++++- pymath/str2tokens.py | 2 +- 6 files changed, 168 insertions(+), 70 deletions(-) diff --git a/pymath/explicable.py b/pymath/explicable.py index 4db74b3..1db3b83 100644 --- a/pymath/explicable.py +++ b/pymath/explicable.py @@ -32,17 +32,25 @@ class Renderable(object): >>> for i in exp.simplify().explain(): ... print(i) 2 \\times \\frac{ 3 }{ 5 } + \\frac{ 3 }{ 5 } \\times 2 + \\frac{ 3 \\times 2 }{ 5 } \\frac{ 6 }{ 5 } >>> with Expression.tmp_render(txt): ... for i in exp.simplify().explain(): ... print(i) 2 * 3 / 5 + 3 / 5 * 2 + ( 3 * 2 ) / 5 6 / 5 >>> for i in exp.simplify().explain(): ... print(i) 2 \\times \\frac{ 3 }{ 5 } + \\frac{ 3 }{ 5 } \\times 2 + \\frac{ 3 \\times 2 }{ 5 } \\frac{ 6 }{ 5 } + # TODO: essayer de ne pas afficher ce changement de position. |lun. avril 6 17:29:56 CEST 2015 + """ class TmpRenderEnv(object): def __enter__(self): @@ -65,8 +73,12 @@ class Explicable(Renderable): def __init__(self, *args, **kwargs): self.steps = [] - def explain(self): - """ Generate and render steps which leed to itself """ + def explain(self, noself = True): + """ Generate and render steps which leed to itself + + :param noself: does explain return self + + """ old_s = '' # les étapes pour l'atteindre try: @@ -81,10 +93,11 @@ class Explicable(Renderable): except AttributeError: pass - # Lui même - new_s = self.STR_RENDER(self.postfix_tokens) - if new_s != old_s: - yield new_s + if noself: + # Lui même + new_s = self.STR_RENDER(self.postfix_tokens) + if new_s != old_s: + yield new_s diff --git a/pymath/expression.py b/pymath/expression.py index 04fbb85..ff5ff57 100644 --- a/pymath/expression.py +++ b/pymath/expression.py @@ -38,23 +38,30 @@ class Expression(Explicable): >>> for i in exp.simplify().explain(): ... print(i) 2 \\times \\frac{ 3 }{ 5 } + \\frac{ 3 }{ 5 } \\times 2 + \\frac{ 3 \\times 2 }{ 5 } \\frac{ 6 }{ 5 } >>> with Expression.tmp_render(): ... for i in exp.simplify().explain(): ... i < [2, 3, 5, '/', '*'] > < [2, < Fraction 3 / 5>, '*'] > - < [2, < Fraction 3 / 5>, '*'] > + < [3, 5, '/', 2, '*'] > + < [3, 2, '*', 5, '/'] > < [6, 5, '/'] > >>> from .render import txt >>> with Expression.tmp_render(txt): ... for i in exp.simplify().explain(): ... print(i) 2 * 3 / 5 + 3 / 5 * 2 + ( 3 * 2 ) / 5 6 / 5 >>> for i in exp.simplify().explain(): ... print(i) 2 \\times \\frac{ 3 }{ 5 } + \\frac{ 3 }{ 5 } \\times 2 + \\frac{ 3 \\times 2 }{ 5 } \\frac{ 6 }{ 5 } """ @@ -117,19 +124,34 @@ class Expression(Explicable): def simplify(self): """ Compute entirely the expression and return the result with .steps attribute """ + #print("\tSimplify self -> ", repr(self)) self.compute_exp() + #print("\t End Compute Simplify self -> ", self) self.simplified = self.child.simplify() - try: - self.simplified.steps = self.child.steps + self.simplified.steps - except AttributeError: - pass + #print("\t self.simplified -> ", repr(self.simplified)) + #print("\t self.child -> ", repr(self.child)) + if self.simplified != self.child: + try: + #print('\t\t in try') + #print("\t\t self.child-> ", self.child) + #print("\t\t|-> self.child.steps -> ", self.child.steps) + #print("\t\t self.simplified -> ", self.simplified) + #print("\t\t|-> self.simplified.steps -> ", self.simplified.steps) + self.simplified.steps = self.child.steps + self.simplified.steps + #print("\t\t|--> self.simplified.steps -> ", self.simplified.steps) + except AttributeError: + pass + + #print("\t self -> ", self) + #print("\t self.simplified.steps ->\n\t\t ", self.simplified.steps) + #print("\tEnd simplify self -> ", repr(self)) return self.simplified def compute_exp(self): """ Create self.child with and stock steps in it """ - child_steps = [self.postfix_tokens] + ini_step = Expression(self.postfix_tokens) tokenList = self.postfix_tokens.copy() tmpTokenList = [] @@ -185,9 +207,17 @@ class Expression(Explicable): del tokenList[0:2] tmpTokenList += tokenList - + #print("\t ----------------") + #print("\t self -> ", repr(self)) + #print("\t tmpTokenList -> ", tmpTokenList) self.child = Expression(tmpTokenList) - self.child.steps = self.develop_steps(tmpTokenList) + if self.child.postfix_tokens == ini_step.postfix_tokens: + self.child.steps = self.develop_steps(tmpTokenList) + else: + self.child.steps = [ini_step] + self.develop_steps(tmpTokenList) + #print("\t\t self -> ", repr(self)) + #print("\t self.child -> ", repr(self.child)) + #print("\t self.child.steps -> ", repr(self.child.steps)) def develop_steps(self, tokenList): """ From a list of tokens, it develops steps of each tokens """ @@ -198,10 +228,16 @@ class Expression(Explicable): tmp_steps.append([i for i in t.explain()]) else: tmp_steps.append(t) - tmp_steps = expand_list(tmp_steps) - steps = [Expression(s) for s in tmp_steps] - - return steps + #print("\t\t tokenList -> ", tokenList) + #print("\t\t 1.tmp_steps -> ", tmp_steps) + if max([len(i) if type(i) == list else 1 for i in tmp_steps]) == 1: + return [] + else: + tmp_steps = expand_list(tmp_steps)[:-1] + #print("\t\t 2.tmp_steps -> ", tmp_steps) + steps = [Expression(s) for s in tmp_steps] + #print("\t\t steps -> ", steps) + return steps @classmethod def isExpression(self, other): @@ -322,7 +358,7 @@ class Expression(Explicable): return Expression(self.postfix_tokens + [op.sub1]) -def test(exp): +def untest(exp): a = Expression(exp) b = a.simplify() @@ -342,20 +378,20 @@ if __name__ == '__main__': #Expression.set_render(txt) #exp = "2 ^ 3 * 5" - #test(exp) + #untest(exp) #exp = "2x + 5" - #test(exp) + #untest(exp) #Expression.set_render(tex) - #test(exp1) + #untest(exp1) #from pymath.operator import op #exp = [2, 3, op.pw, 5, op.mul] - #test(exp) + #untest(exp) - #test([Expression(exp1), Expression(exp), op.add]) + #untest([Expression(exp1), Expression(exp), op.add]) #exp = "1 + 3 * 5" #e = Expression(exp) @@ -363,50 +399,50 @@ if __name__ == '__main__': #print(f) #exp = "2 * 3 * 3 * 5" - #test(exp) + #untest(exp) #exp = "2 * 3 + 3 * 5" - #test(exp) + #untest(exp) #exp = "2 * ( 3 + 4 ) + 3 * 5" - #test(exp) + #untest(exp) #exp = "2 * ( 3 + 4 ) + ( 3 - 4 ) * 5" - #test(exp) + #untest(exp) # #exp = "2 * ( 2 - ( 3 + 4 ) ) + ( 3 - 4 ) * 5" - #test(exp) + #untest(exp) # #exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )" - #test(exp) + #untest(exp) # #exp = "2 + 5 * ( 3 - 4 )" - #test(exp) + #untest(exp) #exp = "( 2 + 5 ) * ( 3 - 4 )^4" - #test(exp) + #untest(exp) #exp = "( 2 + 5 ) * ( 3 * 4 )" - #test(exp) + #untest(exp) #exp = "( 2 + 5 - 1 ) / ( 3 * 4 )" - #test(exp) + #untest(exp) #exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12" - #test(exp) + #untest(exp) #exp = "( 2+ 5 )/( 3 * 4 ) + 1 / 2" - #test(exp) + #untest(exp) #exp="(-2+5)/(3*4)+1/12+5*5" - #test(exp) + #untest(exp) #exp="-2*4(12 + 1)(3-12)" - #test(exp) + #untest(exp) #exp="(-2+5)/(3*4)+1/12+5*5" - #test(exp) + #untest(exp) # TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014 #exp="-2*(-a)(12 + 1)(3-12)" @@ -415,15 +451,27 @@ if __name__ == '__main__': ## Can't handle it yet!! #exp="-(-2)" - #test(exp) + #untest(exp) #print("\n") #exp = Expression.random("({a} + 3)({b} - 1)", ["{a} > 4"]) #for i in exp.simplify(): # print(i) - import doctest - doctest.testmod() + from .fraction import Fraction + f1 = Fraction(3,5) + f2 = Fraction(5,10) + q = f1+f2 + print("---------") + print(q.steps) + print("---------") + + for i in q.explain(): + print(i) + + + #import doctest + #doctest.testmod() # ----------------------------- # Reglages pour 'vim' diff --git a/pymath/fraction.py b/pymath/fraction.py index 7a21356..63ddac7 100644 --- a/pymath/fraction.py +++ b/pymath/fraction.py @@ -136,18 +136,29 @@ class Fraction(Explicable): >>> g = Fraction(2, 3) >>> f + g < Fraction 7 / 6> - >>> (f+g).steps - [< [1, 2, '/', 2, 3, '/', '+'] >, [1, 3, '*', 2, 3, '*', '/', 2, 2, '*', 3, 2, '*', '/', '+'], [3, 6, '/', 4, 6, '/', '+'], [< Fraction 3 / 6>, < Fraction 4 / 6>, '+'], [< Fraction 3 / 6>, < Fraction 4 / 6>, '+']] + >>> print("\\n".join([repr(i) for i in (f+g).steps])) + < [1, 2, '/', 2, 3, '/', '+'] > + < [1, 3, '*', 2, 3, '*', '/', 2, 2, '*', 3, 2, '*', '/', '+'] > + < [3, 6, '/', 4, 6, '/', '+'] > + < [< Fraction 3 / 6>, < Fraction 4 / 6>, '+'] > + < [3, 6, '/', 4, 6, '/', '+'] > + < [3, 4, '+', 6, '/'] > >>> f + 2 < Fraction 5 / 2> - >>> (f+2).steps - [< [1, 2, '/', 2, '+'] >, [1, 1, '*', 2, 1, '*', '/', 2, 2, '*', 1, 2, '*', '/', '+'], [1, 2, '/', 4, 2, '/', '+'], [< Fraction 1 / 2>, < Fraction 4 / 2>, '+'], [< Fraction 1 / 2>, < Fraction 4 / 2>, '+']] + >>> print("\\n".join([repr(i) for i in (f+2).steps])) + < [1, 2, '/', 2, '+'] > + < [1, 1, '*', 2, 1, '*', '/', 2, 2, '*', 1, 2, '*', '/', '+'] > + < [1, 2, '/', 4, 2, '/', '+'] > + < [< Fraction 1 / 2>, < Fraction 4 / 2>, '+'] > + < [1, 2, '/', 4, 2, '/', '+'] > + < [1, 4, '+', 2, '/'] > >>> f = Fraction(3, 4) >>> g = Fraction(5, 4) >>> f + g 2 - >>> (f+g).steps - [< [3, 4, '/', 5, 4, '/', '+'] >, [3, 5, '+', 4, '/'], [8, 4, '/']] + >>> print("\\n".join([repr(i) for i in (f+g).steps])) + < [3, 4, '/', 5, 4, '/', '+'] > + < [3, 5, '+', 4, '/'] > >>> f+0 < Fraction 3 / 4> >>> (f+0).steps @@ -174,8 +185,8 @@ class Fraction(Explicable): exp = Expression([self._num, coef1, op.mul, self._denom, coef1, op.mul, op.div, number._num, coef2, op.mul, number._denom, coef2, op.mul, op.div,op.add]) - ini_step = Expression(self.postfix_tokens + number.postfix_tokens + [op.add]) ans = exp.simplify() + ini_step = Expression(self.postfix_tokens + number.postfix_tokens + [op.add]) ans.steps = [ini_step] + ans.steps return ans @@ -194,8 +205,13 @@ class Fraction(Explicable): >>> g = Fraction(2, 3) >>> f - g < Fraction -1 / 6> - >>> (f-g).steps - [< [1, 2, '/', 2, 3, '/', '-'] >, [1, 3, '*', 2, 3, '*', '/', 2, 2, '*', 3, 2, '*', '/', '-'], [3, 6, '/', 4, 6, '/', '-'], [< Fraction 3 / 6>, < Fraction 4 / 6>, '-'], [< Fraction 3 / 6>, < Fraction 4 / 6>, '-']] + >>> print("\\n".join([repr(i) for i in (f-g).steps])) + < [1, 2, '/', 2, 3, '/', '-'] > + < [1, 3, '*', 2, 3, '*', '/', 2, 2, '*', 3, 2, '*', '/', '-'] > + < [3, 6, '/', 4, 6, '/', '-'] > + < [< Fraction 3 / 6>, < Fraction 4 / 6>, '-'] > + < [3, 6, '/', 4, 6, '/', '-'] > + < [3, 4, '-', 6, '/'] > >>> f - 0 < Fraction 1 / 2> >>> (f-0).steps @@ -263,8 +279,12 @@ class Fraction(Explicable): >>> g = Fraction(2, 3) >>> f*g < Fraction 1 / 3> - >>> (f*g).steps - [< [1, 2, '/', 2, 3, '/', '*'] >, [1, 1, 2, '*', '*', 1, 2, '*', 3, '*', '/'], [1, 2, '*', 2, 3, '*', '/'], [2, 6, '/'], < [2, 6, '/'] >, < [1, 2, '*', 3, 2, '*', '/'] >] + >>> print("\\n".join([repr(i) for i in (f*g).steps])) + < [1, 2, '/', 2, 3, '/', '*'] > + < [1, 1, 2, '*', '*', 1, 2, '*', 3, '*', '/'] > + < [1, 2, '*', 2, 3, '*', '/'] > + < [2, 6, '/'] > + < [1, 2, '*', 3, 2, '*', '/'] > >>> f * 0 0 >>> (f*0).steps @@ -275,8 +295,10 @@ class Fraction(Explicable): [] >>> f*4 2 - >>> (f*4).steps - [< [1, 2, '/', 4, '*'] >, [1, 2, '*', 2, '*', 1, 2, '*', '/'], [2, 2, '*', 2, '/'], [4, 2, '/']] + >>> print("\\n".join([repr(i) for i in (f*4).steps])) + < [1, 2, '/', 4, '*'] > + < [1, 2, '*', 2, '*', 1, 2, '*', '/'] > + < [2, 2, '*', 2, '/'] > """ steps = [] @@ -381,13 +403,17 @@ class Fraction(Explicable): [] >>> f**3 < Fraction 27 / 64> - >>> (f**3).steps - [< [3, 4, '/', 3, '^'] >, [3, 3, '^', 4, 3, '^', '/'], [27, 64, '/'], [27, 64, '/']] + >>> print("\\n".join([repr(i) for i in (f**3).steps])) + < [3, 4, '/', 3, '^'] > + < [3, 3, '^', 4, 3, '^', '/'] > >>> f = Fraction(6, 4) >>> f**3 < Fraction 27 / 8> - >>> (f**3).steps - [< [6, 4, '/', 3, '^'] >, [6, 3, '^', 4, 3, '^', '/'], [216, 64, '/'], < [216, 64, '/'] >, < [27, 8, '*', 8, 8, '*', '/'] >] + >>> print("\\n".join([repr(i) for i in (f**3).steps])) + < [6, 4, '/', 3, '^'] > + < [6, 3, '^', 4, 3, '^', '/'] > + < [216, 64, '/'] > + < [27, 8, '*', 8, 8, '*', '/'] > """ if not type(power) == int: diff --git a/pymath/polynom.py b/pymath/polynom.py index 2bef1eb..e32ab19 100644 --- a/pymath/polynom.py +++ b/pymath/polynom.py @@ -364,7 +364,7 @@ class Polynom(Explicable): >>> Q < Polynom [3, 12, 6]> >>> Q.steps - [< Polynom [< [1, 2, '+'] >, < [3, 4, '+', 5, '+'] >, 6]>, < Polynom [< [1, 2, '+'] >, < [7, 5, '+'] >, 6]>, < Polynom [3, < [7, 5, '+'] >, 6]>] + [< Polynom [< [1, 2, '+'] >, < [3, 4, '+', 5, '+'] >, 6]>, < Polynom [3, < [7, 5, '+'] >, 6]>] """ # TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET 2015 @@ -477,7 +477,7 @@ class Polynom(Explicable): >>> R < Polynom [5, 7, 3]> >>> R.steps - [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < Polynom [< [1, 4, '+'] >, < [2, 5, '+'] >, 3]>, < Polynom [< [1, 4, '+'] >, < [2, 5, '+'] >, 3]>] + [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < Polynom [< [1, 4, '+'] >, < [2, 5, '+'] >, 3]>] """ o_poly = self.conv2poly(other) @@ -517,7 +517,7 @@ class Polynom(Explicable): >>> R < Polynom [-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, '-', '+'] >, < Polynom [< [1, -4, '+'] >, < [2, -5, '+'] >, < [3, -6, '+'] >]>, < Polynom [< [1, -4, '+'] >, < [2, -5, '+'] >, < [3, -6, '+'] >]>] + [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', 5, 'x', '*', '+', 4, '+', '-'] >, < [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', '-', 5, 'x', '*', '-', 4, '-', '+'] >, < Polynom [< [1, -4, '+'] >, < [2, -5, '+'] >, < [3, -6, '+'] >]>] """ o_poly = self.conv2poly(other) ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.sub])] @@ -541,12 +541,12 @@ class Polynom(Explicable): >>> p*3 < Polynom [3, 6]> >>> (p*3).steps - [[< [2, 'x', '*', 1, '+', 3, '*'] >], < Polynom [3, < [2, 3, '*'] >]>, < Polynom [3, < [2, 3, '*'] >]>] + [[< [2, 'x', '*', 1, '+', 3, '*'] >], < Polynom [3, < [2, 3, '*'] >]>] >>> q = Polynom([0,0,4]) >>> q*3 < Polynom [0, 0, 12]> >>> (q*3).steps - [[< [4, 'x', 2, '^', '*', 3, '*'] >], < Polynom [0, 0, < [4, 3, '*'] >]>, < Polynom [0, 0, < [4, 3, '*'] >]>] + [[< [4, 'x', 2, '^', '*', 3, '*'] >], < Polynom [0, 0, < [4, 3, '*'] >]>] >>> r = Polynom([0,1]) >>> r*3 < Polynom [0, 3]> @@ -555,7 +555,7 @@ class Polynom(Explicable): >>> p*q < Polynom [0, 0, 4, 8]> >>> (p*q).steps - [[< [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < Polynom [0, 0, 4, < [2, 4, '*'] >]>, < Polynom [0, 0, 4, < [2, 4, '*'] >]>] + [[< [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < Polynom [0, 0, 4, < [2, 4, '*'] >]>] >>> p*r < Polynom [0, 1, 2]> >>> P = Polynom([1,2,3]) @@ -606,12 +606,12 @@ class Polynom(Explicable): >>> p**2 < Polynom [0, 0, 0, 0, 9]> >>> (p**2).steps - [< [3, 'x', 2, '^', '*', 2, '^'] >, < Polynom [0, 0, 0, 0, < [3, 2, '^'] >]>, < Polynom [0, 0, 0, 0, < [3, 2, '^'] >]>] + [< [3, 'x', 2, '^', '*', 2, '^'] >, < Polynom [0, 0, 0, 0, < [3, 2, '^'] >]>] >>> p = Polynom([1,2]) >>> p**2 < Polynom [1, 4, 4]> >>> (p**2).steps - [< [2, 'x', '*', 1, '+', 2, '^'] >, [< [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < Polynom [1, < [2, 2, '+'] >, < [2, 2, '*'] >]>, < Polynom [1, < [2, 2, '+'] >, < [2, 2, '*'] >]>] + [< [2, 'x', '*', 1, '+', 2, '^'] >, [< [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < Polynom [1, < [2, 2, '+'] >, < [2, 2, '*'] >]>] >>> p = Polynom([0,0,1]) >>> p**3 < Polynom [0, 0, 0, 0, 0, 0, 1]> diff --git a/pymath/polynomDeg2.py b/pymath/polynomDeg2.py index 9a87efb..97e5aeb 100644 --- a/pymath/polynomDeg2.py +++ b/pymath/polynomDeg2.py @@ -83,7 +83,7 @@ class Polynom_deg2(Polynom): ... print(i) \\frac{ - 2 }{ 2 \\times 3 } \\frac{ -2 }{ 6 } - \\frac{ ( -1 ) \\times 2 }{ 3 \\times 2 } + \\frac{ -1 \\times 2 }{ 3 \\times 2 } \\frac{ -1 }{ 3 } """ return Expression([self.b, op.sub1, 2, self.a, op.mul, op.div]).simplify() @@ -97,10 +97,21 @@ class Polynom_deg2(Polynom): < Fraction 2 / 3> >>> for i in P.beta.explain(): # Ça serait bien que l'on puisse enlever des étapes maintenant... ... print(i) - 3 \\times \\frac{ -1 }{ 3 }^{ 2 } + 2 \\times \\frac{ -1 }{ 3 } + 1 + 3 \\times ( \\frac{ -1 }{ 3 } )^{ 2 } + 2 \\times \\frac{ -1 }{ 3 } + 1 + 3 \\times ( \\frac{ -1 }{ 3 } )^{ 2 } + \\frac{ -1 }{ 3 } \\times 2 + 1 + 3 \\times \\frac{ -1^{ 2 } }{ 3^{ 2 } } + \\frac{ -1 \\times 2 }{ 3 } + 1 3 \\times \\frac{ 1 }{ 9 } + \\frac{ -2 }{ 3 } + 1 + \\frac{ 1 }{ 9 } \\times 3 + \\frac{ -1 }{ 3 } \\times 2 + 1 + \\frac{ 1 \\times 1 \\times 3 }{ 3 \\times 3 } + \\frac{ -1 \\times 2 }{ 3 } + 1 + \\frac{ 1 \\times 3 }{ 9 } + \\frac{ -2 }{ 3 } + 1 + \\frac{ 3 }{ 9 } + \\frac{ -2 }{ 3 } + 1 + \\frac{ 1 \\times 3 }{ 3 \\times 3 } + \\frac{ -2 }{ 3 } + 1 \\frac{ 1 }{ 3 } + \\frac{ -2 }{ 3 } + 1 + \\frac{ 1 - 2 }{ 3 } + 1 \\frac{ -1 }{ 3 } + 1 + \\frac{ -1 \\times 1 }{ 3 \\times 1 } + \\frac{ 1 \\times 3 }{ 1 \\times 3 } + \\frac{ -1 }{ 3 } + \\frac{ 3 }{ 3 } + \\frac{ -1 + 3 }{ 3 } \\frac{ 2 }{ 3 } """ return self(self.alpha).simplify() diff --git a/pymath/str2tokens.py b/pymath/str2tokens.py index 7899a03..61c2eb0 100644 --- a/pymath/str2tokens.py +++ b/pymath/str2tokens.py @@ -91,7 +91,7 @@ def in2post_fix(infix_tokens): @return: the corresponding postfix list of tokens. >>> in2post_fix([op.par, 2, op.add, 5, op.sub, 1, ')', op.div, op.par, 3, op.mul, 4, ')']) - [2, 5, '+', 1, '-', 3, 4, '*', '/'] + [2, 5, 1, '-', '+', 3, 4, '*', '/'] >>> in2post_fix([op.sub1, op.par, op.sub1, 2, ')']) [2, '-', '-'] >>> in2post_fix([op.sub1, op.par, op.sub1, 2, op.add, 3, op.mul, 4, ')'])