diff --git a/mapytex/calculus/core/compute/multiply.py b/mapytex/calculus/core/compute/multiply.py index d03e20b..2fc572c 100644 --- a/mapytex/calculus/core/compute/multiply.py +++ b/mapytex/calculus/core/compute/multiply.py @@ -15,7 +15,6 @@ from ..tree import Tree from ..MO.mo import MO, MOnumber, MOstr from ..MO.fraction import MOFraction from ..MO.monomial import MOstrPower, MOMonomial -from .exceptions import MultiplyError from .filters import special_case multiply_doc = """ Multiply MOs @@ -201,10 +200,10 @@ def mostr_mostrpower(left, right): >>> 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) + NotImplementedError: 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" + raise NotImplementedError("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.value+1) @@ -222,13 +221,35 @@ def mostr_mostrpower(left, right): >>> 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) + NotImplementedError: 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" + raise NotImplementedError("Can't multiply MOstr and MOstrPower if they don't" f"have same variable (got {left.variable} and {right.variable})") 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) + + >>> 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) @special_case(multiply_filter) def mostr_mostrpower(left, right): @@ -247,14 +268,157 @@ def mostr_mostrpower(left, right): >>> 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) + NotImplementedError: Can't multiply MOstrPower 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" + raise NotImplementedError("Can't multiply MOstrPower and MOstrPower if they don't" f"have same variable (got {left.variable} and {right.variable})") power = Tree("+", left.power, right.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' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: