From deb5be095596005ae1486caaadd6cb15193999bd Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Sun, 14 Feb 2016 20:22:04 +0300 Subject: [PATCH] Create pstf_factory and fix unittest! :D:D --- pymath/calculus/expression.py | 65 ++++++++++++++++++++++++++-------- pymath/calculus/polynomDeg2.py | 5 +-- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/pymath/calculus/expression.py b/pymath/calculus/expression.py index 4faff4b..2b5ff2b 100644 --- a/pymath/calculus/expression.py +++ b/pymath/calculus/expression.py @@ -13,6 +13,53 @@ from .random_expression import RdExpression __all__ = ['Expression'] +def pstf_factory(pstf_tokens): + """Factory which tranform postfix tokens list into an Expression or the simpliest object type ready to be rendered + + :param pstf_tokens: a postfix tokens list + :returns: the object + + >>> from .operator import op + >>> pstf_t = [2, 3, op.add] + >>> pstf_factory(pstf_t) + < [2, 3, '+'] > + >>> pstf_factory([2]) + 2 + >>> type(pstf_factory([2])) + + >>> pstf_factory([2.45]) + 2.45 + >>> type(pstf_factory([2.45])) + + >>> from .fraction import Fraction + >>> f = Fraction(1,2) + >>> pstf_factory([f]) + < Fraction 1 / 2> + + """ + try: + l_pstf_token = len(pstf_tokens) + except TypeError: + if isinstance(pstf_tokens[0], int): + return Explicable_int(pstf_tokens[0]) + elif isinstance(pstf_tokens[0], float): + return Explicable_float(pstf_tokens[0]) + elif hasattr(pstf_tokens[0], 'STR_RENDER'): + return pstf_tokens[0] + else: + return Expression(self) + else: + if l_pstf_token == 1: + if isinstance(pstf_tokens[0], int): + return Explicable_int(pstf_tokens[0]) + elif isinstance(pstf_tokens[0], float): + return Explicable_float(pstf_tokens[0]) + elif hasattr(pstf_tokens[0], 'STR_RENDER'): + return pstf_tokens[0] + else: + return Expression(self) + else: + return Expression(pstf_tokens) class Expression(Explicable): """A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown""" @@ -31,7 +78,8 @@ class Expression(Explicable): return Expression(random_generator(val_min, val_max)) @classmethod - def tmp_render(cls, render=lambda _, x: Expression(x)): + def tmp_render(cls, render=lambda _, x: pstf_factory(x)): + # def tmp_render(cls, render=lambda _, x: Expression(x)): """ Same ad tmp_render for Renderable but default render is Expression >>> exp = Expression("2*3/5") @@ -48,7 +96,7 @@ class Expression(Explicable): ... i < [2, 3, 5, '/', '*'] > < [2, < Fraction 3 / 5>, '*'] > - < [3, 5, '/', 2, '*'] > + < [< Fraction 3 / 5>, 2, '*'] > < [3, 2, '*', 5, '/'] > < [6, 5, '/'] > >>> from .render import txt @@ -380,24 +428,13 @@ class Expression(Explicable): def __neg__(self): return Expression(self.postfix_tokens + [op.sub1]) + class ExpressionError(Exception): pass class ComputeError(Exception): pass -def untest(exp): - a = Expression(exp) - b = a.simplify() - - for i in b.explain(): - # print(type(i)) - print(i) - - #print(type(a.simplified()), ":", a.simplified()) - - print("\n") - if __name__ == '__main__': print('\n') A = Expression("( -8 x + 8 ) ( -8 - ( -6 x ) )") diff --git a/pymath/calculus/polynomDeg2.py b/pymath/calculus/polynomDeg2.py index c059e67..076eb25 100644 --- a/pymath/calculus/polynomDeg2.py +++ b/pymath/calculus/polynomDeg2.py @@ -126,10 +126,7 @@ class Polynom_deg2(Polynom): 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 \\times 3 }{ 3 \\times 3 } + \\frac{ -1 \\times 2 }{ 3 } + 1 \\frac{ 1 }{ 3 } + \\frac{ -2 }{ 3 } + 1 \\frac{ 1 - 2 }{ 3 } + 1 \\frac{ -1 }{ 3 } + 1