[R] Dispatching on a dgCMatrix does not work.

Søren Højsgaard sorenh at math.aau.dk
Tue Dec 11 13:46:12 CET 2012


I represent a graph as an adjacency matrix of class "dgCMatrix" (from the Matrix package).

> xx
5 x 5 sparse Matrix of class "dgCMatrix"
  a b c d e
a . 1 1 . .
b 1 . 1 . .
c 1 1 . 1 1
d . . 1 . 1
e . . 1 1 .

To check if the matrix defines and undirected graph, I have made the following functions/methods:

is.UG <- function (object) {
   UseMethod("is.UG")  
}
is.UG.dgCMatrix <- function (object) {
    is.adjMAT(object) && (max(abs(t(object) - object)) == 0)
}

Note: 1) is.adjMAT is homegrown and 2) I know there is an "isSymmetric" method, but I can't get that to work either. These functions are in a package and when I load the package I get:

> is.UG(xx)
Error in t.default(object) : argument is not a matrix

> is.UG.dgCMatrix(xx)
Error in t.default(object) : argument is not a matrix

Interestingly, if I copy and past the definitions above into an R-session things do work:

> is.UG <- function (object) {
+    UseMethod("is.UG")  
+ }
> is.UG.dgCMatrix <- function (object) {
+     is.adjMAT(object) && (max(abs(t(object) - object)) == 0)
+ }
> 
> is.UG(xx)
[1] TRUE
> is.UG.dgCMatrix(xx)
[1] TRUE

Can anyone help here?

Best regards
Søren


xx <- new("dgCMatrix"
    , i = c(1L, 2L, 0L, 2L, 0L, 1L, 3L, 4L, 2L, 4L, 2L, 3L)
    , p = c(0L, 2L, 4L, 8L, 10L, 12L)
    , Dim = c(5L, 5L)
    , Dimnames = list(c("a", "b", "c", "d", "e"), c("a", "b", "c", "d", "e"))
    , x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    , factors = list()



More information about the R-help mailing list