diff --git a/mapytex/calculus/core/MO/monomial.py b/mapytex/calculus/core/MO/monomial.py index 26b9836..d461f45 100644 --- a/mapytex/calculus/core/MO/monomial.py +++ b/mapytex/calculus/core/MO/monomial.py @@ -103,6 +103,15 @@ class MOMonomial(MO): >>> x = MOstrPower('x', 2) >>> MOMonomial(4, x) + >>> MOMonomial(4, 'x') + + >>> MOMonomial(4, 'x', 1) + + >>> MOMonomial(4, 'x', 2) + + >>> x = MOstrPower('x', 2) + >>> MOMonomial(4, x, 3) + >>> MOMonomial(0, x) Traceback (most recent call last): ... @@ -118,30 +127,22 @@ class MOMonomial(MO): _power = MO.factory(_variable.power.value * power) _variable = _variable.variable elif isinstance(_variable, MOstr): - _variable = variable _power = MO.factory(power) + _variable = variable 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: - if self._coefficient == 1: - _tree = self._variable + try: + if self._coefficient.value != 1: + _tree = Tree("*", self._coefficient, self.strpower) else: - _tree = Tree("*", - self._coefficient, - self._variable - ) - else: - if self._coefficient == 1: - _tree = MOstrPower(self._variable, self._power) - else: - _tree = Tree("*", - self._coefficient, - MOstrPower(self._variable, self._power) - ) + _tree = self.strpower + except AttributeError: + _tree = Tree("*", self._coefficient, self.strpower) + MO.__init__(self, _tree)