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): class MOMonomial(Molecule):
""" Monomial math object""" """ Monomial math object : ax^n"""
MAINOP = "*" MAINOP = "*"

View File

@ -18,7 +18,7 @@ __all__ = ["MOpolynomial"]
class MOpolynomial(Molecule): class MOpolynomial(Molecule):
""" MO polynomial""" """ MO polynomial: ax^n + ... + z (can't be a monomial)"""
MAINOP = "+" MAINOP = "+"
@ -39,6 +39,14 @@ class MOpolynomial(Molecule):
<MOpolynomial 4x^3 + 1> <MOpolynomial 4x^3 + 1>
>>> MOpolynomial('x', {0: 1, 3: 1}) >>> MOpolynomial('x', {0: 1, 3: 1})
<MOpolynomial x^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) _variable = MO.factory(variable)
@ -58,6 +66,9 @@ class MOpolynomial(Molecule):
raise TypeError("Coefs needs to be a dictionnary or a list") raise TypeError("Coefs needs to be a dictionnary or a list")
self._coefs = _coefs 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() monomials = OrderedDict()
for deg in sorted(self._coefs.keys()): for deg in sorted(self._coefs.keys()):
coef = self._coefs[deg] coef = self._coefs[deg]

View File

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