D - Maple Programming Help

D

Differential operator

 Calling Sequence D(f) D[i](f) D[i](f)(x, y, ...)

Parameters

 f - expression which can be applied as a function i - positive integer or expression or sequence of such x, y, ... - application point

Description

 • The D command serves different purposes. It is primarily a differentiation command, like diff (see also comparison), but it is more general than diff in two aspects: it can represent derivatives evaluated at a point and can differentiate procedures.
 • As a differentiation command, given f, the name of an unknown function of one argument, the call $\mathrm{D}\left(f\right)\left(x\right)$ represents the derivative of f(x) with respect to x. That is, $\mathrm{D}\left(f\right)\left(x\right)=\frac{ⅆ}{ⅆx}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}f\left(x\right)$. To switch between these two representations use convert/D and convert/diff.
 • In the case of partial derivatives, D identifies the differentiation variables by their numerical position. D[i](f) computes the partial derivative of f with respect to its ith argument.  For example,
 > D[1,1,2](g)(x,y);
 ${{\mathrm{D}}}_{{1}{,}{1}{,}{2}}{}\left({g}\right){}\left({x}{,}{y}\right)$ (1)
 > convert((1), diff);
 $\frac{{{\partial }}^{{3}}}{{\partial }{{x}}^{{2}}{\partial }{y}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{g}{}\left({x}{,}{y}\right)$ (2)
 Note: It is assumed that partial derivatives commute so that ${\mathrm{D}}_{i,j}\left(g\right)={\mathrm{D}}_{j,i}\left(g\right)$. In the univariate case, ${\mathrm{D}}_{1}\left(f\right)\left(x\right)$ automatically simplifies to $\mathrm{D}\left(f\right)\left(x\right)$. The zeroth order derivative is represented by D[](g)(x,y) = $\mathrm{diff}\left(g\left(x,y\right),\left[\right]\right)$ = $g\left(x,y\right)$.
 • The D command can represent derivatives evaluated at a point. For example, the derivative of $f\left(x\right)$ evaluated at $x=0$ is represented by
 > D(f)(0);
 ${\mathrm{D}}{}\left({f}\right){}\left({0}\right)$ (3)
 The evaluation point can be any algebraic expression, not necessarily a constant. Derivatives evaluated at a point can also be expressed using a composition of eval and diff.
 > convert((3), diff);
 $\genfrac{}{}{0}{}{\left(\frac{{ⅆ}}{{ⅆ}{\mathrm{t1}}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({\mathrm{t1}}\right)\right)}{\phantom{\left\{{\mathrm{t1}}{=}{0}\right\}}}{|}\genfrac{}{}{0}{}{\phantom{\left(\frac{{ⅆ}}{{ⅆ}{\mathrm{t1}}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({\mathrm{t1}}\right)\right)}}{\left\{{\mathrm{t1}}{=}{0}\right\}}$ (4)
 • The D command is appropriate to represent partial derivatives at fixed values of certain functions. For example, the partial derivative of $F\left(t,v\left(t\right)\right)$ with respect to t at fixed $v\left(t\right)$ is represented by
 > D[1](F)(t,v(t));
 ${{\mathrm{D}}}_{{1}}{}\left({F}\right){}\left({t}{,}{v}{}\left({t}\right)\right)$ (5)
 A diff representation for this derivative also requires the composition with eval
 > convert((5), diff);
 $\genfrac{}{}{0}{}{\left(\frac{{\partial }}{{\partial }{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{F}{}\left({t}{,}{\mathrm{t1}}\right)\right)}{\phantom{\left\{{\mathrm{t1}}{=}{v}{}\left({t}\right)\right\}}}{|}\genfrac{}{}{0}{}{\phantom{\left(\frac{{\partial }}{{\partial }{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{F}{}\left({t}{,}{\mathrm{t1}}\right)\right)}}{\left\{{\mathrm{t1}}{=}{v}{}\left({t}\right)\right\}}$ (6)
 • In the same manner, D can be used to represent the derivative of $F\left(t,v\left(t\right)\right)$ "with respect to $v\left(t\right)$", a function.
 > D[2](F)(t,v(t));
 ${{\mathrm{D}}}_{{2}}{}\left({F}\right){}\left({t}{,}{v}{}\left({t}\right)\right)$ (7)
 > convert((7), diff);
 $\genfrac{}{}{0}{}{\left(\frac{{\partial }}{{\partial }{\mathrm{t1}}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{F}{}\left({t}{,}{\mathrm{t1}}\right)\right)}{\phantom{\left\{{\mathrm{t1}}{=}{v}{}\left({t}\right)\right\}}}{|}\genfrac{}{}{0}{}{\phantom{\left(\frac{{\partial }}{{\partial }{\mathrm{t1}}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{F}{}\left({t}{,}{\mathrm{t1}}\right)\right)}}{\left\{{\mathrm{t1}}{=}{v}{}\left({t}\right)\right\}}$ (8)
 • The D command can differentiate procedures, typically used to represent mathematical mappings, as well as arbitrary algebraic expressions understood as mappings, in the sense that in Maple algebraic expressions are appliable. This functionality is unique to D; it cannot be emulated using the diff command.
 • The derivative of a procedure or mapping of n variables is another mapping of the same number of variables. For example, D(sin) returns cos. In the general case, $\mathrm{D}\left(f\right)$ represents the mapping
 > D(f) = (q -> eval(diff(f(t),t), t=q));
 ${\mathrm{D}}{}\left({f}\right){=}\left({q}{↦}\genfrac{}{}{0}{}{\left(\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({t}\right)\right)}{\phantom{{t}{=}{q}}}{|}\genfrac{}{}{0}{}{\phantom{\left(\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({t}\right)\right)}}{{t}{=}{q}}\right)$ (9)
 so that when $\mathrm{D}\left(f\right)$ is applied to some variable, say x, or some expression, you have
 > (9)(x);
 ${\mathrm{D}}{}\left({f}\right){}\left({x}\right){=}\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({x}\right)$ (10)
 > (9)(x+1);
 ${\mathrm{D}}{}\left({f}\right){}\left({x}{+}{1}\right){=}\genfrac{}{}{0}{}{\left(\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({t}\right)\right)}{\phantom{\left\{{t}{=}{x}{+}{1}\right\}}}{|}\genfrac{}{}{0}{}{\phantom{\left(\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({t}\right)\right)}}{\left\{{t}{=}{x}{+}{1}\right\}}$ (11)
 • When differentiating an arbitrary mapping represented by an algebraic expression, say F, the expression can contain constants, known function names such as sin, exp (not the applied function exp(x)), unknown function names such as f, g, arrow operators such as $x\to {x}^{2}$, and the arithmetic and functional operators. On such expressions, D recursively applies the rules

$\mathrm{D}\left(f+g\right)=\mathrm{D}\left(f\right)+\mathrm{D}\left(g\right)$

$\mathrm{D}\left(fg\right)=\mathrm{D}\left(f\right)g+\mathrm{D}\left(g\right)f$

$\mathrm{D}\left(f@g\right)=\mathrm{D}\left(f\right)@g\mathrm{D}\left(g\right)$

$\mathrm{D}\left({f}^{c}\right)=c\mathrm{D}\left(f\right){f}^{c-1},c::\mathrm{constant}$

 Note: The composition of functions, say f and g, should be entered using the notation $f@g$, not $f\left(g\right)$.  To specify that a symbol, say g, is a constant and not a function name, use the assuming command, like in D(f*g) assuming g::constant; see also assume.
 • When F is a name to which a procedure of n arguments is assigned, D can compute the mappings representing the partial derivatives of F (restrictions apply). The procedure F can have assignments to local variables, if statements, for loops, while loops, etc.
 • It is an error to specify a call to the derivative of a function with respect to the k-th variable and apply it to fewer than k variables. For instance, ${\mathrm{D}}_{2}\left(f\right)\left(x\right)$ yields an error.

Examples

The D and diff commands can both compute derivatives.

 > $\mathrm{D}\left(\mathrm{ln}\right)\left(x\right)=\mathrm{diff}\left(\mathrm{ln}\left(x\right),x\right)$
 $\frac{{1}}{{x}}{=}\frac{{1}}{{x}}$ (12)

When differentiating arbitrary functions, D and diff return expressions involving themselves. To switch between the two representations use convert/D and convert/diff.

 > $\mathrm{D}\left(y\right)\left(x\right)$
 ${\mathrm{D}}{}\left({y}\right){}\left({x}\right)$ (13)
 > $\mathrm{convert}\left(,\mathrm{diff}\right)$
 $\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({x}\right)$ (14)
 > $\mathrm{convert}\left(,\mathrm{D}\right)$
 ${\mathrm{D}}{}\left({y}\right){}\left({x}\right)$ (15)

When differentiating a symbol representing a mathematical mapping (the simplest algebraic expression), the derivative mapping is computed whenever it is known, as in the case of all the mathematical functions of the Maple language for which a differentiation rule is known; otherwise, the input is echoed unevaluated.

 > $\mathrm{D}\left(\mathrm{sin}\right)$
 ${\mathrm{cos}}$ (16)

nth derivative mapping

 > ${\mathrm{D}}^{\left(n\right)}\left(\mathrm{sin}\right)$
 ${z}{↦}{\mathrm{sin}}{}\left({z}{+}\frac{{n}{\cdot }{\mathrm{\pi }}}{{2}}\right)$ (17)
 > $\mathrm{D}\left[\mathrm{}\left(1,n\right)\right]\left(\mathrm{sin}\right)$
 ${z}{↦}{\mathrm{sin}}{}\left({z}{+}\frac{{n}{\cdot }{\mathrm{\pi }}}{{2}}\right)$ (18)

Derivative mapping for unknown f.

 > $\mathrm{D}\left(f\right)$
 ${\mathrm{D}}{}\left({f}\right)$ (19)
 > $\mathrm{D}\left(\mathrm{D}\left(f\right)\right)$
 ${{\mathrm{D}}}^{\left({2}\right)}{}\left({f}\right)$ (20)
 > ${\mathrm{D}}^{\left(2\right)}\left(f\right)$
 ${{\mathrm{D}}}^{\left({2}\right)}{}\left({f}\right)$ (21)

Partial derivative mapping of order n+m

 > $\mathrm{D}\left[\mathrm{}\left(1,n\right),\mathrm{}\left(2,m\right)\right]\left(g\right)$
 ${{\mathrm{D}}}_{{?}{,}{?}}{}\left({g}\right)$ (22)

Partial derivatives are assumed to commute.

 > $\mathrm{D}\left[i,j\right]\left(g\right)-\mathrm{D}\left[j,i\right]\left(g\right)$
 ${0}$ (23)

Derivative mapping of a derivative mapping.

 > $\mathrm{D}\left[1\right]\left(\mathrm{D}\left[2,1\right]\left(g\right)\right)$
 ${{\mathrm{D}}}_{{1}{,}{1}{,}{2}}{}\left({g}\right)$ (24)

For mathematical functions accepting different number of arguments, the following conventions regarding the input are adopted. This is the derivative mapping of the "arctan of one variable" mapping.

 > $\mathrm{D}\left(\mathrm{arctan}\right)$
 ${z}{↦}\frac{{1}}{{{z}}^{{2}}{+}{1}}$ (25)

To represent the partial derivative with respect to the first argument of the "arctan of two variables" mapping use the indexed notation:

 > $\mathrm{D}\left[1\right]\left(\mathrm{arctan}\right)$
 $\left({y}{,}{x}\right){↦}\frac{{1}}{{x}{\cdot }\left({1}{+}\frac{{{y}}^{{2}}}{{{x}}^{{2}}}\right)}$ (26)

The Zeta function accepts one, two, or three arguments. The partial derivative of the "Zeta mapping" with respect to its second argument has an ambiguous meaning: the argument to D could be the Zeta mapping of two or of three variables. The derivative is thus returned uncomputed:

 > $\mathrm{D}\left[2\right]\left(\mathrm{\zeta }\right)$
 ${{\mathrm{D}}}_{{2}}{}\left({\mathrm{\zeta }}\right)$ (27)

This ambiguity gets resolved when evaluating the previous result on two or three arguments, resulting in mathematically different output.

 > $\left(x,y\right)\ne \left(x,y,z\right)$
 ${\mathrm{\zeta }}{}\left({x}{+}{1}{,}{y}\right){\ne }{\mathrm{\zeta }}{}\left({x}{+}{1}{,}{y}{,}{z}\right)$ (28)

Differentiating more elaborated algebraic expressions representing mathematical mappings:

 > $\mathrm{D}\left(\mathrm{exp}+{\mathrm{cos}}^{2}+\mathrm{\pi }+\mathrm{tan}\right)$
 ${-}{2}{}{\mathrm{sin}}{}{\mathrm{cos}}{+}{{\mathrm{tan}}}^{{2}}{+}{\mathrm{exp}}{+}{1}$ (29)
 > $\mathrm{D}\left(2f\mathrm{sin}\right)$
 ${2}{}{\mathrm{D}}{}\left({f}\right){}{\mathrm{sin}}{+}{2}{}{f}{}{\mathrm{cos}}$ (30)
 > $\mathrm{D}\left(2f\mathrm{sin}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{assuming}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}f::\mathrm{constant}$
 ${2}{}{f}{}{\mathrm{cos}}$ (31)

Differentiating procedures:

 > $f≔\left(x,y\right)↦\mathrm{exp}\left(y\cdot x\right)$
 ${f}{≔}\left({x}{,}{y}\right){↦}{{ⅇ}}^{{y}{\cdot }{x}}$ (32)
 > $\mathrm{D}\left[\right]\left(f\right)$
 ${f}$ (33)
 > $\mathrm{D}\left[1\right]\left(f\right)$
 $\left({x}{,}{y}\right){↦}{y}{\cdot }{{ⅇ}}^{{y}{\cdot }{x}}$ (34)
 > $\mathrm{D}\left[2\right]\left(f\right)$
 $\left({x}{,}{y}\right){↦}{x}{\cdot }{{ⅇ}}^{{y}{\cdot }{x}}$ (35)

An example illustrating the use of D and diff to respectively compute partial and total derivatives. This is a mapping representing the Hamiltonian function H (the energy) of a harmonic oscillator, expressed as a function of the momentum p(t) and the position x(t); t represents the time.

 > $H≔\left(p,x\right)↦\frac{{p}^{2}}{2\cdot m}+\frac{k\cdot {x}^{2}}{2}$
 ${H}{≔}\left({p}{,}{x}\right){↦}\frac{{{p}}^{{2}}}{{2}{\cdot }{m}}{+}\frac{{k}{\cdot }{{x}}^{{2}}}{{2}}$ (36)

The equations of motion are Hamilton's equations; together they form Newton's second law. The first Hamilton's equation involves the partial derivative of H with respect to p(t).

 > $\mathrm{eq}\left[1\right]≔\mathrm{diff}\left(x\left(t\right),t\right)=\mathrm{D}\left[1\right]\left(H\right)\left(p\left(t\right),x\left(t\right)\right)$
 ${{\mathrm{eq}}}_{{1}}{≔}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{x}{}\left({t}\right){=}\frac{{p}{}\left({t}\right)}{{m}}$ (37)

The second equation involves the partial derivative of H with respect to x(t).

 > $\mathrm{eq}\left[2\right]≔\mathrm{diff}\left(p\left(t\right),t\right)=-\mathrm{D}\left[2\right]\left(H\right)\left(p\left(t\right),x\left(t\right)\right)$
 ${{\mathrm{eq}}}_{{2}}{≔}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{p}{}\left({t}\right){=}{-}{k}{}{x}{}\left({t}\right)$ (38)

Newton's second law applied to the harmonic oscillator is obtained combining these equations into one. (See simplify with respect to side relations.)

 > $\mathrm{simplify}\left(\mathrm{eq}\left[2\right],\left\{\mathrm{eq}\left[1\right]\right\},\left\{p\left(t\right)\right\}\right)$
 $\left(\frac{{{ⅆ}}^{{2}}}{{ⅆ}{{t}}^{{2}}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{x}{}\left({t}\right)\right){}{m}{=}{-}{k}{}{x}{}\left({t}\right)$ (39)

The energy of a harmonic oscillator is constant. To see that, take the total derivative of the Hamiltonian (the energy E) with respect to the time t.

 > $\mathrm{diff}\left(E\left(t\right)=H\left(p\left(t\right),x\left(t\right)\right),t\right)$
 $\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{E}{}\left({t}\right){=}\frac{{p}{}\left({t}\right){}\left(\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{p}{}\left({t}\right)\right)}{{m}}{+}{k}{}{x}{}\left({t}\right){}\left(\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{x}{}\left({t}\right)\right)$ (40)

Evaluating this result using Hamilton's equations, you find that E is a constant.

 > $\mathrm{eval}\left(,\left[\mathrm{eq}\left[1\right],\mathrm{eq}\left[2\right]\right]\right)$
 $\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{E}{}\left({t}\right){=}{0}$ (41)