# Adding the result of a tensor contraction to a preallocated array

**URL:** <https://discourse.julialang.org/t/adding-the-result-of-a-tensor-contraction-to-a-preallocated-array/110789>\
**Category:** General Usage\
**Tags:** question, performance, tullio, tensoroperations\
**Created:** [February 26, 2024, 4:19pm UTC](https://discourse.julialang.org/t/adding-the-result-of-a-tensor-contraction-to-a-preallocated-array/110789 "2024-02-26T16:19:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulp](https://avatars.discourse-cdn.com/v4/letter/p/a3d4f5/32.png) [@paulp](https://discourse.julialang.org/u/paulp)\
**Post date:** [February 26, 2024, 4:19pm UTC](https://discourse.julialang.org/t/adding-the-result-of-a-tensor-contraction-to-a-preallocated-array/110789/1 "2024-02-26T16:19:40Z")

</div>

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:

```julia
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

```julia
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!
