Merge branch 'dev' into operator

This commit is contained in:
Lafrite 2015-03-08 23:44:55 +01:00
commit 93a8c45c5b
2 changed files with 24 additions and 7 deletions

View File

@ -33,7 +33,7 @@ class Polynom(Explicable):
"""Docstring for Polynom. """ """Docstring for Polynom. """
@classmethod @classmethod
def random(self, coefs_form=[], conditions=[], letter = "x", degree = 0): def random(self, coefs_form=[], conditions=[], letter = "x", degree = 0, name = "P"):
""" Create a random polynom from coefs_form and conditions """ Create a random polynom from coefs_form and conditions
:param coefs_form: list of forms (one by coef) (ascending degree sorted) :param coefs_form: list of forms (one by coef) (ascending degree sorted)
@ -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) return Polynom(coef = coefs, letter = letter, name = name)
def __init__(self, coef = [1], letter = "x" ): def __init__(self, coef = [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)
@ -75,9 +75,15 @@ class Polynom(Explicable):
- [a,b,c]: list of coeficient for same degree. [1,[2,3],4] designate 1 + 2x + 3x + 4x^2 - [a,b,c]: list of coeficient for same degree. [1,[2,3],4] designate 1 + 2x + 3x + 4x^2
- a: a Expression. [1, Expression("2+3"), 4] designate 1 + (2+3)x + 4x^2 - a: a Expression. [1, Expression("2+3"), 4] designate 1 + (2+3)x + 4x^2
:param letter: the string describing the unknown :param letter: the string describing the unknown
:param name: Name of the polynom
>>> Polynom([1, 2, 3]).mainOp >>> P = Polynom([1, 2, 3])
>>> P.mainOp
'+' '+'
>>> P.name
'P'
>>> P._letter
'x'
>>> Polynom([1]).mainOp >>> Polynom([1]).mainOp
'*' '*'
>>> Polynom([0, 0, 3]).mainOp >>> Polynom([0, 0, 3]).mainOp
@ -86,10 +92,13 @@ class Polynom(Explicable):
'x' 'x'
>>> Polynom([1, 2, 3], "y")._letter >>> Polynom([1, 2, 3], "y")._letter
'y' 'y'
>>> Polynom([1, 2, 3], name = "Q").name
'Q'
""" """
super(Polynom, self).__init__() super(Polynom, self).__init__()
self.feed_coef(coef) self.feed_coef(coef)
self._letter = letter self._letter = letter
self.name = name
if self.is_monom(): if self.is_monom():
@ -166,6 +175,9 @@ class Polynom(Explicable):
else: else:
return 0 return 0
def give_name(self, name):
self.name = name
def __str__(self): def __str__(self):
return str(Expression(self.postfix_tokens)) return str(Expression(self.postfix_tokens))
@ -408,6 +420,8 @@ class Polynom(Explicable):
>>> Q = P.derivate() >>> Q = P.derivate()
>>> Q >>> Q
< Polynom [2, 6]> < Polynom [2, 6]>
>>> print(Q.name)
P'
>>> for i in Q.explain(): >>> for i in Q.explain():
... print(i) ... print(i)
2 \\times 3 x + 1 \\times 2 2 \\times 3 x + 1 \\times 2
@ -416,7 +430,10 @@ class Polynom(Explicable):
derv_coefs = [] derv_coefs = []
for (i,c) in enumerate(self._coef): for (i,c) in enumerate(self._coef):
derv_coefs += [Expression([i, c, op.mul])] derv_coefs += [Expression([i, c, op.mul])]
return Polynom(derv_coefs[1:]).simplify()
ans = Polynom(derv_coefs[1:]).simplify()
ans.name = self.name + "'"
return ans
@staticmethod @staticmethod
def postfix_add(numbers): def postfix_add(numbers):

View File

@ -13,12 +13,12 @@ class Polynom_deg2(Polynom):
Child of Polynom with some extra tools Child of Polynom with some extra tools
""" """
def __init__(self, coefs = [0, 0, 1], letter = "x"): 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)))
if coefs[2] == 0: if coefs[2] == 0:
raise ValueError("Polynom_deg2 have to be degree 2 polynoms, coefficient of x^2 can't be 0") raise ValueError("Polynom_deg2 have to be degree 2 polynoms, coefficient of x^2 can't be 0")
Polynom.__init__(self, coefs, letter) Polynom.__init__(self, coefs, letter, name = name)
@property @property
def a(self): def a(self):