diff --git a/mapytex/calculus/API/expression.py b/mapytex/calculus/API/expression.py index 69a67c4..3cfec23 100644 --- a/mapytex/calculus/API/expression.py +++ b/mapytex/calculus/API/expression.py @@ -309,15 +309,15 @@ class Expression(object): comp_exp = opt_exp._compute() - if typed_exp == comp_exp: - typed_exp.set_ancestor(self._ancestor) - return typed_exp - else: + if typed_exp != comp_exp: comp_exp.set_ancestor(self) return comp_exp._simplify(optimize=optimize) + typed_exp.set_ancestor(self._ancestor) + return typed_exp + def simplify(self, optimize=True): - """ Compute as much as possible the expression + """ Simplify the expression, keep the history and factory child :param optimize: bool to optimize tree when it's possible :return: an expression diff --git a/mapytex/calculus/API/tokens/number.py b/mapytex/calculus/API/tokens/number.py index 266e124..7e0abb9 100644 --- a/mapytex/calculus/API/tokens/number.py +++ b/mapytex/calculus/API/tokens/number.py @@ -271,18 +271,19 @@ class Fraction(Token): """ return Decimal(self._mo._value) + @property def simplified(self): """ Get the irreductible version of self :example: >>> f = Fraction("3/4") - >>> f.simplified() + >>> f.simplified >>> f = Fraction("12/9") - >>> f.simplified() + >>> f.simplified >>> f = Fraction("12/4") - >>> f.simplified() + >>> f.simplified """ @@ -293,6 +294,32 @@ class Fraction(Token): return Fraction(simplified) + def simplify(self): + """ Itself or its simplified version + + :example: + >>> f = Fraction("12/8") + >>> fs = f.simplify() + >>> for i in fs.explain(): + ... print(i) + 12 / 8 + 3 / 2 + >>> f = Fraction("5/8") + >>> fs = f.simplify() + >>> for i in fs.explain(): + ... print(i) + 5 / 8 + """ + simplified = self.simplified + try: + if self.numerator == simplified.numerator: + return self + except AttributeError: + pass + + simplified._ancestor = self + return simplified + diff --git a/mapytex/calculus/API/tokens/polynomial.py b/mapytex/calculus/API/tokens/polynomial.py index 035f662..1e005ee 100644 --- a/mapytex/calculus/API/tokens/polynomial.py +++ b/mapytex/calculus/API/tokens/polynomial.py @@ -148,7 +148,11 @@ class Linear(Polynomial): >>> P.differentiate() >>> P.roots - [] + [] + >>> for i in P.roots[0].explain(): + ... print(i) + - 2 / 1 + - 2 """ @@ -190,9 +194,20 @@ class Linear(Polynomial): >>> from ...core.MO.polynomial import MOpolynomial, MOMonomial >>> P = Linear(MOpolynomial('x', [1, 2])) >>> P.roots - [] - >>> #P = Linear(MOpolynomial('x', [1, -2])) - >>> #P.roots + [] + >>> P = Linear(MOpolynomial('x', [2, 1])) + >>> P.roots + [] + >>> for i in P.roots[0].explain(): + ... print(i) + - 1 / 2 + >>> P = Linear(MOpolynomial('x', [10, 6])) + >>> P.roots + [] + >>> for i in P.roots[0].explain(): + ... print(i) + - 6 / 10 + - 3 / 5 """ try: