From roger at ysidro.econ.uiuc.edu  Tue Mar 18 05:49:27 2003
From: roger at ysidro.econ.uiuc.edu (Roger Koenker)
Date: Tue Mar 18 12:44:30 2003
Subject: [Rd] What does is() mean?
In-Reply-To: <3E75FF75.DF72AFE5@research.bell-labs.com>
Message-ID: <Pine.SOL.4.30.0303171155500.17723-100000@ysidro.econ.uiuc.edu>

Thanks very much to John and Robert for their very helpful replies. In the spirit
of John's last comment:

> The whole area of valid objects is one that all of us interested folks
> should discuss.
>
> It would be nice to have some more "real" examples.

I've written an example of an initialize method for a new sparse matrix class matrix.coo.
The object simply contains the nonzero elements, their row and column indices and the
dimension of the matrix.  For most purposes we don't use this format, but it is especially
convenient for some subsetting tasks, and it is a bit simpler than the matrix.csr class.

I hope by offering this up for a critique it might help to clarify some remaining questions.

setClass("matrix.coo",representation(ra="numeric",
        ja="integer",ia="integer", dimension="integer"),
        validity = function(x) {
                if(!length(x@dimension) == 2 )
                        return("invalid dimension attribute")
                else{
                        nrow <- x@dimension[1]
                        ncol <- x@dimension[2]
                        }
                if(!(length(x@ra) ==length(x@ja) && length(x@ra) ==length(x@ia)))
                        return("ra,ja,ia don't have equal lengths")
                if(any(x@ja < 1) || any(x@ja > ncol))
                        return("ja exceeds dim bounds")
                if(any(x@ia < 1) || any(x@ia > nrow))
                        return("ia exceeds dim bounds")
                if(length(x@ra) < 1 || length(x@ra) > nrow*ncol)
                        return("ra has too few, or too many elements")
                TRUE})
setMethod("initialize", "matrix.coo",
        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
       })

The use of validObject here is very convenient since some checking is handled
automatically, like checking for the integer form of the ja,ia, and dim
components.  I have no idea what the overhead cost is of doing this checking.

In SparseM most of the initialize calls will come from code in the package
that (presumably) knows what it is doing.  The motivation for my sudden
interest in validity checking was to control the impulse to construct sparse
objects "on the fly" that didn't conform.  Need I mention, that I've managed
to do this myself...  Efficiency, as usual, is probably not a paramount concern at this
stage,  but I'm little concerned that the efficiency cost is being imposed on
many automatic situations where I'm fairly sure that valid objects are being
made, and it may still not catch the few foolish attempts to make an object "on the
fly."

url:	www.econ.uiuc.edu	Roger Koenker		Dept. of Economics UCL,
email	rkoenker@uiuc.edu	Department of Economics Drayton House,
vox: 	217-333-4558		University of Illinois	30 Gorden St,
fax:   	217-244-6678		Champaign, IL 61820	London,WC1H 0AX, UK
							vox:	020-7679-5838


[Rd] What does is() mean?
Roger Koenker