Fix(Core): apply_on_last_level handle NotImplementedError
This commit is contained in:
parent
f53aa26542
commit
505d2f1c09
@ -83,16 +83,37 @@ x^7
|
|||||||
|
|
||||||
>>> e = Expression.from_str("2x^2+2x+3x")
|
>>> e = Expression.from_str("2x^2+2x+3x")
|
||||||
>>> e_simplified = e.simplify()
|
>>> e_simplified = e.simplify()
|
||||||
|
>>> print(e_simplified)
|
||||||
|
2x^2 + 5x
|
||||||
|
>>> for s in e_simplified.explain():
|
||||||
|
... print(s)
|
||||||
|
2x^2 + 2x + 3x
|
||||||
|
2x^2 + (2 + 3) * x
|
||||||
|
2x^2 + 5x
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .expression import Expression
|
from .expression import Expression
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
e = Expression.from_str("2x^2+3x+4x^2")
|
e = Expression.from_str("2x^2+2x+3x")
|
||||||
print(e)
|
print(e._tree.map_on_leaf(lambda x:(x,)))
|
||||||
e_simplified = e.simplify()
|
for _ in range(5):
|
||||||
print(e_simplified)
|
print("-"*10)
|
||||||
|
e = e._optimize()
|
||||||
|
try:
|
||||||
|
print(e._tree.map_on_leaf(lambda x:(x,)))
|
||||||
|
except AttributeError:
|
||||||
|
print(e)
|
||||||
|
e = e._typing()
|
||||||
|
try:
|
||||||
|
print(e._tree.map_on_leaf(lambda x:(x,)))
|
||||||
|
except AttributeError:
|
||||||
|
print(e)
|
||||||
|
e = e._compute()
|
||||||
|
try:
|
||||||
|
print(e._tree.map_on_leaf(lambda x:(x,)))
|
||||||
|
except AttributeError:
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
@ -91,6 +91,37 @@ class Expression(object):
|
|||||||
def _optimize(self, exclude_nodes=["\\", "**"]):
|
def _optimize(self, exclude_nodes=["\\", "**"]):
|
||||||
""" Return a copy of self with an optimize tree
|
""" Return a copy of self with an optimize tree
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> e = Expression.from_str("2x^2+2x+3x")
|
||||||
|
>>> print(e._tree)
|
||||||
|
+
|
||||||
|
> +
|
||||||
|
| > *
|
||||||
|
| | > 2
|
||||||
|
| | > ^
|
||||||
|
| | | > x
|
||||||
|
| | | > 2
|
||||||
|
| > *
|
||||||
|
| | > 2
|
||||||
|
| | > x
|
||||||
|
> *
|
||||||
|
| > 3
|
||||||
|
| > x
|
||||||
|
>>> print(e._optimize()._tree)
|
||||||
|
+
|
||||||
|
> *
|
||||||
|
| > 2
|
||||||
|
| > ^
|
||||||
|
| | > x
|
||||||
|
| | > 2
|
||||||
|
> +
|
||||||
|
| > *
|
||||||
|
| | > 2
|
||||||
|
| | > x
|
||||||
|
| > *
|
||||||
|
| | > 3
|
||||||
|
| | > x
|
||||||
|
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# TODO: need to test exclude_nodes |ven. oct. 5 08:51:02 CEST 2018
|
# TODO: need to test exclude_nodes |ven. oct. 5 08:51:02 CEST 2018
|
||||||
@ -180,7 +211,8 @@ class Expression(object):
|
|||||||
comp_exp = typed_exp._compute()
|
comp_exp = typed_exp._compute()
|
||||||
|
|
||||||
if typed_exp == comp_exp:
|
if typed_exp == comp_exp:
|
||||||
return self
|
typed_exp.set_ancestor(self._ancestor)
|
||||||
|
return typed_exp
|
||||||
else:
|
else:
|
||||||
comp_exp.set_ancestor(self)
|
comp_exp.set_ancestor(self)
|
||||||
return comp_exp.simplify(optimize=optimize)
|
return comp_exp.simplify(optimize=optimize)
|
||||||
|
@ -35,9 +35,11 @@ Abstracts tools for calculs manipulations
|
|||||||
| > 3
|
| > 3
|
||||||
| > 4
|
| > 4
|
||||||
>>> print(t.apply_on_last_level(compute))
|
>>> print(t.apply_on_last_level(compute))
|
||||||
Traceback (most recent call last):
|
+
|
||||||
...
|
> 2
|
||||||
NotImplementedError: Can't divide 2 int. Need to create a Fraction instead
|
> /
|
||||||
|
| > 3
|
||||||
|
| > 4
|
||||||
>>> tt = t.apply_on_last_level(typing)
|
>>> tt = t.apply_on_last_level(typing)
|
||||||
>>> print(tt.apply_on_last_level(compute))
|
>>> print(tt.apply_on_last_level(compute))
|
||||||
+
|
+
|
||||||
|
@ -330,6 +330,8 @@ class Tree(object):
|
|||||||
try:
|
try:
|
||||||
left_applied = self.left_value.\
|
left_applied = self.left_value.\
|
||||||
apply_on_last_level(function)
|
apply_on_last_level(function)
|
||||||
|
except NotImplementedError:
|
||||||
|
left_applied = self.left_value
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
left_applied = self.left_value
|
left_applied = self.left_value
|
||||||
left_is_leaf = 1
|
left_is_leaf = 1
|
||||||
@ -338,6 +340,8 @@ class Tree(object):
|
|||||||
try:
|
try:
|
||||||
right_applied = self.right_value.\
|
right_applied = self.right_value.\
|
||||||
apply_on_last_level(function)
|
apply_on_last_level(function)
|
||||||
|
except NotImplementedError:
|
||||||
|
right_applied = self.right_value
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
right_applied = self.right_value
|
right_applied = self.right_value
|
||||||
right_is_leaf = 1
|
right_is_leaf = 1
|
||||||
|
Loading…
Reference in New Issue
Block a user