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