Introduction
Introduction
Feb. 2005∗
Douglas Bates
R Core Development Group
[email protected]
May 17, 2023
Abstract
Linear algebra is at the core of many areas of statistical computing and
from its inception the S language has supported numerical linear algebra
via a matrix data type and several functions and operators, such as %*%,
qr, chol, and solve. However, these data types and functions do not
provide direct access to all of the facilities for efficient manipulation of
dense matrices, as provided by the Lapack subroutines, and they do not
provide for manipulation of sparse matrices.
The Matrix package provides a set of S4 classes for dense and sparse
matrices that extend the basic matrix data type. Methods for a wide va-
riety of functions and operators applied to objects from these classes pro-
vide efficient access to BLAS (Basic Linear Algebra Subroutines), Lapack
(dense matrix), TAUCS (sparse matrix) and UMFPACK (sparse matrix)
routines. One notable characteristic of the package is that whenever a ma-
trix is factored, the factorization is stored as part of the original matrix
so that further operations on the matrix can reuse this factorization.
1 Introduction
Linear algebra is at the core of many statistical computing techniques and, from
its inception, the S language has supported numerical linear algebra via a matrix
data type and several functions and operators, such as %*%, qr, chol, and solve.
Initially the numerical linear algebra functions in R called underlying Fortran
routines from the Linpack (Dongarra et al., 1979) and Eispack Smith et al.
(1976) libraries but over the years most of these functions have been switched
to use routines from the Lapack Anderson et al. (1999) library. Furthermore, R
can be configured to use accelerated BLAS (Basic Linear Algebra Subroutines),
∗ There’s an unfinished “2nd Introduction to the Matrix package” which contains partly
newer information, but is not at all self-contained. Eventually that will replace this one.
1
such as those from the Atlas Whaley et al. (2001) project or Goto’s BLAS Goto
and van de Geijn (2002).
Lapack provides routines for operating on several special forms of matrices,
such as triangular matrices and symmetric matrices. Furthermore,matrix de-
compositions like the QR decompositions produce multiple output components
that should be regarded as parts of a single object. There is some support in R
for operations on special forms of matrices (e.g. the backsolve, forwardsolve
and chol2inv functions) and for special structures (e.g. a QR structure is im-
plicitly defined as a list by the qr, qr.qy, qr.qty, and related functions) but it
is not as fully developed as it could be.
Also there is no direct support for sparse matrices in R although Koenker
and Ng (2003) have developed a contributed package for sparse matrices based
on SparseKit.
The Matrix package provides S4 classes and methods for dense and sparse
matrices. The methods for dense matrices use Lapack and BLAS. The sparse
matrix methods use TAUCS (Toledo, 2003), UMFPACK (Davis, 2003), and
Metis (Karapis, 2003).
2
Whenever a factorization or a decomposition is calculated it is preserved as a
(list) element in the factors slot of the original object. In this way a sequence of
operations, such as determining the condition number of a matrix then solving
a linear system based on the matrix, do not require multiple factorizations of
the same matrix nor do they require the user to store the intermediate results.
3
References
E. Anderson, Z. Bai, C. Bischof, S. Blackford, J. Demmel, J. Dongarra, J. Du
Croz, A. Greenbaum, S. Hammarling, A. McKenney, and D. Sorensen. LA-
PACK Users’ Guide. SIAM, Philadelphia, PA, 3rd edition, 1999.
Tim Davis. Umfpack: Unified multifrontal package.
http://www.cise.ufl.edu/research/sparse/umfpack, 2003.
Jack Dongarra, Cleve Moler, Bunch, and G.W. Stewart. Linpack Users’ Guide.
SIAM, 1979.
Kazushige Goto and Robert van de Geijn. On reducing tlb misses in matrix
multiplication. Technical Report TR02-55, Department of Computer Sciences,
U. of Texas at Austin, 2002.
George Karapis. Metis: Family of multilevel partioning algorithms.
http://www-users.cs.umn.edu/ karypis/metis/, 2003.
Roger Koenker and Pin Ng. SparseM: A sparse matrix package for R. J. of
Statistical Software, 8(6), 2003.
B. T. Smith, J. M. Boyle, J. J. Dongarra, B. S. Garbow, Y. Ikebe, V. C. Klema,
and C. B. Moler. Matrix Eigensystem Routines. EISPACK Guide, volume 6
of Lecture Notes in Computer Science. Springer-Verlag, New York, 1976.
Sivan Toledo. Taucs: A library of sparse linear solvers.
http://www.tau.ac.il/ stoledo/taucs/, 2003.
R. Clint Whaley, Antoine Petitet, and Jack J. Dongarra. Auto-
mated empirical optimization of software and the ATLAS project.
Parallel Computing, 27(1–2):3–35, 2001. Also available as Univer-
sity of Tennessee LAPACK Working Note #147, UT-CS-00-448, 2000
(www.netlib.org/lapack/lawns/lawn147.ps).