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