# Tensor operation in Julia

**URL:** <https://discourse.julialang.org/t/tensor-operation-in-julia/99154>\
**Category:** New to Julia\
**Tags:** question, tensoroperations\
**Created:** [May 20, 2023, 9:14am UTC](https://discourse.julialang.org/t/tensor-operation-in-julia/99154 "2023-05-20T09:14:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javad\_Vahedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javad_vahedi/32/50070_2.png) [@Javad\_Vahedi](https://discourse.julialang.org/u/Javad_Vahedi)\
**Post date:** [May 20, 2023, 9:14am UTC](https://discourse.julialang.org/t/tensor-operation-in-julia/99154/1 "2023-05-20T09:14:07Z")

</div>

Dear Julia,

What is the best way to rewrite the following code from Python to Julia?

def ctwa(U, B, J, F):  
# B shape, (n,m)  
# U shape, (n,m)  
# J shape, (n,n,m,m)  
# F shape, (m,m,m)  
BUF = np.einsum(‘jb,jc,abc-\>ja’,B,U,F)  
JU = np.einsum(‘jlbc,lb-\>jbc’,J,U)  
JUUF = np.einsum(‘jbc,jc,abc-\>ja’,JU,U,F)  
return BUF + JUUF

Regards  
JV

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 20, 2023, 9:22am UTC](https://discourse.julialang.org/t/tensor-operation-in-julia/99154/2 "2023-05-20T09:22:02Z")

</div>

> **[GitHub - Jutho/TensorOperations.jl: Julia package for tensor contractions and...](https://github.com/Jutho/TensorOperations.jl)**
>
> Julia package for tensor contractions and related operations - GitHub - Jutho/TensorOperations.jl: Julia package for tensor contractions and related operations

```julia
using TensorOperations

function ctwa(U::Matrix, B::Matrix, J::Array{<:Any,4}, F::Array{<:Any,3})
   @tensor begin
       ...
   end 
end

```

---

<div class="post-metadata">

**Author:** ![Javad\_Vahedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javad_vahedi/32/50070_2.png) [@Javad\_Vahedi](https://discourse.julialang.org/u/Javad_Vahedi)\
**Post date:** [May 20, 2023, 9:28am UTC](https://discourse.julialang.org/t/tensor-operation-in-julia/99154/3 "2023-05-20T09:28:46Z")

</div>

Dear mkitti  
Many hanks for your fast reply:

I have already tried TensorOperation library:

for instance :  
BUF = np.einsum(‘jb,jc,abc-\>ja’,B,U,F)  
@tensor SHF[j,a] := B[j,b] \* U[j,c] F[a,b,c]

but I got the following error:

**ERROR:** TensorOperations.IndexError{String}(“non-matching indices between left and right hand side: $(Expr(:(:=), :(var"##278"[j, a]), :((var"##275"[j, b] \* var"##276"[j, c]) \* var"##277"[a, b, c])))”)

I must match the left and right tensor as follows:

@tensor SHF[i,j,a] := B[i,b] \* U[j,c] F[a,b,c]

and it works, but what I want is a matrix like SHF[j,a] not a tensor SHF[i,j,a]

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [May 20, 2023, 9:57am UTC](https://discourse.julialang.org/t/tensor-operation-in-julia/99154/4 "2023-05-20T09:57:13Z")

</div>

This might be a bug or restriction in TensorOperations …  
In any case, there are several alternative libraries available which seem to work:

```julia
julia> B = randn(5, 3);

julia> U = randn(5, 4);

julia> F = randn(2,3,4);

julia> using TensorCast

julia> TensorCast.@reduce shf[j,a] := sum(b,c) B[j,b]*F[a,b,c]*U[j,c]
5×2 Matrix{Float64}

julia> using OMEinsum

julia> OMEinsum.@ein shf[j,a] := B[j,b]*U[j,c]*F[a,b,c]
5×2 Matrix{Float64}:

```

The latter library also provides a numpy-like string macro, i.e., `ein"jb,jc,abc->ja"(B, U, F)`, giving an almost one-to-one translation from Python. Imho, `TensorCast` is more versatile allowing to mix in non-linear functions and other types of contractions/reductions.
