[Rd] as.function()
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Jan 14 17:48:52 CET 2008
On Jan 14, 2008 6:50 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
> Robin Hankin wrote:
> > Hi
> >
> > [this after some considerable thought as to R-help vs R-devel]
> >
> >
> >
> > I want to write a (S3) method for as.function();
> > toy example follows.
> >
> > Given a matrix "a", I need to evaluate trace(ax) as a function of
> > (matrix) "x".
> >
> > Here's a trace function:
> >
> > tr <- function (a) {
> > i <- seq_len(nrow(a))
> > return(sum(a[cbind(i, i)]))
> > }
> >
> >
> > How do I accomplish the following:
> >
> >
> > a <- crossprod(matrix(rnorm(12),ncol=3))
> > class(a) <- "foo"
> >
> > f <- as.function(a) # need help to write as.function.foo()
> > x <- diag(3)
> >
> > f(x) #should give tr(ax)
> >
> > a <- 4
> > f(x) # should still give tr(ax) even though "a" has been
> > reassigned.
> >
> >
> Brian's answer was what you want. A less general version is this:
>
> > as.function.foo <- function(x, ...) {
> + function(b) tr(x %*% b)
> + }
>
This can also be done using the proto package. p has two
components b and f. q inherits f from p but has its own b.
library(proto)
p <- proto(b = 1:4, f = function(., x) sum(diag(x %*% .$b)))
q <- p$proto(b = 5:8)
p$f(1:4)
q$f(1:4)
More information about the R-devel
mailing list