def test_different_functions_3(self):
     expr = Expression('sin(2/exp(x)+7*arcsin(x))')
     x = Symbol('x')
     self._check__values(
         Differentiation.diff(expr, x),
         'cos(2/exp(x)+7*arcsin(x))*((0*(exp(x))-(2)*'
         '(exp(x)))/(exp(x))^2+7*1/(1-(x)^2)^2)')
Beispiel #2
0
    def diff(expression: Expression, symbol: Symbol, order: int = 1,
             value: float = None):
        """
        Calculates the symbolic derivative of a function of a certain order.

        :param expression:
        :type expression: Expression

        :param symbol:
        :type symbol: Symbol

        :param order:
        :type order: int

        :param value:
        :type value: float

        :return:
        :rtype: Expression
        """
        for index_1 in range(order):
            expression = expression.valid()
            expr_tree = expression.tree(symbol)
            latest_letter = sorted(list(expr_tree.keys()))[-1]
            latest_expr = expr_tree[latest_letter]
            expr_diff = Differentiation.__diff_main(latest_expr, expr_tree,
                                                    symbol)

            index_2 = 0
            while index_2 < len(expr_diff):
                if type(expr_diff[index_2]) == list:
                    expr_diff = expr_diff[:index_2] + expr_diff[index_2] + \
                                expr_diff[index_2 + 1:]
                index_2 += 1

            result = ''.join(str(j) for j in expr_diff)
            for (key, val) in expr_tree.items():
                result = result.replace(key, ''.join(str(j) for j in val))
            expression = Expression(result).simplify()

        if value is None:
            return expression
        else:
            exec(f'{symbol}={value}')
            expression = HelpFunctions.replace_functions(str(expression))
            return eval(str(expression))
 def test_hyperbolic_functions_2(self):
     expr = Expression('ch(x)')
     x = Symbol('x')
     self._check__values(Differentiation.diff(expr, x), 'sh(x)')
 def test_inverse_trigonometrical_functions_4(self):
     expr = Expression('arcctg(x)')
     x = Symbol('x')
     self._check__values(Differentiation.diff(expr, x), '-1/(1+(x)^2)')
 def test_trigonometrical_functions_6(self):
     expr = Expression('csc(x)')
     x = Symbol('x')
     self._check__values(Differentiation.diff(expr, x), '-ctg(x)*csc(x)')
 def test_trigonometrical_functions_3(self):
     expr = Expression('tg(x)')
     x = Symbol('x')
     self._check__values(Differentiation.diff(expr, x), '1/cos(x)^2')
 def test_exponential_functions_2(self):
     expr = Expression('ln(x)')
     x = Symbol('x')
     self._check__values(Differentiation.diff(expr, x), '1/(x)')
 def test_symbol(self):
     expr = Expression('x')
     x = Symbol('x')
     self._check__values(Differentiation.diff(expr, x), '1')
 def test_error_2(self):
     try:
         expr = Expression('sin(x))').valid()
         self.assertEqual(True, False)
     except InvalidExpressionError:
         self.assertEqual(True, True)
 def test_calculations_3(self):
     expr = Expression('sin(x)+cos(x)')
     x = Symbol('x')
     self._check__values(Differentiation.diff(expr, x, value=1),
                         '-0.30116867893975674')
 def test_calculations_2(self):
     expr = Expression('cos(x)')
     x = Symbol('x')
     self._check__values(Differentiation.diff(expr, x, value=1),
                         '-0.8414709848078965')
 def test_calculations_1(self):
     expr = Expression('sin(x)')
     x = Symbol('x')
     self._check__values(Differentiation.diff(expr, x, value=1),
                         '0.5403023058681398')
 def test_different_functions_2(self):
     expr = Expression('sin(2/exp(x))')
     x = Symbol('x')
     self._check__values(
         Differentiation.diff(expr, x), 'cos(2/exp(x))*((0*(exp(x))-'
         '(2)*(exp(x)))/(exp(x))^2)')
 def test_different_functions_1(self):
     expr = Expression('sin(x)^2')
     x = Symbol('x')
     self._check__values(Differentiation.diff(expr, x),
                         'exp(2*ln(sin(x)))*(2*1/(sin(x))*(cos(x)))')