| 1 | #include "ioutils.h" |
| 2 | #include "constructionModelesLassoRank.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. |
| 14 | void mexFunction(int nlhs, mxArray* plhs[], |
| 15 | int nrhs, const mxArray* prhs[]) |
| 16 | { |
| 17 | // Basic sanity checks |
| 18 | if (nrhs!=10) |
| 19 | mexErrMsgIdAndTxt("select:constructionModelesLassoRank:nrhs","10 inputs required."); |
| 20 | if (nlhs!=2) |
| 21 | mexErrMsgIdAndTxt("select:constructionModelesLassoRank:nlhs","3 outputs required."); |
| 22 | |
| 23 | // Get matrices dimensions, to be given to main routine above |
| 24 | const mwSize n = mxGetDimensions(prhs[4])[0]; |
| 25 | const mwSize p = mxGetDimensions(prhs[4])[1]; |
| 26 | const mwSize m = mxGetDimensions(prhs[1])[0]; |
| 27 | const mwSize k = mxGetDimensions(prhs[1])[2]; |
| 28 | const mwSize L = mxGetDimensions(prhs[7])[1]; |
| 29 | |
| 30 | //////////// |
| 31 | // INPUTS // |
| 32 | //////////// |
| 33 | |
| 34 | // pi |
| 35 | const mwSize* dimPi = mxGetDimensions(prhs[0]); |
| 36 | Real* brPi = matlabToBrArray_real(mxGetPr(prhs[0]), dimPi, 2); |
| 37 | |
| 38 | // rho |
| 39 | const mwSize* dimRho = mxGetDimensions(prhs[1]); |
| 40 | Real* brRho = matlabToBrArray_real(mxGetPr(prhs[1]), dimRho, 4); |
| 41 | |
| 42 | // min number of iterations |
| 43 | Int mini = ((Int*)mxGetData(prhs[2]))[0]; |
| 44 | |
| 45 | // max number of iterations |
| 46 | Int maxi = ((Int*)mxGetData(prhs[3]))[0]; |
| 47 | |
| 48 | // X |
| 49 | const mwSize* dimX = mxGetDimensions(prhs[4]); |
| 50 | Real* brX = matlabToBrArray_real(mxGetPr(prhs[4]), dimX, 2); |
| 51 | |
| 52 | // Y |
| 53 | const mwSize* dimY = mxGetDimensions(prhs[5]); |
| 54 | Real* brY = matlabToBrArray_real(mxGetPr(prhs[5]), dimY, 2); |
| 55 | |
| 56 | // tau |
| 57 | Real tau = mxGetScalar(prhs[6]); |
| 58 | |
| 59 | // A1 |
| 60 | const mwSize* dimA = mxGetDimensions(prhs[7]); |
| 61 | Int* brA1 = matlabToBrArray_int(mxGetData(prhs[7]), dimA, 2); |
| 62 | |
| 63 | //rangmin |
| 64 | Int rangmin = ((Int*)mxGetData(prhs[8]))[0]; |
| 65 | |
| 66 | //rangmax |
| 67 | Int rangmax = ((Int*)mxGetData(prhs[9]))[0]; |
| 68 | |
| 69 | ///////////// |
| 70 | // OUTPUTS // |
| 71 | ///////////// |
| 72 | |
| 73 | // phi |
| 74 | mwSize Size = pow(rangmax-rangmin+1,k); |
| 75 | const mwSize dimPhi[] = {p, m, k, L*Size}; |
| 76 | plhs[0] = mxCreateNumericArray(4,dimPhi,mxDOUBLE_CLASS,mxREAL); |
| 77 | Real* phi = mxGetPr(plhs[0]); |
| 78 | |
| 79 | // lvraisemblance |
| 80 | const mwSize dimLvraisemblance[] = {L*Size, 2}; |
| 81 | plhs[1] = mxCreateNumericMatrix(dimLvraisemblance[0],dimLvraisemblance[1],mxDOUBLE_CLASS,mxREAL); |
| 82 | Real* lvraisemblance = mxGetPr(plhs[1]); |
| 83 | |
| 84 | |
| 85 | ////////////////////////////////////////// |
| 86 | // Call to constructionModelesLassoRank // |
| 87 | ////////////////////////////////////////// |
| 88 | |
| 89 | constructionModelesLassoRank( |
| 90 | brPi,brRho,mini,maxi,brX,brY,tau,brA1,rangmin,rangmax, |
| 91 | phi,lvraisemblance, |
| 92 | n,p,m,k,L); |
| 93 | |
| 94 | free(brPi); |
| 95 | free(brRho); |
| 96 | free(brX); |
| 97 | free(brY); |
| 98 | free(brA1); |
| 99 | |
| 100 | //post-processing: convert by-rows outputs to MATLAB matrices |
| 101 | Real* mlPhi = brToMatlabArray_real(phi, dimPhi, 4); |
| 102 | copyArray(mlPhi, phi, dimPhi[0]*dimPhi[1]*dimPhi[2]*dimPhi[3]); |
| 103 | free(mlPhi); |
| 104 | |
| 105 | Real* mlLvraisemblance = brToMatlabArray_real(lvraisemblance, dimLvraisemblance, 2); |
| 106 | copyArray(mlLvraisemblance, lvraisemblance, dimLvraisemblance[0]*dimLvraisemblance[1]); |
| 107 | free(mlLvraisemblance); |
| 108 | |
| 109 | } |