You are on page 1of 7

Syntax

f '
D(f)
D([n1, n2, ], f)
Description
D(f) or, alternatively, f' computes the derivative of the univariate function f.
D([n1, n2, ...], f) computes the partial derivative of the multivariate function f(x1, x2, ).
MuPAD

has two functions for differentiation: diff and D. D represents the differential operator that may be applied to functions; diff is used to
differentiate arithmetical expressions. Mathematically, D(f)(x) coincides withdiff(f(x), x); D([1, 2], f)(x, y) coincides
with diff(f(x, y), x, y). Symbolic calls of D and diff can be converted to one another via rewrite. Cf. Example 8.
D(f) returns the derivative of the univariate functionf. f' is shorthand for D(f).
If f is a multivariate function and denotes the partial derivative of f with respect to its n-th argument, thenD([n1, n2, ...], f) computes the
partial derivative . Cf. Example 5. In particular, D([ ], f) returnsf itself.
Note: It is assumed that partial derivatives commute. Internally, D([n1, n2, ...], f) is converted
to D([m1, m2, ...], f), where [m1, m2, ...] = sort([n1, n2, ...]).
f may be any object which can represent a function. In particular, f may be a functional expression built from simple functions by means of arithmetic operators
(+, -, *, /, ^, @, @@). Any identifier different from CATALAN, EULER, and PIis regarded as an "unknown" function; the same holds for elements of kernel
domains not explicitly mentioned on this page. Cf. Example 1. Any number and each of the three constant identifiers above is regarded as a constant function.
Cf. Example 2.
If f is a list, a set, a table, or an array, then D is applied to each entry of f. Cf. Example 3.
A polynomial f of type DOM_POLY is regarded as polynomial function, the indeterminates being the arguments of the function. Cf. Example 6.
If f is a function environment, a procedure, then D can compute the derivative in some cases; see the "Background" section below. If this is not possible, a
symbolic D call is returned.
Higher partial derivatives D([n1], D([n2], f)) are simplified to D([n1, n2], f). Cf. Example 7.
The derivative of a univariate function f denoted by D(f) is syntactically distinguished from the partial derivativeD([1], f) with respect to the first
variable, even if f represents a univariate function.
The usual rules of differentiation are implemented:
D(f + g) = D(f) + D(g),
D(f * g) = f * D(g) + g * D(f),
D(1/f) = -D(f) / f^2,
D(f @ g) = D(f) @ g * D(g).
Note that the composition of functions is written as f@g and not as f(g).
In order to express the n-th derivative of a univariate function for symbolic n, you can use the "repeated composition operator" @@. Cf. Example 9.
Environment Interactions
D uses option remember.
Examples
Example 1
D(f) computes the derivative of the function f:
D(sin), D(x -> x^2), D(id)

D also works for more complex functional expressions:
D(sin @ exp + 2*(x -> x*ln(x)) + id^2)

If f is an identifier without a value, a symbolic D call is returned:
delete f: D(f + sin)

The same holds for objects of kernel type that cannot be regarded as functions:
D(NIL)

f' is shorthand for D(f):
(f + sin)', (x -> x^2)', id'

Example 2
Constants are regarded as constant functions:
PI', 3', (1/2)'

Example 3
The usual rules of differentiation are implemented. Note that lists and sets may also be taken as input; in this case,D is applied to each element of the list or set:
delete f, g: D([f+g, f*g]); D({1/f, f@g})


Example 4
The derivatives of most special functions of the library can be computed. Again, id denotes the identity function:
D(tan); D(sin*cos); D(1/sin); D(sin@cos); D(2*sin + ln)





Example 5
D can also compute derivatives of procedures:
f := x -> x^2:
g := proc(x) begin tan(ln(x)) end:
D(f), D(g)

We differentiate a function of two arguments by passing a list of indices as first argument to D. In the example below, we first differentiate with respect to the
second argument and then differentiate the result with respect to the first argument:
D([1, 2], (x, y) -> sin(x*y))

The order of the partial derivatives is not relevant:
D([2, 1], (x, y) -> sin(x*y))

delete f, g:
Example 6
A polynomial is regarded as a polynomial function:
D(poly(x^2 + 3*x + 2, [x]))

We differentiate the following bivariate polynomial f twice with respect to its second variable y and once with respect to its first variable x:
f := poly(x^3*y^3, [x, y]):
D([1, 2, 2], f) = diff(f, y, y, x)

delete f:
Example 7
Nested calls to D are flattened:
D([1], D([2], f))

However, this does not hold for calls with only one argument, since D(f) and D([1], f) are not considered to be the same:
D(D(f))

Example 8
D may only be applied to functions whereas diff makes only sense for expressions:
D(sin), diff(sin(x), x)

Applying D to expressions and diff to functions makes no sense:
D(sin(x)), diff(sin, x)

rewrite allows to rewrite expressions with D into diff-expression:
rewrite(D(f)(y), diff), rewrite(D(D(f))(y), diff)

The reverse conversion is possible as well:
map(%, rewrite, D)

Example 9
Sometimes you may need the n-th derivative of a function, where n is unknown. This can be achieved using the repeated composition operator. For example, let
us write a function that computes the k-th Taylor polynomial of a function f at a point x0 and uses x as variable for that polynomial:
kthtaylorpoly:=
(f, k, x, x0) -> _plus(((D@@n)(f)(x0) * (x - x0)^n / n!) $ n = 0..k):
kthtaylorpoly(sin, 7, x, 0)

delete kthtaylorpoly:
Example 10
Advanced users can extend D to their own special mathematical functions (see "Background" section below). To this end, embed your mathematical function f,
say, into a function environmentf and implement the behavior of D for this function as the "D" slot of the function environment. The slot must handle two cases: it
may be either called with only one argument which equals f, or with two arguments where the second one equals f. In the latter case, the first argument is a list
of arbitrary many indices; that is, the slot must be able to handle higher partial derivatives also.
Suppose, for example, that we are given a function f(t, x, y), and that we do not know anything about f except that it is differentiable infinitely often and satisfies the
partial differential equation . To make MuPAD eliminate derivatives with respect to t, we can do the following:
f := funcenv(f):
f::D :=
proc(indexlist, ff)
local
n : DOM_INT, // Number of t-derivatives.
list_2_3 : DOM_LIST; // List of indices of 2's and 3's.
// These remain unchanged.
begin
if args(0) <> 2 then
error("Wrong number of arguments")
end_if;
n := nops(select(indexlist, _equal, 1));
list_2_3 := select(indexlist, _unequal, 1);
// rewrite (d/dt)^n = (d^2/dx^2 + d^2/dy^2)^n
_plus(binomial(n, k) *
hold(D)(sort([2 $ 2*(n-k), 3 $ 2*k].list_2_3), ff)
$ k = 0..n)
end_proc:
Now, partial derivatives with respect to the first argument t are rewritten by derivatives with respect to the second and third argument:
D([1], f^2)(t, x, y)

D([1, 2, 1], f)

delete f:
Parameters
f A function or a functional expression, an array, a list, a polynomial, a set, or a table
n1, n2, Indices: positive integers
Return Values
function or a functional expression. If f is an array or a list etc., a corresponding object containing the derivatives of the entries is returned.
Overloaded By
f
Algorithms
If f is a domain or a function environment with a slot"D", this slot is called to compute the derivative. The slot procedure has the same calling syntax as D. In
particular and in contrast to the slot"diff" the slot must be able to compute higher partial derivatives because the list of indices may have length greater
than one. Cf. Example 10.
If f is a procedure, a function environment without a "D" slot, then f is called with auxiliary identifiers as arguments. The result of the call is then differentiated
using the function diff. If the result of diff yields an expression which can be regarded as function in the auxiliary identifers, then this function is returned,
otherwise an unevaluated call ofD is returned.
Let us take the function environmentsin as an example. It has no "D" slot, thus the procedure op(sin, 1), which is responsible for evaluating the sine
function, is used to compute D(sin), as follows. This procedure is applied to an auxiliary identifier, say x, and differentiated with respect to this identifier
via diff. The result is diff(sin(x), x) = cos(x). Via fp::expr_unapply and fp::unapply, the function cos is computed as the
derivative of sin.
See Also
Syntax
diff(f)
diff(f, x)
diff(f, x1, x2, )
Description
diff(f, x) computes the derivative of the function f with respect to the variable x.
diff(f, x) computes the partial derivative of the arithmetical expression (or polynomial) f with respect to the indeterminate x.
diff(f) computes the 0th derivative of f. Since the 0th derivative of f is f itself, diff(f) returns its evaluated argument.
diff(f, x1, x2, ...) is equivalent to diff(...diff(diff(f, x1), x2)...). In both cases, MuPAD

first differentiates f with respect


to x1, then differentiates the result with respect to x2, and so on. The result is the partial derivative . See Example 2.
If you use nested diff calls, the system internally converts them into a single diff call with multiple arguments. See Example 3.
When computing the second and higher derivatives, use the sequence operator as a shortcut. If n is a nonnegative integer, diff(f, x $ n) returns the nth
derivative of f with respect to x. See Example 4.
The indeterminates x, x1, x2, ... must be identifiers of domain type DOM_IDENT or indexed identifiers of the form x[n] where x is an identifier
and n is an integer. If any indeterminate comes in any other form, MuPAD returns an unresolved diff call. See Example 5.
If f is an arithmetical expression, diff returns an arithmetical expression. If f is a polynomial, diff returns a polynomial. See Example 6.
If the system cannot compute the derivative, it returns an unresolved diff call. See Example 7.
MuPAD assumes that partial derivatives with respect to different indeterminates commute. The function callsdiff(f, x1, x2) and diff(f, x2,
x1) produce the same result diff(f, y1, y2). Here [y1, y2] =sort([x1, x2]). See Example 8.
MuPAD provides two functions, diff and D, for computing derivatives. Use the differential operator D to compute the derivatives of functions. Use
the diff function to compute the derivatives of arithmetical expressions. Mathematically, D(f)(x) coincides with diff(f(x), x) and D([1, 2],
f)(x, y) coincides with diff(f(x, y), x, y). You can convert symbolic calls of D to the calls of diff and vice versa by using rewrite.
See Example 10.
You can extend the functionality of diff for your own special mathematical functions via overloading. This approach works by turning the corresponding function
into a function environment and implementing the differentiation rule for the function as the "diff" slot of the function environment.
If a subexpression of the form g(..) occurs in f, and g is a function environment, then diff(f, x) attempts to call the "diff" slot of g to determine
the derivative of g(..).
The system calls the "diff" slot with the arguments g(..), x.
If g does not have a "diff" slot, then the system function diff returns the symbolic expression diff(g(..), x)for the derivative of the
subexpression.
The system always calls the "diff" slot with exactly two arguments. If you call the diff function with more indeterminates (for example, if you compute a
higher derivative), then MuPAD calls the "diff" slot several times. Each call computes the derivative with respect to one indeterminate. The system caches the
results of the calls of"diff" slots in diff in order to prevent redundant function calls. See Example 11.
Similarly, if an element d of a library domain T occurs as a subexpression of f, then diff(f, x) calls the slotT::diff(d, x) to compute the derivative
of d.
If the domain T does not have a "diff" slot, then diff considers this object as a constant and returns 0 for the corresponding subexpression.
Examples
Example 1
Compute the derivative of x
2
with respect to x:
diff(x^2, x)

Example 2
You can differentiate with respect to multiple variables within a single diff call. For example, differentiate this expression with respect to x, and then with
differentiate the result with respect to y:
diff(x^2*sin(y), x, y) = diff(diff(x^2*sin(y), x), y)

Example 3
MuPAD internally converts nested diff calls into a single diff call with multiple arguments:
diff(diff(f(x, y), x), y)

Example 4
Use the sequence operator $ as a shortcut to compute the third derivative of this expression with respect to x:
diff(sin(x)*cos(x), x $ 3)

Example 5
You can differentiate with respect to an indexed identifier. For example, differentiate this expression with respect tox[1]:
diff(x[1]*y + x[1]*x[r], x[1])

Example 6
You can differentiate polynomials with respect to the polynomial indeterminates or the parameters in the coefficients. For example, differentiate this
polynomial with respect to the indeterminate x:
diff(poly(sin(a)*x^3 + 2*x, [x]), x)

Now differentiate the same polynomial with respect to its symbolic parameter a:
diff(poly(sin(a)*x^3 + 2*x, [x]), a)

Example 7
MuPAD returns the derivative of an unknown function as an unresolved diff call:
diff(f(x) + x, x)

Example 8
MuPAD assumes that all partial derivatives with respect to different indeterminates commute. Therefore, the system can change the order of indeterminates:
diff(f(x, y), x, y) = diff(f(x, y), y, x);

Example 9
You can use diff to differentiate symbolic integrals. For example, compute the second derivative of this indefinite integral:
F1 := int(f(x), x):
diff(F1, x, x)

Now compute the derivative of the definite integral:
F2 := int(f(t, x), t = x..x^2):
diff(F2, x)

Example 10
Use the operator D to compute the derivatives of functions. Use the diff function to compute the derivatives of expressions:
D(sin), diff(sin(x), x)

Applying D to expressions and diff to functions makes no sense:
D(sin(x)), diff(sin, x)

Use the rewrite function to rewrite an expression replacing the operator D with the diff function:
rewrite(D(f)(x), diff), rewrite(D(D(f))(x), diff)

Also, use rewrite to rewrite an expression replacing diff with D:
diff(f(x, x), x) = rewrite(diff(f(x, x), x), D)

Example 11
You can extend diff to your own special functions. To do so, embed your function, f, into a function environment, g, and implement the behavior of diff for
this function as the "diff" slot of the function environment.
If a subexpression of the form g(..) occurs in an expression f, then diff(f, x) calls g::diff(g(..), x) to determine the derivative of the
subexpression g(..).
This example demonstrates extending diff to the exponential function. Since the function environment exp already has a "diff" slot, call the new function
environment Exp to avoid overwriting the existing system function exp.
Here, the "diff" slot implements the chain rule for the exponential function. The derivative is the product of the original function call and the derivative of the
argument:
Exp := funcenv(Exp):
Exp::diff := proc(f, x)
begin
// f = Exp(something), i.e., something = op(f, 1)
f*diff(op(f, 1), x):
end_proc:
diff(Exp(x^2), x)

The report created by prog::trace shows one call to Exp::diff with two arguments. Instead of callingExp::diff twice, the system reads the
required result of the second call from an internal cache for intermediate results in diff:
prog::trace(Exp::diff):
diff(Exp(x^2), x, x)
enter Exp::diff(Exp(x^2), x)
computed 2*x*Exp(x^2)

prog::untrace(Exp::diff): delete f, Exp:
Parameters
f An arithmetical expression or a polynomial of type DOM_POLY
x, x1, x2, Indeterminates: identifiers or indexed identifiers
Return Values

You might also like