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([EMAIL PROTECTED]) == 2 )
return("invalid dimension attribute")
else{
nrow <- [EMAIL PROTECTED]
ncol <- [EMAIL PROTECTED]
}
if(!(length([EMAIL PROTECTED]) ==length([EMAIL PROTECTED]) &&
length([EMAIL PROTECTED]) ==length([EMAIL PROTECTED])))
return("ra,ja,ia don't have equal lengths")
if(any([EMAIL PROTECTED] < 1) || any([EMAIL PROTECTED] > ncol))
return("ja exceeds dim bounds")
if(any([EMAIL PROTECTED] < 1) || any([EMAIL PROTECTED] > nrow))
return("ia exceeds dim bounds")
if(length([EMAIL PROTECTED]) < 1 || length([EMAIL PROTECTED]) >
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)) {
[EMAIL PROTECTED] <- ra
[EMAIL PROTECTED] <- ja
[EMAIL PROTECTED] <- ia
[EMAIL PROTECTED] <- 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 [EMAIL PROTECTED] 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
______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-devel