running embedded R in C -


i have written piece of c code declares square matrix of size 4x4. samples sampling function called rgig in package generalizedhyperbolic in r. inverses matrix using gsl library gnu , spits out result. exercise in calling r c.

#include <stdio.h> #include <stdio.h> #include <math.h> #include <stdlib.h> #include <stddef.h>  // gsl #include <gsl/gsl_machine.h> #include <gsl/gsl_rng.h> #include <gsl/gsl_randist.h> #include <gsl/gsl_cdf.h> #include <gsl/gsl_cblas.h> #include <gsl/gsl_sf_gamma.h> #include <gsl/gsl_vector.h> #include <gsl/gsl_matrix.h> #include <gsl/gsl_blas.h> #include <gsl/gsl_linalg.h>  // r embedding in c #include <rinternals.h> #include <rdefines.h> #include <rembedded.h> #include <r_ext/parse.h>  void gsl_square_matrix_inverse (gsl_matrix *, gsl_matrix *, int); sexp get_rinvgauss(void);  int main(void) {     // define dimension n of matrix     // , signum s (for lu decomposition)     int s, i, j, n = 4;     // define used matrices     gsl_matrix * m = gsl_matrix_alloc (n, n);     gsl_matrix * inverse = gsl_matrix_alloc (n, n);      // r embedding in c     char *localargs[] = {"r", "--no-save","--silent"};     sexp rinvgauss;      // init r embedding     rf_initembeddedr(3, localargs);      printf("\n printing matrix m before set. size %d %d... \n", n, n);     for(i=0; i<n; i++){         printf("\n");         for(j=0; j<n; j++){             printf(" %f ", gsl_matrix_get(m, i, j));         }     }      // set diagonal elements of matrix m inverse gaussian random samples     for(i=0; i<n; i++){         rinvgauss = get_rinvgauss();         gsl_matrix_set(m, i, i, *real(rinvgauss));     }      rf_endembeddedr(0); // end r embedding in c      printf("\n printing matrix m ..... \n");     for(i=0; i<n; i++){         printf("\n");         for(j=0; j<n; j++){             printf(" %f ", gsl_matrix_get(m, i, j));         }     }      // inverse of matrix m     gsl_square_matrix_inverse (m, inverse, n);      printf("\n printing inverse of matrix m ..... \n");     for(i=0; i<n; i++){         printf("\n");         for(j=0; j<n; j++){             printf(" %f", gsl_matrix_get(inverse, i, j));         }     }      return 0; }   sexp get_rinvgauss(void) {     sexp e, s, t, tmp, result;     int erroroccurred, n=1;     double chi=5, psi=4, lambda=0.5;      // create , evaluate 'require(generalizedhyperbolic)'     protect(e = lang2(install("require"), mkstring("generalizedhyperbolic")));     r_tryeval(e, r_globalenv, &erroroccurred);     if (erroroccurred) {         // handle error         printf("\n error loading library generalizedhyperbolic:");     }     unprotect(1);       // create r expressions using paired list     // rgig(n = 1, chi = 5, psi = 4, lambda = 0.5) r api.     protect(t = s = allocvector(langsxp, 5));     // done by: protect(t = s = alloclist(5)); set_typeof(s, langsxp);      tmp = findfun(install("rgig"), r_globalenv);     if(tmp == r_nilvalue) {         printf("no definition function rgig.\n");         unprotect(1);         exit(1);         }     setcar(t, tmp); t = cdr(t);     setcar(t,  scalarinteger(n)); set_tag(t, install("n")); t= cdr(t);     setcar(t,  scalarreal(chi)); set_tag(t, install("chi")); t= cdr(t);     setcar(t,  scalarreal(psi)); set_tag(t, install("psi")); t= cdr(t);     setcar(t,  scalarreal(lambda)); set_tag(t, install("lambda")); t= cdr(t);     protect(result = r_tryeval(vector_elt(s, 0), r_globalenv, null));     unprotect(2);      return(result);  }  void gsl_square_matrix_inverse (gsl_matrix *m, gsl_matrix *inverse, int n){      int s, i, j;     gsl_permutation * perm = gsl_permutation_alloc (n);     // make lu decomposition of matrix m     gsl_linalg_lu_decomp (m, perm, &s);     // invert matrix m     gsl_linalg_lu_invert (m, perm, inverse);  } 

i compiled code using:

r cmd shlib -lgsl -lgslcblas embedr_matinv.c 

with output:

gcc -arch x86_64 -std=gnu99 -i/library/frameworks/r.framework/resources/include -i/library/frameworks/r.framework/resources/include/x86_64 -dndebug  -i/usr/local/include    -fpic  -g -o2  -c embedr_matinv.c -o embedr_matinv.o gcc -arch x86_64 -std=gnu99 -dynamiclib -wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -l/usr/local/lib -o embedr_matinv.so embedr_matinv.o -lgsl -lgslcblas -f/library/frameworks/r.framework/.. -framework r -wl,-framework -wl,corefoundation 

when submit:

r cmd embedr_matinv 

its gives error:

/library/frameworks/r.framework/resources/bin/rcmd: line 62: exec: embedr_matinv: not found 

what doing wrong?

i changed main() test() , made shared object

r cmd shlib -lgsl -lgslcblas embedr_matinv.c -o embedr_matinv  

with output:

gcc -arch x86_64 -std=gnu99 -i/library/frameworks/r.framework/resources/include -i/library/frameworks/r.framework/resources/include/x86_64 -dndebug  -i/usr/local/include    -fpic  -g -o2  -c embedr_matinv.c -o embedr_matinv.o 

if dyn.load("embedr_matinv.so") in r studio, code runs without termination i.e. hangs!

any suggestions on wrong in code?

are using mac osx system?

try this:

sudo cp /library/frameworks/r.framework/resources/library/rserve/libs/x86_64/rserve-bin.so\ /library/frameworks/r.framework/resources/bin/rserve


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -