# Tensor operations with AbstractArrays

I’m looking after a way of doing the following operation. Here is an attempt using TensorCast.jl:

``````using TensorCast

# dummy data to simplify the example
n = 7
D = rand(n, n)
G = rand(7, 7, 7, 7)

@cast P[μ, ν] := D[λ, σ] * (G[μ, ν, σ, λ] - G[μ, λ, σ, ν] / 2)
# => ERROR: LoadError: can't find index λ on the left
``````

I might be doing something wrong, but how could I make it work (possibly using another package)? I would like to have support of:

• arbitrary `AbstractArray`s (the `G` in my code is actually a custom type that wraps a matrix to deal with symmetries, so it is not contiguous in memory)
• matrices of type `LinearAlgebra.Hermitian` (TensorOperations.jl appearently does not)

Does it work in Tullio.jl?

1 Like

I think what you want here is:

``````@reduce P[μ, ν] := sum(λ, σ) D[λ, σ] * (G[μ, ν, σ, λ] - G[μ, λ, σ, ν] / 2)
``````

as `@cast` fairly literally only does broadcasting, it won’t ever call `sum` without being asked to. (Maybe the error message should say this, though!) Tullio (and Einsum and TensorOperations and OMEinsum) do automatically reduce over indices not shown on the left.

This ought to work with Hermitian, but it won’t exploit it to do things more efficiently.

1 Like

Awesome! It does work! Thanks for the solution and for suggesting Tullio.jl. It ended up being faster than TensorCast for what I want:

``````julia> @benchmark @tullio P[μ, ν] := D[λ, σ] * (G[μ, ν, σ, λ] - G[μ, λ, σ, ν] / 2)
BenchmarkTools.Trial:
memory estimate:  320 bytes
allocs estimate:  2
--------------
minimum time:     49.161 μs (0.00% GC)
median time:      49.634 μs (0.00% GC)
mean time:        50.264 μs (0.00% GC)
maximum time:     136.301 μs (0.00% GC)
--------------
samples:          10000
evals/sample:     1

julia> @benchmark @cast P[μ, ν] := sum(λ, σ) D[λ, σ] * (G[μ, ν, σ, λ] - G[μ, λ, σ, ν] / 2)
BenchmarkTools.Trial:
memory estimate:  11.02 KiB
allocs estimate:  28
--------------
minimum time:     62.962 μs (0.00% GC)
median time:      63.602 μs (0.00% GC)
mean time:        66.303 μs (0.62% GC)
maximum time:     2.184 ms (95.12% GC)
--------------
samples:          10000
evals/sample:     1
``````