I'm trying to write a module. Its input is a matrix (tensor), the module should return new tensor with increased rank with 1. The new tensor is defined as $$ O \Rightarrow N_{\alpha \beta \ldots \sigma}^{\gamma \delta \ldots}=\frac{\partial}{\partial x^{\sigma}} O_{\alpha \beta \ldots}^{\gamma \delta \ldots} $$ where $O$ is input ("old tensor") and $N$ is new tensor returned by module. Since the module should work for tensor of general rank (general number of indices), i try to obtain rank of input tensor, generate list of indices representing elements and set them in Do loop (see commentary in code). The problem is in setting values (element by element) in Do loop:
PartialDerivative[tensor_] :=
Module[{tens = tensor, oldorder, neworder, dlist, newtensor, \[Mu],
i, doparams, \[Alpha], l, d, r},
(*coordinates, derivate with respect to them*)
r = {t, x, y, z};
(*dimension*)
d = 4;
(*rank of input tensor*)
oldorder = ArrayDepth[tens];
(*rank of new tensor*)
neworder = oldorder + 1;
(*create list {d,d,...d} for creation of new tensor*)
dlist = Table[d, {i, neworder}];
(*create new dummy tensor with correct size *)
newtensor = SparseArray[{}, dlist];
(*now I try to create iterator list for last Do loop*)
l = {};
(*
lets join all individual iterators, each is type {\[Mu][i], 1, d}
e.g. the first one is {\[Mu][1], 1, d}
and join them into one list
*)
Do[l = Join[l, {{\[Mu][i], 1, d}}], {i, 1, oldorder}];
(*join the last iterator - the derivative index*)
l = Join[l, {{\[Alpha], 1, d}}];
(*now create Sequence from the list*)
doparams = Delete[l, 0];
(*now assign elements of the new tensor from the old tensor - Error line*)
Do[newtensor[[Delete[Array[\[Mu], d], 0], \[Alpha]]] = \!\(
\*SubscriptBox[\(\[PartialD]\), \
\(r[\([\[Alpha]]\)]\)]\(tens[\([Delete[Array[\[Mu], d], 0]]\)]\)\),
doparams];
Return[newtensor];
]
(*calling example - should return identity matrix 4x4*)
PartialDerivative[{t, x, y, z}]
Everything seems to work fine until the the Error line (see code, the line above returning new tensor), where I get error
Argument doparams$22092 at position 2 does not have the correct form for an iterator.
Do you know where is mistake? Or is there another or simpler way to write the function?
Thank you for tips.