From c176bb7bc4e251c942c1e036505451b3a80debc2 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 14 Nov 2018 10:04:27 +0100 Subject: [PATCH] Feat(Typing): Write all str related typing functions --- mapytex/calculus/core/compute/add.py | 2 +- mapytex/calculus/core/typing/add.py | 208 ++++++++++++++++++++++++++- 2 files changed, 203 insertions(+), 7 deletions(-) diff --git a/mapytex/calculus/core/compute/add.py b/mapytex/calculus/core/compute/add.py index bf2cdde..5e3c9a3 100644 --- a/mapytex/calculus/core/compute/add.py +++ b/mapytex/calculus/core/compute/add.py @@ -709,7 +709,7 @@ def mopolynomial_mopolynomial(left, right): """ common_degree = set(left.monomials.keys()).intersection(right.monomials.keys()) if not common_degree: - raise NotImplementedError("Polynomial with no constant, no calculus to do") + raise NotImplementedError("No degree in common, no calculus to do") merge_monomials = {**left.monomials, **right.monomials} for deg in common_degree: diff --git a/mapytex/calculus/core/typing/add.py b/mapytex/calculus/core/typing/add.py index d9d672a..43334c8 100644 --- a/mapytex/calculus/core/typing/add.py +++ b/mapytex/calculus/core/typing/add.py @@ -84,23 +84,23 @@ def moscalar_mostr(left, right): return MOpolynomial(left.variable, {0: right, left.power: 1}) @add.register((MOnumber, MOFraction), MOMonomial) -def moscalar_mostr(left, right): +def moscalar_momonomial(left, right): """ add a scalar with a MOMonomial to create a MOpolynomial >>> a = MOnumber(2) >>> b = MOMonomial(3, 'x', 4) >>> add(a, b) - + >>> a = MOFraction(1, 5) >>> add(a, b) - + """ return MOpolynomial(right.variable, - {0: left, right.power: right.coefficient} + {right.power: right.coefficient, 0: left} ) @add.register(MOMonomial, (MOnumber, MOFraction)) -def moscalar_mostr(left, right): +def momonial_moscalar(left, right): """ add a scalar with a letter to create a MOpolynomial >>> a = MOMonomial(3, 'x', 4) @@ -113,9 +113,43 @@ def moscalar_mostr(left, right): """ return MOpolynomial(left.variable, - {0: right, left.power: left.coeffient} + {0: right, left.power: left.coefficient} ) +@add.register((MOnumber, MOFraction), MOpolynomial) +def moscalar_mopolynomial(left, right): + """ add a scalar with a MOpolynomial to create a MOpolynomial + + >>> a = MOnumber(2) + >>> b = MOpolynomial('x', [0, 2, 3]) + >>> add(a, b) + + >>> a = MOFraction(1, 5) + >>> add(a, b) + + """ + new_coefs = right.coefficients + #! Need to be add at the end to be printed at the beginning + new_coefs[0] = left + return MOpolynomial(right.variable, new_coefs) + +@add.register(MOpolynomial, (MOnumber, MOFraction)) +def mopolynomial_moscalar(left, right): + """ add a scalar with a MOpolynomial to create a MOpolynomial + + >>> a = MOpolynomial('x', [0, 2, 3]) + >>> b = MOnumber(2) + >>> add(a, b) + + >>> b = MOFraction(1, 5) + >>> add(a, b) + + """ + #! Need to be add at the beginning to be printed at the end + new_coefs = {0: right} + new_coefs = {**new_coefs, **left.coefficients} + return MOpolynomial(left.variable, new_coefs) + @add.register(MOstr, MOstrPower) def moscalar_mostr(left, right): """ add a scalar with a letter to create a MOpolynomial @@ -147,6 +181,168 @@ def moscalar_mostr(left, right): if right != left.variable: raise return MOpolynomial(right , {1: 1, left.power: 1}) + +@add.register(MOstr, MOpolynomial) +def mostr_mopolynomial(left, right): + """ add a str with a MOpolynomial to create a MOpolynomial + + >>> a = MOstr("x") + >>> b = MOpolynomial('x', [1, 0, 3]) + >>> add(a, b) + + """ + new_coefs = right.coefficients + #! Need to be add at the end to be printed at the beginning + new_coefs[1] = 1 + return MOpolynomial(right.variable, new_coefs) + +@add.register(MOpolynomial, MOstr) +def mopolynomial_mostr(left, right): + """ add a str with a MOpolynomial to create a MOpolynomial + + >>> a = MOpolynomial('x', [1, 0, 3]) + >>> b = MOstr("x") + >>> add(a, b) + + """ + new_coefs = {1: 1} + new_coefs = {**new_coefs, **left.coefficients} + return MOpolynomial(left.variable, new_coefs) + +@add.register(MOstrPower, MOpolynomial) +def mostrpower_mopolynomial(left, right): + """ add a strPower with a MOpolynomial to create a MOpolynomial + + >>> a = MOstrPower("x", 2) + >>> b = MOpolynomial('x', [1, 2, 0, 4]) + >>> add(a, b) + + """ + new_coefs = right.coefficients + #! Need to be add at the end to be printed at the beginning + new_coefs[left.power] = 1 + return MOpolynomial(right.variable, new_coefs) + +@add.register(MOpolynomial, MOstrPower) +def mopolynomial_mostrpower(left, right): + """ add a strPower with a MOpolynomial to create a MOpolynomial + + >>> a = MOpolynomial('x', [1, 2, 0, 4]) + >>> b = MOstrPower("x", 2) + >>> add(a, b) + + """ + new_coefs = {right.power: 1} + new_coefs = {**new_coefs, **left.coefficients} + return MOpolynomial(left.variable, new_coefs) + +@add.register(MOMonomial, MOpolynomial) +def momonomial_mopolynomial(left, right): + """ add a Monomial with a MOpolynomial to create a MOpolynomial + + >>> a = MOMonomial(3, "x", 2) + >>> b = MOpolynomial('x', [1, 2, 0, 4]) + >>> add(a, b) + + """ + new_coefs = right.coefficients + #! Need to be add at the end to be printed at the beginning + new_coefs[left.power] = left.coefficient + return MOpolynomial(right.variable, new_coefs) + +@add.register(MOpolynomial, MOMonomial) +def mopolynomial_momonomial(left, right): + """ add a Monomial with a MOpolynomial to create a MOpolynomial + + >>> a = MOpolynomial('x', [1, 2, 0, 4]) + >>> b = MOMonomial(3, "x", 2) + >>> add(a, b) + + """ + new_coefs = {right.power: right.coefficient} + new_coefs = {**new_coefs, **left.coefficients} + return MOpolynomial(left.variable, new_coefs) + +@add.register(MOpolynomial, MOpolynomial) +def mopolynomial_mopolynomial(left, right): + """ add a polynomial with a MOpolynomial to create a MOpolynomial + + >>> a = MOpolynomial('x', [1, 0, 3]) + >>> b = MOpolynomial('x', [0, 2, 0, 4]) + >>> add(a, b) + + >>> add(b, a) + + """ + new_coefs = {**right.coefficients, **left.coefficients} + return MOpolynomial(right.variable, new_coefs) + +@add.register(MOstr, MOMonomial) +def mostr_monomial(left, right): + """ add a mostr with a MOMonomial to create a MOpolynomial + + >>> a = MOstr('x') + >>> b = MOMonomial(3, 'x', 4) + >>> add(a, b) + + """ + return MOpolynomial(right.variable, + {right.power: right.coefficient, 1: 1} + ) + +@add.register(MOMonomial, MOstr) +def monomial_mostr(left, right): + """ add a mostr with a MOMonomial to create a MOpolynomial + + >>> a = MOMonomial(3, 'x', 4) + >>> b = MOstr('x') + >>> add(a, b) + + """ + return MOpolynomial(left.variable, + {1: 1, left.power: left.coefficient} + ) + +@add.register(MOstrPower, MOMonomial) +def mostrpower_monomial(left, right): + """ add a mostrPower with a MOMonomial to create a MOpolynomial + + >>> a = MOstrPower('x', 2) + >>> b = MOMonomial(3, 'x', 4) + >>> add(a, b) + + """ + return MOpolynomial(right.variable, + {right.power: right.coefficient, left.power: 1} + ) + +@add.register(MOMonomial, MOstrPower) +def monomial_mostrpower(left, right): + """ add a mostrPower with a MOMonomial to create a MOpolynomial + + >>> a = MOMonomial(3, 'x', 4) + >>> b = MOstrPower('x', 3) + >>> add(a, b) + + """ + return MOpolynomial(left.variable, + {right.power: 1, left.power: left.coefficient} + ) + +@add.register(MOMonomial, MOMonomial) +def monomial_momonomial(left, right): + """ add a moMonomial with a MOMonomial to create a MOpolynomial + + >>> a = MOMonomial(3, 'x', 4) + >>> b = MOMonomial(2, 'x', 3) + >>> add(a, b) + + """ + return MOpolynomial(left.variable, + {right.power: right.coefficient, left.power: left.coefficient} + ) + + # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: