Style: apply black
This commit is contained in:
@@ -14,6 +14,7 @@ from ..core import AssocialTree, Tree, compute, typing, TypingError
|
||||
from .tokens import factory
|
||||
from .renders import renders
|
||||
|
||||
|
||||
class Expression(object):
|
||||
|
||||
"""
|
||||
@@ -32,7 +33,7 @@ class Expression(object):
|
||||
14
|
||||
"""
|
||||
|
||||
RENDER = 'txt'
|
||||
RENDER = "txt"
|
||||
|
||||
def __init__(self, tree, ancestor=None):
|
||||
"""
|
||||
@@ -76,7 +77,7 @@ class Expression(object):
|
||||
return cls(t)
|
||||
|
||||
@classmethod
|
||||
def random(self, template, conditions = [], shuffle = False):
|
||||
def random(self, template, conditions=[], shuffle=False):
|
||||
""" Initiate randomly the expression
|
||||
|
||||
:param template: the template of the expression
|
||||
@@ -128,6 +129,7 @@ class Expression(object):
|
||||
>>> print(e._order())
|
||||
x + 5x + 6x^3 + 2x^2 + 4x^2 + 1 + 3
|
||||
"""
|
||||
|
||||
def signature(leaf):
|
||||
try:
|
||||
leaf.node
|
||||
@@ -138,7 +140,7 @@ class Expression(object):
|
||||
return type(leaf)
|
||||
else:
|
||||
try:
|
||||
typed_leaf = typing(leaf.node, leaf.left_value, leaf.right_value)
|
||||
typed_leaf = typing(leaf.node, leaf.left_value, leaf.right_value)
|
||||
return typed_leaf.signature
|
||||
except (AttributeError, NotImplementedError, TypingError):
|
||||
return type(leaf)
|
||||
@@ -148,8 +150,9 @@ class Expression(object):
|
||||
except AttributeError:
|
||||
return self
|
||||
|
||||
organised = AssocialTree.from_any_tree(self._tree).\
|
||||
organise_by(signature, recursive=True, exclude_nodes=exclude_nodes)
|
||||
organised = AssocialTree.from_any_tree(self._tree).organise_by(
|
||||
signature, recursive=True, exclude_nodes=exclude_nodes
|
||||
)
|
||||
return Expression(organised)
|
||||
|
||||
def _optimize(self, exclude_nodes=["/", "**"]):
|
||||
@@ -380,6 +383,7 @@ class Expression(object):
|
||||
else:
|
||||
yield self
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
@@ -12,6 +12,7 @@ Expression
|
||||
"""
|
||||
from ..core import tree2txt, tree2tex
|
||||
|
||||
|
||||
def _txt(mo_tree):
|
||||
""" txt render for MOs or Trees"""
|
||||
try:
|
||||
@@ -24,6 +25,7 @@ def _txt(mo_tree):
|
||||
except AttributeError:
|
||||
return str(mo_tree)
|
||||
|
||||
|
||||
def _tex(mo_tree):
|
||||
""" Tex render for MOs or Trees"""
|
||||
try:
|
||||
@@ -36,10 +38,8 @@ def _tex(mo_tree):
|
||||
except AttributeError:
|
||||
return str(mo_tree)
|
||||
|
||||
renders = {
|
||||
'txt': _txt,
|
||||
'tex': _tex,
|
||||
}
|
||||
|
||||
renders = {"txt": _txt, "tex": _tex}
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
|
@@ -21,6 +21,7 @@ from .polynomial import Polynomial, Linear, Quadratic
|
||||
|
||||
__all__ = ["factory"]
|
||||
|
||||
|
||||
def factory(exp, name="", ancestor=None):
|
||||
""" Transform a Expression with on MathObject (from core) to a appropriate token (from API)
|
||||
|
||||
@@ -67,22 +68,31 @@ def factory(exp, name="", ancestor=None):
|
||||
raise TypeError(f"Can't build from MOnumber ({mo}) neither int nor decimal")
|
||||
|
||||
elif isinstance(mo, MOFraction):
|
||||
if isinstance(mo._denominator, MOnumber) and \
|
||||
isinstance(mo._numerator, MOnumber):
|
||||
if isinstance(mo._denominator, MOnumber) and isinstance(
|
||||
mo._numerator, MOnumber
|
||||
):
|
||||
return Fraction.from_mo(mo, name, ancestor)
|
||||
|
||||
raise TypeError(f"Can't build from MOFraction ({mo}) numerator and denominator are not MOnumber")
|
||||
raise TypeError(
|
||||
f"Can't build from MOFraction ({mo}) numerator and denominator are not MOnumber"
|
||||
)
|
||||
|
||||
elif isinstance(mo, (MOstr, MOstrPower, MOMonomial, MOpolynomial)):
|
||||
if not isinstance(mo._variable, (MOstr, str)):
|
||||
raise TypeError(f"Can't build Polynom over something else than a letter (got {mo._variable})")
|
||||
if isinstance(mo, MOstr) or \
|
||||
(isinstance(mo, MOMonomial) and mo.power.value == 1) or \
|
||||
(isinstance(mo, MOpolynomial) and mo.power.value == 1):
|
||||
raise TypeError(
|
||||
f"Can't build Polynom over something else than a letter (got {mo._variable})"
|
||||
)
|
||||
if (
|
||||
isinstance(mo, MOstr)
|
||||
or (isinstance(mo, MOMonomial) and mo.power.value == 1)
|
||||
or (isinstance(mo, MOpolynomial) and mo.power.value == 1)
|
||||
):
|
||||
return Linear.from_mo(mo, name, ancestor)
|
||||
elif (isinstance(mo, MOstrPower) and mo.power.value == 2) or \
|
||||
(isinstance(mo, MOMonomial) and mo.power.value == 2) or \
|
||||
(isinstance(mo, MOpolynomial) and mo.power.value == 2):
|
||||
elif (
|
||||
(isinstance(mo, MOstrPower) and mo.power.value == 2)
|
||||
or (isinstance(mo, MOMonomial) and mo.power.value == 2)
|
||||
or (isinstance(mo, MOpolynomial) and mo.power.value == 2)
|
||||
):
|
||||
return Quadratic.from_mo(mo, name, ancestor)
|
||||
else:
|
||||
return Polynomial.from_mo(mo, name, ancestor)
|
||||
@@ -90,6 +100,7 @@ def factory(exp, name="", ancestor=None):
|
||||
else:
|
||||
raise TypeError(f"{type(mo)} is unknown MathObject")
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
@@ -19,6 +19,7 @@ from ...core.MO.fraction import MOFraction
|
||||
|
||||
__all__ = ["Integer", "Decimal"]
|
||||
|
||||
|
||||
class Integer(Token):
|
||||
|
||||
""" Token representing a integer
|
||||
@@ -41,7 +42,7 @@ class Integer(Token):
|
||||
mo = a
|
||||
|
||||
Token.__init__(self, mo, name, ancestor)
|
||||
self._mathtype = 'entier'
|
||||
self._mathtype = "entier"
|
||||
|
||||
@classmethod
|
||||
def from_mo(cls, mo, name="", ancestor=None):
|
||||
@@ -53,13 +54,9 @@ class Integer(Token):
|
||||
return cls(mo, name, ancestor)
|
||||
|
||||
@classmethod
|
||||
def random(cls,
|
||||
name = "",
|
||||
min_value = -10,
|
||||
max_value = 10,
|
||||
rejected = [0, 1],
|
||||
accept_callbacks=[],
|
||||
):
|
||||
def random(
|
||||
cls, name="", min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks=[]
|
||||
):
|
||||
""" Generate a random Integer
|
||||
|
||||
:param name: name of the Integer
|
||||
@@ -69,11 +66,11 @@ class Integer(Token):
|
||||
:param accept_callbacks: list of function for value acceptation
|
||||
|
||||
"""
|
||||
candidate = filter_random(min_value, max_value,
|
||||
rejected, accept_callbacks)
|
||||
candidate = filter_random(min_value, max_value, rejected, accept_callbacks)
|
||||
|
||||
return Integer(candidate, name)
|
||||
|
||||
|
||||
class Decimal(Token):
|
||||
|
||||
""" Token representing a decimal
|
||||
@@ -98,27 +95,28 @@ class Decimal(Token):
|
||||
else:
|
||||
mo = a
|
||||
|
||||
self._mathtype = 'décimal'
|
||||
self._mathtype = "décimal"
|
||||
Token.__init__(self, mo, name, ancestor)
|
||||
|
||||
@classmethod
|
||||
def from_mo(cls, mo, name="", ancestor=None):
|
||||
if not isinstance(mo, MOnumber):
|
||||
raise TypeError
|
||||
if not isinstance(mo.value, _Decimal):
|
||||
if not isinstance(mo.value, _Decimal):
|
||||
raise TypeError
|
||||
|
||||
return cls(mo, name, ancestor)
|
||||
|
||||
@classmethod
|
||||
def random(cls,
|
||||
name= "",
|
||||
min_value = -10,
|
||||
max_value = 10,
|
||||
digits = 2,
|
||||
rejected = [0, 1],
|
||||
reject_callbacks=[],
|
||||
):
|
||||
def random(
|
||||
cls,
|
||||
name="",
|
||||
min_value=-10,
|
||||
max_value=10,
|
||||
digits=2,
|
||||
rejected=[0, 1],
|
||||
reject_callbacks=[],
|
||||
):
|
||||
""" Generate a random Decimal
|
||||
|
||||
:param name: name of the Integer
|
||||
@@ -131,10 +129,10 @@ class Decimal(Token):
|
||||
"""
|
||||
conditions = [lambda x: x in rejected] + reject_callbacks
|
||||
|
||||
float_cand = (max_value - min_value)*random() + min_value
|
||||
float_cand = (max_value - min_value) * random() + min_value
|
||||
candidate = _Decimal(f"{float_cand:.{digits}f}")
|
||||
while any(c(candidate) for c in conditions):
|
||||
float_cand = (max_value - min_value)*random() + min_value
|
||||
float_cand = (max_value - min_value) * random() + min_value
|
||||
candidate = _Decimal(f"{float_cand:.{digits}f}")
|
||||
|
||||
return Decimal(candidate, name)
|
||||
@@ -152,39 +150,44 @@ class Fraction(Token):
|
||||
def __init__(self, a, name="", ancestor=None):
|
||||
if not isinstance(a, MO):
|
||||
if isinstance(a, str):
|
||||
num, denom = a.split('/')
|
||||
num, denom = a.split("/")
|
||||
mo = MOFraction(int(num), int(denom))
|
||||
else:
|
||||
raise TypeError
|
||||
else:
|
||||
mo = a
|
||||
|
||||
self._mathtype = 'fraction'
|
||||
self._mathtype = "fraction"
|
||||
Token.__init__(self, mo, name, ancestor)
|
||||
|
||||
@classmethod
|
||||
def from_mo(cls, mo, name="", ancestor=None):
|
||||
if not isinstance(mo, MOFraction):
|
||||
raise TypeError
|
||||
if not isinstance(mo._numerator, MOnumber):
|
||||
if not isinstance(mo._numerator, MOnumber):
|
||||
raise TypeError
|
||||
if not isinstance(mo._denominator, MOnumber):
|
||||
if not isinstance(mo._denominator, MOnumber):
|
||||
raise TypeError
|
||||
|
||||
return cls(mo, name, ancestor)
|
||||
|
||||
@classmethod
|
||||
def random(cls,
|
||||
name="",
|
||||
fix_num="",
|
||||
min_num=-10, max_num=10, rejected_num=[0],
|
||||
accept_num_callbacks=[],
|
||||
fix_denom="",
|
||||
min_denom=-10, max_denom=10, rejected_denom=[0, 1, -1],
|
||||
accept_denom_callbacks=[],
|
||||
irreductible=False,
|
||||
not_integer=True
|
||||
):
|
||||
def random(
|
||||
cls,
|
||||
name="",
|
||||
fix_num="",
|
||||
min_num=-10,
|
||||
max_num=10,
|
||||
rejected_num=[0],
|
||||
accept_num_callbacks=[],
|
||||
fix_denom="",
|
||||
min_denom=-10,
|
||||
max_denom=10,
|
||||
rejected_denom=[0, 1, -1],
|
||||
accept_denom_callbacks=[],
|
||||
irreductible=False,
|
||||
not_integer=True,
|
||||
):
|
||||
""" Generate a random Fraction
|
||||
|
||||
:param name: Name of the fraction
|
||||
@@ -202,9 +205,7 @@ class Fraction(Token):
|
||||
:param not_integer: can the generated fraction be egal to an interger
|
||||
"""
|
||||
if fix_num == "":
|
||||
num = filter_random(min_num, max_num,
|
||||
rejected_num,
|
||||
accept_num_callbacks)
|
||||
num = filter_random(min_num, max_num, rejected_num, accept_num_callbacks)
|
||||
else:
|
||||
num = fix_num
|
||||
|
||||
@@ -212,17 +213,21 @@ class Fraction(Token):
|
||||
accept_callbacks = accept_denom_callbacks
|
||||
|
||||
if irreductible:
|
||||
|
||||
def prime_with_num(denom):
|
||||
return gcd(num, denom) == 1
|
||||
|
||||
accept_callbacks.append(prime_with_num)
|
||||
if not_integer:
|
||||
|
||||
def not_divise_num(denom):
|
||||
return num % denom != 0
|
||||
|
||||
accept_callbacks.append(not_divise_num)
|
||||
|
||||
denom = filter_random(min_denom, max_denom,
|
||||
rejected_denom,
|
||||
accept_callbacks)
|
||||
denom = filter_random(
|
||||
min_denom, max_denom, rejected_denom, accept_callbacks
|
||||
)
|
||||
else:
|
||||
denom = fix_denom
|
||||
|
||||
|
@@ -15,6 +15,7 @@ from ...core.MO import MO
|
||||
|
||||
__all__ = ["Polynomial", "Quadratic", "Linear"]
|
||||
|
||||
|
||||
class Polynomial(Token):
|
||||
|
||||
""" Token representing a polynomial """
|
||||
@@ -29,7 +30,7 @@ class Polynomial(Token):
|
||||
mo = a
|
||||
|
||||
Token.__init__(self, mo, name, ancestor)
|
||||
self._mathtype = 'polynome'
|
||||
self._mathtype = "polynome"
|
||||
|
||||
@classmethod
|
||||
def from_mo(cls, mo, name="", ancestor=None):
|
||||
@@ -52,6 +53,7 @@ class Polynomial(Token):
|
||||
""" Call a Polynomial to evaluate itself on value """
|
||||
pass
|
||||
|
||||
|
||||
class Linear(Polynomial):
|
||||
|
||||
""" Token representing a linear """
|
||||
@@ -59,12 +61,13 @@ class Linear(Polynomial):
|
||||
def __init__(self, mo, name="", ancestor=None):
|
||||
|
||||
Polynomial.__init__(self, mo, name, ancestor)
|
||||
self._mathtype = 'affine'
|
||||
self._mathtype = "affine"
|
||||
|
||||
@classmethod
|
||||
def random(cls):
|
||||
raise NotImplemented
|
||||
|
||||
|
||||
class Quadratic(Polynomial):
|
||||
|
||||
""" Token representing a quadratic """
|
||||
@@ -72,7 +75,7 @@ class Quadratic(Polynomial):
|
||||
def __init__(self, mo, name="", ancestor=None):
|
||||
|
||||
Polynomial.__init__(self, mo, name, ancestor)
|
||||
self._mathtype = 'polynome du 2nd degré'
|
||||
self._mathtype = "polynome du 2nd degré"
|
||||
|
||||
@classmethod
|
||||
def random(cls):
|
||||
|
@@ -12,13 +12,14 @@ Tokens: practical envelop of math object
|
||||
"""
|
||||
from ..renders import renders
|
||||
|
||||
|
||||
class Token(object):
|
||||
|
||||
""" Token: practical envelop of an math object """
|
||||
|
||||
RENDER = 'txt'
|
||||
RENDER = "txt"
|
||||
|
||||
def __init__(self, mo, name="", ancestor = None):
|
||||
def __init__(self, mo, name="", ancestor=None):
|
||||
self._mo = mo
|
||||
self.name = name
|
||||
self._mathtype = None
|
||||
@@ -71,6 +72,7 @@ class Token(object):
|
||||
from ..expression import Expression
|
||||
from ...core import Tree
|
||||
from . import factory
|
||||
|
||||
if not isinstance(other, Token):
|
||||
_other = factory(other)
|
||||
else:
|
||||
@@ -165,6 +167,7 @@ class Token(object):
|
||||
"""
|
||||
return self._operate(other, "/")
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
Reference in New Issue
Block a user