Similar thing to numpy's einsum in Julia


I am thinking about how to achieve something similar to numpy.einsum in Julia. For instance, if I have a tensor T_{ijk} and a matrix u_{jj'}, I want to get T'_{ijk}= u_{jj'}T_{ij'k}. Also, I want to do things like H_i =\sum_{j}T_{ijj}. How to efficiently do this in Julia? I noticed that there is a package Einsum.jl, is this the best choise? Thanks

1 Like

I like Tullio


This looks good. Thanks

Maybe TensorOperations.jl and OMEinsum.jl also deserve mention, these are the closest to np.einsum, – they translate into a sequence of permutation and multiplication operations. Whereas Tullio.jl, like Einsum.jl, writes a set of loops from scratch.

They all share the same notation, e.g. @tensor T2[i,j,k] := u[j,j2] * T[i,j2,k] and @tullio H[i] := T[i,j,j].


Hi Michael, I have a question about Tullio. Suppose I want to contract some indexes of a tensor T, it seems that @tullio T[i,j] = T[i,j,k,k] is not allowed. Is there a way to change T without creating another array to store the calculated result?

The output needs to be smaller than the input. I suppose you could try writing into a view of the same array, T2 = view(T, :,:,1,1) should be the right size. But this seems delicate and I don’t really recommend it. Some views like this will lead to data races & hard to find bugs.

Got it. Thanks.