4
$\begingroup$

It is general knowledge, that when we deal with derivatives of function in Mma we have to inevitably indicate variables of such functions. Namely, this

 D[f[x],x] 

is correct, while this

D[f,x]

is not, returning zero.

However, as soon as one has a differential expression, especially one containing multiple partial derivatives one faces a too lengthy-expression. It is often difficult to look at, leave alone to work with.

As a workaround, I imagine that it would be very helpful if one could define a function with virtual variables. Namely, I dream about a possibility to define a variable, say, f depending on coordinates f[x,y,z] such that the part [x,y,z] would be invisible, though Mma is aware of its existance and treat it as if it is written down with the coordinates. In this case,

D[f, x]

would return

enter image description here

instead of zero.

If such a definition of virtual variables is possible, the advantage would be in a dramatic shortening of expressions containing differential operations.

Any ideas?

$\endgroup$
1
  • 1
    $\begingroup$ Somewhat related. My last choice is: for inputs, use With e.g. With[{f=f[x, y, z]}, D[f, x, y]]; for outputs, use functions here. $\endgroup$ – xzczd Mar 12 at 8:42
7
$\begingroup$

As mentioned in the comment above, I personally prefer explicitly use With and functions here to simplify inputs and outputs related to ODE/PDE, but still, it's possible to achieve what you want, we just need to make use of $Pre and $PrePrint:

Clear[$Pre, $PrePrint];

rule = {u -> u[x, y], v -> v[x, y], p -> p[x, y]};

$Pre = Function[expr, Unevaluated@expr /. rule, HoldAll];

$PrePrint = 
  Function[expr, 
   TraditionalForm[
    expr /. Derivative[inds__][head_][vars__] :> 
       Apply[Defer[D[head, ##]] &, 
        Transpose[{{vars}, {inds}}] /. {{var_, 0} :> Sequence[], {var_, 1} :> {var}}] /.
     Reverse /@ rule]];

The code in $PrePrint is a modified version of pdConv.

Now we can do something like following:

enter image description here

In the GIF I've directly worked on TraditionalForm of the code, you may transform the code to InputForm with Ctrl+Shift+i, or StandardForm with Ctrl+Shift+n.

However, do notice the approach has at least one side effect i.e. you can no longer use u, v, p for defining functions:

f[u_] := u

Pattern::patvar: First element in pattern Pattern[u[x,y],_] is not a valid pattern name.

$\endgroup$
3
  • $\begingroup$ Thank you. Please comment a bit of how do you use the functions $Pre and $PrePrint. Out of the movie it seems that having once evaluated the code above, you then simply type the operators like D[u,x]. Right? $\endgroup$ – Alexei Boulbitch Mar 13 at 11:51
  • $\begingroup$ @AlexeiBoulbitch Yeah, exactly. $\endgroup$ – xzczd Mar 13 at 11:55
  • $\begingroup$ Thank you very much. $\endgroup$ – Alexei Boulbitch Mar 13 at 11:56
2
$\begingroup$

You can try anonymous functions. here are some examples:

f = #^2 &
(* #1^2 & *)

Derivative[1][f]
(* 2 #1 & *)

f'
(* 2 #1 & *)

Or with 3 arguments:

f = Sqrt[#1^2 + #2^2 + #3^2] &
(* Sqrt[#1^2 + #2^2 + #3^2] & *)

Derivative[1, 0, 0][f]

enter image description here

$\endgroup$
2
  • $\begingroup$ I'm sorry, but I don't think this is what OP asks for. According to the description in the question, OP is looking for a way to simplify the representation of ODE/PDE. $\endgroup$ – xzczd Mar 12 at 9:29
  • $\begingroup$ This is not what I had in mind. The approach of @xzczd seems to be much closer. $\endgroup$ – Alexei Boulbitch Mar 13 at 11:54

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.