#define R_NO_REMAP #include #include #include #include /* polynomial interpolation algorithm for function with values fx evaluated at x */ void interp_sexp(double x[], double fx[], int pts, double tab1[], double tab2[], double *f, double *df, int *err) { int i,j,ni=0; double diff1,diff2,tmp1,tmp2,lim1,lim2; *err=0; /* create an initial table of values from those supplied */ tmp1=fabs(x[0]); for(i=0;i .Call requires this*/ Rf_protect(call2=Rf_lang2(fcn,state2)); /*.C -> .Call requires this*/ Rf_protect(result2=Rf_eval(call2, envir)); /*.C -> .Call requires this*/ Rf_protect(x=Rf_coerceVector(result2,REALSXP));/*.C -> .Call requires this*/ for(k=0;k REAL(x)[k]*/ Rf_unprotect(4); return; }else{ /* several trapezoids */ for(i=1,j=1;j .Call requires this*/ Rf_protect(call2=Rf_lang2(fcn,state2)); /*.C -> .Call requires this*/ Rf_protect(result2=Rf_eval(call2, envir)); /*.C -> .Call requires this*/ Rf_protect(x=Rf_coerceVector(result2,REALSXP));/*.C -> .Call requires this*/ for(k=0;k REAL(x)[k]*/ zz[k]+=pnt2[k];} Rf_unprotect(3); for(k=0;k .Call requires this*/ Rf_protect(call2=Rf_lang2(fcn,state2)); /*.C -> .Call requires this*/ Rf_protect(result2=Rf_eval(call2, envir)); /*.C -> .Call requires this*/ Rf_protect(x=Rf_coerceVector(result2,REALSXP));/*.C -> .Call requires this*/ for(k=0;k REAL(x)[k]*/ zz[k]+=pnt1[k];} Rf_unprotect(3); } Rf_unprotect(1); /* calculate total area */ for(k=0;k=*PTS?1:0); /*repeatedly call polynomial interpolation routine*/ for(i=0;i<*LEN;i++){ fx[j+i**MAX]=sum[i]; if(j1>=*PTS){ interp_sexp(&x[j1-*PTS+i**MAX],&fx[j1-*PTS+i**MAX],*PTS,tab1,tab2,&sumlen[i],&errsum,ERR); if(*ERR)Rf_error("*ERR is now 2 -- Line 177 -- Division by zero in romberg integration C code"); /* check convergence */ if(fabs(errsum)>*EPS*fabs(sumlen[i]))finish=0;} /* decrease step size */ x[j1+i**MAX]=x[j+i**MAX]/9.0; fx[j1+i**MAX]=fx[j+i**MAX];} if(finish){ for(i=0;i<*LEN;i++) REAL(ans)[i] = sumlen[i]; Rf_unprotect(1); return ans;}} *ERR=3; if(*ERR)Rf_error("*ERR is now 3 -- Line 191 -- No convergence in romberg integration C code"); for(i=0;i<*LEN;i++) REAL(ans)[i] = sumlen[i]; Rf_unprotect(1); return ans;} // SEXP call, result; // /* try a state2 def and put in c(1,2,3,9) and see if you can get it*/ // SEXP abcd; // abcd = PROTECT(Rf_allocVector(REALSXP, 4)); // REAL(abcd)[0] = 1; // REAL(abcd)[1] = 2; // REAL(abcd)[2] = 2.5; // REAL(abcd)[3] = 3.11; // /*...*/ // // PROTECT(call = Rf_lang2(fcn,abcd)); // PROTECT(result = Rf_eval(call,envir)); // SEXP foo; // PROTECT(foo = Rf_coerceVector(result, REALSXP)); // int len2 = LENGTH(foo); // for (int i = 0; i < len2; i++) // if (! R_finite(REAL(foo)[i])) // Rf_error("function returned vector with non-finite element"); // UNPROTECT(4); // return foo; //}