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

Hope to close the both side indices check #65

Open
zxm403089989 opened this issue Dec 8, 2022 · 2 comments
Open

Hope to close the both side indices check #65

zxm403089989 opened this issue Dec 8, 2022 · 2 comments

Comments

@zxm403089989
Copy link

zxm403089989 commented Dec 8, 2022

In a specific problem, I need to use nested @cast. But due to the check of coexistence of indices, I cannot accomplish this. Here is a simplified case:

using TensorCast
table=rand(2,2,2,2);
@cast testm1[i,j]:=table[i,j,1,1];
testm2 = sum(testm1,dims=1)
# 1×2 Matrix{Float64}:
#  1.15043  0.772946

@cast test[k,l]:=sum(@cast _[i,j]:=table[i,j,k,l],dims=1)
# ERROR: LoadError: index k appears only on the right
#     inner @cast _[i, j] := (table[i, j, k, l], dims) = 1

My purpose is simple, for given indices k and l, sum the result matrix with indices i,j over i, then with different k,l, we have a new matrix. The first example may clearly express my idea. However, @cast always checks index existence on both side, I can't do this. Maybe it is better to achieve this with cloing index check.

@mcabbott
Copy link
Owner

mcabbott commented Dec 8, 2022

Your first example could be written:

julia> testm2 == @reduce _[_,j] := sum(i) table[i,j,1,1]
true

In the second, are you hoping to get an array of arrays? Perhaps like this:

julia> @reduce test3[k,l][_,j] := sum(i) table[i,j,k,l]
2×2 Matrix{TransmuteDims.TransmutedDimsArray{Float64, 2, (0, 1), (2,), SubArray{Float64, 1, Array{Float64, 3}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64, Int64}, true}}}:
 [0.94672 0.889256]  [0.44493 1.67686]
 [1.11919 1.04312]   [1.1044 0.49902]

julia> test3[1,1] == testm2
true

@zxm403089989
Copy link
Author

Thanks. You are definitely right. Actually, I use this code just as an example. Since TensorCast also includes summation over indices, we can solve the second problem easily by @reduce. My original question also calls package Trapz to perform numerical integration over the first dimension. Just I want to simplify the process I substitute to sum to express my idea in brief.

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