Beispiel #1
0
    def get_cards(self, arguments, evaluator, evaluated):
        first_func_name = arguments[0]
        # is the top-level function call to a function such as factorint or
        # simplify?
        is_function = False
        # is the top-level function being called?
        is_applied = arguments.args or arguments.kwargs

        first_func = evaluator.get(first_func_name)
        is_function = (
            first_func and
            not isinstance(first_func, FunctionClass) and
            not isinstance(first_func, sympy.Atom) and
            first_func_name and first_func_name[0].islower() and
            not first_func_name in OTHER_SYMPY_FUNCTIONS)

        if is_applied:
            convert_input, cards = find_result_set(arguments[0], evaluated)
        else:
            convert_input, cards = find_result_set(None, evaluated)

        components = convert_input(arguments, evaluated)
        if 'input_evaluated' in components:
            evaluated = components['input_evaluated']

        evaluator.set('input_evaluated', evaluated)

        return components, cards, evaluated, (is_function and is_applied)
Beispiel #2
0
    def get_cards(self, arguments, evaluator, evaluated):
        first_func_name = arguments[0]
        # is the top-level function call to a function such as factorint or
        # simplify?
        is_function = False
        # is the top-level function being called?
        is_applied = arguments.args or arguments.kwargs

        first_func = evaluator.get(first_func_name)
        is_function = (first_func and not isinstance(first_func, FunctionClass)
                       and not isinstance(first_func, sympy.Atom)
                       and first_func_name and first_func_name[0].islower()
                       and not first_func_name in OTHER_SYMPY_FUNCTIONS)

        if is_applied:
            convert_input, cards = find_result_set(arguments[0], evaluated)
        else:
            convert_input, cards = find_result_set(None, evaluated)

        components = convert_input(arguments, evaluated)
        if 'input_evaluated' in components:
            evaluated = components['input_evaluated']

        evaluator.set('input_evaluated', evaluated)

        return components, cards, evaluated, (is_function and is_applied)
Beispiel #3
0
    def try_sympy(self, s):
        namespace = {}
        exec PREEXEC in {}, namespace
        a = Eval(namespace)
        # change to True to spare the user from exceptions:
        if not len(s):
            return
        try:
            evaluated = sympify(s,
                                convert_xor=True,
                                locals={
                                    'integrate': sympy.Integral,
                                    'plot': lambda func: func
                                })
            input_repr = repr(evaluated)
            namespace['input_evaluated'] = evaluated
        except sympy_parser.TokenError:
            return [{
                "title": "Input",
                "input": s
            }, {
                "title": "Error",
                "input": s,
                "error": "Invalid input"
            }]
        except Exception as e:
            return self.handle_error(s, e)

        if input_repr is not None:
            result = [
                {
                    "title": "Input",
                    "input": s
                },
                {
                    "title": "SymPy",
                    "input": s,
                    "output": input_repr
                },
            ]

            if isinstance(evaluated, sympy.Basic):
                variables = evaluated.atoms(Symbol)
                if len(variables) == 1:
                    var = variables.pop()
                else:
                    var = None
            else:
                var = None

            convert_input, cards = find_result_set(evaluated)
            namespace['input_evaluated'], var = convert_input(evaluated, var)

            # Come up with a solution to use all variables if more than 1
            # is entered.
            if var != None:  # See a better way to do this.
                input_repr = repr(namespace['input_evaluated'])
                line = "simplify(input_evaluated)"
                simplified = a.eval(line, use_none_for_exceptions=True)
                r = sympify(a.eval(line, use_none_for_exceptions=True))

                if simplified != "None" and simplified != input_repr:
                    result.append({
                        "title": "Simplification",
                        "input": simplified,
                        "output": mathjax_latex(r)
                    })

                for card in cards:
                    try:
                        r = card.eval(a, var)
                        if r != "None":
                            formatted_input = card.format_input(
                                input_repr, var)
                            result.append(
                                dict(title=card.title,
                                     input=formatted_input,
                                     pre_output=latex(
                                         card.pre_output_function(
                                             input_repr, var)),
                                     output=card.format_output(
                                         r, mathjax_latex)))
                    except SyntaxError:
                        pass
            return result
        else:
            return None
Beispiel #4
0
    def try_sympy(self, s):
        namespace = {}
        exec PREEXEC in {}, namespace
        a = Eval(namespace)
        # change to True to spare the user from exceptions:
        if not len(s):
            return
        try:
            evaluated = sympify(s, convert_xor=True, locals={
                'integrate': sympy.Integral,
                'plot': lambda func: func
            })
            input_repr = repr(evaluated)
            namespace['input_evaluated'] = evaluated
        except sympy_parser.TokenError:
            return [
                {"title": "Input", "input": s},
                {"title": "Error", "input": s, "error": "Invalid input"}
            ]
        except Exception as e:
            return self.handle_error(s, e)

        if input_repr is not None:
            result = [
                {"title": "Input", "input": s},
                {"title": "SymPy", "input": s, "output": input_repr},
            ]

            if isinstance(evaluated, sympy.Basic):
                variables = evaluated.atoms(Symbol)
                if len(variables) == 1:
                    var = variables.pop()
                else:
                    var = None
            else:
                var = None

            convert_input, cards = find_result_set(evaluated)
            namespace['input_evaluated'], var = convert_input(evaluated, var)

            # Come up with a solution to use all variables if more than 1
            # is entered.
            if var != None:  # See a better way to do this.
                input_repr = repr(namespace['input_evaluated'])
                line = "simplify(input_evaluated)"
                simplified = a.eval(line, use_none_for_exceptions=True)
                r = sympify(a.eval(line, use_none_for_exceptions=True))

                if simplified != "None" and simplified != input_repr:
                    result.append(
                        {"title": "Simplification", "input": simplified,
                         "output": mathjax_latex(r)})

                for card in cards:
                    try:
                        r = card.eval(a, var)
                        if r != "None":
                            formatted_input = card.format_input(input_repr, var)
                            result.append(dict(
                                title=card.title,
                                input=formatted_input,
                                pre_output=latex(
                                    card.pre_output_function(input_repr, var)),
                                output=card.format_output(r, mathjax_latex)
                            ))
                    except SyntaxError:
                        pass
            return result
        else:
            return None