bandSparse              package:Matrix              R Documentation

_C_o_n_s_t_r_u_c_t _S_p_a_r_s_e _B_a_n_d_e_d _M_a_t_r_i_x _f_r_o_m (_S_u_p-/_S_u_p_e_r-) _D_i_a_g_o_n_a_l_s

_D_e_s_c_r_i_p_t_i_o_n:

     Construct a sparse banded matrix by specifying its non-zero sup-
     and super-diagonals.

_U_s_a_g_e:

     bandSparse(n, m = n, k, diagonals, symmetric = FALSE)

_A_r_g_u_m_e_n_t_s:

     n,m: the matrix dimension (n,m) = (nrow, ncol).

       k: integer vector of "diagonal numbers", with identical meaning
          as in 'band(*, k)'.

diagonals: optional list of sub-/super- diagonals; if missing, the
          result will be a patter*n* matrix, i.e., inheriting from
          class 'nMatrix'.

          'diagonals' can also be n' x d matrix, where 'd <- length(k)'
          and n' >= min(n,m).  In that case, the sub-/super- diagonals
          are taken from the columns of 'diagonals', where only the
          first several rows will be used (typically) for
          off-diagonals. 

symmetric: logical; if true the result will be symmetric (inheriting
          from class 'symmetricMatrix') and only the upper or lower
          triangle must be specified (via 'k' and 'diagonals').

_V_a_l_u_e:

     a sparse matrix (of 'class' 'CsparseMatrix') of dimension n x m
     with diagonal "bands" as specified.

_S_e_e _A_l_s_o:

     'band', for _extraction_ of matrix bands; 'bdiag', 'diag',
     'sparseMatrix', 'Matrix'.

_E_x_a_m_p_l_e_s:

     diags <- list(1:30, 10*(1:20), 100*(1:20))
     s1 <- bandSparse(13, k = -c(0:2, 6), diag = c(diags, diags[2]), symm=TRUE)
     s1
     s2 <- bandSparse(13, k =  c(0:2, 6), diag = c(diags, diags[2]), symm=TRUE)
     stopifnot(identical(s1, t(s2)), is(s1,"dsCMatrix"))

     ## a pattern Matrix of *full* (sub-)diagonals:
     bk <- c(0:4, 7,9)
     (s3 <- bandSparse(30, k = bk, symm = TRUE))

     ## If you want a pattern matrix, but with "sparse"-diagonals,
     ## you currently need to go via logical sparse:
     lLis <- lapply(list(rpois(20, 2), rpois(20,1), rpois(20,3))[c(1:3,2:3,3:2)],
                    as.logical)
     (s4 <- bandSparse(20, k = bk, symm = TRUE, diag = lLis))
     (s4. <- as(drop0(s4), "nsparseMatrix"))

     n <- 1e4
     bk <- c(0:5, 7,11)
     bMat <- matrix(1:8, n, 8, byrow=TRUE)
     bLis <- as.data.frame(bMat)
     B  <- bandSparse(n, k = bk, diag = bLis)
     Bs <- bandSparse(n, k = bk, diag = bLis, symmetric=TRUE)
     B [1:15, 1:30]
     Bs[1:15, 1:30]
     ## can use a list *or* a matrix for specifying the diagonals:
     stopifnot(identical(B,  bandSparse(n, k = bk, diag = bMat)),
               identical(Bs, bandSparse(n, k = bk, diag = bMat, symmetric=TRUE)))

