Fix(Core): Add mopoly * other and reorder monoms after *

This commit is contained in:
Bertrand Benjamin 2018-11-23 12:15:12 +01:00
parent 9e4edc09e3
commit 5cbefa3165
1 changed files with 197 additions and 47 deletions

View File

@ -432,7 +432,11 @@ def lotsmo_mopolynomial(left, right):
+
> *
| > 2
| > 1
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
> +
| > *
| | > 2
@ -441,11 +445,8 @@ def lotsmo_mopolynomial(left, right):
| | | > x
| > *
| | > 2
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > 1
>>> a = MOFraction(1, 5)
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
@ -454,7 +455,11 @@ def lotsmo_mopolynomial(left, right):
| > /
| | > 1
| | > 5
| > 1
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
> +
| > *
| | > /
@ -467,18 +472,19 @@ def lotsmo_mopolynomial(left, right):
| | > /
| | | > 1
| | | > 5
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > 1
>>> a = MOstr("x")
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
+
> *
| > x
| > 1
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
> +
| > *
| | > x
@ -487,11 +493,8 @@ def lotsmo_mopolynomial(left, right):
| | | > x
| > *
| | > x
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > 1
>>> a = MOstrPower("x", 2)
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
@ -500,7 +503,11 @@ def lotsmo_mopolynomial(left, right):
| > ^
| | > x
| | > 2
| > 1
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
> +
| > *
| | > ^
@ -513,11 +520,8 @@ def lotsmo_mopolynomial(left, right):
| | > ^
| | | > x
| | | > 2
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| | > 1
>>> a = MOMonomial(3, "x", 2)
>>> b = MOpolynomial('x', [1, 2, 3])
>>> print(multiply(a, b))
@ -528,7 +532,11 @@ def lotsmo_mopolynomial(left, right):
| | > ^
| | | > x
| | | > 2
| > 1
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
> +
| > *
| | > *
@ -545,13 +553,154 @@ def lotsmo_mopolynomial(left, right):
| | | > ^
| | | | > x
| | | | > 2
| | > 1
"""
coefs = [Tree("*", left, monom) \
for monom in list(right.monomials.values())[::-1]\
]
return Tree.from_list("+", coefs)
@multiply.register(MOpolynomial, \
(MOnumber, MOFraction, MOstr, MOstrPower, MOMonomial))
@special_case(multiply_filter)
def mopolynomial_lotsmo(left, right):
""" Multiply a MOpolynomial with nearly everything
>>> a = MOpolynomial('x', [1, 2, 3])
>>> b = MOnumber(2)
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > 2
> +
| > *
| | > *
| | | > 2
| | | > x
| | > 2
| > *
| | > 1
| | > 2
>>> a = MOpolynomial('x', [1, 2, 3])
>>> b = MOFraction(1, 5)
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > /
| | > 1
| | > 5
> +
| > *
| | > *
| | | > 2
| | | > x
| | > /
| | | > 1
| | | > 5
| > *
| | > 1
| | > /
| | | > 1
| | | > 5
>>> a = MOpolynomial('x', [1, 2, 3])
>>> b = MOstr("x")
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > x
> +
| > *
| | > *
| | | > 2
| | | > x
| | > x
| > *
| | > 1
| | > x
>>> a = MOpolynomial('x', [1, 2, 3])
>>> b = MOstrPower("x", 2)
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > ^
| | > x
| | > 2
> +
| > *
| | > *
| | | > 2
| | | > x
| | > ^
| | | > x
| | | > 2
| > *
| | > 1
| | > ^
| | | > x
| | | > 2
>>> a = MOpolynomial('x', [1, 2, 3])
>>> b = MOMonomial(3, "x", 2)
>>> print(multiply(a, b))
+
> *
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
| > *
| | > 3
| | > ^
| | | > x
| | | > 2
> +
| > *
| | > *
| | | > 2
| | | > x
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
| > *
| | > 1
| | > *
| | | > 3
| | | > ^
| | | | > x
| | | | > 2
"""
coefs = [Tree("*", left, monom) for monom in right.monomials.values()]
coefs = [Tree("*", monom, right) \
for monom in list(left.monomials.values())[::-1] \
]
return Tree.from_list("+", coefs)
@multiply.register(MOpolynomial, MOpolynomial)
@ -565,24 +714,11 @@ def mopolynomial_mopolynomial(left, right):
+
> +
| > *
| | > 1
| | > 4
| > +
| | > *
| | | > 1
| | | > *
| | | | > 5
| | | > 3
| | | > ^
| | | | > x
| | > *
| | | > *
| | | | > 2
| | | | > x
| | | > 4
> +
| > *
| | > *
| | | > 2
| | | > x
| | > *
| | | > 5
| | | > x
@ -596,18 +732,32 @@ def mopolynomial_mopolynomial(left, right):
| | | > 4
| | > *
| | | > *
| | | | > 3
| | | | > ^
| | | | | > x
| | | | | > 2
| | | | > 2
| | | | > x
| | | > *
| | | | > 5
| | | | > x
> +
| > *
| | > *
| | | > 2
| | | > x
| | > 4
| > +
| | > *
| | | > 1
| | | > *
| | | | > 5
| | | | > x
| | > *
| | | > 1
| | | > 4
"""
coefs = [Tree("*", l_monom, r_monom) \
for l_monom in left.monomials.values() \
for r_monom in right.monomials.values()]
for l_monom in list(left.monomials.values())[::-1] \
for r_monom in list(right.monomials.values())[::-1] \
]
return Tree.from_list("+", coefs)