Random classmethod for polynom

This commit is contained in:
Lafrite 2014-12-19 17:17:25 +01:00
parent d817002430
commit adce8e1348
2 changed files with 20 additions and 7 deletions

View File

@ -7,6 +7,7 @@ from .operator import op
from .generic import spe_zip, expand_list, isNumber, transpose_fill, flatten_list
#from .generic import spe_zip, sum_postfix, expand_list, isNumber
from .render import txt
from .random_expression import RdExpression
from itertools import chain
__all__ = ["Polynom"]
@ -16,6 +17,20 @@ class Polynom(object):
"""Docstring for Polynom. """
@classmethod
def random(self, coefs_form=[], conditions=[], letter = "x"):
""" Create a random polynom from coefs_form and conditions
:param coefs_form: list of forms (one by coef) (ascending degree sorted)
:param conditions: condition on variables
/!\ variables need to be in brackets {}
"""
form = str(coefs_form)
coefs = RdExpression(form, conditions)()
return Polynom(coef = eval(coefs), letter = letter)
def __init__(self, coef = [1], letter = "x" ):
"""Initiate the polynom
@ -331,6 +346,10 @@ if __name__ == '__main__':
#for i in p.simplify():
# print(repr(i))
print("\n")
poly = Polynom.random(["{a**2}", "{2*a*b}", "{b**2}"])
print(poly)
import doctest
doctest.testmod()

View File

@ -27,7 +27,7 @@ class RdExpression(object):
self._letters = self.get_letters()
self._gene_varia = {}
self._gene_2replaced= {}
self._gene_2replaced = {}
def get_2replaced(self):
"""Get elements of self._form which will have to be replaced
@ -74,12 +74,6 @@ class RdExpression(object):
:returns: an formated random expression
"""
#if self.FORM == "exp":
# return self.raw_exp(val_min, val_max)
#elif self.FORM == "raw":
# return self.raw_str(val_min, val_max)
#else:
# raise ValueError(self.FORM , " is an undefined form for self.FORM")
return self.raw_str(val_min, val_max)
def raw_str(self, val_min = -10, val_max = 10):