Feat: Expression can be generated randomly!!!
This commit is contained in:
@@ -11,6 +11,8 @@ Expression
|
||||
|
||||
"""
|
||||
from ..core import AssocialTree, Tree, compute, typing, TypingError
|
||||
from ..core.random import extract_rdleaf, extract_rv, random_generator, compute_leafs, replace_rdleaf
|
||||
from ..core.MO import moify
|
||||
from .tokens import factory
|
||||
from .renders import renders
|
||||
|
||||
@@ -68,25 +70,60 @@ class Expression(object):
|
||||
"""
|
||||
t = Tree.from_str(string)
|
||||
if typing:
|
||||
tt = cls(t)._typing()
|
||||
try:
|
||||
return factory(tt)
|
||||
except TypeError as e:
|
||||
return cls(t)
|
||||
return cls._post_precessing(t)
|
||||
|
||||
return cls(t)
|
||||
|
||||
@classmethod
|
||||
def random(self, template, conditions=[], shuffle=False):
|
||||
""" Initiate randomly the expression
|
||||
def random(
|
||||
cls,
|
||||
template,
|
||||
conditions=[],
|
||||
rejected=[0],
|
||||
min_max=(-10, 10),
|
||||
variables_scope={},
|
||||
shuffle=False,
|
||||
):
|
||||
""" Initiate randomly the expression
|
||||
|
||||
:param template: the template of the expression
|
||||
:param conditions: conditions on randomly generate variable
|
||||
:param rejected: Values to reject for all random variables
|
||||
:param min_max: Min and max value for all random variables
|
||||
:param variables_scope: Dictionnary for each random varaibles to fic rejected and min_max
|
||||
:param shuffle: allowing to shuffle the tree
|
||||
:returns: TODO
|
||||
|
||||
:example:
|
||||
>>> Expression.random("{a}/{a*k}") # doctest: +SKIP
|
||||
<Exp: -3 / -15>
|
||||
>>> Expression.random("{a}/{a*k} - 3*{b}", variables_scope={'a':{'min_max':(10, 30)}})
|
||||
<Exp: 18 / 108 - 3 * 9>
|
||||
|
||||
"""
|
||||
pass
|
||||
rd_t = Tree.from_str(template, random=True)
|
||||
leafs = extract_rdleaf(rd_t)
|
||||
rd_varia = extract_rv(leafs)
|
||||
generated = random_generator(
|
||||
rd_varia, conditions, rejected, min_max, variables_scope
|
||||
)
|
||||
computed = compute_leafs(leafs, generated)
|
||||
t = replace_rdleaf(rd_t, computed).map_on_leaf(moify)
|
||||
t = cls._post_precessing(t)
|
||||
|
||||
if shuffle:
|
||||
raise NotImplemented("Can't suffle expression yet")
|
||||
|
||||
return cls(t)
|
||||
|
||||
@classmethod
|
||||
def _post_precessing(cls, t):
|
||||
""" Post process the tree by typing it """
|
||||
tt = cls(t)._typing()
|
||||
try:
|
||||
return factory(tt)
|
||||
except TypeError as e:
|
||||
return cls(t)
|
||||
|
||||
@classmethod
|
||||
def set_render(cls, render):
|
||||
|
Reference in New Issue
Block a user