本文整理汇总了Python中sympy.printing.codeprinter.CodePrinter._print_Add方法的典型用法代码示例。如果您正苦于以下问题:Python CodePrinter._print_Add方法的具体用法?Python CodePrinter._print_Add怎么用?Python CodePrinter._print_Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.printing.codeprinter.CodePrinter
的用法示例。
在下文中一共展示了CodePrinter._print_Add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _print_Add
# 需要导入模块: from sympy.printing.codeprinter import CodePrinter [as 别名]
# 或者: from sympy.printing.codeprinter.CodePrinter import _print_Add [as 别名]
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
示例2: _print_Add
# 需要导入模块: from sympy.printing.codeprinter import CodePrinter [as 别名]
# 或者: from sympy.printing.codeprinter.CodePrinter import _print_Add [as 别名]
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)