From 1b2d778ff6dd6379e75454ae6ca66a65507f21d0 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Sat, 27 Feb 2016 12:29:05 +0300 Subject: [PATCH] pass doctest for abstract_polynom expression fraction generic and polynom --- pymath/calculus/abstract_polynom.py | 46 ++++++++-------- pymath/calculus/expression.py | 36 ++++++------- pymath/calculus/fraction.py | 84 ++++++++++++++--------------- pymath/calculus/generic.py | 6 +-- pymath/calculus/polynom.py | 67 ++--------------------- 5 files changed, 89 insertions(+), 150 deletions(-) diff --git a/pymath/calculus/abstract_polynom.py b/pymath/calculus/abstract_polynom.py index c6c7f96..eb6fa95 100644 --- a/pymath/calculus/abstract_polynom.py +++ b/pymath/calculus/abstract_polynom.py @@ -42,15 +42,15 @@ class AbstractPolynom(Explicable): >>> P = AbstractPolynom([1, 2, 3]) >>> P.mainOp - '+' + + >>> P.name 'P' >>> P._letter 'x' >>> AbstractPolynom([1]).mainOp - '*' + * >>> AbstractPolynom([0, 0, 3]).mainOp - '*' + * >>> AbstractPolynom([1, 2, 3])._letter 'x' >>> AbstractPolynom([1, 2, 3], "y")._letter @@ -139,17 +139,17 @@ class AbstractPolynom(Explicable): >>> p = AbstractPolynom() >>> p.coef_postfix([3],2) - [3, 'x', 2, '^', '*'] + [3, 'x', 2, ^, *] >>> p.coef_postfix([0],1) [] >>> p.coef_postfix([3],0) [3] >>> p.coef_postfix([3],1) - [3, 'x', '*'] + [3, 'x', *] >>> p.coef_postfix([1],1) ['x'] >>> p.coef_postfix([1],2) - ['x', 2, '^'] + ['x', 2, ^] """ ans = [] @@ -173,13 +173,13 @@ class AbstractPolynom(Explicable): >>> p = AbstractPolynom([1, 2]) >>> p.postfix_tokens - [2, 'x', '*', 1, '+'] + [2, 'x', *, 1, +] >>> p = AbstractPolynom([1, -2]) >>> p.postfix_tokens - [2, 'x', '*', '-', 1, '+'] + [2, 'x', *, -, 1, +] >>> p = AbstractPolynom([1,2,3]) >>> p.postfix_tokens - [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+'] + [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +] >>> p = AbstractPolynom([1]) >>> p.postfix_tokens [1] @@ -188,19 +188,19 @@ class AbstractPolynom(Explicable): [0] >>> p = AbstractPolynom([1,[2,3]]) >>> p.postfix_tokens - [2, 'x', '*', 3, 'x', '*', '+', 1, '+'] + [2, 'x', *, 3, 'x', *, +, 1, +] >>> p = AbstractPolynom([1,[2,-3]]) >>> p.postfix_tokens - [2, 'x', '*', 3, 'x', '*', '-', 1, '+'] + [2, 'x', *, 3, 'x', *, -, 1, +] >>> p = AbstractPolynom([1,[-2,-3]]) >>> p.postfix_tokens - [2, 'x', '*', '-', 3, 'x', '*', '-', 1, '+'] + [2, 'x', *, -, 3, 'x', *, -, 1, +] >>> from pymath.calculus.expression import Expression >>> from pymath.calculus.operator import op >>> e = Expression([2,3,op.add]) >>> p = AbstractPolynom([1,e]) >>> p.postfix_tokens - [2, 3, '+', 'x', '*', 1, '+'] + [2, 3, +, 'x', *, 1, +] """ if self == 0: @@ -312,7 +312,7 @@ class AbstractPolynom(Explicable): 6 x^{ 2 } + ( 7 + 5 ) x + 3 6 x^{ 2 } + 12 x + 3 >>> Q.steps - [< [[1, 2], [3, 4, 5], 6]>, < [< [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 @@ -397,7 +397,7 @@ class AbstractPolynom(Explicable): 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]>, < [< [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) @@ -422,7 +422,7 @@ class AbstractPolynom(Explicable): >>> Q < [-1, -2, -3]> >>> Q.steps - [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', '-'] >] + [< [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, -] >] """ ini_step = [Expression(self.postfix_tokens + [op.sub1])] ans = AbstractPolynom([-i for i in self._coef], @@ -446,7 +446,7 @@ class AbstractPolynom(Explicable): ( 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, '+'] >]>] + [< [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 + @@ -471,21 +471,21 @@ class AbstractPolynom(Explicable): >>> p*3 < [3, 6]> >>> (p*3).steps - [[< [2, 'x', '*', 1, '+', 3, '*'] >], < [3, < [2, 3, '*'] >]>] + [[< [2, 'x', *, 1, +, 3, *] >], < [3, < [2, 3, *] >]>] >>> q = AbstractPolynom([0,0,4]) >>> q*3 < [0, 0, 12]> >>> (q*3).steps - [[< [4, 'x', 2, '^', '*', 3, '*'] >], < [0, 0, < [4, 3, '*'] >]>] + [[< [4, 'x', 2, ^, *, 3, *] >], < [0, 0, < [4, 3, *] >]>] >>> r = AbstractPolynom([0,1]) >>> r*3 < [0, 3]> >>> (r*3).steps - [[< ['x', 3, '*'] >]] + [[< ['x', 3, *] >]] >>> p*q < [0, 0, 4, 8]> >>> (p*q).steps - [[< [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < [0, 0, 4, < [2, 4, '*'] >]>] + [[< [2, 'x', *, 1, +, 4, 'x', 2, ^, *, *] >], < [0, 0, 4, < [2, 4, *] >]>] >>> p*r < [0, 1, 2]> >>> P = AbstractPolynom([1,2,3]) @@ -536,12 +536,12 @@ class AbstractPolynom(Explicable): >>> p**2 < [0, 0, 0, 0, 9]> >>> (p**2).steps - [< [3, 'x', 2, '^', '*', 2, '^'] >, < [0, 0, 0, 0, < [3, 2, '^'] >]>] + [< [3, 'x', 2, ^, *, 2, ^] >, < [0, 0, 0, 0, < [3, 2, ^] >]>] >>> p = AbstractPolynom([1,2]) >>> p**2 < [1, 4, 4]> >>> (p**2).steps - [< [2, 'x', '*', 1, '+', 2, '^'] >, [< [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < [1, [2, 2], < [2, 2, '*'] >]>, < [1, < [2, 2, '+'] >, 4]>] + [< [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]> diff --git a/pymath/calculus/expression.py b/pymath/calculus/expression.py index a49573c..e647388 100644 --- a/pymath/calculus/expression.py +++ b/pymath/calculus/expression.py @@ -23,7 +23,7 @@ def pstf_factory(pstf_tokens): >>> from .operator import op >>> pstf_t = [2, 3, op.add] >>> pstf_factory(pstf_t) - < [2, 3, '+'] > + < [2, 3, +] > >>> pstf_factory([2]) 2 >>> type(pstf_factory([2])) @@ -99,11 +99,11 @@ class Expression(Explicable): >>> with Expression.tmp_render(): ... for i in exp.simplify().explain(): ... i - < [2, 3, 5, '/', '*'] > - < [2, < Fraction 3 / 5>, '*'] > - < [< Fraction 3 / 5>, 2, '*'] > - < [3, 2, '*', 5, '/'] > - < [6, 5, '/'] > + < [2, 3, 5, /, *] > + < [2, < Fraction 3 / 5>, *] > + < [< Fraction 3 / 5>, 2, *] > + < [3, 2, *, 5, /] > + < [6, 5, /] > >>> from .render import txt >>> with Expression.tmp_render(txt): ... for i in exp.simplify().explain(): @@ -330,13 +330,13 @@ class Expression(Explicable): >>> a = Expression("1+2") >>> print(a.postfix_tokens) - [1, 2, '+'] + [1, 2, +] >>> b = Expression("3+4") >>> print(b.postfix_tokens) - [3, 4, '+'] + [3, 4, +] >>> c = a + b >>> print(c.postfix_tokens) - [1, 2, '+', 3, 4, '+', '+'] + [1, 2, +, 3, 4, +, +] """ return self.operate(other, op.add) @@ -348,13 +348,13 @@ class Expression(Explicable): >>> a = Expression("1+2") >>> print(a.postfix_tokens) - [1, 2, '+'] + [1, 2, +] >>> b = Expression("3+4") >>> print(b.postfix_tokens) - [3, 4, '+'] + [3, 4, +] >>> c = a - b >>> print(c.postfix_tokens) - [1, 2, '+', 3, 4, '+', '-'] + [1, 2, +, 3, 4, +, -] """ return self.operate(other, op.sub) @@ -366,13 +366,13 @@ class Expression(Explicable): >>> a = Expression("1+2") >>> print(a.postfix_tokens) - [1, 2, '+'] + [1, 2, +] >>> b = Expression("3+4") >>> print(b.postfix_tokens) - [3, 4, '+'] + [3, 4, +] >>> c = a * b >>> print(c.postfix_tokens) - [1, 2, '+', 3, 4, '+', '*'] + [1, 2, +, 3, 4, +, *] """ return self.operate(other, op.mul) @@ -384,13 +384,13 @@ class Expression(Explicable): >>> a = Expression("1+2") >>> print(a.postfix_tokens) - [1, 2, '+'] + [1, 2, +] >>> b = Expression("3+4") >>> print(b.postfix_tokens) - [3, 4, '+'] + [3, 4, +] >>> c = a / b >>> print(c.postfix_tokens) - [1, 2, '+', 3, 4, '+', '/'] + [1, 2, +, 3, 4, +, /] >>> """ return self.operate(other, op.div) diff --git a/pymath/calculus/fraction.py b/pymath/calculus/fraction.py index 9f0e592..7b45b9f 100644 --- a/pymath/calculus/fraction.py +++ b/pymath/calculus/fraction.py @@ -98,7 +98,7 @@ class Fraction(Explicable): >>> f = Fraction(3, 5) >>> f.postfix_tokens - [3, 5, '/'] + [3, 5, /] """ if self._denom == 1: @@ -134,29 +134,29 @@ class Fraction(Explicable): >>> f + g < Fraction 7 / 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, '/'] > + < [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> >>> 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, '/'] > + < [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 >>> print("\\n".join([repr(i) for i in (f+g).steps])) - < [3, 4, '/', 5, 4, '/', '+'] > - < [3, 5, '+', 4, '/'] > - < [8, 4, '/'] > + < [3, 4, /, 5, 4, /, +] > + < [3, 5, +, 4, /] > + < [8, 4, /] > >>> f+0 < Fraction 3 / 4> >>> (f+0).steps @@ -219,12 +219,12 @@ class Fraction(Explicable): >>> f - g < Fraction -1 / 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, '/'] > + < [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 @@ -292,7 +292,7 @@ class Fraction(Explicable): >>> -f < Fraction 1 / 2> >>> (-f).steps - [< [-1, -2, '/'] >] + [< [-1, -2, /] >] """ f = Fraction(-self._num, self._denom) @@ -308,24 +308,24 @@ class Fraction(Explicable): >>> f*g < Fraction 1 / 3> >>> print("\\n".join([repr(i) for i in (f*g).steps])) - < [< Fraction 1 / 2>, < Fraction 2 / 3>, '*'] > - < [1, 2, '*', 2, 3, '*', '/'] > - < [1, 3, '/'] > + < [< Fraction 1 / 2>, < Fraction 2 / 3>, *] > + < [1, 2, *, 2, 3, *, /] > + < [1, 3, /] > >>> f * 0 0 >>> (f*0).steps - [< [< Fraction 1 / 2>, 0, '*'] >] + [< [< Fraction 1 / 2>, 0, *] >] >>> f*1 < Fraction 1 / 2> >>> (f*1).steps - [< [< Fraction 1 / 2>, 1, '*'] >] + [< [< Fraction 1 / 2>, 1, *] >] >>> f*4 2 >>> print("\\n".join([repr(i) for i in (f*4).steps])) - < [< Fraction 1 / 2>, 4, '*'] > - < [1, 2, '*', 2, '*', 1, 2, '*', '/'] > - < [1, 2, '*', 1, '/'] > - < [2, 1, '/'] > + < [< Fraction 1 / 2>, 4, *] > + < [1, 2, *, 2, *, 1, 2, *, /] > + < [1, 2, *, 1, /] > + < [2, 1, /] > """ steps = [Expression([self, other, op.mul])] @@ -442,26 +442,26 @@ class Fraction(Explicable): >>> f**0 1 >>> (f**0).steps - [< [< Fraction 3 / 4>, 0, '^'] >] + [< [< Fraction 3 / 4>, 0, ^] >] >>> f**1 < Fraction 3 / 4> >>> (f**1).steps - [< [< Fraction 3 / 4>, 1, '^'] >] + [< [< Fraction 3 / 4>, 1, ^] >] >>> f**3 < Fraction 27 / 64> >>> print("\\n".join([repr(i) for i in (f**3).steps])) - < [< Fraction 3 / 4>, 3, '^'] > - < [3, 3, '^', 4, 3, '^', '/'] > - < [27, 64, '/'] > + < [< Fraction 3 / 4>, 3, ^] > + < [3, 3, ^, 4, 3, ^, /] > + < [27, 64, /] > >>> f = Fraction(6, 4) >>> f**3 < Fraction 27 / 8> >>> print("\\n".join([repr(i) for i in (f**3).steps])) - < [< Fraction 6 / 4>, 3, '^'] > - < [6, 3, '^', 4, 3, '^', '/'] > - < [216, 64, '/'] > - < [216, 64, '/'] > - < [27, 8, '*', 8, 8, '*', '/'] > + < [< Fraction 6 / 4>, 3, ^] > + < [6, 3, ^, 4, 3, ^, /] > + < [216, 64, /] > + < [216, 64, /] > + < [27, 8, *, 8, 8, *, /] > """ if not isinstance(power, int): diff --git a/pymath/calculus/generic.py b/pymath/calculus/generic.py index 1f7700c..c412e6e 100644 --- a/pymath/calculus/generic.py +++ b/pymath/calculus/generic.py @@ -299,11 +299,11 @@ def postfix_op(numbers, operator, neutral = 0): >>> postfix_op([1], op.add) [1] >>> postfix_op([1, 2], op.add) - [1, 2, '+'] + [1, 2, +] >>> postfix_op([1, 2, 3], op.add) - [1, 2, '+', 3, '+'] + [1, 2, +, 3, +] >>> postfix_op([1, 2, 3], op.mul) - [1, 2, '*', 3, '*'] + [1, 2, *, 3, *] >>> postfix_op(1, op.add) [1] >>> postfix_op([], op.add) diff --git a/pymath/calculus/polynom.py b/pymath/calculus/polynom.py index 163bcbf..12b7fa1 100644 --- a/pymath/calculus/polynom.py +++ b/pymath/calculus/polynom.py @@ -103,15 +103,15 @@ class Polynom(AbstractPolynom): >>> P = Polynom([1, 2, 3]) >>> P.mainOp - '+' + + >>> P.name 'P' >>> P._letter 'x' >>> Polynom([1]).mainOp - '*' + * >>> Polynom([0, 0, 3]).mainOp - '*' + * >>> Polynom([1, 2, 3])._letter 'x' >>> Polynom([1, 2, 3], "y")._letter @@ -182,67 +182,6 @@ for name, func in inspect.getmembers(Polynom): setattr(Polynom, name, polynom_factory(func)) -if __name__ == '__main__': - #from .fraction import Fraction - # with Expression.tmp_render(txt): - # p = Polynom([1, 2, 3]) - # q = Polynom([4, 5, 6]) - # for i in (p*q).explain(): - # print(i) - # r = Polynom([0,1]) - # for i in (r*3).explain(): - # print(i) - # print("q = ", q) - # r = q.reduce() - # print("r = ", r) - # for i in r.explain(): - # print("q = ", i) - # print(p-q) - # for i in p-q: - # print(i) - # Polynom.random(degree = 2, conditions=["{b**2-4*a*c}>0"]) # Polynom deg - # 2 with positive Delta (ax^2 + bx + c) - - #import doctest - # doctest.testmod(optionflags=doctest.ELLIPSIS) - - # while True: - # P = Polynom.random(degree = 2) - # e = Expression.random("{a}/{b}") - # try: - # P(e) - # except RuntimeError: - # print(" P -> " + str(P)) - # print(" e -> " + str(e)) - # - # import sys - # sys.setrecursionlimit(100) - - from .fraction import Fraction - from itertools import permutations - P = Polynom([-5, 6, -4]) - f = Fraction(2, 5) - P(f) - try: - P(f) - except Exception as e: - print(e) - - print("-----------------\n") - f = Fraction(2, 15) - print(str(P).replace('x', '(' + str(f) + ')'), "= ", P(f)) - - print("-----------------\n") - f = Fraction(2, 3) - print(P(f)) - #coefs_p = [[(i-2),(j-2)] for i,j in permutations(range(20),2)] - #fractions = [Fraction(i,j) for i,j in coefs_p if j!=0] - # for f in fractions: - # try: - # P(f) - # #print("ok f -> " + str(f)) - # except RuntimeError: - # print(" f -> " + str(f)) # -----------------------------