From d817002430c5e3353016afe6dc924378c68ab9b2 Mon Sep 17 00:00:00 2001 From: Lafrite Date: Fri, 19 Dec 2014 16:45:22 +0100 Subject: [PATCH 1/3] random expression is now a Expression classmethod --- pymath/expression.py | 20 +++++++++++ pymath/random_expression.py | 68 +++++-------------------------------- 2 files changed, 28 insertions(+), 60 deletions(-) diff --git a/pymath/expression.py b/pymath/expression.py index 17029aa..be47d87 100644 --- a/pymath/expression.py +++ b/pymath/expression.py @@ -6,6 +6,8 @@ from .render import txt, tex from .str2tokens import str2tokens from .operator import op +from .random_expression import RdExpression + __all__ = ['Expression'] class Expression(object): @@ -22,6 +24,19 @@ class Expression(object): def set_df_render(cls): cls.set_render(cls.DEFAULT_RENDER) + @classmethod + def random(self, form="", conditions=[], val_min = -10, val_max=10): + """Create a random expression from form and with conditions + + :param form: the form of the expression (/!\ variables need to be in brackets {}) + :param conditions: condition on variables (/!\ variables need to be in brackets {}) + :param val_min: min value for generate variables + :param val_max: max value for generate variables + + """ + random_generator = RdExpression(form, conditions) + return Expression(random_generator(val_min, val_max)) + def __init__(self, exp): """ Initiate the expression @@ -284,6 +299,11 @@ if __name__ == '__main__': #exp="-(-2)" #test(exp) + print("\n") + exp = Expression.random("({a} + 3)({b} - 1)", ["{a} > 4"]) + for i in exp.simplify(): + print(i) + #import doctest #doctest.testmod() diff --git a/pymath/random_expression.py b/pymath/random_expression.py index a638744..7ed9cef 100644 --- a/pymath/random_expression.py +++ b/pymath/random_expression.py @@ -2,8 +2,6 @@ # encoding: utf-8 from random import randint -from .expression import Expression -from .render import tex, txt import re import pyparsing from .generic import flatten_list @@ -12,44 +10,11 @@ from .arithmetic import gcd class RdExpression(object): """ + A generator of random expression builder - Two forms are available: - - exp: return the expression through Expression class (default) - - raw: return the expression as a raw string - This can be set globally with - - RdExpression.FORM - - or temporary with - - "raw_exp" or "raw_str" methods - """ - FORM = "exp" - DEFAULT_FORM = "exp" - - @classmethod - def set_form(cls, form): - """ Define whether RdExpression create expression with Expression (nice render) or if it only replace inside {} not taking care or render - - >>> form = "{a}*{b}" - >>> exp = RdExpression(form)() - >>> print(type(exp)) - - >>> RdExpression.set_form("raw") - >>> form = "{a}*{b}" - >>> exp = RdExpression(form)() - >>> print(type(exp)) - - """ - - cls.FORM = form - - @classmethod - def set_df_form(cls): - cls.set_form(cls.DEFAULT_FORM) - def __init__(self, form, conditions = []): """Initiate the generator @@ -106,16 +71,16 @@ class RdExpression(object): :param val_min: minimum value random generation :param val_max: maximum value random generation - :param render: Render of the expression (returns an Expression by default) :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") + #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): """Return raw string (don't use Expression for rendering or parsing) @@ -134,18 +99,6 @@ class RdExpression(object): return exp - def raw_exp(self, val_min = -10, val_max = 10): - """Same as raw_str but returns an Expression object - - :param val_min: minimum value random generation - :param val_max: maximum value random generation - :returns: an random Expression object - - """ - exp = self.raw_str(val_min, val_max) - - return Expression(exp) - def gene_varia(self, val_min = -10, val_max = 10): """Randomly generates variables/letters @@ -185,10 +138,6 @@ def desc_rdExp(rdExp): if __name__ == '__main__': - # Adapt render to consol display - #Expression.set_render(txt) - - RdExpression.set_form("raw") form = "{a}*-14 / (2*{b}) / -23 / 4" cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [1]", "{b} not in [1]"] rdExp1 = RdExpression(form, cond) @@ -196,7 +145,6 @@ if __name__ == '__main__': rdExp2 = RdExpression(form) desc_rdExp(rdExp2) - RdExpression.set_df_form() form = "{a+a*10}*4 + {a} + 2*{b}" cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "abs({a}) not in [1]", "{b} not in [1]", "gcd({a},{b}) == 1"] rdExp3 = RdExpression(form, cond) From adce8e13484ab6be36d25e2c185a71d548c49608 Mon Sep 17 00:00:00 2001 From: Lafrite Date: Fri, 19 Dec 2014 17:17:25 +0100 Subject: [PATCH 2/3] 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): From 6ca79a4e693f7bfe8c5153bd87325dae6bb99d17 Mon Sep 17 00:00:00 2001 From: Lafrite Date: Fri, 19 Dec 2014 17:29:12 +0100 Subject: [PATCH 3/3] random classmethod for polynoms --- pymath/polynom.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pymath/polynom.py b/pymath/polynom.py index 907d877..cba41f6 100644 --- a/pymath/polynom.py +++ b/pymath/polynom.py @@ -29,7 +29,8 @@ class Polynom(object): """ form = str(coefs_form) coefs = RdExpression(form, conditions)() - return Polynom(coef = eval(coefs), letter = letter) + coefs = [eval(i) if type(i)==str else i for i in eval(coefs)] + return Polynom(coef = coefs, letter = letter) def __init__(self, coef = [1], letter = "x" ): """Initiate the polynom @@ -347,8 +348,9 @@ if __name__ == '__main__': # print(repr(i)) print("\n") - poly = Polynom.random(["{a**2}", "{2*a*b}", "{b**2}"]) - print(poly) + poly = Polynom.random(["[{a**2}, {a}]", "{2*a*b}", "{b**2}"]) + for i in poly.simplify(): + print(i) import doctest doctest.testmod()