| 1 | #include "ioutils.h" |
| 2 | #include "EMGrank.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!=8) |
| 20 | mexErrMsgIdAndTxt("select:EMGrank:nrhs","8 inputs required."); |
| 21 | if (nlhs!=2) |
| 22 | mexErrMsgIdAndTxt("select:EMGrank:nlhs","3 outputs required."); |
| 23 | |
| 24 | // Get matrices dimensions |
| 25 | const mwSize n = mxGetDimensions(prhs[4])[0]; |
| 26 | const mwSize p = mxGetDimensions(prhs[4])[1]; |
| 27 | const mwSize m = mxGetDimensions(prhs[1])[0]; |
| 28 | const mwSize k = mxGetDimensions(prhs[1])[2]; |
| 29 | |
| 30 | //////////// |
| 31 | // INPUTS // |
| 32 | //////////// |
| 33 | |
| 34 | // Pi |
| 35 | Real* Pi = mxGetPr(prhs[0]); |
| 36 | |
| 37 | // Rho |
| 38 | const mwSize* dimRho = mxGetDimensions(prhs[1]); |
| 39 | Real* brRho = matlabToBrArray_real(mxGetPr(prhs[1]), dimRho, 3); |
| 40 | |
| 41 | // min number of iterations |
| 42 | Int mini = ((Int*)mxGetData(prhs[2]))[0]; |
| 43 | |
| 44 | // max number of iterations |
| 45 | Int maxi = ((Int*)mxGetData(prhs[3]))[0]; |
| 46 | |
| 47 | // X |
| 48 | const mwSize* dimX = mxGetDimensions(prhs[4]); |
| 49 | Real* brX = matlabToBrArray_real(mxGetPr(prhs[4]), dimX, 2); |
| 50 | |
| 51 | // Y |
| 52 | const mwSize* dimY = mxGetDimensions(prhs[5]); |
| 53 | Real* brY = matlabToBrArray_real(mxGetPr(prhs[5]), dimY, 2); |
| 54 | |
| 55 | // tau |
| 56 | Real tau = mxGetScalar(prhs[6]); |
| 57 | |
| 58 | // rank |
| 59 | Int* rank = (Int*)mxGetData(prhs[7]); |
| 60 | |
| 61 | ///////////// |
| 62 | // OUTPUTS // |
| 63 | ///////////// |
| 64 | |
| 65 | // phi |
| 66 | const mwSize dimPhi[] = {p, m, k}; |
| 67 | plhs[0] = mxCreateNumericArray(3,dimPhi,mxDOUBLE_CLASS,mxREAL); |
| 68 | Real* phi = mxGetPr(plhs[0]); |
| 69 | |
| 70 | // LLF |
| 71 | plhs[1] = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL); |
| 72 | Real* LLF = mxGetPr(plhs[1]); |
| 73 | |
| 74 | ///////////////////// |
| 75 | // Call to EMGrank // |
| 76 | ///////////////////// |
| 77 | |
| 78 | EMGrank(Pi,brRho,mini,maxi,brX,brY,tau,rank, |
| 79 | phi,LLF, |
| 80 | n,p,m,k); |
| 81 | |
| 82 | free(brRho); |
| 83 | free(brX); |
| 84 | free(brY); |
| 85 | |
| 86 | //post-processing: convert by-rows outputs to MATLAB matrices |
| 87 | Real* mlPhi = brToMatlabArray_real(phi, dimPhi, 3); |
| 88 | copyArray(mlPhi, phi, dimPhi[0]*dimPhi[1]*dimPhi[2]); |
| 89 | free(mlPhi); |
| 90 | } |