7
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – galadog Feb 4 '15 at 15:54
  • 2
    $\begingroup$ 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. $\endgroup$ – Jens Mar 5 '15 at 17:24
  • 1
    $\begingroup$ 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. $\endgroup$ – kirma Mar 5 '15 at 19:51
  • 1
    $\begingroup$ closely related: Having the derivative be an operator $\endgroup$ – Kuba Mar 9 '17 at 13:30
  • 1
    $\begingroup$ 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. $\endgroup$ – Szabolcs Mar 9 '17 at 14:08
6
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ I have merged these questions moving your answer in the process, as requested. $\endgroup$ – Mr.Wizard Mar 12 '17 at 6:33
4
$\begingroup$

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\}$

$\endgroup$
  • $\begingroup$ 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]? $\endgroup$ – TehHO Feb 3 '15 at 9:48
  • $\begingroup$ @TehHO Possibly so, but my first attempt failed. I'll work on it. $\endgroup$ – Mr.Wizard Feb 3 '15 at 9:58
  • $\begingroup$ 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. $\endgroup$ – xzczd Mar 12 '17 at 4:44
0
$\begingroup$

Try

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

then

grad[x + y]

returns

{1, 1, 0}
$\endgroup$

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.