示例#1
0
    def _print_Rational(self, e):
        """Print a Rational object.

        :param e: The expression.
        :rtype : bce.dom.mathml.all.Base
        :return: The printed MathML object.
        """

        assert isinstance(e, _sympy.Rational)

        if e.q == 1:
            #  Don't do division if the denominator is 1.
            return _mathml.NumberComponent(str(e.p))

        return _mathml.FractionComponent(_mathml.NumberComponent(str(e.p)),
                                         _mathml.NumberComponent(str(e.q)))
示例#2
0
    def _print_int(self, p):
        """Print an int object.

        :param p: The expression.
        :rtype : bce.dom.mathml.all.Base
        :return: The printed MathML object.
        """

        assert isinstance(p, int)

        return _mathml.NumberComponent(str(p))
示例#3
0
    def _print_Number(self, e):
        """Print a Number object.

        :param e: The expression.
        :rtype : bce.dom.mathml.all.Base
        :return: The printed MathML object.
        """

        assert isinstance(e, _sympy.Number)

        return _mathml.NumberComponent(str(e))
示例#4
0
def _print_operand(
        value,
        need_wrapping,
        mexp_parser,
        mexp_protected_header_enabled=False,
        mexp_protected_header_prefix="X"
):
    """Print an operand.

    :type need_wrapping: bool
    :type mexp_parser: bce.parser.interface.mexp_parser.MathExpressionParserInterface
    :type mexp_protected_header_enabled: bool
    :type mexp_protected_header_prefix: str
    :param value: The operand value.
    :param need_wrapping: Set to True if you need to wrap the expression when it is neither an integer nor a symbol.
    :param mexp_parser: The math expression parser.
    :param mexp_protected_header_enabled: Whether the MEXP protected headers are enabled.
    :param mexp_protected_header_prefix: The prefix of the MEXP protected headers.
    :rtype : bce.dom.mathml.all.Base
    :return: The printed MathML node.
    """

    #  Simplify.
    value = value.simplify()

    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 printed expression.
            r = _mathml.RowComponent()
            r.append_object(_mathml.OperatorComponent(_mathml.OPERATOR_LEFT_PARENTHESIS))
            r.append_object(mexp_parser.print_out(
                value,
                printer_type=_interface_printer.PRINTER_TYPE_MATHML,
                protected_header_enabled=mexp_protected_header_enabled,
                protected_header_prefix=mexp_protected_header_prefix
            ))
            r.append_object(_mathml.OperatorComponent(_mathml.OPERATOR_RIGHT_PARENTHESIS))
            return r
        else:
            return mexp_parser.print_out(
                value,
                printer_type=_interface_printer.PRINTER_TYPE_MATHML,
                protected_header_enabled=mexp_protected_header_enabled,
                protected_header_prefix=mexp_protected_header_prefix
            )
示例#5
0
    def _print_Pow(self, e):
        """Print a Pow object.

        :param e: The expression.
        :rtype : bce.dom.mathml.all.Base
        :return: The printed MathML object.
        """

        assert isinstance(e, _sympy.Pow)
        PREC = _sympy_precedence.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.RootComponent(
                    printed_base, _mathml.NumberComponent(str(e.exp.q)))
            else:
                #  Do SQRT operation.
                root = _mathml.SquareRootComponent(printed_base)

            return root

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

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

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

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

            return final_node

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

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