2
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

Found the solution. The problem was somehow in the variables $\mu[i]$, so instead i used variables automatically generated variables

Unique["\[Mu]"]

which are unique and are numbered in way $\mu 12, \mu 35, ...$, so there is no problem with brackets. Also it was needed to call evaluate inside Do.

PartialDerivative[tensor_] := Module[
  {tens = tensor, oldorder, neworder, dlist, newtensor, \[Mu], i, 
   doparams, \[Alpha], l, l2, d, r, variables},

  r = {t, x, y, z};
  d = 4;
  oldorder = ArrayDepth[tens];
  neworder = oldorder + 1;
  dlist = Table[d, {i, neworder}];
  variables = Table[Unique["\[Mu]"], {oldorder}];
  newtensor = SparseArray[{}, dlist];
  l = Table[{variables[[j]], 1, d}, {j, 1, oldorder}];
  l2 = Join[l, {{\[Alpha], 1, d}}];
  Print[l];
  Print[l2];
  doparams = Delete[l2, 0];
  Print[doparams];

  Do[newtensor[[Delete[variables, 0], \[Alpha]]] = \!\(
\*SubscriptBox[\(\[PartialD]\), \(r[\([\)\(\[Alpha]\)\(]\)]\)]\(tens[\
\([Delete[variables, 0]]\)]\)\), doparams // Evaluate];
  Return[newtensor];
  ]
PartialDerivative[{t, x, y, z}]
$\endgroup$
  • $\begingroup$ Shouldn't r and d be determined from the input, not hard-coded? $\endgroup$ – Mr.Wizard Apr 18 '15 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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