Feat(Typing): Write all str related typing functions
This commit is contained in:
parent
a79ffb0cf9
commit
c176bb7bc4
@ -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:
|
||||
|
@ -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)
|
||||
<MOpolynomial 3x^4 + 2>
|
||||
<MOpolynomial 2 + 3x^4>
|
||||
>>> a = MOFraction(1, 5)
|
||||
>>> add(a, b)
|
||||
<MOpolynomial 3x^4 + 1 / 5>
|
||||
<MOpolynomial 1 / 5 + 3x^4>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial 2 + 3x^2 + 2x>
|
||||
>>> a = MOFraction(1, 5)
|
||||
>>> add(a, b)
|
||||
<MOpolynomial 1 / 5 + 3x^2 + 2x>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial 3x^2 + 2x + 2>
|
||||
>>> b = MOFraction(1, 5)
|
||||
>>> add(a, b)
|
||||
<MOpolynomial 3x^2 + 2x + 1 / 5>
|
||||
"""
|
||||
#! 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)
|
||||
<MOpolynomial x + 3x^2 + 1>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial 3x^2 + 1 + x>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial x^2 + 4x^3 + 2x + 1>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial 4x^3 + 2x + 1 + x^2>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial 3x^2 + 4x^3 + 2x + 1>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial 4x^3 + 2x + 1 + 3x^2>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial 3x^2 + 1 + 4x^3 + 2x>
|
||||
>>> add(b, a)
|
||||
<MOpolynomial 4x^3 + 2x + 3x^2 + 1>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial x + 3x^4>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial 3x^4 + x>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial x^2 + 3x^4>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial 3x^4 + x^3>
|
||||
"""
|
||||
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)
|
||||
<MOpolynomial 3x^4 + 2x^3>
|
||||
"""
|
||||
return MOpolynomial(left.variable,
|
||||
{right.power: right.coefficient, left.power: left.coefficient}
|
||||
)
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
Loading…
Reference in New Issue
Block a user