#include "mex.h"

/*testo xtimesy*/
void xtimesy(double x, double *y, double *z, int m, int n) {
	int i, j, count=0;
	for (i=0;i<n;i++) {
		for (j=0;j<m;j++) {
			*(z+count)=x**(y+count);
			count++;
		}
	}
}

/*la gateway routine*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
	double *y, *z;
	double x;
	int status, mrows, ncols;
	
	/*Controlla il giusto numero di argomenti*/
	/*NOTA: non serve un altro stmt quando uso mexErrMsgTxt dentro un IF. Non entrerà mai nell'else se mexErrMsgTxt viene eseguito (mexErrMsgTxt esce dal file mex) */
	if (nrhs!=2) mexErrMsgTxt("Due input richiesti");
	if (nlhs!=1) mexErrMsgTxt("Un output richiesto");
	/*Controlla per essere sicuro che il primo argomenti sia uno scalare*/
	if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) || mxGetN(prhs[0])*mxGetM(prhs[0])!=1) {
		mexErrMsgTxt("L'input x deve essere uno scalare"); }
	/*prende lo scalare x*/
	x=mxGetScalar(prhs[0]);
	/*Crea un punttaore per la matrice di input y*/
	y=mxGetPr(prhs[1]);
	/*Ottiene la dimensione della matrice di input y*/
	mrows = mxGetM(prhs[1]);
	ncols = mxGetN(prhs[1]);
	/*Setta il puntatore di output alla matrice  dioutput*/
	plhs[0] = mxCreateDoubleMatrix(mrows,ncols,mxREAL);
	/*Crea un puntatore C alla copia della matrice di output*/
	z = mxGetPr(plhs[0]);
	/*Chiama  la subroutine C*/
	xtimesy(x,y,z,mrows,ncols);
	}
	
	
	
	
