Merge branch '2nd_deg' into operator

This commit is contained in:
Lafrite 2015-03-08 23:48:40 +01:00
commit c391c8e084
4 changed files with 44 additions and 9 deletions

2
TODO
View File

@ -5,7 +5,7 @@
* bug: expression can't handle -(-2) * bug: expression can't handle -(-2)
* Overload + - * for expression (DONE ~ no steps yet) * Overload + - * for expression (DONE ~ no steps yet)
* Expression should be able to simplify expression with ":" * 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 * Expression parents class and his children: Numerical_exp, toGenerate_exp and formal expression
* Create tbl sgn and variation render * Create tbl sgn and variation render
* Give a name to polynoms

4
bugs
View File

@ -1,3 +1,7 @@
* Soustraction de Polynômes!!!!
* Expression importe mal 4x^2 * Expression importe mal 4x^2
In [9]: e = Expression("3x + 4x^2 - 1") In [9]: e = Expression("3x + 4x^2 - 1")

View File

@ -64,9 +64,9 @@ class Polynom(Explicable):
# On "parse" ce string pour créer les coefs # On "parse" ce string pour créer les coefs
coefs = [eval(i) if type(i)==str else i for i in eval(coefs)] coefs = [eval(i) if type(i)==str else i for i in eval(coefs)]
# Création du polynom # 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 """Initiate the polynom
:param coef: coefficients of the polynom (ascending degree sorted) :param coef: coefficients of the polynom (ascending degree sorted)
@ -96,7 +96,7 @@ class Polynom(Explicable):
'Q' 'Q'
""" """
super(Polynom, self).__init__() super(Polynom, self).__init__()
self.feed_coef(coef) self.feed_coef(coefs)
self._letter = letter self._letter = letter
self.name = name self.name = name

View File

@ -4,8 +4,10 @@
from .polynom import Polynom from .polynom import Polynom
from .expression import Expression from .expression import Expression
from .operator import op from .operator import op
from .random_expression import RdExpression
from math import sqrt from math import sqrt
__all__ = ["Polynom_deg2"]
class Polynom_deg2(Polynom): class Polynom_deg2(Polynom):
@ -13,6 +15,26 @@ class Polynom_deg2(Polynom):
Child of Polynom with some extra tools 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"): def __init__(self, coefs = [0, 0, 1], letter = "x", name = "P"):
if len(coefs) < 3 or len(coefs) > 4: 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))) 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() return self(self.alpha).simplify()
def roots(self): def roots(self, after_coma = 2):
""" Compute roots of the polynom """ Compute roots of the polynom
/!\ Can't manage exact solution because of pymath does not handle sqare root yet /!\ 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] [-1.0, 1.0]
""" """
if self.delta > 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: elif self.delta == 0:
self.roots = [-self.b /(2*self.a)] self._roots = [round(-self.b /(2*self.a), after_coma)]
else: else:
self.roots = [] self._roots = []
return 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): def tbl_sgn(self):
""" Return the sign line for tkzTabLine """ Return the sign line for tkzTabLine