本文整理汇总了Python中sympy.Mul.append方法的典型用法代码示例。如果您正苦于以下问题:Python Mul.append方法的具体用法?Python Mul.append怎么用?Python Mul.append使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.Mul
的用法示例。
在下文中一共展示了Mul.append方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: linear_expand
# 需要导入模块: from sympy import Mul [as 别名]
# 或者: from sympy.Mul import append [as 别名]
def linear_expand(expr):
"""
If a sympy 'Expr' is of the form:
expr = expr_0 + expr_1*a_1 + ... + expr_n*a_n
where all the a_j are noncommuting symbols in basis then
(expr_0, ..., expr_n) and (1, a_1, ..., a_n) are returned. Note that
expr_j*a_j does not have to be of that form, but rather can be any
Mul with a_j as a factor (it doen not have to be a postmultiplier).
expr_0 is the scalar part of the expression.
"""
expr = expand(expr)
if expr.is_commutative: # commutative expr only contains expr_0
return (expr, ), (S.One, )
if isinstance(expr, Mul): # expr only contains one term
(coefs, bases) = expr.args_cnc()
coefs = Mul(*coefs)
bases = bases[0]
elif isinstance(expr, Symbol): # term is Symbol
coefs = S.One
bases = expr
elif isinstance(expr, Add): # expr has multiple terms
coefs = []
bases = []
for arg in expr.args:
term = arg.args_cnc()
coef = Mul(*term[0])
base = term[1][0]
if base in bases: # increment coefficient of base
ibase = list(bases).index(base) # Python 2.5
coefs[ibase] += coef
else: # add base to list
coefs.append(coef)
bases.append(base)
else:
raise NotImplementedError("linear_expand for type %s" % type(expr))
if not isinstance(coefs, list): # convert single coef to list
coefs = [coefs]
if not isinstance(bases, list): # convert single base to list
bases = [bases]
coefs = tuple(coefs)
bases = tuple(bases)
return coefs, bases
示例2: bilinear_expand
# 需要导入模块: from sympy import Mul [as 别名]
# 或者: from sympy.Mul import append [as 别名]
def bilinear_expand(expr):
"""
If a sympy 'Expr' is of the form:
expr = expr_0+expr_1*a_1+...+expr_n*a_n
where all the a_j are noncommuting symbols in basis then
(expr_0,...,expr_n) and (1,a_1,...,a_n) are returned. Note that
expr_j*a_j does not have to be of that form, but rather can be any
Mul with a_j as a factor (it doen not have to be a postmultiplier).
expr_0 is the scalar part of the expression.
"""
if expr.is_commutative: #commutative expr only contains expr_0
return((expr,),(ONE,))
expr = expand(expr)
if isinstance(expr,Mul): #expr only contains one term
x = (coefs,bases) = expr.args_cnc()
coefs = Mul(*coefs)
bases = Mul(*bases)
elif isinstance(expr,Symbol): #term is Symbol
coefs = ONE
bases = expr
elif isinstance(expr,Add): #expr has multiple terms
coefs = []
bases = []
for arg in expr.args:
term = arg.args_cnc()
coefs.append(Mul(*term[0]))
bases.append(Mul(*term[1]))
"""
if not isinstance(coefs,list): #convert single coef to list
coefs = [coefs]
if not isinstance(bases,list): #convert single base to list
bases = [bases]
"""
coefs = tuple(coefs)
bases = tuple(bases)
return(coefs,bases)