1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <R.h>
#include <Rinternals.h>
#include <Rmath.h>
#include <math.h>
SEXP fooC2(SEXP aR, SEXP bR)
{
int i, j, n = length(aR);
double *a = REAL(aR), *b = REAL(bR);
SEXP Rval = allocVector(REALSXP, n);
for (i = 0; i < n; i++) {
REAL(Rval)[i] = 0;
for (j = 0; j < n; j++)
REAL(Rval)[i] += pow(a[j] + i + 1, b[j]);
}
return Rval;
}
Enter to Rename, Shift+Enter to Preview
1
dyn.load("chapter7/fooC2.so") # load the compiled C code
Enter to Rename, Shift+Enter to Preview