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

Error in summation within @reduce #71

Open
zxm403089989 opened this issue Aug 4, 2023 · 1 comment
Open

Error in summation within @reduce #71

zxm403089989 opened this issue Aug 4, 2023 · 1 comment

Comments

@zxm403089989
Copy link

There are some issues when using @reduce as summation. Here, when define a martrx, TensorCast works well.

julia> using TensorCast

julia> A=rand(4,4);

julia> @reduce _[]:=sum(i,j) A[i,j]
0-dimensional Array{Float64, 0}:
8.519131257815888

julia> @reduce _[]:=sum(i) A[i,i]
0-dimensional Array{Float64, 0}:
1.6983845333674332

However, now if I define a 4th-order array, something wrong happens:

julia> B=rand(4,4,4,4);

julia> @reduce _[]:=sum(i) B[i,i,i,i,]
ERROR: LoadError: index i repeated in [i, i, i, i]

julia>  @reduce _[]:=sum(i,j) B[i,j,i,j]
ERROR: LoadError: index i repeated in [i, j, i, j]
@mcabbott
Copy link
Owner

mcabbott commented Aug 4, 2023

What's going on is that there is special handling for the diagonal elements of a matrix, but nothing is written for repeated indices in higher dimensional arrays. There's no built-in analog of Diagonal here which does more dimensions:

julia> let v = randn(3)
       @pretty @cast _[i,i] := v[i]
       end
begin
    @boundscheck v isa Tuple || (ndims(v) == 1 || throw(ArgumentError("expected a vector or tuple v[i]")))
    wolf = Diagonal(v)
end

The extent of the documentation is this, maybe it should say more:

https://docs.juliahub.com/TensorCast/lkx9a/0.4.6/basics/#Repeated-indices

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