constructionModelLassoLME.R
[valse.git] / src / adapters / a.selectiontotale.c
CommitLineData
552b00e2
BA
1#include <R.h>
2#include <Rdefines.h>
8e92c49c 3#include "selectiontotale.h"
552b00e2 4
09ab3c16 5SEXP selectiontotale(
552b00e2
BA
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];
09ab3c16
BA
23 SEXP dimRhoInit = getAttrib(rhoInit_, R_DimSymbol);
24 int m = INTEGER(dimRhoInit)[0];
25 int k = INTEGER(dimRhoInit)[2];
26 int L = length(glambda_);
1d3c1faa
BA
27
28 ////////////
29 // INPUTS //
30 ////////////
31
552b00e2
BA
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 !
09ab3c16 40 double* phiInit = REAL(phiInit_);
552b00e2
BA
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_);
1d3c1faa
BA
47
48 /////////////
49 // OUTPUTS //
50 /////////////
1d3c1faa 51
09ab3c16 52 SEXP A1, A2, rho, pi, dimA, dimRho;
552b00e2
BA
53 PROTECT(dimA = allocVector(INTSXP, 3));
54 int* pDimA = INTEGER(dimA);
55 pDimA[0] = p; pDimA[1] = m+1; pDimA[2] = L;
09ab3c16
BA
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));
552b00e2 62 PROTECT(pi = allocMatrix(REALSXP, k, L));
09ab3c16
BA
63 int *pA1=INTEGER(A1), *pA2=INTEGER(A2);
64 double *pRho=REAL(rho), *pPi=REAL(pi);
1d3c1faa
BA
65
66 /////////////////////////////
67 // Call to selectiontotale //
68 /////////////////////////////
69
09ab3c16 70 selectiontotale_core(
552b00e2
BA
71 phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda,X,Y,seuil,tau,
72 pA1,pA2,pRho,pPi,
1d3c1faa 73 n,p,m,k,L);
1d3c1faa 74
552b00e2
BA
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);
09ab3c16
BA
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);
552b00e2
BA
87
88 UNPROTECT(7);
89 return listParams;
1d3c1faa 90}