Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Sollovin opened this issue Mar 8, 2024 · 2 comments

Comments

@Sollovin
Copy link

Sollovin commented Mar 8, 2024

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
Copy link
Collaborator

lkdvos commented Mar 15, 2024

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants