# How to change the dimension of a tensor inside a loop using XAct/XTensor?

I'd like to define a function CDOrder[k] to compute the k-th order covariant derivative of a vector A[mu] (or, more generally: a one-by-one growing chain of covariant derivatives starting from some "initial" index to a "final" one specified by me as arguments of that function).

I was thinking about using a Do loop to evaluate iteratively the covariant derivatives of the vector until I get the desired order, however, it doesn't seem to work nice, mainly because, in each iteration I'd need to redefine the tensor taking into account the extra dimension added by the actual covariant derivative in order to avoid the next one to be calculated the wrong way (I suppose).

This is all I've managed to do until now:

Some initialization stuff:

Block[{Print}, << xActxTensor]
Block[{Print}, << xActxPert]
Block[{Print}, << xActxPand]
Block[{Print}, << xActxCoba]
Block[{Print}, << xActxTras]

DefManifold[M, 4, {α, β, γ, μ, ν, ρ, σ, τ, λ, χ, η, κ, ζ}]
DefMetric[-1, g[-α, -β], CD, {";", "∇"}, PrintAs -> "ḡ"];
DefTensor[A[-μ], M]
PrintAs[A] ^= "A";


A first trial of a function:

CD[initial_, final_, μ_] :=
Module[
{list = {}},
If[initial <= final,
Do[AppendTo[list, -μ[i]], {i, final, initial, -1}],
Do[AppendTo[list, -μ[i]], {i, final, initial, 1}]
];
DefTensor[ans[α], M];
ans[α] = A[α];
Do[ ans[α] = CD[index][ans[α]], {index, list}];
ans[μ]
]


Some tests: (The first 3 work well and the others don't) - I could conclude that, inside the loop, I'm assigning the value of the new tensor to a single component of the tensor aux, i.e. to aux[alpha] which is interpreted as different from aux[mu]. - I'd have to fix this in order to make things go fine but I don't how to do it yet.

CD[1, 6, α]
CD[6, 1, α]
CD[, , α]

CD[1, 6, μ]
CD[6, 1, μ]
CD[, , μ]


I'd appreciate some tips of you, guys. If you can suggest some changes.

• Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of your code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. Aug 22 '18 at 13:16
• What is M1? I guess the index b belongs to the tangent bundle of M1? Note that you have defined the tensor A with two indices, but then you use it with one index only in the covariant derivative.
– jose
Aug 22 '18 at 21:47
• Ahhhh, you're right Jose, but, for my purposes I just need one index with the vector A so I will edit the original post and let's focus on that scenario. (the b index that you mentioned was supposed to belong to a second metric that's not necessary to consider at this point). The problem is still there. Aug 22 '18 at 21:57
• OK. Then I've edited my answer to use A with only one index.
– jose
Aug 22 '18 at 22:42
• Note also that a tensor called A will print as "A" by default, so you don't need the PrintAs definition above.
– jose
Aug 22 '18 at 22:47

CD is the Levi-Civita covariant derivative associated with the metric g. You cannot add new definitions to it as you did. On the other hand, you have declared that the indices for M are {α, β, γ, μ, ν, ...}. You cannot use α[1] as an index. It must be α itself.

Given the tensor A[-α], the first derivative, with index -β, is CD[-β][A[-α]]. The second derivative, with index -γ, is CD[-γ][CD[-β][A[-α]]], etc. So imagine you want to create a function that gives you high order derivatives of A. You could do this:

CDA[aind_, dinds_] := Construct[RightComposition @@ Map[CD, dinds], A[aind]]


Then try (note how the indices are inserted in the order they are specified, and hence they appear sorted in reverse order):

CDA[-α, {-μ}]
(* CD[-μ][A[-α]] *)

CDA[-α, {-μ, -ν}]
(* CD[-ν][CD[-μ][A[-α]]] *)

CDA[-α, {-μ, -ν, -λ}]
(* CD[-λ][CD[-ν][CD[-μ][A[-α]]]] *)

CDA[-α, {-μ, -ν, -λ, -σ}]
(* CD[-σ][CD[-λ][CD[-ν][CD[-μ][A[-α]]]]] *)

• Thanks jose, this is exactly what I was looking for. Btw, do you know how to implement the alphanumeric style of indices? Aug 23 '18 at 7:17
• What do you mean by the "alphanumeric style"? Are you referring to using indices like a1, a2, a3, etc? You can use any symbol as index, and then you can define any PrintAs definition for each index. For example declare DefManifold[M, 4, {a1, a2, a3}] and then do PrintAs[a1] ^= SubscriptBox["a", 1], etc.
– jose
Aug 23 '18 at 13:39
• Again, thanks. That's all I need. Awesome! Aug 24 '18 at 16:23