4

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?

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96

2 Answers2

7

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.

xzczd
  • 65,995
  • 9
  • 163
  • 468
2

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

Daniel Huber
  • 51,463
  • 1
  • 23
  • 57
  • 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. – xzczd Mar 12 '21 at 09:29
  • This is not what I had in mind. The approach of @xzczd seems to be much closer. – Alexei Boulbitch Mar 13 '21 at 11:54