Feat(Core): Change default str function for MO

This commit is contained in:
Bertrand Benjamin 2018-12-21 12:20:13 +01:00
parent 092fd3c0a7
commit 9a68b826a3
10 changed files with 159 additions and 513 deletions

View File

@ -35,9 +35,7 @@ class MOFraction(Molecule):
>>> print(f.__tex__)
\\frac{2}{3}
>>> print(f)
/
> 2
> 3
2 / 3
>>> f = MOFraction(2, 3, negative = True)
>>> f
<MOFraction - 2 / 3>

View File

@ -185,8 +185,7 @@ class Molecule(MO):
return self._tree
def __str__(self):
# TODO: à changer pour utiliser .__txt__ |ven. déc. 21 08:30:33 CET 2018
return str(self.tree)
return str(self.__txt__)
@property
def __txt__(self):

View File

@ -29,9 +29,7 @@ class MOstrPower(Molecule):
>>> s
<MOstrPower x^2>
>>> print(s)
^
> x
> 2
x^2
>>> print(s.__txt__)
x^2
>>> print(s.__tex__)
@ -133,9 +131,7 @@ class MOMonomial(Molecule):
>>> m
<MOMonomial 4x>
>>> print(m)
*
> 4
> x
4x
>>> print(m.__txt__)
4x
>>> print(m.__tex__)
@ -147,9 +143,7 @@ class MOMonomial(Molecule):
>>> m
<MOMonomial 4x>
>>> print(m)
*
> 4
> x
4x
>>> print(m.__txt__)
4x
>>> print(m.__tex__)

View File

@ -43,12 +43,8 @@ Abstracts tools for calculs manipulations
>>> tt = t.apply_on_last_level(typing)
>>> print(tt.apply_on_last_level(compute))
+
> /
| > 2
| > 1
> /
| > 3
| > 4
> 2 / 1
> 3 / 4
>>> type(t.right_value)
<class 'mapytex.calculus.core.tree.Tree'>
>>> type(tt.right_value)

View File

@ -81,12 +81,8 @@ def monumber_mofraction(left, right):
>>> b = MOFraction(6, 5)
>>> print(add(a, b))
+
> /
| > 4
| > 1
> /
| > 6
| > 5
> 4 / 1
> 6 / 5
"""
left_fraction = MOFraction(left, MOnumber(1))
return Tree("+", left_fraction, right)
@ -100,12 +96,9 @@ def mofraction_monumber(left, right):
>>> b = MOnumber(4)
>>> print(add(a, b))
+
> /
| > 6
| > 5
> /
| > 4
| > 1
> 6 / 5
> 4 / 1
"""
right_fraction = MOFraction(right, MOnumber(1))
return Tree("+", left, right_fraction)
@ -159,9 +152,8 @@ def mofraction_mofraction(left, right):
| > *
| | > 2
| | > 2
> /
| > 1
| > 4
> 1 / 4
Denominators are coprime
@ -248,15 +240,9 @@ def moscalar_mopolynomial(left, right):
>>> b = MOpolynomial("x", [2, 3, 4])
>>> print(add(a, b))
+
> *
| > 4
| > ^
| | > x
| | > 2
> 4x^2
> +
| > *
| | > 3
| | > x
| > 3x
| > +
| | > 1
| | > 2
@ -280,15 +266,9 @@ def mopolynomial_moscalar(left, right):
>>> b = MOnumber(1)
>>> print(add(a, b))
+
> *
| > 4
| > ^
| | > x
| | > 2
> 4x^2
> +
| > *
| | > 3
| | > x
| > 3x
| > +
| | > 2
| | > 1
@ -312,18 +292,13 @@ def mostr_mopolynomial(left, right):
>>> b = MOpolynomial("x", [2, 3, 4])
>>> print(add(a, b))
+
> *
| > 4
| > ^
| | > x
| | > 2
> 4x^2
> +
| > +
| | > x
| | > *
| | | > 3
| | | > x
| | > 3x
| > 2
"""
if 1 not in right.coefficients.keys():
raise NotImplementedError("Polynomial with no constant, no calculus to do")
@ -348,18 +323,13 @@ def mopolynomial_mostr(left, right):
>>> b = MOstr("x")
>>> print(add(a, b))
+
> *
| > 4
| > ^
| | > x
| | > 2
> 4x^2
> +
| > +
| | > *
| | | > 3
| | | > x
| | > 3x
| | > x
| > 2
"""
if 1 not in left.coefficients.keys():
raise NotImplementedError("No degree in common")
@ -385,42 +355,24 @@ def mostrpower_mopolynomial(left, right):
>>> print(add(a, b))
+
> +
| > ^
| | > x
| | > 2
| > *
| | > 4
| | > ^
| | | > x
| | | > 2
| > x^2
| > 4x^2
> +
| > *
| | > 3
| | > x
| > 3x
| > 2
>>> b = MOpolynomial("x", [2, 3, 4, 5])
>>> print(add(a, b))
+
> +
| > *
| | > 5
| | > ^
| | | > x
| | | > 3
| > 5x^3
| > +
| | > ^
| | | > x
| | | > 2
| | > *
| | | > 4
| | | > ^
| | | | > x
| | | | > 2
| | > x^2
| | > 4x^2
> +
| > *
| | > 3
| | > x
| > 3x
| > 2
"""
if left.power not in right.coefficients.keys():
raise NotImplementedError("No degree in common")
@ -446,42 +398,25 @@ def mopolynomial_mostrpower(left, right):
>>> print(add(a, b))
+
> +
| > *
| | > 4
| | > ^
| | | > x
| | | > 2
| > ^
| | > x
| | > 2
| > 4x^2
| > x^2
> +
| > *
| | > 3
| | > x
| > 3x
| > 2
>>> a = MOpolynomial("x", [2, 3, 4, 5])
>>> print(add(a, b))
+
> +
| > *
| | > 5
| | > ^
| | | > x
| | | > 3
| > 5x^3
| > +
| | > *
| | | > 4
| | | > ^
| | | | > x
| | | | > 2
| | > ^
| | | > x
| | | > 2
| | > 4x^2
| | > x^2
> +
| > *
| | > 3
| | > x
| > 3x
| > 2
"""
if right.power not in left.coefficients.keys():
raise NotImplementedError("No degree in common")
@ -507,46 +442,24 @@ def momonomial_mopolynomial(left, right):
>>> print(add(a, b))
+
> +
| > *
| | > 10
| | > ^
| | | > x
| | | > 2
| > *
| | > 4
| | > ^
| | | > x
| | | > 2
| > 10x^2
| > 4x^2
> +
| > *
| | > 3
| | > x
| > 3x
| > 2
>>> b = MOpolynomial("x", [2, 3, 4, 5])
>>> print(add(a, b))
+
> +
| > *
| | > 5
| | > ^
| | | > x
| | | > 3
| > 5x^3
| > +
| | > *
| | | > 10
| | | > ^
| | | | > x
| | | | > 2
| | > *
| | | > 4
| | | > ^
| | | | > x
| | | | > 2
| | > 10x^2
| | > 4x^2
> +
| > *
| | > 3
| | > x
| > 3x
| > 2
"""
if left.power not in right.coefficients.keys():
raise NotImplementedError("No degree in common")
@ -572,46 +485,24 @@ def mopolynomial_momonomial(left, right):
>>> print(add(a, b))
+
> +
| > *
| | > 4
| | > ^
| | | > x
| | | > 2
| > *
| | > 10
| | > ^
| | | > x
| | | > 2
| > 4x^2
| > 10x^2
> +
| > *
| | > 3
| | > x
| > 3x
| > 2
>>> a = MOpolynomial("x", [2, 3, 4, 5])
>>> print(add(a, b))
+
> +
| > *
| | > 5
| | > ^
| | | > x
| | | > 3
| > 5x^3
| > +
| | > *
| | | > 4
| | | > ^
| | | | > x
| | | | > 2
| | > *
| | | > 10
| | | > ^
| | | | > x
| | | | > 2
| | > 4x^2
| | > 10x^2
> +
| > *
| | > 3
| | > x
| > 3x
| > 2
"""
if right.power not in left.coefficients.keys():
raise NotImplementedError("No degree in common")
@ -637,73 +528,40 @@ def mopolynomial_mopolynomial(left, right):
>>> print(add(a, b))
+
> +
| > *
| | > 4
| | > ^
| | | > x
| | | > 2
| > *
| | > 7
| | > ^
| | | > x
| | | > 2
| > 4x^2
| > 7x^2
> +
| > +
| | > *
| | | > 3
| | | > x
| | > *
| | | > 6
| | | > x
| | > 3x
| | > 6x
| > +
| | > 2
| | > 5
>>> b = MOpolynomial("x", [0, 3, 4])
>>> print(add(a, b))
+
> +
| > *
| | > 4
| | > ^
| | | > x
| | | > 2
| > *
| | > 4
| | > ^
| | | > x
| | | > 2
| > 4x^2
| > 4x^2
> +
| > +
| | > *
| | | > 3
| | | > x
| | > *
| | | > 3
| | | > x
| | > 3x
| | > 3x
| > 2
>>> b = MOpolynomial("x", [0, 3, 0, 5])
>>> print(add(a, b))
+
> +
| > *
| | > 5
| | > ^
| | | > x
| | | > 3
| > *
| | > 4
| | > ^
| | | > x
| | | > 2
| > 5x^3
| > 4x^2
> +
| > +
| | > *
| | | > 3
| | | > x
| | > *
| | | > 3
| | | > x
| | > 3x
| | > 3x
| > 2
"""
common_degree = set(left.monomials.keys()).intersection(right.monomials.keys())
if not common_degree:
@ -768,9 +626,7 @@ def mostrpower_momonomial(left, right):
> +
| > 1
| > 3
> ^
| > x
| > 2
> x^2
"""
if right.power != left.power:
raise NotImplementedError("MOs does not have same degree")
@ -790,9 +646,7 @@ def momonomial_mostrpower(left, right):
> +
| > 3
| > 1
> ^
| > x
| > 2
> x^2
"""
if left.power != right.power:
raise NotImplementedError("MOs does not have same degree")
@ -812,9 +666,8 @@ def momonomial_momonomial(left, right):
> +
| > 3
| > 4
> ^
| > x
| > 2
> x^2
"""
if left.power != right.power:
raise NotImplementedError("MOs does not have same degree")

View File

@ -91,21 +91,15 @@ def monumber_mofraction(left, right):
>>> a = MOnumber(4)
>>> b = MOFraction(6, 5)
>>> print(monumber_mofraction(a, b))
>>> print(divide(a, b))
*
> 4
> /
| > 5
| > 6
> 5 / 6
>>> b = MOFraction(6, 5, True)
>>> print(monumber_mofraction(a, b))
>>> print(divide(a, b))
*
> 4
> -
| > None
| > /
| | > 5
| | > 6
> - 5 / 6
"""
return Tree("*", left, right.inverse())
@ -118,12 +112,8 @@ def mofraction_monumber(left, right):
>>> b = MOnumber(4)
>>> print(mofraction_monumber(a, b))
*
> /
| > 6
| > 5
> /
| > 1
| > 4
> 6 / 5
> 1 / 4
"""
right_fraction = MOFraction(MOnumber(1), right)
@ -138,12 +128,8 @@ def mofraction_mofraction(left, right):
>>> b = MOFraction(4, 5)
>>> print(mofraction_mofraction(a, b))
*
> /
| > 1
| > 5
> /
| > 5
| > 4
> 1 / 5
> 5 / 4
"""
return Tree("*", left, right.inverse())

View File

@ -47,35 +47,25 @@ def mofraction(_, right):
>>> a = MOFraction(6, 5)
>>> print(minus(None, a))
-
> None
> /
| > 6
| > 5
- 6 / 5
The fraction is negative
>>> a = MOFraction(6, 5, True)
>>> print(minus(None, a))
/
> 6
> 5
6 / 5
Numerator is negative
>>> a = MOFraction(-6, 5)
>>> print(minus(None, a))
/
> 6
> 5
6 / 5
Denominators is negative
>>> a = MOFraction(6, -5)
>>> print(minus(None, a))
/
> 6
> 5
6 / 5
"""
if right.negative:
return MOFraction(right._numerator, right._denominator)
@ -101,9 +91,7 @@ def mostr(_, right):
:example:
>>> x = MOstr("x")
>>> print(minus(None, x))
*
> -1
> x
- x
"""
return MOMonomial(-1, right)
@ -114,11 +102,7 @@ def mostrpower(_, right):
:example:
>>> x2 = MOstrPower("x", 2)
>>> print(minus(None, x2))
*
> -1
> ^
| > x
| > 2
- x^2
"""
return MOMonomial(-1, right.variable, right.power)
@ -129,11 +113,7 @@ def momonomial(_, right):
:example:
>>> tx2 = MOMonomial(3, "x", 2)
>>> print(minus(None, tx2))
*
> -3
> ^
| > x
| > 2
- 3x^2
"""
try:
return MOMonomial(-right.coefficient.value, right.variable, right.power)
@ -148,17 +128,7 @@ def mopolynomial(_, right):
:example:
>>> P = MOpolynomial('x', [1, -2, 3])
>>> print(minus(None, P))
+
> *
| > -3
| > ^
| | > x
| | > 2
> +
| > *
| | > 2
| | > x
| > -1
- 3x^2 + 2x - 1
"""
neg_coefs = {p: -c.value for (p, c) in right.coefficients.items()}
return MOpolynomial(right.variable, neg_coefs)

View File

@ -158,9 +158,7 @@ def moscalar_monomonial(left, right):
> *
| > 4
| > 5
> ^
| > x
| > 4
> x^4
"""
coefficient = Tree('*', left, right.coefficient)
@ -179,9 +177,7 @@ def monomonial_moscalar(left, right):
> *
| > 4
| > 5
> ^
| > x
| > 4
> x^4
"""
coefficient = Tree('*', right, left.coefficient)
@ -279,7 +275,7 @@ def mostr_mostrpower(left, right):
@multiply.register(MOstrPower, MOMonomial)
@special_case(multiply_filter)
def mostr_momonomial(left, right):
def mostrpower_momonomial(left, right):
""" Multiply a MOstrPower and a MOMonomial
>>> a = MOstrPower('x', 2)
@ -344,11 +340,7 @@ def mostr_momonomial(left, right):
>>> a = MOstr('x')
>>> b = MOMonomial(2, 'x', 4)
>>> print(multiply(a, b))
*
> 2
> ^
| > x
| > 5
2x^5
>>> a = MOstr('x')
>>> b = MOMonomial(2, 'y', 4)
>>> multiply(a, b)
@ -369,11 +361,7 @@ def momonomial_mostr(left, right):
>>> a = MOMonomial(2, 'x', 4)
>>> b = MOstr('x')
>>> print(multiply(a, b))
*
> 2
> ^
| > x
| > 5
2x^5
>>> a = MOMonomial(2, 'y', 4)
>>> b = MOstr('x')
>>> multiply(a, b)
@ -432,17 +420,11 @@ def lotsmo_mopolynomial(left, right):
+
> *
| > 2
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > 3x^2
> +
| > *
| | > 2
| | > *
| | | > 2
| | | > x
| | > 2x
| > *
| | > 2
| | > 1
@ -452,74 +434,45 @@ def lotsmo_mopolynomial(left, right):
>>> print(multiply(a, b))
+
> *
| > /
| | > 1
| | > 5
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > 1 / 5
| > 3x^2
> +
| > *
| | > /
| | | > 1
| | | > 5
| | > *
| | | > 2
| | | > x
| | > 1 / 5
| | > 2x
| > *
| | > /
| | | > 1
| | | > 5
| | > 1 / 5
| | > 1
>>> a = MOstr("x")
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
+
> *
| > x
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > 3x^2
> +
| > *
| | > x
| | > *
| | | > 2
| | | > x
| | > 2x
| > *
| | > x
| | > 1
>>> a = MOstrPower("x", 2)
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
+
> *
| > ^
| | > x
| | > 2
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > x^2
| > 3x^2
> +
| > *
| | > ^
| | | > x
| | | > 2
| | > *
| | | > 2
| | | > x
| | > x^2
| | > 2x
| > *
| | > ^
| | | > x
| | | > 2
| | > x^2
| | > 1
>>> a = MOMonomial(3, "x", 2)
@ -527,32 +480,14 @@ def lotsmo_mopolynomial(left, right):
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > 3x^2
| > 3x^2
> +
| > *
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > *
| | | > 2
| | | > x
| | > 3x^2
| | > 2x
| > *
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > 3x^2
| | > 1
@ -573,17 +508,11 @@ def mopolynomial_lotsmo(left, right):
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > 3x^2
| > 2
> +
| > *
| | > *
| | | > 2
| | | > x
| | > 2x
| | > 2
| > *
| | > 1
@ -594,44 +523,27 @@ def mopolynomial_lotsmo(left, right):
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > /
| | > 1
| | > 5
| > 3x^2
| > 1 / 5
> +
| > *
| | > *
| | | > 2
| | | > x
| | > /
| | | > 1
| | | > 5
| | > 2x
| | > 1 / 5
| > *
| | > 1
| | > /
| | | > 1
| | | > 5
| | > 1 / 5
>>> a = MOpolynomial('x', [1, 2, 3])
>>> b = MOstr("x")
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > 3x^2
| > x
> +
| > *
| | > *
| | | > 2
| | | > x
| | > 2x
| | > x
| > *
| | > 1
@ -642,60 +554,31 @@ def mopolynomial_lotsmo(left, right):
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > ^
| | > x
| | > 2
| > 3x^2
| > x^2
> +
| > *
| | > *
| | | > 2
| | | > x
| | > ^
| | | > x
| | | > 2
| | > 2x
| | > x^2
| > *
| | > 1
| | > ^
| | | > x
| | | > 2
| | > x^2
>>> a = MOpolynomial('x', [1, 2, 3])
>>> b = MOMonomial(3, "x", 2)
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > 3x^2
| > 3x^2
> +
| > *
| | > *
| | | > 2
| | | > x
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > 2x
| | > 3x^2
| > *
| | > 1
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > 3x^2
"""
coefs = [Tree("*", monom, right) \
@ -714,41 +597,23 @@ def mopolynomial_mopolynomial(left, right):
+
> +
| > *
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > *
| | | > 5
| | | > x
| | > 3x^2
| | > 5x
| > +
| | > *
| | | > *
| | | | > 3
| | | | > ^
| | | | | > x
| | | | | > 2
| | | > 3x^2
| | | > 4
| | > *
| | | > *
| | | | > 2
| | | | > x
| | | > *
| | | | > 5
| | | | > x
| | | > 2x
| | | > 5x
> +
| > *
| | > *
| | | > 2
| | | > x
| | > 2x
| | > 4
| > +
| | > *
| | | > 1
| | | > *
| | | | > 5
| | | | > x
| | | > 5x
| | > *
| | | > 1
| | | > 4

View File

@ -142,28 +142,8 @@ def mopolynomial_monumber(left, right):
>>> a = MOnumber(2)
>>> print(power(P, a))
*
> +
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > +
| | > *
| | | > -2
| | | > x
| | > 1
> +
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > +
| | > *
| | | > -2
| | | > x
| | > 1
> 3x^2 - 2x + 1
> 3x^2 - 2x + 1
"""
return Tree.from_list("*", [left]*right.value)

View File

@ -84,6 +84,8 @@ def mul2txt(left, right):
>>> a = MO.factory('x')
>>> mul2txt(MO.factory(3), a)
'3x'
>>> mul2txt(MO.factory(-3), a)
'- 3x'
>>> mul2txt(a, a)
'x * x'
"""
@ -92,8 +94,11 @@ def mul2txt(left, right):
left_ = render_with_parenthesis(left, "*")
right_ = render_with_parenthesis(right, "*")
if (right_[0].isalpha() and (left_.isnumeric() or left_.isdecimal())) or \
right_[0] == '(':
if right_[0].isalpha():
# TODO: C'est bien beurk en dessous... |ven. déc. 21 12:03:07 CET 2018
if type(left).__name__ == 'MOnumber':
display_time = False
elif right_[0] == '(':
display_time = False
if display_time: