[R] use "integrate" for functions defined in C, not R

From: Zhijin Wu <zwu_at_jhsph.edu>
Date: Tue 10 May 2005 - 08:43:35 EST


Dear all,
 I am trying to use the C code for "integrate" function ( that calls Rdqagi and Rdqags) so that I can integrate a function defined in C, instead of passing from R.
  Is there a way for doing this?   

My unsuccessful attempt:
  I looked into the files (including integrate.c, Applic.h) and 1. modified the definition of
"integr_fn" by droping the environment "*ex",

        void integr_fn(double *x, int n) and dropped all use of "ex" used in the code

2. defined my checker function f1 and the vectorizing function "Cintfn" in place of "Rintfn"

          double f1(double x){ return(x);}
          static void Cintfn(double *x, int n)
           {
           int i;
           for(i = 0; i < n; i++) 
           x[i] = f1(x[i]);
           return;
           }
 

3. Similar to "call_dqags", I define a C function "my_call_dqags" that has it's own parameters of "lower, upper" and etc define in C, instead of parsing from R. And I call
  Rdqags(Cintfn,

&lower, &upper, &epsabs, &epsrel, &result,
&abserr, &neval, &ier, &limit, &lenw, &last, iwork, work);
instead of
  Rdqags(Rintfn, (void*)&is,

           &lower, &upper, &epsabs, &epsrel, &result,
           &abserr, &neval, &ier, &limit, &lenw, &last, iwork, work);

I am not passing (void*)&is because I no longer have the "environment".

The code compiles fine with R CMD SHLIB. But it returns 5.3e-317 for my checker function f(x)=x, integration interval (1,2).

Thanks for any hint!

Jean



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue May 10 08:48:23 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:40 EST