diag                  package:spam                  R Documentation

_S_p_a_r_s_e _M_a_t_r_i_x _d_i_a_g_o_n_a_l_s

_D_e_s_c_r_i_p_t_i_o_n:

     Extract or replace the diagonal of a matrix, or construct a
     diagonal matrix.

_U_s_a_g_e:

     # diag(x)
     diag(x=1, nrow, ncol)
     diag(x) <- value

     diag.spam(x=1, nrow, ncol)
     diag.spam(x) <- value

_A_r_g_u_m_e_n_t_s:

       x: a 'spam' matrix, a vector or a scalar.

nrow, ncol: Optional dimensions for the result.

   value: either a single value or a vector of length equal to that of
          the current diagonal.

_D_e_t_a_i_l_s:

     Using 'diag(x)' can have unexpected effects if 'x' is a vector
     that could be of length one. Use 'diag(x, nrow = length(x))' for
     consistent behaviour.

_V_a_l_u_e:

     If 'x' is a spam matrix then 'diag(x)' returns the diagonal of
     'x'.

     The assignment form sets the diagonal of the sparse matrix 'x' to
     the given value(s).

     'diag.spam' works as 'diag' for spam matrices: If 'x' is a vector
     (or 1D array) of length two or more, then 'diag.spam(x)' returns a
     diagonal matrix whose diagonal is 'x'.

     If 'x' is a vector of length one then 'diag.spam(x)' returns an
     identity matrix of order the nearest integer to 'x'.  The
     dimension of the returned matrix can be specified by 'nrow' and
     'ncol' (the default is square).

     The assignment form sets the diagonal of the matrix 'x' to the
     given value(s).

_A_u_t_h_o_r(_s):

     Reinhard Furrer

_S_e_e _A_l_s_o:

     'upper.tri', 'lower.tri'.

_E_x_a_m_p_l_e_s:

     diag.spam(2, 4)           # 2*I4
     smat <- diag.spam(1:5)
     diag( smat)
     diag( smat) <- 5:1

     # The last line is equivalent to 
     diag.spam( smat) <- 5:1

     # Note that diag.spam( 1:5) <- 5:1 not work of course.

