Inputparametters.R + SelecModel
[valse.git] / ProcLassoMLE / EMGLLF_interface.c
1 #include "ioutils.h"
2 #include "EMGLLF.h"
3 #include <mex.h>
4
5 // nlhs, nrhs: resp. numbers of out and in parameters.
6 // plhs: array of out parameters, each being a mxArray
7 // plhs: array of in parameters (immutable), each being a mxArray
8 //
9 // MATLAB translates a call [A,B] = fun(C,D) into mexFunction(2,{A,B},2,{C,D}).
10 // Then mxArrayS are adapted to be passed to a regular C function,
11 // and the results are translated back to mxArrayS into plhs.
12 void mexFunction(
13 int nlhs,
14 mxArray* plhs[],
15 int nrhs,
16 const mxArray* prhs[])
17 {
18 // Basic sanity checks
19 if (nrhs!=11)
20 mexErrMsgIdAndTxt("select:EMGLLF:nrhs","11 inputs required.");
21 if (nlhs!=5)
22 mexErrMsgIdAndTxt("select:EMGLLF:nlhs","5 outputs required.");
23
24 // Get matrices dimensions
25 const mwSize n = mxGetDimensions(prhs[8])[0];
26 const mwSize p = mxGetDimensions(prhs[0])[0];
27 const mwSize m = mxGetDimensions(prhs[0])[1];
28 const mwSize k = mxGetDimensions(prhs[0])[2];
29
30 ////////////
31 // INPUTS //
32 ////////////
33
34 // phiInit
35 const mwSize* dimPhiInit = mxGetDimensions(prhs[0]);
36 Real* brPhiInit = matlabToBrArray_real(mxGetPr(prhs[0]), dimPhiInit, 3);
37
38 // rhoInit
39 const mwSize* dimRhoInit = mxGetDimensions(prhs[1]);
40 Real* brRhoInit = matlabToBrArray_real(mxGetPr(prhs[1]), dimRhoInit, 3);
41
42 // piInit
43 Real* piInit = mxGetPr(prhs[2]);
44
45 // gamInit
46 const mwSize* dimGamInit = mxGetDimensions(prhs[3]);
47 Real* brGamInit = matlabToBrArray_real(mxGetPr(prhs[3]), dimGamInit, 2);
48
49 // min number of iterations
50 Int mini = ((Int*)mxGetData(prhs[4]))[0];
51
52 // max number of iterations
53 Int maxi = ((Int*)mxGetData(prhs[5]))[0];
54
55 // gamma
56 Real gamma = mxGetScalar(prhs[6]);
57
58 // lambda
59 Real lambda = mxGetScalar(prhs[7]);
60
61 // X
62 const mwSize* dimX = mxGetDimensions(prhs[8]);
63 Real* brX = matlabToBrArray_real(mxGetPr(prhs[8]), dimX, 2);
64
65 // Y
66 const mwSize* dimY = mxGetDimensions(prhs[9]);
67 Real* brY = matlabToBrArray_real(mxGetPr(prhs[9]), dimY, 2);
68
69 // tau
70 Real tau = mxGetScalar(prhs[10]);
71
72 /////////////
73 // OUTPUTS //
74 /////////////
75
76 // phi
77 const mwSize dimPhi[] = {dimPhiInit[0], dimPhiInit[1], dimPhiInit[2]};
78 plhs[0] = mxCreateNumericArray(3,dimPhi,mxDOUBLE_CLASS,mxREAL);
79 Real* phi = mxGetPr(plhs[0]);
80
81 // rho
82 const mwSize dimRho[] = {dimRhoInit[0], dimRhoInit[1], dimRhoInit[2]};
83 plhs[1] = mxCreateNumericArray(3,dimRho,mxDOUBLE_CLASS,mxREAL);
84 Real* rho = mxGetPr(plhs[1]);
85
86 // pi
87 plhs[2] = mxCreateNumericMatrix(k,1,mxDOUBLE_CLASS,mxREAL);
88 Real* pi = mxGetPr(plhs[2]);
89
90 // LLF
91 plhs[3] = mxCreateNumericMatrix(maxi,1,mxDOUBLE_CLASS,mxREAL);
92 Real* LLF = mxGetPr(plhs[3]);
93
94 // S
95 const mwSize dimS[] = {p, m, k};
96 plhs[4] = mxCreateNumericArray(3,dimS,mxDOUBLE_CLASS,mxREAL);
97 Real* S = mxGetPr(plhs[4]);
98
99 ////////////////////
100 // Call to EMGLLF //
101 ////////////////////
102
103 EMGLLF(brPhiInit,brRhoInit,piInit,brGamInit,mini,maxi,gamma,lambda,brX,brY,tau,
104 phi,rho,pi,LLF,S,
105 n,p,m,k);
106
107 free(brPhiInit);
108 free(brRhoInit);
109 free(brGamInit);
110 free(brX);
111 free(brY);
112
113 //post-processing: convert by-rows outputs to MATLAB matrices
114 Real* mlPhi = brToMatlabArray_real(phi, dimPhi, 3);
115 copyArray(mlPhi, phi, dimPhi[0]*dimPhi[1]*dimPhi[2]);
116 free(mlPhi);
117
118 Real* mlRho = brToMatlabArray_real(rho, dimRho, 3);
119 copyArray(mlRho, rho, dimRho[0]*dimRho[1]*dimRho[2]);
120 free(mlRho);
121
122 Real* mlS = brToMatlabArray_real(S, dimS, 3);
123 copyArray(mlS, S, dimS[0]*dimS[1]*dimS[2]);
124 free(mlS);
125 }