Feat(Core): Compute with MOpolynomial with all but MOpolynomial

This commit is contained in:
Bertrand Benjamin 2018-11-23 11:53:37 +01:00
parent c8be5c1414
commit 55c0830671

View File

@ -15,6 +15,7 @@ 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 ..MO.polynomial import MOpolynomial
from .filters import special_case from .filters import special_case
multiply_doc = """ Multiply MOs multiply_doc = """ Multiply MOs
@ -419,6 +420,141 @@ def momonomial_momonomial(left, right):
coefs = Tree("*", left.coefficient, right.coefficient) coefs = Tree("*", left.coefficient, right.coefficient)
return Tree("*", coefs, monome) return Tree("*", coefs, monome)
@multiply.register((MOnumber, MOFraction, MOstr, MOstrPower, MOMonomial), \
MOpolynomial)
@special_case(multiply_filter)
def moscalar_mopolynomial(left, right):
""" Multiply a scalar and a MOMonomial
>>> a = MOnumber(2)
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
+
> *
| > 2
| > 1
> +
| > *
| | > 2
| | > *
| | | > 2
| | | > x
| > *
| | > 2
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
>>> a = MOFraction(1, 5)
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
+
> *
| > /
| | > 1
| | > 5
| > 1
> +
| > *
| | > /
| | | > 1
| | | > 5
| | > *
| | | > 2
| | | > x
| > *
| | > /
| | | > 1
| | | > 5
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
>>> a = MOstr("x")
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
+
> *
| > x
| > 1
> +
| > *
| | > x
| | > *
| | | > 2
| | | > x
| > *
| | > x
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
>>> a = MOstrPower("x", 2)
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
+
> *
| > ^
| | > x
| | > 2
| > 1
> +
| > *
| | > ^
| | | > x
| | | > 2
| | > *
| | | > 2
| | | > x
| > *
| | > ^
| | | > x
| | | > 2
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
>>> a = MOMonomial(3, "x", 2)
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > 1
> +
| > *
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > *
| | | > 2
| | | > x
| > *
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
"""
coefs = [Tree("*", left, monom) for monom in right.monomials.values()]
return Tree.from_list("+", coefs)
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4: