我有一个3x3矩阵,其中我计算逆。只有当一些子表达式被新符号替换时,才能清晰地写入反转,因为它们多次出现。我可以努力寻找那些子表达并替换它们吗?我尝试了以下内容,没有成功:
from sympy import *
Ex, Ez, nuxy, nuxz = symbols('E_x E_z nu_xy nu_xz')
# compliance matrix for cross-anisotropic material
compl = Matrix([[1/Ex, -nuxy/Ex, -nuxz/Ez],
[-nuxy/Ex, 1/Ex, -nuxz/Ez],
[-nuxz/Ex, -nuxz/Ex, 1/Ez]])
# stiffness matrix
stiff = compl.inv()
# symbols I want to introduce
m, e = symbols('m e')
meSubs = {Ex/Ez: e, (1 - nuxy - 2*e*nuxz**2): m} # instead of these subexpressions
# stiff.simplify() returns None, is that a bug? that's why I apply simplify together with subs here:
stiff.applyfunc(lambda x: simplify(x.subs(meSubs)))
print stiff
使用sympy 0.6.7(如果需要,我可以升级)。
修改 的
我升级到0.7.1-git(准确地说是cf9c01f8f9b4b749a7f59891f546646e4b38e580),然后运行(感谢@PreludeAndFugue提出建议):
from sympy import *
Ex,Ez,nuxy,nuxz,m=symbols('E_x E_z nu_xy nu_xz m')
compl=Matrix([[1/Ex,-nuxy/Ex,-nuxz/Ez],[-nuxy/Ex,1/Ex,-nuxz/Ez],[-nuxz/Ex,-nuxz/Ex,1/Ez]])
stiff=compl.inv()
stiff.simplify()
stiff.subs({-nuxy-2*nuxz**2+1:m}) # tried other rearrangements of the equality, as well, same result.
stiff.applyfunc(lambda x: together(expand(x)))
pprint(stiff)
获得
⎡ ⎛ 2 ⎞ ⎛ 2⎞ ⎤
⎢ Eₓ⋅⎝ν_xz - 1⎠ -Eₓ⋅⎝-ν_xy - ν_xz ⎠ Eₓ⋅ν_xz ⎥
⎢ ────────────────────────────────── ──────────────────────────────────── ───────────────────⎥
⎢ 2 2 2 2 2 2 2 ⎥
⎢ ν_xy + 2⋅ν_xy⋅ν_xz + 2⋅ν_xz - 1 - ν_xy - 2⋅ν_xy⋅ν_xz - 2⋅ν_xz + 1 -ν_xy - 2⋅ν_xz + 1⎥
⎢ ⎥
⎢ ⎛ 2⎞ ⎛ 2 ⎞ ⎥
⎢ -Eₓ⋅⎝-ν_xy - ν_xz ⎠ Eₓ⋅⎝ν_xz - 1⎠ Eₓ⋅ν_xz ⎥
⎢──────────────────────────────────── ────────────────────────────────── ───────────────────⎥
⎢ 2 2 2 2 2 2 2 ⎥
⎢- ν_xy - 2⋅ν_xy⋅ν_xz - 2⋅ν_xz + 1 ν_xy + 2⋅ν_xy⋅ν_xz + 2⋅ν_xz - 1 -ν_xy - 2⋅ν_xz + 1⎥
⎢ ⎥
⎢ E_z⋅ν_xz E_z⋅ν_xz E_z⋅(ν_xy - 1) ⎥
⎢ ─────────────────── ─────────────────── ────────────────── ⎥
⎢ 2 2 2 ⎥
⎣ -ν_xy - 2⋅ν_xz + 1 -ν_xy - 2⋅ν_xz + 1 ν_xy + 2⋅ν_xz - 1 ⎦
嗯,那么为什么不用“? - v_xy - 2⋅ν_xz²+ 1”替换为m?
答案 0 :(得分:0)
我不确定使用0.6.7是否有问题 - 但建议更新为0.7.1。
当我查看stiff
时,我看不到meSubs
中的替换有用。创建stiff
后,我执行了以下操作:
stiff.simplify()
stiff = stiff.subs({2*nuxz**2: 1 - nuxy - m})
stiff = stiff.applyfunc(lambda x: together(expand(x)))
pprint(stiff)
输出也不错:
[ / 2 \ / 2\ ]
[ E_x*\nu_xz - 1/ E_x*\nu_xy + nu_xz / E_x*nu_xz ]
[ ---------------- -------------------- --------- ]
[ m*(-nu_xy - 1) m*(nu_xy + 1) m ]
[ ]
[ / 2\ / 2 \ ]
[E_x*\nu_xy + nu_xz / E_x*\nu_xz - 1/ E_x*nu_xz ]
[-------------------- ---------------- --------- ]
[ m*(nu_xy + 1) m*(-nu_xy - 1) m ]
[ ]
[ E_z*nu_xz E_z*nu_xz E_z*(-nu_xy + 1)]
[ --------- --------- ----------------]
[ m m m ]
展开:http://docs.sympy.org/0.7.1/modules/core.html?highlight=expand#sympy.core.function.expand
答案 1 :(得分:0)
它确实被替换了,但是subs
在Matrices上不可行。不幸的是,applyfunc
也不会发生可变的工作。我得到了
In [10]: pprint(stiff.subs({-nuxy-2*nuxz**2+1:m}))
⎡ ⎛ 2 ⎞ ⎛ 2⎞ ⎤
⎢ Eₓ⋅⎝nu_xz - 1⎠ -Eₓ⋅⎝-nu_xy - nu_xz ⎠ Eₓ⋅nu_xz ⎥
⎢ ────────────────────────────────────── ──────────────────────────────────────── ──────── ⎥
⎢ 2 2 2 2 2 2 m ⎥
⎢ nu_xy + 2⋅nu_xy⋅nu_xz + 2⋅nu_xz - 1 - nu_xy - 2⋅nu_xy⋅nu_xz - 2⋅nu_xz + 1 ⎥
⎢ ⎥
⎢ ⎛ 2⎞ ⎛ 2 ⎞ ⎥
⎢ -Eₓ⋅⎝-nu_xy - nu_xz ⎠ Eₓ⋅⎝nu_xz - 1⎠ Eₓ⋅nu_xz ⎥
⎢──────────────────────────────────────── ────────────────────────────────────── ──────── ⎥
⎢ 2 2 2 2 2 2 m ⎥
⎢- nu_xy - 2⋅nu_xy⋅nu_xz - 2⋅nu_xz + 1 nu_xy + 2⋅nu_xy⋅nu_xz + 2⋅nu_xz - 1 ⎥
⎢ ⎥
⎢ E_z⋅nu_xz E_z⋅nu_xz -E_z⋅(nu_xy - 1)⎥
⎢ ───────── ───────── ────────────────⎥
⎣ m m m ⎦
默认情况下,计划使Matrix不可变,然后使MutableMatrix完全适用于所有操作。见https://code.google.com/p/sympy/issues/detail?id=3410。但它还没有发生。