Skip to content

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

3 messages · Brian Ripley, Zhijin Wu

#
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
#
On Mon, 9 May 2005, Zhijin Wu wrote:

            
1) This is the wrong list: please read the posting guide.

2) You cannot just leave out arguments in C calls, so it seems that you 
need help with C programming rather than R.  If you include the 
appropriate headers this will be checked, so I guess you have not.
#
Dear Dr. Ripley,
  Thank you for the reply. I was wondering maybe someone has tried
similar things using the C code in R base.   
  I included the same header as the original code "integrate.c", except 
#include <R_ext/Applic.h> has been changed to "Myapplic.h" to use my
definition of 
typedef void integr_fn(double *x, int n). 

I did not simply leave out arguments in the C calls, but redefined all
functions involving "environment" with one less parameter. 

Thank you.

regards,
Jean