Пример #1
0
    def _print_Relational(self, expr):
        return "{0} {1} {2}".format(self.parenthesize(expr.lhs, _precedence(expr)),
                                    _relational_map_matlab[expr.rel_op],
                                    self.parenthesize(expr.rhs, _precedence(expr)))

        return '{0}({1}, {2})'.format(_relational_map_matlab[expr.rel_op],
                                      self._print(expr.lhs), self._print(expr.rhs))
Пример #2
0
 def _print_Relational(self, expr):
     if self._namespace == "ufl.":
         return 'ufl.{0}({1}, {2})'.format(_relational_map[expr.rel_op].lower(),
                                           self._print(expr.lhs), self._print(expr.rhs))
     return "{0} {1} {2}".format(self.parenthesize(expr.lhs, _precedence(expr)),
                                 expr.rel_op,
                                 self.parenthesize(expr.rhs, _precedence(expr)))
Пример #3
0
 def _print_Pow(self, expr, rational=False):
     PREC = _precedence(expr)
     if expr.exp.is_integer and int(expr.exp) == 1:
         return self.parenthesize(expr.base, PREC)
     if expr.exp is sp.S.NegativeOne:
         return "1.0/{0}".format(self.parenthesize(expr.base, PREC))
     if expr.exp.is_integer and int(expr.exp) in [2, 3]:
         return "({0})".format(\
             "*".join(self.parenthesize(expr.base, PREC) \
                      for i in xrange(int(expr.exp))), PREC)
     if expr.exp.is_integer and int(expr.exp) in [-2, -3]:
         return "1.0/({0})".format(\
             "*".join(self.parenthesize(expr.base, PREC) \
                      for i in xrange(-int(expr.exp))), PREC)
     if expr.exp is sp.S.Half and not rational:
         return "{0}sqrt({1})".format(self._namespace,
                                      self._print(expr.base))
     if expr.exp == -0.5:
         return "1/{0}sqrt({1})".format(self._namespace,
                                      self._print(expr.base))
     if self._namespace == "ufl.":
         return "{0}elem_pow({1}, {2})".format(self._namespace,
                                               self._print(expr.base),
                                               self._print(expr.exp))
     if self._namespace in ["np.", "numpy."]:
         return "{0}power({1}, {2})".format(self._namespace,
                                            self._print(expr.base),
                                            self._print(expr.exp))
     
     return "{0}pow({1}, {2})".format(self._namespace,
                                      self._print(expr.base),
                                      self._print(expr.exp))
Пример #4
0
 def _print_Or(self, expr):
     PREC = _precedence(expr)
     if self._namespace == "ufl.":
         if len(expr.args) != 2:
             error("UFL does not support more than 2 operands to Or")
         return "ufl.Or({0}, {1})".format(self._print(expr.args[0]),
                                          self._print(expr.args[1]))
     return "Or({0})".format(", ".join(self._print(arg) for arg in expr.args[::-1]))
Пример #5
0
def _print_Mul(self, expr):

    prec = _precedence(expr)
    
    if self.order not in ('old', 'none'):
        args = expr.as_ordered_factors()
    else:
        # use make_args in case expr was something like -x -> x
        args = sp.Mul.make_args(expr)

    args = tuple(args)

    if _coeff_isneg(expr):
        # If negative and -1 is the first arg: remove it
        if args[0].is_integer and int(args[0]) == 1:
            args = args[1:]
        else:
            args = (-args[0],) + args[1:]
        sign = "-"
    else:
        sign = ""
    
        # If first argument is Mul we do not want to add a parentesize
        if isinstance(args[0], sp.Mul):
            prec -= 1

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

    # 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(sp.Pow(item.base, -item.exp, evaluate=False))
            else:
                b.append(sp.Pow(item.base, -item.exp))
        elif item.is_Rational and item is not sp.S.Infinity:
            if item.p != 1:
                a.append(sp.Rational(item.p))
            if item.q != 1:
                b.append(sp.Rational(item.q))
        else:
            a.append(item)

    a = a or [sp.S.One]

    a_str = map(lambda x:self.parenthesize(x, prec), a)
    b_str = map(lambda x:self.parenthesize(x, prec), b)

    if len(b) == 0:
        return sign + '*'.join(a_str)
    elif len(b) == 1:
        if len(a) == 1 and not (a[0].is_Atom or a[0].is_Add):
            return sign + "{0}/".format(a_str[0]) + '*'.join(b_str)
        else:
            return sign + '*'.join(a_str) + "/{0}".format(b_str[0])
    else:
        return sign + '*'.join(a_str) + "/({0})".format('*'.join(b_str))
Пример #6
0
 def _print_And(self, expr):
     PREC = _precedence(expr)
     if self._namespace == "ufl.":
         if len(expr.args) != 2:
             error("UFL does not support more than 2 operands to And")
         return "ufl.And({0}, {1})".format(self._print(expr.args[0]),
                                           self._print(expr.args[1]))
     return " and ".join(self.parenthesize(arg, PREC) for arg in expr.args[::-1])
     return "{0} and {1}".format(self.parenthesize(expr.args[0], PREC),
                                 self.parenthesize(expr.args[1], PREC))
Пример #7
0
    def _print_Pow(self, expr):
        PREC = _precedence(expr)
        if expr.exp.is_integer and int(expr.exp) == 1:
            return self.parenthesize(expr.base, PREC)
        if expr.exp is sp.S.NegativeOne:
            return '1.0/{0}'.format(self.parenthesize(expr.base, PREC))
        
        if expr.exp == 0.5:
            return 'sqrt({0})'.format(self._print(expr.base))

        # FIXME: Fix paranthesises
        return '{0}^{1}'.format(self.parenthesize(expr.base, PREC),
                                  self.parenthesize(expr.exp, PREC))
Пример #8
0
 def _print_Or(self, expr):
     PREC = _precedence(expr)
     return " | ".join(self.parenthesize(arg, PREC) for arg in expr.args[::-1])
Пример #9
0
 def _print_Not(self, expr):
     PREC = _precedence(expr)
     return "~" + self.parenthesize(expr.args[0], PREC)