Feat(Core): Add exceptions for add typing
This commit is contained in:
parent
ecf7e7678f
commit
8280640a4f
@ -42,7 +42,7 @@ def moscalar_mostr(left, right):
|
|||||||
return MOpolynomial(right, [left, 1])
|
return MOpolynomial(right, [left, 1])
|
||||||
|
|
||||||
@add.register(MOstr, (MOnumber, MOFraction))
|
@add.register(MOstr, (MOnumber, MOFraction))
|
||||||
def moscalar_mostr(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
|
||||||
|
|
||||||
>>> a = MOstr('x')
|
>>> a = MOstr('x')
|
||||||
@ -56,7 +56,7 @@ def moscalar_mostr(left, right):
|
|||||||
return MOpolynomial(left, [right, 1])
|
return MOpolynomial(left, [right, 1])
|
||||||
|
|
||||||
@add.register((MOnumber, MOFraction), MOstrPower)
|
@add.register((MOnumber, MOFraction), MOstrPower)
|
||||||
def moscalar_mostr(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
|
||||||
|
|
||||||
>>> a = MOnumber(2)
|
>>> a = MOnumber(2)
|
||||||
@ -70,7 +70,7 @@ def moscalar_mostr(left, right):
|
|||||||
return MOpolynomial(right.variable, {0: left, right.power: 1})
|
return MOpolynomial(right.variable, {0: left, right.power: 1})
|
||||||
|
|
||||||
@add.register(MOstrPower, (MOnumber, MOFraction))
|
@add.register(MOstrPower, (MOnumber, MOFraction))
|
||||||
def moscalar_mostr(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
|
||||||
|
|
||||||
>>> a = MOstrPower('x', 3)
|
>>> a = MOstrPower('x', 3)
|
||||||
@ -128,7 +128,10 @@ def moscalar_mopolynomial(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 1 / 5 + 3x^2 + 2x>
|
<MOpolynomial 1 / 5 + 3x^2 + 2x>
|
||||||
"""
|
"""
|
||||||
new_coefs = right.coefficients
|
if 0 in right.coefficients.keys():
|
||||||
|
raise NotImplementedError(f"Polynomial with constant ({right.coefficients[0]}), calculus to do")
|
||||||
|
|
||||||
|
new_coefs = {**right.coefficients}
|
||||||
#! Need to be add at the end to be printed at the beginning
|
#! Need to be add at the end to be printed at the beginning
|
||||||
new_coefs[0] = left
|
new_coefs[0] = left
|
||||||
return MOpolynomial(right.variable, new_coefs)
|
return MOpolynomial(right.variable, new_coefs)
|
||||||
@ -145,13 +148,16 @@ def mopolynomial_moscalar(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 3x^2 + 2x + 1 / 5>
|
<MOpolynomial 3x^2 + 2x + 1 / 5>
|
||||||
"""
|
"""
|
||||||
|
if 0 in left.coefficients.keys():
|
||||||
|
raise NotImplementedError("Polynomial with constant, calculus to do")
|
||||||
|
|
||||||
#! Need to be add at the beginning to be printed at the end
|
#! Need to be add at the beginning to be printed at the end
|
||||||
new_coefs = {0: right}
|
new_coefs = {0: 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, MOstrPower)
|
@add.register(MOstr, MOstrPower)
|
||||||
def moscalar_mostr(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
|
||||||
|
|
||||||
>>> a = MOstr('x')
|
>>> a = MOstr('x')
|
||||||
@ -167,7 +173,7 @@ def moscalar_mostr(left, right):
|
|||||||
return MOpolynomial(left , {1: 1, right.power: 1})
|
return MOpolynomial(left , {1: 1, right.power: 1})
|
||||||
|
|
||||||
@add.register(MOstrPower, MOstr)
|
@add.register(MOstrPower, MOstr)
|
||||||
def moscalar_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
|
||||||
|
|
||||||
>>> a = MOstrPower('x', 3)
|
>>> a = MOstrPower('x', 3)
|
||||||
@ -191,7 +197,10 @@ def mostr_mopolynomial(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial x + 3x^2 + 1>
|
<MOpolynomial x + 3x^2 + 1>
|
||||||
"""
|
"""
|
||||||
new_coefs = right.coefficients
|
if 1 in right.coefficients.keys():
|
||||||
|
raise NotImplementedError("Polynomial with no constant, calculus to do")
|
||||||
|
|
||||||
|
new_coefs = {**right.coefficients}
|
||||||
#! Need to be add at the end to be printed at the beginning
|
#! Need to be add at the end to be printed at the beginning
|
||||||
new_coefs[1] = 1
|
new_coefs[1] = 1
|
||||||
return MOpolynomial(right.variable, new_coefs)
|
return MOpolynomial(right.variable, new_coefs)
|
||||||
@ -205,6 +214,9 @@ def mopolynomial_mostr(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 3x^2 + 1 + x>
|
<MOpolynomial 3x^2 + 1 + x>
|
||||||
"""
|
"""
|
||||||
|
if 1 in left.coefficients.keys():
|
||||||
|
raise NotImplementedError("Polynomial with no constant, calculus to do")
|
||||||
|
|
||||||
new_coefs = {1: 1}
|
new_coefs = {1: 1}
|
||||||
new_coefs = {**new_coefs, **left.coefficients}
|
new_coefs = {**new_coefs, **left.coefficients}
|
||||||
return MOpolynomial(left.variable, new_coefs)
|
return MOpolynomial(left.variable, new_coefs)
|
||||||
@ -218,7 +230,10 @@ def mostrpower_mopolynomial(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial x^2 + 4x^3 + 2x + 1>
|
<MOpolynomial x^2 + 4x^3 + 2x + 1>
|
||||||
"""
|
"""
|
||||||
new_coefs = right.coefficients
|
if left.power in right.coefficients.keys():
|
||||||
|
raise NotImplementedError("Degree in common, need to compute")
|
||||||
|
|
||||||
|
new_coefs = {**right.coefficients}
|
||||||
#! Need to be add at the end to be printed at the beginning
|
#! Need to be add at the end to be printed at the beginning
|
||||||
new_coefs[left.power] = 1
|
new_coefs[left.power] = 1
|
||||||
return MOpolynomial(right.variable, new_coefs)
|
return MOpolynomial(right.variable, new_coefs)
|
||||||
@ -232,6 +247,9 @@ def mopolynomial_mostrpower(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 4x^3 + 2x + 1 + x^2>
|
<MOpolynomial 4x^3 + 2x + 1 + x^2>
|
||||||
"""
|
"""
|
||||||
|
if right.power in left.coefficients.keys():
|
||||||
|
raise NotImplementedError("Degree in common, need to compute")
|
||||||
|
|
||||||
new_coefs = {right.power: 1}
|
new_coefs = {right.power: 1}
|
||||||
new_coefs = {**new_coefs, **left.coefficients}
|
new_coefs = {**new_coefs, **left.coefficients}
|
||||||
return MOpolynomial(left.variable, new_coefs)
|
return MOpolynomial(left.variable, new_coefs)
|
||||||
@ -245,7 +263,10 @@ def momonomial_mopolynomial(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 3x^2 + 4x^3 + 2x + 1>
|
<MOpolynomial 3x^2 + 4x^3 + 2x + 1>
|
||||||
"""
|
"""
|
||||||
new_coefs = right.coefficients
|
if left.power in right.coefficients.keys():
|
||||||
|
raise NotImplementedError("Degree in common, need to compute")
|
||||||
|
|
||||||
|
new_coefs = {**right.coefficients}
|
||||||
#! Need to be add at the end to be printed at the beginning
|
#! Need to be add at the end to be printed at the beginning
|
||||||
new_coefs[left.power] = left.coefficient
|
new_coefs[left.power] = left.coefficient
|
||||||
return MOpolynomial(right.variable, new_coefs)
|
return MOpolynomial(right.variable, new_coefs)
|
||||||
@ -259,6 +280,9 @@ def mopolynomial_momonomial(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 4x^3 + 2x + 1 + 3x^2>
|
<MOpolynomial 4x^3 + 2x + 1 + 3x^2>
|
||||||
"""
|
"""
|
||||||
|
if right.power in left.coefficients.keys():
|
||||||
|
raise NotImplementedError("Degree in common, need to compute")
|
||||||
|
|
||||||
new_coefs = {right.power: right.coefficient}
|
new_coefs = {right.power: right.coefficient}
|
||||||
new_coefs = {**new_coefs, **left.coefficients}
|
new_coefs = {**new_coefs, **left.coefficients}
|
||||||
return MOpolynomial(left.variable, new_coefs)
|
return MOpolynomial(left.variable, new_coefs)
|
||||||
@ -274,6 +298,10 @@ def mopolynomial_mopolynomial(left, right):
|
|||||||
>>> add(b, a)
|
>>> add(b, a)
|
||||||
<MOpolynomial 4x^3 + 2x + 3x^2 + 1>
|
<MOpolynomial 4x^3 + 2x + 3x^2 + 1>
|
||||||
"""
|
"""
|
||||||
|
common_degree = set(left.monomials.keys()).intersection(right.monomials.keys())
|
||||||
|
if common_degree:
|
||||||
|
raise NotImplementedError("Degree in common, need to compute")
|
||||||
|
|
||||||
new_coefs = {**right.coefficients, **left.coefficients}
|
new_coefs = {**right.coefficients, **left.coefficients}
|
||||||
return MOpolynomial(right.variable, new_coefs)
|
return MOpolynomial(right.variable, new_coefs)
|
||||||
|
|
||||||
@ -286,6 +314,9 @@ def mostr_monomial(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial x + 3x^4>
|
<MOpolynomial x + 3x^4>
|
||||||
"""
|
"""
|
||||||
|
if right.power == 1:
|
||||||
|
raise NotImplementedError("Monomial is deg 1, need to compute")
|
||||||
|
|
||||||
return MOpolynomial(right.variable,
|
return MOpolynomial(right.variable,
|
||||||
{right.power: right.coefficient, 1: 1}
|
{right.power: right.coefficient, 1: 1}
|
||||||
)
|
)
|
||||||
@ -299,6 +330,9 @@ def monomial_mostr(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 3x^4 + x>
|
<MOpolynomial 3x^4 + x>
|
||||||
"""
|
"""
|
||||||
|
if left.power == 1:
|
||||||
|
raise NotImplementedError("Monomial is deg 1, need to compute")
|
||||||
|
|
||||||
return MOpolynomial(left.variable,
|
return MOpolynomial(left.variable,
|
||||||
{1: 1, left.power: left.coefficient}
|
{1: 1, left.power: left.coefficient}
|
||||||
)
|
)
|
||||||
@ -312,6 +346,9 @@ def mostrpower_monomial(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial x^2 + 3x^4>
|
<MOpolynomial x^2 + 3x^4>
|
||||||
"""
|
"""
|
||||||
|
if left.power == right.power:
|
||||||
|
raise NotImplementedError("MostrPower and MOMonomial are same degree, need to compute")
|
||||||
|
|
||||||
return MOpolynomial(right.variable,
|
return MOpolynomial(right.variable,
|
||||||
{right.power: right.coefficient, left.power: 1}
|
{right.power: right.coefficient, left.power: 1}
|
||||||
)
|
)
|
||||||
@ -325,6 +362,9 @@ def monomial_mostrpower(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 3x^4 + x^3>
|
<MOpolynomial 3x^4 + x^3>
|
||||||
"""
|
"""
|
||||||
|
if left.power == right.power:
|
||||||
|
raise NotImplementedError("MostrPower and MOMonomial are same degree, need to compute")
|
||||||
|
|
||||||
return MOpolynomial(left.variable,
|
return MOpolynomial(left.variable,
|
||||||
{right.power: 1, left.power: left.coefficient}
|
{right.power: 1, left.power: left.coefficient}
|
||||||
)
|
)
|
||||||
@ -338,6 +378,9 @@ def monomial_momonomial(left, right):
|
|||||||
>>> add(a, b)
|
>>> add(a, b)
|
||||||
<MOpolynomial 3x^4 + 2x^3>
|
<MOpolynomial 3x^4 + 2x^3>
|
||||||
"""
|
"""
|
||||||
|
if left.power == right.power:
|
||||||
|
raise NotImplementedError("MOMonomials are same degree, need to compute")
|
||||||
|
|
||||||
return MOpolynomial(left.variable,
|
return MOpolynomial(left.variable,
|
||||||
{right.power: right.coefficient, left.power: left.coefficient}
|
{right.power: right.coefficient, left.power: left.coefficient}
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user