diff --git a/mapytex/calculus/core/compute/multiply.py b/mapytex/calculus/core/compute/multiply.py index cb1773f..b5d02b1 100644 --- a/mapytex/calculus/core/compute/multiply.py +++ b/mapytex/calculus/core/compute/multiply.py @@ -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)