From 881de6dcb127fc7af5c9df0e53f1a4813be03706 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Thu, 7 Jan 2016 19:34:23 +0300 Subject: [PATCH] start to reorganise files --- pymath/__init__.py | 6 +- pymath/calculus/__init__.py | 14 +++++ pymath/{ => calculus}/abstract_polynom.py | 58 +++++++++--------- pymath/{ => calculus}/arithmetic.py | 0 pymath/{ => calculus}/explicable.py | 0 pymath/{ => calculus}/expression.py | 10 ++-- pymath/{ => calculus}/fraction.py | 70 +++++++++++----------- pymath/{ => calculus}/generic.py | 6 +- pymath/{ => calculus}/operator.py | 0 pymath/{ => calculus}/polynom.py | 12 ++-- pymath/{ => calculus}/polynomDeg2.py | 0 pymath/{ => calculus}/random_expression.py | 0 pymath/{ => calculus}/render.py | 0 pymath/{ => calculus}/str2tokens.py | 6 +- 14 files changed, 96 insertions(+), 86 deletions(-) create mode 100644 pymath/calculus/__init__.py rename pymath/{ => calculus}/abstract_polynom.py (80%) rename pymath/{ => calculus}/arithmetic.py (100%) rename pymath/{ => calculus}/explicable.py (100%) rename pymath/{ => calculus}/expression.py (96%) rename pymath/{ => calculus}/fraction.py (81%) rename pymath/{ => calculus}/generic.py (98%) rename pymath/{ => calculus}/operator.py (100%) rename pymath/{ => calculus}/polynom.py (95%) rename pymath/{ => calculus}/polynomDeg2.py (100%) rename pymath/{ => calculus}/random_expression.py (100%) rename pymath/{ => calculus}/render.py (100%) rename pymath/{ => calculus}/str2tokens.py (97%) diff --git a/pymath/__init__.py b/pymath/__init__.py index 294f52c..44a69a2 100644 --- a/pymath/__init__.py +++ b/pymath/__init__.py @@ -1,11 +1,7 @@ #!/usr/bin/env python # encoding: utf-8 -from .expression import Expression -from .polynom import Polynom -from .fraction import Fraction -from .random_expression import random_str -from .render import txt,tex +from .calculus import * # ----------------------------- diff --git a/pymath/calculus/__init__.py b/pymath/calculus/__init__.py new file mode 100644 index 0000000..13f43cf --- /dev/null +++ b/pymath/calculus/__init__.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +# encoding: utf-8 + +from .expression import Expression +from .polynom import Polynom +from .fraction import Fraction +from .random_expression import random_str +#from .render import txt,tex + + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del diff --git a/pymath/abstract_polynom.py b/pymath/calculus/abstract_polynom.py similarity index 80% rename from pymath/abstract_polynom.py rename to pymath/calculus/abstract_polynom.py index bc2fcf6..5e0bfcd 100644 --- a/pymath/abstract_polynom.py +++ b/pymath/calculus/abstract_polynom.py @@ -199,8 +199,8 @@ class AbstractPolynom(Explicable): >>> p = AbstractPolynom([1,[-2,-3]]) >>> p.postfix_tokens [2, 'x', '*', '-', 3, 'x', '*', '-', 1, '+'] - >>> from pymath.expression import Expression - >>> from pymath.operator import op + >>> 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 @@ -280,9 +280,9 @@ class AbstractPolynom(Explicable): >>> P = AbstractPolynom([1,2,3]) >>> P.conv2poly(1) - < [1]> + < [1]> >>> P.conv2poly(0) - < [0]> + < [0]> """ if isNumber(other) and not isPolynom(other): @@ -300,20 +300,20 @@ class AbstractPolynom(Explicable): >>> P = AbstractPolynom([1,2,3]) >>> Q = P.reduce() >>> Q - < [1, 2, 3]> + < [1, 2, 3]> >>> Q.steps [] >>> P = AbstractPolynom([[1,2], [3,4,5], 6]) >>> Q = P.reduce() >>> Q - < [3, 12, 6]> + < [3, 12, 6]> >>> for i in Q.explain(): ... print(i) 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]>, < [3, < [7, 5, '+'] >, 6]>] """ # TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET 2015 @@ -407,9 +407,9 @@ class AbstractPolynom(Explicable): >>> Q = AbstractPolynom([4,5]) >>> R = P+Q >>> R - < [5, 7, 3]> + < [5, 7, 3]> >>> 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]>] """ o_poly = self.conv2poly(other) @@ -431,9 +431,9 @@ class AbstractPolynom(Explicable): >>> P = AbstractPolynom([1,2,3]) >>> Q = -P >>> Q - < [-1, -2, -3]> + < [-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], letter = self._letter).simplify() @@ -447,9 +447,9 @@ class AbstractPolynom(Explicable): >>> Q = AbstractPolynom([4,5,6]) >>> R = P - Q >>> R - < [-3, -3, -3]> + < [-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, '+'] >]>] + [< [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 ) @@ -477,29 +477,29 @@ class AbstractPolynom(Explicable): >>> p = AbstractPolynom([1,2]) >>> p*3 - < [3, 6]> + < [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]> + < [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]> + < [0, 3]> >>> (r*3).steps - [[< ['x', 3, '*'] >]] + [[< ['x', 3, '*'] >]] >>> p*q - < [0, 0, 4, 8]> + < [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]> + < [0, 1, 2]> >>> P = AbstractPolynom([1,2,3]) >>> Q = AbstractPolynom([4,5,6]) >>> P*Q - < [4, 13, 28, 27, 18]> + < [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) @@ -542,20 +542,20 @@ class AbstractPolynom(Explicable): >>> p = AbstractPolynom([0,0,3]) >>> p**2 - < [0, 0, 0, 0, 9]> + < [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]> + < [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, '*'] >]>] >>> p = AbstractPolynom([0,0,1]) >>> p**3 - < [0, 0, 0, 0, 0, 0, 1]> + < [0, 0, 0, 0, 0, 0, 1]> >>> p = AbstractPolynom([1,2,3]) >>> p**2 - < [1, 4, 10, 12, 9]> + < [1, 4, 10, 12, 9]> """ if not type(power): diff --git a/pymath/arithmetic.py b/pymath/calculus/arithmetic.py similarity index 100% rename from pymath/arithmetic.py rename to pymath/calculus/arithmetic.py diff --git a/pymath/explicable.py b/pymath/calculus/explicable.py similarity index 100% rename from pymath/explicable.py rename to pymath/calculus/explicable.py diff --git a/pymath/expression.py b/pymath/calculus/expression.py similarity index 96% rename from pymath/expression.py rename to pymath/calculus/expression.py index 9860b0b..742e0e5 100644 --- a/pymath/expression.py +++ b/pymath/calculus/expression.py @@ -56,11 +56,11 @@ class Expression(Explicable): >>> with Expression.tmp_render(): ... for i in exp.simplify().explain(): ... i - < [2, 3, 5, '/', '*'] > - < [2, < Fraction 3 / 5>, '*'] > - < [3, 5, '/', 2, '*'] > - < [3, 2, '*', 5, '/'] > - < [6, 5, '/'] > + < [2, 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(): diff --git a/pymath/fraction.py b/pymath/calculus/fraction.py similarity index 81% rename from pymath/fraction.py rename to pymath/calculus/fraction.py index 6b98378..f7fa92f 100644 --- a/pymath/fraction.py +++ b/pymath/calculus/fraction.py @@ -142,28 +142,28 @@ 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, '/'] > + < [3, 4, '/', 5, 4, '/', '+'] > + < [3, 5, '+', 4, '/'] > >>> f+0 < Fraction 3 / 4> >>> (f+0).steps @@ -211,12 +211,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 @@ -269,7 +269,7 @@ class Fraction(Explicable): >>> -f < Fraction 1 / 2> >>> (-f).steps - [< [-1, -2, '/'] >] + [< [-1, -2, '/'] >] """ f = Fraction(-self._num, self._denom) @@ -285,11 +285,11 @@ class Fraction(Explicable): >>> f*g < Fraction 1 / 3> >>> 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, '*', '/'] > + < [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 @@ -301,9 +301,9 @@ class Fraction(Explicable): >>> f*4 2 >>> print("\\n".join([repr(i) for i in (f*4).steps])) - < [1, 2, '/', 4, '*'] > - < [1, 2, '*', 2, '*', 1, 2, '*', '/'] > - < [2, 2, '*', 2, '/'] > + < [1, 2, '/', 4, '*'] > + < [1, 2, '*', 2, '*', 1, 2, '*', '/'] > + < [2, 2, '*', 2, '/'] > """ steps = [] @@ -409,16 +409,16 @@ class Fraction(Explicable): >>> f**3 < Fraction 27 / 64> >>> print("\\n".join([repr(i) for i in (f**3).steps])) - < [3, 4, '/', 3, '^'] > - < [3, 3, '^', 4, 3, '^', '/'] > + < [3, 4, '/', 3, '^'] > + < [3, 3, '^', 4, 3, '^', '/'] > >>> f = Fraction(6, 4) >>> f**3 < Fraction 27 / 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, '*', '/'] > + < [6, 4, '/', 3, '^'] > + < [6, 3, '^', 4, 3, '^', '/'] > + < [216, 64, '/'] > + < [27, 8, '*', 8, 8, '*', '/'] > """ if not type(power) == int: diff --git a/pymath/generic.py b/pymath/calculus/generic.py similarity index 98% rename from pymath/generic.py rename to pymath/calculus/generic.py index b83d809..3607ad9 100644 --- a/pymath/generic.py +++ b/pymath/calculus/generic.py @@ -314,7 +314,7 @@ def isPolynom(exp): :param exp: an expression :returns: True if the expression can be a polynom and false otherwise - >>> from pymath.polynom import Polynom + >>> from pymath.calculus.polynom import Polynom >>> p = Polynom([1,2]) >>> isPolynom(p) 1 @@ -335,11 +335,11 @@ def isNumerand(exp): >>> isNumerand(1) 1 - >>> from pymath.polynom import Polynom + >>> from pymath.calculus.polynom import Polynom >>> p = Polynom([1,2]) >>> isNumerand(p) 1 - >>> from pymath.fraction import Fraction + >>> from pymath.calculus.fraction import Fraction >>> f = Fraction(12) >>> isNumerand(f) 1 diff --git a/pymath/operator.py b/pymath/calculus/operator.py similarity index 100% rename from pymath/operator.py rename to pymath/calculus/operator.py diff --git a/pymath/polynom.py b/pymath/calculus/polynom.py similarity index 95% rename from pymath/polynom.py rename to pymath/calculus/polynom.py index e189cb6..af6d34d 100644 --- a/pymath/polynom.py +++ b/pymath/calculus/polynom.py @@ -58,15 +58,15 @@ class Polynom(AbstractPolynom): /!\ variables need to be in brackets {} >>> Polynom.random(["{b}", "{a}"]) # doctest:+ELLIPSIS - < ... + < ... >>> Polynom.random(degree = 2) # doctest:+ELLIPSIS - < ... + < ... >>> Polynom.random(degree = 3) # doctest:+ELLIPSIS - < ... + < ... >>> Polynom.random(degree = 2, conditions=["{b**2-4*a*c}>0"]) # Polynom deg 2 with positive Delta (ax^2 + bx + c) - < ... + < ... >>> Polynom.random(["{c}", "{b}", "{a}"], conditions=["{b**2-4*a*c}>0"]) # Same as above - < ... + < ... """ if (degree > 0 and degree < 26): @@ -151,7 +151,7 @@ class Polynom(AbstractPolynom): >>> P = Polynom([1, 2, 3]) >>> Q = P.derivate() >>> Q - < [2, 6]> + < [2, 6]> >>> print(Q.name) P' >>> for i in Q.explain(): diff --git a/pymath/polynomDeg2.py b/pymath/calculus/polynomDeg2.py similarity index 100% rename from pymath/polynomDeg2.py rename to pymath/calculus/polynomDeg2.py diff --git a/pymath/random_expression.py b/pymath/calculus/random_expression.py similarity index 100% rename from pymath/random_expression.py rename to pymath/calculus/random_expression.py diff --git a/pymath/render.py b/pymath/calculus/render.py similarity index 100% rename from pymath/render.py rename to pymath/calculus/render.py diff --git a/pymath/str2tokens.py b/pymath/calculus/str2tokens.py similarity index 97% rename from pymath/str2tokens.py rename to pymath/calculus/str2tokens.py index 9548b4a..1271934 100644 --- a/pymath/str2tokens.py +++ b/pymath/calculus/str2tokens.py @@ -2,7 +2,7 @@ # encoding: utf-8 from .generic import Stack, isOperator, isNumber, isPolynom -from pymath.operator import op +from .operator import op def str2tokens(exp): """ Parse the string into tokens then turn it into postfix form @@ -12,7 +12,7 @@ def str2tokens(exp): >>> str2tokens('2*3+4') [2, 3, '*', 4, '+'] >>> str2tokens('2x+4') - [2, < [0, 1]>, '*', 4, '+'] + [2, < [0, 1]>, '*', 4, '+'] """ in_tokens = str2in_tokens(exp) post_tokens = in2post_fix(in_tokens) @@ -71,7 +71,7 @@ def str2in_tokens(exp): or tokens[-1] == ")" \ or isPolynom(tokens[-1]): tokens.append("*") - from pymath.polynom import Polynom + from pymath.calculus.polynom import Polynom tokens.append(Polynom([0,1], letter = character)) elif character == ".":