ripley at stats.ox.ac.uk
Wed Jun 5 16:24:22 CEST 2002
On Wed, 5 Jun 2002, Roger Koenker wrote:
> I've been writing some matrix-type methods for a new class of sparse matrices
> and for most methods this has been straightforward. However, there are
> examples, like %*% and chol, that (apparently) R doesn't automatically
> recognize as generic. What to do in these cases? At this point, I've
> been writing new generic methods with slightly perturbed names %m% and
> cholesky for example in the expectation that I'll eventually rename them
> when I discover the source of my confusion. Can someone enlighten me?
chol() is not generic, as you can see from the definition. It is easy for
a package to define a generic and to capture from base the original as the
default method. E.g.
chol <- function(x, ...) UseMethod("chol")
chol.default <- get("chol", pos=length(search()))
(There are other ways to specify base, e.g. envir=NULL, but I am not sure
what will still work come namespaces and 1.6.0.)
%*% is a primitive, and so harder to tell if it is generic. It isn't
> "%*%.foo" <- function(e1, e2) stop("using foo")
> a <- structure(1, class="foo")
> a %*% a
so you need
"%*%.default" <- .Primitive("%*%")
"%*%" <- function(e1, e2) UseMethod("%*%")
Now, other functions in base in R-devel (1.6.0 to be) are sealed and so
will use the original definitions. That may or may not be what you want.
You can always ask R-core to make functions generic and we usually agree.
All of this is in terms of S3 methods, not the formal methods of package
`methods'. Another way is to use formal methods and assign a method, when
most of this is automatic: see the Green Book.
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help