8

How can I define the nabla operator (also known as Del operator) as a an operator, acting on everything to the right of the operator!

enter image description here

Also taking \[Del]^2 would give the second derivivates.

I wish to be able to write \[Del]f[x,y,z] and then evaluate it according to the nabla operator.

What I have now, is just a simple expression:

Del = {D[#, x], D[#, y], D[#, z]} &

But it would be nice to be able to use the actual \[Del] for notation.

I've looked at different examples including: http://reference.wolfram.com/language/ref/NonCommutativeMultiply.html

But I really don't understand any of what's going on there.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
TehHO
  • 109
  • 1
  • 5
  • I wonder if it is possible to use Del for the divergence and curl operators. The direct substitution of dot and cross products do not work. – galadog Feb 04 '15 at 15:54
  • 2
    Your definition of the square is not consistent with conventional practice, where it would give the laplacian instead of just a vector of second derivatives. I don't think it's a good idea to overload Times and Power with these interpretations. Better use clearly distinct notation to make code easier to understand. – Jens Mar 05 '15 at 17:24
  • 1
    If you are using v10, please check details sections of Grad and Laplacian, and related Div and Curl. These symbols have new input forms quite close to what you're asking for. – kirma Mar 05 '15 at 19:51
  • 1
  • If you now asked me what is the best way to phrase this in Mathematica, I would tell you do define grad, div, curl separately. What people call the "del operator" is a notational trick that humans, who context into account when reading math, find convenient. But it does not fit well with a language/notation that must be unambiguous, such as a computer language. – Szabolcs Mar 09 '17 at 13:37
  • As an aside: I tried using the Grad function instead Grad[vector1.vector2, {x, y, z}] which gives {Subscript[b, x] Subscript^(0,1)(a,x)+Subscript[a, x] Subscript^(0,1)(b,x),Subscript[b, y] Subscript^(0,1)(a,y)+Subscript[a, y] Subscript^(0,1)(b,y),Subscript[b, z] Subscript^(0,1)(a,z)+Subscript[a, z] Subscript^(0,1)(b,z)} What are the superscript referring to? – Physkid Mar 09 '17 at 13:41
  • $f^{(1,0)}$ is how Mathematica formats Derivative[1,0][f] in StandardForm. – Szabolcs Mar 09 '17 at 13:45
  • Is there a way to convert this conventional form to a more "mathematically" familiar form? – Physkid Mar 09 '17 at 13:48
  • 1
    Yes, that is done automatically. Try HoldForm[Curl[v, {x, y, z}]] and see how it formats. This is different from interpreting that form when you enter it. Traditional notation is ambiguous. When you are talking to a computer you want to be clear and unambiguous. – Szabolcs Mar 09 '17 at 14:08

3 Answers3

11

Well, over-pursuing similarity of Mathematica code and traditional math notation does more harm than good, still, it's possible to create a nabla operator in Mathematica, here's my approach:

(*$PrePrint=.*)
$PrePrint = # /. 
    Derivative[id__][f_][args__] :> 
     TraditionalForm[
      HoldForm@D[f[args], ##] &[
       Sequence @@ (DeleteCases[Transpose[{{args}, {id}}], {_, 0}] /. {x_, 1} :> x)]] &;

Clear[$independentVariable, ▽, Δ]
$independentVariable = {x, y, z};
▽ /: ▽ f_ := Grad[f, $independentVariable]
▽·f_ := Div[f, $independentVariable]
▽ /: ▽\[Cross]f_ := Curl[f, $independentVariable]
▽·▽ := Δ
▽ /: ▽^2 := Δ
Δ /: Δ f_ := Laplacian[f, $independentVariable]
CenterDot /: (v_·▽) f_ := Grad[f, $independentVariable].v
CenterDot /: (m_?MatrixQ·▽)\[Cross]f_ := 
 TensorContract[LeviCivitaTensor[3].D[f, {$independentVariable}].m\[Transpose], {2, 3}]

Usage:

▽ f[x, y, z]

Mathematica graphics

▽·{f[x, y, z], g[x, y, z], h[x, y, z]}

Mathematica graphics

▽\[Cross]{f[x, y, z], g[x, y, z], h[x, y, z]}

Mathematica graphics

▽·▽ f[x, y, z]
▽^2 f[x, y, z]
Δ f[x, y, z]

Mathematica graphics

({Subscript[v, x], Subscript[v, y], Subscript[v, z]}·▽) f[x, y, z]

Mathematica graphics

({Subscript[v, x], Subscript[v, y], Subscript[v, z]}·▽){f[x, y, z], g[x, y, z],h[x, y, z]}

Mathematica graphics

Alst = Array[Subscript[A, ##] &, {3, 3}];
vector = #[x, y, z] & /@ {u, v, w};
test1 = (Alst·▽)\[Cross]vector

Mathematica graphics

Notice the · is \[CenterDot], and is \[EmptyDownTriangle] rather than \[Del].

To type easier, try the technique in this and this post.

Not sure if the solution will fail in more complicated cases.

BTW the $PrePrint function is modified from the code here.

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

Does it not work as written?

enter image description here

Del = {D[#, x], D[#, y], D[#, z]} &;

∇f[x, y, z]

$\left\{f^{(1,0,0)}(x,y,z),f^{(0,1,0)}(x,y,z),f^{(0,0,1)}(x,y,z)\right\}$


To extend this in the way that I believe you want you can use the Notation Package.

First:

Needs["Notation`"];

Then paste and evaluate:

Cell[BoxData[
 RowBox[{"Notation", "[", 
  RowBox[{
   TemplateBox[{RowBox[{
       SuperscriptBox["\[Del]", "n_"], "expr_"}]},
    "NotationTemplateTag"], " ", "\[DoubleLongRightArrow]", " ", 
   TemplateBox[{RowBox[{
       RowBox[{"del", "[", "n_", "]"}], "[", "expr_", "]"}]},
    "NotationTemplateTag"]}], "]"}]], "Input"]

Which should look like this in the Notebook:

enter image description here

(Or enter the equivalent using the Notation Palette.)

Then add these definitions:

Del = del[1];

del[n_Integer?Positive][expr_] := D[expr, {#, n}] & /@ {x, y, z}

Finally:

enter image description here

$\left\{f^{(1,0,0)}(x,y,z),f^{(0,1,0)}(x,y,z),f^{(0,0,1)}(x,y,z)\right\}$

$\left\{f^{(2,0,0)}(x,y,z),f^{(0,2,0)}(x,y,z),f^{(0,0,2)}(x,y,z)\right\}$

$\left\{f^{(3,0,0)}(x,y,z),f^{(0,3,0)}(x,y,z),f^{(0,0,3)}(x,y,z)\right\}$

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • It does seem to Work for just

    \[del], But do you also know if there is any way to make mathematica interpret the \[Del]^2f[x,y,z] as the second derivatives of the function f[x,y,z]?

    – TehHO Feb 03 '15 at 09:48
  • @TehHO Possibly so, but my first attempt failed. I'll work on it. – Mr.Wizard Feb 03 '15 at 09:58
  • Well, as far as I can tell, \[Del]^2 represent Laplacian and there is no definition for \[Del]^3, see the corresponding wiki page for more details. – xzczd Mar 12 '17 at 04:44
2

Try

grad[f_] := {D[f, x], D[f, y], D[f, z]}

then

grad[x + y]

returns

{1, 1, 0}
Erich Mueller
  • 451
  • 3
  • 9