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
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)
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)
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) if pos: s = pos = reduce(lambda a,b: add(a,b), map(lambda t: self._print(t),pos)) else: s = pos = self._print(self._settings['zero']) if neg: # 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