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