R package can now be installed (compilation OK)
[valse.git] / src / adapters / a.EMGLLF.c
1 #include <R.h>
2 #include <Rdefines.h>
3 #include "EMGLLF.h"
4
5 SEXP EMGLLF(
6 SEXP phiInit_,
7 SEXP rhoInit_,
8 SEXP piInit_,
9 SEXP gamInit_,
10 SEXP mini_,
11 SEXP maxi_,
12 SEXP gamma_,
13 SEXP lambda_,
14 SEXP X_,
15 SEXP Y_,
16 SEXP tau_
17 ) {
18 // Get matrices dimensions
19 int n = INTEGER(getAttrib(X_, R_DimSymbol))[0];
20 SEXP dim = getAttrib(phiInit_, R_DimSymbol);
21 int p = INTEGER(dim)[0];
22 int m = INTEGER(dim)[1];
23 int k = INTEGER(dim)[2];
24
25 ////////////
26 // INPUTS //
27 ////////////
28
29 // get scalar parameters
30 int mini = INTEGER_VALUE(mini_);
31 int maxi = INTEGER_VALUE(maxi_);
32 double gamma = NUMERIC_VALUE(gamma_);
33 double lambda = NUMERIC_VALUE(lambda_);
34 double tau = NUMERIC_VALUE(tau_);
35
36 // Get pointers from SEXP arrays ; WARNING: by columns !
37 double* phiInit = REAL(phiInit_);
38 double* rhoInit = REAL(rhoInit_);
39 double* piInit = REAL(piInit_);
40 double* gamInit = REAL(gamInit_);
41 double* X = REAL(X_);
42 double* Y = REAL(Y_);
43
44 /////////////
45 // OUTPUTS //
46 /////////////
47
48 SEXP phi, rho, pi, LLF, S, dimPhiS, dimRho;
49 PROTECT(dimPhiS = allocVector(INTSXP, 3));
50 int* pDimPhiS = INTEGER(dimPhiS);
51 pDimPhiS[0] = p; pDimPhiS[1] = m; pDimPhiS[2] = k;
52 PROTECT(dimRho = allocVector(INTSXP, 3));
53 int* pDimRho = INTEGER(dimRho);
54 pDimRho[0] = m; pDimRho[1] = m; pDimRho[2] = k;
55 PROTECT(phi = allocArray(REALSXP, dimPhiS));
56 PROTECT(rho = allocArray(REALSXP, dimRho));
57 PROTECT(pi = allocVector(REALSXP, k));
58 PROTECT(LLF = allocVector(REALSXP, maxi-mini+1));
59 PROTECT(S = allocArray(REALSXP, dimPhiS));
60 double *pPhi=REAL(phi), *pRho=REAL(rho), *pPi=REAL(pi), *pLLF=REAL(LLF), *pS=REAL(S);
61
62 ////////////////////
63 // Call to EMGLLF //
64 ////////////////////
65
66 EMGLLF_core(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,lambda,X,Y,tau,
67 pPhi,pRho,pPi,pLLF,pS,
68 n,p,m,k);
69
70 // Build list from OUT params and return it
71 SEXP listParams, listNames;
72 PROTECT(listParams = allocVector(VECSXP, 5));
73 char* lnames[5] = {"phi", "rho", "pi", "LLF", "S"}; //lists labels
74 PROTECT(listNames = allocVector(STRSXP,5));
75 for (int i=0; i<5; i++)
76 SET_STRING_ELT(listNames,i,mkChar(lnames[i]));
77 setAttrib(listParams, R_NamesSymbol, listNames);
78 SET_VECTOR_ELT(listParams, 0, phi);
79 SET_VECTOR_ELT(listParams, 1, rho);
80 SET_VECTOR_ELT(listParams, 2, pi);
81 SET_VECTOR_ELT(listParams, 3, LLF);
82 SET_VECTOR_ELT(listParams, 4, S);
83
84 UNPROTECT(9);
85 return listParams;
86 }