使用 sympy 计算索引张量乘法

发布于 2025-01-12 07:23:29 字数 522 浏览 0 评论 0原文

我想用 sympy 计算以下内容:

 linkToImage

其中 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:

 linkToImage

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2025-01-19 07:23:29

虽然该符号使 einsum 表达式变得方便,但它不是矩阵乘积。它更像是一个扩展的外部产品。没有乘积之和:

In [22]: 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)
    ...: )

In [23]: Res
Out[23]: 
array([[[[3., 0., 0.],
         [0., 1., 0.],
         [0., 0., 1.]],

        [[0., 1., 0.],
         [1., 0., 0.],
         [0., 0., 0.]],

        [[0., 0., 1.],
         [0., 0., 0.],
         [1., 0., 0.]]],


       [[[0., 1., 0.],
         [1., 0., 0.],
         [0., 0., 0.]],

        [[1., 0., 0.],
         [0., 3., 0.],
         [0., 0., 1.]],

        [[0., 0., 0.],
         [0., 0., 1.],
         [0., 1., 0.]]],


       [[[0., 0., 1.],
         [0., 0., 0.],
         [1., 0., 0.]],

        [[0., 0., 0.],
         [0., 0., 1.],
         [0., 1., 0.]],

        [[1., 0., 0.],
         [0., 1., 0.],
         [0., 0., 3.]]]])

In [24]: Res.shape
Out[24]: (3, 3, 3, 3)

numpy中,我们可以使用broadcasting来做同样的事情:

In [25]: res1 = I[:,:,None,None]*I + I[:,None,:,None]*I[None,:,None,:]+I[:,None,None,:]*I[None,:,:,Non
    ...: e]

In [26]: res1.shape
Out[26]: (3, 3, 3, 3)

In [27]: np.allclose(Res, res1)
Out[27]: True

你的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 [22]: 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)
    ...: )

In [23]: Res
Out[23]: 
array([[[[3., 0., 0.],
         [0., 1., 0.],
         [0., 0., 1.]],

        [[0., 1., 0.],
         [1., 0., 0.],
         [0., 0., 0.]],

        [[0., 0., 1.],
         [0., 0., 0.],
         [1., 0., 0.]]],


       [[[0., 1., 0.],
         [1., 0., 0.],
         [0., 0., 0.]],

        [[1., 0., 0.],
         [0., 3., 0.],
         [0., 0., 1.]],

        [[0., 0., 0.],
         [0., 0., 1.],
         [0., 1., 0.]]],


       [[[0., 0., 1.],
         [0., 0., 0.],
         [1., 0., 0.]],

        [[0., 0., 0.],
         [0., 0., 1.],
         [0., 1., 0.]],

        [[1., 0., 0.],
         [0., 1., 0.],
         [0., 0., 3.]]]])

In [24]: Res.shape
Out[24]: (3, 3, 3, 3)

In numpy we can use broadcasting to do the same thing:

In [25]: res1 = I[:,:,None,None]*I + I[:,None,:,None]*I[None,:,None,:]+I[:,None,None,:]*I[None,:,:,Non
    ...: e]

In [26]: res1.shape
Out[26]: (3, 3, 3, 3)

In [27]: np.allclose(Res, res1)
Out[27]: True

Your sp.eye produces a MutableDenseMatrix

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文