Feat(MO): Initiate MOpolynomial

This commit is contained in:
Bertrand Benjamin 2018-11-13 10:58:32 +01:00
parent abf056ce33
commit eff3ccf9f5
2 changed files with 52 additions and 40 deletions

View File

@ -61,8 +61,9 @@ class MOstrPower(MO):
_tree = Tree("^", _tree = Tree("^",
self._variable, self._variable,
self._power self._power,
) )
MO.__init__(self, _tree) MO.__init__(self, _tree)
@property @property
@ -77,11 +78,12 @@ class MOMonomial(MO):
""" Monomial math object""" """ Monomial math object"""
def __init__(self, coefficient, variable): def __init__(self, coefficient, variable, power=1):
""" Initiate the MOMonomial """ Initiate the MOMonomial
:param coefficient: coefficient of the monomial (a non zero constant) :param coefficient: coefficient of the monomial (a non zero constant)
:param variable: variable of the monomial (a MOstr or later a MOSqrt) :param variable: variable of the monomial (a MOstr, a MOstrPower)
:param power: degree of the monomial
>>> x = MOstr('x') >>> x = MOstr('x')
>>> MOMonomial(4, x) >>> MOMonomial(4, x)
@ -99,14 +101,27 @@ class MOMonomial(MO):
raise MOError("The coefficient of a monomial should not be 0") raise MOError("The coefficient of a monomial should not be 0")
self._coefficient = _coefficient self._coefficient = _coefficient
if not isinstance(variable, (MOstr, MOstrPower)): if isinstance(variable, MOstrPower):
raise _variable = variable.variable
self._variable = variable _power = MO.factory(variable.power.value * power)
elif isinstance(variable, MOstr):
_variable = variable
_power = MO.factory(power)
else:
raise MOError(f"variable need to be a MOstrPower or a MOstr. Got {type(variable)}.")
self._variable = _variable
self._power = _power
if self._power == 1:
_tree = Tree("*", _tree = Tree("*",
self._coefficient, self._coefficient,
self._variable self._variable
)
else:
_tree = Tree("*",
self._coefficient,
MOstrPower(self._variable, self._power)
) )
MO.__init__(self, _tree) MO.__init__(self, _tree)
@ -117,21 +132,15 @@ class MOMonomial(MO):
@property @property
def strpower(self): def strpower(self):
if isinstance(self._variable, MOstr): return MOstrPower(self._variable, self._power)
return self._variable
return self._variable
@property @property
def variable(self): def variable(self):
if isinstance(self._variable, MOstr):
return self._variable return self._variable
return self._variable.variable
@property @property
def power(self): def power(self):
if isinstance(self._variable, MOstr): return self._power
return 1
return self._variable.power
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'

View File

@ -10,6 +10,7 @@ from mapytex.calculus.core.tree import Tree
from .mo import MO, MOnumber, MOstr from .mo import MO, MOnumber, MOstr
from .exceptions import MOError from .exceptions import MOError
from ..renders import tree2txt, tree2tex from ..renders import tree2txt, tree2tex
from .monomial import MOMonomial
__all__ = ["MOpolynomial"] __all__ = ["MOpolynomial"]
@ -25,13 +26,13 @@ class MOpolynomial(MO):
:example: :example:
>>> MOpolynomial('x', [1, 2, 3]) >>> MOpolynomial('x', [1, 2, 3])
3x^2 + 2x + 1 <MOpolynomial 3x^2 + 2x + 1>
>>> MOpolynomial('x', [1, 0, 3]) >>> MOpolynomial('x', [1, 0, 3])
3x^2 + 1 <MOpolynomial 3x^2 + 1>
>>> MOpolynomial('x', {0: 1, 1: 2, 2: 3}) >>> MOpolynomial('x', {0: 1, 1: 2, 2: 3})
3x^2 + 2x + 1 <MOpolynomial 3x^2 + 2x + 1>
>>> MOpolynomial('x', {0: 1, 3: 4}) >>> MOpolynomial('x', {0: 1, 3: 4})
4x^3 + 1 <MOpolynomial 4x^3 + 1>
""" """
@ -41,24 +42,26 @@ class MOpolynomial(MO):
self._variable = _variable self._variable = _variable
if isinstance(coefs, dict): if isinstance(coefs, dict):
_coefs = {MO.factory(d): MO.factory(c) for (d, c) in coefs} _coefs = {MO.factory(d): MO.factory(c) for (d, c) in coefs.items()
if c != 0 }
elif isinstance(coefs, list): elif isinstance(coefs, list):
_coefs = {MO.factory(d): MO.factory(c) for (d, c) in enumerate(coefs)} _coefs = {MO.factory(d): MO.factory(c) for (d, c) in enumerate(coefs)
if c != 0 }
else: else:
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
monomials = [] monomials = []
for deg, coef in self._coefs: for deg, coef in self._coefs.items():
if deg == 0: if deg == 0:
monomials.append(coef) monomials.append(coef)
elif deg == 1: else:
monomials.append(coef) monomials.append(
value = Tree("^", MOMonomial(coef, self._variable, deg)
self._variable,
self._power
) )
MO.__init__(self, value)
tree = Tree.from_list("+", monomials[::-1])
MO.__init__(self, tree)
@property @property
def variable(self): def variable(self):