Overloading + operator in R S4 classes and Matrix package

偶尔善良 提交于 2019-12-13 15:32:27

问题


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@M = x
  .Object
})
setMethod("+", c("TestM","TestM"), function(e1,e2) {
  e1@M + e2@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?

Thank you!


回答1:


Try setting the Ops class to be overloaded:

> setMethod(Ops, c("TestM","TestM"), function(e1,e2) {
+   e1@M + e2@M
+ })
[1] "Ops"
attr(,"package")
[1] "base"
> 
> M = Matrix(diag(1:10),sparse=T)
> M+M  # > FINE
10 x 10 sparse Matrix of class "dsCMatrix"

 [1,] 2 . . .  .  .  .  .  .  .
 [2,] . 4 . .  .  .  .  .  .  .
 [3,] . . 6 .  .  .  .  .  .  .
 [4,] . . . 8  .  .  .  .  .  .
 [5,] . . . . 10  .  .  .  .  .
 [6,] . . . .  . 12  .  .  .  .
 [7,] . . . .  .  . 14  .  .  .
 [8,] . . . .  .  .  . 16  .  .
 [9,] . . . .  .  .  .  . 18  .
[10,] . . . .  .  .  .  .  . 20
> M+M  # (NOT error)... was  ERROR
10 x 10 sparse Matrix of class "dsCMatrix"

 [1,] 2 . . .  .  .  .  .  .  .
 [2,] . 4 . .  .  .  .  .  .  .
 [3,] . . 6 .  .  .  .  .  .  .
 [4,] . . . 8  .  .  .  .  .  .
 [5,] . . . . 10  .  .  .  .  .
 [6,] . . . .  . 12  .  .  .  .
 [7,] . . . .  .  . 14  .  .  .
 [8,] . . . .  .  .  . 16  .  .
 [9,] . . . .  .  .  .  . 18  .
[10,] . . . .  .  .  .  .  . 20



回答2:


Almost 3,5 years later, I stumbled upon the same error, which is also topic of a more recent question: setMethod and package Matrix. I have sent a bug report to R-devel but found out afterwards, that the error is only reproducible if overloading is done outside of a package. In other words, if you define the + method for objects of class testM within a package and load the overloaded +-function by loading the package, it will solve the initial problem without having to overload the whole group in a generic manner (which is not always possible).



来源:https://stackoverflow.com/questions/13812373/overloading-operator-in-r-s4-classes-and-matrix-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!