Fix: simplify rendering by expanding tree. still bugs with set_render

This commit is contained in:
2020-12-12 23:14:44 +01:00
parent 1a20e6927d
commit 2109fc46cb
12 changed files with 140 additions and 120 deletions

View File

@@ -37,7 +37,7 @@ def moify_cor(target):
>>> for i in [-2, "+", "x", "*", Decimal("3.3")]:
... list2molist.send(i)
>>> list2molist.throw(STOOOP)
[<MOnumber - 2>, '+', <MOstr x>, '*', <MOnumber 3.3>]
[<MOnumber -2>, '+', <MOstr x>, '*', <MOnumber 3.3>]
"""
try:
@@ -78,7 +78,7 @@ class MOnumber(Atom):
>>> MOnumber(23)
<MOnumber 23>
>>> MOnumber(-23)
<MOnumber - 23>
<MOnumber -23>
As expected there will be trouble with float
@@ -90,13 +90,13 @@ class MOnumber(Atom):
>>> MOnumber(Decimal("23.3"))
<MOnumber 23.3>
>>> MOnumber(Decimal("-23.3"))
<MOnumber - 23.3>
<MOnumber -23.3>
Or directly passe a decimal string
>>> MOnumber("23.3")
<MOnumber 23.3>
>>> MOnumber("-23.3")
<MOnumber - 23.3>
<MOnumber -23.3>
MOnumber initialisation is idempotent
@@ -259,7 +259,8 @@ class MOstr(Atom):
f"An MOstr should be initiate with a single caracter string, got {val}"
)
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)

View File

@@ -32,10 +32,10 @@ class MOFraction(Molecule):
>>> f = MOFraction(2, 3)
>>> f
<MOFraction 2 / 3>
>>> print(f.__txt__)
2 / 3
>>> print(f.__tex__)
\\dfrac{2}{3}
>>> print(f.tree)
/
> 2
> 3
>>> print(f)
2 / 3
>>> f = MOFraction(2, 3, negative = True)
@@ -44,11 +44,13 @@ class MOFraction(Molecule):
"""
_numerator = MO.factory(numerator)
_denominator = MO.factory(denominator)
base_tree = Tree("/", _numerator, _denominator)
if negative:
tree = Tree("-", None, base_tree)
else:
tree = base_tree
Molecule.__init__(self, tree)
self._numerator = _numerator

View File

@@ -57,20 +57,12 @@ class MO(ABC):
pass
def __repr__(self):
return f"<{self.__class__.__name__} {self.__txt__}>"
return f"<{self.__class__.__name__} {self.__str__()}>"
@abstractmethod
def __str__(self):
pass
@abstractmethod
def __txt__(self):
pass
@abstractmethod
def __tex__(self):
pass
def __hash__(self):
try:
return self._tree.__hash__()
@@ -161,21 +153,21 @@ class Molecule(MO):
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
def __init__(self, value):
def __init__(self, tree):
""" Initiate the MO
It should be idempotent.
"""
try:
self._tree = value._tree
self._tree = tree._tree
except AttributeError:
self._tree = value
self._tree = tree
self.is_scalar = True
self._signature = None
@@ -186,18 +178,10 @@ class Molecule(MO):
@property
def content(self):
return self._tree
return self.tree
def __str__(self):
return str(self.__txt__)
@property
def __txt__(self):
return tree2txt(self._tree)
@property
def __tex__(self):
return tree2tex(self._tree)
return tree2txt(self.tree)
# -----------------------------

View File

@@ -31,10 +31,10 @@ class MOstrPower(Molecule):
<MOstrPower x^2>
>>> print(s)
x^2
>>> print(s.__txt__)
x^2
>>> print(s.__tex__)
x^{2}
>>> print(s.tree)
^
> x
> 2
>>> MOstrPower(3, 1)
Traceback (most recent call last):
...
@@ -59,7 +59,8 @@ class MOstrPower(Molecule):
"""
_variable = MO.factory(variable)
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
_power = MO.factory(power)
@@ -102,7 +103,7 @@ class MOstrPower(Molecule):
@property
def signature(self):
""" Name of the mo in the API
:example:
>>> MOstrPower("x", 3).signature
'monome3'
@@ -123,7 +124,8 @@ class MOstrPower(Molecule):
"""
if self._power > 2:
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))
@@ -147,22 +149,20 @@ class MOMonomial(Molecule):
<MOMonomial 4x>
>>> print(m)
4x
>>> print(m.__txt__)
4x
>>> print(m.__tex__)
4x
>>> print(m.tree)
*
> 4
> x
>>> x = MOstrPower('x', 2)
>>> MOMonomial(4, x)
<MOMonomial 4x^2>
>>> m = MOMonomial(4, 'x')
>>> m = MOMonomial(-1, 'x')
>>> m
<MOMonomial 4x>
>>> print(m)
4x
>>> print(m.__txt__)
4x
>>> print(m.__tex__)
4x
<MOMonomial - x>
>>> print(m.tree)
-
> None
> x
>>> MOMonomial(4, 'x', 1)
<MOMonomial 4x>
>>> MOMonomial(4, 'x', 2)
@@ -170,6 +170,13 @@ class MOMonomial(Molecule):
>>> x2 = MOstrPower('x', 2)
>>> MOMonomial(4, x2, 3)
<MOMonomial 4x^6>
>>> m = MOMonomial(-1, 'x', 2)
>>> m
<MOMonomial - x^2>
>>> print(m.tree)
-
> None
> x^2
>>> MOMonomial(0, x)
Traceback (most recent call last):
...
@@ -199,34 +206,21 @@ class MOMonomial(Molecule):
self._power = _power
try:
if self._coefficient.value != 1:
_tree = Tree("*", self._coefficient, self.strpower)
else:
if self.coefficient.value == 1:
_tree = self.strpower
else:
_tree = Tree("*", self.coefficient, self.strpower)
except AttributeError:
_tree = Tree("*", self._coefficient, self.strpower)
_tree = Tree("*", self.coefficient, self.strpower)
Molecule.__init__(self, _tree)
def __str__(self):
if self._coefficient != -1:
return super(MOMonomial, self).__str__()
else:
return "- " + self.strpower.__str__()
@property
def __txt__(self):
if self._coefficient != -1:
return super(MOMonomial, self).__txt__
else:
return "- " + self.strpower.__txt__
def tree(self):
if self._coefficient == -1:
return Tree("-", None, self.strpower)
@property
def __tex__(self):
if self._coefficient != -1:
return super(MOMonomial, self).__tex__
else:
return "- " + self.strpower.__tex__
return Tree("*", self.coefficient, self.strpower)
@property
def coefficient(self):
@@ -265,7 +259,7 @@ class MOMonomial(Molecule):
@property
def signature(self):
""" Name of the mo in the API
:example:
>>> MOMonomial(2, "x").signature
'monome1'