:=, ”(), define and div, grad, curl

I recently posted about : and :=  for defining functional expressions.  I’m starting to enjoy these emoji-like constructions 😉

This is another  colon-equals post.  This time for defining functions involving the maxima differentiation command diff.

Notice below that if we define a function with :=, the naive use of :=diff doesn’t produce a derivative with the expected results upon evaluation.

colonequaldiff

In fact, it’s a good thing that :=diff works like that.  The error with fp(3) above comes from the fact that we’ve actually defined an operator that differentiates the function with respect to the argument we pass…in the case above, differentiating with respect to the symbol u makes sense, while differentiating with respect to the constant 3 doesn’t.

So how to make the derivative function do what we want?  Two ways, that are subtly different, in ways I’m not completely sure of.  More about that when I learn more :-).

First is define,

definediff

Also you can use    ”()       quote-quote with parens around the whole right hand side:

quotequotediff

I used define to write functions for vector valued 3D curves in an earlier post.   In figuring this out, I also learned that the :=diff form is really useful.  Below are three little functions in which I use :=diff to define the vector calculus operators grad, div and curl.  Notice that we pass the function f as an argument, and the :=diff form allows Maxima to differentiate them behind the scenes and return the results of the grad, div, and curl operators as you’d expect. These versions of div, grad and curl behave differently, and for me more as expected, than the functions of those names included in the Maxima vect package.  You can download the .mac file here.

/* Three Maxima functions for the multivariable calculus operators  grad, div, and curl
TheMaximaList.org, 2016
*/

grad(f,x,y,z):=[diff(f,x),diff(f,y),diff(f,z)]$

div(f,x,y,z):=diff(f[1],x)+diff(f[2],y)+diff(f[3],z)$

curl(f,x,y,z):=[ diff(f[3],y)-diff(f[2],z),
diff(f[1],z)-diff(f[3],x),
diff(f[2],x)-diff(f[1],y) ]$

Here is a screenshot showing how to call these functions:

divgradcurl

One thought on “:=, ”(), define and div, grad, curl”

  1. Thank you… Still wondering why there are percent signs in my results but I’m glad you posted this here…

    Like

Leave a comment