[Rd] Error when overloading + operator in R S4 class and using Matrix package
Thibaut Lamadon
thibaut.lamadon at gmail.com
Tue Dec 11 20:24:02 CET 2012
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.
More information about the R-devel
mailing list