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

Uncontracted indices error when summing two contracted objects #121

Closed
tomkimpson opened this issue Aug 10, 2022 · 3 comments
Closed

Uncontracted indices error when summing two contracted objects #121

tomkimpson opened this issue Aug 10, 2022 · 3 comments

Comments

@tomkimpson
Copy link

MWE:

g_inverse = zeros(Float64,4,4)     # 2 indices
g_∂ = zeros(Float64,4,4,4)         # 3 indicies

@tensor begin
Riemann[ρ,σ,μ,ν] := 
g_inverse[ρ,f]*(g_∂[f,μ,λ] + g_∂[f,λ,μ] - g_∂[μ,λ,f])*g_inverse[λ,h]*(g_∂[h,ν,σ] + g_∂[h,σ,ν] - g_∂[ν,σ,h])
-g_inverse[ρ,i]*(g_∂[i,ν,λ] + g_∂[i,λ,ν] - g_∂[ν,λ,i])*g_inverse[λ,k]*(g_∂[k,μ,σ] + g_∂[k,σ,μ] - g_∂[μ,σ,k])
                                
end

throws an error that it cannot be evaluated to a scalar due to uncontracted indices.

Analytically, the indices contract correctly. Additionally each of the two lines within the @tensor constructor work separately, i.e.

@tensor begin
Riemann[ρ,σ,μ,ν] := 
g_inverse[ρ,f]*(g_∂[f,μ,λ] + g_∂[f,λ,μ] - g_∂[μ,λ,f])*g_inverse[λ,h]*(g_∂[h,ν,σ] + g_∂[h,σ,ν] - g_∂[ν,σ,h])                                
end

is ok ✅ , as is

@tensor begin
Riemann[ρ,σ,μ,ν] := 
-g_inverse[ρ,i]*(g_∂[i,ν,λ] + g_∂[i,λ,ν] - g_∂[ν,λ,i])*g_inverse[λ,k]*(g_∂[k,μ,σ] + g_∂[k,σ,μ] - g_∂[μ,σ,k])                              
end

The summation of these two scalar quantities then seems to throw the error?

Any guidance appreciated 🙏

@Jutho
Copy link
Owner

Jutho commented Aug 10, 2022

I think this is mostly a problem of misunderstanding how expressions are parsed in Julia. A line break typically indicates the end of an expression, unless the parser detects that the expression does not make sense, in which case it will try to add the second line and ignore the line break.

So if you had written

@tensor begin
Riemann[ρ,σ,μ,ν] := 
g_inverse[ρ,f]*(g_∂[f,μ,λ] + g_∂[f,λ,μ] - g_∂[μ,λ,f])*g_inverse[λ,h]*(g_∂[h,ν,σ] + g_∂[h,σ,ν] - g_∂[ν,σ,h]) -
g_inverse[ρ,i]*(g_∂[i,ν,λ] + g_∂[i,λ,ν] - g_∂[ν,λ,i])*g_inverse[λ,k]*(g_∂[k,μ,σ] + g_∂[k,σ,μ] - g_∂[μ,σ,k])                                
end

This works fine. But your input is equivalent to

@tensor begin
Riemann[ρ,σ,μ,ν] :=  g_inverse[ρ,f]*(g_∂[f,μ,λ] + g_∂[f,λ,μ] - g_∂[μ,λ,f])*g_inverse[λ,h]*(g_∂[h,ν,σ] + g_∂[h,σ,ν] - g_∂[ν,σ,h])
end

and

@tensor begin
-g_inverse[ρ,i]*(g_∂[i,ν,λ] + g_∂[i,λ,ν] - g_∂[ν,λ,i])*g_inverse[λ,k]*(g_∂[k,μ,σ] + g_∂[k,σ,μ] - g_∂[μ,σ,k])                              
end

For this second case, TensorOperations will try to evaluate to a scalar (but of course fails), which would then be the return value of this block.

@tomkimpson
Copy link
Author

Yes you are right! Sorry I am a new Julia-er so didn't realise this is how it worked. Thanks for your help and apologies for the spam issue!

@Jutho
Copy link
Owner

Jutho commented Aug 10, 2022

No problem, glad I could help out. You've got me worried for a second too, before I noticed the problem.

Also, the error messages of the @tensor macro need to be much improved, they are often very cryptic.

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