Feat(Core): Multiply for monome is done

This commit is contained in:
Bertrand Benjamin 2018-11-23 11:24:33 +01:00
parent 95634d366b
commit 2176cc65df

View File

@ -15,7 +15,6 @@ from ..tree import Tree
from ..MO.mo import MO, MOnumber, MOstr from ..MO.mo import MO, MOnumber, MOstr
from ..MO.fraction import MOFraction from ..MO.fraction import MOFraction
from ..MO.monomial import MOstrPower, MOMonomial from ..MO.monomial import MOstrPower, MOMonomial
from .exceptions import MultiplyError
from .filters import special_case from .filters import special_case
multiply_doc = """ Multiply MOs multiply_doc = """ Multiply MOs
@ -201,10 +200,10 @@ def mostr_mostrpower(left, right):
>>> multiply(a, b) >>> multiply(a, b)
Traceback (most recent call last): 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) NotImplementedError: Can't multiply MOstr and MOstrPower if they don'thave same variable (got x and y)
""" """
if left.variable != right.variable: if left.variable != right.variable:
raise MultiplyError("Can't multiply MOstr and MOstrPower if they don't" raise NotImplementedError("Can't multiply MOstr and MOstrPower if they don't"
f"have same variable (got {left.variable} and {right.variable})") f"have same variable (got {left.variable} and {right.variable})")
return MOstrPower(left.variable, right.power.value+1) return MOstrPower(left.variable, right.power.value+1)
@ -222,13 +221,35 @@ def mostr_mostrpower(left, right):
>>> multiply(a, b) >>> multiply(a, b)
Traceback (most recent call last): 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) NotImplementedError: Can't multiply MOstr and MOstrPower if they don'thave same variable (got x and y)
""" """
if left.variable != right.variable: if left.variable != right.variable:
raise MultiplyError("Can't multiply MOstr and MOstrPower if they don't" raise NotImplementedError("Can't multiply MOstr and MOstrPower if they don't"
f"have same variable (got {left.variable} and {right.variable})") f"have same variable (got {left.variable} and {right.variable})")
return MOstrPower(left.variable, left.power.value+1) return MOstrPower(left.variable, left.power.value+1)
@multiply.register(MOstr, MOstr)
@special_case(multiply_filter)
def mostr_mostr(left, right):
""" Multiply a MOstr and a MOstr
:example:
>>> a = MOstr('x')
>>> b = MOstr('x')
>>> multiply(a, b)
<MOstrPower x^2>
>>> a = MOstr('y')
>>> b = MOstr('x')
>>> multiply(a, b)
Traceback (most recent call last):
...
NotImplementedError: Can't multiply MOstr and MOstr if they don'thave same variable (got y and x)
"""
if left.variable != right.variable:
raise NotImplementedError("Can't multiply MOstr and MOstr if they don't"
f"have same variable (got {left.variable} and {right.variable})")
return MOstrPower(left.variable, 2)
@multiply.register(MOstrPower, MOstrPower) @multiply.register(MOstrPower, MOstrPower)
@special_case(multiply_filter) @special_case(multiply_filter)
def mostr_mostrpower(left, right): def mostr_mostrpower(left, right):
@ -247,14 +268,157 @@ def mostr_mostrpower(left, right):
>>> multiply(a, b) >>> multiply(a, b)
Traceback (most recent call last): 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) NotImplementedError: Can't multiply MOstrPower and MOstrPower if they don'thave same variable (got x and y)
""" """
if left.variable != right.variable: if left.variable != right.variable:
raise MultiplyError("Can't multiply MOstr and MOstrPower if they don't" raise NotImplementedError("Can't multiply MOstrPower and MOstrPower if they don't"
f"have same variable (got {left.variable} and {right.variable})") f"have same variable (got {left.variable} and {right.variable})")
power = Tree("+", left.power, right.power) power = Tree("+", left.power, right.power)
return Tree("^", left.variable, power) return Tree("^", left.variable, power)
@multiply.register(MOstrPower, MOMonomial)
@special_case(multiply_filter)
def mostr_momonomial(left, right):
""" Multiply a MOstrPower and a MOMonomial
>>> a = MOstrPower('x', 2)
>>> b = MOMonomial(2, 'x', 4)
>>> print(multiply(a, b))
*
> 2
> ^
| > x
| > +
| | > 2
| | > 4
>>> a = MOstrPower('x', 2)
>>> b = MOMonomial(2, 'y', 4)
>>> multiply(a, b)
Traceback (most recent call last):
...
NotImplementedError: Can't multiply MOstrPower and Monomial if they don'thave same variable (got x and y)
"""
if left.variable != right.variable:
raise NotImplementedError("Can't multiply MOstrPower and Monomial if they don't"
f"have same variable (got {left.variable} and {right.variable})")
power = Tree("+", left.power, right.power)
monome = Tree("^", left.variable, power)
return Tree("*", right.coefficient, monome)
@multiply.register(MOMonomial, MOstrPower)
@special_case(multiply_filter)
def momonomial_mostr(left, right):
""" Multiply a MOMonomial and a MOstrPower
>>> a = MOMonomial(2, 'x', 4)
>>> b = MOstrPower('x', 2)
>>> print(multiply(a, b))
*
> 2
> ^
| > x
| > +
| | > 4
| | > 2
>>> a = MOMonomial(2, 'y', 4)
>>> b = MOstrPower('x', 2)
>>> multiply(a, b)
Traceback (most recent call last):
...
NotImplementedError: Can't multiply MOstrPower and Monomial if they don'thave same variable (got y and x)
"""
if left.variable != right.variable:
raise NotImplementedError("Can't multiply MOstrPower and Monomial if they don't"
f"have same variable (got {left.variable} and {right.variable})")
power = Tree("+", left.power, right.power)
monome = Tree("^", left.variable, power)
return Tree("*", left.coefficient, monome)
@multiply.register(MOstr, MOMonomial)
@special_case(multiply_filter)
def mostr_momonomial(left, right):
""" Multiply a MOstr and a MOMonomial
>>> a = MOstr('x')
>>> b = MOMonomial(2, 'x', 4)
>>> print(multiply(a, b))
*
> 2
> ^
| > x
| > 5
>>> a = MOstr('x')
>>> b = MOMonomial(2, 'y', 4)
>>> multiply(a, b)
Traceback (most recent call last):
...
NotImplementedError: Can't multiply MOstr and Monomial if they don'thave same variable (got x and y)
"""
if left.variable != right.variable:
raise NotImplementedError("Can't multiply MOstr and Monomial if they don't"
f"have same variable (got {left.variable} and {right.variable})")
return MOMonomial(right.coefficient, right.variable, right.power.value+1)
@multiply.register(MOMonomial, MOstr)
@special_case(multiply_filter)
def momonomial_mostr(left, right):
""" Multiply a MOMonomial and a MOstr
>>> a = MOMonomial(2, 'x', 4)
>>> b = MOstr('x')
>>> print(multiply(a, b))
*
> 2
> ^
| > x
| > 5
>>> a = MOMonomial(2, 'y', 4)
>>> b = MOstr('x')
>>> multiply(a, b)
Traceback (most recent call last):
...
NotImplementedError: Can't multiply MOstr and Monomial if they don'thave same variable (got y and x)
"""
if left.variable != right.variable:
raise NotImplementedError("Can't multiply MOstr and Monomial if they don't"
f"have same variable (got {left.variable} and {right.variable})")
return MOMonomial(left.coefficient, left.variable, left.power.value+1)
@multiply.register(MOMonomial, MOMonomial)
@special_case(multiply_filter)
def momonomial_momonomial(left, right):
""" Multiply a MOMonomial and a MOMonomial
>>> a = MOMonomial(2, 'x', 4)
>>> b = MOMonomial(3, 'x', 2)
>>> print(multiply(a, b))
*
> *
| > 2
| > 3
> ^
| > x
| > +
| | > 4
| | > 2
>>> a = MOMonomial(2, 'y', 4)
>>> b = MOMonomial(3, 'x', 2)
>>> multiply(a, b)
Traceback (most recent call last):
...
NotImplementedError: Can't multiply MOMonomial and Monomial if they don'thave same variable (got y and x)
"""
if left.variable != right.variable:
raise NotImplementedError("Can't multiply MOMonomial and Monomial if they don't"
f"have same variable (got {left.variable} and {right.variable})")
powers = Tree("+", left.power, right.power)
monome = Tree("^", left.variable, powers)
coefs = Tree("*", left.coefficient, right.coefficient)
return Tree("*", coefs, monome)
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4: