deriv                  package:base                  R Documentation

_S_y_m_b_o_l_i_c _a_n_d _A_l_g_o_r_i_t_h_m_i_c _D_e_r_i_v_a_t_i_v_e_s _o_f _S_i_m_p_l_e _E_x_p_r_e_s_s_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     Compute derivatives of simple expressions, symbolically.

_U_s_a_g_e:

     D(expr, namevec)
     deriv(expr, namevec, function.arg = NULL, tag = ".expr")

_A_r_g_u_m_e_n_t_s:

    expr: expression which should be differentiated.

 namevec: character vector, giving the variable names with respect to
          which derivatives will be computed.

function.arg: NOT YET IMPLEMENTED.  If specified, a function
          `prototype' (with empty `body') which will be used to return
          a function with the given argument list, instead of an
          expression.

     tag: character; the prefix to be used for the locally created
          variables in result..

_D_e_t_a_i_l_s:

     `D' is modelled after its S namesake for taking simple symbolic
     derivatives.

     `deriv' is a generic function with a default and a `formula'
     method.  It returns a `call' for computing the `expr' and its
     (partial) derivatives, simultaneously.  It uses so-called
     ``algorithmic derivatives''.

     Currently, `deriv.formula' just calls `deriv.default' after
     extracting the expression to the right of `~'.

_V_a_l_u_e:

     `D' returns an expression and therefore can easily be iterated for
     higher derivatives.

     `deriv' returns a `call' object which becomes an `expression' when
     evaluated once.  Evaluation of the latter expression returns the
     function values with a `".gradient"' attribute containing the
     gradient matrix.

_N_o_t_e:

     This help page should be fixed up by one of R&R or someone else
     who fluently speaks the language in `$R_HOME/src/main/deriv.c'.

     Its author, MM, has only got a vague idea and thinks that a help
     page is better than none.

_R_e_f_e_r_e_n_c_e_s:

     Griewank, A.  and  Corliss, G. F. (1991) Automatic Differentiation
     of Algorithms: Theory, Implementation, and Application. SIAM
     proceedings, Philadelphia.

_S_e_e _A_l_s_o:

     `nlm' for numeric minimization which should make use of
     derivatives.

_E_x_a_m_p_l_e_s:

     ## formula argument :
     dx2x <- deriv(~ x^2, "x") ; dx2x
     expression({
              .value <- x^2
              .grad <- array(0, c(length(.value), 1), list(NULL, c("x")))
              .grad[, "x"] <- 2 * x
              attr(.value, "gradient") <- .grad
              .value
     })
     mode(dx2x)
     x <- -1:2
     eval(dx2x)

     ## Something `tougher':
     trig.exp <- expression(sin(cos(x + y^2)))
     ( D.sc <- D(trig.exp, c("x", "y")) )

     ( dxy <- deriv(trig.exp, c("x", "y")) )
     y <- 1
     eval(dxy)
     eval(D.sc)

