Compare commits
52 Commits
V2.2
...
1a20e6927d
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a20e6927d | |||
| 7649928be8 | |||
| bb43611f67 | |||
| 9dc4d08619 | |||
| 33c4872ff7 | |||
| 76dad93f03 | |||
| 6f7fd416be | |||
| 8d9294afe1 | |||
| 700665c0a5 | |||
| 7e4bfc9ba0 | |||
| cdfd3e47b5 | |||
| 364f16b872 | |||
| cc94f1f50c | |||
| ad6923f8cd | |||
| fadd93b9a6 | |||
| cb8010c7ac | |||
| f12ec38746 | |||
| f8b24472d1 | |||
| 240857a7ad | |||
| 70ec6933ac | |||
| 91779bd945 | |||
| 69e1e43966 | |||
| 16a6c4953d | |||
| 84c71f6b5d | |||
| 2a74722b19 | |||
| 43fd46379c | |||
| 2d296cf1d2 | |||
| 12109156d1 | |||
| 2fad004d6d | |||
| 207dead5d0 | |||
| e24bff23db | |||
| 69c2b3718d | |||
| 9c25142a4b | |||
| 931e55c35e | |||
| aba43c9f19 | |||
| b93f34bc38 | |||
| 204fdf755b | |||
| d75fd4c4cf | |||
| a32b684b6b | |||
| a5f062a151 | |||
| f8e1c7f962 | |||
| 1685c73051 | |||
| 04ac74d17a | |||
| 23ab39f7d4 | |||
| 41385ec561 | |||
| 8389a319f0 | |||
| 6e24756746 | |||
| d45ab560c9 | |||
| fa8beb6bb2 | |||
| b81281d78a | |||
| 071b8caf1d | |||
| 1ed0b9715a |
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
from .calculus import Expression, Integer, Decimal, list_generator
|
from .calculus import Expression, Integer, Decimal, random_list
|
||||||
|
|
||||||
# Expression.set_render('tex')
|
# Expression.set_render('tex')
|
||||||
|
|
||||||
|
|||||||
@@ -136,9 +136,11 @@ class Expression(object):
|
|||||||
:returns: TODO
|
:returns: TODO
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> Expression.random("{a}/{a*k}") # doctest: +SKIP
|
>>> e = Expression.random("{a}/{a*k}")
|
||||||
|
>>> e # doctest: +SKIP
|
||||||
<Exp: -3 / -15>
|
<Exp: -3 / -15>
|
||||||
>>> Expression.random("{a}/{a*k} - 3*{b}", variables_scope={'a':{'min_max':(10, 30)}}) # doctest: +SKIP
|
>>> e = Expression.random("{a}/{a*k} - 3*{b}", variables_scope={'a':{'min_max':(10, 30)}})
|
||||||
|
>>> e # doctest: +SKIP
|
||||||
<Exp: 18 / 108 - 3 * 9>
|
<Exp: 18 / 108 - 3 * 9>
|
||||||
>>> e = Expression.random("{a}*x + {b}*x + 3", ["a>b"], rejected=[0, 1])
|
>>> e = Expression.random("{a}*x + {b}*x + 3", ["a>b"], rejected=[0, 1])
|
||||||
>>> ee = e.simplify()
|
>>> ee = e.simplify()
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ class Token(object):
|
|||||||
|
|
||||||
def explain(self):
|
def explain(self):
|
||||||
""" Yield every calculus step which have lead to self
|
""" Yield every calculus step which have lead to self
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> from mapytex.calculus.API import Expression
|
>>> from mapytex.calculus.API import Expression
|
||||||
>>> e = Expression.from_str("2+3*4")
|
>>> e = Expression.from_str("2+3*4")
|
||||||
@@ -64,25 +64,10 @@ class Token(object):
|
|||||||
yield self
|
yield self
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<{self.__class__.__name__} {self.__txt__}>"
|
return f"<{self.__class__.__name__} {renders['txt'](self._mo)}>"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if self.RENDER == "tex":
|
return renders[self.RENDER](self._mo)
|
||||||
return self.__tex__
|
|
||||||
elif self.RENDER == "txt":
|
|
||||||
return self.__txt__
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Unknow render {self.RENDER}")
|
|
||||||
|
|
||||||
# return renders[self.RENDER](self._mo)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def __txt__(self):
|
|
||||||
return self._mo.__txt__
|
|
||||||
|
|
||||||
@property
|
|
||||||
def __tex__(self):
|
|
||||||
return self._mo.__tex__
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def raw(self):
|
def raw(self):
|
||||||
@@ -265,7 +250,6 @@ class Token(object):
|
|||||||
"""
|
"""
|
||||||
return self._operate(other, "^")
|
return self._operate(other, "^")
|
||||||
|
|
||||||
|
|
||||||
def _roperate(self, other, operation):
|
def _roperate(self, other, operation):
|
||||||
""" Make a operation between 2 Tokens """
|
""" Make a operation between 2 Tokens """
|
||||||
from ..expression import Expression
|
from ..expression import Expression
|
||||||
@@ -315,6 +299,7 @@ class Token(object):
|
|||||||
<Linear x - 3>
|
<Linear x - 3>
|
||||||
"""
|
"""
|
||||||
return self._roperate(other, "-")
|
return self._roperate(other, "-")
|
||||||
|
|
||||||
def __rmul__(self, other):
|
def __rmul__(self, other):
|
||||||
""" Multiply 2 Tokens or a Token and a Expression
|
""" Multiply 2 Tokens or a Token and a Expression
|
||||||
|
|
||||||
@@ -342,7 +327,6 @@ class Token(object):
|
|||||||
"""
|
"""
|
||||||
return self._roperate(other, "/")
|
return self._roperate(other, "/")
|
||||||
|
|
||||||
|
|
||||||
def _get_soul(self, other=None):
|
def _get_soul(self, other=None):
|
||||||
""" Get the builtin soul of self or other """
|
""" Get the builtin soul of self or other """
|
||||||
if isinstance(other, Token):
|
if isinstance(other, Token):
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ Expression is the classe wich handle all calculus. It can randomly generate or i
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from .API import Expression, Integer, Decimal
|
from .API import Expression, Integer, Decimal
|
||||||
from .core import list_generator
|
from .core import random_list
|
||||||
from decimal import getcontext
|
from decimal import getcontext
|
||||||
#getcontext().prec = 2
|
#getcontext().prec = 2
|
||||||
|
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ from .tree import Tree, AssocialTree
|
|||||||
from .compute import compute
|
from .compute import compute
|
||||||
from .typing import typing, TypingError
|
from .typing import typing, TypingError
|
||||||
from .renders import tree2txt, tree2tex
|
from .renders import tree2txt, tree2tex
|
||||||
from .random import list_generator
|
from .random import list_generator as random_list
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ This function ignores tree structure and works with lists
|
|||||||
{'a': -8, 'a*b': -40, 'b': 5, 'c': 4}
|
{'a': -8, 'a*b': -40, 'b': 5, 'c': 4}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__all__ = ["generator"]
|
__all__ = ["list_generator"]
|
||||||
|
|
||||||
from random import choice
|
from random import choice
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
@@ -256,7 +256,7 @@ def build_variable_scope(rd_variables, rejected, min_max, variables_scope):
|
|||||||
return complete_scope
|
return complete_scope
|
||||||
|
|
||||||
|
|
||||||
def list_generator(var_list, conditions=[], rejected=[0], min_max=(-10, 10), variables_scope={}):
|
def list_generator(var_list, conditions=[], rejected=[0], min_max=(-10, 10), variables_scope={}, dictionnary=False):
|
||||||
""" Generate random computed values from the list
|
""" 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")
|
||||||
@@ -264,17 +264,25 @@ def list_generator(var_list, conditions=[], rejected=[0], min_max=(-10, 10), var
|
|||||||
:param rejected: Rejected values for the generator (default [0])
|
:param rejected: Rejected values for the generator (default [0])
|
||||||
:param min_max: (min, max) limits in between variables will be generated
|
:param min_max: (min, max) limits in between variables will be generated
|
||||||
:param variables_scope: rejected and min_max define for individual variables
|
:param variables_scope: rejected and min_max define for individual variables
|
||||||
|
:param dictionnary: the return value will be a dictionnary with var_list as keys (default False)
|
||||||
:return: dictionnary of generated variables
|
:return: dictionnary of generated variables
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> values = list_generator(["a", "a*b", "b", "c"])
|
>>> a, ab, b, c = list_generator(["a", "a*b", "b", "c"])
|
||||||
>>> values # doctest: +SKIP
|
>>> a, ab, b, c # doctest: +SKIP
|
||||||
>>> values["a"] * values["b"] == values["a*b"]
|
(5, -20, -4, -3)
|
||||||
|
>>> a * b == ab
|
||||||
True
|
True
|
||||||
>>> values["a*b"] # doctest: +SKIP
|
>>> ab # doctest: +SKIP
|
||||||
>>> values["a"] * values["b"] # doctest: +SKIP
|
-20
|
||||||
|
>>> a, b # doctest: +SKIP
|
||||||
|
5, -4
|
||||||
|
>>> list_generator(["a", "a*b", "b", "c"], dictionnary=True) # doctest: +SKIP
|
||||||
|
{'a': -3, 'a*b': 18, 'b': -6, 'c': -4}
|
||||||
"""
|
"""
|
||||||
rv = extract_rv(var_list)
|
rv = extract_rv(var_list)
|
||||||
rv_gen = random_generator(rv, conditions, rejected, min_max, variables_scope)
|
rv_gen = random_generator(rv, conditions, rejected, min_max, variables_scope)
|
||||||
generated = compute_leafs(var_list, rv_gen)
|
generated = compute_leafs(var_list, rv_gen)
|
||||||
return generated
|
if dictionnary:
|
||||||
|
return generated
|
||||||
|
return [generated[v] for v in var_list]
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -7,7 +7,7 @@ except ImportError:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="mapytex",
|
name="mapytex",
|
||||||
version="2.2",
|
version="2.3.2",
|
||||||
description="Computing like a student",
|
description="Computing like a student",
|
||||||
author="Benjamin Bertrand",
|
author="Benjamin Bertrand",
|
||||||
author_email="programming@opytex.org",
|
author_email="programming@opytex.org",
|
||||||
|
|||||||
Reference in New Issue
Block a user