-#include "ioutils.h"
+#include <R.h>
+#include <Rdefines.h>
#include "selectiontotale.h"
-#include "EMGLLF.h"
-#include <mex.h>
-// nlhs, nrhs: resp. numbers of out and in parameters.
-// plhs: array of out parameters, each being a mxArray
-// plhs: array of in parameters (immutable), each being a mxArray
-//
-// MATLAB translates a call [A,B] = fun(C,D) into mexFunction(2,{A,B},2,{C,D}).
-// Then mxArrayS are adapted to be passed to a regular C function,
-// and the results are translated back to mxArrayS into plhs.
-void mexFunction(int nlhs, mxArray* plhs[],
- int nrhs, const mxArray* prhs[])
-{
- // Basic sanity checks
- if (nrhs!=12)
- mexErrMsgIdAndTxt("select:selectiontotale:nrhs","12 inputs required.");
- if (nlhs!=4)
- mexErrMsgIdAndTxt("select:selectiontotale:nlhs","4 outputs required.");
-
- // Get matrices dimensions, to be given to main routine above
- const mwSize n = mxGetDimensions(prhs[8])[0];
- const mwSize p = mxGetDimensions(prhs[8])[1];
- const mwSize m = mxGetDimensions(prhs[1])[0];
- const mwSize k = mxGetDimensions(prhs[1])[2];
- const mwSize L = mxGetNumberOfElements(prhs[7]);
+SEXP selectiontotale(
+ SEXP phiInit_,
+ SEXP rhoInit_,
+ SEXP piInit_,
+ SEXP gamInit_,
+ SEXP mini_,
+ SEXP maxi_,
+ SEXP gamma_,
+ SEXP glambda_,
+ SEXP X_,
+ SEXP Y_,
+ SEXP seuil_,
+ SEXP tau_
+) {
+ // Get matrices dimensions
+ SEXP dimX = getAttrib(X_, R_DimSymbol);
+ int n = INTEGER(dimX)[0];
+ int p = INTEGER(dimX)[1];
+ SEXP dimRhoInit = getAttrib(rhoInit_, R_DimSymbol);
+ int m = INTEGER(dimRhoInit)[0];
+ int k = INTEGER(dimRhoInit)[2];
+ int L = length(glambda_);
////////////
// INPUTS //
////////////
- // phiInit
- const mwSize* dimPhiInit = mxGetDimensions(prhs[0]);
- Real* brPhiInit = matlabToBrArray_real(mxGetPr(prhs[0]), dimPhiInit, 3);
-
- // rhoInit
- const mwSize* dimRhoInit = mxGetDimensions(prhs[1]);
- Real* brRhoInit = matlabToBrArray_real(mxGetPr(prhs[1]), dimRhoInit, 3);
-
- // piInit
- Real* piInit = mxGetPr(prhs[2]);
-
- // gamInit
- const mwSize* dimGamInit = mxGetDimensions(prhs[3]);
- Real* brGamInit = matlabToBrArray_real(mxGetPr(prhs[3]), dimGamInit, 2);
-
- // min number of iterations
- Int mini = ((Int*)mxGetData(prhs[4]))[0];
-
- // max number of iterations
- Int maxi = ((Int*)mxGetData(prhs[5]))[0];
-
- // gamma
- Real gamma = mxGetScalar(prhs[6]);
-
- // glambda
- Real* glambda = mxGetPr(prhs[7]);
-
- // X
- const mwSize* dimX = mxGetDimensions(prhs[8]);
- Real* brX = matlabToBrArray_real(mxGetPr(prhs[8]), dimX, 2);
-
- // Y
- const mwSize* dimY = mxGetDimensions(prhs[9]);
- Real* brY = matlabToBrArray_real(mxGetPr(prhs[9]), dimY, 2);
-
- //seuil
- Real seuil = mxGetScalar(prhs[10]);
-
- // tau
- Real tau = mxGetScalar(prhs[11]);
+ // get scalar parameters
+ int mini = INTEGER_VALUE(mini_);
+ int maxi = INTEGER_VALUE(maxi_);
+ double gamma = NUMERIC_VALUE(gamma_);
+ double seuil = NUMERIC_VALUE(seuil_);
+ double tau = NUMERIC_VALUE(tau_);
+
+ // Get pointers from SEXP arrays ; WARNING: by columns !
+ double* phiInit = REAL(phiInit_);
+ double* rhoInit = REAL(rhoInit_);
+ double* piInit = REAL(piInit_);
+ double* gamInit = REAL(gamInit_);
+ double* glambda = REAL(glambda_);
+ double* X = REAL(X_);
+ double* Y = REAL(Y_);
/////////////
// OUTPUTS //
/////////////
-
- // A1
- mwSize dimA[] = {p,m+1,L};
- plhs[0] = mxCreateNumericArray(3,dimA,mxGetClassID(prhs[4]),mxREAL);
- Int* A1 = (Int*)mxGetData(plhs[0]);
-
- // A2
- plhs[1] = mxCreateNumericArray(3,dimA,mxGetClassID(prhs[4]),mxREAL);
- Int* A2 = (Int*)mxGetData(plhs[1]);
-
- // rho
- const mwSize dimRho[] = {dimRhoInit[0], dimRhoInit[1], dimRhoInit[2], L};
- plhs[2] = mxCreateNumericArray(4,dimRho,mxDOUBLE_CLASS,mxREAL);
- Real* Rho = mxGetPr(plhs[2]);
- // pi
- const mwSize dimPi[] = {k, L};
- plhs[3] = mxCreateNumericMatrix(dimPi[0],dimPi[1],mxDOUBLE_CLASS,mxREAL);
- double* Pi = mxGetPr(plhs[3]);
+ SEXP A1, A2, rho, pi, dimA, dimRho;
+ PROTECT(dimA = allocVector(INTSXP, 3));
+ int* pDimA = INTEGER(dimA);
+ pDimA[0] = p; pDimA[1] = m+1; pDimA[2] = L;
+ PROTECT(A1 = allocArray(INTSXP, dimA));
+ PROTECT(A2 = allocArray(INTSXP, dimA));
+ PROTECT(dimRho = allocVector(INTSXP, 4));
+ int* pDimRho = INTEGER(dimRho);
+ pDimRho[0] = m; pDimRho[1] = m; pDimRho[2] = k; pDimRho[3] = L;
+ PROTECT(rho = allocArray(REALSXP, dimRho));
+ PROTECT(pi = allocMatrix(REALSXP, k, L));
+ int *pA1=INTEGER(A1), *pA2=INTEGER(A2);
+ double *pRho=REAL(rho), *pPi=REAL(pi);
/////////////////////////////
// Call to selectiontotale //
/////////////////////////////
- selectiontotale(brPhiInit,brRhoInit,piInit,brGamInit,mini,maxi,gamma,glambda,brX,brY,seuil,tau,
- A1,A2,Rho,Pi,
+ selectiontotale_core(
+ phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda,X,Y,seuil,tau,
+ pA1,pA2,pRho,pPi,
n,p,m,k,L);
-
- free(brPhiInit);
- free(brRhoInit);
- free(brGamInit);
- free(brX);
- free(brY);
-
- //post-processing: convert by-rows outputs to MATLAB matrices
- Int* mlA1 = brToMatlabArray_int(A1,dimA,3);
- copyArray(mlA1,A1,dimA[0]*dimA[1]*dimA[2]);
- free(mlA1);
-
- Int* mlA2 = brToMatlabArray_int(A2,dimA,3);
- copyArray(mlA2,A2,dimA[0]*dimA[1]*dimA[2]);
- free(mlA2);
-
- Real* mlRho = brToMatlabArray_real(Rho, dimRho, 4);
- copyArray(mlRho, Rho, dimRho[0]*dimRho[1]*dimRho[2]*dimRho[3]);
- free(mlRho);
- Real* mlPi = brToMatlabArray_real(Pi, dimPi, 2);
- copyArray(mlPi, Pi, dimPi[0]*dimPi[1]);
- free(mlPi);
+ // Build list from OUT params and return it
+ SEXP listParams, listNames;
+ PROTECT(listParams = allocVector(VECSXP, 4));
+ char* lnames[4] = { "A1", "A2", "rho", "pi" }; //lists labels
+ PROTECT(listNames = allocVector(STRSXP, 4));
+ for (int i=0; i<4; i++)
+ SET_STRING_ELT(listNames,i,mkChar(lnames[i]));
+ setAttrib(listParams, R_NamesSymbol, listNames);
+ SET_VECTOR_ELT(listParams, 0, A1);
+ SET_VECTOR_ELT(listParams, 1, A2);
+ SET_VECTOR_ELT(listParams, 2, rho);
+ SET_VECTOR_ELT(listParams, 3, pi);
+
+ UNPROTECT(7);
+ return listParams;
}