Feat(Compute): Compute MOstr and MOstrPower

This commit is contained in:
Bertrand Benjamin 2018-11-13 12:12:36 +01:00
parent 051d79cb9b
commit a32fd7cbc6
2 changed files with 35 additions and 2 deletions

View File

@ -67,7 +67,7 @@ def compute_capacities(node):
:example:
>>> compute_capacities("+")
[['+', 'MOnumber', 'MOstr', 'MOFraction', 'MOstrPower', 'MOMonomial'], ['MOnumber', True, False, True, False, False], ['MOstr', False, False, False, False, False], ['MOFraction', True, False, True, False, False], ['MOstrPower', False, False, False, False, False], ['MOMonomial', False, False, False, False, False]]
[['+', 'MOnumber', 'MOstr', 'MOFraction', 'MOstrPower', 'MOMonomial'], ['MOnumber', True, False, True, False, False], ['MOstr', False, True, False, False, False], ['MOFraction', True, False, True, False, False], ['MOstrPower', False, False, False, True, False], ['MOMonomial', False, False, False, False, False]]
"""
op = OPERATIONS[node]

View File

@ -13,8 +13,9 @@ Adding MO
from functools import wraps
from multipledispatch import Dispatcher
from ..tree import Tree
from ..MO.mo import MO, MOnumber
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction
from ..MO.monomial import MOstrPower, MOMonomial
from .exceptions import AddError
from .arithmetic import lcm
from .filters import special_case
@ -203,6 +204,38 @@ def mofraction_mofraction(left, right):
return Tree("+", left_frac, right_frac)
@add.register(MOstr, MOstr)
@special_case(add_filter)
def mostr_mostr(left, right):
""" Add 2 MOstr
:example:
>>> a = MOstr("x")
>>> b = MOstr("x")
>>> add(a, b)
<MOMonomial 2x>
"""
if left != right:
raise NotImplementedError("Can't add 2 MOstr with not same letter")
return MOMonomial(2, left)
@add.register(MOstrPower, MOstrPower)
@special_case(add_filter)
def mostrpower_mostrpower(left, right):
""" Add 2 MOstrPower
:example:
>>> a = MOstrPower("x", 2)
>>> b = MOstrPower("x", 2)
>>> add(a, b)
<MOMonomial 2x^2>
"""
if left.variable != right.variable:
raise NotImplementedError("Can't add 2 MOstrPower with not same letter")
if left.power != right.power:
raise NotImplementedError("Can't add 2 MOstrPower with not same power")
return MOMonomial(2, left.variable, left.power)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: