Create MOstrPower and start adding it into multiply

This commit is contained in:
2018-03-18 10:34:46 +03:00
parent 3b59e0ffb0
commit f8e00a8638
2 changed files with 182 additions and 82 deletions

View File

@@ -14,7 +14,7 @@ from multipledispatch import Dispatcher
from ..tree import Tree
from ..MO.mo import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction
from ..MO.monomial import MOMonomial
from ..MO.monomial import MOstrPower, MOMonomial
from .exceptions import MultiplyError
from .filters import special_case
@@ -175,18 +175,102 @@ def mostr_moscalar(left, right):
"""
return MOMonomial(right, left)
@multiply.register((MOnumber, MOFraction), MOstrPower)
@special_case(multiply_filter)
def moscalar_mostrpower(left, right):
""" Multiply a scalar with a MOstrPower
>>> a = MOnumber(4)
>>> x = MOstrPower('x', 4)
>>> print(multiply(a, x))
*
> 4
> ^
| > x
| > 4
"""
return MOMonomial(left, right)
@multiply.register(MOstrPower, (MOnumber, MOFraction))
@special_case(multiply_filter)
def mostrpower_moscalar(left, right):
""" Multiply a MOstrPower with a scalar
>>> a = MOnumber(4)
>>> x = MOstrPower('x', 4)
>>> print(multiply(x, a))
*
> 4
> ^
| > x
| > 4
"""
return MOMonomial(right, left)
@multiply.register((MOnumber, MOFraction), MOMonomial)
@special_case(multiply_filter)
def moscalar_monomonial(left, right):
""" Multiply a scalar with a monomial
>>> a = MOnumber(4)
>>> b = MOMonomial(5, 'x', 3)
>>> x = MOstrPower('x', 4)
>>> b = MOMonomial(5, x)
>>> print(multiply(a, b))
*
> *
| > 4
| > 5
> ^
| > x
| > 4
# >>> print(multiply(a, b))
"""
coefficient = Tree('*', left, right._coefficient)
return Tree('*', coefficient, MOMonomial(1, right._variable, right._power))
coefficient = Tree('*', left, right.coefficient)
return Tree('*', coefficient, right.strpower)
@multiply.register(MOMonomial, (MOnumber, MOFraction))
@special_case(multiply_filter)
def monomonial_moscalar(left, right):
""" Multiply a momonial with a scalar
>>> x = MOstrPower('x', 4)
>>> a = MOMonomial(5, x)
>>> b = MOnumber(4)
>>> print(multiply(a, b))
*
> *
| > 4
| > 5
> ^
| > x
| > 4
"""
coefficient = Tree('*', right, left.coefficient)
return Tree('*', coefficient, left.strpower)
@multiply.register(MOstr, MOstrPower)
@special_case(multiply_filter)
def mostr_mostrpower(left, right):
""" Multiply a MOstr and a MOstrPower
>>> a = MOstr('x')
>>> b = MOstrPower('x', 4)
>>> multiply(a, b)
<MOstrPower x^5>
>>> a = MOstr('x')
>>> b = MOstrPower('y', 4)
>>> multiply(a, b)
Traceback (most recent call last):
...
mapytex.calculus.core.compute.exceptions.MultiplyError: Can't multiply MOstr and MOstrPower if they don'thave same variable (got x and y)
"""
if left.variable != right.variable:
raise MultiplyError("Can't multiply MOstr and MOstrPower if they don't"
f"have same variable (got {left.variable} and {right.variable})")
return MOstrPower(left.variable, right.power+1)
# -----------------------------
# Reglages pour 'vim'