Search code examples
macrosjuliametaprogrammingeinsum

Julia: ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol


I am trying to multiply two array via the Einsum package (uses Meta Programming). I get the following error if I use the @einsum macro with elements of a struct but not if I copy the element beforehand. Can someone explain?

using Einsum

struct MyStruct
    a::Array
end

s1 = MyStruct(rand(5, 2))
s2 = MyStruct(rand(6, 2))

# does not work (ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol)
@einsum result[i, j] := s1.a[i, k] * s2.a[j, k]

# does work
s1_a = s1.a
s2_a = s2.a
@einsum result[i, j] := s1_a[i, k] * s2_a[j, k]

Solution

  • The macro @einsum assumes, in the function extractindices which it uses, that the arrays are simple names (i.e. Symbols like s1_a), not expressions like s1.a or function calls or some such thing. It has simply not been written to accomodate indexing of expressions. The package Einsum has not been updated in 4 years, there might be other packages that can achieve this.