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