使用 sympy 计算索引张量乘法
我想用 sympy 计算以下内容:
其中 I
是一个 3x3 单位矩阵。最终用途是将其与符号矩阵一起使用。
我有以下内容:
import sympy as sp
I = sp.eye(3)
Missing operations with sympy
使用 numpy,我可以只使用 einsum 函数并具有:
import numpy as np
I = np.eye(3)
Res = (np.einsum("ij,kl->ijkl", I, I)
+ np.einsum("ik,jl->ijkl", I, I)
+ np.einsum("il,jk->ijkl", I, I))
但是,einsum 不会接受 sympy 的对象来执行此操作。 我如何用 sympy 计算这个?
I would like to compute the following with sympy:
Where I
is a 3x3 identity matrix. The end use is to use this with symbolic matrices.
I have the following:
import sympy as sp
I = sp.eye(3)
Missing operations with sympy
With numpy I can just use the einsum
function and have:
import numpy as np
I = np.eye(3)
Res = (np.einsum("ij,kl->ijkl", I, I)
+ np.einsum("ik,jl->ijkl", I, I)
+ np.einsum("il,jk->ijkl", I, I))
However, einsum
will not accept sympy's objects for this operation.
How can I compute this with sympy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然该符号使 einsum 表达式变得方便,但它不是矩阵乘积。它更像是一个扩展的外部产品。没有乘积之和:
在
numpy
中,我们可以使用broadcasting
来做同样的事情:你的
sp.eye
产生一个MutableDenseMatrix
https://docs.sympy.org/latest/modules/matrices/dense.html#sympy.matrices.dense.MutableDenseMatrix
请随意研究其文档。我的印象是 sympy 矩阵不能像 numpy 那样实现多维数组。
While the notation makes the
einsum
expression convenient, it isn't a matrix-product. It's more like an extended outer product. There's no sum-of-products:In
numpy
we can usebroadcasting
to do the same thing:Your
sp.eye
produces aMutableDenseMatrix
https://docs.sympy.org/latest/modules/matrices/dense.html#sympy.matrices.dense.MutableDenseMatrix
Feel free to study its docs. My impression is that sympy matrices don't implement multidimensional arrays with anything like the power of
numpy
.