#include <math.h>
#include "mex.h"
#include "matrix.h"
//#include<fstream>


unsigned int numnonzeros(const mxArray*);


void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[]) { 

	int  *ExpTransTktFile;
	int *betaindxflag, *edgeknowindx;
	double *betapara, *tktvectors, *edgeknowledge, *betaparanew, *TRC_ij;	
	double  *tempmax, *temp, *tempneighindx, *tempdiff, *diffmatrix, *exporder;
	

	mwSize i, k;
	mwSize TotalTransSize, betarowsize, betacolnsize, veclen, NumEdge, TktNum, NumExperts;
	mwSize *dims; 
	int j;

	int edgeknow_flag, maxflag;

	mwSize tempneighnum;
	

	betapara=(double *) mxGetPr(prhs[0]);
	ExpTransTktFile=(int *) mxGetPr(prhs[1]); 	
	betaindxflag=(int *)mxGetPr(prhs[2]);	
	TRC_ij=(double *)mxGetPr(prhs[3]);


	

	TotalTransSize=mxGetM(prhs[1]); // How many "transfers"		
	betarowsize=mxGetM(prhs[0]); //the row size of betapara
	betacolnsize=mxGetN(prhs[0]); //the column size of betapara	
	NumExperts=mxGetM(prhs[3]); // the number of experts

	
	
	dims = (mwSize *) mxMalloc (2 * sizeof(mwSize));
    dims[0] = betarowsize;
    dims[1] = betacolnsize;
    plhs[0]= mxCreateNumericArray (2, dims, mxDOUBLE_CLASS, mxREAL);
    betaparanew = (double *) mxGetData (plhs[0]);

	/*dims[0] = betarowsize;
    dims[1] = 1;
    plhs[1]= mxCreateNumericArray (2, dims, mxDOUBLE_CLASS, mxREAL);
    diffmatrix = (double *) mxGetData (plhs[1]);*/


	
	
	tempmax=mxCreateDoubleMatrix(1,1,mxREAL);

	temp=mxCreateDoubleMatrix(1,1,mxREAL);

	tempdiff=mxCreateDoubleMatrix(1,1,mxREAL);


	
	for (i=0;i<TotalTransSize;i++)
	{
		tempmax[0]=0;
		maxflag=0;

	    if (betaindxflag[3*TotalTransSize+i]!=0)
		{
		for (j=0;j<betaindxflag[TotalTransSize+i];j++)
		{
			
			    betaparanew[betaindxflag[i]+j-1]=betapara[betaindxflag[i]+j-1];
				betaparanew[betarowsize+betaindxflag[i]+j-1]=betapara[betarowsize+betaindxflag[i]+j-1];
				betaparanew[2*betarowsize+betaindxflag[i]+j-1]=betapara[2*betarowsize+betaindxflag[i]+j-1];
				betaparanew[3*betarowsize+betaindxflag[i]+j-1]=betapara[3*betarowsize+betaindxflag[i]+j-1];
				betaparanew[4*betarowsize+betaindxflag[i]+j-1]=betapara[4*betarowsize+betaindxflag[i]+j-1];

				edgeknow_flag=((int)betapara[2*betarowsize+betaindxflag[i]+j-1]-1)*NumExperts+(int)betapara[betaindxflag[i]+j-1]-1;

				
				if (betaparanew[4*betarowsize+betaindxflag[i]+j-1]==1)
				{
				betaparanew[3*betarowsize+betaindxflag[i]+j-1]=TRC_ij[edgeknow_flag];	
				tempmax[0]+=TRC_ij[edgeknow_flag];
				}
			
			   
		}

		   for (j=0;j<betaindxflag[TotalTransSize+i];j++)
		   {
			   
				if (betaparanew[4*betarowsize+betaindxflag[i]+j-1]==1)
				{
			    betaparanew[3*betarowsize+betaindxflag[i]+j-1]/=tempmax[0];
				}
		   }
		}
		else
		{
            for (j=0;j<betaindxflag[TotalTransSize+i];j++)
		    {
			
			    betaparanew[betaindxflag[i]+j-1]=betapara[betaindxflag[i]+j-1];
				betaparanew[betarowsize+betaindxflag[i]+j-1]=betapara[betarowsize+betaindxflag[i]+j-1];
				betaparanew[2*betarowsize+betaindxflag[i]+j-1]=betapara[2*betarowsize+betaindxflag[i]+j-1];
				betaparanew[4*betarowsize+betaindxflag[i]+j-1]=betapara[4*betarowsize+betaindxflag[i]+j-1];

				edgeknow_flag=((int)betapara[2*betarowsize+betaindxflag[i]+j-1]-1)*NumExperts+(int)betapara[betaindxflag[i]+j-1]-1;

				
				
				betaparanew[3*betarowsize+betaindxflag[i]+j-1]=TRC_ij[edgeknow_flag];	
				tempmax[0]+=TRC_ij[edgeknow_flag];
				
			    //diffmatrix[betaindxflag[i]+j-1]=temp[0];
		     }

		   for (j=0;j<betaindxflag[TotalTransSize+i];j++)
		    {
			betaparanew[3*betarowsize+betaindxflag[i]+j-1]/=tempmax[0];
		    }
		}

		
	
	}	


}