Feat(Core): Add compute add for 2 mostr and 2 mostrpower

This commit is contained in:
Bertrand Benjamin 2018-11-19 12:17:03 +01:00
parent 8280640a4f
commit 1be3b35e1b

View File

@ -156,6 +156,19 @@ def mopolynomial_moscalar(left, right):
new_coefs = {**new_coefs, **left.coefficients} new_coefs = {**new_coefs, **left.coefficients}
return MOpolynomial(left.variable, new_coefs) return MOpolynomial(left.variable, new_coefs)
@add.register(MOstr, MOstr)
def mostr_mostr(left, right):
""" add 2 mostr
>>> a = MOstr('x')
>>> b = MOstr('x')
>>> add(a, b)
<MOMonomial 2x>
"""
if left != right:
raise NotImplementedError("Can't add 2 Mostr without same letter")
return MOMonomial(2, left)
@add.register(MOstr, MOstrPower) @add.register(MOstr, MOstrPower)
def mostr_mostrpower(left, right): def mostr_mostrpower(left, right):
""" add a scalar with a letter to create a MOpolynomial """ add a scalar with a letter to create a MOpolynomial
@ -188,6 +201,22 @@ def mostrpower_mostr(left, right):
raise raise
return MOpolynomial(right , {1: 1, left.power: 1}) return MOpolynomial(right , {1: 1, left.power: 1})
@add.register(MOstrPower, MOstrPower)
def mostrpower_mostrpower(left, right):
""" add 2 mostrpower
>>> a = MOstrPower('x', 3)
>>> b = MOstrPower('x', 3)
>>> add(a, b)
<MOMonomial 2x^3>
"""
if left.variable != right.variable:
raise NotImplementedError("Can't add 2 Mostrpower without same letter")
if left.power != right.power:
raise NotImplementedError("Can't add 2 Mostrpower with compute if not same degree")
return MOMonomial(2, left.variable, left.power)
@add.register(MOstr, MOpolynomial) @add.register(MOstr, MOpolynomial)
def mostr_mopolynomial(left, right): def mostr_mopolynomial(left, right):
""" add a str with a MOpolynomial to create a MOpolynomial """ add a str with a MOpolynomial to create a MOpolynomial