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

Zhijin Wu zwu at jhsph.edu
Tue May 10 00:43:35 CEST 2005


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




More information about the R-help mailing list