# xAct xCoba: How to extract components of CTensors correctly

I'm working in a specific chart ch with a metric and associated covariant derivative cd:

<< xActxTensor
<< xActxCoba

DefManifold[M4, 4 , {a, b, c, d}];

DefChart[ch, M4, {0, 1, 2, 3}, {t[], r[], \[Theta][], \[CurlyPhi][]}];

DefScalarFunction[ff]
metric = CTensor[DiagonalMatrix[{-ff[r[]], (ff[r[]])^-1, r[]^2, r[]^2 Sin[\[Theta][]]^2}], {-ch,-ch}];
SetCMetric[metric, ch, SignatureOfMetric -> {3, 1, 0}, SignDetOfMetric -> -1];

cd = CovDOfMetric[metric];


Consider a vector field V defined as a Ctensor in the following way:

 DefScalarFunction /@ {v0, v1, v2, v3};
V = CTensor[{v0[r[]], v1[r[]], 0, 0}, {ch}];


When I compute the following expression

V[b]cd[-b][V[a]]


I get the correct result for the 0-th component:

However, if I try to extract that specific component with

V[b]cd[-b][V[{0,ch}]]


the output is wrong:

Basically xCoba first replaces the index a with {0,ch} and only after computes the covariant derivative, thus losing the term involving the Christoffel symbols. How can I extract specific components in the correct way?

More generally, I would like to define functions as DerV[a_]:=V[b]cd[-b][V[a]] and use them to compute DerV[{0,ch}] correctly.

First of all, let's discuss why the answers are different: V[a] is a vector field, but V[{0, ch}] is a scalar field. Hence the operation cd[-b][V[{0, ch}]] is computing the covariant derivative of a scalar field. This is not the same thing as the 0b component of the tensor cd[-b][V[a]].

I think there are two main ways of computing this without defining an intermediate tensor:

1. Use
    V[b] cd[-b][V[a]] Basis[-a, {0, ch}] // ContractBasis


which contracts the basis from outside the covariant derivative, and not from inside as in your initial attempt.

1. Use
    V[b] TensorDerivative[V, cd][{0, ch}, -b] // Simplify


TensorDerivative[V, cd] produces already the rank 2 tensor of the covariant derivative without the need of indices, and then you can extract components from it in a simpler way.

I should also emphasize here the convention in xAct of appending indices (rather than prepending them) when taking covariant derivatives. Both cd[-b][V[a]] and TensorDerivative[V, cd][a, -b] return tensors with index configuration DerV[a, -b] in which the derivative index has been appended to the index of V.

This is probably not the "right way" to do it within xCoba, but what I typically do is the following:

DerV = Head[V[b] cd[-b][V[a]]]
DerV[{0, ch}] // Simplify

(* v1[r[]] ((v0[r[]] Derivative[1][ff][r[]])/ff[r[]] + Derivative[1][v0][r[]]) *)


Doing it this way means that DerV is itself defined as a CTensor the same way that V is, with a "slot" to accept either an abstract index (for further calculations) or a particular component index.

There may be a more elegant way to do it without using Head (which feels a bit "dangerous", like it might have knock-on effects somewhere else that I don't know about.) The maintainers of xTensor occasionally come by this forum, so maybe they'll be able to provide you with a more idiomatic answer.