Feat(Compute): Add MOpolynomial with scalar
This commit is contained in:
@@ -16,6 +16,7 @@ from ..tree import Tree
|
||||
from ..MO.mo import MO, MOnumber, MOstr
|
||||
from ..MO.fraction import MOFraction
|
||||
from ..MO.monomial import MOstrPower, MOMonomial
|
||||
from ..MO.polynomial import MOpolynomial
|
||||
from .exceptions import AddError
|
||||
from .arithmetic import lcm
|
||||
from .filters import special_case
|
||||
@@ -236,6 +237,72 @@ def mostrpower_mostrpower(left, right):
|
||||
raise NotImplementedError("Can't add 2 MOstrPower with not same power")
|
||||
return MOMonomial(2, left.variable, left.power)
|
||||
|
||||
@add.register((MOnumber, MOFraction), MOpolynomial)
|
||||
@special_case(add_filter)
|
||||
def monumber_mofraction(left, right):
|
||||
""" Add a scalar to a polynomial
|
||||
|
||||
:example:
|
||||
>>> a = MOnumber(1)
|
||||
>>> b = MOpolynomial("x", [2, 3, 4])
|
||||
>>> print(add(a, b))
|
||||
+
|
||||
> +
|
||||
| > *
|
||||
| | > 4
|
||||
| | > ^
|
||||
| | | > x
|
||||
| | | > 2
|
||||
| > *
|
||||
| | > 3
|
||||
| | > x
|
||||
> +
|
||||
| > 1
|
||||
| > 2
|
||||
"""
|
||||
if 0 not in right.coefficients.keys():
|
||||
raise NotImplementedError("Polynomial with no constant, no calculus to do")
|
||||
|
||||
right_const = right.coefficients[0]
|
||||
right_other = {k:v for k,v in right.coefficients.items() if k != 0}
|
||||
right_remain = MOpolynomial(right.variable, right_other)
|
||||
|
||||
add_consts = Tree("+", left, right_const)
|
||||
return Tree('+', right_remain, add_consts)
|
||||
|
||||
@add.register(MOpolynomial, (MOnumber, MOFraction))
|
||||
@special_case(add_filter)
|
||||
def monumber_mofraction(left, right):
|
||||
""" Add a scalar to a polynomial
|
||||
|
||||
:example:
|
||||
>>> a = MOpolynomial("x", [2, 3, 4])
|
||||
>>> b = MOnumber(1)
|
||||
>>> print(add(a, b))
|
||||
+
|
||||
> +
|
||||
| > *
|
||||
| | > 4
|
||||
| | > ^
|
||||
| | | > x
|
||||
| | | > 2
|
||||
| > *
|
||||
| | > 3
|
||||
| | > x
|
||||
> +
|
||||
| > 1
|
||||
| > 2
|
||||
"""
|
||||
if 0 not in left.coefficients.keys():
|
||||
raise NotImplementedError("Polynomial with no constant, no calculus to do")
|
||||
|
||||
left_const = left.coefficients[0]
|
||||
left_other = {k:v for k,v in left.coefficients.items() if k != 0}
|
||||
left_remain = MOpolynomial(left.variable, left_other)
|
||||
|
||||
add_consts = Tree("+", right, left_const)
|
||||
return Tree('+', left_remain, add_consts)
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
Reference in New Issue
Block a user