[Rd] setClass question

John Chambers jmc at research.bell-labs.com
Tue Aug 26 10:14:02 MEST 2003


Roger Koenker wrote:
> 
> 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

Well, hopefully.  But there seems a slight problem in the validity
method.

You require a dimension slot of length 2, but the default object
generated from matrix.csr fails that:


R> library(SparseM)
[1] "SparseM library loaded"
R> new("matrix.csr")
Error in validObject(.Object) : Invalid "matrix.csr" object: invalid
dimension attribute

Enter a frame number, or 0 to exit   
1:new("matrix.csr") 
2:initialize(value, ...) 
3:initialize(value, ...) 
4:.local(.Object, ...) 
5:validObject(.Object) 
Selection: 5
Browse[1]> object at dimension
numeric(0)

(Your extension of the class uncovered the problem by generating the
default object, as the traceback below showed.)

John

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

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc



More information about the R-devel mailing list