Sparse Matrix Class
spam.RdThis group of functions evaluates and coerces changes in class structure.
Usage
spam(x, nrow = 1, ncol = 1, eps = getOption("spam.eps"))
<!-- % force64 = getOption("spam.force64")) -->
as.spam(x, eps = getOption("spam.eps"))
<!-- %, force64 = getOption("spam.force64")) -->
is.spam(x)Arguments
- x
is a matrix (of either dense or sparse form), a list, vector object or a distance object
- nrow
number of rows of matrix
- ncol
number of columns of matrix
Details
The functions spam and as.spam act like matrix
and as.matrix
to coerce an object to a sparse matrix object of class spam.
If x is a list, it should contain either two or three elements.
In case of the former, the list should contain a n by two
matrix of indicies (called ind) and the values.
In case of the latter, the list should contain three vectors
containing the row, column indices (called i and
j) and the values. In both cases partial matching is done.
In case there are several triplets with the same i, j,
the values are added.
eps should be at least as large as .Machine$double.eps.
If getOption("spam.force64") is TRUE, a 64-bit
spam matrix is returned in any case. If FALSE, a 32-bit
matrix is returned when possible.
Note
The zero matrix has the element zero stored in (1,1).
The functions do not test the presence of NA/NaN/Inf. Virtually
all call a Fortran routine with the NAOK=NAOK
argument, which defaults to FALSE resulting in an error.
Hence, the NaN do not always properly propagate through (i.e.
spam is not IEEE-754 compliant).
References
Reinhard Furrer, Stephan R. Sain (2010). "spam: A Sparse Matrix R Package with Emphasis on MCMC Methods for Gaussian Markov Random Fields.", Journal of Statistical Software, 36(10), 1-25, doi:10.18637/jss.v036.i10 .
See also
SPAM for a general overview of the package;
spam_random to create matrices with a random sparsity pattern;
cleanup to purge a sparse matrix;
spam.options for details about the safemode flag;
read.MM and foreign to create spam
matrices from MatrixMarket
files and from certain Matrix or SparseM formats.
Examples
# old message, do not loop, when you create a large sparse matrix
set.seed(13)
nz <- 128
ln <- nz^2
smat <- spam(0,ln,ln)
is <- sample(ln,nz)
js <- sample(ln,nz)
## IGNORE_RDIFF_BEGIN
system.time(for (i in 1:nz) smat[is[i], js[i]] <- i)
#> user system elapsed
#> 0.034 0.005 0.040
system.time(smat[cbind(is,js)] <- 1:nz)
#> user system elapsed
#> 0.002 0.000 0.001
## IGNORE_RDIFF_END
getClass("spam")
#> Class "spam" [package "spam"]
#>
#> Slots:
#>
#> Name: entries colindices rowpointers dimension
#> Class: numeric numeric numeric numeric
options(spam.NAOK=TRUE)
as.spam(c(1, NA))
#> [,1]
#> [1,] 1
#> [2,] NA
#> Class 'spam' (32-bit)