Ejemplo n.º 1
0
def test_Number():
    assert precedence(Integer(0)) == PRECEDENCE["Atom"]
    assert precedence(Integer(1)) == PRECEDENCE["Atom"]
    assert precedence(Integer(-1)) == PRECEDENCE["Add"]
    assert precedence(Integer(10)) == PRECEDENCE["Atom"]
    assert precedence(Rational(5, 2)) == PRECEDENCE["Mul"]
    assert precedence(Rational(-5, 2)) == PRECEDENCE["Add"]
    assert precedence(Float(5)) == PRECEDENCE["Atom"]
    assert precedence(Float(-5)) == PRECEDENCE["Add"]
    assert precedence(oo) == PRECEDENCE["Atom"]
    assert precedence(-oo) == PRECEDENCE["Add"]
Ejemplo n.º 2
0
def test_Number():
    assert precedence(Integer(0)) == PRECEDENCE["Atom"]
    assert precedence(Integer(1)) == PRECEDENCE["Atom"]
    assert precedence(Integer(-1)) == PRECEDENCE["Add"]
    assert precedence(Integer(10)) == PRECEDENCE["Atom"]
    assert precedence(Rational(5, 2)) == PRECEDENCE["Mul"]
    assert precedence(Rational(-5, 2)) == PRECEDENCE["Add"]
    assert precedence(Float(5)) == PRECEDENCE["Atom"]
    assert precedence(Float(-5)) == PRECEDENCE["Add"]
    assert precedence(oo) == PRECEDENCE["Atom"]
    assert precedence(-oo) == PRECEDENCE["Add"]
Ejemplo n.º 3
0
def test_Number():
    assert precedence(Integer(0)) == PRECEDENCE['Atom']
    assert precedence(Integer(1)) == PRECEDENCE['Atom']
    assert precedence(Integer(-1)) == PRECEDENCE['Add']
    assert precedence(Integer(10)) == PRECEDENCE['Atom']
    assert precedence(Rational(5, 2)) == PRECEDENCE['Mul']
    assert precedence(Rational(-5, 2)) == PRECEDENCE['Add']
    assert precedence(Float(5)) == PRECEDENCE['Atom']
    assert precedence(Float(-5)) == PRECEDENCE['Add']
    assert precedence(oo) == PRECEDENCE['Atom']
    assert precedence(-oo) == PRECEDENCE['Add']
Ejemplo n.º 4
0
    def _print_Relational(self, expr):

        charmap = {
            "==": "Eq",
            "!=": "Ne",
        }

        if expr.rel_op in charmap:
            return '%s(%s, %s)' % (charmap[expr.rel_op], expr.lhs, expr.rhs)

        return '%s %s %s' % (self.parenthesize(expr.lhs, precedence(expr)),
                             self._relationals.get(expr.rel_op) or expr.rel_op,
                             self.parenthesize(expr.rhs, precedence(expr)))
Ejemplo n.º 5
0
 def _print_Mul(self, expr):
     PREC = precedence(expr)
     c, nc = expr.args_cnc()
     res = super(MCodePrinter, self)._print_Mul(expr.func(*c))
     if nc:
         res += '*'
         res += '**'.join(self.parenthesize(a, PREC) for a in nc)
     return res
Ejemplo n.º 6
0
 def _print_Pow(self, expr):
     PREC = precedence(expr)
     if expr.exp == -1:
         return '1/%s' % (self.parenthesize(expr.base, PREC))
     elif expr.exp == 0.5:
         return 'Math.sqrt(%s)' % self._print(expr.base)
     else:
         return 'Math.pow(%s, %s)' % (self._print(
             expr.base), self._print(expr.exp))
Ejemplo n.º 7
0
    def _print_Add(self, expr):
        # purpose: print complex numbers nicely in Fortran.
        # collect the purely real and purely imaginary parts:
        pure_real = []
        pure_imaginary = []
        mixed = []
        for arg in expr.args:
            if arg.is_number and arg.is_extended_real:
                pure_real.append(arg)
            elif arg.is_number and arg.is_imaginary:
                pure_imaginary.append(arg)
            else:
                mixed.append(arg)
        if len(pure_imaginary) > 0:
            if len(mixed) > 0:
                PREC = precedence(expr)
                term = Add(*mixed)
                t = self._print(term)
                if t.startswith('-'):
                    sign = "-"
                    t = t[1:]
                else:
                    sign = "+"
                if precedence(term) < PREC:
                    t = "(%s)" % t

                return "cmplx(%s,%s) %s %s" % (
                    self._print(Add(*pure_real)),
                    self._print(-S.ImaginaryUnit * Add(*pure_imaginary)),
                    sign,
                    t,
                )
            else:
                return "cmplx(%s,%s)" % (
                    self._print(Add(*pure_real)),
                    self._print(-S.ImaginaryUnit * Add(*pure_imaginary)),
                )
        else:
            return CodePrinter._print_Add(self, expr)
Ejemplo n.º 8
0
    def _print_Add(self, expr, order=None):
        if self.order == 'none':
            terms = list(expr.args)
        else:
            terms = expr.as_ordered_terms(order=order or self.order)

        PREC = precedence(expr)
        l = []
        for term in terms:
            t = self._print(term)
            if t.startswith('-'):
                sign = "-"
                t = t[1:]
            else:
                sign = "+"
            if precedence(term) < PREC:
                l.extend([sign, "(%s)" % t])
            else:
                l.extend([sign, t])
        sign = l.pop(0)
        if sign == '+':
            sign = ""
        return sign + ' '.join(l)
Ejemplo n.º 9
0
 def _print_Pow(self, expr):
     PREC = precedence(expr)
     if expr.exp == -1:
         return '1.0/%s' % (self.parenthesize(expr.base, PREC))
     elif expr.exp == 0.5:
         if expr.base.is_integer:
             # Fortan intrinsic sqrt() does not accept integer argument
             if expr.base.is_Number:
                 return 'sqrt(%s.0d0)' % self._print(expr.base)
             else:
                 return 'sqrt(dble(%s))' % self._print(expr.base)
         else:
             return 'sqrt(%s)' % self._print(expr.base)
     else:
         return CodePrinter._print_Pow(self, expr)
Ejemplo n.º 10
0
    def _print_Mul(self, expr):

        prec = precedence(expr)

        c, e = expr.as_coeff_Mul()
        if c < 0:
            expr = _keep_coeff(-c, e)
            sign = "-"
        else:
            sign = ""

        a = []  # items in the numerator
        b = []  # items that are in the denominator (if any)

        if self.order != 'none':
            args = expr.as_ordered_factors()
        else:
            # use make_args in case expr was something like -x -> x
            args = Mul.make_args(expr)

        multiple_ones = len([x for x in args if x == S.One]) > 1

        # Gather args for numerator/denominator
        for item in args:
            if item.is_commutative and item.is_Pow and item.exp.is_Rational and item.exp.is_negative:
                if item.exp != -1:
                    b.append(Pow(item.base, -item.exp, evaluate=False))
                else:
                    b.append(Pow(item.base, -item.exp))
            elif item.is_Rational and item is not S.Infinity:
                if item.p != 1 or multiple_ones:
                    a.append(Rational(item.p))
                if item.q != 1:
                    b.append(Rational(item.q))
            else:
                a.append(item)

        a = a or [S.One]

        a_str = [self.parenthesize(x, prec) for x in a]
        b_str = [self.parenthesize(x, prec) for x in b]

        if len(b) == 0:
            return sign + '*'.join(a_str)
        elif len(b) == 1:
            return sign + '*'.join(a_str) + "/" + b_str[0]
        else:
            return sign + '*'.join(a_str) + "/(%s)" % '*'.join(b_str)
Ejemplo n.º 11
0
    def _print_Pow(self, expr, rational=False):
        PREC = precedence(expr)

        if expr.exp is S.Half and not rational:
            return "sqrt(%s)" % self._print(expr.base)

        if expr.is_commutative:
            if -expr.exp is S.Half and not rational:
                # Note: Don't test "expr.exp == -S.Half" here, because that will
                # match -0.5, which we don't want.
                return "1/sqrt(%s)" % self._print(expr.base)
            if expr.exp is -S.One:
                # Similarly to the S.Half case, don't test with "==" here.
                return '1/%s' % self.parenthesize(expr.base, PREC)

        e = self.parenthesize(expr.exp, PREC)
        if self.printmethod == '_diofantrepr' and expr.exp.is_Rational and expr.exp.q != 1:
            # the parenthesized exp should be '(Rational(a, b))' so strip parens,
            # but just check to be sure.
            if e.startswith('(Rational'):
                return '%s**%s' % (self.parenthesize(expr.base, PREC), e[1:-1])
        return '%s**%s' % (self.parenthesize(expr.base, PREC), e)
Ejemplo n.º 12
0
def test_And_Or():
    # precendence relations between logical operators, ...
    assert precedence(x & y) > precedence(x | y)
    assert precedence(~y) > precedence(x & y)
    # ... and with other operators (cfr. other programming languages)
    assert precedence(x + y) > precedence(x | y)
    assert precedence(x + y) > precedence(x & y)
    assert precedence(x * y) > precedence(x | y)
    assert precedence(x * y) > precedence(x & y)
    assert precedence(~y) > precedence(x * y)
    assert precedence(~y) > precedence(x - y)
    # double checks
    assert precedence(x & y) == PRECEDENCE['And']
    assert precedence(x | y) == PRECEDENCE['Or']
    assert precedence(~y) == PRECEDENCE['Not']
Ejemplo n.º 13
0
def test_Product():
    assert precedence(Product(x, (x, y, y + 1))) == PRECEDENCE['Atom']
Ejemplo n.º 14
0
def test_Sum():
    assert precedence(Sum(x, (x, y, y + 1))) == PRECEDENCE['Atom']
Ejemplo n.º 15
0
def test_Mul():
    assert precedence(x * y) == PRECEDENCE['Mul']
    assert precedence(-x * y) == PRECEDENCE['Add']
Ejemplo n.º 16
0
def test_Order():
    assert precedence(Order(x)) == PRECEDENCE['Atom']
Ejemplo n.º 17
0
def test_Relational():
    assert precedence(Rel(x + y, y, "<")) == PRECEDENCE["Relational"]
Ejemplo n.º 18
0
def test_Derivative():
    assert precedence(Derivative(x, y)) == PRECEDENCE['Atom']
Ejemplo n.º 19
0
 def _print_HadamardProduct(self, expr):
     return '.*'.join(
         [self.parenthesize(arg, precedence(expr)) for arg in expr.args])
Ejemplo n.º 20
0
 def _print_MatAdd(self, expr):
     return ' + '.join(
         [self.parenthesize(arg, precedence(expr)) for arg in expr.args])
Ejemplo n.º 21
0
def test_And_Or():
    # precendence relations between logical operators, ...
    assert precedence(x & y) > precedence(x | y)
    assert precedence(~y) > precedence(x & y)
    # ... and with other operators (cfr. other programming languages)
    assert precedence(x + y) > precedence(x | y)
    assert precedence(x + y) > precedence(x & y)
    assert precedence(x*y) > precedence(x | y)
    assert precedence(x*y) > precedence(x & y)
    assert precedence(~y) > precedence(x*y)
    assert precedence(~y) > precedence(x - y)
    # double checks
    assert precedence(x & y) == PRECEDENCE["And"]
    assert precedence(x | y) == PRECEDENCE["Or"]
    assert precedence(~y) == PRECEDENCE["Not"]
Ejemplo n.º 22
0
def test_Order():
    assert precedence(Order(x)) == PRECEDENCE["Atom"]
Ejemplo n.º 23
0
def test_Symbol():
    assert precedence(x) == PRECEDENCE["Atom"]
Ejemplo n.º 24
0
def test_Sum():
    assert precedence(Sum(x, (x, y, y + 1))) == PRECEDENCE["Atom"]
Ejemplo n.º 25
0
def test_Relational():
    assert precedence(Rel(x + y, y, "<")) == PRECEDENCE["Relational"]
Ejemplo n.º 26
0
def test_Product():
    assert precedence(Product(x, (x, y, y + 1))) == PRECEDENCE["Atom"]
Ejemplo n.º 27
0
def test_Mul():
    assert precedence(x*y) == PRECEDENCE["Mul"]
    assert precedence(-x*y) == PRECEDENCE["Add"]
Ejemplo n.º 28
0
def test_Pow():
    assert precedence(x**y) == PRECEDENCE["Pow"]
    assert precedence(-x**y) == PRECEDENCE["Add"]
    assert precedence(x**-y) == PRECEDENCE["Pow"]
Ejemplo n.º 29
0
def test_Integral():
    assert precedence(Integral(x, y)) == PRECEDENCE["Atom"]
Ejemplo n.º 30
0
def test_Function():
    assert precedence(sin(x)) == PRECEDENCE['Func']
Ejemplo n.º 31
0
    def _print_Mod(self, expr):
        PREC = precedence(expr)

        a, b = expr.args
        return '%s%%%s' % (self.parenthesize(a,
                                             PREC), self.parenthesize(b, PREC))
Ejemplo n.º 32
0
def test_Integral():
    assert precedence(Integral(x, y)) == PRECEDENCE['Atom']
Ejemplo n.º 33
0
 def _print_MatPow(self, expr):
     PREC = precedence(expr)
     return '%s**%s' % (self.parenthesize(
         expr.base, PREC), self.parenthesize(expr.exp, PREC))
Ejemplo n.º 34
0
def test_Add():
    assert precedence(x + y) == PRECEDENCE["Add"]
    assert precedence(x*y + 1) == PRECEDENCE["Add"]
Ejemplo n.º 35
0
def test_Derivative():
    assert precedence(Derivative(x, y)) == PRECEDENCE["Atom"]
Ejemplo n.º 36
0
def test_Pow():
    assert precedence(x**y) == PRECEDENCE['Pow']
    assert precedence(-x**y) == PRECEDENCE['Add']
    assert precedence(x**-y) == PRECEDENCE['Pow']
Ejemplo n.º 37
0
def test_Function():
    assert precedence(sin(x)) == PRECEDENCE["Atom"]
    assert precedence(Derivative(x, y)) == PRECEDENCE["Atom"]
Ejemplo n.º 38
0
def test_Relational():
    assert precedence(Rel(x + y, y, '<')) == PRECEDENCE['Relational']
Ejemplo n.º 39
0
def test_Mul():
    assert precedence(x * y) == PRECEDENCE["Mul"]
    assert precedence(-x * y) == PRECEDENCE["Add"]
Ejemplo n.º 40
0
def test_Symbol():
    assert precedence(x) == PRECEDENCE['Atom']
Ejemplo n.º 41
0
def test_Function():
    assert precedence(sin(x)) == PRECEDENCE["Func"]
Ejemplo n.º 42
0
def test_Add():
    assert precedence(x + y) == PRECEDENCE['Add']
    assert precedence(x * y + 1) == PRECEDENCE['Add']
Ejemplo n.º 43
0
def test_Pow():
    assert precedence(x**y) == PRECEDENCE["Pow"]
    assert precedence(-x**y) == PRECEDENCE["Add"]
    assert precedence(x**-y) == PRECEDENCE["Pow"]