Back
Close

Computing with Data

elgeish
33.3K views

Interfacing with C Code

For example, consider the task of computing some function given two vectors a and b of size n. The C code to compute the result appears below (fooC.c); note the presence of the pre-processor directive include <R.h>. You may execute code from fooC.R after running the command R CMD SHLIB fooC.c; the runnable example below runs that command for you:

#include <R.h>
#include <math.h>
void fooC(double* a, double* b, int* n, double* result) {
int i, j;
for (i = 0; i < (*n); i++) {
result[i] = 0;
for (j = 0; j < (*n); j++)
result[i] += pow(a[j] + i + 1, b[j]);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants