

deriv {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, symboli-
     cally.

_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, simultane-
     ously.  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'.

     It's 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_:

     A. Griewank, G. F. Corliss (1991).  Automatic Differen-
     tiation 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)

