From p.dalgaard@kubism.ku.dk  Tue Jun  3 23:29:16 1997
From: p.dalgaard@kubism.ku.dk (Peter Dalgaard BSA)
Date: 04 Jun 1997 00:29:16 +0200
Subject: R-alpha: unif_rand() again
In-Reply-To: Luke Tierney's message of Tue, 3 Jun 1997 13:15:43 -0500 (CDT)
References: <9706031815.AA15866@nokomis.stat.umn.edu>
Message-ID: <x2soyzqq0z.fsf@bush.kubism.ku.dk>

Luke Tierney <luke@stat.umn.edu> writes:

> 
> Peter Dalgaard BSA wrote:
> 
> > Here's a model that does (seem to) work:
> > 
> > > dyn.load("runif.so");.C("select_rand",runif); .C("unif_rand",3.3)
> > [[1]]
> ...
> > with
> > 
> > double select_rand(void * f)
> > {
> >         func = f;
> > }
> > 
> 
> I'm not sure I see the point of the separate select_rand -- you can
> just pass runif as an argument to unif_rand.

Now what *did* I mean by that?.... Oh, yes. The original point
involved calling R from a C function. I'm .C-ing it in that bit of
code just for testing it. In practice, it might well get called from
another C function doing a simulation. So, since it appears to be
tricky to do the string-to-functionSEXP conversion in C, it might be
easier to explicitly initialize them from R with the "select_rand"
mechanism. Of course, one might also pass the R function directly to
the simulation routine, and have that one set things up.

Come to think of it, I'm not sure that we *want* the S semantics for
this. Consider

f<-function(a) a^2
Phi<-function(z)
{
	f<-function(z) exp(-z^2/2)/sqrt(2*pi)
	.C("integrate", f, -infty, zero)
}
where integrate() needs to call_S. It must be ensured that it passes
the inner 'f'. 

Hmm. That's not really an argument for having a call_S("f",...) rather
than call_S(f,...). It is no different from the work involved in any
other name lookup (including constructions like "x %myop% y" or
"'hello, world!'(5)"); it's just that it seems a bit silly to search
for a function by name when you already know full well what the
address of the function is. Inefficient, but hardly impossible.

Maybe one could set it up so that call_S did the string thing and
call_R the direct function pointer?

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel 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-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


R-alpha: unif_rand() again
Peter Dalgaard BSA