From adce8e13484ab6be36d25e2c185a71d548c49608 Mon Sep 17 00:00:00 2001 From: Lafrite Date: Fri, 19 Dec 2014 17:17:25 +0100 Subject: [PATCH] Random classmethod for polynom --- pymath/polynom.py | 19 +++++++++++++++++++ pymath/random_expression.py | 8 +------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pymath/polynom.py b/pymath/polynom.py index 2241b6b..907d877 100644 --- a/pymath/polynom.py +++ b/pymath/polynom.py @@ -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() diff --git a/pymath/random_expression.py b/pymath/random_expression.py index 7ed9cef..26dd4ee 100644 --- a/pymath/random_expression.py +++ b/pymath/random_expression.py @@ -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):