本文整理汇总了Python中sympy.printing.precedence.precedence函数的典型用法代码示例。如果您正苦于以下问题:Python precedence函数的具体用法?Python precedence怎么用?Python precedence使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了precedence函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _print_Add
def _print_Add(self, expr):
args = list(expr.args)
# Now we need to sort the factors in Add, which are in "rest". Any
# ordering is fine, but some ordering looks better and some looks bad.
# This particular solution is slow, but it ensures a sane ordering. It
# can of course be improved:
args.sort(Basic._compare_pretty)
PREC = precedence(expr)
l = []
for term in args:
t = self._print(term)
if t.startswith('-'):
sign = "-"
t = t[1:]
else:
sign = "+"
if precedence(term) < PREC:
l.extend([sign, "(%s)"%t])
else:
l.extend([sign, t])
sign = l.pop(0)
if sign=='+':
sign = ""
return sign + ' '.join(l)
开发者ID:fperez,项目名称:sympy,代码行数:26,代码来源:str.py
示例2: test_Number
def test_Number():
assert precedence(Integer(0)) == PRECEDENCE["Atom"]
assert precedence(Integer(1)) == PRECEDENCE["Atom"]
assert precedence(Integer(-1)) == PRECEDENCE["Add"]
assert precedence(Integer(10)) == PRECEDENCE["Atom"]
assert precedence(Rational(5, 2)) == PRECEDENCE["Mul"]
assert precedence(Rational(-5, 2)) == PRECEDENCE["Add"]
assert precedence(Float(5)) == PRECEDENCE["Atom"]
assert precedence(Float(-5)) == PRECEDENCE["Add"]
assert precedence(oo) == PRECEDENCE["Atom"]
assert precedence(-oo) == PRECEDENCE["Add"]
开发者ID:AALEKH,项目名称:sympy,代码行数:11,代码来源:test_precedence.py
示例3: _print_Relational
def _print_Relational(self, expr):
charmap = {"==": "Eq", "!=": "Ne", ":=": "Assignment"}
if expr.rel_op in charmap:
return "%s(%s, %s)" % (charmap[expr.rel_op], expr.lhs, expr.rhs)
return "%s %s %s" % (
self.parenthesize(expr.lhs, precedence(expr)),
self._relationals.get(expr.rel_op) or expr.rel_op,
self.parenthesize(expr.rhs, precedence(expr)),
)
开发者ID:MooVI,项目名称:sympy,代码行数:12,代码来源:str.py
示例4: _print_Pow
def _print_Pow(self, expr):
if expr.exp == 2:
PREC = precedence(expr)
s = str(self.parenthesize(expr.base, PREC))
return '%s*%s' % (s,s)
else:
return super(CCodePrinter,self)._print_Pow(expr)
开发者ID:memmett,项目名称:PyWENO,代码行数:7,代码来源:codeprinters.py
示例5: parenthesize
def parenthesize(self, item, level):
printed = self._print(item)
if precedence(item) <= level:
return "(%s)" % printed
else:
return printed
开发者ID:asmeurer,项目名称:scipy-2011-tutorial,代码行数:7,代码来源:mathematica.py
示例6: _print_Pow
def _print_Pow(self, expr):
PREC = precedence(expr)
if expr.exp is S.NegativeOne:
return '1/%s'%(self.parenthesize(expr.base, PREC))
else:
return '%s**%s'%(self.parenthesize(expr.base, PREC),
self.parenthesize(expr.exp, PREC))
开发者ID:fperez,项目名称:sympy,代码行数:7,代码来源:str.py
示例7: _print_Pow
def _print_Pow(self, expr, rational=False):
PREC = precedence(expr)
#if expr.base is RootOfUnity:
# return 'Exp['+str(2*expr.base.n)+ 'I Pi /'+ str(expr.base.n) + ']'
if expr.exp is S.Half and not rational:
return "sqrt(%s)" % self._print(expr.base)
if expr.is_commutative:
if -expr.exp is S.Half and not rational:
# Note: Don't test "expr.exp == -S.Half" here, because that will
# match -0.5, which we don't want.
return "1/sqrt(%s)" % self._print(expr.base)
if expr.exp is -S.One:
# Similarly to the S.Half case, don't test with "==" here.
return '1/%s' % self.parenthesize(expr.base, PREC)
e = self.parenthesize(expr.exp, PREC)
if self.printmethod == '_sympyrepr' and expr.exp.is_Rational and expr.exp.q != 1:
# the parenthesized exp should be '(Rational(a, b))' so strip parens,
# but just check to be sure.
if e.startswith('(Rational'):
return '%s^%s' % (self.parenthesize(expr.base, PREC), e[1:-1])
return '%s^%s' % (self.parenthesize(expr.base, PREC), e)
开发者ID:MooVI,项目名称:commutator,代码行数:25,代码来源:mathematica_printer.py
示例8: _print_Pow
def _print_Pow(self, expr, rational=False):
# WARNING: Code mostly copied from sympy source code!
from sympy.core import S
from sympy.printing.precedence import precedence
PREC = precedence(expr)
if expr.exp is S.Half and not rational:
return "sqrt(%s)" % self._print(expr.base)
if expr.is_commutative:
if -expr.exp is S.Half and not rational:
# Note: Don't test "expr.exp == -S.Half" here, because that will
# match -0.5, which we don't want.
return "1/sqrt(%s)" % self._print(expr.base)
if expr.exp is -S.One:
# Similarly to the S.Half case, don't test with "==" here.
return '1/%s' % self.parenthesize(expr.base, PREC)
e = self.parenthesize(expr.exp, PREC)
if self.printmethod == '_sympyrepr' and expr.exp.is_Rational and expr.exp.q != 1:
# the parenthesized exp should be '(Rational(a, b))' so strip parens,
# but just check to be sure.
if e.startswith('(Rational'):
e = e[1:-1]
# Changes below this line!
if e == "2":
return '{0}*{0}'.format(self.parenthesize(expr.base, PREC))
elif e == "3":
return '{0}*{0}*{0}'.format(self.parenthesize(expr.base, PREC))
else:
return 'pow(%s,%s)' % (self.parenthesize(expr.base, PREC), e)
开发者ID:chawkm,项目名称:firedrake,代码行数:33,代码来源:pointeval_utils.py
示例9: _print_Pow
def _print_Pow(self, expr):
prec = precedence(expr)
if expr.exp == -1:
return '1/%s' % (self.parenthesize(expr.base, prec))
else:
return '%s^%s' % (self.parenthesize(expr.base, prec),
self.parenthesize(expr.exp, prec))
开发者ID:asmeurer,项目名称:scipy-2011-tutorial,代码行数:8,代码来源:mathematica.py
示例10: _print_Mul
def _print_Mul(self, expr):
PREC = precedence(expr)
c, nc = expr.args_cnc()
res = super(MCodePrinter, self)._print_Mul(expr.func(*c))
if nc:
res += '*'
res += '**'.join(self.parenthesize(a, PREC) for a in nc)
return res
开发者ID:JoshLipsmeyer,项目名称:sympy,代码行数:8,代码来源:mathematica.py
示例11: _print_Mul
def _print_Mul(self, expr):
coeff, terms = expr.as_coeff_mul()
if coeff.is_negative:
coeff = -coeff
if coeff is not S.One:
terms = (coeff,) + terms
sign = "-"
else:
terms = (coeff,) + terms
sign = ""
a = [] # items in the numerator
b = [] # items that are in the denominator (if any)
if self.order != 'old':
args = expr._new_rawargs(*terms).as_ordered_factors()
else:
args = terms
# Gather args for numerator/denominator
for item in args:
if item.is_Pow and item.exp.is_Rational and item.exp.is_negative:
b.append(Pow(item.base, -item.exp))
elif item.is_Rational and item is not S.Infinity:
if item.p != 1:
a.append(Rational(item.p))
if item.q != 1:
b.append(Rational(item.q))
else:
a.append(item)
if len(a)==0:
a = [S.One]
a_str = map(lambda x:self.parenthesize(x, precedence(expr)), a)
b_str = map(lambda x:self.parenthesize(x, precedence(expr)), 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 + "%s/"%a_str[0] + '*'.join(b_str)
else:
return sign + '*'.join(a_str) + "/%s"%b_str[0]
else:
return sign + '*'.join(a_str) + "/(%s)"%'*'.join(b_str)
开发者ID:Jerryy,项目名称:sympy,代码行数:46,代码来源:str.py
示例12: _print_Pow
def _print_Pow(self, expr):
PREC = precedence(expr)
if expr.exp is S.NegativeOne:
return '1.0/%s'%(self.parenthesize(expr.base, PREC))
elif expr.exp == 0.5:
return 'sqrt(%s)' % self._print(expr.base)
else:
return StrPrinter._print_Pow(self, expr)
开发者ID:goriccardo,项目名称:sympy,代码行数:8,代码来源:fcode.py
示例13: _print_Pow
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:
return "sqrt(%s)" % self._print(expr.base)
else:
return "pow(%s, %s)" % (self._print(expr.base), self._print(expr.exp))
开发者ID:smichr,项目名称:sympy,代码行数:8,代码来源:ccode.py
示例14: _print_Pow
def _print_Pow(self, expr):
PREC = precedence(expr)
if expr.exp == -1:
return '1/%s' % (self.parenthesize(expr.base, PREC))
elif expr.exp == 0.5:
return 'Math.sqrt(%s)' % self._print(expr.base)
else:
return 'Math.pow(%s, %s)' % (self._print(expr.base),
self._print(expr.exp))
开发者ID:AALEKH,项目名称:sympy,代码行数:9,代码来源:jscode.py
示例15: _print_Pow
def _print_Pow(self, expr):
PREC = precedence(expr)
if expr.exp.is_Rational and expr.exp.p == 1 and expr.exp.q == 2:
return 'sqrt(%s)' % self._print(expr.base)
if expr.exp.is_Rational and expr.exp.is_negative:
return '1/%s'%self._print(expr.base**abs(expr.exp))
else:
return '%s^%s'%(self.parenthesize(expr.base, PREC),
self.parenthesize(expr.exp, PREC))
开发者ID:Grahack,项目名称:geophar,代码行数:9,代码来源:custom_objects.py
示例16: _print_MatMul
def _print_MatMul(self, expr):
c, m = expr.as_coeff_mmul()
if c.is_number and c < 0:
expr = _keep_coeff(-c, m)
sign = "-"
else:
sign = ""
return sign + '*'.join([self.parenthesize(arg, precedence(expr))
for arg in expr.args])
开发者ID:KonstantinTogoi,项目名称:sympy,代码行数:10,代码来源:str.py
示例17: _print_Pow
def _print_Pow(self, expr):
PREC = precedence(expr)
if expr.exp is NegativeOne:
return "(1.0/%s)" % (self.parenthesize(expr.base, PREC))
# For the kernel code, it's better to calculate the power
# here explicitly by multiplication.
elif expr.exp == 2:
return "(%s*%s)" % (self.parenthesize(expr.base, PREC), self.parenthesize(expr.base, PREC))
else:
return int2float("powf(%s,%s)" % (self.parenthesize(expr.base, PREC), self.parenthesize(expr.exp, PREC)))
开发者ID:Mokosha,项目名称:sailfish,代码行数:10,代码来源:sym.py
示例18: _print_Relational
def _print_Relational(self, expr):
charmap = {
"==": "Eq",
"!=": "Ne",
":=": "Assignment",
'+=': "AddAugmentedAssignment",
"-=": "SubAugmentedAssignment",
"*=": "MulAugmentedAssignment",
"/=": "DivAugmentedAssignment",
"%=": "ModAugmentedAssignment",
}
if expr.rel_op in charmap:
return '%s(%s, %s)' % (charmap[expr.rel_op], expr.lhs, expr.rhs)
return '%s %s %s' % (self.parenthesize(expr.lhs, precedence(expr)),
self._relationals.get(expr.rel_op) or expr.rel_op,
self.parenthesize(expr.rhs, precedence(expr)))
开发者ID:madan96,项目名称:sympy,代码行数:19,代码来源:str.py
示例19: _print_Pow
def _print_Pow(self, expr):
PREC = precedence(expr)
if expr.exp is S.NegativeOne:
return '1.0/%s'%(self.parenthesize(expr.base, PREC))
elif isinstance(expr.base, Symbol) and expr.exp == 2:
tmp = self.parenthesize(expr.base, PREC)
return tmp+"*"+tmp
else:
return 'pow(%s,%s)'%(self.parenthesize(expr.base, PREC),
self.parenthesize(expr.exp, PREC))
开发者ID:molmod,项目名称:hipart,代码行数:10,代码来源:writer.py
示例20: _print_Mul
def _print_Mul(self, expr):
prec = precedence(expr)
c, e = expr.as_coeff_Mul()
if c < 0:
expr = _keep_coeff(-c, e)
sign = "-"
else:
sign = ""
a = [] # items in the numerator
b = [] # items that are in the denominator (if any)
pow_paren = [] # Will collect all pow with more than one base element and exp = -1
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 = Mul.make_args(expr)
# 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(Pow(item.base, -item.exp, evaluate=False))
else:
if len(item.args[0].args) != 1 and isinstance(item.base, Mul): # To avoid situations like #14160
pow_paren.append(item)
b.append(Pow(item.base, -item.exp))
elif item.is_Rational and item is not S.Infinity:
if item.p != 1:
a.append(Rational(item.p))
if item.q != 1:
b.append(Rational(item.q))
else:
a.append(item)
a = a or [S.One]
a_str = [self.parenthesize(x, prec, strict=False) for x in a]
b_str = [self.parenthesize(x, prec, strict=False) for x in b]
# To parenthesize Pow with exp = -1 and having more than one Symbol
for item in pow_paren:
if item.base in b:
b_str[b.index(item.base)] = "(%s)" % b_str[b.index(item.base)]
if len(b) == 0:
return sign + '*'.join(a_str)
elif len(b) == 1:
return sign + '*'.join(a_str) + "/" + b_str[0]
else:
return sign + '*'.join(a_str) + "/(%s)" % '*'.join(b_str)
开发者ID:Lenqth,项目名称:sympy,代码行数:55,代码来源:str.py
注:本文中的sympy.printing.precedence.precedence函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论