I'm trying to create a function which receives as inputs a coordinate system $x^\mu$, metric tensor $g_{\mu\nu}$, a general tensor $T$ of rank $n$, and a list $I$ of length $n$ that looks like this:

$$I=(c_1,c_2,\dots,c_n)\quad\quad \forall i\in[1,n]_{\mathbb N}: c_i\in\{\mathrm{u},\mathrm{d}\}$$

which describes the location of the indices of the tensor ($\mathrm{u}$ for up, $\mathrm{d}$ for down). For example - if $T=T^{\mu\ \ \sigma}_{\ \ \nu\rho}$, then $I=(\mathrm{u},\mathrm{d},\mathrm{d},\mathrm{u})$ and $n=4$.

The output of the function should be the covariant derivative of the tensor in the given coordinate system, defined as the following:

$$\nabla_iv^j=\partial_iv^j+\Gamma^j_{\ ik}v^k \\\nabla_iv_j=\partial_iv_j-\Gamma^k_{\ ij}v_k$$

where $\partial_i\equiv{\partial}/{\partial x_i}$, and $\Gamma^i_{\ ij}$ are the Christoffel symbols. The main difficulty here for me is differentiating between the up and down indices. Also, it's not very clear how the ""matrix"" multiplication should be made.

• I did create a function which computes the Christoffel symbols given a metric $g$ and returns them as a tensor of degree 3 (essentially, it returns a matrix of column vectors), but I don't calculating them in a different way which will make the implementation of the covariant derivative function easier.

• I did search for such implementations, but I didn't find any implementations which differ between contravectors and covectors (up and down indices).


  • $\begingroup$ Have you seen Compute covariant derivative in Mathematica? $\endgroup$
    – MarcoB
    May 3, 2022 at 17:57
  • $\begingroup$ @MarcoB Yes I have, I'm looking for a example which works with higher dimensions. $\endgroup$
    – Amit Zach
    May 3, 2022 at 18:59
  • $\begingroup$ @AmitZach have a look at my answer here. The computation of the Christoffel is for one index up and two down. I think that the logic is clear, but please, feel free to ask any questions $\endgroup$
    – bmf
    May 3, 2022 at 20:59

1 Answer 1


I do not really understand the problem implementing the definition $$ \begin{align} {(\nabla_{e_c} T)^{a_1 \ldots a_r}}_{b_1 \ldots b_s} = {} &\frac{\partial}{\partial x^c}{T^{a_1 \ldots a_r}}_{b_1 \ldots b_s} \\ &+ \,{\Gamma ^{a_1}}_{dc} {T^{d a_2 \ldots a_r}}_{b_1 \ldots b_s} + \cdots + {\Gamma^{a_r}}_{dc} {T^{a_1 \ldots a_{r-1}d}}_{b_1 \ldots b_s} \\ &-\,{\Gamma^d}_{b_1 c} {T^{a_1 \ldots a_r}}_{d b_2 \ldots b_s} - \cdots - {\Gamma^d}_{b_s c} {T^{a_1 \ldots a_r}}_{b_1 \ldots b_{s-1} d}. \end{align} $$ (taken from wikipedia) in Mathematica


should do the trick. I have not checked this extensively but at least for one covariant derivative of a mixed second rank tensor (energy momentum conservation TOV eq.) it works

gdd=DiagonalMatrix[{-Exp[\[Nu][r]],Exp[\[Lambda][r]],r^2,Sin[\[Theta]]^2 r^2}];
\[CapitalGamma]ddd=Table[1/2 (D[gdd[[c,a]],q[[b]]]+D[gdd[[c,b]],q[[a]]]-D[gdd[[a,b]],q[[c]]]),{c,1,dim},{a,1,dim},{b,1,dim}];


Energy conservation

Some tips for writing code concerning Ricci/tensor calculus like this: do not use matrix/vector multiplication but rather write out the sums explicitly to avoid any confusion about placement of indices. Writing a wrapper for tensor-type objects which holds the elements as well as the index structure might also be a good idea to keep the information about the index location and data in one place. E.g.:


gdd=tensor[DiagonalMatrix[{-Exp[\[Nu][r]],Exp[\[Lambda][r]],r^2,Sin[\[Theta]]^2 r^2}],{"d","d"}]

tensor object

this would make passing around the index locations for a given object easier and less error prone but is of course not necessary.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.