Design Issues
Design Issues
Abstract
This is a (currently very incomplete) write-up of the many smaller and
larger design decisions we have made in organizing functionalities in the
Matrix package.
Classes: There’s a rich hierarchy of matrix classes, which you can
visualize as a set of trees whose inner (and “upper”) nodes are virtual
classes and only the leaves are non-virtual “actual” classes.
Functions and Methods:
- setAs()
- others
> library(Matrix)
> (D4 <- Diagonal(4, 10*(1:4)))
1
> str(D4)
> diag(D4)
[1] 10 20 30 40
We can modify the diagonal in the traditional way (via method definition for
diag<-()):
> diag(D4) <- diag(D4) + 1:4
> D4
Slots:
2
Class: character integer list
Extends:
Class "sparseMatrix", directly
Class "Matrix", by class "sparseMatrix", distance 2
Class "mMatrix", by class "Matrix", distance 3
Class "replValueSp", by class "Matrix", distance 3
2 Matrix Transformations
2.1 Coercions between Matrix classes
You may need to transform Matrix objects into specific shape (triangular,
symmetric), content type (double, logical, . . . ) or storage structure (dense
or sparse). Every useR should use as(x, <superclass>) to this end, where
<superclass> is a virtual Matrix super class, such as "triangularMatrix"
"dMatrix", or "sparseMatrix".
In other words, the user should not coerce directly to a specific desired class
such as "dtCMatrix", even though that may occasionally work as well.
Here is a set of rules to which the Matrix developers and the users should
typically adhere:
3
> (M <- spMatrix(4,4, i=1:4, j=c(3:1,4), x=c(4,1,4,8))) # dgTMatrix
[1,] . . 4 .
[2,] . 1 . .
[3,] 4 . . .
[4,] . . . 8
[1,] . . 4 .
[2,] . 1 . .
[3,] 4 . . .
[4,] . . . 8
Rule 3 : All the following coercions to virtual matrix classes should work:
1. as(m, "dMatrix")
2. as(m, "lMatrix")
3. as(m, "nMatrix")
4. as(m, "denseMatrix")
5. as(m, "sparseMatrix")
6. as(m, "generalMatrix")
1. as(m1, "triangularMatrix")
should work when m1 is a triangular matrix, i.e. the upper or lower
triangle of m1 contains only zeros.
2. as(m2, "symmetricMatrix") should work when m2 is a symmetric
matrix in the sense of isSymmetric(m2) returning TRUE. Note that
this is typically equivalent to something like isTRUE(all.equal(m2,
t(m2))), i.e., the lower and upper triangle of the matrix have to be
equal up to small numeric fuzz.
3 Session Info
> toLatex(sessionInfo())
4
• Locale: LC_CTYPE=en_US.UTF-8, LC_NUMERIC=C, LC_TIME=en_US.UTF-8,
LC_COLLATE=C, LC_MONETARY=en_US.UTF-8, LC_MESSAGES=en_US.UTF-8,
LC_PAPER=en_US.UTF-8, LC_NAME=C, LC_ADDRESS=C, LC_TELEPHONE=C,
LC_MEASUREMENT=en_US.UTF-8, LC_IDENTIFICATION=C
• Running under: Debian GNU/Linux 11 (bullseye)