本文整理汇总了Python中sympy.Add.__new__方法的典型用法代码示例。如果您正苦于以下问题:Python Add.__new__方法的具体用法?Python Add.__new__怎么用?Python Add.__new__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.Add
的用法示例。
在下文中一共展示了Add.__new__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
# 需要导入模块: from sympy import Add [as 别名]
# 或者: from sympy.Add import __new__ [as 别名]
def __new__(cls, *args):
args = map(matrixify, args)
args = [arg for arg in args if arg!=0]
if not all(arg.is_Matrix for arg in args):
raise ValueError("Mix of Matrix and Scalar symbols")
# Check that the shape of the args is consistent
A = args[0]
for B in args[1:]:
if A.shape != B.shape:
raise ShapeError("Matrices %s and %s are not aligned"%(A,B))
expr = Add.__new__(cls, *args)
if expr == S.Zero:
return ZeroMatrix(*args[0].shape)
expr = matrixify(expr)
if expr.is_Mul:
return MatMul(*expr.args)
# Clear out Identities
# Any zeros around?
if expr.is_Add and any(M.is_ZeroMatrix for M in expr.args):
newargs = [M for M in expr.args if not M.is_ZeroMatrix] # clear out
if len(newargs)==0: # Did we lose everything?
return ZeroMatrix(*args[0].shape)
if expr.args != newargs: # Removed some 0's but not everything?
return MatAdd(*newargs) # Repeat with simpler expr
return expr