本文整理汇总了Python中sympy.core.Add.as_leading_term方法的典型用法代码示例。如果您正苦于以下问题:Python Add.as_leading_term方法的具体用法?Python Add.as_leading_term怎么用?Python Add.as_leading_term使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.core.Add
的用法示例。
在下文中一共展示了Add.as_leading_term方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
# 需要导入模块: from sympy.core import Add [as 别名]
# 或者: from sympy.core.Add import as_leading_term [as 别名]
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
示例2: __new__
# 需要导入模块: from sympy.core import Add [as 别名]
# 或者: from sympy.core.Add import as_leading_term [as 别名]
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:
expr = expr.as_leading_term(*symbols)
coeff, terms = expr.as_coeff_mul()
expr = Mul(*[t for t in terms if t.has(*symbols)])
if expr is S.Zero:
return expr
elif not expr.has(*symbols):
expr = S.One
# create Order instance:
symbols.sort(Basic.compare)
obj = Expr.__new__(cls, expr, *symbols, **assumptions)
return obj
示例3: __new__
# 需要导入模块: from sympy.core import Add [as 别名]
# 或者: from sympy.core.Add import as_leading_term [as 别名]
def __new__(cls, expr, *args, **kwargs):
expr = sympify(expr)
if not args:
if expr.is_Order:
variables = expr.variables
point = expr.point
else:
variables = list(expr.free_symbols)
point = [S.Zero]*len(variables)
else:
args = list(args if is_sequence(args) else [args])
variables, point = [], []
if is_sequence(args[0]):
for a in args:
v, p = list(map(sympify, a))
variables.append(v)
point.append(p)
else:
variables = list(map(sympify, args))
point = [S.Zero]*len(variables)
if not all(isinstance(v, Symbol) for v in variables):
raise TypeError('Variables are not symbols, got %s' % variables)
if len(list(uniq(variables))) != len(variables):
raise ValueError('Variables are supposed to be unique symbols, got %s' % variables)
if expr.is_Order:
expr_vp = dict(expr.args[1:])
new_vp = dict(expr_vp)
vp = dict(zip(variables, point))
for v, p in vp.items():
if v in new_vp.keys():
if p != new_vp[v]:
raise NotImplementedError(
"Mixing Order at different points is not supported.")
else:
new_vp[v] = p
if set(expr_vp.keys()) == set(new_vp.keys()):
return expr
else:
variables = list(new_vp.keys())
point = [new_vp[v] for v in variables]
if expr is S.NaN:
return S.NaN
if not all(p is S.Zero for p in point) and \
not all(p is S.Infinity for p in point):
raise NotImplementedError('Order at points other than 0 '
'or oo not supported, got %s as a point.' % point)
if variables:
if len(variables) > 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(variables, point)
expr = Add(*[f.expr for (e, f) in lst])
elif expr:
if point[0] == S.Zero:
expr = expr.as_leading_term(*variables)
expr = expr.as_independent(*variables, as_Add=False)[1]
expr = expand_power_base(expr)
expr = expand_log(expr)
if len(variables) == 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 = variables[0]
margs = list(Mul.make_args(
expr.as_independent(x, 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)
#.........这里部分代码省略.........
示例4: __new__
# 需要导入模块: from sympy.core import Add [as 别名]
# 或者: from sympy.core.Add import as_leading_term [as 别名]
def __new__(cls, expr, *symbols):
expr = sympify(expr)
if expr is S.NaN:
return S.NaN
point = S.Zero
if symbols:
symbols = list(map(sympify, symbols))
if symbols[-1] in (S.Infinity, S.Zero):
point = symbols[-1]
symbols = symbols[:-1]
if not all(isinstance(s, Symbol) for s in symbols):
raise NotImplementedError(
'Order at points other than 0 or oo not supported.')
if not symbols:
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))
args = tuple(symbols) + (point,)
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(*args)
expr = Add(*[f.expr for (e, f) in lst])
elif expr:
expr = expr.as_leading_term(*symbols)
expr = expr.as_independent(*symbols, 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, 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=default_sort_key)
args = (expr,) + tuple(symbols) + (point,)
obj = Expr.__new__(cls, *args)
return obj
示例5: __new__
# 需要导入模块: from sympy.core import Add [as 别名]
# 或者: from sympy.core.Add import as_leading_term [as 别名]
def __new__(cls, expr, *symbols, **assumptions):
expr = sympify(expr).expand()
if expr is S.NaN:
return S.NaN
if symbols:
symbols = map(sympify, symbols)
else:
symbols = list(expr.free_symbols)
symbols.sort(Basic.compare)
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:
symbol_map = {}
new_symbols = []
for s in symbols:
if isinstance(s, Symbol):
new_symbols.append(s)
continue
z = Dummy('z')
x1,s1 = solve4linearsymbol(s, z)
expr = expr.subs(x1,s1)
symbol_map[z] = s
new_symbols.append(z)
if symbol_map:
r = Order(expr, *new_symbols, **assumptions)
expr = r.expr.subs(symbol_map)
symbols = []
for s in r.variables:
if s in symbol_map:
symbols.append(symbol_map[s])
else:
symbols.append(s)
else:
if expr.is_Add:
lst = expr.extract_leading_order(*symbols)
expr = Add(*[f.expr for (e,f) in lst])
else:
expr = expr.as_leading_term(*symbols)
coeff, terms = expr.as_coeff_mul()
if coeff is S.Zero:
return coeff
expr = Mul(*[t for t in terms if t.has(*symbols)])
elif expr is not S.Zero:
expr = S.One
if expr is S.Zero:
return expr
# create Order instance:
obj = Expr.__new__(cls, expr, *symbols, **assumptions)
return obj
示例6: __new__
# 需要导入模块: from sympy.core import Add [as 别名]
# 或者: from sympy.core.Add import as_leading_term [as 别名]
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