I seek to replace derivatives like D[u[x, y], x, x] which are evaluated as $u^{(2,0)}[x,y]$ by variables with names like uxx. Derivatives that I work with are denoted by their "order-vector", for example {2,0} is the vector for this particular derivative and {1,1} is for uxy. I have a list of different derivative vectors and I want to create rule for replacement which is comprised of elements like $u^{(2,0)}[x,y]\rightarrow uxx$. But for that, I need to transform {2,0} into (2,0) which mathematica doesn't want to do:

ToExpression[StringReplace[ToString[dsT], {"}" -> ")", "{" -> "("}]]

This returns an error though without "ToExpression" the output is exaxtly what I need "(1, 0, 0, 0)". How can I do this conversion for any length of derivative vector?

  • 1
    $\begingroup$ Your ToExpression approach won't work because (1, 0, 0, 0) is not a valid Mathematica expression (try entering it directly and see). Instead, note that $u^{(2,0)}[x,y]$ is actually represented internally as Derivative[2, 0][u][x, y], as you can find out by evaluating FullForm on such an expression. So you should be replacing expressions involving Derivative[2, 0], not involving (2, 0). $\endgroup$ – user484 Mar 14 '18 at 15:38
  • $\begingroup$ @Rahul but neither 2,0 is a valid mathematica expression. How can I put it into Derivative[]? $\endgroup$ – Vsevolod A. Mar 14 '18 at 15:44

Here is one method:

derivativeToSymbol[expr_] /; Nest[Head, expr, 3] === Derivative := Block[{args, tmp},
          args = List @@ expr; tmp = Head[expr];
          Symbol[(ToString @@ tmp) <> 
                 MapThread[ConstantArray, {ToString /@ args, List @@ Head[tmp]}]]]


derivativeToSymbol[D[u[x, y], x, x]]

derivativeToSymbol[D[u[x, y, z], {x, 3}, z]]
| improve this answer | |
  • $\begingroup$ Can I adopt that to unknown number of variables? Like if I know their number beforehand how to write function u as u[t1,t2,t3...tn] automatically with just the number n? $\endgroup$ – Vsevolod A. Mar 14 '18 at 15:42
  • $\begingroup$ Nope. But you should have included that requirement in your question if you really need it. $\endgroup$ – J. M.'s technical difficulties Mar 14 '18 at 15:47
  • $\begingroup$ There is really no way to declare list of arguments for a function? I can generate this "code" by very simple string manipulatios. $\endgroup$ – Vsevolod A. Mar 14 '18 at 15:52

Turned out to be:

dsT = {1, 0, 0, 1, 0};
vars = Table[ToExpression["x" <> ToString[i]], {i, 1, Length[dsT]}];

RowBox[{"Sequence", "@@", "dsT"}], ")"}],
MultilineFunction->None]\)[Sequence @@ vars]
| improve this answer | |

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.