diff --git a/TODO b/TODO index 64b0f4a..db39e93 100644 --- a/TODO +++ b/TODO @@ -5,7 +5,7 @@ * bug: expression can't handle -(-2) * Overload + - * for expression (DONE ~ no steps yet) * Expression should be able to simplify expression with ":" +* Add name to polynom * Expression parents class and his children: Numerical_exp, toGenerate_exp and formal expression * Create tbl sgn and variation render -* Give a name to polynoms diff --git a/bugs b/bugs index 5d1f54b..553d1d1 100644 --- a/bugs +++ b/bugs @@ -1,3 +1,7 @@ +* Soustraction de Polynômes!!!! + + + * Expression importe mal 4x^2 In [9]: e = Expression("3x + 4x^2 - 1") diff --git a/pymath/polynom.py b/pymath/polynom.py index 13a17b6..2bef1eb 100644 --- a/pymath/polynom.py +++ b/pymath/polynom.py @@ -64,9 +64,9 @@ class Polynom(Explicable): # On "parse" ce string pour créer les coefs coefs = [eval(i) if type(i)==str else i for i in eval(coefs)] # Création du polynom - return Polynom(coef = coefs, letter = letter, name = name) + return Polynom(coefs = coefs, letter = letter, name = name) - def __init__(self, coef = [1], letter = "x", name = "P"): + def __init__(self, coefs = [1], letter = "x", name = "P"): """Initiate the polynom :param coef: coefficients of the polynom (ascending degree sorted) @@ -96,7 +96,7 @@ class Polynom(Explicable): 'Q' """ super(Polynom, self).__init__() - self.feed_coef(coef) + self.feed_coef(coefs) self._letter = letter self.name = name diff --git a/pymath/polynomDeg2.py b/pymath/polynomDeg2.py index 099eff2..9a87efb 100644 --- a/pymath/polynomDeg2.py +++ b/pymath/polynomDeg2.py @@ -4,8 +4,10 @@ from .polynom import Polynom from .expression import Expression from .operator import op +from .random_expression import RdExpression from math import sqrt +__all__ = ["Polynom_deg2"] class Polynom_deg2(Polynom): @@ -13,6 +15,26 @@ class Polynom_deg2(Polynom): Child of Polynom with some extra tools """ + @classmethod + def random(self, coefs_form = ["{c}", "{b}", "{a}"], conditions = [], letter = "x", name = "P"): + """ Create a 2nd degree poly from coefs_form ans conditions + + :param coefs_form: list of forms (one by coef) (ascending degree sorted) + :param conditions: condition on variables + :param letter: the letter for the polynom + + """ + if len(coefs_form) != 3: + raise ValueError("Polynom_deg2 have to be degree 2 polynoms, they need 3 coefficients, {} are given".format(len(coefs_form))) + + form = str(coefs_form) + # On créé les valeurs toutes concaténées dans un string + coefs = RdExpression(form, conditions)() + # On "parse" ce string pour créer les coefs + coefs = [eval(i) if type(i)==str else i for i in eval(coefs)] + # Création du polynom + return Polynom_deg2(coefs = coefs, letter = letter, name = name) + def __init__(self, coefs = [0, 0, 1], letter = "x", name = "P"): if len(coefs) < 3 or len(coefs) > 4: raise ValueError("Polynom_deg2 have to be degree 2 polynoms, they need 3 coefficients, {} are given".format(len(coefs))) @@ -83,7 +105,7 @@ class Polynom_deg2(Polynom): """ return self(self.alpha).simplify() - def roots(self): + def roots(self, after_coma = 2): """ Compute roots of the polynom /!\ Can't manage exact solution because of pymath does not handle sqare root yet @@ -101,12 +123,21 @@ class Polynom_deg2(Polynom): [-1.0, 1.0] """ if self.delta > 0: - self.roots = [(-self.b - sqrt(self.delta))/(2*self.a), (-self.b + sqrt(self.delta))/(2*self.a)] + self._roots = [round((-self.b - sqrt(self.delta))/(2*self.a),after_coma), round((-self.b + sqrt(self.delta))/(2*self.a),after_coma)] elif self.delta == 0: - self.roots = [-self.b /(2*self.a)] + self._roots = [round(-self.b /(2*self.a), after_coma)] else: - self.roots = [] - return self.roots + self._roots = [] + return self._roots + + def tbl_sgn_header(self): + """ Return header of the sign line for tkzTabLine""" + if self.delta > 0: + return "{$-\\infty$, " + str(min(self.roots())) + " , " + str( max(self.roots())) + " , $+\\infty$}" + elif self.delta == 0: + return "{$-\\infty$, " + str(self.roots()[0]) + " , $+\\infty$}" + else: + return "{$-\\infty$, $+\\infty$}" def tbl_sgn(self): """ Return the sign line for tkzTabLine