Merge branch 'dev' into polynom
This commit is contained in:
commit
096e025302
@ -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_default_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
|
||||
|
||||
@ -293,6 +308,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()
|
||||
|
||||
|
@ -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,21 @@ 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)()
|
||||
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
|
||||
|
||||
@ -331,6 +347,11 @@ if __name__ == '__main__':
|
||||
#for i in p.simplify():
|
||||
# print(repr(i))
|
||||
|
||||
print("\n")
|
||||
poly = Polynom.random(["[{a**2}, {a}]", "{2*a*b}", "{b**2}"])
|
||||
for i in poly.simplify():
|
||||
print(i)
|
||||
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
|
@ -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))
|
||||
<class 'pymath.expression.Expression'>
|
||||
>>> RdExpression.set_form("raw")
|
||||
>>> form = "{a}*{b}"
|
||||
>>> exp = RdExpression(form)()
|
||||
>>> print(type(exp))
|
||||
<class 'str'>
|
||||
"""
|
||||
|
||||
cls.FORM = form
|
||||
|
||||
@classmethod
|
||||
def set_df_form(cls):
|
||||
cls.set_form(cls.DEFAULT_FORM)
|
||||
|
||||
def __init__(self, form, conditions = []):
|
||||
"""Initiate the generator
|
||||
|
||||
@ -62,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
|
||||
@ -106,16 +71,10 @@ 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")
|
||||
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 +93,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 +132,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 +139,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)
|
||||
|
Loading…
Reference in New Issue
Block a user