Ejemplo n.º 1
0
Archivo: rust.py Proyecto: bjodah/sympy
 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)
Ejemplo n.º 2
0
 def __init__(self, settings={}):
     """Register function mappings supplied by user"""
     CodePrinter.__init__(self, settings)
     self.known_functions = dict(known_functions)
     userfuncs = settings.get('user_functions', {})
     for k, v in userfuncs.items():
         if not isinstance(v, list):
             userfuncs[k] = [(lambda *x: True, v)]
     self.known_functions.update(userfuncs)
Ejemplo n.º 3
0
 def __init__(self, settings=None):
     CodePrinter.__init__(self, settings)
     self._init_leading_padding()
     assign_to = self._settings['assign_to']
     if isinstance(assign_to, basestring):
         self._settings['assign_to'] = C.Symbol(assign_to)
     elif not isinstance(assign_to, (C.Basic, type(None))):
         raise TypeError("FCodePrinter cannot assign to object of type %s" %
                 type(assign_to))
Ejemplo n.º 4
0
 def _print_Float(self, expr):
     if expr == float("inf"):
         return "1e200"
     elif expr == float("-inf"):
         return "-1e200"
     else:
         return CodePrinter._print_Float(self, expr)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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))
Ejemplo n.º 7
0
 def _print_Function(self, expr):
     if expr.func.__name__ in self.known_functions:
         cond_cfunc = self.known_functions[expr.func.__name__]
         for cond, cfunc in cond_cfunc:
             if cond(*expr.args):
                 return "%s(%s)" % (cfunc, self.stringify(expr.args, ", "))
     if hasattr(expr, '_imp_') and isinstance(expr._imp_, C.Lambda):
         # inlined function
         return self._print(expr._imp_(*expr.args))
     return CodePrinter._print_Function(self, expr)
Ejemplo n.º 8
0
Archivo: glsl.py Proyecto: cklb/sympy
    def _print_Mul(self, expr, **kwargs):
        if(self._settings['use_operators']):
            return CodePrinter._print_Mul(self, expr, **kwargs)
        terms = expr.as_ordered_factors()
        def mul(a,b):
            # return self.known_functions['mul']+'(%s, %s)' % (a,b)
            return self._print_Function_with_args('mul', (a,b))

        s = reduce(lambda a,b: mul(a,b), map(lambda t: self._print(t), terms))
        return s
Ejemplo 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)
     # leading columns depend on fixed or free format
     if self._settings['source_format'] == 'fixed':
         self._lead_code = "      "
         self._lead_cont = "     @ "
         self._lead_comment = "C     "
     elif self._settings['source_format'] == 'free':
         self._lead_code = ""
         self._lead_cont = "      "
         self._lead_comment = "! "
     else:
         raise ValueError("Unknown source format: %s" % self._settings[
                          'source_format'])
     standards = set([66, 77, 90, 95, 2003, 2008])
     if self._settings['standard'] not in standards:
         raise ValueError("Unknown Fortran standard: %s" % self._settings[
                          'standard'])
Ejemplo n.º 10
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.º 11
0
Archivo: glsl.py Proyecto: cklb/sympy
    def _print_Add(self, expr, order=None):
        if(self._settings['use_operators']):
            return CodePrinter._print_Add(self, expr, order=order)

        terms = expr.as_ordered_terms()

        def partition(p,l):
            return reduce(lambda x, y: (x[0]+[y], x[1]) if p(y) else (x[0], x[1]+[y]), l,  ([], []))
        def add(a,b):
            return self._print_Function_with_args('add', (a, b))
            # return self.known_functions['add']+'(%s, %s)' % (a,b)
        neg, pos = partition(lambda arg: _coeff_isneg(arg), terms)
        s = pos = reduce(lambda a,b: add(a,b), map(lambda t: self._print(t),pos))
        if(len(neg) > 0):
            # sum the absolute values of the negative terms
            neg = reduce(lambda a,b: add(a,b), map(lambda n: self._print(-n),neg))
            # then subtract them from the positive terms
            s = self._print_Function_with_args('sub', (pos,neg))
            # s = self.known_functions['sub']+'(%s, %s)' % (pos,neg)
        return s
Ejemplo n.º 12
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)
Ejemplo n.º 13
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
Ejemplo n.º 14
0
 def __init__(self, **kwargs):
     """Register function mappings supplied by user"""
     CodePrinter.__init__(self, kwargs)
     self.known_functions = dict(known_functions)
Ejemplo n.º 15
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
Ejemplo n.º 16
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)
Ejemplo n.º 17
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)
Ejemplo n.º 18
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)
Ejemplo n.º 19
0
 def __init__(self, **kwargs):
     """Register function mappings supplied by user"""
     CodePrinter.__init__(self, kwargs)
     self.known_functions = dict(known_functions)
Ejemplo n.º 20
0
def setup_test_printer(*args, **kwargs):
    p = CodePrinter(*args, **kwargs)
    p._not_supported = set()
    p._number_symbols = set()
    return p
def setup_test_printer(**kwargs):
    p = CodePrinter(settings=kwargs)
    p._not_supported = set()
    p._number_symbols = set()
    return p