Tuesday, November 24, 2009

Rmath example

The following is an example of code using stand alone R math library
and the compilation command.

/*
 *  C CODE
 */

#define MATHLIB_STANDALONE
#include <Rmath.h>
#include <stdio.h>
#include <stdlib.h>

int main(){
  int i;
  unsigned int SEED1, SEED2;
  double mu, sigma, PHI_X, *X;

  mu = 0;
  sigma = 1;
  SEED1 = 12345;
  SEED2 = 67890;
  set_seed(SEED1, SEED2);

  X = (double *) malloc(10 * sizeof(double));
  for(i = 0; i < 10; i++){
    X[i] = rnorm(mu, sigma);
    PHI_X = pnorm(X[i], mu, sigma, 1, 0);
    printf("X: %f, PHI(X): %f\n", X[i], PHI_X);
  }
  free(X);
}



/*
 * COMPILATION:  gcc -I/Users/local/lib64/R/include -L/Users/local/lib64 -lRmath -lm tmp2.c
 */

No comments: