From r.hankin at noc.soton.ac.uk  Mon Jan 14 09:11:52 2008
From: r.hankin at noc.soton.ac.uk (Robin Hankin)
Date: Mon, 14 Jan 2008 08:11:52 +0000
Subject: [Rd] as.function()
Message-ID: <39993FF2-8FE4-4FB9-A752-8A3ABAB18CEF@noc.soton.ac.uk>

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.





[my real example is very much more complicated than this but
I need this toy one too and I can't see how to modify  
as.function.polynomial()
to do what I want]




--
Robin Hankin
Uncertainty Analyst and Neutral Theorist,
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743



[Rd] as.function()
Robin Hankin