Question

I'm wondering how to get functionality similar to numpy.einsum in Julia.

Specifically, I have a 3rd order tensor that I'm looking to multiply by a 2nd tensor (matrix), contracting both of the dimensions to yield a 1st order tensor (vector).

Currently, I'm using PyCall so that I can use the numpy.einsum function like so:

using PyCall
@pyimport numpy as np

a = rand(5,4,3)
b = rand(5,4)

c = np.einsum("ijk,ij", a,b)
size(c) == (3,)

It feels kind of silly to rely on calling python in order to do tensor math. I also imagine that a julia implementation would have speed advantages. However, I haven't any function for this in julia, and the brute force summation is 1-2 orders of magnitude slower. What functions can I use?

Was it helpful?

Solution

Doesn't sum(a.*b,(1,2)) do what you want?

OTHER TIPS

There is Tullio. Tullio is a pretty flexible einsum macro. It understands array operations written in index notation such as just permutations and matrix multiplication, but also convolutions, stencils, scatter/gather, and broadcasting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top