Gradient of a Pure FunctionFabio Cavallini 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
but
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 ▽ ( \[ EmptyDownTriangle ] ) and not ∇ (
or the alternative forms
the gradient at the generic point
and analogously for
The only limitation is that the functions to which For pure functions, shorter and more efficient code is possible:
However, A third solution has the flexibility of
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
For example, the two-dimensional gradient
applied to
and similarly for With this formalism, any differential operator is easily
constructed. The
gives, for example,
Here is the
The operator Document converted by Mathematica of Wolfram Research |