diff --git a/mapytex/calculus/core/MO/monomial.py b/mapytex/calculus/core/MO/monomial.py index 18a1533..ede5a68 100644 --- a/mapytex/calculus/core/MO/monomial.py +++ b/mapytex/calculus/core/MO/monomial.py @@ -27,23 +27,23 @@ class MOstrPower(MO): >>> MOstrPower(3, 1) Traceback (most recent call last): - ... + ... mapytex.calculus.core.MO.exceptions.MOError: The variable of a monomial should be convertible into MOstr >>> MOstrPower("x", 0) Traceback (most recent call last): - ... + ... mapytex.calculus.core.MO.exceptions.MOError: The power of a MOstrPower should be greater than 1 >>> MOstrPower("x", 1) Traceback (most recent call last): - ... + ... mapytex.calculus.core.MO.exceptions.MOError: The power of a MOstrPower should be greater than 1 >>> MOstrPower("x", -2) Traceback (most recent call last): - ... + ... mapytex.calculus.core.MO.exceptions.MOError: The power of a MOstrPower should be greater than 1 >>> MOstrPower("x", 2.4) Traceback (most recent call last): - ... + ... mapytex.calculus.core.MO.exceptions.MOError: The power of a monomial should be a integer """ @@ -58,11 +58,12 @@ class MOstrPower(MO): elif not isinstance(_power._tree, int): raise MOError("The power of a monomial should be a integer") self._power = _power - + _tree = Tree("^", self._variable, - self._power - ) + self._power, + ) + MO.__init__(self, _tree) @property @@ -77,11 +78,12 @@ class MOMonomial(MO): """ Monomial math object""" - def __init__(self, coefficient, variable): + def __init__(self, coefficient, variable, power=1): """ Initiate the MOMonomial :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') >>> MOMonomial(4, x) @@ -99,15 +101,28 @@ class MOMonomial(MO): raise MOError("The coefficient of a monomial should not be 0") self._coefficient = _coefficient - if not isinstance(variable, (MOstr, MOstrPower)): - raise - self._variable = variable + if isinstance(variable, MOstrPower): + _variable = 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)}.") - _tree = Tree("*", - self._coefficient, - self._variable + self._variable = _variable + self._power = _power - ) + if self._power == 1: + _tree = Tree("*", + self._coefficient, + self._variable + ) + else: + _tree = Tree("*", + self._coefficient, + MOstrPower(self._variable, self._power) + ) MO.__init__(self, _tree) @@ -117,21 +132,15 @@ class MOMonomial(MO): @property def strpower(self): - if isinstance(self._variable, MOstr): - return self._variable - return self._variable + return MOstrPower(self._variable, self._power) @property def variable(self): - if isinstance(self._variable, MOstr): - return self._variable - return self._variable.variable + return self._variable @property def power(self): - if isinstance(self._variable, MOstr): - return 1 - return self._variable.power + return self._power # ----------------------------- # Reglages pour 'vim' diff --git a/mapytex/calculus/core/MO/polynomial.py b/mapytex/calculus/core/MO/polynomial.py index 210669b..ad9c3c6 100644 --- a/mapytex/calculus/core/MO/polynomial.py +++ b/mapytex/calculus/core/MO/polynomial.py @@ -10,6 +10,7 @@ from mapytex.calculus.core.tree import Tree from .mo import MO, MOnumber, MOstr from .exceptions import MOError from ..renders import tree2txt, tree2tex +from .monomial import MOMonomial __all__ = ["MOpolynomial"] @@ -25,13 +26,13 @@ class MOpolynomial(MO): :example: >>> MOpolynomial('x', [1, 2, 3]) - 3x^2 + 2x + 1 + >>> MOpolynomial('x', [1, 0, 3]) - 3x^2 + 1 + >>> MOpolynomial('x', {0: 1, 1: 2, 2: 3}) - 3x^2 + 2x + 1 + >>> MOpolynomial('x', {0: 1, 3: 4}) - 4x^3 + 1 + """ @@ -41,24 +42,26 @@ class MOpolynomial(MO): self._variable = _variable 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): - _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: raise TypeError("Coefs needs to be a dictionnary or a list") self._coefs = _coefs monomials = [] - for deg, coef in self._coefs: + for deg, coef in self._coefs.items(): if deg == 0: monomials.append(coef) - elif deg == 1: - monomials.append(coef) - value = Tree("^", - self._variable, - self._power - ) - MO.__init__(self, value) + else: + monomials.append( + MOMonomial(coef, self._variable, deg) + ) + + tree = Tree.from_list("+", monomials[::-1]) + MO.__init__(self, tree) @property def variable(self):