Skip to content

Sparse matrix performance question

3 messages · scott white, Douglas Bates

#
On Mon, Dec 6, 2010 at 1:11 PM, scott white <distributedintel at gmail.com> wrote:
Assignment of submatrices in a sparse matrix can be slow because there
is so much checking that needs to be done.  It is probably easier to
do the calculation directly on the data component of the matrix and
generate a new one.  The tricky bit to remember is that the indices in
the sparse matrix representation are 0-based so you need to add 1 when
using them in R.

I enclose a transcript.
-------------- next part --------------

R version 2.12.0 (2010-10-15)
Copyright (C) 2010 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
Loading required package: lattice

Attaching package: 'Matrix'

The following object(s) are masked from 'package:base':

    det
+                   j=sample(5000, 1000, replace=TRUE),
+                   x=rnorm(1000), dims=c(5000, 5000))
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:1000] 2014 549 1098 3137 130 1523 2198 3921 4323 931 ...
  ..@ p       : int [1:5001] 0 0 0 0 0 0 0 0 0 0 ...
  ..@ Dim     : int [1:2] 5000 5000
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : NULL
  ..@ x       : num [1:1000] -0.4236 -0.5322 0.0675 -0.4105 -2.3708 ...
  ..@ factors : list()
[1]    1 4996
num [1:5000] 0 0.501 0 0.598 -0.957 ...
+                     x=M at x/rs[M at i + 1L], index1=FALSE)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:1000] 2014 549 1098 3137 130 1523 2198 3921 4323 931 ...
  ..@ p       : int [1:5001] 0 0 0 0 0 0 0 0 0 0 ...
  ..@ Dim     : int [1:2] 5000 5000
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : NULL
  ..@ x       : num [1:1000] 1 1 1 -0.655 1 ...
  ..@ factors : list()
0    1 
4082  918
user  system elapsed 
  3.010   0.120   3.612