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([EMAIL PROTECTED]) == 2 )
return("invalid dimension attribute")
else{
nrow <- [EMAIL PROTECTED]
ncol <- [EMAIL PROTECTED]
}
if(!(length([EMAIL PROTECTED]) ==length([EMAIL PROTECTED])))
return("ra and ja don't have equal lengths")
if(any([EMAIL PROTECTED] < 1) || any([EMAIL PROTECTED] > ncol))
return("ja exceeds dim bounds")
if(any([EMAIL PROTECTED] < 1))
return("some elements of ia are <= 0")
if(any(diff([EMAIL PROTECTED])<0))
return("ia vector not monotone increasing")
if([EMAIL PROTECTED]([EMAIL PROTECTED])] != length([EMAIL
PROTECTED])+1)
return("last element of ia doesn't conform")
if(length([EMAIL PROTECTED]) != nrow+1)
return("ia has wrong number of elments")
if(length([EMAIL PROTECTED]) < 1 || length([EMAIL PROTECTED]) >
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)) {
[EMAIL PROTECTED] <- ra
[EMAIL PROTECTED] <- ja
[EMAIL PROTECTED] <- ia
[EMAIL PROTECTED] <- dimension
validObject(.Object)
.Object
})
url: www.econ.uiuc.edu/~roger/my.html Roger Koenker
email [EMAIL PROTECTED] Department of Economics
vox: 217-333-4558 University of Illinois
fax: 217-244-6678 Champaign, IL 61820
______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-devel