Fix: add special case to add typing
Some checks failed
continuous-integration/drone/push Build is failing

Sometimes, typing/add.py tried to build MOpolynomial with only one
coefficient.
This commit is contained in:
Bertrand Benjamin 2021-10-20 15:31:14 +02:00
parent d6e3f774fa
commit 60ee751e91
3 changed files with 72 additions and 4 deletions

View File

@ -132,7 +132,7 @@ class MOstrPower(Molecule):
class MOMonomial(Molecule):
""" Monomial math object"""
""" Monomial math object : ax^n"""
MAINOP = "*"

View File

@ -18,7 +18,7 @@ __all__ = ["MOpolynomial"]
class MOpolynomial(Molecule):
""" MO polynomial"""
""" MO polynomial: ax^n + ... + z (can't be a monomial)"""
MAINOP = "+"
@ -39,6 +39,14 @@ class MOpolynomial(Molecule):
<MOpolynomial 4x^3 + 1>
>>> MOpolynomial('x', {0: 1, 3: 1})
<MOpolynomial x^3 + 1>
>>> MOpolynomial('x', [0, 0, 3])
Traceback (most recent call last):
...
TypeError: A MOpolynomial can't be monomial it has to have more than one coefficient.
>>> MOpolynomial('x', {3: 1})
Traceback (most recent call last):
...
TypeError: A MOpolynomial can't be monomial it has to have more than one coefficient.
"""
_variable = MO.factory(variable)
@ -58,6 +66,9 @@ class MOpolynomial(Molecule):
raise TypeError("Coefs needs to be a dictionnary or a list")
self._coefs = _coefs
if len(self._coefs) == 1:
raise TypeError("A MOpolynomial can't be monomial it has to have more than one coefficient.")
monomials = OrderedDict()
for deg in sorted(self._coefs.keys()):
coef = self._coefs[deg]

View File

@ -11,11 +11,11 @@ Add MO with typing
"""
from multipledispatch import Dispatcher
from ..tree import Tree
from ..MO import MO, MOnumber, MOstr
from ..MO import MOnumber, MOstr
from ..MO.monomial import MOstrPower, MOMonomial
from ..MO.polynomial import MOpolynomial
from ..MO.fraction import MOFraction
from ..compute.filters import special_case
add_doc = """ Add MOs
@ -27,8 +27,26 @@ add_doc = """ Add MOs
add = Dispatcher("add", doc=add_doc)
def add_filter(left, right):
""" Special cases for add MO (adding 0)
:param left: MO
:param right: MO
:returns: MO if it is a special case, nothing other wise
"""
try:
if left == 0:
return right
except TypeError:
pass
try:
if right == 0:
return left
except TypeError:
pass
@add.register((MOnumber, MOFraction), MOstr)
@special_case(add_filter)
def moscalar_mostr(left, right):
""" add a scalar with a letter to create a MOpolynomial
@ -39,11 +57,15 @@ def moscalar_mostr(left, right):
>>> a = MOFraction(1, 5)
>>> add(a, b)
<MOpolynomial x + 1 / 5>
>>> a = MOnumber(0)
>>> add(a, b)
<MOstr x>
"""
return MOpolynomial(right, [left, 1])
@add.register(MOstr, (MOnumber, MOFraction))
@special_case(add_filter)
def mostr_moscalar(left, right):
""" add a scalar with a letter to create a MOpolynomial
@ -54,11 +76,15 @@ def mostr_moscalar(left, right):
>>> b = MOFraction(1, 5)
>>> add(a, b)
<MOpolynomial x + 1 / 5>
>>> b = MOnumber(0)
>>> add(a, b)
<MOstr x>
"""
return MOpolynomial(left, [right, 1])
@add.register((MOnumber, MOFraction), MOstrPower)
@special_case(add_filter)
def moscalar_mostrpower(left, right):
""" add a scalar with a letter to create a MOpolynomial
@ -74,6 +100,7 @@ def moscalar_mostrpower(left, right):
@add.register(MOstrPower, (MOnumber, MOFraction))
@special_case(add_filter)
def mostrpower_moscalar(left, right):
""" add a scalar with a letter to create a MOpolynomial
@ -89,6 +116,7 @@ def mostrpower_moscalar(left, right):
@add.register((MOnumber, MOFraction), MOMonomial)
@special_case(add_filter)
def moscalar_momonomial(left, right):
""" add a scalar with a MOMonomial to create a MOpolynomial
@ -99,11 +127,17 @@ def moscalar_momonomial(left, right):
>>> a = MOFraction(1, 5)
>>> add(a, b)
<MOpolynomial 3x^4 + 1 / 5>
>>> a = MOnumber(0)
>>> b = MOMonomial(2, 'x', 4)
>>> add(a, b)
<MOMonomial 2x^4>
"""
return MOpolynomial(right.variable, {right.power: right.coefficient, 0: left})
@add.register(MOMonomial, (MOnumber, MOFraction))
@special_case(add_filter)
def momonial_moscalar(left, right):
""" add a scalar with a letter to create a MOpolynomial
@ -115,11 +149,17 @@ def momonial_moscalar(left, right):
>>> add(a, b)
<MOpolynomial 3x^4 + 1 / 5>
>>> a = MOMonomial(2, 'x', 4)
>>> b = MOnumber(0)
>>> add(a, b)
<MOMonomial 2x^4>
"""
return MOpolynomial(left.variable, {0: right, left.power: left.coefficient})
@add.register((MOnumber, MOFraction), MOpolynomial)
@special_case(add_filter)
def moscalar_mopolynomial(left, right):
""" add a scalar with a MOpolynomial to create a MOpolynomial
@ -143,6 +183,7 @@ def moscalar_mopolynomial(left, right):
@add.register(MOpolynomial, (MOnumber, MOFraction))
@special_case(add_filter)
def mopolynomial_moscalar(left, right):
""" add a scalar with a MOpolynomial to create a MOpolynomial
@ -164,6 +205,7 @@ def mopolynomial_moscalar(left, right):
@add.register(MOstr, MOstr)
@special_case(add_filter)
def mostr_mostr(left, right):
""" add 2 mostr
@ -178,6 +220,7 @@ def mostr_mostr(left, right):
@add.register(MOstr, MOstrPower)
@special_case(add_filter)
def mostr_mostrpower(left, right):
""" add a scalar with a letter to create a MOpolynomial
@ -195,6 +238,7 @@ def mostr_mostrpower(left, right):
@add.register(MOstrPower, MOstr)
@special_case(add_filter)
def mostrpower_mostr(left, right):
""" add a scalar with a letter to create a MOpolynomial
@ -212,6 +256,7 @@ def mostrpower_mostr(left, right):
@add.register(MOstrPower, MOstrPower)
@special_case(add_filter)
def mostrpower_mostrpower(left, right):
""" add 2 mostrpower
@ -231,6 +276,7 @@ def mostrpower_mostrpower(left, right):
@add.register(MOstr, MOpolynomial)
@special_case(add_filter)
def mostr_mopolynomial(left, right):
""" add a str with a MOpolynomial to create a MOpolynomial
@ -249,6 +295,7 @@ def mostr_mopolynomial(left, right):
@add.register(MOpolynomial, MOstr)
@special_case(add_filter)
def mopolynomial_mostr(left, right):
""" add a str with a MOpolynomial to create a MOpolynomial
@ -266,6 +313,7 @@ def mopolynomial_mostr(left, right):
@add.register(MOstrPower, MOpolynomial)
@special_case(add_filter)
def mostrpower_mopolynomial(left, right):
""" add a strPower with a MOpolynomial to create a MOpolynomial
@ -284,6 +332,7 @@ def mostrpower_mopolynomial(left, right):
@add.register(MOpolynomial, MOstrPower)
@special_case(add_filter)
def mopolynomial_mostrpower(left, right):
""" add a strPower with a MOpolynomial to create a MOpolynomial
@ -301,6 +350,7 @@ def mopolynomial_mostrpower(left, right):
@add.register(MOMonomial, MOpolynomial)
@special_case(add_filter)
def momonomial_mopolynomial(left, right):
""" add a Monomial with a MOpolynomial to create a MOpolynomial
@ -319,6 +369,7 @@ def momonomial_mopolynomial(left, right):
@add.register(MOpolynomial, MOMonomial)
@special_case(add_filter)
def mopolynomial_momonomial(left, right):
""" add a Monomial with a MOpolynomial to create a MOpolynomial
@ -336,6 +387,7 @@ def mopolynomial_momonomial(left, right):
@add.register(MOpolynomial, MOpolynomial)
@special_case(add_filter)
def mopolynomial_mopolynomial(left, right):
""" add a polynomial with a MOpolynomial to create a MOpolynomial
@ -355,6 +407,7 @@ def mopolynomial_mopolynomial(left, right):
@add.register(MOstr, MOMonomial)
@special_case(add_filter)
def mostr_monomial(left, right):
""" add a mostr with a MOMonomial to create a MOpolynomial
@ -370,6 +423,7 @@ def mostr_monomial(left, right):
@add.register(MOMonomial, MOstr)
@special_case(add_filter)
def monomial_mostr(left, right):
""" add a mostr with a MOMonomial to create a MOpolynomial
@ -385,6 +439,7 @@ def monomial_mostr(left, right):
@add.register(MOstrPower, MOMonomial)
@special_case(add_filter)
def mostrpower_monomial(left, right):
""" add a mostrPower with a MOMonomial to create a MOpolynomial
@ -402,6 +457,7 @@ def mostrpower_monomial(left, right):
@add.register(MOMonomial, MOstrPower)
@special_case(add_filter)
def monomial_mostrpower(left, right):
""" add a mostrPower with a MOMonomial to create a MOpolynomial
@ -419,6 +475,7 @@ def monomial_mostrpower(left, right):
@add.register(MOMonomial, MOMonomial)
@special_case(add_filter)
def monomial_momonomial(left, right):
""" add a moMonomial with a MOMonomial to create a MOpolynomial