Compare commits
5 Commits
a267acd7b3
...
0f575ae0ae
Author | SHA1 | Date |
---|---|---|
Bertrand Benjamin | 0f575ae0ae | |
Bertrand Benjamin | b738cf8dd8 | |
Bertrand Benjamin | 32112a4591 | |
Bertrand Benjamin | b43c64fc7e | |
Bertrand Benjamin | aad2395a3a |
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
from .calculus import Expression, Integer, Decimal, render, Polynomial, Fraction#, random_list,
|
from .calculus import Expression, render, random
|
||||||
|
|
||||||
# Expression.set_render('tex')
|
# Expression.set_render('tex')
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ Make calculus as a student
|
||||||
|
|
||||||
Expression is the classe wich handle all calculus. It can randomly generate or import calculus, simplify them and explain them as a student would do.
|
Expression is the classe wich handle all calculus. It can randomly generate or import calculus, simplify them and explain them as a student would do.
|
||||||
|
|
||||||
>>> from mapytex.calculus import Expression
|
|
||||||
>>> render.set_render("txt")
|
>>> render.set_render("txt")
|
||||||
>>> e = Expression.from_str("2x + 6 - 3x")
|
>>> e = Expression.from_str("2x + 6 - 3x")
|
||||||
>>> print(e)
|
>>> print(e)
|
||||||
|
@ -27,16 +26,23 @@ Expression is the classe wich handle all calculus. It can randomly generate or i
|
||||||
(2 - 3) * x + 6
|
(2 - 3) * x + 6
|
||||||
- x + 6
|
- x + 6
|
||||||
|
|
||||||
|
Create random Expression
|
||||||
|
========================
|
||||||
|
|
||||||
|
>>> e = random.expression("{a} / {b} + {c} / {d}")
|
||||||
|
>>> print(e) # doctest: +SKIP
|
||||||
|
- 3 / - 10 + 3 / 5
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .API import Expression, Integer, Decimal, render, Polynomial, Fraction
|
from .API import render, Expression
|
||||||
#from .core import random_list
|
#from decimal import getcontext
|
||||||
from decimal import getcontext
|
from . import random
|
||||||
|
|
||||||
#getcontext().prec = 2
|
#getcontext().prec = 2
|
||||||
|
|
||||||
|
|
||||||
__all__ = ["Expression"]
|
__all__ = ["render", "Expression", "random"]
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
from .list import list_generator as list
|
||||||
|
from .expression import expression_generator as expression
|
||||||
|
|
||||||
|
__all__ = ["list", "expression"]
|
||||||
|
|
||||||
|
"""
|
||||||
|
Generate random stuffs
|
||||||
|
======================
|
||||||
|
|
||||||
|
list_generator
|
||||||
|
==============
|
||||||
|
|
||||||
|
Generate random lists
|
||||||
|
|
||||||
|
expression_generator
|
||||||
|
====================
|
||||||
|
|
||||||
|
Generate random Expression
|
||||||
|
"""
|
|
@ -8,10 +8,16 @@
|
||||||
|
|
||||||
|
|
||||||
from random import choice
|
from random import choice
|
||||||
|
import math
|
||||||
|
|
||||||
|
EVAL_FUN = {**math.__dict__}
|
||||||
|
|
||||||
|
|
||||||
def complete_variable_configs(variables, global_config:dict={}, configs:dict={})->dict:
|
|
||||||
""" Completes variables configurations with the global configuration
|
def complete_variable_configs(
|
||||||
|
variables, global_config: dict = {}, configs: dict = {}
|
||||||
|
) -> dict:
|
||||||
|
"""Completes variables configurations with the global configuration
|
||||||
|
|
||||||
:param variables: list of random variables to generate
|
:param variables: list of random variables to generate
|
||||||
:param global_config: global parameters
|
:param global_config: global parameters
|
||||||
|
@ -26,7 +32,7 @@ def complete_variable_configs(variables, global_config:dict={}, configs:dict={})
|
||||||
... "b": {"min_max": (-5, 0)},
|
... "b": {"min_max": (-5, 0)},
|
||||||
... "c": {"rejected": [2], "min_max": (0, 5)}
|
... "c": {"rejected": [2], "min_max": (0, 5)}
|
||||||
... })
|
... })
|
||||||
>>> completed["a"] == {'rejected': [0, 1], 'min_max': (-10, 10)}
|
>>> completed["a"] == {'rejected': [0, 1], 'min_max': (-10, 10)}
|
||||||
True
|
True
|
||||||
>>> completed["b"] == {'rejected': [], 'min_max': (-5, 0)}
|
>>> completed["b"] == {'rejected': [], 'min_max': (-5, 0)}
|
||||||
True
|
True
|
||||||
|
@ -45,11 +51,14 @@ def complete_variable_configs(variables, global_config:dict={}, configs:dict={})
|
||||||
complete_configs[variable] = dict(global_config, **configs[variable])
|
complete_configs[variable] = dict(global_config, **configs[variable])
|
||||||
return complete_configs
|
return complete_configs
|
||||||
|
|
||||||
|
|
||||||
def random_generator(
|
def random_generator(
|
||||||
variables:list[str], conditions:list[str]=[], global_config:dict={}, configs:dict={},
|
variables: list[str],
|
||||||
)-> dict[str, int]:
|
conditions: list[str] = [],
|
||||||
""" Generate random variables
|
global_config: dict = {},
|
||||||
|
configs: dict = {},
|
||||||
|
) -> dict[str, int]:
|
||||||
|
"""Generate random variables
|
||||||
|
|
||||||
:param variables: list of random variables to generate
|
:param variables: list of random variables to generate
|
||||||
:param conditions: condition over variables
|
:param conditions: condition over variables
|
||||||
|
@ -57,8 +66,10 @@ def random_generator(
|
||||||
:param configs: global parameters
|
:param configs: global parameters
|
||||||
:return: dictionnary of generated variables
|
:return: dictionnary of generated variables
|
||||||
|
|
||||||
|
In variables and configurations, you have access to all math module functions
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> gene = random_generator(["a", "b"],
|
>>> gene = random_generator(["a", "b"],
|
||||||
... ["a > 0"],
|
... ["a > 0"],
|
||||||
... {"rejected": [0], "min_max":(-10, 10)},
|
... {"rejected": [0], "min_max":(-10, 10)},
|
||||||
... {"a": {"rejected": [0, 1]},
|
... {"a": {"rejected": [0, 1]},
|
||||||
|
@ -70,7 +81,7 @@ def random_generator(
|
||||||
True
|
True
|
||||||
>>> gene["b"] < 0
|
>>> gene["b"] < 0
|
||||||
True
|
True
|
||||||
>>> gene = random_generator(["a", "b"],
|
>>> gene = random_generator(["a", "b"],
|
||||||
... ["a % b == 0"],
|
... ["a % b == 0"],
|
||||||
... {"rejected": [0, 1], "min_max":(-10, 10)}
|
... {"rejected": [0, 1], "min_max":(-10, 10)}
|
||||||
... )
|
... )
|
||||||
|
@ -81,9 +92,7 @@ def random_generator(
|
||||||
>>> gene["a"] % gene["b"]
|
>>> gene["a"] % gene["b"]
|
||||||
0
|
0
|
||||||
"""
|
"""
|
||||||
complete_scope = complete_variable_configs(
|
complete_scope = complete_variable_configs(variables, global_config, configs)
|
||||||
variables, global_config, configs
|
|
||||||
)
|
|
||||||
choices_list = {
|
choices_list = {
|
||||||
v: list(
|
v: list(
|
||||||
set(
|
set(
|
||||||
|
@ -101,9 +110,7 @@ def random_generator(
|
||||||
|
|
||||||
generate_variable = {v: choice(choices_list[v]) for v in variables}
|
generate_variable = {v: choice(choices_list[v]) for v in variables}
|
||||||
|
|
||||||
while not all([eval(c, __builtins__, generate_variable) for c in conditions]):
|
while not all([eval(c, EVAL_FUN, generate_variable) for c in conditions]):
|
||||||
generate_variable = {v: choice(choices_list[v]) for v in variables}
|
generate_variable = {v: choice(choices_list[v]) for v in variables}
|
||||||
|
|
||||||
return generate_variable
|
return generate_variable
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
|
import math
|
||||||
|
|
||||||
def extract_letters(words:list[str])->set[str]:
|
EVAL_FUN = {**math.__dict__}
|
||||||
""" Extracts unique letters from a list of words
|
|
||||||
|
def extract_letters(words: list[str]) -> set[str]:
|
||||||
|
"""Extracts unique letters from a list of words
|
||||||
|
|
||||||
:param words: list of leafs
|
:param words: list of leafs
|
||||||
:return: set of letters
|
:return: set of letters
|
||||||
|
@ -18,18 +21,23 @@ def extract_letters(words:list[str])->set[str]:
|
||||||
return letters
|
return letters
|
||||||
|
|
||||||
|
|
||||||
def eval_words(words:list[str], values:dict[str,int]) -> dict[str, int]:
|
def eval_words(words: list[str], values: dict[str, int]) -> dict[str, int]:
|
||||||
""" Evaluate words replacing letters with values
|
"""Evaluate words replacing letters with values
|
||||||
|
|
||||||
:param words: list of words
|
:param words: list of words
|
||||||
:param values: Dictionary of letters:value
|
:param values: Dictionary of letters:value
|
||||||
:return: Dictionary of evaluated words from generated values
|
:return: Dictionary of evaluated words from generated values
|
||||||
|
|
||||||
|
In words, you have access to all math module functions
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> leafs = ["a", "a*k"]
|
>>> leafs = ["a", "a*k"]
|
||||||
>>> generated_values = {"a":2, "k":3}
|
>>> generated_values = {"a":2, "k":3}
|
||||||
>>> eval_words(leafs, generated_values)
|
>>> eval_words(leafs, generated_values)
|
||||||
{'a': 2, 'a*k': 6}
|
{'a': 2, 'a*k': 6}
|
||||||
|
>>> leafs = ["exp(a)", "gcd(a, k)"]
|
||||||
|
>>> generated_values = {"a":2, "k":3}
|
||||||
|
>>> eval_words(leafs, generated_values)
|
||||||
|
{'exp(a)': 7.38905609893065, 'gcd(a, k)': 1}
|
||||||
"""
|
"""
|
||||||
return {word: eval(word, values) for word in words}
|
return {word: eval(word, EVAL_FUN, values) for word in words}
|
||||||
|
|
||||||
|
|
|
@ -16,38 +16,38 @@ __all__ = ["reject_random", "filter_random", "FilterRandom"]
|
||||||
|
|
||||||
|
|
||||||
def reject_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks=[]):
|
def reject_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks=[]):
|
||||||
""" Generate a random integer with the rejection method
|
"""Generate a random integer with the rejection method
|
||||||
|
|
||||||
:param name: name of the Integer
|
:param name: name of the Integer
|
||||||
:param min_value: minimum value
|
:param min_value: minimum value
|
||||||
:param max_value: maximum value
|
:param max_value: maximum value
|
||||||
:param rejected: rejected values
|
:param rejected: rejected values
|
||||||
:param accept_callbacks: list of function for value rejection
|
:param accept_callbacks: list of function for value rejection
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> a = reject_random()
|
>>> a = reject_random()
|
||||||
>>> a not in [0, 1]
|
>>> a not in [0, 1]
|
||||||
True
|
True
|
||||||
>>> a >= -10
|
>>> a >= -10
|
||||||
True
|
True
|
||||||
>>> a <= 10
|
>>> a <= 10
|
||||||
True
|
True
|
||||||
>>> a = reject_random(min_value=3, max_value=11, rejected=[5, 7])
|
>>> a = reject_random(min_value=3, max_value=11, rejected=[5, 7])
|
||||||
>>> a not in [5, 7]
|
>>> a not in [5, 7]
|
||||||
True
|
True
|
||||||
>>> a >= 3
|
>>> a >= 3
|
||||||
True
|
True
|
||||||
>>> a <= 11
|
>>> a <= 11
|
||||||
True
|
True
|
||||||
>>> a = reject_random(accept_callbacks=[lambda x: x%2])
|
>>> a = reject_random(accept_callbacks=[lambda x: x%2])
|
||||||
>>> a%2
|
>>> a%2
|
||||||
1
|
1
|
||||||
>>> random.seed(0)
|
>>> random.seed(0)
|
||||||
>>> reject_random()
|
>>> reject_random()
|
||||||
2
|
2
|
||||||
>>> random.seed(1)
|
>>> random.seed(1)
|
||||||
>>> reject_random()
|
>>> reject_random()
|
||||||
-6
|
-6
|
||||||
|
|
||||||
"""
|
"""
|
||||||
conditions = [lambda x: x not in rejected] + accept_callbacks
|
conditions = [lambda x: x not in rejected] + accept_callbacks
|
||||||
|
@ -60,38 +60,38 @@ def reject_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks
|
||||||
|
|
||||||
|
|
||||||
def filter_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks=[]):
|
def filter_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks=[]):
|
||||||
""" Generate a random integer by filtering then choosing a candidate
|
"""Generate a random integer by filtering then choosing a candidate
|
||||||
|
|
||||||
:param name: name of the Integer
|
:param name: name of the Integer
|
||||||
:param min_value: minimum value
|
:param min_value: minimum value
|
||||||
:param max_value: maximum value
|
:param max_value: maximum value
|
||||||
:param rejected: rejected values
|
:param rejected: rejected values
|
||||||
:param accept_callbacks: list of function for value rejection
|
:param accept_callbacks: list of function for value rejection
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> a = filter_random()
|
>>> a = filter_random()
|
||||||
>>> a not in [0, 1]
|
>>> a not in [0, 1]
|
||||||
True
|
True
|
||||||
>>> a >= -10
|
>>> a >= -10
|
||||||
True
|
True
|
||||||
>>> a <= 10
|
>>> a <= 10
|
||||||
True
|
True
|
||||||
>>> a = filter_random(min_value=3, max_value=11, rejected=[5, 7])
|
>>> a = filter_random(min_value=3, max_value=11, rejected=[5, 7])
|
||||||
>>> a not in [5, 7]
|
>>> a not in [5, 7]
|
||||||
True
|
True
|
||||||
>>> a >= 3
|
>>> a >= 3
|
||||||
True
|
True
|
||||||
>>> a <= 11
|
>>> a <= 11
|
||||||
True
|
True
|
||||||
>>> a = filter_random(accept_callbacks=[lambda x: x%2])
|
>>> a = filter_random(accept_callbacks=[lambda x: x%2])
|
||||||
>>> a%2
|
>>> a%2
|
||||||
1
|
1
|
||||||
>>> random.seed(0)
|
>>> random.seed(0)
|
||||||
>>> filter_random()
|
>>> filter_random()
|
||||||
-7
|
-7
|
||||||
>>> random.seed(1)
|
>>> random.seed(1)
|
||||||
>>> filter_random()
|
>>> filter_random()
|
||||||
6
|
6
|
||||||
"""
|
"""
|
||||||
candidates = set(range(min_value, max_value + 1))
|
candidates = set(range(min_value, max_value + 1))
|
||||||
candidates = {c for c in candidates if c not in rejected}
|
candidates = {c for c in candidates if c not in rejected}
|
||||||
|
@ -111,8 +111,7 @@ def filter_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks
|
||||||
|
|
||||||
class FilterRandom(object):
|
class FilterRandom(object):
|
||||||
|
|
||||||
""" Integer random generator which filter then choose candidate
|
"""Integer random generator which filter then choose candidate"""
|
||||||
"""
|
|
||||||
|
|
||||||
# TODO: Faire un cache pour éviter de reconstruire les listes à chaque fois |ven. déc. 21 19:07:42 CET 2018
|
# TODO: Faire un cache pour éviter de reconstruire les listes à chaque fois |ven. déc. 21 19:07:42 CET 2018
|
||||||
|
|
||||||
|
@ -133,7 +132,7 @@ class FilterRandom(object):
|
||||||
}
|
}
|
||||||
|
|
||||||
def add_candidates(self, low, high):
|
def add_candidates(self, low, high):
|
||||||
""" Add candidates between low and high to _candidates """
|
"""Add candidates between low and high to _candidates"""
|
||||||
if low < self._min:
|
if low < self._min:
|
||||||
self._min = low
|
self._min = low
|
||||||
useless_low = False
|
useless_low = False
|
||||||
|
@ -157,11 +156,11 @@ class FilterRandom(object):
|
||||||
)
|
)
|
||||||
|
|
||||||
def candidates(self, min_value=-10, max_value=10):
|
def candidates(self, min_value=-10, max_value=10):
|
||||||
""" Return candidates between min_value and max_value """
|
"""Return candidates between min_value and max_value"""
|
||||||
return [c for c in self._candidates if (c > min_value and c < max_value)]
|
return [c for c in self._candidates if (c > min_value and c < max_value)]
|
||||||
|
|
||||||
def __call__(self, min_value=-10, max_value=10):
|
def __call__(self, min_value=-10, max_value=10):
|
||||||
""" Randomly choose on candidate """
|
"""Randomly choose on candidate"""
|
||||||
self.add_candidates(min_value, max_value)
|
self.add_candidates(min_value, max_value)
|
||||||
return random.choice(self.candidates(min_value, max_value))
|
return random.choice(self.candidates(min_value, max_value))
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ from ...core.coroutine import coroutine, STOOOP
|
||||||
|
|
||||||
@coroutine
|
@coroutine
|
||||||
def look_for_rdleaf(target):
|
def look_for_rdleaf(target):
|
||||||
""" Coroutine which look to "{...}" which are RdLeaf
|
"""Coroutine which look to "{...}" which are RdLeaf
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> from ...core.str2 import list_sink
|
>>> from ...core.str2 import list_sink
|
||||||
|
@ -53,9 +53,7 @@ def look_for_rdleaf(target):
|
||||||
|
|
||||||
|
|
||||||
class RdLeaf:
|
class RdLeaf:
|
||||||
""" Random leaf
|
"""Random leaf"""
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self._name = name
|
self._name = name
|
||||||
|
|
|
@ -4,8 +4,9 @@ from .grammar import extract_letters, eval_words
|
||||||
from .generate import random_generator
|
from .generate import random_generator
|
||||||
from .str2 import rdstr2
|
from .str2 import rdstr2
|
||||||
|
|
||||||
|
|
||||||
class RandomTree(MutableTree):
|
class RandomTree(MutableTree):
|
||||||
""" MutableTree that accept {a} syntax for random generation
|
"""MutableTree that accept {a} syntax for random generation
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> t = RandomTree()
|
>>> t = RandomTree()
|
||||||
|
@ -15,7 +16,7 @@ class RandomTree(MutableTree):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_str(cls, expression):
|
def from_str(cls, expression):
|
||||||
""" Initiate a random tree from a string that need to be parsed
|
"""Initiate a random tree from a string that need to be parsed
|
||||||
|
|
||||||
:exemple:
|
:exemple:
|
||||||
>>> t = RandomTree.from_str("{b}*x+{c}")
|
>>> t = RandomTree.from_str("{b}*x+{c}")
|
||||||
|
@ -40,7 +41,7 @@ class RandomTree(MutableTree):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def random_leaves(self) -> list[str]:
|
def random_leaves(self) -> list[str]:
|
||||||
""" Get list of random leaves
|
"""Get list of random leaves
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> from .leaf import RdLeaf
|
>>> from .leaf import RdLeaf
|
||||||
|
@ -61,10 +62,9 @@ class RandomTree(MutableTree):
|
||||||
rd_leafs.append(leaf.name)
|
rd_leafs.append(leaf.name)
|
||||||
return rd_leafs
|
return rd_leafs
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def random_value(self) -> set[str]:
|
def random_value(self) -> set[str]:
|
||||||
""" Get set of random values to generate
|
"""Get set of random values to generate
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> from .leaf import RdLeaf
|
>>> from .leaf import RdLeaf
|
||||||
|
@ -77,9 +77,8 @@ class RandomTree(MutableTree):
|
||||||
"""
|
"""
|
||||||
return extract_letters(self.random_leaves)
|
return extract_letters(self.random_leaves)
|
||||||
|
|
||||||
|
def eval_random_leaves(self, leaves_value: dict[str, int]):
|
||||||
def eval_random_leaves(self, leaves_value:dict[str, int]):
|
"""Given random leaves value get the tree
|
||||||
""" Given random leaves value get the tree
|
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> from .leaf import RdLeaf
|
>>> from .leaf import RdLeaf
|
||||||
|
@ -93,21 +92,27 @@ class RandomTree(MutableTree):
|
||||||
> 2
|
> 2
|
||||||
> 6
|
> 6
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def replace(leaf):
|
def replace(leaf):
|
||||||
try:
|
try:
|
||||||
return leaf.replace(leaves_value)
|
return leaf.replace(leaves_value)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return leaf
|
return leaf
|
||||||
|
|
||||||
return self.map_on_leaf(replace).map_on_leaf(moify)
|
return self.map_on_leaf(replace).map_on_leaf(moify)
|
||||||
|
|
||||||
def generate(self, conditions:list[str]=[], global_config:dict={} , configs:dict={}) -> Tree:
|
def generate(
|
||||||
""" Generate a random version of self
|
self, conditions: list[str] = [], global_config: dict = {}, configs: dict = {}
|
||||||
|
) -> Tree:
|
||||||
|
"""Generate a random version of self
|
||||||
|
|
||||||
:param conditions: list of conditions
|
:param conditions: list of conditions
|
||||||
:param config: global configuration for generated values
|
:param config: global configuration for generated values
|
||||||
:param configs: specific configuration for each generated values
|
:param configs: specific configuration for each generated values
|
||||||
|
|
||||||
"""
|
"""
|
||||||
generated_values = random_generator(self.random_value, conditions, global_config, configs)
|
generated_values = random_generator(
|
||||||
|
self.random_value, conditions, global_config, configs
|
||||||
|
)
|
||||||
leaves = eval_words(self.random_leaves, generated_values)
|
leaves = eval_words(self.random_leaves, generated_values)
|
||||||
return self.eval_random_leaves(leaves)
|
return self.eval_random_leaves(leaves)
|
||||||
|
|
|
@ -1,13 +1,19 @@
|
||||||
|
|
||||||
from ...core.operator import is_operator
|
from ...core.operator import is_operator
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from ...core.str2 import concurent_broadcast, lookforNumbers, pparser, missing_times, lookfor
|
from ...core.str2 import (
|
||||||
|
concurent_broadcast,
|
||||||
|
lookforNumbers,
|
||||||
|
pparser,
|
||||||
|
missing_times,
|
||||||
|
lookfor,
|
||||||
|
)
|
||||||
from ...core.coroutine import STOOOP
|
from ...core.coroutine import STOOOP
|
||||||
from ...core.MO import moify_cor
|
from ...core.MO import moify_cor
|
||||||
from .leaf import look_for_rdleaf
|
from .leaf import look_for_rdleaf
|
||||||
|
|
||||||
|
|
||||||
def rdstr2(sink):
|
def rdstr2(sink):
|
||||||
""" Return a pipeline which parse random expression and with sink as endpoint
|
"""Return a pipeline which parse random expression and with sink as endpoint
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> from ...core.str2 import list_sink
|
>>> from ...core.str2 import list_sink
|
||||||
|
@ -22,8 +28,7 @@ def rdstr2(sink):
|
||||||
|
|
||||||
def pipeline(expression):
|
def pipeline(expression):
|
||||||
str2_corout = look_for_rdleaf(
|
str2_corout = look_for_rdleaf(
|
||||||
lookforNumbers(operator_corout(
|
lookforNumbers(operator_corout(missing_times(moify_cor(pparser(sink)))))
|
||||||
missing_times(moify_cor(pparser(sink)))))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
for i in expression.replace(" ", ""):
|
for i in expression.replace(" ", ""):
|
||||||
|
@ -33,4 +38,3 @@ def rdstr2(sink):
|
||||||
return a
|
return a
|
||||||
|
|
||||||
return pipeline
|
return pipeline
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,18 @@ from ..API.expression import Expression
|
||||||
from .core.random_tree import RandomTree
|
from .core.random_tree import RandomTree
|
||||||
|
|
||||||
DEFAUTL_CONFIG = {
|
DEFAUTL_CONFIG = {
|
||||||
"rejected": [0, 1],
|
"rejected": [0, 1],
|
||||||
"min_max": (-10, 10),
|
"min_max": (-10, 10),
|
||||||
}
|
}
|
||||||
|
|
||||||
def expression_generator(template:str, conditions:list[str]=[], global_config:dict={}, configs:dict={}):
|
|
||||||
""" Generate a random expression
|
def expression_generator(
|
||||||
|
template: str,
|
||||||
|
conditions: list[str] = [],
|
||||||
|
global_config: dict = {},
|
||||||
|
configs: dict = {},
|
||||||
|
):
|
||||||
|
"""Generate a random expression
|
||||||
|
|
||||||
:param template: the template of the expression
|
:param template: the template of the expression
|
||||||
:param conditions: conditions on randomly generate variable
|
:param conditions: conditions on randomly generate variable
|
||||||
|
@ -31,5 +37,7 @@ def expression_generator(template:str, conditions:list[str]=[], global_config:di
|
||||||
|
|
||||||
"""
|
"""
|
||||||
rd_tree = RandomTree.from_str(template)
|
rd_tree = RandomTree.from_str(template)
|
||||||
generated_tree = rd_tree.generate(conditions, dict(DEFAUTL_CONFIG, **global_config), configs)
|
generated_tree = rd_tree.generate(
|
||||||
|
conditions, dict(DEFAUTL_CONFIG, **global_config), configs
|
||||||
|
)
|
||||||
return Expression._post_processing(generated_tree)
|
return Expression._post_processing(generated_tree)
|
||||||
|
|
|
@ -13,12 +13,18 @@ from .core.generate import random_generator
|
||||||
from .core.grammar import extract_letters, eval_words
|
from .core.grammar import extract_letters, eval_words
|
||||||
|
|
||||||
DEFAUTL_CONFIG = {
|
DEFAUTL_CONFIG = {
|
||||||
"rejected": [0],
|
"rejected": [0],
|
||||||
"min_max": (-10, 10),
|
"min_max": (-10, 10),
|
||||||
}
|
}
|
||||||
|
|
||||||
def list_generator(var_list:list[str], conditions:list[str]=[], global_config:dict={}, configs:dict={})->list[int]:
|
|
||||||
""" Generate random computed values from the list
|
def list_generator(
|
||||||
|
template: list[str],
|
||||||
|
conditions: list[str] = [],
|
||||||
|
global_config: dict = {},
|
||||||
|
configs: dict = {},
|
||||||
|
) -> list[int]:
|
||||||
|
"""Generate random computed values from the list
|
||||||
|
|
||||||
:param rd_variables: list of random variables to generate (can be computed value - "a*b")
|
:param rd_variables: list of random variables to generate (can be computed value - "a*b")
|
||||||
:param conditions: condition over variables
|
:param conditions: condition over variables
|
||||||
|
@ -50,7 +56,9 @@ def list_generator(var_list:list[str], conditions:list[str]=[], global_config:di
|
||||||
>>> a not in [2, 3, 5, 7]
|
>>> a not in [2, 3, 5, 7]
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
rv = extract_letters(var_list)
|
rv = extract_letters(template)
|
||||||
rv_gen = random_generator(rv, conditions, dict(DEFAUTL_CONFIG, **global_config), configs)
|
rv_gen = random_generator(
|
||||||
generated = eval_words(var_list, rv_gen)
|
rv, conditions, dict(DEFAUTL_CONFIG, **global_config), configs
|
||||||
return [generated[v] for v in var_list]
|
)
|
||||||
|
generated = eval_words(template, rv_gen)
|
||||||
|
return [generated[v] for v in template]
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
import pytest
|
|
||||||
import mapytex
|
|
||||||
|
|
||||||
def test_random_function():
|
|
||||||
assert 1 == 1
|
|
||||||
#mapytex.random("{a}")
|
|
|
@ -17,7 +17,7 @@ def test_changing_render():
|
||||||
|
|
||||||
def test_changing_rending():
|
def test_changing_rending():
|
||||||
e = mapytex.Expression.from_str("2*3")
|
e = mapytex.Expression.from_str("2*3")
|
||||||
f = mapytex.Fraction("2/3")
|
f = mapytex.Expression.from_str("2/3")
|
||||||
assert str(e) == "2 * 3"
|
assert str(e) == "2 * 3"
|
||||||
assert str(f) == "2 / 3"
|
assert str(f) == "2 / 3"
|
||||||
mapytex.render.set_render("tex")
|
mapytex.render.set_render("tex")
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
import mapytex
|
||||||
|
|
||||||
|
def test_generate_list():
|
||||||
|
random_list = mapytex.random.list(["a", "b"])
|
||||||
|
assert len(random_list) == 2
|
||||||
|
random_list = mapytex.random.list(["a", "b", "c"])
|
||||||
|
assert len(random_list) == 3
|
||||||
|
random_list = mapytex.random.list(["a", "b", "a", "b"])
|
||||||
|
assert random_list[0] == random_list[2]
|
||||||
|
assert random_list[1] == random_list[3]
|
||||||
|
|
||||||
|
def test_generate_list_calculus():
|
||||||
|
random_list = mapytex.random.list(["a", "b", "a+b"])
|
||||||
|
assert random_list[0] + random_list[1] == random_list[2]
|
||||||
|
|
||||||
|
random_list = mapytex.random.list(["a", "b", "a-b"])
|
||||||
|
assert random_list[0] - random_list[1] == random_list[2]
|
||||||
|
|
||||||
|
random_list = mapytex.random.list(["a", "b", "a*b"])
|
||||||
|
assert random_list[0] * random_list[1] == random_list[2]
|
||||||
|
|
||||||
|
random_list = mapytex.random.list(["a", "b", "a/b"])
|
||||||
|
assert random_list[0] / random_list[1] == random_list[2]
|
||||||
|
|
||||||
|
def test_generate_list_calculus_math():
|
||||||
|
import math
|
||||||
|
a, b, gcd = mapytex.random.list(["a", "b", "gcd(a, b)"])
|
||||||
|
assert math.gcd(a, b) == gcd
|
||||||
|
a, b, exp, log = mapytex.random.list(["a", "b", "exp(a)", "log(b)"])
|
||||||
|
assert math.exp(a) == exp
|
||||||
|
assert math.log(b) == log
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_list_conditions():
|
||||||
|
a, b = mapytex.random.list(["a", "b"], conditions=["a + b == 10"])
|
||||||
|
assert a + b == 10
|
||||||
|
a, b = mapytex.random.list(["a", "b"], conditions=["a * b > 0", "a + b == 10"])
|
||||||
|
assert a + b == 10
|
||||||
|
assert a * b > 0
|
||||||
|
|
||||||
|
def test_generate_list_conditions_math():
|
||||||
|
import math
|
||||||
|
a, b = mapytex.random.list(["a", "b"], conditions=["gcd(a, b) == 3"])
|
||||||
|
assert math.gcd(a, b) == 3
|
||||||
|
|
||||||
|
def test_generate_list_global_config():
|
||||||
|
global_config = {"rejected": [0, 1, 2, 3]}
|
||||||
|
a, = mapytex.random.list(["a"], global_config=global_config)
|
||||||
|
assert a not in global_config["rejected"]
|
||||||
|
|
||||||
|
global_config = {"min_max": (20, 30)}
|
||||||
|
a, = mapytex.random.list(["a"], global_config=global_config)
|
||||||
|
assert a >= 20
|
||||||
|
assert a <= 30
|
||||||
|
|
Loading…
Reference in New Issue