[Rd] setClass question
Roger Koenker
roger at ysidro.econ.uiuc.edu
Sun Aug 24 13:47:53 MEST 2003
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 at dimension) == 2 )
return("invalid dimension attribute")
else{
nrow <- object at dimension[1]
ncol <- object at dimension[2]
}
if(!(length(object at ra) ==length(object at ja)))
return("ra and ja don't have equal lengths")
if(any(object at ja < 1) || any(object at ja > ncol))
return("ja exceeds dim bounds")
if(any(object at ia < 1))
return("some elements of ia are <= 0")
if(any(diff(object at ia)<0))
return("ia vector not monotone increasing")
if(object at ia[length(object at ia)] != length(object at ra)+1)
return("last element of ia doesn't conform")
if(length(object at ia) != nrow+1)
return("ia has wrong number of elments")
if(length(object at ra) < 1 || length(object at 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 at ra <- ra
.Object at ja <- ja
.Object at ia <- ia
.Object at dimension <- dimension
validObject(.Object)
.Object
})
url: www.econ.uiuc.edu/~roger/my.html Roger Koenker
email rkoenker at uiuc.edu Department of Economics
vox: 217-333-4558 University of Illinois
fax: 217-244-6678 Champaign, IL 61820
More information about the R-devel
mailing list