diff --git a/pymath/abstract_polynom.py b/pymath/abstract_polynom.py index 969fcf9..f434983 100644 --- a/pymath/abstract_polynom.py +++ b/pymath/abstract_polynom.py @@ -277,9 +277,9 @@ class AbstractPolynom(Explicable): >>> P = AbstractPolynom([1,2,3]) >>> P.conv2poly(1) - < AbstractPolynom [1]> + < [1]> >>> P.conv2poly(0) - < AbstractPolynom [0]> + < [0]> """ if isNumber(other) and not isPolynom(other): @@ -297,15 +297,15 @@ class AbstractPolynom(Explicable): >>> P = AbstractPolynom([1,2,3]) >>> Q = P.reduce() >>> Q - < AbstractPolynom [1, 2, 3]> + < [1, 2, 3]> >>> Q.steps [] >>> P = AbstractPolynom([[1,2], [3,4,5], 6]) >>> Q = P.reduce() >>> Q - < AbstractPolynom [3, 12, 6]> + < [3, 12, 6]> >>> Q.steps - [< AbstractPolynom [< [1, 2, '+'] >, < [3, 4, '+', 5, '+'] >, 6]>, < AbstractPolynom [< [1, 2, '+'] >, < [7, 5, '+'] >, 6]>, < AbstractPolynom [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 @@ -399,9 +399,9 @@ class AbstractPolynom(Explicable): >>> Q = AbstractPolynom([4,5]) >>> R = P+Q >>> R - < AbstractPolynom [5, 7, 3]> + < [5, 7, 3]> >>> R.steps - [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < AbstractPolynom [< [1, 4, '+'] >, < [2, 5, '+'] >, 3]>, < AbstractPolynom [< [1, 4, '+'] >, < [2, 5, '+'] >, 3]>] + [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < [< [1, 4, '+'] >, < [2, 5, '+'] >, 3]>] """ o_poly = self.conv2poly(other) @@ -423,7 +423,7 @@ class AbstractPolynom(Explicable): >>> P = AbstractPolynom([1,2,3]) >>> Q = -P >>> Q - < AbstractPolynom [-1, -2, -3]> + < [-1, -2, -3]> >>> Q.steps [< [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', '-'] >] """ @@ -439,9 +439,15 @@ class AbstractPolynom(Explicable): >>> Q = AbstractPolynom([4,5,6]) >>> R = P - Q >>> R - < AbstractPolynom [-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, '-', '+'] >, < AbstractPolynom [< [1, -4, '+'] >, < [2, -5, '+'] >, < [3, -6, '+'] >]>, < AbstractPolynom [< [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 ) + 3 x^{ 2 } + 2 x + 1 - 6 x^{ 2 } - 5 x - 4 + ( 3 - 6 ) x^{ 2 } + ( 2 - 5 ) x + 1 - 4 + - 3 x^{ 2 } - 3 x - 3 """ o_poly = self.conv2poly(other) ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.sub])] @@ -463,29 +469,29 @@ class AbstractPolynom(Explicable): >>> p = AbstractPolynom([1,2]) >>> p*3 - < AbstractPolynom [3, 6]> + < [3, 6]> >>> (p*3).steps - [[< [2, 'x', '*', 1, '+', 3, '*'] >], < AbstractPolynom [3, < [2, 3, '*'] >]>, < AbstractPolynom [3, < [2, 3, '*'] >]>] + [[< [2, 'x', '*', 1, '+', 3, '*'] >], < [3, < [2, 3, '*'] >]>] >>> q = AbstractPolynom([0,0,4]) >>> q*3 - < AbstractPolynom [0, 0, 12]> + < [0, 0, 12]> >>> (q*3).steps - [[< [4, 'x', 2, '^', '*', 3, '*'] >], < AbstractPolynom [0, 0, < [4, 3, '*'] >]>, < AbstractPolynom [0, 0, < [4, 3, '*'] >]>] + [[< [4, 'x', 2, '^', '*', 3, '*'] >], < [0, 0, < [4, 3, '*'] >]>] >>> r = AbstractPolynom([0,1]) >>> r*3 - < AbstractPolynom [0, 3]> + < [0, 3]> >>> (r*3).steps [[< ['x', 3, '*'] >]] >>> p*q - < AbstractPolynom [0, 0, 4, 8]> + < [0, 0, 4, 8]> >>> (p*q).steps - [[< [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < AbstractPolynom [0, 0, 4, < [2, 4, '*'] >]>, < AbstractPolynom [0, 0, 4, < [2, 4, '*'] >]>] + [[< [2, 'x', '*', 1, '+', 4, 'x', 2, '^', '*', '*'] >], < [0, 0, 4, < [2, 4, '*'] >]>] >>> p*r - < AbstractPolynom [0, 1, 2]> + < [0, 1, 2]> >>> P = AbstractPolynom([1,2,3]) >>> Q = AbstractPolynom([4,5,6]) >>> P*Q - < AbstractPolynom [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) @@ -528,20 +534,20 @@ class AbstractPolynom(Explicable): >>> p = AbstractPolynom([0,0,3]) >>> p**2 - < AbstractPolynom [0, 0, 0, 0, 9]> + < [0, 0, 0, 0, 9]> >>> (p**2).steps - [< [3, 'x', 2, '^', '*', 2, '^'] >, < AbstractPolynom [0, 0, 0, 0, < [3, 2, '^'] >]>, < AbstractPolynom [0, 0, 0, 0, < [3, 2, '^'] >]>] + [< [3, 'x', 2, '^', '*', 2, '^'] >, < [0, 0, 0, 0, < [3, 2, '^'] >]>] >>> p = AbstractPolynom([1,2]) >>> p**2 - < AbstractPolynom [1, 4, 4]> + < [1, 4, 4]> >>> (p**2).steps - [< [2, 'x', '*', 1, '+', 2, '^'] >, [< [2, 'x', '*', 1, '+', 2, 'x', '*', 1, '+', '*'] >], < AbstractPolynom [1, < [2, 2, '+'] >, < [2, 2, '*'] >]>, < AbstractPolynom [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 - < AbstractPolynom [0, 0, 0, 0, 0, 0, 1]> + < [0, 0, 0, 0, 0, 0, 1]> >>> p = AbstractPolynom([1,2,3]) >>> p**2 - < AbstractPolynom [1, 4, 10, 12, 9]> + < [1, 4, 10, 12, 9]> """ if not type(power): diff --git a/pymath/polynom.py b/pymath/polynom.py index 247d476..cfe70bf 100644 --- a/pymath/polynom.py +++ b/pymath/polynom.py @@ -18,11 +18,15 @@ def polynom_factory(func): @wraps(func) def wrapper(*args, **kwrds): P = func(*args, **kwrds) - if isinstance(P,Polynom) and P.degree == 2: + if issubclass(type(P),AbstractPolynom) and P.degree == 2: from .polynomDeg2 import Polynom_deg2 new_P = Polynom_deg2(poly=P) new_P.steps = P.steps return new_P + elif issubclass(type(P),AbstractPolynom): + new_P = Polynom(poly=P) + new_P.steps = P.steps + return new_P else: return P return wrapper @@ -54,13 +58,15 @@ class Polynom(AbstractPolynom): /!\ variables need to be in brackets {} >>> Polynom.random(["{b}", "{a}"]) # doctest:+ELLIPSIS - < Polynom ... + < ... >>> Polynom.random(degree = 2) # doctest:+ELLIPSIS - < Polynom ... + < ... + >>> 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 ... + < ... >>> Polynom.random(["{c}", "{b}", "{a}"], conditions=["{b**2-4*a*c}>0"]) # Same as above - < Polynom ... + < ... """ if (degree > 0 and degree < 26): @@ -76,7 +82,7 @@ class Polynom(AbstractPolynom): # Création du polynom return Polynom(coefs = coefs, letter = letter, name = name) - def __init__(self, coefs = [1], letter = "x", name = "P"): + def __init__(self, coefs = [1], letter = "x", name = "P", poly = 0): """Initiate the polynom :param coef: coefficients of the polynom (ascending degree sorted) @@ -105,6 +111,11 @@ class Polynom(AbstractPolynom): >>> Polynom([1, 2, 3], name = "Q").name 'Q' """ + if poly: + coefs = poly._coef + letter = poly._letter + name = poly.name + super(Polynom, self).__init__(coefs, letter, name) def __call__(self, value): @@ -140,7 +151,7 @@ class Polynom(AbstractPolynom): >>> P = Polynom([1, 2, 3]) >>> Q = P.derivate() >>> Q - < Polynom [2, 6]> + < [2, 6]> >>> print(Q.name) P' >>> for i in Q.explain(): diff --git a/pymath/polynomDeg2.py b/pymath/polynomDeg2.py index de88570..80a1ce4 100644 --- a/pymath/polynomDeg2.py +++ b/pymath/polynomDeg2.py @@ -5,6 +5,7 @@ from .polynom import Polynom from .expression import Expression from .operator import op from .random_expression import RdExpression + from math import sqrt __all__ = ["Polynom_deg2"] @@ -219,10 +220,6 @@ class Polynom_deg2(Polynom): return "\\tkzTabVar{-/{}, +/{$" + str(beta) + "$}, -/{}}" - - - - if __name__ == '__main__': # from .render import txt # with Expression.tmp_render(txt): diff --git a/pymath/str2tokens.py b/pymath/str2tokens.py index 61c2eb0..9548b4a 100644 --- a/pymath/str2tokens.py +++ b/pymath/str2tokens.py @@ -12,7 +12,7 @@ def str2tokens(exp): >>> str2tokens('2*3+4') [2, 3, '*', 4, '+'] >>> str2tokens('2x+4') - [2, < Polynom [0, 1]>, '*', 4, '+'] + [2, < [0, 1]>, '*', 4, '+'] """ in_tokens = str2in_tokens(exp) post_tokens = in2post_fix(in_tokens)