Jutho / TensorOperations.jl

Julia package for tensor contractions and related operations
https://jutho.github.io/TensorOperations.jl/stable/
Other
438 stars 55 forks source link

`tensortrace` not working on Arrays of Symbolic Expressions from Symbolics.jl. #163

Open Sollovin opened 4 months ago

Sollovin commented 4 months ago

Hi, I got a problem when I'm trying to use Symbolics.jl and TensorOperations.jl together. tensortrace not working on Arrays of Symbolic Expressions from Symbolics.jl.

Minimal code reproduces the error:

using Symbolics, TensorOperations

tensor2 = Symbolics.variables(:ρ, 1:6, 1:2, 1:2, 1:6, 1:2, 1:2)
tensortrace(tensor2, (1, 2, 3, 1, 4, 5))

which gives out

ERROR: MethodError: scalartype(::Type{Union{}}) is ambiguous.

Candidates:
  scalartype(::Type{T}) where T<:Number
    @ VectorInterface ~/.julia/packages/VectorInterface/TAlcJ/src/number.jl:6
  scalartype(::Type{A}) where {T, A<:(AbstractArray{T})}
    @ VectorInterface ~/.julia/packages/VectorInterface/TAlcJ/src/abstractarray.jl:7
  scalartype(::Type{<:Tuple{T, Vararg{T}}}) where T
    @ VectorInterface ~/.julia/packages/VectorInterface/TAlcJ/src/tuple.jl:12
  scalartype(::Type{TT}) where TT<:Tuple
    @ VectorInterface ~/.julia/packages/VectorInterface/TAlcJ/src/tuple.jl:13

Possible fix, define
  scalartype(::Type{Union{}})

Stacktrace:
  [1] scalartype(::Type{Array{Union{}, 4}})
    @ VectorInterface ~/.julia/packages/VectorInterface/TAlcJ/src/abstractarray.jl:7
  [2] tensoralloc(::Type{Array{Union{}, 4}}, ::NTuple{4, Int64}, ::Bool)
    @ TensorOperations ~/.julia/packages/TensorOperations/LAzcX/src/implementation/allocator.jl:90
  [3] tensoralloc_add(::Type, ::Tuple{NTuple{4, Int64}, Tuple{}}, ::Array{Num, 6}, ::Symbol, ::Bool)
    @ TensorOperations ~/.julia/packages/TensorOperations/LAzcX/src/implementation/allocator.jl:36
  [4] tensoralloc_add(TC::Type, pC::Tuple{NTuple{4, Int64}, Tuple{}}, A::Array{Num, 6}, conjA::Symbol)
    @ TensorOperations ~/.julia/packages/TensorOperations/LAzcX/src/implementation/allocator.jl:34
  [5] tensortrace(::Tuple{NTuple{…}, Tuple{}}, ::Array{Num, 6}, ::Tuple{Tuple{…}, Tuple{…}}, ::Symbol, ::VectorInterface.One)
    @ TensorOperations ~/.julia/packages/TensorOperations/LAzcX/src/implementation/functions.jl:168
  [6] tensortrace(IC::NTuple{4, Int64}, A::Array{Num, 6}, IA::NTuple{6, Int64}, conjA::Symbol, α::VectorInterface.One)
    @ TensorOperations ~/.julia/packages/TensorOperations/LAzcX/src/implementation/functions.jl:146
  [7] tensortrace(IC::Vector{Int64}, A::Array{Num, 6}, IA::NTuple{6, Int64}, conjA::Symbol, α::VectorInterface.One)
    @ TensorOperations ~/.julia/packages/TensorOperations/LAzcX/src/implementation/functions.jl:162
  [8] tensortrace
    @ TensorOperations ~/.julia/packages/TensorOperations/LAzcX/src/implementation/functions.jl:158 [inlined]
  [9] tensortrace(A::Array{Num, 6}, IA::NTuple{6, Int64})
    @ TensorOperations ~/.julia/packages/TensorOperations/LAzcX/src/implementation/functions.jl:158
 [10] top-level scope
    @ REPL[7]:1
Some type information was truncated. Use `show(err)` to see complete types.
lkdvos commented 3 months ago

Hi Sollovin, my apologies for the late reply.

I checked out the issue, and it seems like the root is an ambiguity related to VectorInterface. This package defines a "hard zero" Zero and "hard one" One, and these method definitions are ambiguous with the ones defined in Symbolics for Num. I am not entirely sure where to add the fix, and maybe some package extension for VectorIterface-Symbolics is necessary at some point, but you should be able to at least locally resolve the issue by just defining the following methods:

# point ambigous methods to the ones defined in VectorInterface
using Symbolics: Num
using TensorOperations: One, Zero # alternatively: using VectorInterface: One, Zero
Base.:*(x::Num, y::One) = invoke(Base.:*, Tuple{Number, One}, x, y)
Base.:*(x::One, y::Num) = invoke(Base.:*, Tuple{One, Number}, x, y)
Base.:*(x::Num, y::Zero) = invoke(Base.:*, Tuple{Number, Zero}, x, y)
Base.:*(x::Zero, y::Num) = invoke(Base.:*, Tuple{Zero, Number}, x, y)

After which your example runs on my machine. Feel free to let me know if you encounter any more issues :)

Sollovin commented 3 months ago

Hi lkdvos, thanks a lot for your reply.

It now works well with the above definition.

Thanks again and hoping you making progress in fixing it properly.