From tpapp at Princeton.EDU  Tue Dec 19 18:25:16 2006
From: tpapp at Princeton.EDU (Tamas K Papp)
Date: Tue, 19 Dec 2006 18:25:16 +0100
Subject: [Rd] preserving sparse matrices (Matrix)
Message-ID: <20061219172516.GA1299@pu100877.student.princeton.edu>

Hi,

I have sparse (tridiagonal) matrices, and I use the Matrix package for
handling them.  For certain computations, I need to either set the
first row to zero, or double the last row.  I find that neither
operation preserves sparsity, eg

> m <- Diagonal(5)
> m
5 x 5 diagonal matrix of class "ddiMatrix"
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    .    .    .    .
[2,]    .    1    .    .    .
[3,]    .    .    1    .    .
[4,]    .    .    .    1    .
[5,]    .    .    .    .    1
> m[1,] <- 0
> m
5 x 5 Matrix of class "dgeMatrix"
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    1    0    0    0
[3,]    0    0    1    0    0
[4,]    0    0    0    1    0
[5,]    0    0    0    0    1
> m <- Diagonal(5)
> rbind(m,1:5)
6 x 5 Matrix of class "dgeMatrix"
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    0    0    0    0
[2,]    0    1    0    0    0
[3,]    0    0    1    0    0
[4,]    0    0    0    1    0
[5,]    0    0    0    0    1
[6,]    1    2    3    4    5

Currently, I am calling Matrix to make these sparse again, but my
matrices are large (100x100).  Is there a way to perform the
operations I want without losing sparsity?

Thanks,

Tamas



[Rd] preserving sparse matrices (Matrix)
Tamas K Papp