[Rd] Creating a vector class

Duncan Murdoch dmurdoch at pair.com
Thu May 29 23:03:53 MEST 2003

I'm trying to create a package for working on orientation data, i.e.
data where the observations are 3D rotations.

There are several different representations of orientations in common
use:  SO(3) matrices, Euler angles, unit quaternions, etc.  One thing
I'd like is to make it convenient to work in any representation, and
have conversions to others done as needed.

I'm trying to do all of this in S4 classes.  Here's the current class
structure I'm using; please let me know if there's something wrong
with this:

The base class is abstract.  All representations will deal with
vectors of orientations, but at this level I don't know how that will
be implemented:

setIs('orientation', 'vector')

First questions:  is this the way to say that orientations behave as
vectors?  Do I need to say that?  Should I say that?

One representation is as an SO(3) matrix (i.e. a 3x3 matrix with
determinant 1).  I have a descendant class that stores these in a 
3 x 3 x n array:

setClass('rotmatrix', representation(x = 'array'))

rotmatrix <- function(a) {
    d <- dim(a)
    if (length(d) < 3) d <- c(d,1)
    a <- array(a, d)
    stopifnot(dim(a)[1] == 3, dim(a)[2] == 3)
    new('rotmatrix', x = a)

Other representations have other storage methods, e.g.

setClass('quaternion', representation(x = 'matrix'))
setIs('quaternion', 'orientation')

Now I want to make sure these work as vectors.  I don't need to define
a '[' method for the abstract base class, do I?  I originally set this
definition for the rotmatrix class:

setMethod('[', 'rotmatrix',
    def = function(x, i) rotmatrix(x at x[,,i,drop=FALSE])

However, this gives warnings:

>In method for function "[": Expanding the signature to 
>include omitted arguments in definition: j = "missing", 
>drop = "missing" 

I notice in the setMethod example the right way to do it:

setMethod('[', 'rotmatrix',
    function(x, i, j, ..., drop) rotmatrix(x at x[,,i,drop=FALSE])

But where are the meanings for j and drop defined?  Even if I don't
declare orientation to be a vector, I get this warning, so where is it
coming from?  Is it good or bad to say that orientation is a vector?
What implications does it have?

Duncan Murdoch

More information about the R-devel mailing list