From p.dalgaard at biostat.ku.dk  Sun Oct 30 23:19:43 2005
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: 30 Oct 2005 23:19:43 +0100
Subject: [Rd] [R] unvectorized option for outer()
In-Reply-To: <Pine.LNX.4.63a.0510301300260.28148@homer23.u.washington.edu>
References: <8B08A3A1EA7AAC41BE24C750338754E69FDF14@HERMES.demogr.mpg.de>
	<43629490.9060204@acm.org>
	<971536df0510281643u50929fa6rf097c40948700804@mail.gmail.com>
	<4364A4FC.2030400@durham.ac.uk>
	<Pine.LNX.4.63a.0510301300260.28148@homer23.u.washington.edu>
Message-ID: <x2y84amyv4.fsf@turmalin.kubism.ku.dk>

Thomas Lumley <tlumley at u.washington.edu> writes:

> On Sun, 30 Oct 2005, Jonathan Rougier wrote:
> 
> > I'm not sure about this.  Perhaps I am a dinosaur, but my feeling is
> > that if people are writing functions in R that might be subject to
> > simple operations like outer products, then they ought to be writing
> > vectorised functions!
> 
> I would agree.  How about an oapply() function that does multiway (rather 
> than just two-way) outer products.  Basing the name on "apply" would 
> emphasize the similarity to other flexible, not particularly optimized 
> second-order functions.

In fairness, it should probably be said that not all problems
vectorize naturally. One example is

  ssd <- function(A,alpha) sum((Y - A*exp(-alpha*t))^2)

However, it should be worth noting that with the mapply() function at
hand, it is pretty easy to turn a non-vectorized function into a
vectorized one. 

  SSD <- function(A,alpha) mapply(ssd, A, alpha)

(Anybody want to try their hand on writing a general Vectorize()
function? I.e. one that allowed

   outer(Avec, alphavec, Vectorize(ssd))

to work.)
-- 
   O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



[Rd] [R] unvectorized option for outer()
Peter Dalgaard