Exemplo n.º 1
0
 def __init__(self, settings={}):
     CodePrinter.__init__(self, settings)
     self.known_functions = dict(known_functions)
     userfuncs = settings.get('user_functions', {})
     self.known_functions.update(userfuncs)
     self._dereference = set(settings.get('dereference', []))
     self.reserved_words = set(reserved_words)
     self.local_vars = settings.get('local_vars', {})
Exemplo n.º 2
0
 def _print_Mul(self, expr):
     # purpose: print complex numbers nicely in Fortran.
     if expr.is_number and expr.is_imaginary:
         return "cmplx(0,%s)" % (
             self._print(-S.ImaginaryUnit*expr)
         )
     else:
         return CodePrinter._print_Mul(self, expr)
Exemplo n.º 3
0
 def _print_Function(self, expr):
     # All constant function args are evaluated as floats
     prec =  self._settings['precision']
     args = [N(a, prec) for a in expr.args]
     eval_expr = expr.func(*args)
     if not isinstance(eval_expr, C.Function):
         return self._print(eval_expr)
     else:
         return CodePrinter._print_Function(self, expr.func(*args))
Exemplo n.º 4
0
 def _print_Function(self, expr):
     # All constant function args are evaluated as floats
     prec = self._settings['precision']
     args = [N(a, prec) for a in expr.args]
     eval_expr = expr.func(*args)
     if not isinstance(eval_expr, Function):
         return self._print(eval_expr)
     else:
         return CodePrinter._print_Function(self, expr.func(*args))
Exemplo n.º 5
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)
Exemplo n.º 6
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)
Exemplo 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_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)
Exemplo n.º 8
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_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)
Exemplo n.º 9
0
 def __init__(self, settings={}):
     CodePrinter.__init__(self, settings)
     self.known_functions = dict(known_functions)
     userfuncs = settings.get('user_functions', {})
     self.known_functions.update(userfuncs)
Exemplo n.º 10
0
 def _print_Float(self, expr):
     printed = CodePrinter._print_Float(self, expr)
     e = printed.find('e')
     if e > -1:
         return "%sd%s" % (printed[:e], printed[e + 1:])
     return "%sd0" % printed
Exemplo n.º 11
0
 def __init__(self, settings={}):
     CodePrinter.__init__(self, settings)
     self.known_functions = dict(known_functions)
     userfuncs = settings.get('user_functions', {})
     self.known_functions.update(userfuncs)
Exemplo n.º 12
0
 def _print_Float(self, expr):
     printed = CodePrinter._print_Float(self, expr)
     e = printed.find('e')
     if e > -1:
         return "%sd%s" % (printed[:e], printed[e + 1:])
     return "%sd0" % printed
Exemplo n.º 13
0
 def _print_Mul(self, expr):
     # purpose: print complex numbers nicely in Fortran.
     if expr.is_number and expr.is_imaginary:
         return "cmplx(0,%s)" % (self._print(-S.ImaginaryUnit * expr))
     else:
         return CodePrinter._print_Mul(self, expr)