[Rd] What does is() mean?
Roger Koenker
roger at ysidro.econ.uiuc.edu
Tue Mar 18 05:49:27 MET 2003
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 at dimension) == 2 )
return("invalid dimension attribute")
else{
nrow <- x at dimension[1]
ncol <- x at dimension[2]
}
if(!(length(x at ra) ==length(x at ja) && length(x at ra) ==length(x at ia)))
return("ra,ja,ia don't have equal lengths")
if(any(x at ja < 1) || any(x at ja > ncol))
return("ja exceeds dim bounds")
if(any(x at ia < 1) || any(x at ia > nrow))
return("ia exceeds dim bounds")
if(length(x at ra) < 1 || length(x at 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 at ra <- ra
.Object at ja <- ja
.Object at ia <- ia
.Object at 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 at 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
More information about the R-devel
mailing list