Fix(Core): Tree.from_str allow - in front of parenthesis

This commit is contained in:
Bertrand Benjamin 2018-12-17 14:54:48 +01:00
parent e7015e282d
commit 2551037b65
2 changed files with 86 additions and 6 deletions

View File

@ -11,9 +11,12 @@ Minus MO: take the opposit
"""
from multipledispatch import Dispatcher
from ..MO.mo import MO, MOnumber
from ..MO.fraction import MOFraction
from .exceptions import MinusError
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction
from ..MO.monomial import MOstrPower, MOMonomial
from ..MO.polynomial import MOpolynomial
from ..tree import Tree
minus_doc = """ Opposite of a MO
@ -26,7 +29,7 @@ minus_doc = """ Opposite of a MO
minus = Dispatcher("minus", doc=minus_doc)
@minus.register(type(None), MOnumber)
def monumber(left, right):
def monumber(_, right):
"""
>>> a = MOnumber(4)
@ -37,7 +40,7 @@ def monumber(left, right):
return MO.factory(- right.value)
@minus.register(type(None), MOFraction)
def mofraction(left, right):
def mofraction(_, right):
""" 4 differents cases
Either fraction , numerator or denominator is negative
@ -91,6 +94,75 @@ def mofraction(left, right):
return MOFraction(right._numerator, right._denominator, True)
@minus.register(type(None), MOstr)
def mostr(_, right):
""" Opposite of 'x' is '-x'
:example:
>>> x = MOstr("x")
>>> print(minus(None, x))
*
> -1
> x
"""
return MOMonomial(-1, right)
@minus.register(type(None), MOstrPower)
def mostrpower(_, right):
""" Opposite of 'x^n' is '-x^n'
:example:
>>> x2 = MOstrPower("x", 2)
>>> print(minus(None, x2))
*
> -1
> ^
| > x
| > 2
"""
return MOMonomial(-1, right.variable, right.power)
@minus.register(type(None), MOMonomial)
def momonomial(_, right):
""" Opposite of 'ax^n' is '-ax^n'
:example:
>>> tx2 = MOMonomial(3, "x", 2)
>>> print(minus(None, tx2))
*
> -3
> ^
| > x
| > 2
"""
try:
return MOMonomial(-right.coefficient.value, right.variable, right.power)
except TypeError:
coef = Tree("-", None, right.coefficient)
return Tree("*", coef, right.strpower)
@minus.register(type(None), MOpolynomial)
def mopolynomial(_, right):
""" Opposite of a polynomial
:example:
>>> P = MOpolynomial('x', [1, -2, 3])
>>> print(minus(None, P))
+
> *
| > -3
| > ^
| | > x
| | > 2
> +
| > *
| | > 2
| | > x
| > -1
"""
neg_coefs = {p: -c.value for (p, c) in right.coefficients.items()}
return MOpolynomial(right.variable, neg_coefs)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:

View File

@ -20,7 +20,7 @@ from .operator import OPERATORS, is_operator
__all__ = ["Tree", "MutableTree"]
class Tree(object):
class Tree():
"""
Binary tree
@ -219,6 +219,15 @@ class Tree(object):
Traceback (most recent call last):
...
TypeError: Tree can't have empty node or leaf
>>> t = MutableTree("-", None, 1)
>>> print(t)
-
> None
> 1
>>> print(Tree.from_any_tree(t))
-
> None
> 1
>>> tl = LeafTree("/", 1, 4)
>>> t2 = MutableTree("*", tl, 3)
>>> t = Tree.from_any_tree(t2)
@ -233,7 +242,6 @@ class Tree(object):
right_value = tree.right_value
if node is None or \
left_value is None or \
right_value is None:
raise TypeError("Tree can't have empty node or leaf")