I would like to add a class to the SparseM package. I have a class
"matrix.csr"
that describes a matrix in compressed sparse row format, now I would like a
class
matrix.diag.csr that describes such objects when they happen to be diagonal.
The idea is that matrix.diag.csr objects should behave (later in life) exactly
like
matrix.csr objects, the distinction is only needed in order to clarify the
situation when they are created. So I have:
setClass("matrix.diag.csr","matrix.csr")
My understanding (shakey, as it is) is that if I now try to construct a
matrix.diag.csr
object the checking of it should be just like the checking for a matrix.csr
object, but
>
new("matrix.csr",ra=1:3,ja=1:3,ia=1:4,dimension=as.integer(c(3,3)))
An object of class "matrix.csr"
Slot "ra":
[1] 1 2 3
Slot "ja":
[1] 1 2 3
Slot "ia":
[1] 1 2 3 4
Slot "dimension":
[1] 3 3
>
new("matrix.diag.csr",ra=1:3,ja=1:3,ia=1:4,dimension=as.integer(c(3,3)))
Error in validObject(.Object) : Invalid "matrix.csr" object: invalid
dimension attribute
the traceback() says:
16: stop(paste("Invalid \"", Class, "\" object: ",
errors, sep = ""))
15: validObject(.Object)
14: .local(.Object, ...)
13: initialize(value, ...)
12: initialize(value, ...)
11: new("matrix.csr")
10: asMethod(object)
9: as(object, superClass)
8: validityMethod(as(object, superClass))
7: identical(x, TRUE)
6: anyStrings(validityMethod(as(object, superClass)))
5: validObject(.Object)
4: .local(.Object, ...)
3: initialize(value, ...)
2: initialize(value, ...)
1: new("matrix.diag.csr", ra = 1:3, ja = 1:3, ia = 1:4, dimension =
as.integer(c(3,
3)))
which might be revelatory to some of you, but leaves me in the dark. Any
suggestions
would be greatly appreciated. In case it is relevant
setClass("matrix.csr",representation(ra="numeric",
ja="integer",ia="integer",
dimension="integer"),
validity = function(object) {
if(!length(object@dimension) == 2 )
return("invalid dimension attribute")
else{
nrow <- object@dimension[1]
ncol <- object@dimension[2]
}
if(!(length(object@ra) ==length(object@ja)))
return("ra and ja don't have equal
lengths")
if(any(object@ja < 1) || any(object@ja > ncol))
return("ja exceeds dim bounds")
if(any(object@ia < 1))
return("some elements of ia are <= 0")
if(any(diff(object@ia)<0))
return("ia vector not monotone increasing")
if(object@ia[length(object@ia)] != length(object@ra)+1)
return("last element of ia doesn't
conform")
if(length(object@ia) != nrow+1)
return("ia has wrong number of elments")
if(length(object@ra) < 1 || length(object@ra) > nrow*ncol)
return("ra has too few, or too many elements")
TRUE})
setMethod("initialize", "matrix.csr",
function(.Object, ra = numeric(0), ja = integer(0),
ia = integer(0),dimension = integer(0)) {
.Object@ra <- ra
.Object@ja <- ja
.Object@ia <- ia
.Object@dimension <- dimension
validObject(.Object)
.Object
})
url: www.econ.uiuc.edu/~roger/my.html Roger Koenker
email rkoenker@uiuc.edu Department of Economics
vox: 217-333-4558 University of Illinois
fax: 217-244-6678 Champaign, IL 61820
I have another S4 methods query -- this time about undocumented objects:
I'm now getting the following warning from R CMD check SparseM:
* checking for missing documentation entries ... WARNING
Undocumented code objects:
%x% as.matrix diag diag<- diff t
Undocumented S4 methods:
\S4method{coerce}{ANY,array} \S4method{coerce}{ANY,call}
\S4method{coerce}{ANY,character} \S4method{coerce}{ANY,complex}
\S4method{coerce}{ANY,environment} \S4method{coerce}{ANY,expression}
\S4method{coerce}{ANY,function} \S4method{coerce}{ANY,integer}
\S4method{coerce}{ANY,list} \S4method{coerce}{ANY,logical}
\S4method{coerce}{ANY,matrix} \S4method{coerce}{ANY,name}
\S4method{coerce}{ANY,numeric} \S4method{coerce}{ANY,single}
\S4method{coerce}{ANY,ts} \S4method{coerce}{ANY,vector}
\S4method{coerce}{ANY,NULL}
\S4method{coerce}{vector,matrix.diag.csr}
\S4method{diff}{matrix.csr} \S4method{initialize}{traceable}
\S4method{initialize}{signature} \S4method{initialize}{environment}
A couple of these are directly related to the package, but the rest
seem to be items that I wouldn't have thought that I should be responsible
for documenting. I'm wondering whether the coerce items are related to
my earlier query. Even supposing that they were due to some blunder
of mine, I'm still puzzled by the initialize items which were present
even before I began to mess with my new setClass strategy. Again,
many thanks for any suggestions.
url: www.econ.uiuc.edu/~roger/my.html Roger Koenker
email rkoenker@uiuc.edu Department of Economics
vox: 217-333-4558 University of Illinois
fax: 217-244-6678 Champaign, IL 61820
Roger Koenker wrote:> > I would like to add a class to the SparseM package. I have a class "matrix.csr" > that describes a matrix in compressed sparse row format, now I would like a class > matrix.diag.csr that describes such objects when they happen to be diagonal. > The idea is that matrix.diag.csr objects should behave (later in life) exactly like > matrix.csr objects, the distinction is only needed in order to clarify the > situation when they are created. So I have: > > setClass("matrix.diag.csr","matrix.csr") > > My understanding (shakey, as it is) is that if I now try to construct a matrix.diag.csr > object the checking of it should be just like the checking for a matrix.csr object, butWell, hopefully. But there seems a slight problem in the validity method. You require a dimension slot of length 2, but the default object generated from matrix.csr fails that: R> library(SparseM) [1] "SparseM library loaded" R> new("matrix.csr") Error in validObject(.Object) : Invalid "matrix.csr" object: invalid dimension attribute Enter a frame number, or 0 to exit 1:new("matrix.csr") 2:initialize(value, ...) 3:initialize(value, ...) 4:.local(.Object, ...) 5:validObject(.Object) Selection: 5 Browse[1]> object@dimension numeric(0) (Your extension of the class uncovered the problem by generating the default object, as the traceback below showed.) John> > > new("matrix.csr",ra=1:3,ja=1:3,ia=1:4,dimension=as.integer(c(3,3))) > An object of class "matrix.csr" > Slot "ra": > [1] 1 2 3 > > Slot "ja": > [1] 1 2 3 > > Slot "ia": > [1] 1 2 3 4 > > Slot "dimension": > [1] 3 3 > > > new("matrix.diag.csr",ra=1:3,ja=1:3,ia=1:4,dimension=as.integer(c(3,3))) > Error in validObject(.Object) : Invalid "matrix.csr" object: invalid dimension attribute > > the traceback() says: > > 16: stop(paste("Invalid \"", Class, "\" object: ", errors, sep = "")) > 15: validObject(.Object) > 14: .local(.Object, ...) > 13: initialize(value, ...) > 12: initialize(value, ...) > 11: new("matrix.csr") > 10: asMethod(object) > 9: as(object, superClass) > 8: validityMethod(as(object, superClass)) > 7: identical(x, TRUE) > 6: anyStrings(validityMethod(as(object, superClass))) > 5: validObject(.Object) > 4: .local(.Object, ...) > 3: initialize(value, ...) > 2: initialize(value, ...) > 1: new("matrix.diag.csr", ra = 1:3, ja = 1:3, ia = 1:4, dimension = as.integer(c(3, > 3))) > > which might be revelatory to some of you, but leaves me in the dark. Any suggestions > would be greatly appreciated. In case it is relevant > > setClass("matrix.csr",representation(ra="numeric", > ja="integer",ia="integer", dimension="integer"), > validity = function(object) { > if(!length(object@dimension) == 2 ) > return("invalid dimension attribute") > else{ > nrow <- object@dimension[1] > ncol <- object@dimension[2] > } > if(!(length(object@ra) ==length(object@ja))) > return("ra and ja don't have equal lengths") > if(any(object@ja < 1) || any(object@ja > ncol)) > return("ja exceeds dim bounds") > if(any(object@ia < 1)) > return("some elements of ia are <= 0") > if(any(diff(object@ia)<0)) > return("ia vector not monotone increasing") > if(object@ia[length(object@ia)] != length(object@ra)+1) > return("last element of ia doesn't conform") > if(length(object@ia) != nrow+1) > return("ia has wrong number of elments") > if(length(object@ra) < 1 || length(object@ra) > nrow*ncol) > return("ra has too few, or too many elements") > TRUE}) > setMethod("initialize", "matrix.csr", > function(.Object, ra = numeric(0), ja = integer(0), > ia = integer(0),dimension = integer(0)) { > .Object@ra <- ra > .Object@ja <- ja > .Object@ia <- ia > .Object@dimension <- dimension > validObject(.Object) > .Object > }) > > url: www.econ.uiuc.edu/~roger/my.html Roger Koenker > email rkoenker@uiuc.edu Department of Economics > vox: 217-333-4558 University of Illinois > fax: 217-244-6678 Champaign, IL 61820 > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-devel-- John M. Chambers jmc@bell-labs.com Bell Labs, Lucent Technologies office: (908)582-2681 700 Mountain Avenue, Room 2C-282 fax: (908)582-3340 Murray Hill, NJ 07974 web: http://www.cs.bell-labs.com/~jmc