def analyze_rec(self, template: Expression, template_conditions: List,
                    expression: Expression,
                    analysis: MatchAnalysisReport) -> MatchAnalysisReport:

        if not analysis.expression_match_template:
            return analysis

        # Handle two simpy expressions
        if not template.contains_user_defined_funct(
        ) and not expression.contains_user_defined_funct():
            # TODO: REFACTOR
            conditions = template_conditions.get(
                template.to_expression_string())
            if conditions != None and "IS_CONSTANT" in conditions:
                match = expression.is_constant()
            else:
                match = template.is_equivalent_to(expression)

            analysis = self.build_match_analysis_report(
                match, analysis, template, template_conditions, expression)

        elif template.is_user_defined_func():
            match = template.free_symbols_match(expression)
            analysis = self.build_match_analysis_report(
                match, analysis, template, template_conditions, expression)

        elif not template.compare_func(expression):
            analysis = self.build_match_analysis_report(
                False, analysis, template, template_conditions, expression)

        # Handle non leaves with at least one user defined function
        elif template.children_amount() == expression.children_amount():
            analysis = self.analyze_exp_with_user_def_func_eq_sizes(
                template, template_conditions, expression, analysis)

        # Handle different size children. for example f(x)  = x + x^2
        else:
            analysis = self.analyze_exp_with_user_def_func_diff_sizes(
                template, template_conditions, expression, analysis)

        return analysis
Exemplo n.º 2
0
    def test_evaluate_success(self):

        for problem in success_cases:
            data = {
                'problem_input': problem['problem_input'],
                'type': 'derivative'
            }

            response = self.client.post(path='/validations/evaluate',
                                        data=data,
                                        format='json')

            body = json.loads(response.content)
            result_expression = Expression(body['result']['expression'],
                                           body['result']['variables'])
            expected_expression = Expression(
                problem['derivative']['expression'],
                problem['derivative']['variables'])
            self.assertEquals(response.status_code, status.HTTP_200_OK)

            self.assertTrue(
                expected_expression.is_equivalent_to(result_expression))
 def test_simple_multiplication_should_apply(self):
     exp = Expression('\\frac{d(2*x)}{dx}')
     expected = Expression('2*\\frac{d(x)}{dx}')
     result = theorem.apply_to(exp)
     self.assertEquals(len(result), 1)
     self.assertTrue(expected.is_equivalent_to(result[0]))
Exemplo n.º 4
0
    def test_compare(self):
        exp_one = Expression("x + x")
        exp_two = Expression("2 * x")

        self.assertTrue(exp_one.is_equivalent_to(exp_two))