Adding the result of a tensor contraction to a preallocated array

Hi all,

I’d like to perform an arbitrary contraction between two tensors and add the result to an existing array, without allocating an additional array. Tullio has the operation that I want, here is a simple example:

using Tullio

result = zeros(3, 5)

b = rand(3, 6)
c = rand(6, 5)

@tullio result[i,j] += b[i,k] * c[k, j]

However, my contraction expressions are generated at runtime and I can’t find a good way to produce the corresponding call to the macro from the contraction string. So, for the given example, the indices would be given in a string like “ik,kj->ij” and I’d need to call the macro using that. In the global scope I could do something like

using Tullio

result = zeros(3, 5)

b = rand(3, 6)
c = rand(6, 5)

exp_result = "i,j"
exp_b = "i,k"
exp_c = "k,j"

calc = "@tullio result[$exp_result] += b[$exp_b] * c[$exp_c]"

eval(Meta.parse(calc))

but this doesn’t work in a function and does seem like a bad idea anyway.
I understand that there are other einsum-packages for Julia, but for none of them I saw both required features (to have the += operation in place on a given array, and the acceptance of contracted axes as a parameter).

Is there a simple way to get Tullio to do what I want? Or any other package?

Help would be greatly appreciated. Thanks!