From cb51adb811037428129ac947a5f7999c8c4539ae Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 28 Nov 2016 19:29:18 +0100
Subject: [PATCH] renaming

---
 src/adapters/EMGLLF_interface.c               | 125 -----------
 src/adapters/a.EMGLLF.c                       | 197 ++++++++++++++++++
 .../{EMGrank_interface.c => a.EMGrank.c}      |   0
 ...face.c => a.constructionModelesLassoMLE.c} |   0
 ...ace.c => a.constructionModelesLassoRank.c} |   0
 ...totale_interface.c => a.selectiontotale.c} |   0
 src/sources/utils/{ioutils.c => io.c}         |   0
 src/sources/utils/{ioutils.h => io.h}         |   0
 8 files changed, 197 insertions(+), 125 deletions(-)
 delete mode 100644 src/adapters/EMGLLF_interface.c
 create mode 100644 src/adapters/a.EMGLLF.c
 rename src/adapters/{EMGrank_interface.c => a.EMGrank.c} (100%)
 rename src/adapters/{constructionModelesLassoMLE_interface.c => a.constructionModelesLassoMLE.c} (100%)
 rename src/adapters/{constructionModelesLassoRank_interface.c => a.constructionModelesLassoRank.c} (100%)
 rename src/adapters/{selectiontotale_interface.c => a.selectiontotale.c} (100%)
 rename src/sources/utils/{ioutils.c => io.c} (100%)
 rename src/sources/utils/{ioutils.h => io.h} (100%)

diff --git a/src/adapters/EMGLLF_interface.c b/src/adapters/EMGLLF_interface.c
deleted file mode 100644
index 470e845..0000000
--- a/src/adapters/EMGLLF_interface.c
+++ /dev/null
@@ -1,125 +0,0 @@
-#include "ioutils.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!=11) 
-		mexErrMsgIdAndTxt("select:EMGLLF:nrhs","11 inputs required.");
-	if (nlhs!=5) 
-		mexErrMsgIdAndTxt("select:EMGLLF:nlhs","5 outputs required.");
-	
-	// Get matrices dimensions
-	const mwSize n = mxGetDimensions(prhs[8])[0];
-	const mwSize p = mxGetDimensions(prhs[0])[0];
-	const mwSize m = mxGetDimensions(prhs[0])[1];
-	const mwSize k = mxGetDimensions(prhs[0])[2];
-
-	////////////
-	// 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]);
-
-	// lambda
-	Real lambda = mxGetScalar(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);
-	
-	// tau
-	Real tau = mxGetScalar(prhs[10]);
-
-	/////////////
-	// OUTPUTS //
-	/////////////
-
-	// phi
-	const mwSize dimPhi[] = {dimPhiInit[0], dimPhiInit[1], dimPhiInit[2]};
-	plhs[0] = mxCreateNumericArray(3,dimPhi,mxDOUBLE_CLASS,mxREAL);
-	Real* phi = mxGetPr(plhs[0]);
-
-	// rho
-	const mwSize dimRho[] = {dimRhoInit[0], dimRhoInit[1], dimRhoInit[2]};
-	plhs[1] = mxCreateNumericArray(3,dimRho,mxDOUBLE_CLASS,mxREAL);
-	Real* rho = mxGetPr(plhs[1]);
-
-	// pi
-	plhs[2] = mxCreateNumericMatrix(k,1,mxDOUBLE_CLASS,mxREAL);
-	Real* pi = mxGetPr(plhs[2]);
-
-	// LLF
-	plhs[3] = mxCreateNumericMatrix(maxi,1,mxDOUBLE_CLASS,mxREAL);
-	Real* LLF = mxGetPr(plhs[3]);
-
-	// S
-	const mwSize dimS[] = {p, m, k};
-    plhs[4] = mxCreateNumericArray(3,dimS,mxDOUBLE_CLASS,mxREAL);
-	Real* S = mxGetPr(plhs[4]);
-
-	////////////////////
-	// Call to EMGLLF //
-	////////////////////
-
-	EMGLLF(brPhiInit,brRhoInit,piInit,brGamInit,mini,maxi,gamma,lambda,brX,brY,tau,
-		phi,rho,pi,LLF,S,
-		n,p,m,k);
-	
-	free(brPhiInit);
-	free(brRhoInit);
-	free(brGamInit);
-	free(brX);
-	free(brY);
-	
-	//post-processing: convert by-rows outputs to MATLAB matrices
-	Real* mlPhi = brToMatlabArray_real(phi, dimPhi, 3);
-	copyArray(mlPhi, phi, dimPhi[0]*dimPhi[1]*dimPhi[2]);
-	free(mlPhi);
-	
-	Real* mlRho = brToMatlabArray_real(rho, dimRho, 3);
-	copyArray(mlRho, rho, dimRho[0]*dimRho[1]*dimRho[2]);
-	free(mlRho);
-    
-    Real* mlS = brToMatlabArray_real(S, dimS, 3);
-	copyArray(mlS, S, dimS[0]*dimS[1]*dimS[2]);
-	free(mlS);
-}
diff --git a/src/adapters/a.EMGLLF.c b/src/adapters/a.EMGLLF.c
new file mode 100644
index 0000000..241a09f
--- /dev/null
+++ b/src/adapters/a.EMGLLF.c
@@ -0,0 +1,197 @@
+#include <R.h>
+#include <Rdefines.h>
+#include "sources/EMGLLF.h"
+#include "sources/utils/io.h"
+
+SEXP EMGLLF(
+	SEXP M_, 
+	SEXP NIix_, 
+	SEXP alpha_, 
+	SEXP h_, 
+	SEXP epsilon_, 
+	SEXP maxiter_, 
+	SEXP symmNeighbs_, 
+	SEXP trace_
+) {
+	// get parameters
+	double alpha = NUMERIC_VALUE(alpha_);
+	double h = NUMERIC_VALUE(h_);
+	double epsilon = NUMERIC_VALUE(epsilon_);
+	int maxiter = INTEGER_VALUE(maxiter_);
+	int symmNeighbs = LOGICAL_VALUE(symmNeighbs_);
+	int trace = LOGICAL_VALUE(trace_);
+
+	// extract infos from M and get associate pointer
+	SEXP dim = getAttrib(M_, R_DimSymbol);
+	int nrow = INTEGER(dim)[0];
+	int ncol = INTEGER(dim)[1];
+	// M is always given by columns: easier to process in rows
+	double* pM = transpose(REAL(M_), nrow, ncol);
+
+	// extract NIix list vectors in a jagged array
+	int* lengthNIix = (int*)malloc(nrow*sizeof(int));
+	int** NIix = (int**)malloc(nrow*sizeof(int*));
+	for (int i=0; i<nrow; i++)
+	{
+		lengthNIix[i] = LENGTH(VECTOR_ELT(NIix_,i));
+		SEXP tmp;
+		PROTECT(tmp = AS_INTEGER(VECTOR_ELT(NIix_,i)));
+		NIix[i] = (int*)malloc(lengthNIix[i]*sizeof(int));
+		for (int j=0; j<lengthNIix[i]; j++)
+			NIix[i][j] = INTEGER(tmp)[j];
+		UNPROTECT(1);
+		// WARNING: R indices start at 1,
+		// so we must lower every index right now to avoid future bug
+		for (int j=0; j<lengthNIix[i]; j++)
+			NIix[i][j]--;
+	}
+
+	// Main call to core algorithm
+	Parameters params = getVarsWithConvexOptim_core(
+		pM, lengthNIix, NIix, nrow, ncol, alpha, h, epsilon, maxiter, symmNeighbs, trace);
+
+	// free neighborhoods parameters arrays
+	free(lengthNIix);
+	for (int i=0; i<nrow; i++)
+		free(NIix[i]);
+	free(NIix);
+
+	// copy matrix F into pF for output to R (1D matrices)
+	SEXP f;
+	PROTECT(f = allocMatrix(REALSXP, nrow, ncol));
+	double* pF = REAL(f);
+	for (int i=0; i<nrow; i++)
+	{
+		for (int j=0; j<ncol; j++)
+			pF[i+nrow*j] = params.f[i][j];
+	}
+	// copy theta into pTheta for output to R
+	SEXP theta;
+	PROTECT(theta = allocVector(REALSXP, nrow));
+	double* pTheta = REAL(theta);
+	for (int i=0; i<nrow; i++)
+		pTheta[i] = params.theta[i];
+
+	// free params.f and params.theta
+	free(params.theta);
+	for (int i=0; i<nrow; i++)
+		free(params.f[i]);
+	free(params.f);
+
+	// build return list with f and theta
+	SEXP listParams, listNames;
+	PROTECT(listParams = allocVector(VECSXP, 2));
+	char* lnames[2] = {"f", "theta"}; //lists labels
+	PROTECT(listNames = allocVector(STRSXP,2));
+	for (int i=0; i<2; i++)
+		SET_STRING_ELT(listNames,i,mkChar(lnames[i]));
+	setAttrib(listParams, R_NamesSymbol, listNames);
+	SET_VECTOR_ELT(listParams, 0, f);
+	SET_VECTOR_ELT(listParams, 1, theta);
+
+	UNPROTECT(4);
+	return listParams;
+
+
+
+
+
+
+
+
+	// Get matrices dimensions
+	const mwSize n = mxGetDimensions(prhs[8])[0];
+	const mwSize p = mxGetDimensions(prhs[0])[0];
+	const mwSize m = mxGetDimensions(prhs[0])[1];
+	const mwSize k = mxGetDimensions(prhs[0])[2];
+
+	////////////
+	// 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]);
+
+	// lambda
+	Real lambda = mxGetScalar(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);
+	
+	// tau
+	Real tau = mxGetScalar(prhs[10]);
+
+	/////////////
+	// OUTPUTS //
+	/////////////
+
+	// phi
+	const mwSize dimPhi[] = {dimPhiInit[0], dimPhiInit[1], dimPhiInit[2]};
+	plhs[0] = mxCreateNumericArray(3,dimPhi,mxDOUBLE_CLASS,mxREAL);
+	Real* phi = mxGetPr(plhs[0]);
+
+	// rho
+	const mwSize dimRho[] = {dimRhoInit[0], dimRhoInit[1], dimRhoInit[2]};
+	plhs[1] = mxCreateNumericArray(3,dimRho,mxDOUBLE_CLASS,mxREAL);
+	Real* rho = mxGetPr(plhs[1]);
+
+	// pi
+	plhs[2] = mxCreateNumericMatrix(k,1,mxDOUBLE_CLASS,mxREAL);
+	Real* pi = mxGetPr(plhs[2]);
+
+	// LLF
+	plhs[3] = mxCreateNumericMatrix(maxi,1,mxDOUBLE_CLASS,mxREAL);
+	Real* LLF = mxGetPr(plhs[3]);
+
+	// S
+	const mwSize dimS[] = {p, m, k};
+    plhs[4] = mxCreateNumericArray(3,dimS,mxDOUBLE_CLASS,mxREAL);
+	Real* S = mxGetPr(plhs[4]);
+
+	////////////////////
+	// Call to EMGLLF //
+	////////////////////
+
+	EMGLLF(brPhiInit,brRhoInit,piInit,brGamInit,mini,maxi,gamma,lambda,brX,brY,tau,
+		phi,rho,pi,LLF,S,
+		n,p,m,k);
+	
+	free(brPhiInit);
+	free(brRhoInit);
+	free(brGamInit);
+	free(brX);
+	free(brY);
+
+
+
+
+
+
+
+}
diff --git a/src/adapters/EMGrank_interface.c b/src/adapters/a.EMGrank.c
similarity index 100%
rename from src/adapters/EMGrank_interface.c
rename to src/adapters/a.EMGrank.c
diff --git a/src/adapters/constructionModelesLassoMLE_interface.c b/src/adapters/a.constructionModelesLassoMLE.c
similarity index 100%
rename from src/adapters/constructionModelesLassoMLE_interface.c
rename to src/adapters/a.constructionModelesLassoMLE.c
diff --git a/src/adapters/constructionModelesLassoRank_interface.c b/src/adapters/a.constructionModelesLassoRank.c
similarity index 100%
rename from src/adapters/constructionModelesLassoRank_interface.c
rename to src/adapters/a.constructionModelesLassoRank.c
diff --git a/src/adapters/selectiontotale_interface.c b/src/adapters/a.selectiontotale.c
similarity index 100%
rename from src/adapters/selectiontotale_interface.c
rename to src/adapters/a.selectiontotale.c
diff --git a/src/sources/utils/ioutils.c b/src/sources/utils/io.c
similarity index 100%
rename from src/sources/utils/ioutils.c
rename to src/sources/utils/io.c
diff --git a/src/sources/utils/ioutils.h b/src/sources/utils/io.h
similarity index 100%
rename from src/sources/utils/ioutils.h
rename to src/sources/utils/io.h
-- 
2.44.0