Fix(API): Rewrite tree apply_on_last_level and reorganise simplify
This commit is contained in:
@@ -21,6 +21,7 @@ Generate and compute like a student!
|
||||
2 + 12
|
||||
14
|
||||
|
||||
|
||||
>>> e = Expression.from_str("2+3/2")
|
||||
>>> e_simplified = e.simplify()
|
||||
>>> print(e_simplified)
|
||||
@@ -34,6 +35,20 @@ Generate and compute like a student!
|
||||
(4 + 3) / 2
|
||||
7 / 2
|
||||
|
||||
>>> e = Expression.from_str("(2+3)/2 + 1")
|
||||
>>> e_simplified = e.simplify()
|
||||
>>> print(e_simplified)
|
||||
7 / 2
|
||||
>>> for s in e_simplified.explain():
|
||||
... print(s)
|
||||
(2 + 3) / 2 + 1
|
||||
5 / 2 + 1
|
||||
5 / 2 + 1 / 1
|
||||
5 / 2 + (1 * 2) / (1 * 2)
|
||||
5 / 2 + 2 / 2
|
||||
(5 + 2) / 2
|
||||
7 / 2
|
||||
|
||||
>>> e = Expression.from_str("(2/3)^4")
|
||||
>>> e_simplified = e.simplify()
|
||||
>>> print(e_simplified)
|
||||
@@ -54,10 +69,28 @@ x^2 * x * x^4
|
||||
x^3 * x^4
|
||||
x^(3 + 4)
|
||||
x^7
|
||||
|
||||
>>> e = Expression.from_str("2x+2+3x")
|
||||
>>> e_simplified = e.simplify()
|
||||
>>> print(e_simplified)
|
||||
5x + 2
|
||||
>>> for s in e_simplified.explain():
|
||||
... print(s)
|
||||
2x + 2 + 3x
|
||||
2x + 3x + 2
|
||||
(2 + 3) * x + 2
|
||||
5x + 2
|
||||
"""
|
||||
|
||||
from .expression import Expression
|
||||
|
||||
if __name__ == "__main__":
|
||||
e = Expression.from_str("2x+2+3x")
|
||||
print(e)
|
||||
e_simplified = e.simplify()
|
||||
print(e_simplified)
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
|
@@ -10,7 +10,7 @@
|
||||
Expression
|
||||
|
||||
"""
|
||||
from ..core import AssocialTree, Tree, compute, typing
|
||||
from ..core import AssocialTree, Tree, compute, typing, TypingError
|
||||
from .renders import renders
|
||||
|
||||
class Expression(object):
|
||||
@@ -104,20 +104,31 @@ class Expression(object):
|
||||
>>> f._ancestor
|
||||
<Exp: 2 + 12>
|
||||
"""
|
||||
t = self._tree
|
||||
if optimize:
|
||||
try:
|
||||
# TODO: need to test exclude_nodes |ven. oct. 5 08:51:02 CEST 2018
|
||||
self._tree = self._tree.balance(
|
||||
t = t.balance(
|
||||
exclude_nodes=["\\", "**"]
|
||||
)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
more_typing = 1
|
||||
while more_typing:
|
||||
try:
|
||||
t = t.apply_on_last_level(typing)
|
||||
except TypingError:
|
||||
more_typing = 0
|
||||
except NotImplementedError:
|
||||
more_typing = 0
|
||||
except AttributeError:
|
||||
more_typing = 0
|
||||
|
||||
try:
|
||||
t = self._tree.apply_on_last_level(compute, typing)
|
||||
t = t.apply_on_last_level(compute)
|
||||
except AttributeError:
|
||||
return self
|
||||
except NotImplementedError:
|
||||
return self
|
||||
else:
|
||||
e = Expression(t, ancestor=self)
|
||||
return e.simplify(optimize=optimize)
|
||||
|
Reference in New Issue
Block a user