且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

numpy.einsum for朱莉娅? (2)

更新时间:2023-12-01 09:39:04

编辑/更新:现在,这是一个已注册的程序包,因此您可以Pkg.add("Einsum"),并且应该可以使用(请参见下面的示例开始).

Edit/Update: This is now a registered package, so you can Pkg.add("Einsum") and you should be good to go (see the example below to get started).

原始答案:我刚刚创建了一些非常初步的代码来执行此操作.这完全符合Matt B.在其评论中的描述.希望有帮助,让我知道它是否有问题.

Original Answer: I just created some very preliminary code to do this. It follows exactly what Matt B. described in his comment. Hope it helps, let me know if there are problems with it.

https://github.com/ahwillia/Einsum.jl

这是实现示例的方式:

using Einsum

a = rand(10,10,10)
b = rand(10,10)
c = rand(10,10,10)
Q = zeros(10,10)

@einsum Q[i,j] = a[i,m,k]*b[m,l]*c[l,k,j]

在后台,宏将构建以下一系列嵌套的for循环,并在编译之前将其插入到您的代码中. (请注意,这不是插入的确切代码,它还会使用macroexpand查看完整代码,以检查输入的尺寸是否一致):

Under the hood the macro builds the following series of nested for loops and inserts them into your code before compile time. (Note this is not the exact code inserted, it also checks to make sure the dimensions of the inputs agree, using macroexpand to see the full code):

for j = 1:size(Q,2)
    for i = 1:size(Q,1)
        s = 0
        for l = 1:size(b,2)
            for k = 1:size(a,3)
                for m = 1:size(a,2)
                    s += a[i,m,k] * b[m,l] * c[l,k,j]
                end
            end
        end
        Q[i,j] = s
    end
end