### Gradient of a Pure Function

Fabio Cavallini
Osservatorio Geofisico Sperimentale di Trieste
P.O. Box 2011, 34016 Trieste, Italy

cavallini@ogs.trieste.it

The gradient is an operator that associates a vector field to a scalar field. Both scalar and vector fields may be naturally represented in Mathematica as pure functions. However, there is no built-in Mathematica function that computes the gradient. The standard package

has a function to compute gradients,

but operates on expressions like x+y+z rather than pure functions. Moreover, a maximum of three independent variables is allowed, and their names must be fixed a priori. For example:

This note presents and compares four ways to implement the gradient operator for pure functions. The author acknowledges helpful conversations with G. Seriani.

Roman Maeder's Programming in Mathematica [Maeder 1997, 116] contains an elegant implementation of the gradient:

This implementation allows for any number of Cartesian independent variables, but it too operates on expressions and not on pure functions. One can overcome these limitations by defining the operator

We use the symbol &#x25BD; ( \[ EmptyDownTriangle ] ) and not &nabla; (), which is an prefix operator. The operator computes the gradient of a pure function of an arbitrary number of independent variables. For example, given the pure function

or the alternative forms

the gradient at the generic point is

and analogously for and .

The only limitation is that the functions to which is applied must be defined in terms of Cartesian coordinates. However, in dimension higher than three this is by far the most common situation and, in dimension two or three, similar routines may be written (using the appropriate mathematical formulae) to cope with the spherical, cylindrical, or any other coordinate system. The main problem with is that the derivatives are re-evaluated each time the gradient is applied. Hence, it is not efficient for repeated numerical computations.

For pure functions, shorter and more efficient code is possible:

However, does not work with the alternative forms or .

A third solution has the flexibility of and the efficiency of ; the only penalty for the improvement is that we must use an extra argument (the number n of independendent variables):

For example:

One might argue that the preceding solutions constitute, in some sense, a way of "cheating." Indeed, these algorithms temporarily create an expression from a pure function, then perform the derivatives, and turn the result into a function again, but do not handle the abstract differential operators as such. The real problem is the lack of an "operator algebra" in Mathematica; that is, rules for manipulating operators. A step in this direction is achieved with the package Notation.m.

We introduce the notation

which applies a function of operators to the same argument.

We may define the -dimensional gradient as

applied to and evaluated at yields

and similarly for .

With this formalism, any differential operator is easily constructed. The -dimensional divergence

gives, for example,

Here is the -dimensional Laplacian:

The operator is applicable to functions defined in any of the three possible ways (as , , or above), but is not that fast or efficient. is the best solution when the given function is defined as , but does not work in the other cases. Possibly the best compromise in terms of flexibility, efficiency, and conciseness is obtained by . Finally, has the same good qualities as and is even more elegant from the mathematical point of view.

Document converted by Mathematica of Wolfram Research