Tullio for general dimensional tensors

Tullio.jl seems to be super faster. But how can I write tullio code for general dimensional tensors.
For example, an outer product of vectors, do we need such a branch as follow? Is there any way to write the following code for a general d ?

using Tullio

function get_outer_product(v)
       d = length(v)
       if d == 2
           v1 = v[1]
           v2 = v[2]
           @tullio R[i,j] := v1[i] * v2[j]
       elseif d == 3
           v1 = v[1]
           v2 = v[2]
           v3 = v[3]
           @tullio R[i,j,k] := v1[i] * v2[j] * v3[k]
       elseif d == 4
           v1 = v[1]
           v2 = v[2]
           v3 = v[3]
           v4 = v[4]
           @tullio R[i,j,k,l] := v1[i] * v2[j] * v3[k] * v4[l]
       end
       return R
end


v = [rand(4), rand(3), rand(5)]
R = get_outer_product(v) # 4×3×5 tensor


For this specific instance I would use multiple dispatch like so:

using Tullio

function get_outer_product(v1, v2)
       @tullio R[i,j] := v1[i] * v2[j]
end

function get_outer_product(v1, v2, v3)
       @tullio R[i,j,k] := v1[i] * v2[j] * v3[k]
end

function get_outer_product(v1, v2, v3, v4)
       @tullio R[i,j,k,l] := v1[i] * v2[j] * v3[k] * v4[l]
end


v = [rand(4), rand(3), rand(5)]
R = get_outer_product(v...) # 4×3×5 tensor

Todo this generically for any number of vectors you probably need some generated function magic…

1 Like

Yeah. I am looking for the generated function magic…