diff --git a/pymath/calculus/abstract_polynom.py b/pymath/calculus/abstract_polynom.py index a762522..1248307 100644 --- a/pymath/calculus/abstract_polynom.py +++ b/pymath/calculus/abstract_polynom.py @@ -426,6 +426,31 @@ class AbstractPolynom(Explicable): ans.this_append_before(steps) return ans + def replace_letter(self, letter): + """ Replace the letter in the expression + + :param letter: the new letter. + :returns: The expression with the new letter. + + >>> A = AbstractPolynom([1, 2]) + >>> A + < AbstractPolynom x [1, 2]> + >>> B = A.replace_letter("y") + >>> B + < Expression [2, < Polynom y [0, 1]>, *, 1, +]> + >>> C = A.replace_letter(2) + >>> C + < Expression [2, 2, *, 1, +]> + + """ + postfix_exp = [ + Expression(letter) if i == self._letter + else i + for i in self.postfix_tokens + ] + + return Expression(postfix_exp) + def __eq__(self, other): try: o_poly = self.conv2poly(other) diff --git a/pymath/calculus/polynom.py b/pymath/calculus/polynom.py index dccfb3f..1aabd7b 100644 --- a/pymath/calculus/polynom.py +++ b/pymath/calculus/polynom.py @@ -147,13 +147,9 @@ class Polynom(AbstractPolynom): 3 h^{ 2 } + 8 h + 6 >>> R = P(Q) """ - postfix_exp = [ - Expression(value) if i == self._letter - else i - for i in self.postfix_tokens - ] + eval_exp = self.replace_letter(value) - return Expression(postfix_exp).simplify() + return eval_exp.simplify() def derivate(self): """ Return the derivated polynom