[Rd] Error when overloading + operator in R S4 class and using Matrix package

From: Thibaut Lamadon <thibaut.lamadon_at_gmail.com>
Date: Tue, 11 Dec 2012 19:24:02 +0000


Dear all,

I get a weird effect when trying to overload the + operator and using the Matrix package with sparse matrices. I first define a very simple class that does not use the Matrix package but has a + operator. I then sum two sparse matrices. The first M+M addition delivers the expected result but the second throws an error. Here is a very simple code that generates the error:

require(Matrix)
setClass("TestM",representation(M='numeric')) setMethod("initialize", "TestM", function(.Object,x) {   .Object_at_M = x
  .Object
})

setMethod("+", c("TestM","TestM"), function(e1,e2) {   e1_at_M + e2_at_M
})

M = Matrix(diag(1:10),sparse=T)
M+M # > FINE
M+M # > ERROR M = Matrix(diag(1:10),sparse=F)
M+M # > FINE
M+M # > FINE The second addition throws the following error:

Error in forceSymmetric(callGeneric(as(e1, "dgCMatrix"), as(e2, "dgCMatrix"))) :   error in evaluating the argument 'x' in selecting a method for function 'forceSymmetric': Error in .Arith.Csparse(e1, e2, .Generic, class. = "dgCMatrix") :
  object '.Generic' not found

And the error does not happen if the matrices are not sparse. Is there some interference between the+ I define and the + for sparseMatrix ? Do I not define the + operator correctly?

I posted this on stackoverflow and was advised to ask here if this might be an S4 bug.

Thank you very much,

t.



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed 12 Dec 2012 - 09:17:45 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 12 Dec 2012 - 18:42:50 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive