From roger at ysidro.econ.uiuc.edu  Sun Aug 24 13:47:53 2003
From: roger at ysidro.econ.uiuc.edu (Roger Koenker)
Date: Sun Aug 24 19:41:16 2003
Subject: [Rd] setClass question
Message-ID: <Pine.SOL.4.30.0308241224001.8144-100000@ysidro.econ.uiuc.edu>

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


[Rd] setClass question
Roger Koenker