Beispiel #1
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
   int n, p ; 
   double *a, *l;
   double *adiag, *ldiag;
   int *acol_ptr, *arow_ind ;
   int *lcol_ptr, *lrow_ind ;

   /* Working arrays */

   double *w1, *w2 ; 
   int *iw ;

   /* Local variables */

   int i, nnz ;
   double alpha ;

   /* Right hand side */

   n = (int) mxGetScalar(prhs[0]);  
   a =  mxGetPr(prhs[1]);  
   arow_ind =  mxGetIr(prhs[1]);  
   acol_ptr =  mxGetJc(prhs[1]);  
   adiag = mxGetPr(prhs[2]) ;      
   p = (int) mxGetScalar(prhs[3]) ;

   nnz = acol_ptr[n] ;

   /* Left-hand side */

   if (plhs[0] != NULL) mxDestroyArray(plhs[0]) ;
   plhs[0] = mxCreateSparse(n, n, nnz+n*p, mxREAL) ;

   l =  mxGetPr(plhs[0]);  
   lrow_ind =  mxGetIr(plhs[0]);  
   lcol_ptr =  mxGetJc(plhs[0]);  

   if (plhs[1] != NULL) mxDestroyArray(plhs[1]) ;
   plhs[1] = mxCreateDoubleMatrix(n, 1, mxREAL) ;
   ldiag = mxGetPr(plhs[1]) ;

   /* Allocate working arrays */

   if (( (w1=(double *)calloc(n, sizeof(double)))==(double *)NULL ) ||
       ( (w2=(double *)calloc(n, sizeof(double)))==(double *)NULL ) ||
       ( (iw=(int *)calloc(3*n, sizeof(int)))==(int *)NULL )) {
     printf("Not enough memory\n") ;
   } 

   /* Change a's i j into fortran index */

   for (i = 0; i < nnz; i++) arow_ind[i] = arow_ind[i] + 1 ;
   for (i = 0; i <= n; i++)  acol_ptr[i] = acol_ptr[i] + 1 ;

   /* Call icf fortran subroutine */

   alpha = 0.0 ;

   dicfs_(&n, &nnz, a, adiag, acol_ptr, arow_ind,
	  l, ldiag, lcol_ptr, lrow_ind, &p, &alpha,
	  iw, w1, w2) ;


   /* Change a's i j into C index */
   for (i = 0; i < nnz; i++) 
      arow_ind[i] = arow_ind[i] - 1 ;
   for (i = 0; i < lcol_ptr[n]; i++) 
      lrow_ind[i] = lrow_ind[i] - 1 ;
   for (i = 0; i <= n; i++) {
      acol_ptr[i] = acol_ptr[i] - 1 ;
      lcol_ptr[i] = lcol_ptr[i] - 1 ;
   }

   /* Free memory */

   free(w1) ;   
   free(w2) ;   
   free(iw) ;   
} /* mexFunction */
Beispiel #2
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    /* counters */
    mwIndex c, r, nv, oc, or, vc, tc;
    mwSize m, n;
    double iv;

    /* pointers */
    mwIndex *io, *jo;
    const double *v, *i, *j;
    double *vo;

    /* input check */
    if ((nrhs != 1) &&
        (nrhs != 2) &&
        (nrhs != 5))
        mexErrMsgTxt("Requires 1, 2, or 5 input arguments.");
    if ((mxGetClassID(*prhs) != mxDOUBLE_CLASS) ||
         mxIsSparse(*prhs))
        mexErrMsgTxt("First input argument must be of type full double.");
    nv = mxGetNumberOfElements(*prhs);
    if ((nrhs == 2) &&
        ((!mxIsSparse(prhs[1])) ||
         (mxGetClassID(prhs[1]) != mxDOUBLE_CLASS) ||
         (mxGetNzmax(prhs[1]) < nv)))
        mexErrMsgTxt("For two-argument call, second input argument must be of type sparse double with at least V values.");
    else if ((nrhs == 5) &&
        ((mxGetClassID(prhs[1]) != mxDOUBLE_CLASS) ||
         (mxIsSparse(prhs[1])) ||
         (mxGetNumberOfElements(prhs[1]) != 1) ||
         (mxGetClassID(prhs[2]) != mxDOUBLE_CLASS) ||
         (mxIsSparse(prhs[2])) ||
         (mxGetNumberOfElements(prhs[2]) != 1) ||
         (mxGetClassID(prhs[3]) != mxDOUBLE_CLASS) ||
         (mxIsSparse(prhs[3])) ||
         (mxGetNumberOfElements(prhs[3]) != nv) ||
         (mxGetClassID(prhs[4]) != mxDOUBLE_CLASS) ||
         (mxIsSparse(prhs[4])) ||
         (mxGetNumberOfElements(prhs[4]) != nv)))
        mexErrMsgTxt("For five-argument call, arguments must be Vx1 v, 1x1 m, 1x1 n, Vx1 i, and Vx1 j.");

    /* get data */
    v = (const double *) mxGetData(*prhs);

    /* one argument only */
    if (nrhs == 1) {

        /* create output */
        *plhs = mxCreateSparse(nv, nv, nv, mxREAL);
        if (*plhs == NULL)
            mexErrMsgTxt("Error creating sparse double matrix.");
        vo = (double *) mxGetData(*plhs);
        if (vo == NULL)
            mexErrMsgTxt("Error retrieving double data pointer.");
        io = (mwIndex *) mxGetIr(*plhs);
        if (io == NULL)
            mexErrMsgTxt("Error retrieving Ir index pointer.");
        jo = (mwIndex *) mxGetJc(*plhs);
        if (jo == NULL)
            mexErrMsgTxt("Error retrieving Jc index pointer.");

        /* loop */
        for (vc = 0; vc < nv; ++vc) {
            *vo++ = *v++;
            *io++ = vc;
            *jo++ = vc;
        }
        *jo = vc;

    /* two arguments, overwrite existing sparse */
    } else if (nrhs == 2) {

        /* copy pointer */
        *plhs = (mxArray *) ((void *) prhs[1]);

        /* get data pointer */
        vo = (double *) mxGetData(*plhs);

        /* loop */
        for (vc = nv; vc > 0; --vc)
            *vo++ = *v++;

    /* five arguments, create new sparse from complex data */
    } else {

        /* get size */
        i = (const double *) mxGetData(prhs[1]);
        m = (int) *i;
        i = (const double *) mxGetData(prhs[2]);
        n = (int) *i;
        i = (const double *) mxGetData(prhs[3]);
        j = (const double *) mxGetData(prhs[4]);

        /* create sparse matrix */
        *plhs = mxCreateSparse(m, n, nv, mxREAL);
        if (*plhs == NULL)
            mexErrMsgTxt("Error creating sparse double matrix.");
        vo = (double *) mxGetData(*plhs);
        if (vo == NULL)
            mexErrMsgTxt("Error retrieving double data pointer.");
        io = (mwIndex *) mxGetIr(*plhs);
        if (io == NULL)
            mexErrMsgTxt("Error retrieving Ir index pointer.");
        jo = (mwIndex *) mxGetJc(*plhs);
        if (jo == NULL)
            mexErrMsgTxt("Error retrieving Jc index pointer.");

        /* loop */
        for (vc = nv, or = 0, oc = 0, tc = 0; vc > 0; --vc) {

            /* get target index */
            r = (mwIndex) *i++;
            c = (mwIndex) *j++;
            iv = *v++;

            /* only allow if valid */
            if ((c < oc) ||
                ((c == oc) && (r <= or)) ||
                (r < 1) ||
                (r > m) ||
                (c < 1) ||
                (c > n))
                continue;

            /* update column ? */
            while (c > oc) {
                *jo++ = tc;
                ++oc;
            }

            /* write into arrays */
            *io++ = (r - 1);
            *vo++ = iv;

            /* keep track of written values */
            or = r;
            ++tc;
        }

        /* extend column */
        while (oc <= n) {
            *jo++ = tc;
            ++oc;
        }
    }
}
Beispiel #3
0
void mexFunction
(
    int nlhs,
    mxArray *plhs [],
    int nrhs,
    const mxArray *prhs []
)
{
    /* Compressed column form of L, x and b */
    mwIndex *Lp, *Ap, *Ai, *Up, *pp, *pi ;
    Int *Li, *Ui;
    mwIndex *Lp1, *Li1, *Up1, *Ui1 ;
    double *Lx, *Ax, *Ux, *px ;
    double *Lx1, *Ux1 ;
    double *t1, *t2 ;

    mwIndex anrow ;
    mwIndex ancol ;
    mwIndex lnnz ;                       
    mwIndex unnz ;                       
    mwIndex i ;
    mwIndex j ;
    mwIndex app_xnnz ;
    mwIndex memsize ;
    mwIndex result ;

    mwIndex *ws;  /* workspace */
    double *X ;     /* space to scatter x */
    mwIndex *pinv ; /* column permutation inverse */

    if ( nlhs != 3 || nrhs < 3 )
    {
      //mexErrMsgTxt (" Incorrect number of arguments to sproductmex \n") ;
    }

    Ap = mxGetJc(prhs[0]) ;
    Ai = mxGetIr(prhs[0]) ;
    Ax = mxGetPr(prhs[0]) ;

    anrow = mxGetM (prhs[0]) ;
    ancol = mxGetN (prhs[0]) ;

    t1 = mxGetPr (prhs[1]) ;
    t2 = mxGetPr (prhs[2]) ;

    lnnz = (mwIndex)*t1 ;
    unnz = (mwIndex)*t2 ;
    /*printf("lnnz=%d unnz=%d\n", lnnz, unnz);*/

   
    BaskerClassicNS::BaskerClassic <mwIndex, double> mybasker;
    mybasker.factor(anrow, ancol, lnnz, Ap, Ai, Ax);
    mybasker.returnL(&anrow, &lnnz, &Lp, &Li, &Lx);
    mybasker.returnU(&anrow, &unnz, &Up, &Ui, &Ux);
    mybasker.returnP(&pp);
    

    plhs[0] = mxCreateSparse (anrow, ancol, lnnz+1, mxREAL) ;
    Lp1 = mxGetJc (plhs[0]) ;
    Li1 = mxGetIr (plhs[0]) ;
    Lx1 = mxGetPr (plhs[0]) ;

    plhs[1] = mxCreateSparse (anrow, ancol, unnz, mxREAL) ;
    Up1 = mxGetJc (plhs[1]) ;
    Ui1 = mxGetIr (plhs[1]) ;
    Ux1 = mxGetPr (plhs[1]) ;

    
    mwIndex *pp1, *pp2;
    double *ppx;
    plhs[2] = mxCreateSparse (ancol, ancol, ancol, mxREAL);
    pp1 = mxGetJc (plhs[2]);
    pp2 = mxGetIr (plhs[2]);
    ppx = mxGetPr (plhs[2]);
    
    
    Lp1[0] = Lp[0];
    for ( i = 0 ; i < ancol ; i++)
    {
        Lp1[i+1] = Lp[i+1];
        for ( j = Lp[i] ; j < Lp[i+1] ; j++ )
        {
            Li1[j] = Li[j];
            Lx1[j] = Lx[j];
        }
    } 

    Up1[0] = Up[0];
    for ( i = 0 ; i < ancol ; i++)
    {
        Up1[i+1] = Up[i+1];
        for ( j = Up[i] ; j < Up[i+1] ; j++ )
        {
            Ui1[j] = Ui[j];
            Ux1[j] = Ux[j];
        }
    } 
  
    
    //mexPrintf("Perm \n");
    for ( i = 0; i < ancol; i++)
      {

	//mexPrintf("%d ", pp[i]);
	pp1[i] = i;
	//pp2[i] = i;
	pp2[pp[i]] = i ;
	ppx[i] = 1;
      }
    pp1[ancol] = ancol;

   
    
    mxFree (pp) ;
    mxFree (Lp) ;
    mxFree (Li) ;
    mxFree (Lx) ;
    mxFree (Up) ;
    mxFree (Ui) ;
    mxFree (Ux) ;
    
}
Beispiel #4
0
const char *model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model)
{
	int i, j, n;
	double *ptr;
	mxArray *return_model, **rhs;
	int out_id = 0;

	rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD);

	// Parameters
	rhs[out_id] = mxCreateDoubleMatrix(5, 1, mxREAL);
	ptr = mxGetPr(rhs[out_id]);
	ptr[0] = model->param.svm_type;
	ptr[1] = model->param.kernel_type;
	ptr[2] = model->param.degree;
	ptr[3] = model->param.gamma;
	ptr[4] = model->param.coef0;
	out_id++;

	// nr_class
	rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
	ptr = mxGetPr(rhs[out_id]);
	ptr[0] = model->nr_class;
	out_id++;

	// total SV
	rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
	ptr = mxGetPr(rhs[out_id]);
	ptr[0] = model->l;
	out_id++;

	// rho
	n = model->nr_class*(model->nr_class-1)/2;
	rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
	ptr = mxGetPr(rhs[out_id]);
	for(i = 0; i < n; i++)
		ptr[i] = model->rho[i];
	out_id++;

	// Label
	if(model->label)
	{
		rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL);
		ptr = mxGetPr(rhs[out_id]);
		for(i = 0; i < model->nr_class; i++)
			ptr[i] = model->label[i];
	}
	else
		rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
	out_id++;

	// probA, probB
	if(model->probA != NULL && model->probB != NULL)
	{
		rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
		ptr = mxGetPr(rhs[out_id]);
		for(i = 0; i < n; i++)
			ptr[i] = model->probA[i];

		rhs[out_id+1] = mxCreateDoubleMatrix(n, 1, mxREAL);
		ptr = mxGetPr(rhs[out_id+1]);
		for(i = 0; i < n; i++)
			ptr[i] = model->probB[i];
	}
	else
	{
		rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
		rhs[out_id+1] = mxCreateDoubleMatrix(0, 0, mxREAL);
	}
	out_id+=2;

	// nSV
	if(model->nSV)
	{
		rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL);
		ptr = mxGetPr(rhs[out_id]);
		for(i = 0; i < model->nr_class; i++)
			ptr[i] = model->nSV[i];
	}
	else
		rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
	out_id++;

	// sv_coef
	rhs[out_id] = mxCreateDoubleMatrix(model->l, model->nr_class-1, mxREAL);
	ptr = mxGetPr(rhs[out_id]);
	for(i = 0; i < model->nr_class-1; i++)
		for(j = 0; j < model->l; j++)
			ptr[(i*(model->l))+j] = model->sv_coef[i][j];
	out_id++;

	// SVs
	{
		int ir_index, nonzero_element;
		mwIndex *ir, *jc;
		mxArray *pprhs[1], *pplhs[1];	

		if(model->param.kernel_type == PRECOMPUTED)
		{
			nonzero_element = model->l;
			num_of_feature = 1;
		}
		else
		{
			nonzero_element = 0;
			for(i = 0; i < model->l; i++) {
				j = 0;
				while(model->SV[i][j].index != -1) {
					nonzero_element++;
					j++;
				}
			}
		}

		// SV in column, easier accessing
		rhs[out_id] = mxCreateSparse(num_of_feature, model->l, nonzero_element, mxREAL);
		ir = mxGetIr(rhs[out_id]);
		jc = mxGetJc(rhs[out_id]);
		ptr = mxGetPr(rhs[out_id]);
		ir_index = jc[0] = 0;
		for(i = 0;i < model->l; i++)
		{
			if(model->param.kernel_type == PRECOMPUTED)
			{
				// make a (1 x model->l) matrix
				ir[ir_index] = 0; 
				ptr[ir_index] = model->SV[i][0].value;
				ir_index++;
				jc[i+1] = jc[i] + 1;
			}
			else
			{
				int x_index = 0;
				while (model->SV[i][x_index].index != -1)
				{
					ir[ir_index] = model->SV[i][x_index].index - 1; 
					ptr[ir_index] = model->SV[i][x_index].value;
					ir_index++, x_index++;
				}
				jc[i+1] = jc[i] + x_index;
			}
		}
		// transpose back to SV in row
		pprhs[0] = rhs[out_id];
		if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose"))
			return "cannot transpose SV matrix";
		rhs[out_id] = pplhs[0];
		out_id++;
	}

	/* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */
	return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names);

	/* Fill struct matrix with input arguments */
	for(i = 0; i < NUM_OF_RETURN_FIELD; i++)
		mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i]));
	/* return */
	plhs[0] = return_model;
	mxFree(rhs);

	return NULL;
}
Beispiel #5
0
void mexFunction(
    int nargout,
    mxArray *out[],
    int nargin,
    const mxArray *in[]
)
{
    /* declare variables */
    int nr, nc, np, total;
    int i, j, k, ix, iy, jx, jy, ii, jj, iip1, jjp1, iip2, jjp2, step;
    double sigma, di, dj, a, z, maxori, phase1, phase2, slope;
// 	int *ir, *jc;
    mwIndex*ir,*jc;
	unsigned int *pi, *pj;
	double *emag, *ephase, *w;
    
    /* check argument */
    if (nargin<4) {
        mexErrMsgTxt("Four input arguments required");
    }
    if (nargout>1) {
        mexErrMsgTxt("Too many output arguments");
    }

    /* get edgel information */
	nr = mxGetM(in[0]);
	nc = mxGetN(in[0]);
	if ( nr*nc ==0 || nr != mxGetM(in[1]) || nc != mxGetN(in[1]) ) {
	    mexErrMsgTxt("Edge magnitude and phase shall be of the same image size");
	}
    emag = mxGetPr(in[0]);
    ephase = mxGetPr(in[1]);
    np = nr * nc;
    
    /* get new index pair */
    if (!mxIsUint32(in[2]) | !mxIsUint32(in[3])) {
        mexErrMsgTxt("Index pair shall be of type UINT32");
    }
    if (mxGetM(in[3]) * mxGetN(in[3]) != np + 1) {
        mexErrMsgTxt("Wrong index representation");
    }
    pi = (unsigned int*)mxGetData(in[2]); /* pointer to w_i */
    pj = (unsigned int*)mxGetData(in[3]); /* pointer to w_j */   

    /* create output */
    out[0] = mxCreateSparse(np,np,pj[np],mxREAL);
    if (out[0]==NULL) {
	    mexErrMsgTxt("Not enough memory for the output matrix");
	}
	w = mxGetPr(out[0]);
	ir = mxGetIr(out[0]); /* */
	jc = mxGetJc(out[0]);
	
    /* find my sigma */
	if (nargin<5) {
	    sigma = 0;
    	for (k=0; k<np; k++) { 
    	    if (emag[k]>sigma) { sigma = emag[k]; }
    	}
    	sigma = sigma / 6;
    	printf("sigma = %6.5f",sigma);
	} else {
	    sigma = mxGetScalar(in[4]);
	}
	a = 0.5 / (sigma * sigma);
	
    /* computation */ 
    total = 0;
    for (j=0; j<np; j++) {            

        jc[j] = total;
        jx = j / nr; /* col */
        jy = j % nr; /* row */
        
        for (k=pj[j]; k<pj[j+1]; k++) {
        
            i = pi[k];
          
            if (i==j) {
                maxori = 1;
            
            } else {
        
                ix = i / nr; 
                iy = i % nr;

                /* scan */            
                di = (double) (iy - jy);
                dj = (double) (ix - jx);
            
                maxori = 0.;
	            phase1 = ephase[j];

	               
                /* sample in i direction */
                if (abs(di) >= abs(dj)) {  
            	    slope = dj / di;
            	    step = (iy>=jy) ? 1 : -1;
            	
              	    iip1 = jy;
            	    jjp1 = jx;
	
	               
	                for (ii=0;ii<abs(di);ii++){
	                    iip2 = iip1 + step;
	                    jjp2 = (int)(0.5 + slope*(iip2-jy) + jx);
	  	  
	                    phase2 = ephase[iip2+jjp2*nr];
               
	                    if (phase1 != phase2) {
	                        z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]);
	                        if (z > maxori){
	                            maxori = z;
	                        }
	                    } 
	             
	                    iip1 = iip2;
	                    jjp1 = jjp2;
	                    phase1 = phase2;
	                }
	            
	            /* sample in j direction */    
                } else { 
	                slope = di / dj;
	                step =  (ix>=jx) ? 1: -1;

    	            jjp1 = jx;
	                iip1 = jy;	           
	    
	 
	                for (jj=0;jj<abs(dj);jj++){
	                    jjp2 = jjp1 + step;
	                    iip2 = (int)(0.5+ slope*(jjp2-jx) + jy);
	  	  
	                    phase2 = ephase[iip2+jjp2*nr];
	     
	                    if (phase1 != phase2){
	                        z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]);
	                        if (z > maxori){ 
	                            maxori = z; 
	                        }
	                        
	                    }
	  
	                    iip1 = iip2;
	                    jjp1 = jjp2;
	                    phase1 = phase2;
	                }
                }            
            
                maxori = 0.5 * maxori; 
                //maxori = exp(-maxori * maxori * a); //segmentation
                maxori = 1-exp(-maxori * maxori * a); //symmetry
            }       
		    ir[total] = i;

		    if (maxori>0.2) w[total] = maxori; else w[total]=0.2; //symmetry
            //w[total] = maxori; //segmentation

		    total = total + 1;
			
		} /* i */
    } /* j */
        
    jc[np] = total;
}  
Beispiel #6
0
void mexFunction
(
    /* === Parameters ======================================================= */

    int nlhs,			/* number of left-hand sides */
    mxArray *plhs [],		/* left-hand side matrices */
    int nrhs,			/* number of right--hand sides */
    const mxArray *prhs []	/* right-hand side matrices */
)
{
    Zmat A;

    char       *fname, rhstyp[4], title[81], key[9], type[4];
    int        i,j,k,l, mynrhs, status;
    integer    nrhsix, m, *rhsptr=NULL, *rhsind=NULL, ierr, nr,nc,nz, tmp,tmp0,tmp2,tmp3,ncolumns;
    size_t     mrows, ncols, sizebuf;
    mwSize     nnz, buflen;
    mwIndex    *irs, *jcs;
    double     *pr, *pi, *sr, *si;
    doublecomplex *sol, *rhs=NULL, *rhsval=NULL;
    mxArray    *fout, *f_input;
    FILE *fp;


    if (nrhs!=1)
       mexErrMsgTxt("Only one input argument required.");
    else if (nlhs!=3)
       mexErrMsgTxt("Three output arguments are required.");
    else if (mxGetClassID(prhs[0])!=mxCHAR_CLASS)
       mexErrMsgTxt("Input must be a string.");


    /* get filename */
    f_input = (mxArray *) prhs[0];
    mrows = mxGetM (f_input) ;
    ncols = mxGetN (f_input) ;
    /* Get the length of the input string. */
    buflen = (mrows*ncols) + 1;

    /* Allocate memory for input and output strings. */
    fname = (char *) mxCalloc((size_t)buflen, (size_t)sizeof(char));

    /* Copy the string data from tmp into a C string pdata */
    status = mxGetString(f_input, fname, buflen);


    ncolumns = 0;
    tmp0 = 0;

    if ((fp=fopen(fname,"r"))==NULL) {
       mexPrintf(" file %s",fname);
       mexErrMsgTxt(" not found");
       return;
    }
    fclose(fp);

    rhstyp[0]=' ';
    rhstyp[1]=' ';
    rhstyp[2]=' ';


    A.nc=0;
    Zreadmtc(&tmp0,&tmp0,&tmp0,fname,A.a,A.ja,A.ia,
	     rhs,&ncolumns,rhstyp,&nr,&nc,&nz,title,key,type,
	     &nrhsix,rhsptr,rhsind,rhsval,&ierr,strlen(fname),2,72,8,3);
    ncols=ncolumns;
    /* if a right hand side is given, then use these */
    if (ierr) {
        mexPrintf(" ierr = %d\n",ierr);
        mexErrMsgTxt(" error in reading the matrix, stop.\n");
	switch(ierr) {
	case 1:
	  mexErrMsgTxt("too many columns\n");
	  break;  
	case 2:
	  mexErrMsgTxt("too many nonzeros\n");
	  break;  
	case 3:
	  mexErrMsgTxt("too many columns and nonzeros\n");
	  break;  
	case 4:
	  mexErrMsgTxt("right hand side has incompatible type\n");
	  break;  
	case 5:
	  mexErrMsgTxt("too many right hand side entries\n");
	  break;  
	case 6:
	  mexErrMsgTxt("wrong type (real/complex)\n");
	  break;  
	}
        exit(ierr);
    }

    if (ncols>0) {
       m=1;
       if (rhstyp[1]=='G' || rhstyp[1]=='g') {
	 m++;
       }
       if (rhstyp[2]=='X' || rhstyp[2]=='x') {
	 m++;
       }
    }
    else
       m=0;
    m*=nr*ncols;

    rhsptr=NULL;
    rhsind=NULL;
    rhsval=NULL;
    if (ncols!=0 && (rhstyp[0]=='M' || rhstyp[0]=='m')) {
       rhsptr=(integer *)  MAlloc((size_t)(ncols+1)*sizeof(integer),"Zloadhbo:rhsptr");
       rhsind=(integer *)  MAlloc((size_t)nrhsix*sizeof(integer),   "Zloadhbo:rhsind");
       rhsval=(doublecomplex *)   MAlloc((size_t)nrhsix*sizeof(doublecomplex),    "Zloadhbo:rhsval");
    }
    

    A.ia=(integer *)     MAlloc((size_t)(nc+1)*sizeof(integer),  "Zloadhbo:A.ia");
    A.ja=(integer *)     MAlloc((size_t)nz*sizeof(integer),      "Zloadhbo:A.ja");
    A.a =(doublecomplex*)MAlloc((size_t)nz*sizeof(doublecomplex),"Zloadhbo:A.a");
    A.nr=nr;
    A.nc=nc;
    rhs =(doublecomplex *)MAlloc((size_t)m*sizeof(doublecomplex),"Zloadhbo:rhs");
    /* advance pointer to reserve space when uncompressing the right hand side */
    if (ncols!=0 && (rhstyp[0]=='M' || rhstyp[0]=='m'))
       rhs+=nr*ncols;
    
    tmp = 3;
    tmp2 = nc;
    tmp3 = nz;

    if (ncols!=0 && (rhstyp[0]=='M' || rhstyp[0]=='m'))
       m-=nr*ncols;
    Zreadmtc(&tmp2,&tmp3,&tmp,fname,A.a,A.ja,A.ia,
	    rhs,&m,rhstyp,&nr,&nc,&nz,title,key,type,
	    &nrhsix,rhsptr,rhsind,rhsval,&ierr,strlen(fname),2,72,8,3);
    if (ncols!=0 && (rhstyp[0]=='M' || rhstyp[0]=='m'))
       m+=nr*ncols;



    if (ierr) {
        mexPrintf(" ierr = %d\n",ierr);
        mexErrMsgTxt(" error in reading the matrix, stop.\n");
	return;
    }


    /* set output parameters */
    nlhs=3;
    fout =mxCreateSparse((mwSize)nr,(mwSize)nc, (mwSize)nz, mxCOMPLEX);
    plhs[0]=fout;

    sr  = (double *)  mxGetPr(fout);
    si  = (double *)  mxGetPi(fout);
    irs = (mwIndex *) mxGetIr(fout);
    jcs = (mwIndex *) mxGetJc(fout);

    jcs[0]=0;
    for (i=0; i<nc; i++) {
        jcs[i+1]=A.ia[i+1]-1;
	for (j=A.ia[i]-1; j<A.ia[i+1]-1; j++) {
	    irs[j]=A.ja[j]-1;
	    sr[j] =A.a[j].r;
	    si[j] =A.a[j].i;
	}
    }


    if (ncols>0) {
       m=1;
       if (rhstyp[1]=='G' || rhstyp[1]=='g') {
	 m++;
       }
       if (rhstyp[2]=='X' || rhstyp[2]=='x') {
	 m++;
       }
    }
    else
       m=0;


    fout=mxCreateDoubleMatrix((mwSize)nr,(mwSize)m*ncols, mxCOMPLEX);
    plhs[1]=fout;

    if (ncols!=0 && (rhstyp[0]=='M' || rhstyp[0]=='m')) {
    }
    else {
	pr = mxGetPr(fout);
	pi = mxGetPi(fout);
	for (i=0; i<nr*m*ncols; i++) {
	    pr[i]=rhs[i].r;
	    pi[i]=rhs[i].i;
	}
    }

    rhstyp[3]='\0';
    plhs[2] = mxCreateString(rhstyp);



    free(A.a);
    free(A.ia);
    free(A.ja);
    if (ncols>0)
      free(rhs);

    if (ncols!=0 && (rhstyp[0]=='M' || rhstyp[0]=='m')) {
      free(rhsptr);
      free(rhsind);
      free(rhsval);
    }

    return;
}
Beispiel #7
0
Need ompcore(double D[], double x[], double DtX[], double XtX[], double G[], mwSize n, mwSize m, mwSize L,
                 int T, double eps, int gamma_mode, int profile, double msg_delta, int erroromp)
{
  
  profdata pd;
  /* mxArray *Gamma;*/
  mwIndex i, j, signum, pos, *ind, *gammaIr, *gammaJc, gamma_count;
  mwSize allocated_coefs, allocated_cols;
  int DtX_specified, XtX_specified, batchomp, standardomp, *selected_atoms,*times_atoms ;
  double *alpha, *r, *Lchol, *c, *Gsub, *Dsub, sum, *gammaPr, *tempvec1, *tempvec2; 
  double eps2, resnorm, delta, deltaprev, secs_remain;
  int mins_remain, hrs_remain;
  clock_t lastprint_time, starttime;

  Need my;
  
  /*** status flags ***/
  
  DtX_specified = (DtX!=0);   /* indicates whether D'*x was provided */
  XtX_specified = (XtX!=0);   /* indicates whether sum(x.*x) was provided */
  
  standardomp = (G==0);       /* batch-omp or standard omp are selected depending on availability of G */
  batchomp = !standardomp;
  
  
  
  /*** allocate output matrix ***/
  
  
  if (gamma_mode == FULL_GAMMA) {
    
    /* allocate full matrix of size m X L */
    
   Gamma = mxCreateDoubleMatrix(m, L, mxREAL);
    gammaPr = mxGetPr(Gamma);
    gammaIr = 0;
    gammaJc = 0;
  }
  else {
    
    /* allocate sparse matrix with room for allocated_coefs nonzeros */
    
    /* for error-omp, begin with L*sqrt(n)/2 allocated nonzeros, otherwise allocate L*T nonzeros */
    allocated_coefs = erroromp ? (mwSize)(ceil(L*sqrt((double)n)/2.0) + 1.01) : L*T;
    Gamma = mxCreateSparse(m, L, allocated_coefs, mxREAL);
    gammaPr = mxGetPr(Gamma);
    gammaIr = mxGetIr(Gamma);
    gammaJc = mxGetJc(Gamma);
    gamma_count = 0;
    gammaJc[0] = 0;
  }
  
  
  /*** helper arrays ***/
  
  alpha = (double*)mxMalloc(m*sizeof(double));        /* contains D'*residual */
  ind = (mwIndex*)mxMalloc(n*sizeof(mwIndex));        /* indices of selected atoms */
  selected_atoms = (int*)mxMalloc(m*sizeof(int));     /* binary array with 1's for selected atoms */
  times_atoms = (int*)mxMalloc(m*sizeof(int)); 
  c = (double*)mxMalloc(n*sizeof(double));            /* orthogonal projection result */
  
  /* current number of columns in Dsub / Gsub / Lchol */
  allocated_cols = erroromp ? (mwSize)(ceil(sqrt((double)n)/2.0) + 1.01) : T;
  
  /* Cholesky decomposition of D_I'*D_I */
  Lchol = (double*)mxMalloc(n*allocated_cols*sizeof(double));

  /* temporary vectors for various computations */
  tempvec1 = (double*)mxMalloc(m*sizeof(double));
  tempvec2 = (double*)mxMalloc(m*sizeof(double));
  
  if (batchomp) {
    /* matrix containing G(:,ind) - the columns of G corresponding to the selected atoms, in order of selection */
    Gsub = (double*)mxMalloc(m*allocated_cols*sizeof(double));
  }
  else {
    /* matrix containing D(:,ind) - the selected atoms from D, in order of selection */
    Dsub = (double*)mxMalloc(n*allocated_cols*sizeof(double));
    
    /* stores the residual */
    r = (double*)mxMalloc(n*sizeof(double));        
  }
  
  if (!DtX_specified) {
    /* contains D'*x for the current signal */
    DtX = (double*)mxMalloc(m*sizeof(double));  
  }
  
  
  
  /*** initializations for error omp ***/
  
  if (erroromp) {
    eps2 = eps*eps;        /* compute eps^2 */
    if (T<0 || T>n) {      /* unspecified max atom num - set max atoms to n */
      T = n;
    }
  }
  
  
  
  /*** initialize timers ***/
  
  initprofdata(&pd);             /* initialize profiling counters */
  starttime = clock();           /* record starting time for eta computations */
  lastprint_time = starttime;    /* time of last status display */
  
  
  
  /**********************   perform omp for each signal   **********************/
  
  
  
  for (signum=0; signum<L; ++signum) {
    
    
    /* initialize residual norm and deltaprev for error-omp */
    
    if (erroromp) {
      if (XtX_specified) {
        resnorm = XtX[signum];
      }
      else {
        resnorm = dotprod(x+n*signum, x+n*signum, n);
        addproftime(&pd, XtX_TIME);
      }
      deltaprev = 0;     /* delta tracks the value of gamma'*G*gamma */
    }
    else {
      /* ignore residual norm stopping criterion */
      eps2 = 0;
      resnorm = 1;
    }
    
    
    if (resnorm>eps2 && T>0) {
      
      /* compute DtX */
      
      if (!DtX_specified) {
        matT_vec(1, D, x+n*signum, DtX, n, m);
        addproftime(&pd, DtX_TIME);
      }
      
      
      /* initialize alpha := DtX */
      
      memcpy(alpha, DtX + m*signum*DtX_specified, m*sizeof(double));
      
      
      /* mark all atoms as unselected */
      
      for (i=0; i<m; ++i) {
        selected_atoms[i] = 0;
      }
	   for (i=0; i<m; ++i) {
        times_atoms[i] = 0;
      }
      
    }
    

    /* main loop */
    
    i=0;
    while (resnorm>eps2 && i<T) {

      /* index of next atom */
      
      pos = maxabs(alpha, m);
      addproftime(&pd, MAXABS_TIME);
      
      
      /* stop criterion: selected same atom twice, or inner product too small */
      
      if (selected_atoms[pos] || alpha[pos]*alpha[pos]<1e-14) {
        break;
      }
      
      
      /* mark selected atom */
      
      ind[i] = pos;
      selected_atoms[pos] = 1;
	  times_atoms[pos]++;
      
      
      /* matrix reallocation */
      
      if (erroromp && i>=allocated_cols) {
        
        allocated_cols = (mwSize)(ceil(allocated_cols*MAT_INC_FACTOR) + 1.01);
        
        Lchol = (double*)mxRealloc(Lchol,n*allocated_cols*sizeof(double));
        
        batchomp ? (Gsub = (double*)mxRealloc(Gsub,m*allocated_cols*sizeof(double))) :
                   (Dsub = (double*)mxRealloc(Dsub,n*allocated_cols*sizeof(double))) ;
      }
      
      
      /* append column to Gsub or Dsub */
      
      if (batchomp) {
        memcpy(Gsub+i*m, G+pos*m, m*sizeof(double));
      }
      else {
        memcpy(Dsub+i*n, D+pos*n, n*sizeof(double));
      }
      
      
      /*** Cholesky update ***/
      
      if (i==0) {
        *Lchol = 1;
      }
      else {
        
        /* incremental Cholesky decomposition: compute next row of Lchol */
        
        if (standardomp) {
          matT_vec(1, Dsub, D+n*pos, tempvec1, n, i);      /* compute tempvec1 := Dsub'*d where d is new atom */
          addproftime(&pd, DtD_TIME);
        }
        else {
          vec_assign(tempvec1, Gsub+i*m, ind, i);          /* extract tempvec1 := Gsub(ind,i) */
        }
        backsubst('L', Lchol, tempvec1, tempvec2, n, i);   /* compute tempvec2 = Lchol \ tempvec1 */
        for (j=0; j<i; ++j) {                              /* write tempvec2 to end of Lchol */
          Lchol[j*n+i] = tempvec2[j];
        }
        
        /* compute Lchol(i,i) */
        sum = 0;
        for (j=0; j<i; ++j) {         /* compute sum of squares of last row without Lchol(i,i) */
          sum += SQR(Lchol[j*n+i]);
        }
        if ( (1-sum) <= 1e-14 ) {     /* Lchol(i,i) is zero => selected atoms are dependent */
          break;
        }
        Lchol[i*n+i] = sqrt(1-sum);
      }
      
      addproftime(&pd, LCHOL_TIME);

      i++;
      
      
      /* perform orthogonal projection and compute sparse coefficients */
      
      vec_assign(tempvec1, DtX + m*signum*DtX_specified, ind, i);   /* extract tempvec1 = DtX(ind) */
      cholsolve('L', Lchol, tempvec1, c, n, i);                     /* solve LL'c = tempvec1 for c */
      addproftime(&pd, COMPCOEF_TIME);
      

      /* update alpha = D'*residual */
      
      if (standardomp) {
        mat_vec(-1, Dsub, c, r, n, i);             /* compute r := -Dsub*c */
        vec_sum(1, x+n*signum, r, n);              /* compute r := x+r */
        
        
        /*memcpy(r, x+n*signum, n*sizeof(double));   /* assign r := x */
        /*mat_vec1(-1, Dsub, c, 1, r, n, i);         /* compute r := r-Dsub*c */
        
        addproftime(&pd, COMPRES_TIME);
        matT_vec(1, D, r, alpha, n, m);            /* compute alpha := D'*r */
        addproftime(&pd, DtR_TIME);
        
        /* update residual norm */
        if (erroromp) {
          resnorm = dotprod(r, r, n);
          addproftime(&pd, UPDATE_RESNORM_TIME);
        }
      }
      else {
        mat_vec(1, Gsub, c, tempvec1, m, i);                              /* compute tempvec1 := Gsub*c */
        memcpy(alpha, DtX + m*signum*DtX_specified, m*sizeof(double));    /* set alpha = D'*x */
        vec_sum(-1, tempvec1, alpha, m);                                  /* compute alpha := alpha - tempvec1 */
        addproftime(&pd, UPDATE_DtR_TIME);
        
        /* update residual norm */
        if (erroromp) {
          vec_assign(tempvec2, tempvec1, ind, i);      /* assign tempvec2 := tempvec1(ind) */
          delta = dotprod(c,tempvec2,i);               /* compute c'*tempvec2 */
          resnorm = resnorm - delta + deltaprev;       /* residual norm update */
          deltaprev = delta;
          addproftime(&pd, UPDATE_RESNORM_TIME);
        }
      }
    }
    
    
    /*** generate output vector gamma ***/

    if (gamma_mode == FULL_GAMMA) {    /* write the coefs in c to their correct positions in gamma */
      for (j=0; j<i; ++j) {
        gammaPr[m*signum + ind[j]] = c[j];
      }
    }
    else {
      /* sort the coefs by index before writing them to gamma */
      quicksort(ind,c,i);
      addproftime(&pd, INDEXSORT_TIME);
      
      /* gamma is full - reallocate */
      if (gamma_count+i >= allocated_coefs) {
        
        while(gamma_count+i >= allocated_coefs) {
          allocated_coefs = (mwSize)(ceil(GAMMA_INC_FACTOR*allocated_coefs) + 1.01);
        }
        
        mxSetNzmax(Gamma, allocated_coefs);
        mxSetPr(Gamma, mxRealloc(gammaPr, allocated_coefs*sizeof(double)));
        mxSetIr(Gamma, mxRealloc(gammaIr, allocated_coefs*sizeof(mwIndex)));
        
        gammaPr = mxGetPr(Gamma);
        gammaIr = mxGetIr(Gamma);
      }
      
      /* append coefs to gamma and update the indices */
      for (j=0; j<i; ++j) {
        gammaPr[gamma_count] = c[j];
        gammaIr[gamma_count] = ind[j];
        gamma_count++;
      }
      gammaJc[signum+1] = gammaJc[signum] + i;
    }
    
    
    
    /*** display status messages ***/
    
    if (msg_delta>0 && (clock()-lastprint_time)/(double)CLOCKS_PER_SEC >= msg_delta)
    {
      lastprint_time = clock();
      
      /* estimated remainig time */
      secs2hms( ((L-signum-1)/(double)(signum+1)) * ((lastprint_time-starttime)/(double)CLOCKS_PER_SEC) ,
        &hrs_remain, &mins_remain, &secs_remain);
      
      mexPrintf("omp: signal %d / %d, estimated remaining time: %02d:%02d:%05.2f\n",        
        signum+1, L, hrs_remain, mins_remain, secs_remain);
      mexEvalString("drawnow;");
    }
    
  }
  
  /* end omp */
  
  
  
  /*** print final messages ***/
  
  if (msg_delta>0) {
    mexPrintf("omp: signal %d / %d\n", signum, L);
  }
  
  if (profile) {
    printprofinfo(&pd, erroromp, batchomp, L);
  }
  
  
  
  /* free memory */
  
  if (!DtX_specified) {
    mxFree(DtX);
  }
  if (standardomp) {
    mxFree(r);
    mxFree(Dsub);
  }
  else {
    mxFree(Gsub);
  }  
  mxFree(tempvec2);
  mxFree(tempvec1);
  mxFree(Lchol);
  mxFree(c);
  mxFree(selected_atoms);
  mxFree(ind);
  mxFree(alpha);

  my.qGamma=Gamma;
  my.qtimes__atoms=times__atoms;
  
  /*return Gamma;*/
  return  my;
 
}
/* read in a problem (in svmlight format) */
void read_problem(const char *filename, mxArray *plhs[])
{
	int elements, max_index, min_index, i, k;
	FILE *fp = fopen(filename,"r");
	int l = 0;
	mwIndex *ir, *jc;
	/* double *labels, *samples; */
    double *samples;
	
	if(fp == NULL)
	{
		mexPrintf("can't open input file %s\n",filename);
		fake_answer(plhs);
		return;
	}

	max_index = 0;
	min_index = 1; /* our index starts from 1 */
	elements = 0;
    
    int index;
    double value;
    
	while(1)
	{
		/* label */
		/* int index;
		double value;
		fscanf(fp,"%lf",&value); */

		/* features */
		while(1)
		{
			int c;
			do {
				c = getc(fp);
				if(c=='\n') goto out;
				if(c==EOF) goto eof;
			} while(isspace(c));
			ungetc(c,fp);
			fscanf(fp,"%d:%lf",&index, &value);
			if (index < min_index)
				min_index = index;
			elements++;
		}	
out:
		if(index > max_index)
			max_index = index;
		l++;
	}
eof:
	rewind(fp);

	/* y */
	/* plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL); */
	/* x^T */
	if (min_index <= 0)
		/* plhs[1] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL); */
        plhs[0] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL);
	else
		/* plhs[1] = mxCreateSparse(max_index, l, elements, mxREAL); */
        plhs[0] = mxCreateSparse(max_index, l, elements, mxREAL);
    
	/* labels = mxGetPr(plhs[0]); */
	/* samples = mxGetPr(plhs[1]); */
    samples = mxGetPr(plhs[0]);
    
    /*
	ir = mxGetIr(plhs[1]);
	jc = mxGetJc(plhs[1]); */
    
    ir = mxGetIr(plhs[0]);
	jc = mxGetJc(plhs[0]);

	k=0;
	for(i=0;i<l;i++)
	{
		jc[i] = k;
		/* fscanf(fp,"%lf",&labels[i]); */

		while(1)
		{
			int c, index;
			do {
				c = getc(fp);
				if(c=='\n') goto out2;
			} while(isspace(c));
			ungetc(c,fp);
			fscanf(fp,"%d:%lf",&index,&samples[k]);
			ir[k] = index - min_index; /* precomputed kernel has <index> start from 0 */
			++k;
		}	
out2:
		;
	}
	jc[l] = k;

	fclose(fp);

    /*
	{
		mxArray *rhs[1], *lhs[1];
        rhs[0] = plhs[0];
		if(mexCallMATLAB(1, lhs, 1, rhs, "transpose"))
		{
			mexPrintf("Error: cannot transpose problem\n");
			return;
		}
        plhs[0] = lhs[0];
	}
     */

}
/* ************************************************************
   PROCEDURE mexFunction - Entry for Matlab
   ************************************************************ */
void mexFunction(const int nlhs, mxArray *plhs[],
  const int nrhs, const mxArray *prhs[])
{
  const mxArray *L_FIELD;
  mxArray *myplhs[NPAROUT];
  int    m, i, j, inz, iwsiz, nsuper, tmpsiz, fwsiz, nskip, nadd, m1;
  double *fwork, *d, *skipPr, *orgd;
  const double *permPr,*xsuperPr,*Ppr,*absd;
  int    *perm, *snode, *xsuper, *iwork, *xlindx, *skip, *skipJc;
  const int *LINir, *Pjc, *Pir;
  double canceltol, maxu, abstol;
  jcir   L;
  char useAbsd, useDelay;
/* ------------------------------------------------------------
   Check for proper number of arguments
   blkchol(L,P, pars,absd) with nparinmin=2.
   ------------------------------------------------------------ */
  if(nrhs < NPARINMIN)
    mexErrMsgTxt("blkchol requires more input arguments");
  if(nlhs > NPAROUT)
    mexErrMsgTxt("blkchol produces less output arguments");
/* ------------------------------------------------------------
   Get input matrix P to be factored.
   ------------------------------------------------------------ */
  if( (m = mxGetM(P_IN)) != mxGetN(P_IN))
    mexErrMsgTxt("P must be square");
  if(!mxIsSparse(P_IN))
    mexErrMsgTxt("P must be sparse");
  Pjc    = mxGetJc(P_IN);
  Pir    = mxGetIr(P_IN);
  Ppr    = mxGetPr(P_IN);
/* ------------------------------------------------------------
   Disassemble block Cholesky structure L
   ------------------------------------------------------------ */
  if(!mxIsStruct(L_IN))
    mexErrMsgTxt("Parameter `L' should be a structure.");
  if( (L_FIELD = mxGetField(L_IN,0,"perm")) == NULL)        /* L.perm */
    mexErrMsgTxt("Missing field L.perm.");
  if(m != mxGetM(L_FIELD) * mxGetN(L_FIELD))
    mexErrMsgTxt("perm size mismatch");
  permPr = mxGetPr(L_FIELD);
  if( (L_FIELD = mxGetField(L_IN,0,"L")) == NULL)           /* L.L */
    mexErrMsgTxt("Missing field L.L.");
  if( m != mxGetM(L_FIELD) || m != mxGetN(L_FIELD) )
    mexErrMsgTxt("Size L.L mismatch.");
  if(!mxIsSparse(L_FIELD))
    mexErrMsgTxt("L.L should be sparse.");
  L.jc = mxGetJc(L_FIELD);
  LINir = mxGetIr(L_FIELD);
  if( (L_FIELD = mxGetField(L_IN,0,"xsuper")) == NULL)      /* L.xsuper */
    mexErrMsgTxt("Missing field L.xsuper.");
  nsuper = mxGetM(L_FIELD) * mxGetN(L_FIELD) - 1;
  if( nsuper > m )
    mexErrMsgTxt("Size L.xsuper mismatch.");
  xsuperPr = mxGetPr(L_FIELD);
  if( (L_FIELD = mxGetField(L_IN,0,"tmpsiz")) == NULL)      /* L.tmpsiz */
    mexErrMsgTxt("Missing field L.tmpsiz.");
  tmpsiz   = mxGetScalar(L_FIELD);
/* ------------------------------------------------------------
   Disassemble pars structure: canceltol, maxu
   ------------------------------------------------------------ */
  canceltol = 1E-15;           /* supply with defaults */
  maxu = 5E5;
  abstol = 1E-20;
  useAbsd = 0;
  useDelay = 0;
  if(nrhs >= NPARINMIN + 1){       /* 3rd argument = pars */
    if(!mxIsStruct(PARS_IN))
      mexErrMsgTxt("Parameter `pars' should be a structure.");
    if( (L_FIELD = mxGetField(PARS_IN,0,"canceltol")) != NULL)
      canceltol  = mxGetScalar(L_FIELD);  /* pars.canceltol */
    if( (L_FIELD = mxGetField(PARS_IN,0,"maxu")) != NULL)
      maxu = mxGetScalar(L_FIELD);  /* pars.maxu */
    if( (L_FIELD = mxGetField(PARS_IN,0,"abstol")) != NULL){
      abstol = mxGetScalar(L_FIELD);  /* pars.abstol */
      abstol = MAX(abstol, 0.0);
    }
    if( (L_FIELD = mxGetField(PARS_IN,0,"delay")) != NULL)
      useDelay = mxGetScalar(L_FIELD);  /* pars.delay */
/* ------------------------------------------------------------
   Get optional vector absd
   ------------------------------------------------------------ */
    if(nrhs >= NPARIN){
      useAbsd = 1;
      absd = mxGetPr(ABSD_IN);
      if(m != mxGetM(ABSD_IN) * mxGetN(ABSD_IN))
        mexErrMsgTxt("absd size mismatch");
    }
  }
/* ------------------------------------------------------------
   Create sparse output matrix L(m x m).
   ------------------------------------------------------------ */
  L_OUT = mxCreateSparse(m,m, L.jc[m],mxREAL);
  L.ir  = mxGetIr(L_OUT);
  L.pr  = mxGetPr(L_OUT);
  memcpy(mxGetJc(L_OUT), L.jc, (m+1) * sizeof(int));
  memcpy(L.ir, LINir, L.jc[m] * sizeof(int));
/* ------------------------------------------------------------
   Create ouput vector d(m).
   ------------------------------------------------------------ */
  D_OUT = mxCreateDoubleMatrix(m,1,mxREAL);
  d     = mxGetPr(D_OUT);
/* ------------------------------------------------------------
   Compute required sizes of working arrays:
   iwsiz = 2*(m + nsuper).
   fwsiz = tmpsiz.
   ------------------------------------------------------------ */
  iwsiz = MAX(2*(m+nsuper), 1);
  fwsiz = MAX(tmpsiz, 1);
/* ------------------------------------------------------------
   Allocate working arrays:
   integer: perm(m), snode(m), xsuper(nsuper+1),
      iwork(iwsiz), xlindx(m+1), skip(m),
   double: orgd(m), fwork(fwsiz).
   ------------------------------------------------------------ */
  m1 = MAX(m,1);                  /* avoid alloc to 0 */
  perm      = (int *) mxCalloc(m1,sizeof(int)); 
  snode     = (int *) mxCalloc(m1,sizeof(int)); 
  xsuper    = (int *) mxCalloc(nsuper+1,sizeof(int));
  iwork     = (int *) mxCalloc(iwsiz,sizeof(int));
  xlindx    = (int *) mxCalloc(m+1,sizeof(int));
  skip      = (int *) mxCalloc(m1, sizeof(int));
  orgd    = (double *) mxCalloc(m1,sizeof(double)); 
  fwork   = (double *) mxCalloc(fwsiz,sizeof(double)); 
/* ------------------------------------------------------------
   Convert PERM, XSUPER to integer and C-Style
   ------------------------------------------------------------ */
  for(i = 0; i < m; i++){
    j = permPr[i];
    perm[i] = --j;
  }
  for(i = 0; i <= nsuper; i++){
    j =  xsuperPr[i];
    xsuper[i] = --j;
  }
/* ------------------------------------------------------------
   Let L = tril(P(PERM,PERM)), uses orgd(m) as temp working storage.
   ------------------------------------------------------------ */
  permuteP(L.jc,L.ir,L.pr, Pjc,Pir,Ppr, perm, orgd, m);
/* ------------------------------------------------------------
   If no orgd has been supplied, take orgd = diag(L on input)
   Otherwise, let orgd = absd(perm).
   ------------------------------------------------------------ */
  if(useAbsd)
    for(j = 0; j < m; j++)
      orgd[j] = absd[perm[j]];
  else
    for(j = 0; j < m; j++)
      orgd[j] = L.pr[L.jc[j]];
/* ------------------------------------------------------------
   Create "snode" and "xlindx"; change L.ir to the compact subscript
   array (with xlindx), and do BLOCK SPARSE CHOLESKY.
   ------------------------------------------------------------ */
  nskip = spchol(m, nsuper, xsuper, snode, xlindx,
                 L.ir, orgd, L.jc, L.pr, d, perm, abstol,
                 canceltol, maxu, skip, &nadd, iwsiz, iwork, fwsiz, fwork);
  if(nskip < 0)
    mexErrMsgTxt("Insufficient workspace in pblkchol");
/* ------------------------------------------------------------
   Copy original row-indices from LINir to L.ir.
   ------------------------------------------------------------ */
  memcpy(L.ir, LINir, L.jc[m] * sizeof(int));
/* ------------------------------------------------------------
   Create output matrices skip = sparse([],[],[],m,1,nskip),
   diagadd = sparse([],[],[],m,1,nadd),
   ------------------------------------------------------------ */
  SKIP_OUT = mxCreateSparse(m,1, MAX(1,nskip),mxREAL);
  memcpy(mxGetIr(SKIP_OUT), skip, nskip * sizeof(int));
  skipJc = mxGetJc(SKIP_OUT);
  skipJc[0] = 0; skipJc[1] = nskip;
  skipPr   = mxGetPr(SKIP_OUT);
/* ------------------------------------------------------------
   useDelay = 1 then L(:,i) is i-th column before ith pivot; useful
     for pivot-delaying strategy. (Fwslv(L, L(:,i)) still required.)
   ------------------------------------------------------------ */
  if(useDelay == 1)
    for(j = 0; j < nskip; j++)
      skipPr[j] = 1.0;
  else
    for(j = 0; j < nskip; j++){
      i = skip[j];
      skipPr[j] = L.pr[L.jc[i]];             /* Set skipped l(:,i)=ei. */
      L.pr[L.jc[i]] = 1.0;
      fzeros(L.pr+L.jc[i]+1,L.jc[i+1]-L.jc[i]-1);
    }
  DIAGADD_OUT = mxCreateSparse(m,1, MAX(1,nadd),mxREAL);
  memcpy(mxGetIr(DIAGADD_OUT), iwork, nadd * sizeof(int));
  skipJc = mxGetJc(DIAGADD_OUT);
  skipJc[0] = 0; skipJc[1] = nadd;
  skipPr   = mxGetPr(DIAGADD_OUT);
  for(j = 0; j < nadd; j++)
    skipPr[j] = orgd[iwork[j]];
/* ------------------------------------------------------------
   Release working arrays.
   ------------------------------------------------------------ */
  mxFree(fwork);
  mxFree(orgd);
  mxFree(skip);
  mxFree(xlindx);
  mxFree(iwork);
  mxFree(xsuper);
  mxFree(snode);
  mxFree(perm);
/* ------------------------------------------------------------
   Copy requested output parameters (at least 1), release others.
   ------------------------------------------------------------ */
  i = MAX(nlhs, 1);
  memcpy(plhs,myplhs, i * sizeof(mxArray *));
  for(; i < NPAROUT; i++)
    mxDestroyArray(myplhs[i]);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[]) {
    double *Pw_d, *pr_x, *Pw_z, *Pz_d;
    double beta;
    int *ir_x, prev_ir_x,*ir_Pw_d, *jc_x, *jc_Pw_d, *bad_index;
    int element_count, nTopics;
    int nn,ii,jj;
    unsigned int nWords, nDocs, indc, indx, indy, next_indx;
    bool temperedEM;
    
    /* sanity check of the parameters */
    if ((!(mxIsSparse(prhs[0])))
    ||(!((nrhs==3)||(nrhs==4)))) {
        printf("usage : C = mex_Estep(X,Pw_z,Pz_d)\n");
        printf("   or : C = mex_Estep(X,Pw_z,Pz_d,beta)\n");
        printf(" computes the normalization oonstant 'C' only for those values\n");
        printf(" which are needed (nonzeros of X). If no 'beta' is given this function\n");
        printf(" does plain EM, TEM otherwise\n");
        return;
    }
    
    nWords = mxGetM(prhs[0]);
    nDocs  = mxGetN(prhs[0]);
    nTopics = mxGetM(prhs[2]);
    jc_x = mxGetJc(prhs[0]);
    ir_x = mxGetIr(prhs[0]);
    pr_x = mxGetPr(prhs[0]);
    
    
    Pw_z = mxGetPr(prhs[1]);
    Pz_d = mxGetPr(prhs[2]);
    
    if (nrhs == 4)
        beta = *mxGetPr(prhs[3]);
    else
        beta = 1;
    
    temperedEM = beta!=1;
    
    /* sanity check of the parameters */
    if ((! ( mxGetM(prhs[1]) == nWords))|| (!(mxGetN(prhs[1]) == nTopics) ) ){
        printf("Dimensions mismatch of Pw_z, should be of size %d x %d\n",nWords,nTopics);
        return;
    }
    if ((!(mxGetM(prhs[2])==nTopics))||(!(mxGetN(prhs[2])==nDocs))){
        printf("Dimensions mismatch of Pz_d, should be of size %d x %d\n",nTopics,nDocs);
        return;
    }
    
    /* create sparse output matrix */
    plhs[0] = mxCreateSparse(nWords, nDocs, numnonzeros(prhs[0]), mxREAL);
    jc_Pw_d = mxGetJc(plhs[0]);
    ir_Pw_d = mxGetIr(plhs[0]);
    Pw_d = mxGetPr(plhs[0]);
    
    element_count = 0;
    jc_Pw_d[0] = 0;
    indx = 0;
    
    
    if (!(temperedEM)){ /* plain EM */
        for (indc = 0; indc < 10; indc++) {
            jc_Pw_d[indc+1] = jc_Pw_d[indc];
            next_indx = jc_x[indc]; /* next column index */
            while (indx < next_indx) { /* while nonzero entries in the matrix */
                
                jc_Pw_d[indc]++; /* point to next index */
                
                /* copy element of sparse matrix */
                ir_Pw_d[element_count] = ir_x[indx];
                
                ii = indc;
                jj = ir_Pw_d[element_count];
                
                Pw_d[element_count] = 0;
                
                /* sum over all topics */
                for (nn=0;nn<nTopics;nn++)
                    Pw_d[element_count] += (Pz_d[ii*nTopics+nn] * Pw_z[jj+nWords*nn]);
                
				printf("%d %d %d %d %f\n",jc_x[indc], ir_x[indx], jc_Pw_d[indc], ir_Pw_d[element_count],Pw_d[element_count]);

                indx++;
                element_count++;
				
            }
        }

    }
    else { /* tempered EM - see comments in plain EM */
        for (indc = 0; indc < nDocs; indc++) {
            jc_Pw_d[indc+1] = jc_Pw_d[indc];
            next_indx = jc_x[indc+1];
            while (indx < next_indx) {
                jc_Pw_d[indc+1]++;
                ir_Pw_d[element_count] = ir_x[indx];
                ii = indc;
                jj = ir_Pw_d[element_count];
                Pw_d[element_count] = 0;
                for (nn=0;nn<nTopics;nn++)
                    Pw_d[element_count] += pow(Pz_d[ii*nTopics+nn] * Pw_z[jj+nWords*nn],beta);
                indx++;
                element_count++;
            }
        }
    }
    
}
void mexFunction(
    int nargout,
    mxArray *out[],
    int nargin,
    const mxArray *in[]
)
{
    /* declare variables */
    int nr, nc, np, total;
    int i, j, k, ix, iy, jx, jy;
	int *ir, *jc;
	int squareDistance;
    /* unsigned long *pi, *pj; */
unsigned int *pi, *pj;
	double *w;

	double temp,a1,a2,wij;
	double rMin;
	double sigmaX, sigmaIntensite,valeurMinW;
	double *image;
    
    /* check argument */
    if (nargin<7) {
        mexErrMsgTxt("Four input arguments required");
    }
    if (nargout>1) {
        mexErrMsgTxt("Too many output arguments");
    }

    /* get edgel information */
	nr = mxGetM(in[0]);
	nc = mxGetN(in[0]);
    	np = nr * nc;

    image = mxGetPr(in[0]);


    
    /* get new index pair */
    if (!mxIsUint32(in[1]) | !mxIsUint32(in[2])) {
        mexErrMsgTxt("Index pair shall be of type UINT32");
    }
    if (mxGetM(in[2]) * mxGetN(in[2]) != np + 1) {
        mexErrMsgTxt("Wrong index representation");
    }
    pi = mxGetData(in[1]);
    pj = mxGetData(in[2]);    

    /* create output */
    out[0] = mxCreateSparse(np,np,pj[np],mxREAL);
    if (out[0]==NULL) {
	    mexErrMsgTxt("Not enough memory for the output matrix");
	}
	w = mxGetPr(out[0]);
	ir = mxGetIr(out[0]);
	jc = mxGetJc(out[0]);
	
	rMin = mxGetScalar(in[3]);
	sigmaX = mxGetScalar(in[4]);
	sigmaIntensite= mxGetScalar(in[5]);
	valeurMinW = mxGetScalar(in[6]);

	a1 = 1.0/ (sigmaX*sigmaX);
	a2 = 1.0 / (sigmaIntensite*sigmaIntensite );

    /* computation */ 
    total = 0;
    for (j=0; j<np; j++) {            

        jc[j] = total;
        jx = j / nr; /* col */
        jy = j % nr; /* row */
        
        for (k=pj[j]; k<pj[j+1]; k++) {
        
            i = pi[k];
          
            if (i==j) {
                wij= 1; /*voir*/
            
            } else {        
                ix = i / nr; 
                iy = i % nr;

		squareDistance = (ix-jx)*(ix-jx)+(iy-jy)*(iy-jy);
		
	           temp = image[i]-image[j];
		   wij = exp(- squareDistance * a1 - temp*temp * a2 );
		   /*if(wij < valeurMinW)
		      wij = -0.1;*/
            }
		ir[total] = i;

		w[total] = wij;
		total = total + 1;
			
	  } /* i */

    } /* j */
        
    jc[np] = total;
}
Beispiel #12
0
void mexFunction
(
    int nlhs,
    mxArray *plhs [],
    int nrhs,
    const mxArray *prhs []
)
{
    /* Compressed column form of L, x and b */
    mwIndex *Lp, *Ap, *Ai, *Up, *pp, *pi ;
    Int *Li, *Ui;
    mwIndex *Lp1, *Li1, *Up1, *Ui1 ;
    double *Lx, *Ax, *Ux, *px ;
    double *Lx1, *Ux1 ;
    double *t1, *t2 ;

    mwIndex anrow ;
    mwIndex ancol ;
    mwIndex lnnz ;                       
    mwIndex unnz ;                       
    mwIndex i ;
    mwIndex j ;
    mwIndex app_xnnz ;
    mwIndex memsize ;
    mwIndex result ;

    mwIndex *ws;  /* workspace */
    double *X ;     /* space to scatter x */
    mwIndex *pinv ; /* column permutation inverse */

    if ( nlhs != 3 || nrhs < 3 )
    {
        mexErrMsgTxt (" Incorrect number of arguments to sproductmex \n") ;
    }

    Ap = mxGetJc(prhs[0]) ;
    Ai = mxGetIr(prhs[0]) ;
    Ax = mxGetPr(prhs[0]) ;

    anrow = mxGetM (prhs[0]) ;
    ancol = mxGetN (prhs[0]) ;

    t1 = mxGetPr (prhs[1]) ;
    t2 = mxGetPr (prhs[2]) ;

    lnnz = (mwIndex)*t1 ;
    unnz = (mwIndex)*t2 ;
    /*printf("lnnz=%d unnz=%d\n", lnnz, unnz);*/

    /* O(n) initialization */
    ws = (mwIndex *) mxCalloc ( (ancol)+(4*anrow), sizeof(mwIndex)) ;
    X = (double *) mxCalloc ( 2*anrow, sizeof(double)) ;
    pinv = (mwIndex *) mxCalloc ( ancol, sizeof(mwIndex)) ;

    Lp = (mwIndex *) mxCalloc ( ancol+1, sizeof(mwIndex)) ;
    Li = (mwIndex *) mxCalloc ( lnnz, sizeof(Int)) ;
    Lx = (double *) mxCalloc ( lnnz, sizeof(double)) ;

    Up = (mwIndex *) mxCalloc ( ancol+1, sizeof(mwIndex)) ;
    Ui = (mwIndex *) mxCalloc ( unnz, sizeof(Int)) ;
    Ux = (double *) mxCalloc ( unnz, sizeof(double)) ;

    PRINT(("Calling basker \n"));
    result = basker_basker_l(Ap, Ai, Ax, anrow, ancol, ws , X, 
                Lp, &Li, &Lx, Up, &Ui, &Ux, &lnnz, &unnz, pinv) ;
    PRINT(("Back in mex function %d \n",Lp[ancol]));
    
    if (result)
    {
        mxFree (X) ;
        mxFree (ws) ;
        mxFree (Lp) ;
        mxFree (Li) ;
        mxFree (Lx) ;
        mxFree (Up) ;
        mxFree (Ui) ;
        mxFree (Ux) ;
        mexErrMsgTxt (" basker failed \n") ;
    }

    plhs[0] = mxCreateSparse (anrow, ancol, Lp[ancol], mxREAL) ;
    Lp1 = mxGetJc (plhs[0]) ;
    Li1 = mxGetIr (plhs[0]) ;
    Lx1 = mxGetPr (plhs[0]) ;

    plhs[1] = mxCreateSparse (anrow, ancol, Up[ancol], mxREAL) ;
    Up1 = mxGetJc (plhs[1]) ;
    Ui1 = mxGetIr (plhs[1]) ;
    Ux1 = mxGetPr (plhs[1]) ;

    plhs[2] = mxCreateSparse (ancol, ancol, ancol, mxREAL) ;
    pp = mxGetJc (plhs[2]) ;
    pi = mxGetIr (plhs[2]) ;
    px = mxGetPr (plhs[2]) ;

    Lp1[0] = Lp[0];
    for ( i = 0 ; i < ancol ; i++)
    {
        Lp1[i+1] = Lp[i+1];
        for ( j = Lp[i] ; j < Lp[i+1] ; j++ )
        {
            Li1[j] = Li[j];
            Lx1[j] = Lx[j];
        }
    } 

    Up1[0] = Up[0];
    for ( i = 0 ; i < ancol ; i++)
    {
        Up1[i+1] = Up[i+1];
        for ( j = Up[i] ; j < Up[i+1] ; j++ )
        {
            Ui1[j] = Ui[j];
            Ux1[j] = Ux[j];
        }
    } 

    for ( i = 0 ; i < ancol ; i++)
    {
      pp[i] = i ;
      pi[ pinv[i] ] = i ;
      px[i] = 1 ;
    }  
    pp[anrow] = ancol ;

    /*for ( i = 0 ; i < ancol ; i++)
        printf("pinv[%d] = %d\n", i, pinv[i]);*/

    for ( i = 0 ; i < ancol ; i++)
    {
        for ( j = Lp1[i] ; j < Lp1[i+1] ; j++ )
        {
            Li1[j] = pinv[Li1[j]] ;
        }
    }


    mxFree (X) ;
    mxFree (ws) ;
    mxFree (pinv) ;
    mxFree (Lp) ;
    mxFree (Li) ;
    mxFree (Lx) ;
    mxFree (Up) ;
    mxFree (Ui) ;
    mxFree (Ux) ;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
                 const mxArray *prhs[])
{
  double *srww_old, *srww_new,*srwp_old,*srdp_new, *probs, *DS_NEW, *WS_NEW, *WS_OLD, *SI_NEW, *WC_OLD, *PROBA, *C, *Z;
  double ALPHA,BETA, GAMMA0, GAMMA1, DELTA;
  int *irww_old, *irww_new, *jcww_old, *jcww_new, *irwp_old, *jcwp_old, *irdp_new, *jcdp_new;
  int *z,*d,*w, *s, *c, *sc, *order, *wp_old, *dp_new, *dp_new2, *ztot_old, *wtot_new, *wc_new;
  int W_NEW, W_OLD,T_NEW, T_OLD,D_NEW,NITER,SEED,OUTPUT, nzmax_new, nzmax_old, nzmaxdp_new, ntokens_new, ntokens_old;
  int i,j,cc,nt,n,wi,wipre,di, startindex, endindex, currentrow, count;
  int NSAMPLE, updatecols;
  
  /* Check for proper number of arguments. */
  if (nrhs != 19) {
    mexErrMsgTxt("19 input arguments required");
  } else if (nlhs != 4) {
    mexErrMsgTxt("4 output arguments required");
  }

// Syntax                                       0       1          2       3       4   5    6         7      8       9      10        11      12      13      14       15       16       17     18 
//   [ DP_NEW,C,Z,PROB ] = NewDocumentsLDACOL( WS_NEW , DS_NEW , SI_NEW , WW_NEW , T , N , NSAMPLE , ALPHA , BETA , GAMMA0, GAMMA1 , DELTA , SEED , OUTPUT , WW_OLD , WP_OLD , WC_OLD , WS_OLD, UPDATECOLS );
  
  
  /* process the input arguments */
  if (mxIsDouble( prhs[ 0 ] ) != 1)
      mexErrMsgTxt("WS_NEW must be double precision"); 
  WS_NEW = mxGetPr(prhs[0]);
  ntokens_new = mxGetM( prhs[ 0 ] ) * mxGetN( prhs[ 0 ] );
  
  if (mxIsDouble( prhs[ 1 ] ) != 1)
      mexErrMsgTxt("DS_NEW must be double precision"); 
  DS_NEW = mxGetPr(prhs[1]);
  
  if (mxIsDouble( prhs[ 2 ] ) != 1)
      mexErrMsgTxt("SI_NEW must be double precision");
  SI_NEW = mxGetPr(prhs[2]);
  
  if ((mxIsSparse( prhs[ 3 ] ) != 1) || (mxIsDouble( prhs[ 3 ] ) != 1))
      mexErrMsgTxt("WW_NEW collocation matrix must be a sparse double precision matrix");

  /* dealing with sparse array WW_NEW */
  srww_new  = mxGetPr(prhs[3]);
  irww_new  = mxGetIr(prhs[3]);
  jcww_new  = mxGetJc(prhs[3]);
  nzmax_new = mxGetNzmax(prhs[3]);  
  W_NEW     = mxGetM( prhs[3] );
  
  if (mxGetN( prhs[3] ) != W_NEW) mexErrMsgTxt("WW_NEW matrix should be square");
  
  D_NEW    = 0;
  for (i=0; i<ntokens_new; i++) {
      if (DS_NEW[ i ] > D_NEW) D_NEW = (int) DS_NEW[ i ];
      if (WS_NEW[ i ] > W_NEW) mexErrMsgTxt("Some word tokens in WS_NEW stream exceed number of word types in WW_NEW matrix");
  }
   
  T_NEW    = (int) mxGetScalar(prhs[4]);
  if (T_NEW<=0) mexErrMsgTxt("Number of topics must be greater than zero");
  
  NITER    = (int) mxGetScalar(prhs[5]);
  if (NITER<0) mexErrMsgTxt("Number of iterations must be greater than zero");
  
  NSAMPLE    = (int) mxGetScalar(prhs[6]);
  if (NSAMPLE<0) mexErrMsgTxt("Number of samples must be greater than zero");
  
  ALPHA = (double) mxGetScalar(prhs[7]);
  if (ALPHA<=0) mexErrMsgTxt("ALPHA must be greater than zero");
  
  BETA = (double) mxGetScalar(prhs[8]);
  if (BETA<=0) mexErrMsgTxt("BETA must be greater than zero");
  
  GAMMA0 = (double) mxGetScalar(prhs[9]);
  if (GAMMA0<=0) mexErrMsgTxt("GAMMA0 must be greater than zero");
  
  GAMMA1 = (double) mxGetScalar(prhs[10]);
  if (GAMMA1<=0) mexErrMsgTxt("GAMMA1 must be greater than zero");
  
  DELTA = (double) mxGetScalar(prhs[11]);
  if (DELTA<=0) mexErrMsgTxt("DELTA must be greater than zero");
  
  SEED = (int) mxGetScalar(prhs[12]);
  // set the seed of the random number generator
  
  OUTPUT = (int) mxGetScalar(prhs[13]);
  
  // dealing with sparse array WW_OLD 
  if ((mxIsSparse( prhs[ 14 ] ) != 1) || (mxIsDouble( prhs[ 14 ] ) != 1))
      mexErrMsgTxt("WW_OLD collocation frequency matrix must be a sparse double precision matrix");  
  srww_old  = mxGetPr(prhs[14]);
  irww_old  = mxGetIr(prhs[14]);
  jcww_old  = mxGetJc(prhs[14]);
  nzmax_old = mxGetNzmax(prhs[14]);
  W_OLD     = mxGetM( prhs[14] );
  if (W_OLD != W_NEW) mexErrMsgTxt("WW_OLD and WW_NEW matrices have different dimensions");
  if (mxGetN( prhs[14] ) != W_NEW) mexErrMsgTxt("WW_OLD matrix should be square");
  
  // dealing with sparse array WP_OLD 
  if ((mxIsSparse( prhs[ 15 ] ) != 1) || (mxIsDouble( prhs[ 15 ] ) != 1))
      mexErrMsgTxt("WP_OLD topic-word matrix must be a sparse double precision matrix");  
  srwp_old  = mxGetPr(prhs[15]);
  irwp_old  = mxGetIr(prhs[15]);
  jcwp_old  = mxGetJc(prhs[15]);
  if (mxGetM( prhs[15] ) != W_NEW) mexErrMsgTxt("Number of words in WP_OLD matrix does not match WW_NEW number of words");
  if (mxGetN( prhs[15] ) != T_NEW) mexErrMsgTxt("Number of topics in WP_OLD matrix does not match given number of topics");
 
  WC_OLD = mxGetPr( prhs[ 16 ]);
  if ((mxGetM( prhs[16] ) * mxGetN( prhs[16] )) != W_NEW ) mexErrMsgTxt("Number of words in WC_OLD matrix does not match WW_NEW number of words");   
  
  if (mxIsDouble( prhs[ 17 ] ) != 1) mexErrMsgTxt("WS_OLD must be double precision"); 
  WS_OLD = mxGetPr(prhs[ 17 ]);
  ntokens_old = mxGetM( prhs[ 17 ] ) * mxGetN( prhs[ 17 ] );
  
  updatecols = (int) mxGetScalar(prhs[18]);
  
  // seeding
  seedMT( 1 + SEED * 2 ); // seeding only works on uneven numbers
  
  /* allocate memory */
  z  = (int *) mxCalloc( ntokens_new , sizeof( int ));
  d  = (int *) mxCalloc( ntokens_new , sizeof( int ));
  w  = (int *) mxCalloc( ntokens_new , sizeof( int ));
  s  = (int *) mxCalloc( ntokens_new , sizeof( int ));
  c  = (int *) mxCalloc( ntokens_new , sizeof( int ));
  sc  = (int *) mxCalloc( ntokens_new , sizeof( int ));
 
  for (i=0; i<ntokens_new; i++) w[ i ] = (int) (WS_NEW[ i ] - 1); // Matlab indexing not zero based
  for (i=0; i<ntokens_new; i++) d[ i ] = (int) (DS_NEW[ i ] - 1); // Matlab indexing not zero based
  for (i=0; i<ntokens_new; i++) s[ i ] = (int) SI_NEW[ i ];
  
  order   = (int *) mxCalloc( ntokens_new , sizeof( int ));
  wp_old  = (int *) mxCalloc( T_NEW*W_NEW , sizeof( int ));
  dp_new  = (int *) mxCalloc( T_NEW*D_NEW , sizeof( int ));
  dp_new2  = (int *) mxCalloc( T_NEW*D_NEW , sizeof( int ));
  wc_new  = (int *) mxCalloc( W_NEW , sizeof( int ));
  ztot_old  = (int *) mxCalloc( T_NEW , sizeof( int ));
  wtot_new  = (int *) mxCalloc( W_NEW , sizeof( int ));
  probs  = (double *) mxCalloc( T_NEW , sizeof( double ));
  
  // FILL IN WTOT_NEW with OLD COUNTS
  for (i=0; i<ntokens_old; i++) {
     j = (int) WS_OLD[ i ] - 1;
     wtot_new[ j ]++; // calculate number of words of each type
  }
  
  // FILL IN WTOT_NEW with NEW COUNTS
  if (updatecols==1)
  for (i=0; i<ntokens_new; i++) {
     j = w[ i ];
     wtot_new[ j ]++; // calculate number of words of each type
  }
  
  // FILL IN WC WITH COUNTS FROM OLD SET
  for (i=0; i<W_NEW; i++) {
     wc_new[ i ] = (int) WC_OLD[ i ];   
  }
  
  // FILL IN wp_old and ztot_old
  for (j=0; j<T_NEW; j++) {
      startindex = *( jcwp_old + j );
      endindex   = *( jcwp_old + j + 1 ) - 1;
      
      for (i=startindex; i<=endindex; i++) {
          currentrow = *( irwp_old + i );
          count      = (int) *( srwp_old + i );
          wp_old[ j + currentrow*T_NEW ] = count;
          ztot_old[ j ] += count;
      }    
  }
  
  // FILL IN SC COUNTS 
  for (i=1; i<ntokens_new; i++) // start with second item
  {
      wi = w[ i ]; // word index
      
      count = 0;
      
      // what is the previous word?
      wipre = w[ i-1 ];
      
      // calculate how many times the current word follows the previous word IN THE OLD DOCUMENT SET    
      startindex = *( jcww_old + wipre ); // look up start and end index for column wipre in WW_OLD matrix
      endindex   = *( jcww_old + wipre + 1 ) - 1;   
      for (j=startindex; j<=endindex; j++) {
          currentrow = *( irww_old + j );
          if (currentrow == wi) count = (int) *( srww_old + j );
      }
      
      // calculate how many times the current word follows the previous word IN THE NEW DOCUMENT SET
      if (updatecols==1) {
          startindex = *( jcww_new + wipre ); // look up start and end index for column wipre in WW_NEW matrix
          endindex   = *( jcww_new + wipre + 1 ) - 1;
          for (j=startindex; j<=endindex; j++) {
              currentrow = *( irww_new + j );
              if (currentrow == wi) count += (int) *( srww_new + j ); // add the count to what was there previously !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
          }
      }
      
      sc[ i ] = count;
  }
  
  
  
  /*for (i=0; i<10; i++) {
     mexPrintf( "i=%4d w[i]=%3d d[i]=%d z[i]=%d s[i]=%d\n" , i , w[i] , d[i] , z[i] , s[i] );    
  }*/
  
  if (OUTPUT==2) {
      mexPrintf( "Running NEW DOCUMENT LDA COL Gibbs Sampler Version 1.0\n" );
      mexPrintf( "Arguments:\n" );
      mexPrintf( "\tNumber of words      W = %d\n" , W_NEW );
      mexPrintf( "\tNumber of docs       D = %d\n" , D_NEW );
      mexPrintf( "\tNumber of topics     T = %d\n" , T_NEW );
      mexPrintf( "\tNumber of iterations N = %d\n" , NITER );
      mexPrintf( "\tNumber of samples    S = %d\n" , NSAMPLE );
      mexPrintf( "\tHyperparameter   ALPHA = %4.4f\n" , ALPHA );
      mexPrintf( "\tHyperparameter    BETA = %4.4f\n" , BETA );
      mexPrintf( "\tHyperparameter  GAMMA0 = %4.4f\n" , GAMMA0 );
      mexPrintf( "\tHyperparameter  GAMMA1 = %4.4f\n" , GAMMA1 );
      mexPrintf( "\tHyperparameter  DELTA  = %4.4f\n" , DELTA );
      mexPrintf( "\tSeed number            = %d\n" , SEED );
      mexPrintf( "\tNumber of tokens       = %d\n" , ntokens_new );
      mexPrintf( "\tUpdating collocation counts with new documents? = %d\n" , updatecols );
  }
  
  /* ---------------------------------------------
     create the PROB vector
   -----------------------------------------------*/
  plhs[ 3 ] = mxCreateDoubleMatrix( ntokens_new , 1 , mxREAL );
  PROBA = mxGetPr( plhs[ 3 ] );
  
  /* run the model */  
  GibbsSamplerLDACOL( ALPHA, BETA, GAMMA0,GAMMA1,DELTA, W_NEW, T_NEW, D_NEW, NITER, NSAMPLE , OUTPUT, ntokens_new, z, d, w, s, c, sc, wp_old, dp_new, dp_new2, ztot_old, wtot_new, order, probs, wc_new , PROBA , updatecols );
  

  /* ---------------------------------------------
   convert the full DP matrix into a sparse matrix 
   -----------------------------------------------*/
  nzmaxdp_new = 0;
  for (i=0; i<D_NEW; i++) {
      for (j=0; j<T_NEW; j++)
          nzmaxdp_new += (int) ( *( dp_new2 + j + i*T_NEW )) > 0; // !!!!!!!!!!!!!!!!!! copy from dp_new2
  }
  
  if (OUTPUT==2) {
      mexPrintf( "Constructing sparse output matrix dp_new\n" );
      mexPrintf( "Number of nonzero entries for DP = %d\n" , nzmaxdp_new );
  }
  
  plhs[0] = mxCreateSparse( D_NEW,T_NEW,nzmaxdp_new,mxREAL);
  srdp_new  = mxGetPr(plhs[0]);
  irdp_new = mxGetIr(plhs[0]);
  jcdp_new = mxGetJc(plhs[0]);
  n = 0;
  for (j=0; j<T_NEW; j++) {
      *( jcdp_new + j ) = n;
      for (i=0; i<D_NEW; i++) {
          cc = (int) *( dp_new2 + i*T_NEW + j ); // !!!!!!!!!!!!!!!!!! copy from dp_new2
          if (cc >0) {
              *( srdp_new + n ) = cc;
              *( irdp_new + n ) = i;
              n++;
          }
      }
  } 
  *( jcdp_new + T_NEW ) = n;
  
  /* ---------------------------------------------
     create the C route vector
   -----------------------------------------------*/
  plhs[ 1 ] = mxCreateDoubleMatrix( ntokens_new , 1 , mxREAL );
  C = mxGetPr( plhs[ 1 ] );
  for (i=0; i<ntokens_new; i++) C[ i ] = (double) c[ i ];
  
  /* ---------------------------------------------
     create the topic assignment vector
   -----------------------------------------------*/
  plhs[ 2 ] = mxCreateDoubleMatrix( ntokens_new , 1 , mxREAL );
  Z = mxGetPr( plhs[ 2 ] );
  for (i=0; i<ntokens_new; i++) Z[ i ] = (double) z[ i ] + 1;

}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 
{
	mwIndex *ir, *jc;
	double *pr;
	char *filename, *pch, mystring[MAX];
	int buflen, d, i, D, W, NNZ;
	FILE *doc;

	/* Check for proper number of arguments. */
	if (nrhs != 1) {
		mexErrMsgTxt("Only one input argument required");
	} else if (nlhs != 1) {
		mexErrMsgTxt("Only one output argument required");
	}

	// open document file
	buflen = (int) (1 + (mxGetM(prhs[0])*mxGetN(prhs[0])));
	filename = (char *) mxCalloc(buflen, sizeof(char));
	mxGetString( prhs[0], filename, buflen);
	doc = fopen(filename, "r"); 
	if (doc == NULL) mexErrMsgTxt("Text file cannot be found.");

	// read the file header
	fgets(mystring , sizeof(mystring) , doc);
	if ((mystring != NULL) && (atoi(mystring) != 0)) {
		D = atoi(mystring);
		mexPrintf( "#Document: %d \n", D);
	} else {
		mexErrMsgTxt("File header error.");
	}
	
	fgets(mystring , sizeof(mystring) , doc);
	if ((mystring != NULL) && (atoi(mystring) != 0)){
		W = atoi(mystring);
		mexPrintf( "#Vocabulary: %d \n", W);
	} else {
		mexErrMsgTxt("File header error.");
	}
	
	fgets(mystring , sizeof(mystring) , doc);
	if ((mystring != NULL) && (atoi(mystring) != 0)){
		NNZ = atoi(mystring);
		mexPrintf( "#NNZ: %d \n", NNZ);
	} else {
		mexErrMsgTxt("File header error.");
	}

	// create W x D sparse matrix for data
	plhs[0] = mxCreateSparse(W, D, NNZ, mxREAL);
	pr  = (double *) mxGetPr(plhs[0]);
	ir = mxGetIr(plhs[0]);
	jc = mxGetJc(plhs[0]);

	// copy data to sparse matrix
	i = 0;
	jc[0] = 0;
	while (fgets(mystring , sizeof(mystring) , doc) != NULL) {	
		pch = strtok(mystring, " ");
		if ((pch != NULL) && (atoi(pch) != 0)) {
			d = atoi(pch);
			pch = strtok(NULL, " ");
			ir[i] = atoi(pch) - 1;
			pch = strtok(NULL, " ");
			pr[i] = atoi(pch);
			i++;
			jc[d] = i;
		}
	}
	fclose(doc);
}
Beispiel #15
0
/* ************************************************************
   PROCEDURE mexFunction - Entry for Matlab
      [perm,dz] = incorder(At,Ajc1,ifirst)   
   ************************************************************ */
void mexFunction(int nlhs, mxArray *plhs[],
   int nrhs, const mxArray *prhs[])
{
  mxArray *myplhs[NPAROUT];
  mwIndex i, m, firstPSD, lenud, iwsiz, lenfull, maxnnz;
  mwIndex *iwork, *Atjc1, *Ajc, *Air, *perm;
  const mwIndex *Atjc2, *Atir;
  double *permPr;
  const double *Ajc1Pr;
  char *cwork;
  jcir dz;
/* ------------------------------------------------------------
   Check for proper number of arguments
   ------------------------------------------------------------ */
  mxAssert(nrhs >= NPARINMIN, "incorder requires more input arguments.");
  mxAssert(nlhs <= NPAROUT, "incorder produces less output arguments.");
/* --------------------------------------------------
   GET STATISTICS:
   -------------------------------------------------- */
  if(nrhs == NPARIN){
    firstPSD = (mwIndex) mxGetScalar(IFIRST_IN);        /* double to mwIndex */
    mxAssert(firstPSD>0,"");
    --firstPSD;                               /* Fortran to C */
  }
  else
    firstPSD = 0;                           /* default: use all subscripts */
  lenfull = mxGetM(AT_IN);
  lenud = lenfull - firstPSD;
/* --------------------------------------------------
   Check size At, and get At.
   -------------------------------------------------- */
  mxAssert(mxIsSparse(AT_IN), "At must be a sparse matrix.");
  m = mxGetN(AT_IN);
  Atjc2 = mxGetJc(AT_IN)+1;        /* points to end of constraint */
  Atir = mxGetIr(AT_IN);           /* subscripts */
/* ------------------------------------------------------------
   ALLOCATE WORKING arrays:
   mwIndex Atjc1(m),  Ajc(1+lenud), Air(maxnnz), perm(m),
     iwork(iwsiz). iwsiz = MAX(lenud,1+m)
   char cwork(lenud).
   ------------------------------------------------------------ */
  Atjc1 = (mwIndex *) mxCalloc(MAX(m,1), sizeof(mwIndex));
  Ajc = (mwIndex *) mxCalloc(1+lenud, sizeof(mwIndex));
  perm = (mwIndex *) mxCalloc(MAX(1,m), sizeof(mwIndex));
  iwsiz = MAX(lenud, 1 + m);
  iwork = (mwIndex *) mxCalloc(iwsiz, sizeof(mwIndex)); /* iwork(iwsiz) */
  cwork = (char *) mxCalloc(MAX(1,lenud), sizeof(char));
/* ------------------------------------------------------------
   Get input AJc1
   ------------------------------------------------------------ */
  if(nrhs == NPARIN){
    Ajc1Pr = mxGetPr(AJC1_IN);
    mxAssert(mxGetM(AJC1_IN) * mxGetN(AJC1_IN) >= m, "Ajc1 size mismatch");
/* ------------------------------------------------------------
   Double to mwIndex: Atjc1
   ------------------------------------------------------------ */
    for(i = 0; i < m; i++)
      Atjc1[i] = (mwIndex) Ajc1Pr[i];           /* double to mwIndex */
/* ------------------------------------------------------------
   Let maxnnz = number of PSD nonzeros = sum(Atjc2-Atjc1)
   ------------------------------------------------------------ */
  maxnnz = 0;
  for(i = 0; i < m; i++)
    maxnnz += Atjc2[i] - Atjc1[i];
  }
  else{
    memcpy(Atjc1, mxGetJc(AT_IN), m * sizeof(mwIndex)); /* default column start */
    maxnnz = Atjc2[m-1];                    /* maxnnz = nnz(At) */
  }
/* ------------------------------------------------------------
   ALLOCATE WORKING array mwIndex Air(maxnnz)
   ------------------------------------------------------------ */
  Air = (mwIndex *) mxCalloc(MAX(1,maxnnz), sizeof(mwIndex));
/* ------------------------------------------------------------
   CREATE OUTPUT ARRAYS PERM(m) and DZ=sparse(lenfull,m,lenud).
   ------------------------------------------------------------ */
  PERM_OUT = mxCreateDoubleMatrix(m,(mwSize)1,mxREAL);
  permPr = mxGetPr(PERM_OUT);
  DZ_OUT = mxCreateSparse(lenfull,m,lenud,mxREAL);
  dz.jc = mxGetJc(DZ_OUT);
  dz.ir = mxGetIr(DZ_OUT);
/* ------------------------------------------------------------
   Let (Ajc,Air) := At(first:end,:)', the transpose of PSD-part.
   Uses iwork(lenud)
   ------------------------------------------------------------ */
  spPartTransp(Air,Ajc, Atir,Atjc1,Atjc2, firstPSD,lenfull, m,iwork);
/* ------------------------------------------------------------
   The main job: greedy order of columns of At
   ------------------------------------------------------------ */
  incorder(perm, dz.jc,dz.ir, Atjc1,Atjc2,Atir, Ajc,Air,
           m, firstPSD,lenud, iwork, cwork);    /* uses iwork(m+1) */
/* ------------------------------------------------------------
   REALLOC (shrink) dz to dz.jc[m] nonzeros.
   ------------------------------------------------------------ */
  mxAssert(dz.jc[m] <= lenud,"");
  maxnnz = MAX(1,dz.jc[m]);                     /* avoid realloc to 0 */
  if((dz.ir = (mwIndex *) mxRealloc(dz.ir, maxnnz * sizeof(mwIndex))) == NULL)
    mexErrMsgTxt("Memory allocation error");
  if((dz.pr = (double *) mxRealloc(mxGetPr(DZ_OUT), maxnnz*sizeof(double)))
     == NULL)
    mexErrMsgTxt("Memory allocation error");
  mxSetPr(DZ_OUT,dz.pr);
  mxSetIr(DZ_OUT,dz.ir);
  mxSetNzmax(DZ_OUT,maxnnz);
  for(i = 0; i < maxnnz; i++)
    dz.pr[i] = 1.0;
/* ------------------------------------------------------------
   Convert C-mwIndex to Fortran-double
   ------------------------------------------------------------ */
  for(i = 0; i < m; i++)
    permPr[i] = perm[i] + 1.0;
/* ------------------------------------------------------------
   Release working arrays
   ------------------------------------------------------------ */
  mxFree(cwork);
  mxFree(iwork);
  mxFree(perm);
  mxFree(Air);
  mxFree(Ajc);
  mxFree(Atjc1);
/* ------------------------------------------------------------
   Copy requested output parameters (at least 1), release others.
   ------------------------------------------------------------ */
  i = MAX(nlhs, 1);
  memcpy(plhs,myplhs, i * sizeof(mxArray *));
  for(; i < NPAROUT; i++)
    mxDestroyArray(myplhs[i]);
}
Beispiel #16
0
/* ************************************************************
   PROCEDURE mexFunction - Entry for Matlab
   y = bwblksolve(L,b, [y])
     y(L.fullperm) = L.L' \ b
   ************************************************************ */
void mexFunction(const int nlhs, mxArray *plhs[],
  const int nrhs, const mxArray *prhs[])
{
 const mxArray *L_FIELD;
 mwIndex m,n, j, k, nsuper, inz;
 double *y, *fwork;
 const double *permPr, *b, *xsuperPr;
 const mwIndex *yjc, *yir, *bjc, *bir;
 mwIndex *perm, *xsuper, *iwork, *snode;
 jcir L;
 char bissparse;
 /* ------------------------------------------------------------
    Check for proper number of arguments 
    ------------------------------------------------------------ */
 mxAssert(nrhs >= MINNPARIN, "fwblkslv requires more input arguments.");
 mxAssert(nlhs == 1, "fwblkslv generates only 1 output argument.");
 /* ------------------------------------------------------------
    Disassemble block Cholesky structure L
    ------------------------------------------------------------ */
 mxAssert(mxIsStruct(L_IN), "Parameter `L' should be a structure.");
 L_FIELD = mxGetField(L_IN,(mwIndex)0,"perm");                    /* L.perm */
 mxAssert( L_FIELD != NULL, "Missing field L.perm.");
 m = mxGetM(L_FIELD) * mxGetN(L_FIELD);
 permPr = mxGetPr(L_FIELD);
 L_FIELD = mxGetField(L_IN,(mwIndex)0,"L");         /* L.L */
 mxAssert( L_FIELD != NULL, "Missing field L.L.");
 mxAssert( m == mxGetM(L_FIELD) && m == mxGetN(L_FIELD), "Size L.L mismatch.");
 mxAssert(mxIsSparse(L_FIELD), "L.L should be sparse.");
 L.jc = mxGetJc(L_FIELD);
 L.ir = mxGetIr(L_FIELD);
 L.pr = mxGetPr(L_FIELD);
 L_FIELD = mxGetField(L_IN,(mwIndex)0,"xsuper");          /* L.xsuper */
 mxAssert( L_FIELD != NULL, "Missing field L.xsuper.");
 nsuper = mxGetM(L_FIELD) * mxGetN(L_FIELD) - 1;
 mxAssert( nsuper <= m, "Size L.xsuper mismatch.");
 xsuperPr = mxGetPr(L_FIELD);
 /* ------------------------------------------------------------
    Get rhs matrix b.
    If it is sparse, then we also need the sparsity structure of y.
    ------------------------------------------------------------ */
 b = mxGetPr(B_IN);
 mxAssert( mxGetM(B_IN) == m, "Size mismatch b.");
 n = mxGetN(B_IN);
 if( (bissparse = mxIsSparse(B_IN)) ){
   bjc = mxGetJc(B_IN);
   bir = mxGetIr(B_IN);
   mxAssert(nrhs >= NPARIN, "bwblkslv requires more inputs in case of sparse b.");
   mxAssert(mxGetM(Y_IN) == m && mxGetN(Y_IN) == n, "Size mismatch y.");
   mxAssert(mxIsSparse(Y_IN), "y should be sparse.");
 }
/* ------------------------------------------------------------
   Allocate output y. If bissparse, then Y_IN gives the sparsity structure.
   ------------------------------------------------------------ */
 if(!bissparse)
   Y_OUT = mxCreateDoubleMatrix(m, n, mxREAL);
 else{
   yjc = mxGetJc(Y_IN);
   yir = mxGetIr(Y_IN);
   Y_OUT = mxCreateSparse(m,n, yjc[n],mxREAL);
   memcpy(mxGetJc(Y_OUT), yjc, (n+1) * sizeof(mwIndex));
   memcpy(mxGetIr(Y_OUT), yir, yjc[n] * sizeof(mwIndex));
 }
 y = mxGetPr(Y_OUT);
 /* ------------------------------------------------------------
    Allocate working arrays
    ------------------------------------------------------------ */
 fwork = (double *) mxCalloc(m, sizeof(double));
 iwork = (mwIndex *) mxCalloc(2*m+nsuper+1, sizeof(mwIndex));
 perm = iwork;                   /* m */
 xsuper = iwork + m;             /*nsuper+1*/
 snode = xsuper + (nsuper+1);    /* m */
 /* ------------------------------------------------------------
    Convert real to integer array, and from Fortran to C style.
    ------------------------------------------------------------ */
 for(k = 0; k < m; k++)
   perm[k] = permPr[k] - 1;
 for(k = 0; k <= nsuper; k++)
   xsuper[k] = xsuperPr[k] - 1;
/* ------------------------------------------------------------
   In case of sparse b, we also create snode, which maps each subnode
   to the supernode containing it.
   ------------------------------------------------------------ */
 if(bissparse)
   for(j = 0, k = 0; k < nsuper; k++)
     while(j < xsuper[k+1])
       snode[j++] = k;
 /* ------------------------------------------------------------
    The actual job is done here: y(perm) = L'\b.
    ------------------------------------------------------------ */
 if(!bissparse)
   for(j = 0; j < n; j++){
     memcpy(fwork,b, m * sizeof(double));
     bwsolve(fwork,L.jc,L.ir,L.pr,xsuper,nsuper,y);  /* y(m) as work */
     for(k = 0; k < m; k++)            /* y(perm) = fwork */
       y[perm[k]] = fwork[k];
     y += m; b += m;
   }
 else{          /* sparse y,b: don't use perm */
   fzeros(fwork,m);
   for(j = 0; j < n; j++){
     inz = yjc[j];
     for(k = bjc[j]; k < bjc[j+1]; k++)            /* fwork = b */
       fwork[bir[k]] = b[k];
     selbwsolve(fwork,L.jc,L.ir,L.pr,xsuper,nsuper, snode,
                yir+inz,yjc[j+1]-inz);
     for(k = inz; k < yjc[j+1]; k++)
       y[k] = fwork[yir[k]];
     for(k = inz; k < yjc[j+1]; k++)            /* fwork = all-0 */
       fwork[yir[k]] = 0.0;
   }
 }
 /* ------------------------------------------------------------
    RELEASE WORKING ARRAYS.
    ------------------------------------------------------------ */
 mxFree(fwork);
 mxFree(iwork);
}
void mexFunction( int nlhs,   mxArray  *plhs[], 
                  int nrhs,   const mxArray  *prhs[] )

{    
     mxArray  *rhs[2]; 
     mxArray  *blk_cell_pr, *A_cell_pr;
     double   *A,  *B,  *blksize;
     mwIndex  *irA, *jcA, *irB, *jcB;
     mwIndex  *cumblksize, *blknnz;
     mwSize   iscellA, mblk, mA, nA, m1, n1, rowidx, colidx, isspA, isspB;

     mwIndex  subs[2];
     mwSize   nsubs=2; 
     mwSize   n, n2, k, nsub, index, numblk, NZmax;
     double   ir2=1/sqrt(2); 

/* CHECK FOR PROPER NUMBER OF ARGUMENTS */

     if (nrhs < 2){
         mexErrMsgTxt("mexsmat: requires at least 2 input arguments."); }
     else if (nlhs>1){ 
         mexErrMsgTxt("mexsmat: requires 1 output argument."); }

/* CHECK THE DIMENSIONS */

     iscellA = mxIsCell(prhs[1]); 
     if (iscellA) { mA = mxGetM(prhs[1]); nA = mxGetN(prhs[1]); }
     else         { mA = 1; nA = 1; }
     if (mxGetM(prhs[0]) != mA) {
         mexErrMsgTxt("mexsmat: blk and Avec not compatible"); }

/***** main body *****/ 
       
     if (nrhs > 3) {rowidx = (mwSize)*mxGetPr(prhs[3]); } else {rowidx = 1;}  
     if (rowidx > mA) {
         mexErrMsgTxt("mexsmat: rowidx exceeds size(Avec,1)."); }
     subs[0] = rowidx-1;  /* subtract 1 to adjust for Matlab index */
     subs[1] = 1;
     index = mxCalcSingleSubscript(prhs[0],nsubs,subs); 
     blk_cell_pr = mxGetCell(prhs[0],index);
     if (blk_cell_pr == NULL) { 
        mexErrMsgTxt("mexsmat: blk not properly specified"); }    
     numblk  = mxGetN(blk_cell_pr);            
     blksize = mxGetPr(blk_cell_pr);
     cumblksize = mxCalloc(numblk+1,sizeof(mwSize)); 
     blknnz = mxCalloc(numblk+1,sizeof(mwSize)); 
     cumblksize[0] = 0; blknnz[0] = 0; 
     n = 0;  n2 = 0; 
     for (k=0; k<numblk; ++k) {
          nsub = (mwSize) blksize[k];
          n  += nsub; 
          n2 += nsub*(nsub+1)/2;  
          cumblksize[k+1] = n; 
          blknnz[k+1] = n2;
     }
     /***** assign pomwSizeers *****/
     if (iscellA) { 
         subs[0] = rowidx-1; 
         subs[1] = 0;
         index = mxCalcSingleSubscript(prhs[1],nsubs,subs); 
         A_cell_pr = mxGetCell(prhs[1],index); 
         A  = mxGetPr(A_cell_pr); 
         m1 = mxGetM(A_cell_pr); 
         n1 = mxGetN(A_cell_pr);
         isspA = mxIsSparse(A_cell_pr);
         if (isspA) { irA = mxGetIr(A_cell_pr);
                      jcA = mxGetJc(A_cell_pr); } 
     } else { 
         A  = mxGetPr(prhs[1]); 
         m1 = mxGetM(prhs[1]); 
         n1 = mxGetN(prhs[1]); 
         isspA = mxIsSparse(prhs[1]); 
         if (isspA) {  irA = mxGetIr(prhs[1]); 
                       jcA = mxGetJc(prhs[1]); }
     }
     if (numblk > 1) 
        { isspB = 1; }
     else { 
        if (nrhs > 2) {isspB = (mwSize)*mxGetPr(prhs[2]);} else {isspB = isspA;} 
     }
     if (nrhs > 4) {colidx = (mwSize)*mxGetPr(prhs[4]) -1;} else {colidx = 0;} 
     if (colidx > n1) { 
         mexErrMsgTxt("mexsmat: colidx exceeds size(Avec,2)."); 
     }    
     /***** create return argument *****/
     if (isspB) {
	 if (isspA) { NZmax = jcA[colidx+1]-jcA[colidx]; } 
         else       { NZmax = blknnz[numblk]; } 
	 rhs[0] = mxCreateSparse(n,n,2*NZmax,mxREAL); 
	 B = mxGetPr(rhs[0]); irB = mxGetIr(rhs[0]); jcB = mxGetJc(rhs[0]);
     } else {
         plhs[0] = mxCreateDoubleMatrix(n,n,mxREAL); 
         B = mxGetPr(plhs[0]); 
     }
     /***** Do the computations in a subroutine *****/
     if (numblk == 1) { 
         smat1(n,ir2,A,irA,jcA,isspA,m1,colidx,B,irB,jcB,isspB);  }
     else { 
         smat2(n,numblk,cumblksize,blknnz,ir2,A,irA,jcA,isspA,m1,colidx,B,irB,jcB,isspB);  
     }
     if (isspB) {
        /*** if isspB, (actual B) = B+B' ****/ 
        mexCallMATLAB(1, &rhs[1], 1, &rhs[0], "transpose"); 
        mexCallMATLAB(1, &plhs[0],2, rhs, "+");  
        mxDestroyArray(*rhs); 
     }
 return;
 }
Beispiel #18
0
  m  = mxGetM(prhs[0]);
  n  = mxGetN(prhs[0]);
  pr = mxGetPr(prhs[0]);
  pi = mxGetPi(prhs[0]);
  cmplx = (pi == NULL ? 0 : 1);

  /* Allocate space for sparse matrix. 
   * NOTE:  Assume at most 20% of the data is sparse.  Use ceil
   * to cause it to round up. 
   */

  percent_sparse = 0.2;
  nzmax = (int)ceil((double)m*(double)n*percent_sparse);

  plhs[0] = mxCreateSparse(m,n,nzmax,cmplx);
  sr  = mxGetPr(plhs[0]);
  si  = mxGetPi(plhs[0]);
  irs = mxGetIr(plhs[0]);
  jcs = mxGetJc(plhs[0]);
    
  /* Copy nonzeros. */
  k = 0; 
  isfull = 0;
  for (j = 0; (j < n); j++) {
    int i;
    jcs[j] = k;
    for (i = 0; (i < m); i++) {
      if (IsNonZero(pr[i]) || (cmplx && IsNonZero(pi[i]))) {

        /* Check to see if non-zero element will fit in 
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
                 const mxArray *prhs[])
{
  double *srwp, *srdp, *srmp, *probs, *d, *w, *ZIN, *XIN, *Z, *X;
  double ALPHA,BETA, GAMMA;
  mwIndex *irwp, *jcwp, *irdp, *jcdp, *irmp, *jcmp;
  int *z,*x, *wp, *mp, *dp, *ztot, *mtot, *stot, *sp, *first, *second, *third;
  int W,T,S,SINPUT,D,NN,SEED,OUTPUT, nzmax, nzmaxwp,nzmaxmp, nzmaxdp, ntokens;
  int NWS,NDS,i,j,c,n,wi,di,i1,i2,i3,i4,S2,S3, startcond;
  
  // Syntax
  //   [ WP , DP , MP , Z , X ] = GibbsSamplerHMMLDA( WS , DS , T , S , N , ALPHA , BETA , GAMMA , SEED , OUTPUT , ZIN , XIN )

  /* Check for proper number of arguments. */
  if (nrhs < 10) {
    mexErrMsgTxt("At least 10 input arguments required");
  } else if (nlhs != 5) {
    mexErrMsgTxt("5 output arguments required");
  }
  
  startcond = 0;
  if (nrhs > 10) startcond = 1;
  
  if (sizeof( int ) != sizeof( mxINT32_CLASS ))
    mexErrMsgTxt("Problem with internal integer representation -- contact programmer" );
  
  w   = mxGetPr( prhs[0] );
  NWS = mxGetN( prhs[0] );
  
  d   = mxGetPr( prhs[1] );
  NDS = mxGetN( prhs[1] );
  
  if ((mxIsSparse( prhs[ 0 ] ) == 1) || (mxIsDouble( prhs[ 0 ] ) != 1) || (mxGetM( prhs[0]) != 1))
      mexErrMsgTxt("WS input stream must be a one-dimensional, non-sparse, double precision vector of word indices");
 
  if ((mxIsSparse( prhs[ 1 ] ) == 1) || (mxIsDouble( prhs[ 1 ] ) != 1) || (mxGetM( prhs[1]) != 1))
      mexErrMsgTxt("DS input stream must be a one-dimensional, non-sparse, double precision vector of document indices");
  
  if (NWS != NDS)
      mexErrMsgTxt("WS and DS input streams must have equal dimensions" );
    
  ntokens = NWS;
  
  T    = (int) mxGetScalar(prhs[2]);
  if (T<=0) mexErrMsgTxt("Number of topics must be greater than zero");
  
  SINPUT    = (int) mxGetScalar(prhs[3]);
  if (SINPUT<=0) mexErrMsgTxt("Number of syntactic states must be greater than zero");
  
  // need one syntactic state for sentence start 
  // need one syntactic state for topic state
  S = SINPUT + 2;
  
  NN    = (int) mxGetScalar(prhs[4]);
  if (NN<0) mexErrMsgTxt("Number of iterations must be greater than zero");
  
  ALPHA = (double) mxGetScalar(prhs[5]);
  if (ALPHA<=0) mexErrMsgTxt("ALPHA must be greater than zero");
  
  BETA = (double) mxGetScalar(prhs[6]);
  if (BETA<=0) mexErrMsgTxt("BETA must be greater than zero");
  
  GAMMA = (double) mxGetScalar(prhs[7]);
  if (GAMMA<=0) mexErrMsgTxt("GAMMA must be greater than zero");
  
  SEED = (int) mxGetScalar(prhs[8]);
  // set the seed of the random number generator
  
  OUTPUT = (int) mxGetScalar(prhs[9]);
  
  if (startcond==1) {
      if ((mxGetN( prhs[ 10 ] )*mxGetM( prhs[ 10 ] )) != ntokens) mexErrMsgTxt( "The ZIN vector should have have the same number of tokens as WS" );
      if ((mxGetN( prhs[ 11 ] )*mxGetM( prhs[ 11 ] )) != ntokens) mexErrMsgTxt( "The ZIN vector should have have the same number of tokens as WS" );
      
      ZIN = mxGetPr( prhs[ 10 ]);
      XIN = mxGetPr( prhs[ 11 ]);
  }
  
  // seeding
  seedMT( 1 + SEED * 2 ); // seeding only works on uneven numbers
  
  W       = 0;
  D       = 0;
  for (i=0; i<ntokens; i++) {
      // in Matlab WI=1...W word occurence, WI=0 sentence marker  
      wi = (int) w[ i ] - 1; // word indices are not zero based in Matlab
      di = (int) d[ i ] - 1; // document indices are not zero based in Matlab
      
      //mexPrintf( "i=%d wi=%d di=%d\n" , i , wi , di );
      
      if (wi > W) W = wi;
      if (di > D) D = di;
      
      if (wi < -1) mexErrMsgTxt("Unrecognized code in word stream (<-2)");
      if (di <  0) mexErrMsgTxt("Unrecognized code in document stream (<0)");
  }
  W = W + 1;
  D = D + 1;
 
  if ( wi != -1 )
       mexErrMsgTxt("Word stream should end with sentence marker");
  
  n = ntokens;
  //mexPrintf( "W=%d D=%d n=%d w[n-1]=%d\n" , W , D , n , (int) w[ n-1 ] );
  
  
  // allocate memory 
  z  = (int *) mxCalloc( ntokens , sizeof( int ));
  x  = (int *) mxCalloc( ntokens , sizeof( int ));
  
  if (startcond==1) {
     for (i=0; i<ntokens; i++) {
        z[ i ] = (int) ZIN[ i ] - 1;
        x[ i ] = (int) XIN[ i ] - 1;
     }
  }
  
  first   = (int *) mxCalloc( ntokens , sizeof( int ));
  second  = (int *) mxCalloc( ntokens , sizeof( int ));
  third   = (int *) mxCalloc( ntokens , sizeof( int ));
  mp  = (int *) mxCalloc( W*S , sizeof( int ));
  wp  = (int *) mxCalloc( T*W , sizeof( int ));
  dp  = (int *) mxCalloc( T*D , sizeof( int ));
  ztot  = (int *) mxCalloc( T , sizeof( int ));
  mtot  = (int *) mxCalloc( S , sizeof( int ));
  stot  = (int *) mxCalloc( S * S * S , sizeof( int ));
  sp    = (int *) mxCalloc( S * S * S * S , sizeof( int ));
  probs  = (double *) mxCalloc( T+S , sizeof( double ));
  
  
  //for (i=0; i<n; i++) mexPrintf( "i=%4d w[i]=%3d d[i]=%d\n" , i , w[i] , d[i] , z[i] );    
  
  if (OUTPUT==2) {
      mexPrintf( "Running HMM-LDA Gibbs Sampler Version 1.0\n" );
      if (startcond==1) mexPrintf( "Starting sampler from previous state\n" );
      mexPrintf( "Arguments:\n" );
      mexPrintf( "\tNumber of words            W = %d\n" , W );
      mexPrintf( "\tNumber of docs             D = %d\n" , D );
      mexPrintf( "\tNumber of topics           T = %d\n" , T );
      mexPrintf( "\tNumber of syntactic states S = %d\n" , S );
      mexPrintf( "\tNumber of iterations       N = %d\n" , NN );
      mexPrintf( "\tHyperparameter         ALPHA = %4.4f\n" , ALPHA );
      mexPrintf( "\tHyperparameter          BETA = %4.4f\n" , BETA );
      mexPrintf( "\tHyperparameter         GAMMA = %4.4f\n" , GAMMA );
      mexPrintf( "\tSeed number             SEED = %d\n" , SEED );
      mexPrintf( "Properties of WS stream\n" );
      mexPrintf( "\tNumber of tokens       NZ = %d\n" , ntokens );
      mexPrintf( "Internal Memory Allocation\n" );
      mexPrintf( "\tz,x,first,second,third indices combined = %d bytes\n" , 5 * sizeof( int) * ntokens );
      mexPrintf( "\twp   matrix = %d bytes\n" , sizeof( int ) * W * T  );
      mexPrintf( "\tmp   matrix = %d bytes\n" , sizeof( int ) * W * S  );
      mexPrintf( "\tdp   matrix = %d bytes\n" , sizeof( int ) * D * T  );
      mexPrintf( "\tstot matrix = %d bytes\n" , sizeof( int ) * S * S * S );
      mexPrintf( "\tsp   matrix = %d bytes\n" , sizeof( int ) * S * S * S * S );
      //mexPrintf( "Checking: sizeof(int)=%d sizeof(long)=%d sizeof(double)=%d\n" , sizeof(int) , sizeof(long) , sizeof(double));
  }
  
  // run the model 
  GibbsSamplerHMMLDA( ALPHA, BETA, GAMMA, W, T, S , D, NN, OUTPUT, n, z, x, d, w, wp, mp, dp, ztot, mtot, stot , sp , first,second,third, probs, startcond );
  
  // convert the full wp matrix into a sparse matrix 
  nzmaxwp = 0;
  for (i=0; i<W; i++) {
     for (j=0; j<T; j++)
         nzmaxwp += (int) ( *( wp + j + i*T )) > 0;
  }
  if (OUTPUT==2) mexPrintf( "Constructing sparse output matrix WP  nnz=%d\n" , nzmaxwp );

  plhs[0] = mxCreateSparse( W,T,nzmaxwp,mxREAL);
  srwp  = mxGetPr(plhs[0]);
  irwp = mxGetIr(plhs[0]);
  jcwp = mxGetJc(plhs[0]); 
  n = 0;
  for (j=0; j<T; j++) {
      *( jcwp + j ) = n;
      for (i=0; i<W; i++) {
         c = (int) *( wp + i*T + j );
         if (c >0) {
             *( srwp + n ) = c;
             *( irwp + n ) = i;
             n++;
         }
      }    
  }
  
  *( jcwp + T ) = n;    
   
  // processing DP as sparse output matrix 
  nzmaxdp = 0;
  for (i=0; i<D; i++) {
      for (j=0; j<T; j++)
          nzmaxdp += (int) ( *( dp + j + i*T )) > 0;
  }  
  if (OUTPUT==2) mexPrintf( "Constructing sparse output matrix DP  nnz=%d\n" , nzmaxdp );
  
  plhs[1] = mxCreateSparse( D,T,nzmaxdp,mxREAL);
  srdp  = mxGetPr(plhs[1]);
  irdp = mxGetIr(plhs[1]);
  jcdp = mxGetJc(plhs[1]);
  
  n = 0;
  for (j=0; j<T; j++) {
      *( jcdp + j ) = n;
      for (i=0; i<D; i++) {
          c = (int) *( dp + i*T + j );
          if (c >0) {
              *( srdp + n ) = c;
              *( irdp + n ) = i;
              n++;
          }
      }
  }
  *( jcdp + T ) = n;
 
  // processing MP as sparse output matrix 
  nzmaxmp = 0;
  for (i=0; i<W; i++) {
      for (j=0; j<S; j++) {
          nzmaxmp += (int) ( mp[ j + i*S ] > 0 );
      }    
  }  
  if (OUTPUT==2) mexPrintf( "Constructing sparse output matrix MP nnz=%d\n" , nzmaxmp );
  
  plhs[2] = mxCreateSparse( W,SINPUT,nzmaxmp,mxREAL);
  srmp  = mxGetPr(plhs[2]);
  irmp = mxGetIr(plhs[2]);
  jcmp = mxGetJc(plhs[2]);
  
  n = 0;
  for (j=0; j<SINPUT; j++) {
      *( jcmp + j ) = n;
      for (i=0; i<W; i++) {
          c = mp[ i*S + (j+2) ];
          if (c >0) {
              *( srmp + n ) = c;
              *( irmp + n ) = i;
              n++;
          }
      }
  }
  *( jcmp + SINPUT ) = n;
  
  
  
  /* ---------------------------------------------
     create the topic assignment vector
   -----------------------------------------------*/
  plhs[ 3 ] = mxCreateDoubleMatrix( ntokens , 1 , mxREAL );
  Z = mxGetPr( plhs[ 3 ] );
  for (i=0; i<ntokens; i++) Z[ i ] = (double) z[ i ] + 1;

   /* ---------------------------------------------
     create the HMM state assignment vector
   -----------------------------------------------*/
  plhs[ 4 ] = mxCreateDoubleMatrix( ntokens , 1 , mxREAL );
  X = mxGetPr( plhs[ 4 ] );
  for (i=0; i<ntokens; i++) X[ i ] = (double) x[ i ] + 1;
}
Beispiel #20
0
void mexFunction(
      int nlhs,   mxArray  *plhs[], 
      int nrhs,   const mxArray  *prhs[] )

{    double   *A, *U, *V, *VT, *S, *flag, *work, *AA;  

     mwIndex  subs[2];
     mwSize   nsubs=2; 
     mwIndex  *irS, *jcS, *iwork; 
     mwSize   M, N, lwork, info, options, minMN, maxMN, k, j; 
     mwSize   LDA, LDU, LDVT, nU, mVT, mS, nS; 
     char     *jobz;

/* CHECK FOR PROPER NUMBER OF ARGUMENTS */

   if (nrhs > 2){
      mexErrMsgTxt("mexsvd: requires at most 2 input arguments."); }
   if (nlhs > 4){ 
      mexErrMsgTxt("mexsvd: requires at most 4 output argument."); }   

/* CHECK THE DIMENSIONS */

    M = mxGetM(prhs[0]); 
    N = mxGetN(prhs[0]); 
    if (mxIsSparse(prhs[0])) {
       mexErrMsgTxt("mexeig: sparse matrix not allowed."); }   
    A = mxGetPr(prhs[0]); 
    LDA = M; 
    minMN = MIN(M,N); 
    maxMN = MAX(M,N); 
    options = 0; 
    if (nrhs==2) { options = (int)*mxGetPr(prhs[1]); } 
    if (options==0) { 
        /***** economical SVD ******/
       jobz="S"; nU = minMN; mVT = minMN; mS = minMN; nS = minMN; 
    } else {
        /***** full SVD ******/
       jobz="A"; nU = M; mVT = N; mS = M; nS = N; 
    }   
    LDU  = M; 
    LDVT = mVT; 
    /***** create return argument *****/
    if (nlhs >=3) {
       /***** compute singular values and vectors ******/
       plhs[0] = mxCreateDoubleMatrix(M,nU,mxREAL); 
       U = mxGetPr(plhs[0]);  
       plhs[1] = mxCreateSparse(mS,nS,minMN,mxREAL); 
       S   = mxGetPr(plhs[1]); 
       irS = mxGetIr(plhs[1]); 
       jcS = mxGetJc(plhs[1]);
       plhs[2] = mxCreateDoubleMatrix(N,mVT,mxREAL);     
       V = mxGetPr(plhs[2]); 
       plhs[3] = mxCreateDoubleMatrix(1,1,mxREAL);     
       flag = mxGetPr(plhs[3]); 
    } else { 
       /***** compute only singular values ******/
       plhs[0]= mxCreateDoubleMatrix(minMN,1,mxREAL); 
       S = mxGetPr(plhs[0]); 
       plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);     
       flag = mxGetPr(plhs[1]); 
       U = mxCalloc(M*nU,sizeof(double)); 
       V = mxCalloc(N*mVT,sizeof(double)); 
       jobz="N";
    }
    /***** Do the computations in a subroutine *****/   
    lwork = 4*minMN*minMN + MAX(maxMN,5*minMN*minMN+4*minMN);  
    work  = mxCalloc(lwork,sizeof(double)); 
    iwork = mxCalloc(8*minMN,sizeof(int)); 
    VT = mxCalloc(mVT*N,sizeof(double)); 
    AA = mxCalloc(M*N,sizeof(double)); 
    memcpy(AA,mxGetPr(prhs[0]),(M*N)*sizeof(double));

    dgesdd(jobz,&M,&N, AA,&LDA,S,U,&LDU,VT,&LDVT,work,&lwork,iwork, &info); 

    flag[0] = (double)info; 
    if (nlhs >= 3) { 
       for (k=0; k<minMN; k++) { irS[k] = k; }
       jcS[0] = 0;
       for (k=1; k<=nS; k++) { 
         if (k<minMN) { jcS[k] = k; } else { jcS[k] = minMN; }
       }  
       for (k=0; k<mVT; k++) { 
          for (j=0; j<N; j++) { V[j+k*N] = VT[k+j*mVT]; }
       }
    }
    return;
 }
Beispiel #21
0
mxArray* omp_ls(const double m_dict[], 
    const double m_x[],
    mwSize M, 
    mwSize N,
    mwSize S,
    mwSize K, 
    double res_norm_bnd,
    int sparse_output,
    int verbose){

    // List of indices of selected atoms
    mwIndex *selected_atoms = 0; 
    // Simple binary mask of selected atoms
    int* selected_atoms_mask = 0;   
    // The submatrix of selected atoms
    double* m_subdict = 0;
    // Copy of subdictionary for least squares
    double* m_subdict_copy = 0;
    // The proxy D' x
    double* v_proxy = 0;
    // The inner product of residual with atoms
    double* v_h = 0;
    // The residual
    double* v_r = 0;
    // Result of orthogonal projection LL' c = p_I
    double* v_c = 0;
    // Some temporary vectors
    double *v_t1 = 0, *v_t2 = 0;
    // Pointer to new atom
    const double* wv_new_atom;
    // residual norm squared
    double res_norm_sqr;
    // square of upper bound on residual norm
    double res_norm_bnd_sqr = SQR(res_norm_bnd);
    // Pointer to current signal
    const double *wv_x = 0;

    /// Output array
    mxArray* p_alpha;
    double* m_alpha;
    // row indices for non-zero entries in Alpha
    mwIndex *ir_alpha;
    // indices for first non-zero entry in column
    mwIndex *jc_alpha;
    /// Index for non-zero entries in alpha
    mwIndex nz_index;


    // counters
    int i, j , k, s;
    // index of new atom
    mwIndex new_atom_index;
    // misc variables 
    double d1, d2;

    // Maximum number of columns to be used in representations
    mwSize max_cols;

    // structure for tracking time spent.
    omp_profile profile;

    if (K < 0 || K > M) {
        // K cannot be greater than M.
        K = M;
    }
    max_cols = (mwSize)(ceil(sqrt((double)M)/2.0) + 1.01);
    if(max_cols < K){
        max_cols = K;
    }
    // Memory allocations
    // Number of selected atoms cannot exceed M
    selected_atoms = (mwIndex*) mxMalloc(M*sizeof(mwIndex));
    // Total number of atoms is N
    selected_atoms_mask = (int*) mxMalloc(N*sizeof(int));
    // Coefficients of solution of least square problem
    v_c = (double*)mxMalloc(M*sizeof(double));
    // Giving enough space for temporary vectors
    v_t1 = (double*)mxMalloc(N*sizeof(double));
    v_t2 = (double*)mxMalloc(N*sizeof(double));
    // Keeping max_cols space for subdictionary. 
    m_subdict = (double*)mxMalloc(max_cols*M*sizeof(double));
    m_subdict_copy = (double*)mxMalloc(max_cols*M*sizeof(double));
    // Proxy vector is in R^N
    v_proxy = (double*)mxMalloc(N*sizeof(double));
    // h is in R^N.
    v_h = (double*)mxMalloc(N*sizeof(double));
    // Residual is in signal space R^M.
    v_r = (double*)mxMalloc(M*sizeof(double));

    if (sparse_output == 0){
        p_alpha = mxCreateDoubleMatrix(N, S, mxREAL);
        m_alpha =  mxGetPr(p_alpha);
        ir_alpha = 0;
        jc_alpha = 0;
    }else{
        p_alpha = mxCreateSparse(N, S, max_cols*S, mxREAL);
        m_alpha = mxGetPr(p_alpha);
        ir_alpha = mxGetIr(p_alpha);
        jc_alpha = mxGetJc(p_alpha);
        nz_index = 0;
        jc_alpha[0] = 0;
    }
    omp_profile_init(&profile);

    for(s=0; s<S; ++s){
        wv_x = m_x + M*s;
        // Initialization
        res_norm_sqr = inner_product(wv_x, wv_x, M);
        //Compute proxy p  = D' * x
        mult_mat_t_vec(1, m_dict, wv_x, v_proxy, M, N);
        omp_profile_toctic(&profile, TIME_DtR);
        // h = p = D' * r
        copy_vec_vec(v_proxy, v_h, N);
        for (i=0; i<N; ++i){
            selected_atoms_mask[i] = 0;
        }
        // Number of atoms selected so far.
        k = 0;
        // Iterate for each atom
        while (k < K &&  res_norm_sqr > res_norm_bnd_sqr){
            omp_profile_tic(&profile);
            // Pick the index of (k+1)-th atom
            new_atom_index = abs_max_index(v_h, N);
            omp_profile_toctic(&profile, TIME_MaxAbs);
            // If this atom is already selected, we will break
            if (selected_atoms_mask[new_atom_index]){
                // This is unlikely due to orthogonal structure of OMP
                if (verbose){
                    mexPrintf("This atom is already selected.");
                }
                break;
            }
            // Check for small values
            d2 = v_h[new_atom_index];
            if (SQR(d2) < 1e-14){
                // The inner product of residual with new atom is way too small.
                break;
            }
            // Store the index of new atom
            selected_atoms[k] = new_atom_index;
            selected_atoms_mask[new_atom_index] = 1;

            // Copy the new atom to the sub-dictionary
            wv_new_atom = m_dict + new_atom_index*M;
            copy_vec_vec(wv_new_atom, m_subdict+k*M, M);
            omp_profile_toctic(&profile, TIME_DictSubMatrixUpdate);

            // It is time to increase the count of selected atoms
            ++k;
            // Least squares
            copy_vec_vec(m_subdict, m_subdict_copy, M*k);
            copy_vec_vec(wv_x, v_t1, M);
            least_square(m_subdict_copy, v_t1, v_c, M, k, 1);
            omp_profile_toctic(&profile, TIME_LeastSquares);
            // Compute residual
            // r  = x - D_I c
            mult_mat_vec(-1, m_subdict, v_c, v_r, M, k);
            sum_vec_vec(1, wv_x, v_r, M);
            omp_profile_toctic(&profile, TIME_RUpdate);
            // Update h = D' r
            mult_mat_t_vec(1, m_dict, v_r, v_h, M, N);
            // Update residual norm squared
            res_norm_sqr = inner_product(v_r, v_r, M);
            omp_profile_toctic(&profile, TIME_DtR);
            //mexPrintf(".\n");
        }

        // Write the output vector
        if(sparse_output == 0){
            // Write the output vector
            double* wv_alpha =  m_alpha + N*s;
            fill_vec_sparse_vals(v_c, selected_atoms, wv_alpha, N, k);
        }
        else{
            // Sort the row indices
            quicksort_indices(selected_atoms, v_c, k);
            // add the non-zero entries for this column
            for(j=0; j <k; ++j){
                m_alpha[nz_index] = v_c[j];
                ir_alpha[nz_index] = selected_atoms[j];
                ++nz_index;
            }
            // fill in the total number of nonzero entries in the end.
            jc_alpha[s+1] = jc_alpha[s] + k;
        }
    }
    if(verbose){
        omp_profile_print(&profile);
    }

    // Memory cleanup
    mxFree(selected_atoms);
    mxFree(selected_atoms_mask);
    mxFree(v_c);
    mxFree(v_t1);
    mxFree(v_t2);
    mxFree(m_subdict);
    mxFree(m_subdict_copy);
    mxFree(v_proxy);
    mxFree(v_h);
    mxFree(v_r);

    // Return the result
    return p_alpha;
}
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
/*input for C function */
double *in; /*input image*/
double *weight; /*output weight*/
double h; 
int nx,ny,nxny,newnx,newny;
int nwin, bloc;
double a, percent_sparse,v,c,sum,e,dist;
int i,j,k,nzmax;
int *dadr,*dd,x,y,xp,yp,adr,adrp,psize,wsize,ds,realadr,realadrp,li;
double *w,*ww,*lw;
  /*symetry image*/
  double* in_sym;
mwIndex *irs,*jcs;
char s;

/*Output for C function */

int l;
if (nrhs < 1 ) 
    mexErrMsgTxt("At least one inputs required.");
if (nrhs > 6 ) 
    mexErrMsgTxt("Too many inputs required.");
if (nlhs != 1) 
    mexErrMsgTxt("One output required.");

  

/*Input Image */
 nx = mxGetM(prhs[0]);
 ny = mxGetN(prhs[0]);
 in = mxGetPr(prhs[0]);
  h = mxGetScalar(prhs[1]);
  nxny=nx*ny;
     

if (nrhs<3)
    nwin=3;
else
   nwin=mxGetScalar(prhs[2]);
 

if (nrhs<4)
    bloc=6;
else
   bloc=mxGetScalar(prhs[3]);
    


/*Other parameters*/
 percent_sparse=(2*bloc+1)*(2*bloc+1)/(double)nxny;
  nzmax=(mwSize)ceil((double)nxny*(double)nxny*percent_sparse);
  
  plhs[0] = mxCreateSparse(nxny,nxny,nzmax,0);
  weight= mxGetPr(plhs[0]);

   
 /* Create a C pointer to a copy of the output matrix,but be careful that the matrix is tranposed */  
  a=(2*nwin)/4.;
  c=1;
   
  
   /*Sparse weight paramters*/
      irs= mxGetIr(plhs[0]);
      jcs= mxGetJc(plhs[0]);
  
 
 
  /* precompute weights */
  ds =nwin; 
  psize = (2*nwin+1)*(2*nwin+1); /* patch size;  patch = [-ds,ds]x[-ds,ds]  */
  wsize = (2*bloc+1)*(2*bloc+1); /* window size;  patch = [-d,d]x[-d,d]  */
  
  w = (double *)malloc(psize*sizeof(double)); /*patch weight */
 dadr = (int *)malloc(psize*sizeof(int));/*patch index */
 lw = (double *)malloc(wsize*sizeof(double)); /*local window weight */
 
 /*symstry bord of image*/
  
  newnx=nx+2*ds;
  newny=ny+2*ds;

  in_sym = (double *)malloc(newny*newnx*sizeof(double));
   
  /* extend the original image */
  for (j=0,k=0;j<newny;j++)
    {
      if (j<ds) 
	y=ds-j;
      else if (j>ny+ds-1)
	y=2*ny+ds-j-2;
      else y=j-ds;
      
      for (i=0;i<newnx;i++,k++)
	{
	  if (i<=ds) 
	    x=ds-i;
	  else if (i>nx+ds-1) x=2*nx+ds-i-2;
	  else x=i-ds;
	  in_sym[k]=in[y*nx+x];
	}
    }
 
/*Precompute patch weight and index */
  for(sum=0.,i=0,x=-ds;x<=ds;x++)
    for(y=-ds;y<=ds;y++,i++) {
      dadr[i] = y*newnx+x;
      w[i] = exp(-(double)(x*x+y*y)/(2.*a*a));
      sum += w[i];
    }
  
  for (i=psize;i--;) w[i] /= 2*sum*h*h;

 
             
  /* Principal loop */
      k=0;
      for (y=ds;y<newny-ds;y++) 
      for (x=ds;x<newnx-ds;x++)
        {	
         adr = y*newnx+x;
         realadr=(y-ds)*nx+x-ds;
    	 sum = 0.;     
         jcs[realadr] = k;
     
     /* loop on patches */
    /* Count non zero weight for realadr columns */
         for (xp=MAX(x-bloc,ds);xp<=MIN(x+bloc,nx-1+ds);xp++)
	       for (yp=MAX(y-bloc,ds);yp<=MIN(y+bloc,ny-1+ds);yp++)
	       { 
            adrp = yp*newnx+xp;
            realadrp=(yp-ds)*nx+xp-ds;
	        for (i=psize,dist=0.,ww=w,dd=dadr;i--;ww++,dd++) {
	         v = in_sym[adr+*dd]-in_sym[adrp+*dd];
             dist += *ww*(double)(v*v); 
            }   
	     e = (adrp==adr?c:exp(-dist));
         weight[k] = e;
         irs[k]=realadrp;
          k++;
	               }
        
       }
    jcs[nxny]=k;
  free(dadr);
  free(w);
  free(in_sym);
 

  
  
}
Beispiel #23
0
void mexFunction
(
    int	nargout,
    mxArray *pargout [ ],
    int	nargin,
    const mxArray *pargin [ ]
    )
{
  double dummy = 0, beta [2], *px, *C, *Ct, *C2, *fil, *Zt, *zt, done=1.0, *zz, dzero=0.0;
  cholmod_sparse Amatrix, *A, *Lsparse ;
  cholmod_factor *L ;
  cholmod_common Common, *cm ;
  Int minor, *It2, *Jt2 ;
  mwIndex l, k2, h, k, i, j, ik, *I, *J, *Jt, *It, *I2, *J2, lfi, *w, *w2, *r;
  mwSize nnz, nnzlow, m, n;
  int nz = 0;
  mwSignedIndex one=1, lfi_si;
  mxArray *Am, *Bm;
  char *uplo="L", *trans="N";
  

  /* ---------------------------------------------------------------------- */
  /* Only one input. We have to find first the Cholesky factorization.      */ 
  /* start CHOLMOD and set parameters */ 
  /* ---------------------------------------------------------------------- */

  if (nargin == 1) {
    cm = &Common ;
    cholmod_l_start (cm) ;
    sputil_config (SPUMONI, cm) ;
    
    /* convert to packed LDL' when done */
    cm->final_asis = FALSE ;
    cm->final_super = FALSE ;
    cm->final_ll = FALSE ;
    cm->final_pack = TRUE ;
    cm->final_monotonic = TRUE ;

    /* since numerically zero entries are NOT dropped from the symbolic
     * pattern, we DO need to drop entries that result from supernodal
     * amalgamation. */
    cm->final_resymbol = TRUE ;

    cm->quick_return_if_not_posdef = (nargout < 2) ;
  }

  /* This will disable the supernodal LL', which will be slow. */
  /* cm->supernodal = CHOLMOD_SIMPLICIAL ; */
  
  /* ---------------------------------------------------------------------- */
  /* get inputs */
  /* ---------------------------------------------------------------------- */
  
  if (nargin > 3)
    {
      mexErrMsgTxt ("usage: Z = sinv(A), or Z = sinv(LD, 1)") ;
    }
  
  n = mxGetM (pargin [0]) ;
  m = mxGetM (pargin [0]) ;
  
  if (!mxIsSparse (pargin [0]))
    {
      mexErrMsgTxt ("A must be sparse") ;
    }
  if (n != mxGetN (pargin [0]))
    {
      mexErrMsgTxt ("A must be square") ;
    }

  /* Only one input. We have to find first the Cholesky factorization.      */
  if (nargin == 1) {
    /* get sparse matrix A, use tril(A)  */
    A = sputil_get_sparse (pargin [0], &Amatrix, &dummy, -1) ; 
    
    A->stype = -1 ;	    /* use lower part of A */
    beta [0] = 0 ;
    beta [1] = 0 ;
    
    /* ---------------------------------------------------------------------- */
    /* analyze and factorize */
    /* ---------------------------------------------------------------------- */
    
    L = cholmod_l_analyze (A, cm) ;
    cholmod_l_factorize_p (A, beta, NULL, 0, L, cm) ;
    
    if (cm->status != CHOLMOD_OK)
      {
	mexErrMsgTxt ("matrix is not positive definite") ;
      }
    
    /* ---------------------------------------------------------------------- */
    /* convert L to a sparse matrix */
    /* ---------------------------------------------------------------------- */

    Lsparse = cholmod_l_factor_to_sparse (L, cm) ;
    if (Lsparse->xtype == CHOLMOD_COMPLEX)
      {
	mexErrMsgTxt ("matrix is complex") ;
      }
    
    /* ---------------------------------------------------------------------- */
    /* Set the sparse Cholesky factorization in Matlab format */
    /* ---------------------------------------------------------------------- */
    /*Am = sputil_put_sparse (&Lsparse, cm) ;
      I = mxGetIr(Am);
      J = mxGetJc(Am);
      C = mxGetPr(Am);
      nnz = mxGetNzmax(Am); */

    It2 = Lsparse->i;
    Jt2 = Lsparse->p;
    Ct = Lsparse->x;
    nnz = (mwSize) Lsparse->nzmax;

    Am = mxCreateSparse(m, m, nnz, mxREAL) ;
    I = mxGetIr(Am);
    J = mxGetJc(Am);
    C = mxGetPr(Am);
    for (j = 0 ;  j < n+1 ; j++)  J[j] = (mwIndex) Jt2[j];
    for ( i = 0 ; i < nnz ; i++) {
	I[i] = (mwIndex) It2[i];
	C[i] = Ct[i];
    }
    
    cholmod_l_free_sparse (&Lsparse, cm) ;

    /*FILE *out1 = fopen( "output1.txt", "w" );
    if( out1 != NULL )
      fprintf( out1, "Hello %d\n", nnz );
      fclose (out1);*/
    
  } else {
    /* The cholesky factorization is given as an input.      */
    /* We have to copy it into workspace                     */
    It = mxGetIr(pargin [0]);
    Jt = mxGetJc(pargin [0]);
    Ct = mxGetPr(pargin [0]);
    nnz = mxGetNzmax(pargin [0]);
    
    Am = mxCreateSparse(m, m, nnz, mxREAL) ;
    I = mxGetIr(Am);
    J = mxGetJc(Am);
    C = mxGetPr(Am);
    for (j = 0 ;  j < n+1 ; j++)  J[j] = Jt[j];
    for ( i = 0 ; i < nnz ; i++) {
	I[i] = It[i];
	C[i] = Ct[i];
    }    
  }

  /* Evaluate the sparse inverse */
  C[nnz-1] = 1.0/C[J[m-1]];               /* set the last element of sparse inverse */
  fil = mxCalloc((mwSize)1,sizeof(double));
  zt = mxCalloc((mwSize)1,sizeof(double));
  Zt = mxCalloc((mwSize)1,sizeof(double));
  zz = mxCalloc((mwSize)1,sizeof(double));
  for (j=m-2;j!=-1;j--){
    lfi = J[j+1]-(J[j]+1);
    
    /* if (lfi > 0) */
    if ( J[j+1] > (J[j]+1) )
      {
	/*	printf("lfi = %u \n ", lfi);
	printf("lfi*double = %u \n", (mwSize)lfi*sizeof(double));
	printf("lfi*lfi*double = %u \n", (mwSize)lfi*(mwSize)lfi*sizeof(double));
	printf("\n \n");
	*/
	
	fil = mxRealloc(fil,(mwSize)lfi*sizeof(double));
	for (i=0;i<lfi;i++) fil[i] = C[J[j]+i+1];                   /* take the j'th lower triangular column of the Cholesky */
	
	zt = mxRealloc(zt,(mwSize)lfi*sizeof(double));              /* memory for the sparse inverse elements to be evaluated */
	Zt = mxRealloc(Zt,(mwSize)lfi*(mwSize)lfi*sizeof(double));  /* memory for the needed sparse inverse elements */
	
	/* Set the lower triangular for Zt */
	k2 = 0;
	for (k=J[j]+1;k<J[j+1];k++){
	  ik = I[k];
	  h = k2;
	  for (l=J[ik];l<=J[ik+1];l++){
	    if (I[l] == I[ J[j]+h+1 ]){
	      Zt[h+lfi*k2] = C[l];
	      h++;
	    }
	  }
	  k2++;
	}
	
	
	/* evaluate zt = fil*Zt */
	lfi_si = (mwSignedIndex) lfi;
	dsymv(uplo, &lfi_si, &done, Zt, &lfi_si, fil, &one, &dzero, zt, &one);
	
	/* Set the evaluated sparse inverse elements, zt, into C */
	k=lfi-1;
	for (i = J[j+1]-1; i!=J[j] ; i--){
	  C[i] = -zt[k];
	  k--;
	}
	/* evaluate the j'th diagonal of sparse inverse */
	dgemv(trans, &one, &lfi_si, &done, fil, &one, zt, &one, &dzero, zz, &one); 
	C[J[j]] = 1.0/C[J[j]] + zz[0];
      }
    else
      {
	/* evaluate the j'th diagonal of sparse inverse */
	C[J[j]] = 1.0/C[J[j]];	
      }
  }
    
  /* Free the temporary variables */
  mxFree(fil);
  mxFree(zt);
  mxFree(Zt);
  mxFree(zz);

  /* ---------------------------------------------------------------------- */
  /* Permute the elements according to r(q) = 1:n                           */
  /* Done only if the Cholesky was evaluated here                           */
  /* ---------------------------------------------------------------------- */
  if (nargin == 1) {
   
    Bm = mxCreateSparse(m, m, nnz, mxREAL) ;     
    It = mxGetIr(Bm);
    Jt = mxGetJc(Bm);
    Ct = mxGetPr(Bm);                            /* Ct = C(r,r) */ 
    
    r = (mwIndex *) L->Perm;                         /* fill reducing ordering */
    w = mxCalloc(m,sizeof(mwIndex));                 /* column counts of Am */
    
    /* count entries in each column of Bm */
    for (j=0; j<m; j++){
      k = r ? r[j] : j ;       /* column j of Bm is column k of Am */
      for (l=J[j] ; l<J[j+1] ; l++){
	i = I[l];
	ik = r ? r[i] : i ;    /* row i of Bm is row ik of Am */
	w[ max(ik,k) ]++;
      }
    }
    cumsum2(Jt, w, m);
    for (j=0; j<m; j++){
      k = r ? r[j] : j ;             /* column j of Bm is column k of Am */
      for (l=J[j] ; l<J[j+1] ; l++){
	i= I[l];
	ik = r ? r[i] : i ;          /* row i of Bm is row ik of Am */
	It [k2 = w[max(ik,k)]++ ] = min(ik,k);
	Ct[k2] = C[l];
      }
    }
    mxFree(w);
    
    /* ---------------------------------------------------------------------- */
    /* Transpose the permuted (upper triangular) matrix Bm into Am */
    /* (this way we get sorted columns)                            */
    /* ---------------------------------------------------------------------- */
    w = mxCalloc(m,sizeof(mwIndex));                 
    for (i=0 ; i<Jt[m] ; i++) w[It[i]]++;        /* row counts of Bm */
    cumsum2(J, w, m);                            /* row pointers */
    for (j=0 ; j<m ; j++){
      for (i=Jt[j] ; i<Jt[j+1] ; i++){
	I[ l=w[ It[i] ]++ ] = j;
	C[l] = Ct[i];
      }
    }
    mxFree(w);
    mxDestroyArray(Bm);
  }
  
  /* ---------------------------------------------------------------------- */
  /* Fill the upper triangle of the sparse inverse */
  /* ---------------------------------------------------------------------- */
  
  w = mxCalloc(m,sizeof(mwIndex));        /* workspace */
  w2 = mxCalloc(m,sizeof(mwIndex));       /* workspace */
  for (k=0;k<J[m];k++) w[I[k]]++;     /* row counts of the lower triangular */
  for (k=0;k<m;k++) w2[k] = w[k] + J[k+1] - J[k] - 1;   /* column counts of the sparse inverse */
  
  nnz = (mwSize)2*nnz - m;                       /* The number of nonzeros in Z */
  pargout[0] = mxCreateSparse(m,m,nnz,mxREAL);   /* The sparse matrix */
  It = mxGetIr(pargout[0]);
  Jt = mxGetJc(pargout[0]);
  Ct = mxGetPr(pargout[0]);
  
  cumsum2(Jt, w2, m);               /* column starting points */
  for (j = 0 ; j < m ; j++){           /* fill the upper triangular */
    for (k = J[j] ; k < J[j+1] ; k++){
      It[l = w2[ I[k]]++] = j ;	 /* place C(i,j) as entry Ct(j,i) */
      if (Ct) Ct[l] = C[k] ;
    }
  }
  for (j = 0 ; j < m ; j++){           /* fill the lower triangular */
    for (k = J[j]+1 ; k < J[j+1] ; k++){
      It[l = w2[j]++] = I[k] ;         /* place C(j,i) as entry Ct(j,i) */
      if (Ct) Ct[l] = C[k] ;
    }
  }
  
  mxFree(w2);
  mxFree(w);
  
  /* ---------------------------------------------------------------------- */
  /* return to MATLAB */
  /* ---------------------------------------------------------------------- */
  
  /* ---------------------------------------------------------------------- */
  /* free workspace and the CHOLMOD L, except for what is copied to MATLAB */
  /* ---------------------------------------------------------------------- */
  if (nargin == 1) {
    cholmod_l_free_factor (&L, cm) ;
    cholmod_l_finish (cm) ;
    cholmod_l_print_common (" ", cm) ;
  }
  mxDestroyArray(Am);
  
}
Beispiel #24
0
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
	double *v, *x, sigma, *lambda, *pr; mwIndex *ir, *jc;
	char *mode; int imode;

    //Check Inputs
    if(nrhs < 1) {
        printInfo();        
        return;
    }   
    if(nrhs < 2) {
        mexErrMsgTxt("You must supply the callback mode and input vector.");
        return;
    }
    if(mxIsEmpty(prhs[0]) || !mxIsChar(prhs[0])) {
        mexErrMsgTxt("The mode must be a string!");
        return;
    }
    if(!mxIsEmpty(prhs[1])) {
        if(mxIsClass(prhs[1],"scipvar") || mxIsClass(prhs[1],"barvec")) {
            mexErrMsgTxt("SCIP and BARON cannot be used with this callback function - please specify 'mcode' via symbset as the cbmode.");
            return;
        }           
        if(!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) || mxIsSparse(prhs[1])) {
            mexErrMsgTxt("The input vector must be a dense real double vector!");
            return;
        }
    }
    else {
        mexErrMsgTxt("The input vector must be a dense real double vector!");
        return;
    }
	//Check x input size
	if(mxGetNumberOfElements(prhs[1]) != getNoVar()) {
		mexErrMsgTxt("The input vector is not the right size!");
	}
	//Get x
	x = mxGetPr(prhs[1]);
	
	//Determine input mode and setup return variable
	mode = mxArrayToString(prhs[0]);
	lower(mode);
	if(!strcmp(mode,"obj")) {
		imode = 0;
		plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);
		v = mxGetPr(plhs[0]);
	}
	else if(!strcmp(mode,"grad")) {
		imode = 1;
		plhs[0] = mxCreateDoubleMatrix(1,getNoVar(), mxREAL);
		v = mxGetPr(plhs[0]);
	}
	else if(!strcmp(mode,"con")) {
		imode = 2;
		plhs[0] = mxCreateDoubleMatrix(getNoCon(),1, mxREAL);
		v = mxGetPr(plhs[0]);
	}
	else if(!strcmp(mode,"jac")) {
		imode = 3;
		plhs[0] = mxCreateSparse(getNoCon(),getNoVar(),getNNZJac(),mxREAL);   
		pr = mxGetPr(plhs[0]);
		ir = mxGetIr(plhs[0]);
		jc = mxGetJc(plhs[0]);
	}
	else if(!strcmp(mode,"hess")) {
		if(nrhs < 4) {
			mexErrMsgTxt("You must supply the callback mode, input vector, sigma and lambda for Hessian Evaluations.");
			return;
		}
		//Check length of Sigma
		if(mxIsEmpty(prhs[2]) || !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || mxGetNumberOfElements(prhs[2]) != 1)
			mexErrMsgTxt("Sigma must be a real, double scalar.");
		//Check length of Lambda
		if(!mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxIsSparse(prhs[3]) || mxGetNumberOfElements(prhs[3]) != getNoCon())
			mexErrMsgTxt("Lambda must be a real, double, dense vector with ncon elements.");
		//Get Sigma, Lambda
		sigma = *mxGetPr(prhs[2]);
		lambda = mxGetPr(prhs[3]);
		imode = 4;
		plhs[0] = mxCreateSparse(getNoVar(),getNoVar(),getNNZHess(),mxREAL);   
		pr = mxGetPr(plhs[0]);
		ir = mxGetIr(plhs[0]);
		jc = mxGetJc(plhs[0]);
	}
	else
		mexErrMsgTxt("Unknown mode - options are 'obj', 'grad', 'con', 'jac', or 'hess'");
	mxFree(mode);
	
	
	//Call Req Callback
	switch(imode)
	{
		case 0: //objective
			*v = objective(x);
			break;
		case 1: //gradient
			gradient(x,v);
			break;
		case 2: //constraints
			constraints(x,v);
			break;
		case 3: //jacobian
			jacobian(x,pr,ir,jc);
			break;
		case 4: //hessian
			hessian(x,sigma,lambda,pr,ir,jc);
			break;
	}
}
Beispiel #25
0
 void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	ASL_pfgh *asl = (ASL_pfgh*)cur_ASL;
	FILE *nl;
	Jmp_buf err_jmp0;
	cgrad *cg, **cgp;
	char *buf1, buf[512], *what, **whatp;
	fint *hcs, *hr, i, nerror;
	int *cs;
	mwIndex *Ir, *Jc;
	real *H, *He, *J1, *W, *c, *f, *g, *v, *t, *x;
	static fint n, nc, nhnz, nz;
	static real *Hsp;
	static char ignore_complementarity[] =
		"Warning: ignoring %d complementarity conditions.\n";

	if (nrhs == 1 && mxIsChar(prhs[0])) {
		if (nlhs < 6 || nlhs > 7)
			usage();
		if (mxGetString(prhs[0], buf1 = buf, sizeof(buf)))
			mexErrMsgTxt("Expected 'stub' as argument\n");
		at_end();
		mexAtExit(at_end);
		asl = (ASL_pfgh*)ASL_alloc(ASL_read_pfgh);
		return_nofile = 1;
		if (!(nl = jac0dim(buf1,strlen(buf)))) {
			sprintf(msgbuf, "Can't open %.*s\n",
				sizeof(msgbuf)-20, buf);
			mexErrMsgTxt(msgbuf);
			}
		if (n_obj <= 0)
			printf("Warning: objectve == 0\n");
		n = n_var;
		nc = n_con;
		nz = nzc;
		X0 = mxGetPr(plhs[0] = mxCreateDoubleMatrix(n, 1, mxREAL));
		LUv = mxGetPr(plhs[1] = mxCreateDoubleMatrix(n, 1, mxREAL));
		Uvx = mxGetPr(plhs[2] = mxCreateDoubleMatrix(n, 1, mxREAL));
		pi0 = mxGetPr(plhs[3] = mxCreateDoubleMatrix(nc, 1, mxREAL));
		LUrhs = mxGetPr(plhs[4] = mxCreateDoubleMatrix(nc, 1, mxREAL));
		Urhsx = mxGetPr(plhs[5] = mxCreateDoubleMatrix(nc, 1, mxREAL));
		if (nlhs == 7) {
			cvar = (int*)M1alloc(nc*sizeof(int));
			plhs[6] = mxCreateDoubleMatrix(nc, 1, mxREAL);
			x = mxGetPr(plhs[6]);
			}
		else if (n_cc)
			printf(ignore_complementarity, n_cc);
		pfgh_read(nl, ASL_findgroups);
		if (nlhs == 7)
			for(i = 0; i < nc; i++)
				x[i] = cvar[i];

		/* Arrange to compute the whole sparese Hessian */
		/* of the Lagrangian function (both triangles). */

		nhnz = sphsetup(0, 0, nc > 0, 0);
		Hsp = (real *)M1alloc(nhnz*sizeof(real));
		return;
		}

	if (!asl)
		mexErrMsgTxt("spamfunc(\"stub\") has not been called\n");
	nerror = -1;
	err_jmp1 = &err_jmp0;
	what = "(?)";
	whatp = &what;
	if (nlhs == 2) {
		if (nrhs != 2)
			usage();
		x = sizechk(prhs[0],"x",n);
		t = sizechk(prhs[1],"0 or 1", 1);
		if (setjmp(err_jmp0.jb)) {
			sprintf(msgbuf, "Trouble evaluating %s\n", *whatp);
			mexErrMsgTxt(msgbuf);
			}
		if (t[0] == 0.) {
			f = mxGetPr(plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL));
			c = mxGetPr(plhs[1] = mxCreateDoubleMatrix(nc, 1, mxREAL));
			what = "f";
			*f = objval(0, x, &nerror);
			what = "c";
			conval(x, c, &nerror);
			return;
			}
		g = mxGetPr(plhs[0] = mxCreateDoubleMatrix(n, 1, mxREAL));
		J1 = mxGetPr(plhs[1] = mxCreateSparse(nc, n, nz, mxREAL));
		what = "g";
		objgrd(0, x, g, &nerror);
		if (nc) {
			what = "J";
			jacval(x, J1, &nerror);
			Ir = mxGetIr(plhs[1]);
			/*memcpy(mxGetJc(plhs[1]), A_colstarts, (n+1)*sizeof(int));*/
			for(Jc = mxGetJc(plhs[1]), cs = A_colstarts, i = 0; i <= n; ++i)
				Jc[i] = cs[i];
			cgp = Cgrad;
			for(i = 0; i < nc; i++)
				for(cg = *cgp++; cg; cg = cg->next)
					Ir[cg->goff] = i;
			}
		return;
		}
	if (nlhs == 0 && (nrhs == 3 || nrhs == 4)) {
		/* eval2('solution message', x, v): x = primal, v = dual */
		/* optional 4th arg = solve_result_num */
		if (!mxIsChar(prhs[0]))
			usage();
		x = sizechk(prhs[1],"x",n);
		v = sizechk(prhs[2],"v",nc);
		if (mxGetString(prhs[0], buf, sizeof(buf)))
			mexErrMsgTxt(
			 "Expected 'solution message' as first argument\n");
		solve_result_num = nrhs == 3 ? -1 /* unknown */
			: (int)*sizechk(prhs[3],"solve_result_num",1);
		write_sol(buf, x, v, 0);
		return;
		}
	if (nlhs != 1 || nrhs != 1)
		usage();
	v = sizechk(prhs[0],"v",nc);
	W = mxGetPr(plhs[0] = mxCreateSparse(n, n, nhnz, mxREAL));

	what = "W";
	sphes(H = Hsp, 0, 0, v);

	/* Expand the Hessian lower triangle into the full Hessian... */

	Ir = mxGetIr(plhs[0]);
	Jc = mxGetJc(plhs[0]);
	hcs = sputinfo->hcolstarts;
	hr = sputinfo->hrownos;
	for(i = 0; i <= n; i++)
		Jc[i] = hcs[i];
	He = H + hcs[n];
	while(H < He) {
		*W++ = *H++;
		*Ir++ = *hr++;
		}
	}
Beispiel #26
0
static mxArray *readdata(file_t *fp, mxArray **header, int start, int howmany){
    /*
      if start!=0 || howmany!=0 and data is cell, will only read cell from start to start+howmany
      if data is not cell and start==-1, will skip the data.
      Only the first call to readdata will possibly have howmany!=0
     */
    if(fp->eof) return NULL;
    header_t header2;
    if(read_header2(&header2, fp)){
	return NULL;
    }
    uint32_t magic=header2.magic & 0xFFFF;
    if(magic==0){//end of file or empty file
	fp->eof=1;
	return NULL;
    }
    if(header){
	if(header2.str)
	    *header=mxCreateString(header2.str);
	else
	    *header=mxCreateString("");
    }
    free(header2.str); header2.str=NULL;
    long nx=header2.nx;
    long ny=header2.ny;
    int start_save=start;
    if(iscell(magic)){
	if(start!=-1 && howmany==0){
	    if(start!=0){
		error("Invalid use\n");
	    }
	    howmany=nx*ny-start;
	}
    }else if(fp->isfits){
	if(howmany!=0){
	    /*first read of fits file. determine if we need it*/
	    if(start>0){
		start=-1;//skip this block.
	    }
	}
    }else{
	if((start!=0 && start!=-1) || howmany!=0){
	    error("invalid use");
	}
    }
    int iscell=0;
    if(fp->eof) return NULL;
    mxArray *out=NULL;
    switch(magic){
    case MCC_ANY:
    case MCC_DBL:
    case MCC_CMP:
    case MC_CSP:
    case MC_SP:
    case MC_DBL:
    case MC_CMP:
    case MC_INT32:
    case MC_INT64:
	{
	    iscell=1;
	    mwIndex ix;
	    if(fp->eof) return NULL;
	    if(nx*ny>1 && skip_unicell){
		out=mxCreateCellMatrix(nx,ny);
	    }
	    mxArray *header0=mxCreateCellMatrix(nx*ny+1,1);
	    for(ix=0; ix<nx*ny; ix++){
		int start2=0;
		if(start==-1 || ix<start || ix>=start+howmany){
		    start2=-1;
		}
		mxArray *header3=NULL;
		mxArray *tmp=readdata(fp, &header3, start2, 0);
		if(fp->eof){
		    break;
		}
		if(tmp && tmp!=SKIPPED){
		    if(nx*ny==1 && skip_unicell){//only one entry
			out=tmp;
		    }else{
			mxSetCell(out, ix, tmp);
		    }
		    if(header3){
			mxSetCell(header0, ix, header3);
		    }
		}
	    }
	    if(header){
		mxSetCell(header0, nx*ny, *header);
		*header=header0;
	    }
	}
	break;
    case M_SP64:
    case M_SP32:
	{
	    if(start==-1) error("Invalid use\n");
	    size_t size;
	    if(magic==M_SP32){
		size=4;
	    }else if(magic==M_SP64){
		size=8;
	    }else{
		size=0;
		error("Invalid magic\n");
	    }
	    uint64_t nzmax;
	    if(nx!=0 && ny!=0){
		zfread(&nzmax,sizeof(uint64_t),1,fp);
	    }else{
		nzmax=0;
	    }
	    if(fp->eof) return NULL;
	    out=mxCreateSparse(nx,ny,nzmax,mxREAL);
	    if(nx!=0 && ny!=0 && nzmax!=0){
		if(sizeof(mwIndex)==size){/*Match*/
		    zfread(mxGetJc(out), size,ny+1,fp);
		    zfread(mxGetIr(out), size,nzmax, fp);
		}else{
		    long i;
		    mwIndex *Jc0=mxGetJc(out);
		    mwIndex *Ir0=mxGetIr(out);
		    void *Jc=malloc(size*(ny+1));
		    void *Ir=malloc(size*nzmax);
		    zfread(Jc, size, ny+1, fp);
		    zfread(Ir, size, nzmax, fp);
		    if(size==4){
			uint32_t* Jc2=Jc;
			uint32_t* Ir2=Ir;
			for(i=0; i<ny+1; i++){
			    Jc0[i]=Jc2[i];
			}
			for(i=0; i<nzmax; i++){
			    Ir0[i]=Ir2[i];
			}
			free(Jc);
			free(Ir);
		    }else if(size==8){
			uint64_t* Jc2=Jc;
			uint64_t* Ir2=Ir;
			for(i=0; i<ny+1; i++){
			    Jc0[i]=Jc2[i];
			}
			for(i=0; i<nzmax; i++){
			    Ir0[i]=Ir2[i];
			}
			free(Jc);
			free(Ir);
		    }else{
			mexErrMsgTxt("Invalid sparse format\n");
		    }
		}
		zfread(mxGetPr(out), sizeof(double), nzmax, fp);
	    }
	}
	break;
    case M_CSP64:
    case M_CSP32:/*complex sparse*/
	{
	    if(start==-1) error("Invalid use\n");
	    size_t size;
	    switch(magic){
	    case M_CSP32:
		size=4;break;
	    case M_CSP64:
		size=8;break;
	    default:
		size=0;
	    }
	    uint64_t nzmax;
	    if(nx!=0 && ny!=0){
		zfread(&nzmax,sizeof(uint64_t),1,fp);
	    }else{
		nzmax=0;
	    }
	    if(fp->eof) return NULL;
	    out=mxCreateSparse(nx,ny,nzmax,mxCOMPLEX);
	    if(nx!=0 && ny!=0){
		long i;
		if(sizeof(mwIndex)==size){
		    zfread(mxGetJc(out), size,ny+1,fp);
		    zfread(mxGetIr(out), size,nzmax, fp);
		}else{
		    mwIndex *Jc0=mxGetJc(out);
		    mwIndex *Ir0=mxGetIr(out);
		    void *Jc=malloc(size*(ny+1));
		    void *Ir=malloc(size*nzmax);
		    zfread(Jc, size, ny+1, fp);
		    zfread(Ir, size, nzmax, fp);
		    if(size==4){
			uint32_t* Jc2=Jc;
			uint32_t* Ir2=Ir;
			for(i=0; i<ny+1; i++){
			    Jc0[i]=Jc2[i];
			}
			for(i=0; i<nzmax; i++){
			    Ir0[i]=Ir2[i];
			}
			free(Jc);
			free(Ir);
		    }else if(size==8){
			uint64_t* Jc2=Jc;
			uint64_t* Ir2=Ir;
			for(i=0; i<ny+1; i++){
			    Jc0[i]=Jc2[i];
			}
			for(i=0; i<nzmax; i++){
			    Ir0[i]=Ir2[i];
			}
			free(Jc);
			free(Ir);
		    }else{
			info("size=%lu\n", size);
			error("Invalid sparse format\n");
		    }
		}
		dcomplex *tmp=malloc(sizeof(dcomplex)*nzmax);
		zfread(tmp, sizeof(dcomplex), nzmax, fp);
		double *Pr=mxGetPr(out);
		double *Pi=mxGetPi(out);
		for(i=0; i<nzmax; i++){
		    Pr[i]=tmp[i].x;
		    Pi[i]=tmp[i].y;
		}
		free(tmp);
	    }
	}
	break;
    case M_DBL:/*double array*/
	if(start==-1){
	    if(zfseek(fp, sizeof(double)*nx*ny, SEEK_CUR)){
		error("Seek failed\n");
	    }
	    out=SKIPPED;
	}else{
	    out=mxCreateDoubleMatrix(nx,ny,mxREAL);
	    if(nx!=0 && ny!=0){
		zfread(mxGetPr(out), sizeof(double),nx*ny,fp);
	    }
	}
	break;
    case M_FLT:/*float array*/
	if(start==-1){
	    if(zfseek(fp, sizeof(float)*nx*ny, SEEK_CUR)){
		error("Seek failed\n");
	    }
	    out=SKIPPED;
	}else{
	    mwSize nxy[2]={nx,ny};
	    out=mxCreateNumericArray(2, nxy,mxSINGLE_CLASS, mxREAL);
	    if(nx!=0 && ny!=0){
		zfread((float*)mxGetPr(out), sizeof(float),nx*ny, fp);
	    }
	}break;
    case M_INT64:/*long array*/
    case M_INT32:
    case M_INT16:
    case M_INT8:
	{
	    int byte=0;
	    mxClassID id;
	    switch(magic){
	    case M_INT64: byte=8; id=mxINT64_CLASS; break;
	    case M_INT32: byte=4; id=mxINT32_CLASS; break;
	    case M_INT16: byte=2; id=mxINT16_CLASS; break;
	    case M_INT8:  byte=1; id=mxINT8_CLASS; break;
	    default: id=0;
	    }
	    if(start==-1){
		if(zfseek(fp, byte*nx*ny, SEEK_CUR)){
		    error("Seek failed\n");
		}
		out=SKIPPED;
	    }else{
		out=mxCreateNumericMatrix(nx,ny,id,mxREAL);
		if(nx!=0 && ny!=0){
		    /*Don't use sizeof(mxINT64_CLASS), it is just an integer, 
		      not a valid C type.*/
		    zfread(mxGetPr(out), byte,nx*ny,fp);
		}
	    }
	}
	break;
    case M_CMP:/*double complex array*/
	if(start==-1){
	    if(zfseek(fp, 16*nx*ny, SEEK_CUR)){
		error("Seek failed\n");
	    }
	    out=SKIPPED;
	}else{
	    out=mxCreateDoubleMatrix(nx,ny,mxCOMPLEX);
	    if(nx!=0 && ny!=0){
		dcomplex*tmp=malloc(sizeof(dcomplex)*nx*ny);
		zfread(tmp,sizeof(dcomplex),nx*ny,fp);
		double *Pr=mxGetPr(out);
		double *Pi=mxGetPi(out);
		long i;
		for(i=0; i<nx*ny; i++){
		    Pr[i]=tmp[i].x;
		    Pi[i]=tmp[i].y;
		}
		free(tmp);
	    }
	}
	break;
    case M_ZMP:/*float complex array. convert to double*/
	if(start==-1){
	    if(zfseek(fp, 8*nx*ny, SEEK_CUR)){
		error("Seek failed\n");
	    }
	    out=SKIPPED;
	}else{
	    mwSize nxy[2]={nx, ny};
	    out=mxCreateNumericArray(2, nxy,mxSINGLE_CLASS, mxCOMPLEX);
	    if(nx!=0 && ny!=0){
		fcomplex*tmp=malloc(sizeof(fcomplex)*nx*ny);
		zfread(tmp,sizeof(fcomplex),nx*ny,fp);
		float *Pr=(float*)mxGetPr(out);
		float *Pi=(float*)mxGetPi(out);
		long i;
		for(i=0; i<nx*ny; i++){
		    Pr[i]=tmp[i].x;
		    Pi[i]=tmp[i].y;
		}
		free(tmp);
	    }
	}
	break;
    case M_HEADER:
	break;
    default:
	fprintf(stderr,"magic=%x\n",magic);
	warning("Unrecognized file. Please recompile the mex routines in the newest code\n");
	out=NULL;
    }
    start=start_save;
    if(!iscell && fp->isfits==1){/*fits file may contain extra extensions.*/
	fp->isfits++;
	int icell=0;
	mxArray **outarr=NULL;
	mxArray **headerarr=NULL;
	while(out){
	    icell++;
	    outarr=realloc(outarr, sizeof(mxArray*)*icell);
	    headerarr=realloc(headerarr, sizeof(mxArray*)*icell);
	    if(out==SKIPPED) out=NULL;
	    outarr[icell-1]=out;
	    if(header) headerarr[icell-1]=*header;
	    int start2=0;
	    if(howmany!=0){//selective reading.
		if(icell<start || icell+1>start+howmany){
		    start2=-1;//don't read next data.
		}
	    }
	    out=readdata(fp, header, start2, 0);
	}
	if(icell>1){/*set output.*/
	    out=mxCreateCellMatrix(icell, 1);
	    if(header) *header=mxCreateCellMatrix(icell, 1);
	    int i;
	    for(i=0; i<icell; i++){
		mxSetCell(out, i, outarr[i]);
		if(header) mxSetCell(*header, i, headerarr[i]);
	    }
	    free(outarr);
	    free(headerarr);
	}else{
	    out=outarr[0];
	    if(header) *header=headerarr[0];
	}
    }
    if(!out){
	out=mxCreateDoubleMatrix(0,0,mxREAL);
    }
    return out;
}
Beispiel #27
0
/* ************************************************************
   PROCEDURE mexFunction - Entry for Matlab
   y = fwblksolve(L,b, [y])
     y = L.L \ b(L.perm)
   ************************************************************ */
void mexFunction(const int nlhs, mxArray *plhs[],
  const int nrhs, const mxArray *prhs[])
{
 const mxArray *L_FIELD;
 mwIndex m,n, j, k, nsuper, inz;
 double *y,*fwork;
 const double *permPr, *b, *xsuperPr;
 const mwIndex *yjc, *yir, *bjc, *bir;
 mwIndex *perm, *invperm, *snode, *xsuper, *iwork;
 jcir L;
 char bissparse;
 /* ------------------------------------------------------------
    Check for proper number of arguments 
    ------------------------------------------------------------ */
 mxAssert(nrhs >= MINNPARIN, "fwblkslv requires more input arguments.");
 mxAssert(nlhs <= NPAROUT, "fwblkslv generates only 1 output argument.");
 /* ------------------------------------------------------------
    Disassemble block Cholesky structure L
    ------------------------------------------------------------ */
 mxAssert(mxIsStruct(L_IN), "Parameter `L' should be a structure.");
 if( (L_FIELD = mxGetField(L_IN,(mwIndex)0,"perm")) == NULL)      /* L.perm */
   mexErrMsgTxt("Missing field L.perm.");
 m = mxGetM(L_FIELD) * mxGetN(L_FIELD);
 permPr = mxGetPr(L_FIELD);
 if( (L_FIELD = mxGetField(L_IN,(mwIndex)0,"L")) == NULL)      /* L.L */
   mexErrMsgTxt("Missing field L.L.");
 if( m != mxGetM(L_FIELD) || m != mxGetN(L_FIELD) )
   mexErrMsgTxt("Size L.L mismatch.");
 if(!mxIsSparse(L_FIELD))
   mexErrMsgTxt("L.L should be sparse.");
 L.jc = mxGetJc(L_FIELD);
 L.ir = mxGetIr(L_FIELD);
 L.pr = mxGetPr(L_FIELD);
 if( (L_FIELD = mxGetField(L_IN,(mwIndex)0,"xsuper")) == NULL)      /* L.xsuper */
   mexErrMsgTxt("Missing field L.xsuper.");
 nsuper = mxGetM(L_FIELD) * mxGetN(L_FIELD) - 1;
 if( nsuper > m )
   mexErrMsgTxt("Size L.xsuper mismatch.");
 xsuperPr = mxGetPr(L_FIELD);
 /* ------------------------------------------------------------
    Get rhs matrix b.
    If it is sparse, then we also need the sparsity structure of y.
    ------------------------------------------------------------ */
 b = mxGetPr(B_IN);
 if( mxGetM(B_IN) != m )
   mexErrMsgTxt("Size mismatch b.");
 n = mxGetN(B_IN);
 if( (bissparse = mxIsSparse(B_IN)) ){
   bjc = mxGetJc(B_IN);
   bir = mxGetIr(B_IN);
   if(nrhs < NPARIN)
     mexErrMsgTxt("fwblkslv requires more inputs in case of sparse b.");
   if(mxGetM(Y_IN) != m || mxGetN(Y_IN) != n)
     mexErrMsgTxt("Size mismatch y.");
   if(!mxIsSparse(Y_IN))
     mexErrMsgTxt("y should be sparse.");
 }
/* ------------------------------------------------------------
   Allocate output y. If bissparse, then Y_IN gives the sparsity structure.
   ------------------------------------------------------------ */
 if(!bissparse)
   Y_OUT = mxCreateDoubleMatrix(m, n, mxREAL);
 else{
   yjc = mxGetJc(Y_IN);
   yir = mxGetIr(Y_IN);
   Y_OUT = mxCreateSparse(m,n, yjc[n],mxREAL);
   memcpy(mxGetJc(Y_OUT), yjc, (n+1) * sizeof(mwIndex));
   memcpy(mxGetIr(Y_OUT), yir, yjc[n] * sizeof(mwIndex));
 }
 y = mxGetPr(Y_OUT);
/* ------------------------------------------------------------
   Allocate working arrays fwork(m) and iwork(2*m + nsuper+1)
   ------------------------------------------------------------ */
 fwork = (double *) mxCalloc(m, sizeof(double));
 iwork = (mwIndex *) mxCalloc(2*m+nsuper+1, sizeof(mwIndex));
 perm = iwork;
 invperm = perm;
 xsuper = iwork + m;
 snode = xsuper + (nsuper+1);
/* ------------------------------------------------------------
   Convert real to integer array, and from Fortran to C style.
   In case of sparse b, we store the inverse perm, instead of perm itself.
   ------------------------------------------------------------ */
 for(k = 0; k <= nsuper; k++)
   xsuper[k] = xsuperPr[k] - 1;
 if(!bissparse)
   for(k = 0; k < m; k++)               /* Get perm if !bissparse */
     perm[k] = permPr[k] - 1;
 else{
   for(k = 0; k < m; k++){              /* Get invperm if bissparse */
     j = permPr[k];
     invperm[--j] = k;
   }
/* ------------------------------------------------------------
   In case of sparse b, we also create snode, which maps each subnode
   to the supernode containing it.
   ------------------------------------------------------------ */
   for(j = 0, k = 0; k < nsuper; k++)
     while(j < xsuper[k+1])
       snode[j++] = k;
 }
/* ------------------------------------------------------------
   The actual job is done here: y = L\b(perm).
   ------------------------------------------------------------ */
 if(!bissparse)
   for(j = 0; j < n; j++){
     for(k = 0; k < m; k++)            /* y = b(perm) */
       y[k] = b[perm[k]];
     fwsolve(y,L.jc,L.ir,L.pr,xsuper,nsuper,fwork);
     y += m; b += m;
   }
 else
   for(j = 0, inz = 0; j < n; j++){
     for(k = inz; k < yjc[j+1]; k++)            /* fwork = all-0 */
       fwork[yir[k]] = 0.0;
     for(k = bjc[j]; k < bjc[j+1]; k++)            /* fwork = b(perm) */
       fwork[invperm[bir[k]]] = b[k];
     selfwsolve(fwork,L.jc,L.ir,L.pr,xsuper,nsuper, snode,
                yir+inz,yjc[j+1]-inz);
     for(; inz < yjc[j+1]; inz++)
       y[inz] = fwork[yir[inz]];
   }
 /* ------------------------------------------------------------
    RELEASE WORKING ARRAYS.
    ------------------------------------------------------------ */
 mxFree(iwork);
 mxFree(fwork);
}
Beispiel #28
0
const char *model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model)
{
	int i, j, n;
	SVM_REAL *ptr;
	mxArray *return_model, **rhs;
	int out_id = 0;

	rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD);

	/* Parameters */
#ifdef _ALL_FLOAT
	rhs[out_id] = mxCreateNumericMatrix(5, 1, mxSINGLE_CLASS, mxREAL);
	ptr = (float *) mxGetData(rhs[out_id]);
#else
	rhs[out_id] = mxCreateDoubleMatrix(5, 1, mxREAL);
	ptr = mxGetPr(rhs[out_id]);
#endif
	ptr[0] = (SVM_REAL)model->param.svm_type;
	ptr[1] = (SVM_REAL)model->param.kernel_type;
	ptr[2] = (SVM_REAL)model->param.degree;
	ptr[3] = model->param.gamma;
	ptr[4] = model->param.coef0;
	out_id++;

	/* nr_class */
#ifdef _ALL_FLOAT
	rhs[out_id] = mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS,mxREAL);
	ptr = (float *) mxGetData(rhs[out_id]);
#else
	rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
	ptr = mxGetPr(rhs[out_id]);
#endif
	ptr[0] = (SVM_REAL)model->nr_class;
	out_id++;

	/* total SV */
#ifdef _ALL_FLOAT
	rhs[out_id] = mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS,mxREAL);
	ptr = (float *) mxGetData(rhs[out_id]);
#else
	rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
	ptr = mxGetPr(rhs[out_id]);
#endif
	ptr[0] = (SVM_REAL)model->l;
	out_id++;

	/* rho */
	n = model->nr_class*(model->nr_class-1)/2;
#ifdef _ALL_FLOAT
	rhs[out_id] = mxCreateNumericMatrix(n, 1, mxSINGLE_CLASS,mxREAL);
	ptr = (float *) mxGetData(rhs[out_id]);
#else
	rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
	ptr = mxGetPr(rhs[out_id]);
#endif
	for(i = 0; i < n; i++)
		ptr[i] = model->rho[i];
	out_id++;

	/* Label */
	if(model->label)
	{
#ifdef _ALL_FLOAT
		rhs[out_id] = mxCreateNumericMatrix(model->nr_class, 1, mxSINGLE_CLASS,mxREAL);
		ptr = (float *) mxGetData(rhs[out_id]);
#else
		rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL);
		ptr = mxGetPr(rhs[out_id]);
#endif
		for(i = 0; i < model->nr_class; i++)
			ptr[i] = (SVM_REAL)model->label[i];
	}
	else
#ifdef _ALL_FLOAT
		rhs[out_id] = mxCreateNumericMatrix(0, 0, mxSINGLE_CLASS,mxREAL);
#else
		rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
#endif
	out_id++;

	/* probA */
	if(model->probA != NULL)
	{
#ifdef _ALL_FLOAT
		rhs[out_id] = mxCreateNumericMatrix(n, 1, mxSINGLE_CLASS,mxREAL);
		ptr = (float *) mxGetData(rhs[out_id]);
#else
		rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
		ptr = mxGetPr(rhs[out_id]);
#endif
		for(i = 0; i < n; i++)
			ptr[i] = model->probA[i];
	}
	else
#ifdef _ALL_FLOAT
		rhs[out_id] = mxCreateNumericMatrix(0, 0, mxSINGLE_CLASS,mxREAL);
#else
		rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
#endif
	out_id ++;

	/* probB */
	if(model->probB != NULL)
	{
#ifdef _ALL_FLOAT
		rhs[out_id] = mxCreateNumericMatrix(n, 1, mxSINGLE_CLASS,mxREAL);
		ptr = (float *) mxGetData(rhs[out_id]);
#else
		rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
		ptr = mxGetPr(rhs[out_id]);
#endif
		for(i = 0; i < n; i++)
			ptr[i] = model->probB[i];
	}
	else
#ifdef _ALL_FLOAT
		rhs[out_id] = mxCreateNumericMatrix(0, 0, mxSINGLE_CLASS,mxREAL);
#else
		rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
#endif
	out_id++;

	/* nSV */
	if(model->nSV)
	{
#ifdef _ALL_FLOAT
		rhs[out_id] = mxCreateNumericMatrix(model->nr_class, 1, mxSINGLE_CLASS,mxREAL);
		ptr = (float *) mxGetData(rhs[out_id]);
#else
		rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL);
		ptr = mxGetPr(rhs[out_id]);
#endif
		for(i = 0; i < model->nr_class; i++)
			ptr[i] = (SVM_REAL)model->nSV[i];
	}
	else
#ifdef _ALL_FLOAT
		rhs[out_id] = mxCreateNumericMatrix(0, 0, mxSINGLE_CLASS,mxREAL);
#else
		rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
#endif
	out_id++;

	/* sv_coef */
#ifdef _ALL_FLOAT
	rhs[out_id] = mxCreateNumericMatrix(model->l, model->nr_class-1, mxSINGLE_CLASS,mxREAL);
	ptr = (float *) mxGetData(rhs[out_id]);
#else
	rhs[out_id] = mxCreateDoubleMatrix(model->l, model->nr_class-1, mxREAL);
	ptr = mxGetPr(rhs[out_id]);
#endif
	for(i = 0; i < model->nr_class-1; i++)
		for(j = 0; j < model->l; j++)
			ptr[(i*(model->l))+j] = model->sv_coef[i][j];
	out_id++;

	/* SVs */
	{
#ifdef _DENSE_REP
#ifdef _ALL_FLOAT
		rhs[out_id] = mxCreateNumericMatrix(model->l, num_of_feature, mxSINGLE_CLASS,mxREAL);
		ptr = (float *) mxGetData(rhs[out_id]);
#else
		rhs[out_id] = mxCreateDoubleMatrix(model->l, num_of_feature,  mxREAL);
		ptr = mxGetPr(rhs[out_id]);
#endif
		for(i=0;i < num_of_feature;i++)
			for(j=0;j < model->l;j++)
				ptr[(i*(model->l))+j] = model->SV[j].values[i];
#else
		int ir_index, nonzero_element;
		mwIndex *ir, *jc;
		mxArray *pprhs[1], *pplhs[1];	
		if(model->param.kernel_type == PRECOMPUTED)
		{
			nonzero_element = model->l;
			num_of_feature = 1;
		}
		else
		{
			nonzero_element = 0;
			for(i = 0; i < model->l; i++) {
				j = 0;
				while(model->SV[i][j].index != -1) 
				{
					nonzero_element++;
					j++;
				}
			}
		}

		/* SV in column, easier accessing */
		double *ptr2;
		rhs[out_id] = mxCreateSparse(num_of_feature, model->l, nonzero_element, mxREAL);
		ptr2 = mxGetPr(rhs[out_id]);
		ir = mxGetIr(rhs[out_id]);
		jc = mxGetJc(rhs[out_id]);
		jc[0] = ir_index = 0;		
		for(i = 0;i < model->l; i++)
		{
			if(model->param.kernel_type == PRECOMPUTED)
			{
				/* make a (1 x model->l) matrix */
				ir[ir_index] = 0; 
				ptr2[ir_index] = model->SV[i][0].value;
				ir_index++;
				jc[i+1] = jc[i] + 1;
			}
			else
			{
				int x_index = 0;
				while (model->SV[i][x_index].index != -1)
				{
					ir[ir_index] = model->SV[i][x_index].index - 1; 
					ptr2[ir_index] = model->SV[i][x_index].value;
					ir_index++, x_index++;
				}
				jc[i+1] = jc[i] + x_index;
			}
		}
		/* transpose back to SV in row */
		pprhs[0] = rhs[out_id];
		if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose"))
			return "cannot transpose SV matrix";
		rhs[out_id] = pplhs[0];
#endif
		out_id++;
	}
#ifdef _USE_CHI_SQUARE
	rhs[out_id] = mxCreateString(model->param.feat_parm_text);
#endif

	/* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */
	return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names);

	/* Fill struct matrix with input arguments */
	for(i = 0; i < NUM_OF_RETURN_FIELD; i++)
		mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i]));
	/* return */
	plhs[0] = return_model;
	mxFree(rhs);

	return NULL;
}
Beispiel #29
0
/* ************************************************************
   PROCEDURE mexFunction - Entry for Matlab
   ************************************************************ */
void mexFunction(int nlhs, mxArray *plhs[],
  int nrhs, const mxArray *prhs[])
{
  const mxArray *L_FIELD;
  mwIndex maxnnz, i,j, nsuper,m,n;
  const mwIndex *ljc,*lir,*bjc,*bir;
  mwIndex *xjc,*xir, *snode,*xlindx,*lindx, *iwork,*xsuper, *invperm;
  bool *cwork;
  double *xpr;
  const double *permPr, *xsuperPr;
/* ------------------------------------------------------------
   Check for proper number of arguments
   ------------------------------------------------------------ */
  mxAssert(nrhs >= NPARIN, "symbfwblk requires more input arguments");
  mxAssert(nlhs <= NPAROUT, "symbfwblk produces 1 output argument");
/* ------------------------------------------------------------
   Get rhs-input B
   ------------------------------------------------------------ */
  mxAssert(mxIsSparse(B_IN), "B must be sparse");
  m = mxGetM(B_IN);
  n = mxGetN(B_IN);
  bjc = mxGetJc(B_IN);
  bir = mxGetIr(B_IN);
/* ------------------------------------------------------------
   Disassemble block Cholesky structure L
   ------------------------------------------------------------ */
  mxAssert(mxIsStruct(L_IN), "Parameter `L' should be a structure.");
  L_FIELD = mxGetField(L_IN,(mwIndex)0,"perm"); 
  mxAssert( L_FIELD != NULL, "Missing field L.perm.");        /* L.perm */
  mxAssert(m == mxGetM(L_FIELD) * mxGetN(L_FIELD), "L.perm size mismatches B");
  permPr = mxGetPr(L_FIELD);
  L_FIELD = mxGetField(L_IN,(mwIndex)0,"L"); 
  mxAssert( L_FIELD!= NULL, "Missing field L.L.");           /* L.L */
  mxAssert( m == mxGetM(L_FIELD) && m == mxGetN(L_FIELD), "Size L.L mismatch.");
  mxAssert(mxIsSparse(L_FIELD), "L.L should be sparse.");
  ljc = mxGetJc(L_FIELD);
  lir = mxGetIr(L_FIELD);
  L_FIELD = mxGetField(L_IN,(mwIndex)0,"xsuper"); 
  mxAssert( L_FIELD!= NULL, "Missing field L.xsuper.");     /* L.xsuper */
  nsuper = mxGetM(L_FIELD) * mxGetN(L_FIELD) - 1;
  mxAssert( nsuper <= m , "Size L.xsuper mismatch.");
  xsuperPr = mxGetPr(L_FIELD);
/* ------------------------------------------------------------
   Allocate mwIndex-part of sparse output matrix X(m x n)
   Heuristically set nnz to nnz(B) + 4*m.
   ------------------------------------------------------------ */
  maxnnz = bjc[n] + 4 * m;
  xjc = (mwIndex *) mxCalloc(n + 1, sizeof(mwIndex));
  xir = (mwIndex *) mxCalloc(maxnnz, sizeof(mwIndex));
/* ------------------------------------------------------------
   Allocate working arrays:
   mwIndex invperm(m), snode(m), xsuper(nsuper+1), xlindx(nsuper+1), lindx(nnz(L)),
   iwork(nsuper).
   char cwork(nsuper).
   ------------------------------------------------------------ */
  invperm   = (mwIndex *) mxCalloc(m,sizeof(mwIndex)); 
  snode     = (mwIndex *) mxCalloc(m,sizeof(mwIndex)); 
  xsuper    = (mwIndex *) mxCalloc(nsuper+1,sizeof(mwIndex));
  xlindx    = (mwIndex *) mxCalloc(nsuper+1,sizeof(mwIndex));
  lindx     = (mwIndex *) mxCalloc(ljc[m], sizeof(mwIndex));
  iwork = (mwIndex *) mxCalloc(nsuper, sizeof(mwIndex));
  cwork = (bool *) mxCalloc(nsuper, sizeof(bool));
/* ------------------------------------------------------------
   Convert PERM, XSUPER to integer and C-Style
   ------------------------------------------------------------ */
  for(i = 0; i < m; i++){
    j = (mwIndex) permPr[i];
    mxAssert(j>0,"");
    invperm[--j] = i;                /* so that invperm[perm[i]] = i */
  }
  for(i = 0; i <= nsuper; i++){
    j =  (mwIndex) xsuperPr[i];
    mxAssert(j>0,"");    
    xsuper[i] = --j;
  }
/* ------------------------------------------------------------
   Create "snode" from xsuper, and get compact subscript (xlindx,lindx)
   from (ljc,lir,xsuper), i.e. nz-pattern per supernode.
   ------------------------------------------------------------ */
  snodeCompress(xlindx,lindx,snode, ljc,lir,xsuper,nsuper);
/* ------------------------------------------------------------
   Compute nz structure after forward solve
   ------------------------------------------------------------ */
  symbfwmat(xjc, &xir, &maxnnz, bjc, bir, invperm, snode, xsuper,
            xlindx, lindx,
            nsuper, m, n, iwork, cwork);
/* ------------------------------------------------------------
   Create output matrix x
   ------------------------------------------------------------ */
  X_OUT = mxCreateSparse(m,n, (mwSize)1,mxREAL);
  mxFree(mxGetJc(X_OUT));                    /* jc */
  mxFree(mxGetIr(X_OUT));                    /* ir */
  mxFree(mxGetPr(X_OUT));                    /* pr */
  xpr = (double *) mxCalloc(maxnnz,sizeof(double));
  mxSetJc(X_OUT, xjc);
  mxSetIr(X_OUT, xir);
  mxSetPr(X_OUT, xpr);
  mxSetNzmax(X_OUT, maxnnz);
  for(i = 0; i < maxnnz; i++)
    xpr[i] = 1.0;
/* ------------------------------------------------------------
   Release working arrays.
   ------------------------------------------------------------ */
  mxFree(cwork);
  mxFree(iwork);
  mxFree(lindx);
  mxFree(xlindx);
  mxFree(xsuper);
  mxFree(snode);
  mxFree(invperm);
}
void mexFunction(int nlhs, mxArray  *plhs[], 
                 int nrhs, const mxArray  *prhs[] )

{    mxArray  *blk_cell_pr;
     double   *A,  *B,  *AI, *BI, *blksize; 
     mwIndex  *irA, *jcA, *irB, *jcB;
     int      *cumblksize, *blknnz;
     int       mblk, isspA, isspB, iscmpA;

     mwIndex  subs[2];
     mwSize   nsubs=2; 
     int      m, n, n2, nsub, k, index, numblk, NZmax, type; 
     double   r2; 

/* CHECK FOR PROPER NUMBER OF ARGUMENTS */

   if (nrhs < 2){
      mexErrMsgTxt("mexsvec: requires at least 2 input arguments."); }
   if (nlhs > 1){ 
      mexErrMsgTxt("mexsvec: requires 1 output argument."); }

/* CHECK THE DIMENSIONS */

    mblk = mxGetM(prhs[0]); 
    if (mblk > 1) { 
       mexErrMsgTxt("mexsvec: blk can have only 1 row."); }
    m = mxGetM(prhs[1]); 
    n = mxGetN(prhs[1]); 
    if (m != n) { 
       mexErrMsgTxt("mexsvec: matrix must be square."); }

    subs[0] = 0; subs[1] = 1;
    index = mxCalcSingleSubscript(prhs[0],nsubs,subs); 
    blk_cell_pr = mxGetCell(prhs[0],index);
    numblk  = mxGetN(blk_cell_pr);
    blksize = mxGetPr(blk_cell_pr); 
    if (numblk == 1) { 
       n2 = n*(n+1)/2; 
    } else { 
       cumblksize = mxCalloc(numblk+1,sizeof(int)); 
       blknnz = mxCalloc(numblk+1,sizeof(int)); 
       cumblksize[0] = 0; blknnz[0] = 0; 
       n = 0; n2 = 0; 
       for (k=0; k<numblk; ++k) {
           nsub = (int) blksize[k];
           n  += nsub; 
           n2 += nsub*(nsub+1)/2;  
           cumblksize[k+1] = n; 
           blknnz[k+1] = n2;  }
    }
    /***** assign pointers *****/
    A = mxGetPr(prhs[1]); 
    isspA = mxIsSparse(prhs[1]);
    iscmpA = mxIsComplex(prhs[1]);   
    if (isspA) {  irA = mxGetIr(prhs[1]); 
                  jcA = mxGetJc(prhs[1]); 
                  NZmax = mxGetNzmax(prhs[1]);  
    } else { 
       NZmax = n2; 
    }
    if (iscmpA) { AI = mxGetPi(prhs[1]); }
    if ((numblk > 1) & (!isspA)) {
       mexErrMsgTxt("mexsvec: matrix must be sparse for numblk > 1"); }
    if (nrhs > 2) { 
       if (mxGetM(prhs[2])>1) { isspB = (int)*mxGetPr(prhs[2]); }
       else if (NZmax < n2/2) { isspB = 1; }
       else                   { isspB = 0; } 
    } else {        
       if (NZmax < n2/2) { isspB = 1; }
       else              { isspB = 0; }
    } 
    if (nrhs > 3) { type = (int)*mxGetPr(prhs[3]); } 
    else          { type = 0; } 
    /***** create return argument *****/
    if (isspB) {
       if (iscmpA) { 
          plhs[0] = mxCreateSparse(n2,1,NZmax,mxCOMPLEX); 
       } else { 
          plhs[0] = mxCreateSparse(n2,1,NZmax,mxREAL); 
       }
       B = mxGetPr(plhs[0]);
       irB = mxGetIr(plhs[0]); 
       jcB = mxGetJc(plhs[0]); 
       jcB[0] = 0; 
    } else {
       if (iscmpA) { 
          plhs[0] = mxCreateDoubleMatrix(n2,1,mxCOMPLEX); 
       } else { 
          plhs[0] = mxCreateDoubleMatrix(n2,1,mxREAL); 
       }
       B = mxGetPr(plhs[0]);  
    } 
    if (iscmpA) { BI = mxGetPi(plhs[0]); }   
    /***** Do the computations in a subroutine *****/
    r2 = sqrt(2); 
    if (type == 0) { 
       if (iscmpA) {
         if (numblk == 1) { 
            svec1cmp(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI);  }
         else {
            svec2cmp(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI); 
         }
       } else { 
         if (numblk == 1) { 
            svec1(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB);  }
         else {
            svec2(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB); 
         }
       }
    } else {
       if (iscmpA) { 
         if (numblk == 1) { 
            svec3cmp(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI);  }
         else {
            svec4cmp(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI); 
	 }
       } else {
         if (numblk == 1) { 
            svec3(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB);  }
         else {
            svec4(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB); 
	 }
       }
    }
    return;
 }