Derivatives and partial derivatives

### 6.19.4 Derivatives and partial derivatives

The diff command computes derivatives and partial derivatives of expressions.
derive is a synonym for diff.

To compute first order derivatives:

- diff takes one mandatory argument and one optional argument:
  - expr, an expression or a list of expressions.
  - Optionally, x, a variable (resp. a list of variable names, see several variable functions in 6.21). If the only variable is x, this second argument can be omitted.
- diff(expr ⟨ ,x ⟩) returns the derivative (resp. a vector of derivatives) of the expression expr (or list of expressions) with respect to the variable x (resp. with respect to each variable in the list x).

Examples.

- Compute:

  ∂ (x y2 z3+x y z)
  ∂ z

  Input:
  diff(x*y^2*z^3+x*y*z,z)
  Output:

  3 x y2 z2+x y

- Compute the 3 first order partial derivatives of x*y2*z3+x*y*z.
  Input:
  diff(x*y^2*z^3+x*y,[x,y,z])
  Output:

  ⎡ ⎣ | y2 z3+y,2 x y z3+x,3 x y2 z2 | ⎤ ⎦

- Compute:

  ∂3 (x.y2.z3+x.y.z)
  ∂ y∂2 z

  Input:
  diff(x*y ^2*z^3+x*y*z,y,z$2)
  Output:

  12 x y z

To compute higher order derivatives:

- diff takes more than two arguments:
  - expr, an expression.
  - x1,x2…, the names of the derivation variables. Note that for repeated variables, you can use the $ operator (see Section 6.39.2) followed by the number of derivations with respect to the variable; for example, instead of writing x,x,x you could write x$3.
- diff(expr,x1,x2,…) returns the partial derivative of expr with respect to the variables x1,x2,….

Examples.

- Compute:

  ∂2 (x y2 z3+x y z)
  ∂ x∂ z

  Input:
  diff(x*y^2*z^3+x*y*z,x,z)
  Output:

  3 y2 z2+y

- Compute:

  ∂3 (x y2 z3+x y z)
  ∂ x∂2 z

  Input:
  diff(x*y^2*z^3+x*y*z,x,z,z)
  or:
  diff(x*y^2*z^3+x*y*z,x,z$2)
  Output:

  6 y2 z

- Compute the third derivative of:

  1
  x2+2

  Input:
  normal(diff((1)/(x^2+2),x,x,x))
  or:
  normal(diff((1)/(x^2+2),x$3))
  Output:

  −24 x3+48 x
  x8+8 x6+24 x4+32 x2+16

Remark.

- Note the difference between diff(f,x,y) and diff(f,[x,y]):
  diff(f,x,y) returns ∂2(f)/∂ x∂ y and diff(f,[x,y]) returns [∂(f)/∂ x, ∂ (f)/∂ y]
- Never define a derivative function with f1(x):=diff(f(x),x). Indeed, x would mean two different things Xcas is unable to deal with: on the left hand side, x is the variable name to define the f1 function, and on the right hand side, x is the differentiation variable. The right way to define a derivative is either with function_diff or:
  f1:=unapply(diff(f(x),x),x)