本文整理汇总了Python中sympy.core.compatibility.cmp_to_key函数的典型用法代码示例。如果您正苦于以下问题:Python cmp_to_key函数的具体用法?Python cmp_to_key怎么用?Python cmp_to_key使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cmp_to_key函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __print_set
def __print_set(self, set_):
items = list(set_)
items.sort( key=cmp_to_key(Basic.compare_pretty) )
s = self._print_seq(items, '(', ')')
s = prettyForm(*stringPict.next(type(set_).__name__, s))
return s
示例2: __print_set
def __print_set(self, expr):
items = list(expr)
items.sort( key=cmp_to_key(Basic.compare_pretty) )
args = ', '.join(self._print(item) for item in items)
if args:
args = '[%s]' % args
return '%s(%s)' % (type(expr).__name__, args)
示例3: _print_dict
def _print_dict(self, expr):
keys = sorted(expr.keys(), key=cmp_to_key(Basic.compare_pretty))
items = []
for key in keys:
item = "%s: %s" % (self._print(key), self._print(expr[key]))
items.append(item)
return "{%s}"%", ".join(items)
示例4: _as_ordered_terms
def _as_ordered_terms(self, expr, order=None):
"""A compatibility function for ordering terms in Add. """
order = order or self.order
if order == 'old':
return sorted(Add.make_args(expr), key=cmp_to_key(Basic._compare_pretty))
else:
return expr.as_ordered_terms(order=order)
示例5: _print_dict
def _print_dict(self, expr):
items = []
keys = sorted(expr.keys(), key=cmp_to_key(Basic.compare_pretty))
for key in keys:
val = expr[key]
items.append("%s : %s" % (self._print(key), self._print(val)))
return r"\begin{Bmatrix}%s\end{Bmatrix}" % r", & ".join(items)
示例6: _print_dict
def _print_dict(self, d):
items = []
keys = sorted(d.keys(), key=cmp_to_key(Basic.compare_pretty) )
for k in keys:
K = self._print(k)
V = self._print(d[k])
s = prettyForm(*stringPict.next(K, ': ', V))
items.append(s)
return self._print_seq(items, '{', '}')
示例7: __new__
def __new__(cls, expr, *symbols, **assumptions):
expr = sympify(expr).expand()
if expr is S.NaN:
return S.NaN
if symbols:
symbols = map(sympify, symbols)
if not all(isinstance(s, Symbol) for s in symbols):
raise NotImplementedError(
'Order at points other than 0 not supported.')
else:
symbols = list(expr.free_symbols)
if expr.is_Order:
new_symbols = list(expr.variables)
for s in symbols:
if s not in new_symbols:
new_symbols.append(s)
if len(new_symbols) == len(expr.variables):
return expr
symbols = new_symbols
elif symbols:
if expr.is_Add:
lst = expr.extract_leading_order(*symbols)
expr = Add(*[f.expr for (e, f) in lst])
elif expr:
if len(symbols) > 1 or expr.is_commutative is False:
# TODO
# We cannot use compute_leading_term because that only
# works in one symbol.
expr = expr.as_leading_term(*symbols)
else:
expr = expr.compute_leading_term(symbols[0])
terms = expr.as_coeff_mul(*symbols)[1]
s = set(symbols)
expr = Mul(*[t for t in terms if s & t.free_symbols])
if expr is S.Zero:
return expr
elif not expr.has(*symbols):
expr = S.One
# create Order instance:
symbols.sort(key=cmp_to_key(Basic.compare))
obj = Expr.__new__(cls, expr, *symbols, **assumptions)
return obj
示例8: _print_LatticeOp
def _print_LatticeOp(self, expr):
args = sorted(expr.args, key=cmp_to_key(expr._compare_pretty))
return expr.func.__name__ + "(%s)"%", ".join(self._print(arg) for arg in args)
示例9: __new__
def __new__(cls, expr, *symbols, **assumptions):
expr = sympify(expr)
if expr is S.NaN:
return S.NaN
if symbols:
symbols = map(sympify, symbols)
if not all(isinstance(s, Symbol) for s in symbols):
raise NotImplementedError(
'Order at points other than 0 not supported.')
else:
symbols = list(expr.free_symbols)
if expr.is_Order:
v = set(expr.variables)
symbols = v | set(symbols)
if symbols == v:
return expr
symbols = list(symbols)
elif symbols:
symbols = list(set(symbols))
if len(symbols) > 1:
# XXX: better way? We need this expand() to
# workaround e.g: expr = x*(x + y).
# (x*(x + y)).as_leading_term(x, y) currently returns
# x*y (wrong order term!). That's why we want to deal with
# expand()'ed expr (handled in "if expr.is_Add" branch below).
expr = expr.expand()
if expr.is_Add:
lst = expr.extract_leading_order(*symbols)
expr = Add(*[f.expr for (e, f) in lst])
elif expr:
expr = expr.as_leading_term(*symbols)
expr = expr.as_independent(*symbols, **dict(as_Add=False))[1]
expr = expand_power_base(expr)
expr = expand_log(expr)
if len(symbols) == 1:
# The definition of O(f(x)) symbol explicitly stated that
# the argument of f(x) is irrelevant. That's why we can
# combine some power exponents (only "on top" of the
# expression tree for f(x)), e.g.:
# x**p * (-x)**q -> x**(p+q) for real p, q.
x = symbols[0]
margs = list(Mul.make_args(
expr.as_independent(x, **dict(as_Add=False))[1]))
for i, t in enumerate(margs):
if t.is_Pow:
b, q = t.args
if b in (x, -x) and q.is_real and not q.has(x):
margs[i] = x**q
elif b.is_Pow and not b.exp.has(x):
b, r = b.args
if b in (x, -x) and r.is_real:
margs[i] = x**(r*q)
elif b.is_Mul and b.args[0] is S.NegativeOne:
b = -b
if b.is_Pow and not b.exp.has(x):
b, r = b.args
if b in (x, -x) and r.is_real:
margs[i] = x**(r*q)
expr = Mul(*margs)
if expr is S.Zero:
return expr
if not expr.has(*symbols):
expr = S.One
# create Order instance:
symbols.sort(key=cmp_to_key(Basic.compare))
obj = Expr.__new__(cls, expr, *symbols, **assumptions)
return obj
示例10: cmp_to_key
from __future__ import print_function, division
from collections import defaultdict
from sympy.core.basic import C, Basic
from sympy.core.compatibility import cmp_to_key, reduce, is_sequence
from sympy.core.singleton import S
from sympy.core.operations import AssocOp
from sympy.core.cache import cacheit
from sympy.core.numbers import ilcm, igcd
from sympy.core.expr import Expr
# Key for sorting commutative args in canonical order
_args_sortkey = cmp_to_key(Basic.compare)
def _addsort(args):
# in-place sorting of args
args.sort(key=_args_sortkey)
def _unevaluated_Add(*args):
"""Return a well-formed unevaluated Add: Numbers are collected and
put in slot 0 and args are sorted. Use this when args have changed
but you still want to return an unevaluated Add.
Examples
========
>>> from sympy.core.add import _unevaluated_Add as uAdd
>>> from sympy import S, Add
>>> from sympy.abc import x, y
示例11: __new__
def __new__(cls, expr, *symbols, **assumptions):
expr = sympify(expr).expand()
if expr is S.NaN:
return S.NaN
if symbols:
symbols = map(sympify, symbols)
if not all(isinstance(s, Symbol) for s in symbols):
raise NotImplementedError(
'Order at points other than 0 not supported.')
else:
symbols = list(expr.free_symbols)
if expr.is_Order:
v = set(expr.variables)
symbols = v | set(symbols)
if symbols == v:
return expr
symbols = list(symbols)
elif symbols:
symbols = list(set(symbols))
if expr.is_Add:
lst = expr.extract_leading_order(*symbols)
expr = Add(*[f.expr for (e, f) in lst])
elif expr:
if len(symbols) > 1 or expr.is_commutative is False:
# TODO
# We cannot use compute_leading_term because that only
# works in one symbol.
expr = expr.as_leading_term(*symbols)
else:
expr = expr.compute_leading_term(symbols[0])
margs = list(Mul.make_args(expr.as_independent(*symbols)[1]))
if len(symbols) == 1:
# The definition of O(f(x)) symbol explicitly stated that
# the argument of f(x) is irrelevant. That's why we can
# combine some power exponents (only "on top" of the
# expression tree for f(x)), e.g.:
# x**p * (-x)**q -> x**(p+q) for real p, q.
x = symbols[0]
for i, t in enumerate(margs):
if t.is_Pow:
b, q = t.args
if b in (x, -x) and q.is_real and not q.has(x):
margs[i] = x**q
elif b.is_Pow and not b.exp.has(x):
b, r = b.args
if b in (x, -x) and r.is_real:
margs[i] = x**(r*q)
elif b.is_Mul and b.args[0] is S.NegativeOne:
b = -b
if b.is_Pow and not b.exp.has(x):
b, r = b.args
if b in (x, -x) and r.is_real:
margs[i] = x**(r*q)
expr = Mul(*margs)
if expr is S.Zero:
return expr
if not expr.has(*symbols):
expr = S.One
# create Order instance:
symbols.sort(key=cmp_to_key(Basic.compare))
obj = Expr.__new__(cls, expr, *symbols, **assumptions)
return obj