Compare commits
64 Commits
Author | SHA1 | Date |
---|---|---|
Bertrand Benjamin | 60ee751e91 | |
Bertrand Benjamin | d6e3f774fa | |
Bertrand Benjamin | 1347c30b92 | |
Bertrand Benjamin | bf55470467 | |
Bertrand Benjamin | 78ce8f767a | |
Bertrand Benjamin | cbcead48f7 | |
Bertrand Benjamin | ff4d8471ef | |
Bertrand Benjamin | 460255b151 | |
Bertrand Benjamin | 95fd12c430 | |
Bertrand Benjamin | a1608a20d1 | |
Bertrand Benjamin | 48088762be | |
Bertrand Benjamin | 2109fc46cb | |
Bertrand Benjamin | 1a20e6927d | |
Bertrand Benjamin | 7649928be8 | |
Bertrand Benjamin | bb43611f67 | |
Bertrand Benjamin | 9dc4d08619 | |
Bertrand Benjamin | 33c4872ff7 | |
Bertrand Benjamin | 76dad93f03 | |
Bertrand Benjamin | 6f7fd416be | |
Bertrand Benjamin | 8d9294afe1 | |
Bertrand Benjamin | 700665c0a5 | |
Bertrand Benjamin | 7e4bfc9ba0 | |
Bertrand Benjamin | cdfd3e47b5 | |
Bertrand Benjamin | 364f16b872 | |
Bertrand Benjamin | cc94f1f50c | |
Bertrand Benjamin | ad6923f8cd | |
Bertrand Benjamin | fadd93b9a6 | |
Bertrand Benjamin | cb8010c7ac | |
Bertrand Benjamin | f12ec38746 | |
Bertrand Benjamin | f8b24472d1 | |
Bertrand Benjamin | 240857a7ad | |
Bertrand Benjamin | 70ec6933ac | |
Bertrand Benjamin | 91779bd945 | |
Bertrand Benjamin | 69e1e43966 | |
Bertrand Benjamin | 16a6c4953d | |
Bertrand Benjamin | 84c71f6b5d | |
Bertrand Benjamin | 2a74722b19 | |
Bertrand Benjamin | 43fd46379c | |
Bertrand Benjamin | 2d296cf1d2 | |
Bertrand Benjamin | 12109156d1 | |
Bertrand Benjamin | 2fad004d6d | |
Bertrand Benjamin | 207dead5d0 | |
Bertrand Benjamin | e24bff23db | |
Bertrand Benjamin | 69c2b3718d | |
Bertrand Benjamin | 9c25142a4b | |
Bertrand Benjamin | 931e55c35e | |
Bertrand Benjamin | aba43c9f19 | |
Bertrand Benjamin | b93f34bc38 | |
Bertrand Benjamin | 204fdf755b | |
Bertrand Benjamin | d75fd4c4cf | |
Bertrand Benjamin | a32b684b6b | |
Bertrand Benjamin | a5f062a151 | |
Bertrand Benjamin | f8e1c7f962 | |
Bertrand Benjamin | 1685c73051 | |
Bertrand Benjamin | 04ac74d17a | |
Bertrand Benjamin | 23ab39f7d4 | |
Bertrand Benjamin | 41385ec561 | |
Bertrand Benjamin | 8389a319f0 | |
Bertrand Benjamin | 6e24756746 | |
Bertrand Benjamin | d45ab560c9 | |
Bertrand Benjamin | fa8beb6bb2 | |
Bertrand Benjamin | b81281d78a | |
Bertrand Benjamin | 071b8caf1d | |
Bertrand Benjamin | 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, random_list
|
from .calculus import Expression, Integer, Decimal, random_list, render, Polynomial, Fraction
|
||||||
|
|
||||||
# Expression.set_render('tex')
|
# Expression.set_render('tex')
|
||||||
|
|
||||||
|
|
|
@ -108,26 +108,35 @@ x^7
|
||||||
(6 + 6) * x + 4x^2 + 9
|
(6 + 6) * x + 4x^2 + 9
|
||||||
4x^2 + 12x + 9
|
4x^2 + 12x + 9
|
||||||
|
|
||||||
|
>>> e = Expression.from_str("(2x-3)(-x+2)")
|
||||||
|
>>> e_simplified = e.simplify()
|
||||||
|
>>> e_simplified
|
||||||
|
<Quadratic - 2x^2 + 7x - 6>
|
||||||
|
>>> for s in e_simplified.explain():
|
||||||
|
... print(s)
|
||||||
|
(2x - 3)(- x + 2)
|
||||||
|
(2x - 3)(- x + 2)
|
||||||
|
2x(- x) + 2x * 2 - 3(- x) - 3 * 2
|
||||||
|
2(- 1) * x^(1 + 1) + 2 * 2 * x - 3(- 1) * x - 6
|
||||||
|
4x + 3x - 2x^2 - 6
|
||||||
|
(4 + 3) * x - 2x^2 - 6
|
||||||
|
- 2x^2 + 7x - 6
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from .renders import render
|
||||||
from .expression import Expression
|
from .expression import Expression
|
||||||
from .tokens.number import Integer, Decimal
|
from .tokens import Token
|
||||||
|
from .tokens.polynomial import Polynomial
|
||||||
|
from .tokens.number import Integer, Decimal, Fraction
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
e = Expression.from_str("1+2/3/4/5")
|
e = Expression.from_str("(2x-3)(-x+2)")
|
||||||
et = e._typing()
|
e_simplified = e.simplify()
|
||||||
print("typing")
|
e_simplified
|
||||||
print(e._tree)
|
for s in e_simplified.explain():
|
||||||
e = et._order()
|
print(s._tree.map_on_leaf(lambda x: type(x)))
|
||||||
print("order")
|
print(s)
|
||||||
print(e._tree)
|
|
||||||
e = e._optimize()
|
|
||||||
print("then optimize")
|
|
||||||
print(e._tree)
|
|
||||||
e = et._optimize()
|
|
||||||
print("optimize without order")
|
|
||||||
print(e._tree)
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
|
|
@ -21,7 +21,7 @@ from ..core.random import (
|
||||||
)
|
)
|
||||||
from ..core.MO import moify
|
from ..core.MO import moify
|
||||||
from .tokens import factory
|
from .tokens import factory
|
||||||
from .renders import renders
|
from .renders import render
|
||||||
|
|
||||||
|
|
||||||
class Expression(object):
|
class Expression(object):
|
||||||
|
@ -42,7 +42,6 @@ class Expression(object):
|
||||||
14
|
14
|
||||||
"""
|
"""
|
||||||
|
|
||||||
RENDER = "txt"
|
|
||||||
|
|
||||||
def __init__(self, tree, ancestor=None):
|
def __init__(self, tree, ancestor=None):
|
||||||
"""
|
"""
|
||||||
|
@ -50,41 +49,6 @@ class Expression(object):
|
||||||
self._tree = tree
|
self._tree = tree
|
||||||
self._ancestor = ancestor
|
self._ancestor = ancestor
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def set_render(cls, render):
|
|
||||||
""" Define default render function
|
|
||||||
|
|
||||||
:param render: render name (txt or tex)
|
|
||||||
|
|
||||||
:example:
|
|
||||||
>>> e = Expression.from_str("2+3*4")
|
|
||||||
>>> print(e)
|
|
||||||
2 + 3 * 4
|
|
||||||
>>> e = Expression.from_str("2+3/4")
|
|
||||||
>>> print(e)
|
|
||||||
2 + 3 / 4
|
|
||||||
>>> es = e.simplify()
|
|
||||||
>>> print(es)
|
|
||||||
11 / 4
|
|
||||||
>>> Expression.set_render('tex')
|
|
||||||
>>> Expression.RENDER
|
|
||||||
'tex'
|
|
||||||
>>> e = Expression.from_str("2+3*4")
|
|
||||||
>>> print(e)
|
|
||||||
2 + 3 \\times 4
|
|
||||||
>>> e = Expression.from_str("2+3/4")
|
|
||||||
>>> print(e)
|
|
||||||
2 + \\dfrac{3}{4}
|
|
||||||
>>> es = e.simplify()
|
|
||||||
>>> print(es)
|
|
||||||
\\dfrac{11}{4}
|
|
||||||
>>> Expression.set_render('txt')
|
|
||||||
"""
|
|
||||||
from .tokens.token import Token
|
|
||||||
|
|
||||||
Token.set_render(render)
|
|
||||||
cls.RENDER = render
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_str(cls, string, typing=True):
|
def from_str(cls, string, typing=True):
|
||||||
""" Initiate the expression from a string
|
""" Initiate the expression from a string
|
||||||
|
@ -174,10 +138,10 @@ class Expression(object):
|
||||||
return cls(t)
|
return cls(t)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return renders[self.RENDER](self._tree)
|
return render(self._tree)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<Exp: {renders['txt'](self._tree)}>"
|
return f"<Exp: {render(self._tree, 'txt')}>"
|
||||||
|
|
||||||
def _order(self, exclude_nodes=["*", "/", "**"]):
|
def _order(self, exclude_nodes=["*", "/", "**"]):
|
||||||
""" Order the expression base on types
|
""" Order the expression base on types
|
||||||
|
@ -204,7 +168,8 @@ class Expression(object):
|
||||||
return type(leaf)
|
return type(leaf)
|
||||||
else:
|
else:
|
||||||
try:
|
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
|
return typed_leaf.signature
|
||||||
except (AttributeError, NotImplementedError, TypingError):
|
except (AttributeError, NotImplementedError, TypingError):
|
||||||
return type(leaf)
|
return type(leaf)
|
||||||
|
@ -344,15 +309,15 @@ class Expression(object):
|
||||||
|
|
||||||
comp_exp = opt_exp._compute()
|
comp_exp = opt_exp._compute()
|
||||||
|
|
||||||
if typed_exp == comp_exp:
|
if typed_exp != comp_exp:
|
||||||
typed_exp.set_ancestor(self._ancestor)
|
|
||||||
return typed_exp
|
|
||||||
else:
|
|
||||||
comp_exp.set_ancestor(self)
|
comp_exp.set_ancestor(self)
|
||||||
return comp_exp._simplify(optimize=optimize)
|
return comp_exp._simplify(optimize=optimize)
|
||||||
|
|
||||||
|
typed_exp.set_ancestor(self._ancestor)
|
||||||
|
return typed_exp
|
||||||
|
|
||||||
def simplify(self, optimize=True):
|
def simplify(self, optimize=True):
|
||||||
""" Compute as much as possible the expression
|
""" Simplify the expression, keep the history and factory child
|
||||||
|
|
||||||
:param optimize: bool to optimize tree when it's possible
|
:param optimize: bool to optimize tree when it's possible
|
||||||
:return: an expression
|
:return: an expression
|
||||||
|
|
|
@ -12,34 +12,59 @@ Expression
|
||||||
"""
|
"""
|
||||||
from ..core import tree2txt, tree2tex
|
from ..core import tree2txt, tree2tex
|
||||||
|
|
||||||
|
class Render(object):
|
||||||
|
""" Object which render Expression or token """
|
||||||
|
|
||||||
def _txt(mo_tree):
|
def __init__(self, default="txt"):
|
||||||
""" txt render for MOs or Trees"""
|
self._default = default
|
||||||
|
self._render = default
|
||||||
|
self.renders = {}
|
||||||
|
|
||||||
|
def register_render(self, name, func, attribute):
|
||||||
|
""" register a render """
|
||||||
try:
|
try:
|
||||||
return tree2txt(mo_tree)
|
self.renders[name]
|
||||||
|
except KeyError:
|
||||||
|
self.renders[name] = {"name": name, "func": func, "attribute": attribute}
|
||||||
|
else:
|
||||||
|
raise ValueError("This render name already exists")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def render_name(self):
|
||||||
|
return self._render
|
||||||
|
|
||||||
|
@property
|
||||||
|
def render(self):
|
||||||
|
return self.renders[self._render]
|
||||||
|
|
||||||
|
def __call__(self, mo_tree, tmp_render=''):
|
||||||
|
if tmp_render:
|
||||||
|
r = self.renders[tmp_render]
|
||||||
|
else:
|
||||||
|
r = self.render
|
||||||
|
|
||||||
|
try:
|
||||||
|
return r["func"](mo_tree)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return mo_tree.__txt__
|
return getattr(mo_tree, r["attribute"])
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return str(mo_tree)
|
return str(mo_tree)
|
||||||
|
|
||||||
|
def set_render(self, render):
|
||||||
def _tex(mo_tree):
|
""" Define the render """
|
||||||
""" Tex render for MOs or Trees"""
|
if render in self.renders.keys():
|
||||||
try:
|
self._render = render
|
||||||
return tree2tex(mo_tree)
|
else:
|
||||||
except ValueError:
|
raise ValueError("This render does not exists")
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
return mo_tree.__tex__
|
|
||||||
except AttributeError:
|
|
||||||
return str(mo_tree)
|
|
||||||
|
|
||||||
|
|
||||||
renders = {"txt": _txt, "tex": _tex}
|
|
||||||
|
render = Render()
|
||||||
|
render.register_render("txt", tree2txt, "__txt__")
|
||||||
|
render.register_render("tex", tree2tex, "__tex__")
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
|
|
@ -18,7 +18,7 @@ from ...core.MO import MO, MOnumber
|
||||||
from ...core.MO.fraction import MOFraction
|
from ...core.MO.fraction import MOFraction
|
||||||
from random import random
|
from random import random
|
||||||
|
|
||||||
__all__ = ["Integer", "Decimal"]
|
__all__ = ["Integer", "Decimal", "Fraction"]
|
||||||
|
|
||||||
|
|
||||||
class Integer(Token):
|
class Integer(Token):
|
||||||
|
@ -141,7 +141,7 @@ class Decimal(Token):
|
||||||
|
|
||||||
class Fraction(Token):
|
class Fraction(Token):
|
||||||
|
|
||||||
""" Token representing a fraction
|
""" Token representing a fraction of numbers
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> Fraction("3/4")
|
>>> Fraction("3/4")
|
||||||
|
@ -237,11 +237,25 @@ class Fraction(Token):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def numerator(self):
|
def numerator(self):
|
||||||
return self._mo.numerator
|
""" Get numerator of the fraction
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> a = Fraction("3/4")
|
||||||
|
>>> a.numerator
|
||||||
|
<Integer 3>
|
||||||
|
"""
|
||||||
|
return Integer(self._mo.numerator)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def denominator(self):
|
def denominator(self):
|
||||||
return self._mo.denominator
|
""" Get denominator of the fraction
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> a = Fraction("3/4")
|
||||||
|
>>> a.denominator
|
||||||
|
<Integer 4>
|
||||||
|
"""
|
||||||
|
return Integer(self._mo.denominator)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def decimal(self):
|
def decimal(self):
|
||||||
|
@ -255,7 +269,58 @@ class Fraction(Token):
|
||||||
>>> f.decimal
|
>>> f.decimal
|
||||||
<Decimal 0.3333333333333333333333333333>
|
<Decimal 0.3333333333333333333333333333>
|
||||||
"""
|
"""
|
||||||
return Decimal(_Decimal(self._mo.numerator._value) / _Decimal(self._mo.denominator._value))
|
return Decimal(self._mo._value)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def simplified(self):
|
||||||
|
""" Get the irreductible version of self
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> f = Fraction("3/4")
|
||||||
|
>>> f.simplified
|
||||||
|
<Fraction 3 / 4>
|
||||||
|
>>> f = Fraction("12/9")
|
||||||
|
>>> f.simplified
|
||||||
|
<Fraction 4 / 3>
|
||||||
|
>>> f = Fraction("12/4")
|
||||||
|
>>> f.simplified
|
||||||
|
<Integer 3>
|
||||||
|
|
||||||
|
"""
|
||||||
|
simplified = self._mo.simplified()
|
||||||
|
|
||||||
|
if isinstance(simplified, MOnumber):
|
||||||
|
return Integer(simplified)
|
||||||
|
|
||||||
|
return Fraction(simplified)
|
||||||
|
|
||||||
|
def simplify(self):
|
||||||
|
""" Itself or its simplified version
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> f = Fraction("12/8")
|
||||||
|
>>> fs = f.simplify()
|
||||||
|
>>> for i in fs.explain():
|
||||||
|
... print(i)
|
||||||
|
12 / 8
|
||||||
|
3 / 2
|
||||||
|
>>> f = Fraction("5/8")
|
||||||
|
>>> fs = f.simplify()
|
||||||
|
>>> for i in fs.explain():
|
||||||
|
... print(i)
|
||||||
|
5 / 8
|
||||||
|
"""
|
||||||
|
simplified = self.simplified
|
||||||
|
try:
|
||||||
|
if self.numerator == simplified.numerator:
|
||||||
|
return self
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
simplified._ancestor = self
|
||||||
|
return simplified
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
|
|
@ -15,6 +15,7 @@ from .token import Token
|
||||||
from . import to_be_token
|
from . import to_be_token
|
||||||
from ...core.MO import MO
|
from ...core.MO import MO
|
||||||
from ...core.MO.atoms import moify
|
from ...core.MO.atoms import moify
|
||||||
|
from ...core.MO.polynomial import MOpolynomial
|
||||||
|
|
||||||
__all__ = ["Polynomial", "Quadratic", "Linear"]
|
__all__ = ["Polynomial", "Quadratic", "Linear"]
|
||||||
|
|
||||||
|
@ -44,9 +45,21 @@ class Polynomial(Token):
|
||||||
return cls(mo, name, ancestor)
|
return cls(mo, name, ancestor)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_coefficients(cls, coefficients):
|
def from_coefficients(cls, coefficients, variable_name="x", name=""):
|
||||||
""" Initiate polynomial from list of coefficients """
|
""" Initiate polynomial from list of coefficients
|
||||||
pass
|
|
||||||
|
:examples:
|
||||||
|
>>> P = Polynomial.from_coefficients([1, 2, 3])
|
||||||
|
>>> P
|
||||||
|
<Polynomial 3x^2 + 2x + 1>
|
||||||
|
>>> P = Polynomial.from_coefficients([1, 2, -3])
|
||||||
|
>>> P
|
||||||
|
<Polynomial - 3x^2 + 2x + 1>
|
||||||
|
>>> P = Polynomial.from_coefficients([1, 2, 3], "y")
|
||||||
|
>>> P
|
||||||
|
<Polynomial 3y^2 + 2y + 1>
|
||||||
|
"""
|
||||||
|
return cls(MOpolynomial(variable_name, coefficients), name)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def random(cls):
|
def random(cls):
|
||||||
|
@ -135,7 +148,11 @@ class Linear(Polynomial):
|
||||||
>>> P.differentiate()
|
>>> P.differentiate()
|
||||||
<Integer 2>
|
<Integer 2>
|
||||||
>>> P.roots
|
>>> P.roots
|
||||||
[<Fraction - 2 / 1>]
|
[<Integer - 2>]
|
||||||
|
>>> for i in P.roots[0].explain():
|
||||||
|
... print(i)
|
||||||
|
- 2 / 1
|
||||||
|
- 2
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -177,9 +194,20 @@ class Linear(Polynomial):
|
||||||
>>> from ...core.MO.polynomial import MOpolynomial, MOMonomial
|
>>> from ...core.MO.polynomial import MOpolynomial, MOMonomial
|
||||||
>>> P = Linear(MOpolynomial('x', [1, 2]))
|
>>> P = Linear(MOpolynomial('x', [1, 2]))
|
||||||
>>> P.roots
|
>>> P.roots
|
||||||
[<Fraction - 2 / 1>]
|
[<Integer - 2>]
|
||||||
>>> #P = Linear(MOpolynomial('x', [1, -2]))
|
>>> P = Linear(MOpolynomial('x', [2, 1]))
|
||||||
>>> #P.roots
|
>>> P.roots
|
||||||
|
[<Fraction - 1 / 2>]
|
||||||
|
>>> for i in P.roots[0].explain():
|
||||||
|
... print(i)
|
||||||
|
- 1 / 2
|
||||||
|
>>> P = Linear(MOpolynomial('x', [10, 6]))
|
||||||
|
>>> P.roots
|
||||||
|
[<Fraction - 3 / 5>]
|
||||||
|
>>> for i in P.roots[0].explain():
|
||||||
|
... print(i)
|
||||||
|
- 6 / 10
|
||||||
|
- 3 / 5
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
Tokens: practical envelop of math object
|
Tokens: practical envelop of math object
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from ..renders import renders
|
from ..renders import render
|
||||||
from ...core.MO.atoms import moify
|
from ...core.MO.atoms import moify
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,8 +18,6 @@ class Token(object):
|
||||||
|
|
||||||
""" Token: practical envelop of an math object """
|
""" Token: practical envelop of an math object """
|
||||||
|
|
||||||
RENDER = "txt"
|
|
||||||
|
|
||||||
def __init__(self, mo, name="", ancestor=None):
|
def __init__(self, mo, name="", ancestor=None):
|
||||||
self._mo = mo
|
self._mo = mo
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -34,14 +32,6 @@ class Token(object):
|
||||||
):
|
):
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def set_render(cls, render):
|
|
||||||
""" Define default render function
|
|
||||||
|
|
||||||
:param render: render name (txt or tex)
|
|
||||||
"""
|
|
||||||
cls.RENDER = render
|
|
||||||
|
|
||||||
def explain(self):
|
def explain(self):
|
||||||
""" Yield every calculus step which have lead to self
|
""" Yield every calculus step which have lead to self
|
||||||
|
|
||||||
|
@ -64,25 +54,16 @@ class Token(object):
|
||||||
yield self
|
yield self
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<{self.__class__.__name__} {self.__txt__}>"
|
try:
|
||||||
|
return f"<{self.__class__.__name__} {render(self._mo._tree, 'txt')}>"
|
||||||
|
except AttributeError:
|
||||||
|
return f"<{self.__class__.__name__} {render(self._mo, 'txt')}>"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if self.RENDER == "tex":
|
try:
|
||||||
return self.__tex__
|
return render(self._mo._tree)
|
||||||
elif self.RENDER == "txt":
|
except AttributeError:
|
||||||
return self.__txt__
|
return render(self._mo)
|
||||||
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):
|
||||||
|
@ -261,10 +242,9 @@ class Token(object):
|
||||||
>>> c = a ** 2
|
>>> c = a ** 2
|
||||||
>>> c
|
>>> c
|
||||||
<Decimal 5.29>
|
<Decimal 5.29>
|
||||||
|
|
||||||
"""
|
"""
|
||||||
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 """
|
||||||
|
@ -315,6 +295,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 +323,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):
|
||||||
|
|
|
@ -13,7 +13,7 @@ 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
|
>>> from mapytex.calculus import Expression
|
||||||
>>> Expression.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)
|
||||||
2x + 6 - 3x
|
2x + 6 - 3x
|
||||||
|
@ -30,7 +30,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, render, Polynomial, Fraction
|
||||||
from .core import random_list
|
from .core import random_list
|
||||||
from decimal import getcontext
|
from decimal import getcontext
|
||||||
#getcontext().prec = 2
|
#getcontext().prec = 2
|
||||||
|
|
|
@ -37,7 +37,7 @@ def moify_cor(target):
|
||||||
>>> for i in [-2, "+", "x", "*", Decimal("3.3")]:
|
>>> for i in [-2, "+", "x", "*", Decimal("3.3")]:
|
||||||
... list2molist.send(i)
|
... list2molist.send(i)
|
||||||
>>> list2molist.throw(STOOOP)
|
>>> list2molist.throw(STOOOP)
|
||||||
[<MOnumber - 2>, '+', <MOstr x>, '*', <MOnumber 3.3>]
|
[<MOnumber -2>, '+', <MOstr x>, '*', <MOnumber 3.3>]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
@ -78,7 +78,7 @@ class MOnumber(Atom):
|
||||||
>>> MOnumber(23)
|
>>> MOnumber(23)
|
||||||
<MOnumber 23>
|
<MOnumber 23>
|
||||||
>>> MOnumber(-23)
|
>>> MOnumber(-23)
|
||||||
<MOnumber - 23>
|
<MOnumber -23>
|
||||||
|
|
||||||
As expected there will be trouble with float
|
As expected there will be trouble with float
|
||||||
|
|
||||||
|
@ -90,13 +90,13 @@ class MOnumber(Atom):
|
||||||
>>> MOnumber(Decimal("23.3"))
|
>>> MOnumber(Decimal("23.3"))
|
||||||
<MOnumber 23.3>
|
<MOnumber 23.3>
|
||||||
>>> MOnumber(Decimal("-23.3"))
|
>>> MOnumber(Decimal("-23.3"))
|
||||||
<MOnumber - 23.3>
|
<MOnumber -23.3>
|
||||||
|
|
||||||
Or directly passe a decimal string
|
Or directly passe a decimal string
|
||||||
>>> MOnumber("23.3")
|
>>> MOnumber("23.3")
|
||||||
<MOnumber 23.3>
|
<MOnumber 23.3>
|
||||||
>>> MOnumber("-23.3")
|
>>> MOnumber("-23.3")
|
||||||
<MOnumber - 23.3>
|
<MOnumber -23.3>
|
||||||
|
|
||||||
MOnumber initialisation is idempotent
|
MOnumber initialisation is idempotent
|
||||||
|
|
||||||
|
@ -259,7 +259,8 @@ class MOstr(Atom):
|
||||||
f"An MOstr should be initiate with a single caracter string, got {val}"
|
f"An MOstr should be initiate with a single caracter string, got {val}"
|
||||||
)
|
)
|
||||||
if not val.isalpha():
|
if not val.isalpha():
|
||||||
raise MOError(f"An MOstr should be initiate with a alpha string, got {val}")
|
raise MOError(
|
||||||
|
f"An MOstr should be initiate with a alpha string, got {val}")
|
||||||
|
|
||||||
Atom.__init__(self, val)
|
Atom.__init__(self, val)
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
from mapytex.calculus.core.tree import Tree
|
from mapytex.calculus.core.tree import Tree
|
||||||
from .mo import Molecule, MO
|
from .mo import Molecule, MO
|
||||||
from .atoms import MOnumber
|
from .atoms import MOnumber
|
||||||
|
from decimal import Decimal
|
||||||
|
from ..arithmetic import gcd
|
||||||
|
|
||||||
__all__ = ["MOFraction"]
|
__all__ = ["MOFraction"]
|
||||||
|
|
||||||
|
@ -32,10 +34,10 @@ class MOFraction(Molecule):
|
||||||
>>> f = MOFraction(2, 3)
|
>>> f = MOFraction(2, 3)
|
||||||
>>> f
|
>>> f
|
||||||
<MOFraction 2 / 3>
|
<MOFraction 2 / 3>
|
||||||
>>> print(f.__txt__)
|
>>> print(f.tree)
|
||||||
2 / 3
|
/
|
||||||
>>> print(f.__tex__)
|
> 2
|
||||||
\\dfrac{2}{3}
|
> 3
|
||||||
>>> print(f)
|
>>> print(f)
|
||||||
2 / 3
|
2 / 3
|
||||||
>>> f = MOFraction(2, 3, negative = True)
|
>>> f = MOFraction(2, 3, negative = True)
|
||||||
|
@ -44,11 +46,13 @@ class MOFraction(Molecule):
|
||||||
"""
|
"""
|
||||||
_numerator = MO.factory(numerator)
|
_numerator = MO.factory(numerator)
|
||||||
_denominator = MO.factory(denominator)
|
_denominator = MO.factory(denominator)
|
||||||
|
|
||||||
base_tree = Tree("/", _numerator, _denominator)
|
base_tree = Tree("/", _numerator, _denominator)
|
||||||
if negative:
|
if negative:
|
||||||
tree = Tree("-", None, base_tree)
|
tree = Tree("-", None, base_tree)
|
||||||
else:
|
else:
|
||||||
tree = base_tree
|
tree = base_tree
|
||||||
|
|
||||||
Molecule.__init__(self, tree)
|
Molecule.__init__(self, tree)
|
||||||
|
|
||||||
self._numerator = _numerator
|
self._numerator = _numerator
|
||||||
|
@ -68,6 +72,10 @@ class MOFraction(Molecule):
|
||||||
def denominator(self):
|
def denominator(self):
|
||||||
return self._denominator
|
return self._denominator
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _value(self):
|
||||||
|
return Decimal(self._numerator._value) / Decimal(self._denominator._value)
|
||||||
|
|
||||||
def inverse(self):
|
def inverse(self):
|
||||||
""" return the inverse fraction """
|
""" return the inverse fraction """
|
||||||
return MOFraction(self._denominator, self._numerator, self.negative)
|
return MOFraction(self._denominator, self._numerator, self.negative)
|
||||||
|
@ -88,6 +96,37 @@ class MOFraction(Molecule):
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def simplified(self):
|
||||||
|
""" Simplified version of self
|
||||||
|
|
||||||
|
:examplex
|
||||||
|
>>> f = MOFraction(2, 3)
|
||||||
|
>>> f
|
||||||
|
<MOFraction 2 / 3>
|
||||||
|
>>> f.simplified()
|
||||||
|
<MOFraction 2 / 3>
|
||||||
|
>>> f = MOFraction(2, 6)
|
||||||
|
>>> f
|
||||||
|
<MOFraction 2 / 6>
|
||||||
|
>>> f.simplified()
|
||||||
|
<MOFraction 1 / 3>
|
||||||
|
>>> f = MOFraction(32, 24)
|
||||||
|
>>> f.simplified()
|
||||||
|
<MOFraction 4 / 3>
|
||||||
|
>>> f = MOFraction(32, 8)
|
||||||
|
>>> f.simplified()
|
||||||
|
<MOnumber 4>
|
||||||
|
"""
|
||||||
|
frac_gcd = gcd(self.numerator._value, self.denominator._value)
|
||||||
|
new_num = self.numerator._value / frac_gcd
|
||||||
|
new_denom = self.denominator._value / frac_gcd
|
||||||
|
|
||||||
|
if new_denom == 1:
|
||||||
|
return MOnumber(new_num)
|
||||||
|
|
||||||
|
return MOFraction(new_num, new_denom)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
|
|
@ -57,20 +57,12 @@ class MO(ABC):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<{self.__class__.__name__} {self.__txt__}>"
|
return f"<{self.__class__.__name__} {self.__str__()}>"
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def __txt__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def __tex__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
try:
|
try:
|
||||||
return self._tree.__hash__()
|
return self._tree.__hash__()
|
||||||
|
@ -161,21 +153,21 @@ class Molecule(MO):
|
||||||
|
|
||||||
It is a wrapping of tree
|
It is a wrapping of tree
|
||||||
|
|
||||||
Its wrapping tree can be access throw .tree property
|
Its wrapping tree can be access through .tree property
|
||||||
"""
|
"""
|
||||||
|
|
||||||
MAINOP = None
|
MAINOP = None
|
||||||
|
|
||||||
def __init__(self, value):
|
def __init__(self, tree):
|
||||||
""" Initiate the MO
|
""" Initiate the MO
|
||||||
|
|
||||||
It should be idempotent.
|
It should be idempotent.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self._tree = value._tree
|
self._tree = tree._tree
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
self._tree = value
|
self._tree = tree
|
||||||
|
|
||||||
self.is_scalar = True
|
self.is_scalar = True
|
||||||
self._signature = None
|
self._signature = None
|
||||||
|
@ -186,18 +178,10 @@ class Molecule(MO):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def content(self):
|
def content(self):
|
||||||
return self._tree
|
return self.tree
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.__txt__)
|
return tree2txt(self.tree)
|
||||||
|
|
||||||
@property
|
|
||||||
def __txt__(self):
|
|
||||||
return tree2txt(self._tree)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def __tex__(self):
|
|
||||||
return tree2tex(self._tree)
|
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
|
|
@ -31,10 +31,10 @@ class MOstrPower(Molecule):
|
||||||
<MOstrPower x^2>
|
<MOstrPower x^2>
|
||||||
>>> print(s)
|
>>> print(s)
|
||||||
x^2
|
x^2
|
||||||
>>> print(s.__txt__)
|
>>> print(s.tree)
|
||||||
x^2
|
^
|
||||||
>>> print(s.__tex__)
|
> x
|
||||||
x^{2}
|
> 2
|
||||||
>>> MOstrPower(3, 1)
|
>>> MOstrPower(3, 1)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
|
@ -59,7 +59,8 @@ class MOstrPower(Molecule):
|
||||||
"""
|
"""
|
||||||
_variable = MO.factory(variable)
|
_variable = MO.factory(variable)
|
||||||
if not isinstance(_variable, MOstr):
|
if not isinstance(_variable, MOstr):
|
||||||
raise MOError("The variable of a monomial should be convertible into MOstr")
|
raise MOError(
|
||||||
|
"The variable of a monomial should be convertible into MOstr")
|
||||||
self._variable = _variable
|
self._variable = _variable
|
||||||
|
|
||||||
_power = MO.factory(power)
|
_power = MO.factory(power)
|
||||||
|
@ -123,14 +124,15 @@ class MOstrPower(Molecule):
|
||||||
"""
|
"""
|
||||||
if self._power > 2:
|
if self._power > 2:
|
||||||
return Tree(
|
return Tree(
|
||||||
"*", self.power, MOstrPower(self.variable, self._power._value - 1)
|
"*", self.power, MOstrPower(self.variable,
|
||||||
|
self._power._value - 1)
|
||||||
)
|
)
|
||||||
return Tree("*", self.power, MOstr(self.variable))
|
return Tree("*", self.power, MOstr(self.variable))
|
||||||
|
|
||||||
|
|
||||||
class MOMonomial(Molecule):
|
class MOMonomial(Molecule):
|
||||||
|
|
||||||
""" Monomial math object"""
|
""" Monomial math object : ax^n"""
|
||||||
|
|
||||||
MAINOP = "*"
|
MAINOP = "*"
|
||||||
|
|
||||||
|
@ -147,22 +149,20 @@ class MOMonomial(Molecule):
|
||||||
<MOMonomial 4x>
|
<MOMonomial 4x>
|
||||||
>>> print(m)
|
>>> print(m)
|
||||||
4x
|
4x
|
||||||
>>> print(m.__txt__)
|
>>> print(m.tree)
|
||||||
4x
|
*
|
||||||
>>> print(m.__tex__)
|
> 4
|
||||||
4x
|
> x
|
||||||
>>> x = MOstrPower('x', 2)
|
>>> x = MOstrPower('x', 2)
|
||||||
>>> MOMonomial(4, x)
|
>>> MOMonomial(4, x)
|
||||||
<MOMonomial 4x^2>
|
<MOMonomial 4x^2>
|
||||||
>>> m = MOMonomial(4, 'x')
|
>>> m = MOMonomial(-1, 'x')
|
||||||
>>> m
|
>>> m
|
||||||
<MOMonomial 4x>
|
<MOMonomial - x>
|
||||||
>>> print(m)
|
>>> print(m.tree)
|
||||||
4x
|
-
|
||||||
>>> print(m.__txt__)
|
> None
|
||||||
4x
|
> x
|
||||||
>>> print(m.__tex__)
|
|
||||||
4x
|
|
||||||
>>> MOMonomial(4, 'x', 1)
|
>>> MOMonomial(4, 'x', 1)
|
||||||
<MOMonomial 4x>
|
<MOMonomial 4x>
|
||||||
>>> MOMonomial(4, 'x', 2)
|
>>> MOMonomial(4, 'x', 2)
|
||||||
|
@ -170,6 +170,13 @@ class MOMonomial(Molecule):
|
||||||
>>> x2 = MOstrPower('x', 2)
|
>>> x2 = MOstrPower('x', 2)
|
||||||
>>> MOMonomial(4, x2, 3)
|
>>> MOMonomial(4, x2, 3)
|
||||||
<MOMonomial 4x^6>
|
<MOMonomial 4x^6>
|
||||||
|
>>> m = MOMonomial(-1, 'x', 2)
|
||||||
|
>>> m
|
||||||
|
<MOMonomial - x^2>
|
||||||
|
>>> print(m.tree)
|
||||||
|
-
|
||||||
|
> None
|
||||||
|
> x^2
|
||||||
>>> MOMonomial(0, x)
|
>>> MOMonomial(0, x)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
|
@ -199,34 +206,21 @@ class MOMonomial(Molecule):
|
||||||
self._power = _power
|
self._power = _power
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if self._coefficient.value != 1:
|
if self.coefficient.value == 1:
|
||||||
_tree = Tree("*", self._coefficient, self.strpower)
|
|
||||||
else:
|
|
||||||
_tree = self.strpower
|
_tree = self.strpower
|
||||||
|
else:
|
||||||
|
_tree = Tree("*", self.coefficient, self.strpower)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
_tree = Tree("*", self._coefficient, self.strpower)
|
_tree = Tree("*", self.coefficient, self.strpower)
|
||||||
|
|
||||||
Molecule.__init__(self, _tree)
|
Molecule.__init__(self, _tree)
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
if self._coefficient != -1:
|
|
||||||
return super(MOMonomial, self).__str__()
|
|
||||||
else:
|
|
||||||
return "- " + self.strpower.__str__()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def __txt__(self):
|
def tree(self):
|
||||||
if self._coefficient != -1:
|
if self._coefficient == -1:
|
||||||
return super(MOMonomial, self).__txt__
|
return Tree("-", None, self.strpower)
|
||||||
else:
|
|
||||||
return "- " + self.strpower.__txt__
|
|
||||||
|
|
||||||
@property
|
return Tree("*", self.coefficient, self.strpower)
|
||||||
def __tex__(self):
|
|
||||||
if self._coefficient != -1:
|
|
||||||
return super(MOMonomial, self).__tex__
|
|
||||||
else:
|
|
||||||
return "- " + self.strpower.__tex__
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def coefficient(self):
|
def coefficient(self):
|
||||||
|
|
|
@ -18,7 +18,7 @@ __all__ = ["MOpolynomial"]
|
||||||
|
|
||||||
class MOpolynomial(Molecule):
|
class MOpolynomial(Molecule):
|
||||||
|
|
||||||
""" MO polynomial"""
|
""" MO polynomial: ax^n + ... + z (can't be a monomial)"""
|
||||||
|
|
||||||
MAINOP = "+"
|
MAINOP = "+"
|
||||||
|
|
||||||
|
@ -39,6 +39,14 @@ class MOpolynomial(Molecule):
|
||||||
<MOpolynomial 4x^3 + 1>
|
<MOpolynomial 4x^3 + 1>
|
||||||
>>> MOpolynomial('x', {0: 1, 3: 1})
|
>>> MOpolynomial('x', {0: 1, 3: 1})
|
||||||
<MOpolynomial x^3 + 1>
|
<MOpolynomial x^3 + 1>
|
||||||
|
>>> MOpolynomial('x', [0, 0, 3])
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
TypeError: A MOpolynomial can't be monomial it has to have more than one coefficient.
|
||||||
|
>>> MOpolynomial('x', {3: 1})
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
TypeError: A MOpolynomial can't be monomial it has to have more than one coefficient.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
_variable = MO.factory(variable)
|
_variable = MO.factory(variable)
|
||||||
|
@ -58,6 +66,9 @@ class MOpolynomial(Molecule):
|
||||||
raise TypeError("Coefs needs to be a dictionnary or a list")
|
raise TypeError("Coefs needs to be a dictionnary or a list")
|
||||||
self._coefs = _coefs
|
self._coefs = _coefs
|
||||||
|
|
||||||
|
if len(self._coefs) == 1:
|
||||||
|
raise TypeError("A MOpolynomial can't be monomial it has to have more than one coefficient.")
|
||||||
|
|
||||||
monomials = OrderedDict()
|
monomials = OrderedDict()
|
||||||
for deg in sorted(self._coefs.keys()):
|
for deg in sorted(self._coefs.keys()):
|
||||||
coef = self._coefs[deg]
|
coef = self._coefs[deg]
|
||||||
|
|
|
@ -40,7 +40,7 @@ def compute(node, left_v, right_v):
|
||||||
>>> compute("+", MOnumber(1), MOnumber(2))
|
>>> compute("+", MOnumber(1), MOnumber(2))
|
||||||
<MOnumber 3>
|
<MOnumber 3>
|
||||||
>>> compute("-", None, MOnumber(2))
|
>>> compute("-", None, MOnumber(2))
|
||||||
<MOnumber - 2>
|
<MOnumber -2>
|
||||||
>>> compute("*", MOnumber(1), MOnumber(2))
|
>>> compute("*", MOnumber(1), MOnumber(2))
|
||||||
<MOnumber 2>
|
<MOnumber 2>
|
||||||
>>> compute("~", MOnumber(1), MOnumber(2))
|
>>> compute("~", MOnumber(1), MOnumber(2))
|
||||||
|
@ -70,7 +70,8 @@ def compute_capacities(node):
|
||||||
op = OPERATIONS[node]
|
op = OPERATIONS[node]
|
||||||
lines = [[node] + [mo.__name__ for mo in MOS]]
|
lines = [[node] + [mo.__name__ for mo in MOS]]
|
||||||
for left_mo in MOS:
|
for left_mo in MOS:
|
||||||
lines.append([left_mo.__name__] + [(left_mo, i) in op.funcs for i in MOS])
|
lines.append([left_mo.__name__] +
|
||||||
|
[(left_mo, i) in op.funcs for i in MOS])
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ def monumber(_, right):
|
||||||
|
|
||||||
>>> a = MOnumber(4)
|
>>> a = MOnumber(4)
|
||||||
>>> minus(None, a)
|
>>> minus(None, a)
|
||||||
<MOnumber - 4>
|
<MOnumber -4>
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return MO.factory(-right.value)
|
return MO.factory(-right.value)
|
||||||
|
|
|
@ -91,9 +91,11 @@ def mul2tex(left, right):
|
||||||
'- 3x'
|
'- 3x'
|
||||||
>>> mul2tex(a, a)
|
>>> mul2tex(a, a)
|
||||||
'x \\times x'
|
'x \\times x'
|
||||||
|
>>> mul2tex(a, MO.factory(-3))
|
||||||
|
'x(- 3)'
|
||||||
"""
|
"""
|
||||||
left_ = render_with_parenthesis(left, "*")
|
left_ = render_with_parenthesis(left, "*")
|
||||||
right_ = render_with_parenthesis(right, "*")
|
right_ = render_with_parenthesis(right, "*", is_at_right=True)
|
||||||
|
|
||||||
display_time = True
|
display_time = True
|
||||||
# if (right_[0].isalpha() and (left_.isnumeric() or left_.isdecimal())) or right_[
|
# if (right_[0].isalpha() and (left_.isnumeric() or left_.isdecimal())) or right_[
|
||||||
|
@ -183,23 +185,17 @@ def pow2tex(left, right):
|
||||||
return f"{left_}^{{{right_}}}"
|
return f"{left_}^{{{right_}}}"
|
||||||
|
|
||||||
|
|
||||||
def render_with_parenthesis(subtree, operator):
|
def render_with_parenthesis(subtree, operator, is_at_right=False):
|
||||||
subtree_need_parenthesis = False
|
subtree_need_parenthesis = False
|
||||||
try:
|
try:
|
||||||
subtree.node
|
subtree.node
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
try:
|
|
||||||
if (
|
|
||||||
OPERATORS[subtree.MAINOP]["precedence"]
|
|
||||||
< OPERATORS[operator]["precedence"]
|
|
||||||
):
|
|
||||||
subtree_need_parenthesis = True
|
|
||||||
except (AttributeError, KeyError):
|
|
||||||
pass
|
|
||||||
try:
|
try:
|
||||||
subtree_ = subtree.__tex__
|
subtree_ = subtree.__tex__
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
subtree_ = str(subtree)
|
subtree_ = str(subtree)
|
||||||
|
if subtree_.startswith("-") and OPERATORS["-"]["precedence"] < OPERATORS[operator]["precedence"] and is_at_right:
|
||||||
|
subtree_need_parenthesis = True
|
||||||
else:
|
else:
|
||||||
if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]:
|
if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]:
|
||||||
subtree_need_parenthesis = True
|
subtree_need_parenthesis = True
|
||||||
|
@ -210,7 +206,8 @@ def render_with_parenthesis(subtree, operator):
|
||||||
return subtree_
|
return subtree_
|
||||||
|
|
||||||
|
|
||||||
OPERATOR2TEX = {"+": plus2tex, "-": minus2tex, "*": mul2tex, "/": div2tex, "^": pow2tex}
|
OPERATOR2TEX = {"+": plus2tex, "-": minus2tex,
|
||||||
|
"*": mul2tex, "/": div2tex, "^": pow2tex}
|
||||||
|
|
||||||
|
|
||||||
def tree2tex(tree):
|
def tree2tex(tree):
|
||||||
|
@ -230,8 +227,17 @@ def tree2tex(tree):
|
||||||
from ..tree import Tree
|
from ..tree import Tree
|
||||||
|
|
||||||
if not isinstance(tree, Tree):
|
if not isinstance(tree, Tree):
|
||||||
raise ValueError(f"Can only render a Tree (got {type(tree).__name__}: {tree})")
|
raise ValueError(
|
||||||
return OPERATOR2TEX[tree.node](tree.left_value, tree.right_value)
|
f"Can only render a Tree (got {type(tree).__name__}: {tree})")
|
||||||
|
|
||||||
|
def expand(leaf):
|
||||||
|
try:
|
||||||
|
return leaf.tree
|
||||||
|
except AttributeError:
|
||||||
|
return leaf
|
||||||
|
expanded_tree = tree.map_on_leaf(expand)
|
||||||
|
|
||||||
|
return OPERATOR2TEX[expanded_tree.node](expanded_tree.left_value, expanded_tree.right_value)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
|
|
@ -91,11 +91,13 @@ def mul2txt(left, right):
|
||||||
'- 3x'
|
'- 3x'
|
||||||
>>> mul2txt(a, a)
|
>>> mul2txt(a, a)
|
||||||
'x * x'
|
'x * x'
|
||||||
|
>>> mul2txt(a, MO.factory(-3))
|
||||||
|
'x(- 3)'
|
||||||
"""
|
"""
|
||||||
display_time = True
|
display_time = True
|
||||||
|
|
||||||
left_ = render_with_parenthesis(left, "*")
|
left_ = render_with_parenthesis(left, "*")
|
||||||
right_ = render_with_parenthesis(right, "*")
|
right_ = render_with_parenthesis(right, "*", is_at_right=True)
|
||||||
|
|
||||||
if right_[0].isalpha():
|
if right_[0].isalpha():
|
||||||
# TODO: C'est bien beurk en dessous... |ven. déc. 21 12:03:07 CET 2018
|
# TODO: C'est bien beurk en dessous... |ven. déc. 21 12:03:07 CET 2018
|
||||||
|
@ -187,23 +189,22 @@ def pow2txt(left, right):
|
||||||
return f"{left_}^{right_}"
|
return f"{left_}^{right_}"
|
||||||
|
|
||||||
|
|
||||||
def render_with_parenthesis(subtree, operator):
|
def tree_with_parenthesis(subtree, operator):
|
||||||
|
""" Assuming the subtree is a tree, then have .node """
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def render_with_parenthesis(subtree, operator, is_at_right=False):
|
||||||
subtree_need_parenthesis = False
|
subtree_need_parenthesis = False
|
||||||
try:
|
try:
|
||||||
subtree.node
|
subtree.node
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
try:
|
|
||||||
if (
|
|
||||||
OPERATORS[subtree.MAINOP]["precedence"]
|
|
||||||
< OPERATORS[operator]["precedence"]
|
|
||||||
):
|
|
||||||
subtree_need_parenthesis = True
|
|
||||||
except (AttributeError, KeyError):
|
|
||||||
pass
|
|
||||||
try:
|
try:
|
||||||
subtree_ = subtree.__txt__
|
subtree_ = subtree.__txt__
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
subtree_ = str(subtree)
|
subtree_ = str(subtree)
|
||||||
|
if subtree_.startswith("-") and OPERATORS["-"]["precedence"] < OPERATORS[operator]["precedence"] and is_at_right:
|
||||||
|
subtree_need_parenthesis = True
|
||||||
else:
|
else:
|
||||||
if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]:
|
if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]:
|
||||||
subtree_need_parenthesis = True
|
subtree_need_parenthesis = True
|
||||||
|
@ -214,7 +215,8 @@ def render_with_parenthesis(subtree, operator):
|
||||||
return subtree_
|
return subtree_
|
||||||
|
|
||||||
|
|
||||||
OPERATOR2TXT = {"+": plus2txt, "-": minus2txt, "*": mul2txt, "/": div2txt, "^": pow2txt}
|
OPERATOR2TXT = {"+": plus2txt, "-": minus2txt,
|
||||||
|
"*": mul2txt, "/": div2txt, "^": pow2txt}
|
||||||
|
|
||||||
|
|
||||||
def tree2txt(tree):
|
def tree2txt(tree):
|
||||||
|
@ -234,8 +236,17 @@ def tree2txt(tree):
|
||||||
from ..tree import Tree
|
from ..tree import Tree
|
||||||
|
|
||||||
if not isinstance(tree, Tree):
|
if not isinstance(tree, Tree):
|
||||||
raise ValueError(f"Can only render a Tree (got {type(tree).__name__}: {tree})")
|
raise ValueError(
|
||||||
return OPERATOR2TXT[tree.node](tree.left_value, tree.right_value)
|
f"Can only render a Tree (got {type(tree).__name__}: {tree})")
|
||||||
|
|
||||||
|
def expand(leaf):
|
||||||
|
try:
|
||||||
|
return leaf.tree
|
||||||
|
except AttributeError:
|
||||||
|
return leaf
|
||||||
|
expanded_tree = tree.map_on_leaf(expand)
|
||||||
|
|
||||||
|
return OPERATOR2TXT[expanded_tree.node](expanded_tree.left_value, expanded_tree.right_value)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
|
|
@ -484,7 +484,8 @@ def lookforNumbers(target):
|
||||||
if current.replace("-", "", 1).isdigit():
|
if current.replace("-", "", 1).isdigit():
|
||||||
current += tok
|
current += tok
|
||||||
else:
|
else:
|
||||||
raise ParsingError(f"Can't build decimal with '{current}'")
|
raise ParsingError(
|
||||||
|
f"Can't build decimal with '{current}'")
|
||||||
elif tok == "-":
|
elif tok == "-":
|
||||||
if current == "":
|
if current == "":
|
||||||
current = tok
|
current = tok
|
||||||
|
@ -802,7 +803,8 @@ def str2(sink, convert_to_mo=True):
|
||||||
operator_corout(missing_times(moify_cor(pparser(sink))))
|
operator_corout(missing_times(moify_cor(pparser(sink))))
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
str2_corout = lookforNumbers(operator_corout(missing_times(pparser(sink))))
|
str2_corout = lookforNumbers(
|
||||||
|
operator_corout(missing_times(pparser(sink))))
|
||||||
|
|
||||||
for i in expression.replace(" ", ""):
|
for i in expression.replace(" ", ""):
|
||||||
str2_corout.send(i)
|
str2_corout.send(i)
|
||||||
|
@ -819,7 +821,7 @@ def rdstr2(sink):
|
||||||
:example:
|
:example:
|
||||||
>>> rdstr2list = rdstr2(list_sink)
|
>>> rdstr2list = rdstr2(list_sink)
|
||||||
>>> rdstr2list("{a}+{a*b}-2")
|
>>> rdstr2list("{a}+{a*b}-2")
|
||||||
[<RdLeaf a>, '+', <RdLeaf a*b>, '+', <MOnumber - 2>]
|
[<RdLeaf a>, '+', <RdLeaf a*b>, '+', <MOnumber -2>]
|
||||||
>>> rdstr2list("{a}({b}x+{c})")
|
>>> rdstr2list("{a}({b}x+{c})")
|
||||||
[<RdLeaf a>, '*', [<RdLeaf b>, '*', <MOstr x>, '+', <RdLeaf c>]]
|
[<RdLeaf a>, '*', [<RdLeaf b>, '*', <MOstr x>, '+', <RdLeaf c>]]
|
||||||
"""
|
"""
|
||||||
|
@ -828,7 +830,8 @@ def rdstr2(sink):
|
||||||
|
|
||||||
def pipeline(expression):
|
def pipeline(expression):
|
||||||
str2_corout = look_for_rdleaf(
|
str2_corout = look_for_rdleaf(
|
||||||
lookforNumbers(operator_corout(missing_times(moify_cor(pparser(sink)))))
|
lookforNumbers(operator_corout(
|
||||||
|
missing_times(moify_cor(pparser(sink)))))
|
||||||
)
|
)
|
||||||
|
|
||||||
for i in expression.replace(" ", ""):
|
for i in expression.replace(" ", ""):
|
||||||
|
|
|
@ -11,11 +11,11 @@ Add MO with typing
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from multipledispatch import Dispatcher
|
from multipledispatch import Dispatcher
|
||||||
from ..tree import Tree
|
from ..MO import MOnumber, MOstr
|
||||||
from ..MO import MO, MOnumber, MOstr
|
|
||||||
from ..MO.monomial import MOstrPower, MOMonomial
|
from ..MO.monomial import MOstrPower, MOMonomial
|
||||||
from ..MO.polynomial import MOpolynomial
|
from ..MO.polynomial import MOpolynomial
|
||||||
from ..MO.fraction import MOFraction
|
from ..MO.fraction import MOFraction
|
||||||
|
from ..compute.filters import special_case
|
||||||
|
|
||||||
add_doc = """ Add MOs
|
add_doc = """ Add MOs
|
||||||
|
|
||||||
|
@ -27,8 +27,26 @@ add_doc = """ Add MOs
|
||||||
|
|
||||||
add = Dispatcher("add", doc=add_doc)
|
add = Dispatcher("add", doc=add_doc)
|
||||||
|
|
||||||
|
def add_filter(left, right):
|
||||||
|
""" Special cases for add MO (adding 0)
|
||||||
|
|
||||||
|
:param left: MO
|
||||||
|
:param right: MO
|
||||||
|
:returns: MO if it is a special case, nothing other wise
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if left == 0:
|
||||||
|
return right
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
if right == 0:
|
||||||
|
return left
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
|
||||||
@add.register((MOnumber, MOFraction), MOstr)
|
@add.register((MOnumber, MOFraction), MOstr)
|
||||||
|
@special_case(add_filter)
|
||||||
def moscalar_mostr(left, right):
|
def moscalar_mostr(left, right):
|
||||||
""" add a scalar with a letter to create a MOpolynomial
|
""" add a scalar with a letter to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -39,11 +57,15 @@ def moscalar_mostr(left, right):
|
||||||
>>> a = MOFraction(1, 5)
|
>>> a = MOFraction(1, 5)
|
||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial x + 1 / 5>
|
<MOpolynomial x + 1 / 5>
|
||||||
|
>>> a = MOnumber(0)
|
||||||
|
>>> add(a, b)
|
||||||
|
<MOstr x>
|
||||||
"""
|
"""
|
||||||
return MOpolynomial(right, [left, 1])
|
return MOpolynomial(right, [left, 1])
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOstr, (MOnumber, MOFraction))
|
@add.register(MOstr, (MOnumber, MOFraction))
|
||||||
|
@special_case(add_filter)
|
||||||
def mostr_moscalar(left, right):
|
def mostr_moscalar(left, right):
|
||||||
""" add a scalar with a letter to create a MOpolynomial
|
""" add a scalar with a letter to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -54,11 +76,15 @@ def mostr_moscalar(left, right):
|
||||||
>>> b = MOFraction(1, 5)
|
>>> b = MOFraction(1, 5)
|
||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial x + 1 / 5>
|
<MOpolynomial x + 1 / 5>
|
||||||
|
>>> b = MOnumber(0)
|
||||||
|
>>> add(a, b)
|
||||||
|
<MOstr x>
|
||||||
"""
|
"""
|
||||||
return MOpolynomial(left, [right, 1])
|
return MOpolynomial(left, [right, 1])
|
||||||
|
|
||||||
|
|
||||||
@add.register((MOnumber, MOFraction), MOstrPower)
|
@add.register((MOnumber, MOFraction), MOstrPower)
|
||||||
|
@special_case(add_filter)
|
||||||
def moscalar_mostrpower(left, right):
|
def moscalar_mostrpower(left, right):
|
||||||
""" add a scalar with a letter to create a MOpolynomial
|
""" add a scalar with a letter to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -74,6 +100,7 @@ def moscalar_mostrpower(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOstrPower, (MOnumber, MOFraction))
|
@add.register(MOstrPower, (MOnumber, MOFraction))
|
||||||
|
@special_case(add_filter)
|
||||||
def mostrpower_moscalar(left, right):
|
def mostrpower_moscalar(left, right):
|
||||||
""" add a scalar with a letter to create a MOpolynomial
|
""" add a scalar with a letter to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -89,6 +116,7 @@ def mostrpower_moscalar(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register((MOnumber, MOFraction), MOMonomial)
|
@add.register((MOnumber, MOFraction), MOMonomial)
|
||||||
|
@special_case(add_filter)
|
||||||
def moscalar_momonomial(left, right):
|
def moscalar_momonomial(left, right):
|
||||||
""" add a scalar with a MOMonomial to create a MOpolynomial
|
""" add a scalar with a MOMonomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -99,11 +127,17 @@ def moscalar_momonomial(left, right):
|
||||||
>>> a = MOFraction(1, 5)
|
>>> a = MOFraction(1, 5)
|
||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 3x^4 + 1 / 5>
|
<MOpolynomial 3x^4 + 1 / 5>
|
||||||
|
|
||||||
|
>>> a = MOnumber(0)
|
||||||
|
>>> b = MOMonomial(2, 'x', 4)
|
||||||
|
>>> add(a, b)
|
||||||
|
<MOMonomial 2x^4>
|
||||||
"""
|
"""
|
||||||
return MOpolynomial(right.variable, {right.power: right.coefficient, 0: left})
|
return MOpolynomial(right.variable, {right.power: right.coefficient, 0: left})
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOMonomial, (MOnumber, MOFraction))
|
@add.register(MOMonomial, (MOnumber, MOFraction))
|
||||||
|
@special_case(add_filter)
|
||||||
def momonial_moscalar(left, right):
|
def momonial_moscalar(left, right):
|
||||||
""" add a scalar with a letter to create a MOpolynomial
|
""" add a scalar with a letter to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -115,11 +149,17 @@ def momonial_moscalar(left, right):
|
||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 3x^4 + 1 / 5>
|
<MOpolynomial 3x^4 + 1 / 5>
|
||||||
|
|
||||||
|
>>> a = MOMonomial(2, 'x', 4)
|
||||||
|
>>> b = MOnumber(0)
|
||||||
|
>>> add(a, b)
|
||||||
|
<MOMonomial 2x^4>
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return MOpolynomial(left.variable, {0: right, left.power: left.coefficient})
|
return MOpolynomial(left.variable, {0: right, left.power: left.coefficient})
|
||||||
|
|
||||||
|
|
||||||
@add.register((MOnumber, MOFraction), MOpolynomial)
|
@add.register((MOnumber, MOFraction), MOpolynomial)
|
||||||
|
@special_case(add_filter)
|
||||||
def moscalar_mopolynomial(left, right):
|
def moscalar_mopolynomial(left, right):
|
||||||
""" add a scalar with a MOpolynomial to create a MOpolynomial
|
""" add a scalar with a MOpolynomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -143,6 +183,7 @@ def moscalar_mopolynomial(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOpolynomial, (MOnumber, MOFraction))
|
@add.register(MOpolynomial, (MOnumber, MOFraction))
|
||||||
|
@special_case(add_filter)
|
||||||
def mopolynomial_moscalar(left, right):
|
def mopolynomial_moscalar(left, right):
|
||||||
""" add a scalar with a MOpolynomial to create a MOpolynomial
|
""" add a scalar with a MOpolynomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -164,6 +205,7 @@ def mopolynomial_moscalar(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOstr, MOstr)
|
@add.register(MOstr, MOstr)
|
||||||
|
@special_case(add_filter)
|
||||||
def mostr_mostr(left, right):
|
def mostr_mostr(left, right):
|
||||||
""" add 2 mostr
|
""" add 2 mostr
|
||||||
|
|
||||||
|
@ -178,6 +220,7 @@ def mostr_mostr(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOstr, MOstrPower)
|
@add.register(MOstr, MOstrPower)
|
||||||
|
@special_case(add_filter)
|
||||||
def mostr_mostrpower(left, right):
|
def mostr_mostrpower(left, right):
|
||||||
""" add a scalar with a letter to create a MOpolynomial
|
""" add a scalar with a letter to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -195,6 +238,7 @@ def mostr_mostrpower(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOstrPower, MOstr)
|
@add.register(MOstrPower, MOstr)
|
||||||
|
@special_case(add_filter)
|
||||||
def mostrpower_mostr(left, right):
|
def mostrpower_mostr(left, right):
|
||||||
""" add a scalar with a letter to create a MOpolynomial
|
""" add a scalar with a letter to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -212,6 +256,7 @@ def mostrpower_mostr(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOstrPower, MOstrPower)
|
@add.register(MOstrPower, MOstrPower)
|
||||||
|
@special_case(add_filter)
|
||||||
def mostrpower_mostrpower(left, right):
|
def mostrpower_mostrpower(left, right):
|
||||||
""" add 2 mostrpower
|
""" add 2 mostrpower
|
||||||
|
|
||||||
|
@ -231,6 +276,7 @@ def mostrpower_mostrpower(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOstr, MOpolynomial)
|
@add.register(MOstr, MOpolynomial)
|
||||||
|
@special_case(add_filter)
|
||||||
def mostr_mopolynomial(left, right):
|
def mostr_mopolynomial(left, right):
|
||||||
""" add a str with a MOpolynomial to create a MOpolynomial
|
""" add a str with a MOpolynomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -249,6 +295,7 @@ def mostr_mopolynomial(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOpolynomial, MOstr)
|
@add.register(MOpolynomial, MOstr)
|
||||||
|
@special_case(add_filter)
|
||||||
def mopolynomial_mostr(left, right):
|
def mopolynomial_mostr(left, right):
|
||||||
""" add a str with a MOpolynomial to create a MOpolynomial
|
""" add a str with a MOpolynomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -266,6 +313,7 @@ def mopolynomial_mostr(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOstrPower, MOpolynomial)
|
@add.register(MOstrPower, MOpolynomial)
|
||||||
|
@special_case(add_filter)
|
||||||
def mostrpower_mopolynomial(left, right):
|
def mostrpower_mopolynomial(left, right):
|
||||||
""" add a strPower with a MOpolynomial to create a MOpolynomial
|
""" add a strPower with a MOpolynomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -284,6 +332,7 @@ def mostrpower_mopolynomial(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOpolynomial, MOstrPower)
|
@add.register(MOpolynomial, MOstrPower)
|
||||||
|
@special_case(add_filter)
|
||||||
def mopolynomial_mostrpower(left, right):
|
def mopolynomial_mostrpower(left, right):
|
||||||
""" add a strPower with a MOpolynomial to create a MOpolynomial
|
""" add a strPower with a MOpolynomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -301,6 +350,7 @@ def mopolynomial_mostrpower(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOMonomial, MOpolynomial)
|
@add.register(MOMonomial, MOpolynomial)
|
||||||
|
@special_case(add_filter)
|
||||||
def momonomial_mopolynomial(left, right):
|
def momonomial_mopolynomial(left, right):
|
||||||
""" add a Monomial with a MOpolynomial to create a MOpolynomial
|
""" add a Monomial with a MOpolynomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -319,6 +369,7 @@ def momonomial_mopolynomial(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOpolynomial, MOMonomial)
|
@add.register(MOpolynomial, MOMonomial)
|
||||||
|
@special_case(add_filter)
|
||||||
def mopolynomial_momonomial(left, right):
|
def mopolynomial_momonomial(left, right):
|
||||||
""" add a Monomial with a MOpolynomial to create a MOpolynomial
|
""" add a Monomial with a MOpolynomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -336,6 +387,7 @@ def mopolynomial_momonomial(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOpolynomial, MOpolynomial)
|
@add.register(MOpolynomial, MOpolynomial)
|
||||||
|
@special_case(add_filter)
|
||||||
def mopolynomial_mopolynomial(left, right):
|
def mopolynomial_mopolynomial(left, right):
|
||||||
""" add a polynomial with a MOpolynomial to create a MOpolynomial
|
""" add a polynomial with a MOpolynomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -355,6 +407,7 @@ def mopolynomial_mopolynomial(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOstr, MOMonomial)
|
@add.register(MOstr, MOMonomial)
|
||||||
|
@special_case(add_filter)
|
||||||
def mostr_monomial(left, right):
|
def mostr_monomial(left, right):
|
||||||
""" add a mostr with a MOMonomial to create a MOpolynomial
|
""" add a mostr with a MOMonomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -370,6 +423,7 @@ def mostr_monomial(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOMonomial, MOstr)
|
@add.register(MOMonomial, MOstr)
|
||||||
|
@special_case(add_filter)
|
||||||
def monomial_mostr(left, right):
|
def monomial_mostr(left, right):
|
||||||
""" add a mostr with a MOMonomial to create a MOpolynomial
|
""" add a mostr with a MOMonomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -385,6 +439,7 @@ def monomial_mostr(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOstrPower, MOMonomial)
|
@add.register(MOstrPower, MOMonomial)
|
||||||
|
@special_case(add_filter)
|
||||||
def mostrpower_monomial(left, right):
|
def mostrpower_monomial(left, right):
|
||||||
""" add a mostrPower with a MOMonomial to create a MOpolynomial
|
""" add a mostrPower with a MOMonomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -402,6 +457,7 @@ def mostrpower_monomial(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOMonomial, MOstrPower)
|
@add.register(MOMonomial, MOstrPower)
|
||||||
|
@special_case(add_filter)
|
||||||
def monomial_mostrpower(left, right):
|
def monomial_mostrpower(left, right):
|
||||||
""" add a mostrPower with a MOMonomial to create a MOpolynomial
|
""" add a mostrPower with a MOMonomial to create a MOpolynomial
|
||||||
|
|
||||||
|
@ -419,6 +475,7 @@ def monomial_mostrpower(left, right):
|
||||||
|
|
||||||
|
|
||||||
@add.register(MOMonomial, MOMonomial)
|
@add.register(MOMonomial, MOMonomial)
|
||||||
|
@special_case(add_filter)
|
||||||
def monomial_momonomial(left, right):
|
def monomial_momonomial(left, right):
|
||||||
""" add a moMonomial with a MOMonomial to create a MOpolynomial
|
""" add a moMonomial with a MOMonomial to create a MOpolynomial
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,6 @@ pyparsing==2.3.0
|
||||||
pytest==3.10.1
|
pytest==3.10.1
|
||||||
simplegeneric==0.8.1
|
simplegeneric==0.8.1
|
||||||
six==1.11.0
|
six==1.11.0
|
||||||
tabulate==0.8.2
|
tabulate==0.8.7
|
||||||
traitlets==4.3.2
|
traitlets==4.3.2
|
||||||
wcwidth==0.1.7
|
wcwidth==0.1.7
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
import mapytex
|
||||||
|
|
||||||
|
def test_default_render():
|
||||||
|
assert mapytex.render.render_name == "txt"
|
||||||
|
|
||||||
|
def test_default_rending():
|
||||||
|
e = mapytex.Expression("2*3")
|
||||||
|
assert str(e) == "2*3"
|
||||||
|
|
||||||
|
def test_changing_render():
|
||||||
|
assert mapytex.render.render_name == "txt"
|
||||||
|
mapytex.render.set_render("tex")
|
||||||
|
assert mapytex.render.render_name == "tex"
|
||||||
|
mapytex.render.set_render("txt")
|
||||||
|
assert mapytex.render.render_name == "txt"
|
||||||
|
|
||||||
|
|
||||||
|
def test_changing_rending():
|
||||||
|
e = mapytex.Expression.from_str("2*3")
|
||||||
|
f = mapytex.Fraction("2/3")
|
||||||
|
assert str(e) == "2 * 3"
|
||||||
|
assert str(f) == "2 / 3"
|
||||||
|
mapytex.render.set_render("tex")
|
||||||
|
assert str(e) == "2 \\times 3"
|
||||||
|
assert str(f) == "\\dfrac{2}{3}"
|
||||||
|
mapytex.render.set_render("txt")
|
||||||
|
assert str(e) == "2 * 3"
|
||||||
|
assert str(f) == "2 / 3"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue