# Problem with double sum on the calculation of Ricci tensor

I wrote a program in Mathematica to calculate the Riemann and Ricci tensors (obviously, passing trought Christoffel symbols first), however just later I thought of calculating Ricci tensor directily from the conextions, without calculating the Riemann tensor beforehand. The part of "Ricci tensor" (below) is doing it correctly, however the code of "calculating the Ricci tensor directly, without the Riemann tensor" gives the wrong answer. Yes, I already checked if the equations are correct... I think the problem is in the double sum made by the variable "rho" and "lambda", but I can't understand why at all...

 g = ({
{-Exp[2 α[r]], 0, 0, 0},
{0, Exp[2 β[r]], 0, 0},
{0, 0, r^2, 0},
{0, 0, 0, r^2 Sin[θ]^2}
}) ; T = ({
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
}); var = {t, r, θ, ϕ};
h = Inverse[g]; Print["Inverse Metric=" MatrixForm[h]];

Print[Style[
"Christoffel Symbol", {Bold, Underlined, Larger}]]; Γ[
i_, j_, k_] :=
Sum[1/2 h[[i,
l]] (D[g[[k, l]], var[[j]]] + D[g[[j, l]], var[[k]]] -
D[g[[k, j]], var[[l]]]), {l, 1, 4}]; i := 1; j := 1; k := 1;
For[i = 1, i <= 4, i++,
For[j = 1, j <= 4, j++,
For[If[j < k, k = j], k <= 4, k++,
If[Γ[i, j, k] =!=  0,
Print[Subscript[Γ, var[[j]], var[[k]]]^var[[i]],
"=", FullSimplify[Γ[i, j, k]]]]]]];

Print[Style["Riemann Tensor", {Bold, Underlined, Larger}]];
RiemannTensor[a_, b_, c_,
d_] := (D[Γ[a, d, b],
var[[c]]]) - (D[Γ[a, c, b],
var[[d]]]) + Γ[a, c, 1] Γ[1, d,
b] + Γ[a, c, 2] Γ[2, d,
b] + Γ[a, c, 3] Γ[3, d,
b] + Γ[a, c, 4] Γ[4, d,
b] - Γ[a, d, 1] Γ[1, b,
c] - Γ[a, d, 2] Γ[2, b,
c] - Γ[a, d, 3] Γ[3, b,
c] - Γ[a, d, 4] Γ[4, b,
c]; a := 1; b := 1; c := 1; d := 1;
For[a = 1, a <= 4, a++,
For[b = 1, b <= 4, b++,
For[c = 1, c <= 4, c++,
For[If[c < d, d = c], d <= 4, d++,
If[RiemannTensor[a, b, c, d] =!= 0,
Print[Subscript[R^var[[a]], var[[b]], var[[c]], var[[d]]], "=",
FullSimplify[RiemannTensor[a, b, c, d]]]]]]]];

Print[Style["Ricci Tensor", {Bold, Underlined, Larger}]];
RicciTensor[m_, n_] :=
Sum[RiemannTensor[soma, m, soma, n], {soma, 1, 4}]; m := 1; n := 1
For[m = 1, m <= 4, m++,
For[If[m < n, n = m], n <= 4, n++,
If[RicciTensor[m, n] =!= 0,
Print[Subscript[R, var[[m]], var[[n]]], "=",
FullSimplify[RicciTensor[m, n]]]]]];

Clear[RicciTensor];

Print[Style[
"Calculating Ricci Tensor directly, without Riemann Tensor", {Bold,
Underlined, Larger}]]; alpha := 1; beta := 1;
RicciTensor[alpha_, beta_] :=
Sum[D[Γ[rho, beta, alpha], var[[rho]]] -
D[Γ[rho, rho, alpha],
var[[beta]]] + Γ[rho, rho, lambda] Γ[
lambda, beta, alpha] - Γ[rho, beta,
lambda] Γ[lambda, rho, alpha], {lambda, 1, 4,
1}, {rho, 1, 4, 1}];
For[alpha = 1, alpha <= 4, alpha++,
For[If[alpha <= beta, beta = alpha], beta <= 4, beta++,
If[RicciTensor[alpha, beta] =!= 0,
Print[Subscript[R, var[[alpha]], var[[beta]]], "=",
FullSimplify[RicciTensor[alpha, beta]]]]]];

• it's a nice code and works properly. However, it's problematic in calculating Ricci tensor directly from Christoffel symbols. I replaced the suggested code by Michael, but it still doesn't work. I also don't understand why he suggested writing {rho, 1, 4, 1} instead of {rho, 1, 4} in the first summation for instance. That part of the code which doesn't work is the following: Thanks again! RicciTensor[m_, n_] := (Sum[ D[Γ[c, n, m], var[[c]]] - D[Γ[c, c, m], var[[n]]], {c, 1, 4, 1}] + Sum[Γ[c, c, d] Γ[d, n, m] - Γ[c, n, d] Γ[d, c, m], {d, 1, 4, 1}, {c, 1, 4, 1}]); m := 1; n := 1; For[m = 1, m <= Nov 22 '17 at 14:47

The first two sums should not be summed over $\lambda$; but since you've put them inside the double sum, Mathematica throws four copies of them in. Corrected code:
RicciTensor[alpha_, beta_] :=
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.45835766196250916, "perplexity": 14140.977839271882}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-39/segments/1631780060803.2/warc/CC-MAIN-20210928122846-20210928152846-00247.warc.gz"}