Exemple #1
0
    def _print_Rational(self, e):
        if e.q == 1:
            #  Don't do division if the denominator is 1.
            return _mathml_comp.NumberComponent(str(e.p))

        return _mathml_comp.FractionComponent(
            _mathml_comp.NumberComponent(str(e.p)),
            _mathml_comp.NumberComponent(str(e.q)))
Exemple #2
0
def _decompile_operand(value, need_wrapping, options):
    """Decompile an operand.

    :type need_wrapping: bool
    :type options: _opt.Option
    :param value: The operand value (must be simplified).
    :param need_wrapping: Set to True if you need to wrap the expression when it is neither an integer nor a symbol.
                          Otherwise, set to False.
    :param options: The BCE options.
    :return: The decompiled DOM node.
    """

    if value.is_Integer:
        return _mathml.NumberComponent(str(value))
    else:
        if need_wrapping and not (value.is_Integer or value.is_Symbol):
            #  Use a pair of parentheses to wrap the decompiled expression.
            r = _mathml.RowComponent()
            r.append_object(_mathml.OperatorComponent(_mathml.OPERATOR_LEFT_PARENTHESIS))
            r.append_object(_mexp_decompiler.decompile_mexp(value, options.get_protected_math_symbol_header()))
            r.append_object(_mathml.OperatorComponent(_mathml.OPERATOR_RIGHT_PARENTHESIS))
            return r
        else:
            return _mexp_decompiler.decompile_mexp(value, options.get_protected_math_symbol_header())
Exemple #3
0
 def _print_int(self, p):
     return _mathml_comp.NumberComponent(str(p))
Exemple #4
0
 def _print_Number(self, e):
     return _mathml_comp.NumberComponent(str(e))
Exemple #5
0
    def _print_Pow(self, e):
        PREC = precedence(e)

        if e.exp.is_Rational and e.exp.p == 1:
            #  If the exponent is like {1/x}, do SQRT operation if x is 2, otherwise, do
            #  root operation.
            printed_base = self._print(e.base)

            if e.exp.q != 2:
                #  Do root operation.
                root = _mathml_comp.RootComponent(
                    printed_base, _mathml_comp.NumberComponent(str(e.exp.q)))
            else:
                #  Do SQRT operation.
                root = _mathml_comp.SquareRootComponent(printed_base)

            return root

        if e.exp.is_negative:
            if e.exp.is_Integer and e.exp == Integer(-1):
                final_node = _mathml_comp.FractionComponent(
                    _mathml_comp.NumberComponent("1"), self._print(e.base))
            else:
                #  frac{1, base ^ |exp|}
                neg_exp = -e.exp

                #  Get node for the base.
                if precedence(e.base) < PREC:
                    base_node = _mathml_comp.RowComponent()
                    base_node.append_object(
                        _mathml_comp.OperatorComponent(
                            _mathml_comp.OPERATOR_LEFT_PARENTHESIS))
                    base_node.append_object(self._print(e.base))
                    base_node.append_object(
                        _mathml_comp.OperatorComponent(
                            _mathml_comp.OPERATOR_RIGHT_PARENTHESIS))
                else:
                    base_node = self._print(e.base)

                #  Get node for the exponent.
                if precedence(neg_exp) < PREC:
                    exp_node = _mathml_comp.RowComponent()
                    exp_node.append_object(
                        _mathml_comp.OperatorComponent(
                            _mathml_comp.OPERATOR_LEFT_PARENTHESIS))
                    exp_node.append_object(self._print(neg_exp))
                    exp_node.append_object(
                        _mathml_comp.OperatorComponent(
                            _mathml_comp.OPERATOR_RIGHT_PARENTHESIS))
                else:
                    exp_node = neg_exp

                final_node = _mathml_comp.FractionComponent(
                    _mathml_comp.NumberComponent("1"),
                    _mathml_comp.SuperComponent(base_node, exp_node))

            return final_node

        #  Get node for the base.
        if precedence(e.base) < PREC:
            base_node = _mathml_comp.RowComponent()
            base_node.append_object(
                _mathml_comp.OperatorComponent(
                    _mathml_comp.OPERATOR_LEFT_PARENTHESIS))
            base_node.append_object(self._print(e.base))
            base_node.append_object(
                _mathml_comp.OperatorComponent(
                    _mathml_comp.OPERATOR_RIGHT_PARENTHESIS))
        else:
            base_node = self._print(e.base)

        return _mathml_comp.SuperComponent(base_node, self._print(e.exp))