コード例 #1
0
void homogToFlow(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
  /* [us,vs]=homogToFlow(H,m,n,[r0],[r1],[c0],[c1]); */
  int ind=0, i, j, m, n, m1, n1; double *H, *us, *vs;
  double r, c, r0, c0, r1, c1, m2, n2, z;

  /* extract inputs */
  H  = (double*) mxGetData(prhs[0]);
  m  = (int) mxGetScalar(prhs[1]);
  n  = (int) mxGetScalar(prhs[2]);
  if(nrhs==7) {
    r0 = mxGetScalar(prhs[3]);
    r1 = mxGetScalar(prhs[4]);
    c0 = mxGetScalar(prhs[5]);
    c1 = mxGetScalar(prhs[6]);
    m1  = (int) (r1-r0+1);
    n1  = (int) (c1-c0+1);
  } else {
    m1=m; r0 = (-m+1.0)/2.0;
    n1=n; c0 = (-n+1.0)/2.0;
  }
  m2 = (m-1.0)/2.0;
  n2 = (n-1.0)/2.0;

  /* initialize memory */
  us = mxMalloc(sizeof(double)*m1*n1);
  vs = mxMalloc(sizeof(double)*m1*n1);

  /* Compute flow at each grid point */
  for( i=0; i<9; i++ ) H[i]=H[i]/H[8];
  if( H[2]==0 && H[5]==0 ) {
    for( i=0; i<n1; i++ ) {
      r = H[0]*r0 + H[3]*(c0+i) + H[6] + m2;
      c = H[1]*r0 + H[4]*(c0+i) + H[7] + n2 - i;
      for(j=0; j<m1; j++) {
        us[ind]=r-j; vs[ind]=c;
        r+=H[0]; c+=H[1]; ind++;
      }
    }
  } else {
    for( i=0; i<n1; i++ ) {
      r = H[0]*r0 + H[3]*(c0+i) + H[6];
      c = H[1]*r0 + H[4]*(c0+i) + H[7];
      z = H[2]*r0 + H[5]*(c0+i) + 1;
      for(j=0; j<m1; j++) {
        us[ind]=r/z+m2-j; vs[ind]=c/z+n2-i;
        r+=H[0]; c+=H[1]; z+=H[2]; ind++;
      }
    }
  }

  /* create output array */
  plhs[0] = mxCreateNumericMatrix(0,0,mxDOUBLE_CLASS,mxREAL);
  plhs[1] = mxCreateNumericMatrix(0,0,mxDOUBLE_CLASS,mxREAL);
  mxSetData(plhs[0],us); mxSetM(plhs[0],m1); mxSetN(plhs[0],n1);
  mxSetData(plhs[1],vs); mxSetM(plhs[1],m1); mxSetN(plhs[1],n1);
}
コード例 #2
0
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
  double *xin, *yin, *xout, *yout;
  size_t nin, nout;

  /* Check for proper number of arguments. */
  if (nrhs!=2)
    mexErrMsgTxt("Two inputs required.");
  if (nlhs>2)
    mexErrMsgTxt("Too many output arguments.");

  /* Check for matching dimensions. */
  if ( mxGetM(prhs[0]) != mxGetM(prhs[1]) || 
       mxGetN(prhs[0]) != mxGetN(prhs[1]) )
    mexErrMsgTxt("Inputs must have the same dimensions.");

  /* Check for proper numeric class and dimensions. */
  if ( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
       !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
       ( mxGetM(prhs[0]) != 1 && mxGetN(prhs[0]) != 1 ) )
    mexErrMsgTxt("Inputs must be double non complex vectors.");

  /* Assign pointers to corresponding inputs. */
  nin = mxGetNumberOfElements(prhs[0]);
  xin = mxGetPr(prhs[0]);
  yin = mxGetPr(prhs[1]);

  /* Call to triangulation function based on gpc. */
  xout = NULL;
  yout = NULL;
  nout = 0;
  poly2tri_gpc(&xout, &yout, &nout, xin, yin, nin);

  /* Assign pointers to corresponding outputs. */
  if (nlhs > 0)
  {
    plhs[0] = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
    mxSetM(plhs[0], 3);
    mxSetN(plhs[0], nout);
    mxSetPr(plhs[0], xout);
  }
  if (nlhs > 1)
  {
    plhs[1] = mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
    mxSetM(plhs[1], 3);
    mxSetN(plhs[1], nout);
    mxSetPr(plhs[1], yout);
  }
}
コード例 #3
0
static double MatlabCallback(int n, const double *x, int *undefined_flag, void *data) {
    mxArray *rhs[2];
    mxArray *lhs[2];
    double *oldPtr;
    double fVal;
    bool violatesConstraints;

    //feval in Matlab will take the function handle and the state as
    //inputs.
    //The first function handle is f. 
    rhs[0]=(mxArray*)data;
    rhs[1]=mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);

    //Set the matrix data to x 
    oldPtr=mxGetPr(rhs[1]);
    
    //x will not be modified, but the const must be typecast away to use
    //the mxSetPr function.    
    mxSetPr(rhs[1],(double*)x);
    mxSetM(rhs[1], (size_t)n);
    mxSetN(rhs[1], 1);
    
    //Get the function value and gradient.
    mexCallMATLAB(2,lhs,2,rhs,"feval");
    
    //Get the function value.
    fVal=getDoubleFromMatlab(lhs[0]);
      
    violatesConstraints=getBoolFromMatlab(lhs[1]);
    
    if(violatesConstraints) {
        *undefined_flag=1;
    }
    
    //Get rid of the returned Matlab Matrices.
    mxDestroyArray(lhs[0]);
    mxDestroyArray(lhs[1]);
    
    //Set the data pointer back to what it was during allocation that
    //mxDestroyArray does not have a problem. 
    mxSetPr(rhs[1],oldPtr);
    mxSetM(rhs[1], 0);
    mxSetN(rhs[1], 0);
          
    //Get rid of the temporary natrix.
    mxDestroyArray(rhs[1]);

	return fVal;
}
コード例 #4
0
ファイル: shmResult2mat.c プロジェクト: davidozog/PRESTO
/*  MEX GATEWAY  */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{

  int num, jobid, i;
  mwSize idx;
  double *arg_p;

  arg_p = mxGetPr(prhs[0]);

  /* TODO: This uses 2x the minimal necessary memory */
 /*  UINT8_T data[(int)arg_p[1]]; */
  UINT8_T data[4096];
  UINT8_T *mesg_data;

  get_shmem_mesg(data, prhs[0], &jobid);

  mesg_data = mxCalloc((int)arg_p[0], sizeof(UINT8_T));
  for ( idx = 0; idx < (int)arg_p[0]; idx++ ) {
    mesg_data[idx] = data[idx];
  }
  plhs[0] = mxCreateNumericMatrix(0, 0, mxUINT8_CLASS, mxREAL);
/*
  printf(" mesg_data[0] : %d \n", mesg_data[0]);
  printf(" mesg_data[end] : %d \n", mesg_data[(int)arg_p[0]-1]);
  printf(" nbytes : %d \n", (int)arg_p[0]);
*/

  /* Point mxArray to dynamicData */
  mxSetData(plhs[0], mesg_data);
  mxSetM(plhs[0], (int)arg_p[0]);
  mxSetN(plhs[0], 1);

  return;
}
コード例 #5
0
ファイル: cs_mex.c プロジェクト: Al-th/matlab
/* return a complex sparse matrix to MATLAB */
mxArray *cs_cl_mex_put_sparse (cs_cl **Ahandle)
{
    cs_cl *A ;
    double *x, *z ;
    mxArray *Amatlab ;
    CS_INT k ;

    A = *Ahandle ;
    if (!A) mexErrMsgTxt ("failed") ;
    Amatlab = mxCreateSparse (0, 0, 0, mxCOMPLEX) ;
    mxSetM (Amatlab, A->m) ;
    mxSetN (Amatlab, A->n) ;
    mxSetNzmax (Amatlab, A->nzmax) ;
    cs_cl_free (mxGetJc (Amatlab)) ;
    cs_cl_free (mxGetIr (Amatlab)) ;
    cs_cl_free (mxGetPr (Amatlab)) ;
    cs_cl_free (mxGetPi (Amatlab)) ;
    mxSetJc (Amatlab, (void *) (A->p)) ; /* assign A->p pointer to MATLAB A */
    mxSetIr (Amatlab, (void *) (A->i)) ;
    x = cs_dl_malloc (A->nzmax, sizeof (double)) ;
    z = cs_dl_malloc (A->nzmax, sizeof (double)) ;
    for (k = 0 ; k < A->nzmax ; k++)
    {
        x [k] = creal (A->x [k]) ;      /* copy and split numerical values */
        z [k] = cimag (A->x [k]) ;
    }
    cs_cl_free (A->x) ;                 /* free copy of complex values */
    mxSetPr (Amatlab, x) ;
    mxSetPi (Amatlab, z) ;
    cs_cl_free (A) ;                    /* frees A struct only, not A->p, etc */
    *Ahandle = NULL ;
    return (Amatlab) ;
}
コード例 #6
0
ファイル: vintersect.cpp プロジェクト: zackattacknyu/CS274B
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
  if (nrhs != 2) mexErrMsgTxt("Takes 2 input arguments");
  int N1=mxGetN(prhs[0]); int N2=mxGetN(prhs[1]);
  if (!mxIsUint32(prhs[0]) && N1>0) mexErrMsgTxt("Arguments must be sorted uint32s");
  if (!mxIsUint32(prhs[1]) && N2>0) mexErrMsgTxt("Arguments must be sorted uint32s");

  uint32_t *src, *cmp, *dest;
  src = (uint32_t *) mxGetData(prhs[0]); 
  cmp = (uint32_t *) mxGetData(prhs[1]);
  
  plhs[0] = mxCreateNumericMatrix(1,N1,mxUINT32_CLASS,mxREAL); 
  dest=(uint32_t *) mxGetData(plhs[0]);

  uint32_t* end = std::set_intersection( src, src+N1, cmp, cmp+N2, dest );
  mxSetN(plhs[0],end-dest);
/*	
  unsigned int i,j,k;
  for (i=0,j=0,k=0; i<N1 && j<N2; ) {
    if (src[i] < cmp[j]) dest[k++]=src[i++]; 
    else if (src[i]==cmp[j]) { i++; }
    else if (src[i] > cmp[j] && j<N2) j++;
  }
  while (i<N1) dest[k++]=src[i++];
  mxSetN(plhs[0],k);
*/

}
コード例 #7
0
ファイル: shmem2mat.c プロジェクト: davidozog/PRESTO
/*  the gateway routine.  */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{

  double *num_bytes; 
  
  num_bytes = mxGetPr(prhs[0]);

  int num;
  num = (int)*num_bytes;

  /* TODO: This uses 2x the minimal necessary memory */
  UINT8_T data[num];

  get_shmem_mesg(data, num);

  UINT8_T *mesg_data;
  mwSize idx;
  mesg_data = mxCalloc(num, sizeof(UINT8_T));
  for ( idx = 0; idx < num; idx++ ) {
    mesg_data[idx] = data[idx];
  }
  plhs[0] = mxCreateNumericMatrix(0, 0, mxUINT8_CLASS, mxREAL);

  /* Point mxArray to dynamicData */
  mxSetData(plhs[0], mesg_data);
  mxSetM(plhs[0], num);
  mxSetN(plhs[0], 1);

  return;
}
コード例 #8
0
ファイル: TC_mex.cpp プロジェクト: miscco/NM_TC
mxArray* GetMexArray(int N, int M) {
    mxArray* Array	= mxCreateDoubleMatrix(0, 0, mxREAL);
    mxSetM(Array, N);
    mxSetN(Array, M);
    mxSetData(Array, mxMalloc(sizeof(double)*M*N));
    return Array;
}
コード例 #9
0
ファイル: spqr_mx.cpp プロジェクト: GHilmarG/Ua
mxArray *spqr_mx_put_sparse
(
    cholmod_sparse **Ahandle,	// CHOLMOD version of the matrix
    cholmod_common *cc
)
{
    mxArray *Amatlab ;
    cholmod_sparse *A ;
    Long nz, is_complex ;

    A = *Ahandle ;
    is_complex = (A->xtype != CHOLMOD_REAL) ;
    Amatlab = mxCreateSparse (0, 0, 0, is_complex ? mxCOMPLEX: mxREAL) ;
    mxSetM (Amatlab, A->nrow) ;
    mxSetN (Amatlab, A->ncol) ;
    mxSetNzmax (Amatlab, A->nzmax) ;
    mxFree (mxGetJc (Amatlab)) ;
    mxFree (mxGetIr (Amatlab)) ;
    mxFree (mxGetPr (Amatlab)) ;
    mxSetJc (Amatlab, (mwIndex *) A->p) ;
    mxSetIr (Amatlab, (mwIndex *) A->i) ;

    nz = cholmod_l_nnz (A, cc) ;
    put_values (nz, Amatlab, (double *) A->x, is_complex, cc) ;

    A->p = NULL ;
    A->i = NULL ;
    A->x = NULL ;
    A->z = NULL ;
    cholmod_l_free_sparse (Ahandle, cc) ;
    return (Amatlab) ;
}
コード例 #10
0
ファイル: CountmRNA.c プロジェクト: koeppllab/DPP
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
  double *rPath, *xPath, *deltamRNA;
  int offIdx, synthIdx, numGeneOns, numEvents;

 

  
  /* The input must be a noncomplex scalar double.*/
  numEvents = mxGetN(prhs[0]);
  
  /* Create matrix for the return argument. */
  plhs[0] = mxCreateDoubleMatrix(1,numEvents, mxREAL);
 
   
  /* Assign pointers to each input and output. */
  rPath = mxGetPr(prhs[0]);
  offIdx = mxGetScalar(prhs[1]);
  synthIdx = mxGetScalar(prhs[2]);

  deltamRNA = mxGetPr(plhs[0]);
  
  /* Call the timestwo subroutine. */
  numGeneOns = CountmRNA(rPath, deltamRNA, offIdx, synthIdx, numEvents);
  mxSetN(plhs[0], numGeneOns);
  
}
コード例 #11
0
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])

{
    #define spikes_out plhs[0]
    
    #define spikesA_in prhs[0]
    #define spikesB_in prhs[1]
    #define cespA_in prhs[2]
    #define cespB_in prhs[3]
    #define length_in prhs[4]
    
    int num_spikesA, num_spikesB, *length, i, j;
    float *spikesA, *spikesB, *spikes, *cespA, *cespB;
    
    spikesA = (float *)mxGetPr(spikesA_in);
    spikesB = (float *)mxGetPr(spikesB_in);
    cespA = (float *)mxGetPr(cespA_in);
    cespB = (float *)mxGetPr(cespB_in);
    
    length = (int *)mxGetPr(length_in);
    
    num_spikesA = mxGetN(spikesA_in);
    num_spikesB = mxGetN(spikesB_in); 
    
    spikes_out = mxCreateNumericArray(0, 0, mxSINGLE_CLASS, mxREAL);
    mxSetM(spikes_out, 2);
    mxSetN(spikes_out, *length);
    mxSetData(spikes_out, mxMalloc(sizeof(float) * 2 * *length));
    spikes = (float *)mxGetPr(spikes_out);      
      
    i = 0;
    j = 0;
    
    while (i + j < *length) {
        if ((spikesA[i] < spikesB[j] && i < num_spikesA)||(j == num_spikesB)) {
            spikes[2*(i+j)] = cespA[i];
            if (j > 0)
                spikes[1 + 2*(i+j)] = cespB[j-1];
            else
                spikes[1 + 2*(i+j)] = 0;
            if (i < num_spikesA)
                i++;
        }
        else {
            spikes[1 + 2*(i+j)] = cespB[j];

            if (i > 0)
                spikes[2*(i+j)] = cespA[i-1];
            else
                spikes[2*(i+j)] = 0;
            if (j < num_spikesB)
                j++;
        }

    }
      
    return;
}
コード例 #12
0
void flowToInds(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
  /* [rs,cs,is]=flowToInds(us,vs,m,n,flag); */
  int m, n, m1, n1, flag; double *us, *vs;
  int *is, ind=0, i, j, fr, fc; double *rs, *cs, r, c;

  /* extract inputs */
  us = (double*) mxGetData(prhs[0]);
  vs = (double*) mxGetData(prhs[1]);
  m  = (int) mxGetScalar(prhs[2]);
  n  = (int) mxGetScalar(prhs[3]);
  flag = (int) mxGetScalar(prhs[4]);

  /* initialize memory */
  m1=(int)mxGetM(prhs[0]); n1=(int)mxGetN(prhs[0]);
  rs  = mxMalloc(sizeof(double)*m1*n1);
  cs  = mxMalloc(sizeof(double)*m1*n1);
  is  = mxMalloc(sizeof(int)*m1*n1);

  /* clamp and compute ids according to flag */
  if( flag==1 ) { /* nearest neighbor */
    for(i=0; i<n1; i++) for(j=0; j<m1; j++) {
      r=us[ind]+j+1; r = r<1 ? 1 : (r>m ? m : r);
      c=vs[ind]+i+1; c = c<1 ? 1 : (c>n ? n : c);
      is[ind] = ((int) (r-.5)) + ((int) (c-.5)) * m; ind++;
    }
  } else if(flag==2) { /* bilinear */
    for(i=0; i<n1; i++) for(j=0; j<m1; j++) {
      r=us[ind]+j+1; r = r<2 ? 2 : (r>m-1 ? m-1 : r); fr = (int) r;
      c=vs[ind]+i+1; c = c<2 ? 2 : (c>n-1 ? n-1 : c); fc = (int) c;
      rs[ind]=r-fr; cs[ind]=c-fc; is[ind]=(fr-1)+(fc-1)*m; ind++;
    }
  } else { /* other cases - clamp only */
    for(i=0; i<n1; i++) for(j=0; j<m1; j++) {
      r=us[ind]+j+1; rs[ind] = r<2 ? 2 : (r>m-1 ? m-1 : r);
      c=vs[ind]+i+1; cs[ind] = c<2 ? 2 : (c>n-1 ? n-1 : c); ind++;
    }
  }

  /* create output array */
  plhs[0] = mxCreateNumericMatrix(0,0,mxDOUBLE_CLASS,mxREAL);
  plhs[1] = mxCreateNumericMatrix(0,0,mxDOUBLE_CLASS,mxREAL);
  plhs[2] = mxCreateNumericMatrix(0,0,mxINT32_CLASS,mxREAL);
  mxSetData(plhs[0],rs); mxSetM(plhs[0],m1); mxSetN(plhs[0],n1);
  mxSetData(plhs[1],cs); mxSetM(plhs[1],m1); mxSetN(plhs[1],n1);
  mxSetData(plhs[2],is); mxSetM(plhs[2],m1); mxSetN(plhs[2],n1);
}
コード例 #13
0
void homogsToFlow(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
  /* [us,vs]=homogsToFlow(H,M); */
  int i, j, k, m, n, q, *dims, affine=1, ind=0; unsigned int *M;
  double *H, *H1, *us, *vs; double r, c, r0, c0, z;

  /* extract inputs */
  H = (double*) mxGetData(prhs[0]);
  M = (unsigned int*) mxGetData(prhs[1]);
  m = (int) mxGetM(prhs[1]); n = (int) mxGetN(prhs[1]);
  if(mxGetNumberOfDimensions(prhs[0])==2) q=1; else {
    dims=(int*) mxGetDimensions(prhs[0]); q=dims[2]; }

  /* initialize memory */
  us = mxMalloc(sizeof(double)*m*n);
  vs = mxMalloc(sizeof(double)*m*n);

  /* Compute flow at each grid point */
  r0=(-m+1.0)/2.0; c0=(-n+1.0)/2.0;
  for( k=0; k<q; k++ ) {
    H1=H+k*9; for(i=0; i<9; i++) H1[i]=H1[i]/H1[8];
    affine=affine && H1[2]==0 && H1[5]==0;
  }
  if( affine ) {
    for( i=0; i<n; i++ ) for(j=0; j<m; j++) {
      k=M[ind]; H1=H+k*9; r=r0+j; c=c0+i;
      us[ind] = H1[0]*r + H1[3]*c + H1[6] - r;
      vs[ind] = H1[1]*r + H1[4]*c + H1[7] - c;
      ind++;
    }
  } else {
    for( i=0; i<n; i++ ) for(j=0; j<m; j++) {
      k=M[ind]; H1=H+k*9; r=r0+j; c=c0+i;
      us[ind] = H1[0]*r + H1[3]*c + H1[6];
      vs[ind] = H1[1]*r + H1[4]*c + H1[7];
      z       = H1[2]*r + H1[5]*c + 1;
      us[ind]=us[ind]/z-r; vs[ind]=vs[ind]/z-c; ind++;
    }
  }

  /* create output array */
  plhs[0] = mxCreateNumericMatrix(0,0,mxDOUBLE_CLASS,mxREAL);
  plhs[1] = mxCreateNumericMatrix(0,0,mxDOUBLE_CLASS,mxREAL);
  mxSetData(plhs[0],us); mxSetM(plhs[0],m); mxSetN(plhs[0],n);
  mxSetData(plhs[1],vs); mxSetM(plhs[1],m); mxSetN(plhs[1],n);
}
コード例 #14
0
void mexFunction(
	int nlhs, mxArray *plhs[], 
	int nrhs, const mxArray *prhs[]
) {
	
//--
// DECLARE
//--
	
double  *x, *y, a, b; int x_nr_rows, x_nr_columns;

double  *mu, *var, *gamma;

int m;

//--
// HANDLE INPUT
//--
 
 /* GATEWAY */

 if (nrhs!=4 || nlhs>1)
    mexErrMsgTxt("GGFIT: Supply arguments\nSyntax: Y = GENGG(X,SHAPE,DEV,MEAN)\n");

 x_nr_rows = mxGetM(prhs[0]);
 x_nr_columns = mxGetN(prhs[0]);
 if (x_nr_rows >= x_nr_columns)
   {
   x_nr_columns = 1;
   }
   else
   {
   x_nr_rows = 1;
   }

 /* match inputs */
 x = mxGetPr(prhs[0]);       /* Complex parts ignored */
 gamma = mxGetPr(prhs[1]);   /* Complex parts ignored */
 var = mxGetPr(prhs[2]);     /* Complex parts ignored */
 mu = mxGetPr(prhs[3]);      /* Complex parts ignored */

 /* match outputs */
 y = mxCalloc(x_nr_rows*x_nr_columns,sizeof(double));
 plhs[0] = mxCreateDoubleMatrix(x_nr_rows, x_nr_columns, mxREAL);
 mxSetM(plhs[0],x_nr_rows);
 mxSetN(plhs[0],x_nr_columns);
 mxSetPr(plhs[0],y);

 /* CALCULATION OF GENERALISED GAUSSIAN */
 for (m=0; m<(x_nr_rows*x_nr_columns); m++)
   {
   b = pow( ( gammaHJB(3/(*gamma))/gammaHJB(1/(*gamma)) ),0.5 )/(*var);
   a = b*(*gamma)/(2*gammaHJB(1/(*gamma)));
   y[m] = a*exp( -pow( fabs(b*(x[m]- *mu)), *gamma ) );
 } /* for */

} /* main */
コード例 #15
0
ファイル: gen_mx.c プロジェクト: Field-Lab/matlab
mxArray *mxCreateEmptyMatrix(void)
{
  mxArray *out;

  out = mxCreateDoubleScalar(0.0);
  mxSetM(out,0);
  mxSetN(out,0);

  return out;
}
コード例 #16
0
ファイル: mexHelperBLAS.c プロジェクト: hosna/m-files
int mfiles_cs2mx(const cs *in, mxArray *out) {
    if (!out) {
        return EXIT_FAILURE;
    }
    mxSetM(out, in->m);
    mxSetN(out, in->n);
    mxSetNzmax(out, in->nzmax);
    mxSetJc(out, (mwIndex *) in->p);
    mxSetIr(out, (mwIndex *) in->i);
    mxSetPr(out, in->x);
    return EXIT_SUCCESS;
}
コード例 #17
0
ファイル: mexutil.c プロジェクト: AlphaJi/pmtksupport
mxArray *mxCreateNumericMatrixE(mwSize m, mwSize n, mxClassID classid, 
				mxComplexity ComplexFlag)
{
  mwSize sz = m*n*sizeof(double);
  mxArray *a = mxCreateNumericMatrix(1, 1, classid, ComplexFlag);
  mxSetM(a,m);
  mxSetN(a,n);
  mxSetPr(a, mxRealloc(mxGetPr(a),sz));
  if(ComplexFlag == mxCOMPLEX) {
    mxSetPi(a, mxRealloc(mxGetPi(a),sz));
  }
  return a;
}
コード例 #18
0
int csimMexCreate(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{ 
  /*
  ** Create some fresh empty network
  */
  if ( !TheNetwork ) TheNetwork = new MexNetwork();
  
  if ( nrhs < 2 || nrhs > 3 || nlhs != 1 )
    mexErrMsgTxt("CSIM-Usage: idx = csim('create',className[,n]);\n");

  char *className;
  if ( getString(prhs[1],&className) )
    mexErrMsgTxt("CSIM-Usage: idx = csim('create',className[,n]); className not a string.\n");

  int n=1; 
  if ( nrhs > 2 ) {
    double tmp;
    if ( getDouble(prhs[2],&tmp) )
      mexErrMsgTxt("CSIM-Usage: idx = csim('create',className[,n]); n not a single double.\n");
  
    n = (int)(tmp);
  }

  uint32 *idx = (uint32 *)mxCalloc(n,sizeof(uint32));
 
  int switchError = 0;
  //  #define __SWITCH_COMMAND__ { for(int i=0; i<n; i++) if ( (idx[i]=TheNetwork->addNewObject((Advancable *)(new TYPE))) < 0 ) { TheCsimError.add("can not add %s (i=%i)\n",className,i); return -1; } }
  Advancable *a; a=0;
#define __SWITCH_COMMAND__ { for(int i=0; i<n; i++) { \
                               idx[i]=TheNetwork->addNewObject(a=(Advancable *)(new TYPE)); \
                               if ( a->init(a) < 0 ) { \
                                 TheCsimError.add("csim('create',className,n): error calling init of %s!\n",className); \
                                 return -1; \
                               } \
                             } \
                           }
#include "switch.i"

  if ( switchError  < 0 ) {
    TheCsimError.add("csim('create',className,n): Unknown className %s!\n",className);
    return -1;
  } else {
    plhs[0] = mxCreateNumericMatrix ( 0, 0, mxUINT32_CLASS, mxREAL );
    mxSetM(plhs[0],1);
    mxSetN(plhs[0],n);
    mxSetData(plhs[0],(void *)idx);
  }
  
  return 0; 

}
コード例 #19
0
ファイル: ConnectivityCheck.c プロジェクト: JWCS/ML-CVtest2
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
    //Input is unit8 matrix. Output is same size uint8 matrix to delete
    //Acquire and comfirm inputs/outputs
    //Pad with zeros around
    //loop through elements, pull the local 3x3, perform connectivity check
    //write to delete as 1 true, leave alone as 0 false
    #define A_IN prhs[0]
    #define X_OUT plhs[0]
    mxArray *A, *X, *PAD, *padSize, *toPad[2]; 
    padSize = mxCreateNumericMatrix( 1, 2, mxUINT8_CLASS, mxREAL );
    int M, N, m, n, *vals;
    vals[0] = 1; vals[1] = 1; mxSetData( padSize, vals );
    
    if( nrhs != 1 )
        mexErrMsgTxt("Wrong number of input arguments.");
    else if( nlhs != 1 )
        mexErrMsgTxt("Wrong number of output arguments.");
    if( !IS_REAL_2D_FULL_UINT8( A_IN ) )
        mexErrMsgTxt("Input must be uint8, not sparse, and 2D real.");
    
    M = mxGetM(A_IN);
    N = mxGetN(A_IN);
    A = (mxArray*)mxGetPr(A_IN);
    X = (mxArray*)mxGetPr(X_OUT);
    X = mxCreateNumericMatrix( 0, 0, mxUINT8_CLASS, mxREAL );
    mxSetM( X, M+2 );
    mxSetN( X, N+2 );
    mxSetData( X, mxMalloc( sizeof(uint8_t)*(M+2)*(N+2) ) );
    PAD = mxCreateNumericMatrix( 0, 0, mxUINT8_CLASS, mxREAL );
    mxSetM( PAD, M+2 );
    mxSetN( PAD, N+2 );
    mxSetData( PAD, mxMalloc( sizeof(uint8_t)*(M+2)*(N+2) ) );
    toPad[0] = A; toPad[1] = padSize;
    mexCallMATLAB( 1, &PAD, 2, toPad, "padarray" );
    memcpy( mxGetPr(X), PAD, sizeof(uint8_t)*(M+2)*(N+2) );
    
    return;
}
コード例 #20
0
ファイル: TC_mex.cpp プロジェクト: miscco/NM_TC
mxArray* get_marker(Stim &stim) {
    extern const int red;
    mxArray* marker	= mxCreateDoubleMatrix(0, 0, mxREAL);
    mxSetM(marker, 1);
    mxSetN(marker, stim.marker_stimulation.size());
    mxSetData(marker, mxMalloc(sizeof(double)*stim.marker_stimulation.size()));
    double* Pr_Marker = mxGetPr(marker);
    unsigned counter  = 0;
    /* Division by res transforms marker time from dt to sampling rate */
    for(auto & elem : stim.marker_stimulation) {
        Pr_Marker[counter++] = elem/red;
    }
    return marker;
}
コード例 #21
0
ファイル: uuid.cpp プロジェクト: jackjallen/MATLAB2
/*
 * Main entry
 */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    char uidstr[40];
    uuidgen(uidstr);

    const char *s = uidstr;
    const char **ps = &s;

    plhs[0] = mxCreateCharMatrixFromStrings(1, ps);
    
    if( nrhs == 1 ){
      mxSetN( plhs[0] , *(mxGetPr(prhs[0])) );
    }
}
コード例 #22
0
ファイル: ode.c プロジェクト: nvanriel/ADAPT
realtype    *reAllocate2DOutputMemory( realtype *pMem, void *cvode_mem, N_Vector y0, mxArray *plhs, mwSize dim1, mwSize dim2 ) {
    void            *pOld;
    
    pOld  = pMem;
    pMem  = mxRealloc( pMem, sizeof( realtype ) * dim1 * dim2 );
    
    if ( pMem == NULL ) memErr( cvode_mem, y0, pOld, "ERROR: Insufficient memory for reallocation during simulation loop!" );
    
    mxSetPr( plhs, pMem );
    mxSetM( plhs, dim1 );
    mxSetN( plhs, dim2 );

    return pMem;
}
コード例 #23
0
ファイル: deleteLastCol.c プロジェクト: ArcadiusK/openAlgo
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	/*
	mwSize is a type that represents size values, such as array dimensions.
	mxGetN Call mxGetN to determine the number of columns in the specified mxArray. 
	plhs <-- "left hand side"
	prhs <-- "right hand side"
	http://cnx.org/content/m12348/latest/ (mex in general)
	http://www.mathworks.com/help/matlab/apiref/mwsize.html (mwSize)
	http://www.mathworks.com/help/matlab/apiref/mxgetn.html?searchHighlight=mxgetn (mxGetN)
	
	The first line assigns n as a variable that will hold array dimensions
	'prhs[0] represents the input array because it is the '0' (first) element input in the function
	*/
    mwSize n;
    if( n = mxGetN(prhs[0]) )
        mxSetN(prhs[0], n - 1);
}
コード例 #24
0
mxArray *sfmult_yalloc	// return Y
(
    Int m,
    Int n,
    int Ycomplex	// true if Y is complex
)
{
    // (TO DO): guard against integer overflow
    mxArray *Y = mxCreateDoubleMatrix (0, 0, Ycomplex ? mxCOMPLEX : mxREAL) ;
    MXFREE (mxGetPr (Y)) ;
    mxSetPr (Y, mxMalloc (MAX (m*n, 1) * sizeof (double))) ;
    if (Ycomplex)
    {
	MXFREE (mxGetPi (Y)) ;
	mxSetPi (Y, mxMalloc (MAX (m*n, 1) * sizeof (double))) ;
    }
    mxSetM (Y, m) ;
    mxSetN (Y, n) ;
    return (Y) ;
}
コード例 #25
0
ファイル: cs_mex.c プロジェクト: Al-th/matlab
/* return a real sparse matrix to MATLAB */
mxArray *cs_dl_mex_put_sparse (cs_dl **Ahandle)
{
    cs_dl *A ;
    mxArray *Amatlab ;
    A = *Ahandle ;
    if (!A) mexErrMsgTxt ("failed") ;
    Amatlab = mxCreateSparse (0, 0, 0, mxREAL) ;
    mxSetM (Amatlab, A->m) ;
    mxSetN (Amatlab, A->n) ;
    mxSetNzmax (Amatlab, A->nzmax) ;
    cs_free (mxGetJc (Amatlab)) ;
    cs_free (mxGetIr (Amatlab)) ;
    cs_free (mxGetPr (Amatlab)) ;
    mxSetJc (Amatlab, (void *) (A->p)) ; /* assign A->p pointer to MATLAB A */
    mxSetIr (Amatlab, (void *) (A->i)) ;
    mxSetPr (Amatlab, A->x) ;
    cs_free (A) ;                       /* frees A struct only, not A->p, etc */
    *Ahandle = NULL ;
    return (Amatlab) ;
}
コード例 #26
0
ファイル: spqr_mx.cpp プロジェクト: GHilmarG/Ua
mxArray *spqr_mx_put_dense2
(
    Long m,
    Long n,
    double *Ax,         // size nz if real; size 2*nz if complex
    int is_complex,
    cholmod_common *cc
)
{
    mxArray *A ;

    if (cc == NULL) return (NULL) ;

    A = mxCreateDoubleMatrix (0, 0, is_complex ? mxCOMPLEX : mxREAL) ;
    mxSetM (A, m) ;
    mxSetN (A, n) ; 
    mxFree (mxGetPr (A)) ;
    mxFree (mxGetPi (A)) ;
    put_values (m*n, A, Ax, is_complex, cc) ;
    return (A) ;
}
コード例 #27
0
/* return a sparse matrix to MATLAB */
mxArray *cs_mex_put_sparse (cs **Ahandle)
{
    cs *A ;
    mxArray *Amatlab ;
    A = *Ahandle ;
    Amatlab = mxCreateSparse (0, 0, 0, mxREAL) ;
    mxSetM (Amatlab, A->m) ;
    mxSetN (Amatlab, A->n) ;
    mxSetNzmax (Amatlab, A->nzmax) ;
    cs_free (mxGetJc (Amatlab)) ;
    cs_free (mxGetIr (Amatlab)) ;
    cs_free (mxGetPr (Amatlab)) ;
    mxSetJc (Amatlab, A->p) ;           /* assign A->p pointer to MATLAB A */
    mxSetIr (Amatlab, A->i) ;
    mxSetPr (Amatlab, A->x) ;
    mexMakeMemoryPersistent (A->p) ;    /* ensure MATLAB does not free A->p */
    mexMakeMemoryPersistent (A->i) ;
    mexMakeMemoryPersistent (A->x) ;
    cs_free (A) ;                       /* frees A struct only, not A->p, etc */
    *Ahandle = NULL ;
    return (Amatlab) ;
}
コード例 #28
0
ファイル: releaseinplace.c プロジェクト: 2php/lrslibrary
/* Gateway of releaseinplace */
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[]) {
    
    /* Check arguments */
    if (nrhs!=1)
        mexErrMsgTxt("RELEASEINPLACE: One input argument required.");
    
    if( nlhs != 0 ) {
        mexErrMsgTxt("RELEASEINPLACE: Zero output arguments required.");
    }
    
    mxSetM((mxArray *)prhs[0], 0);
    mxSetN((mxArray *)prhs[0], 0);
    /* Equivalent to doing this:  mxSetPr(prhs[0], NULL); 
       but access directly to data pointer in order to by pass Matlab
       checking - Thanks to James Tursa */
    ((Internal_mxArray*)(prhs[0]))->data.number_array.pdata = NULL;
    ((Internal_mxArray*)(prhs[0]))->data.number_array.pimag_data = NULL; 
      
    return;

} /* Gateway of releaseinplace.c */ 
コード例 #29
0
ファイル: eigK.c プロジェクト: HongliangZhou/protein
/* ************************************************************
   PROCEDURE mexFunction - Entry for Matlab
     [lab,q] = eigK(x,K)
     Computes spectral coefficients of x w.r.t. K
   REMARK If this function is used internally by SeDuMi, then
     complex numbers are stored in a single real vector. To make
     it invokable from the Matlab command-line by the user, we
     also allow Matlab complex vector x.
   ************************************************************ */
void mexFunction(const int nlhs, mxArray *plhs[],
  const int nrhs, const mxArray *prhs[])
{
 mxArray *output_array[3], *Xk, *hXk;
 coneK cK;
 int k, nk, nksqr, lendiag,i,ii,nkp1, lenfull;
 double *lab,*q,*qpi,*labk,*xwork,*xpiwork;
 const double *x,*xpi;

/* ------------------------------------------------------------
   Check for proper number of arguments
   ------------------------------------------------------------ */
  mxAssert(nrhs >= NPARIN, "eigK requires more input arguments");
  mxAssert(nlhs <= NPAROUT, "eigK produces less output arguments");
/* ------------------------------------------------------------
   Disassemble cone K structure
   ------------------------------------------------------------ */
  conepars(K_IN, &cK);
/* ------------------------------------------------------------
   Compute statistics based on cone K structure
   ------------------------------------------------------------ */
  lendiag = cK.lpN + 2 * (cK.lorN + cK.rconeN) + cK.rLen + cK.hLen;
  lenfull = cK.lpN + cK.qDim + cK.rDim + cK.hDim;
  if(cK.rconeN > 0)
    for(i = 0; i < cK.rconeN; i++)
      lenfull += cK.rconeNL[i];
/* ------------------------------------------------------------
   Get input vector x
   ------------------------------------------------------------ */
  mxAssert(mxGetM(X_IN) * mxGetN(X_IN) == lenfull, "Size mismatch x");
  mxAssert(!mxIsSparse(X_IN), "x must be full (not sparse).");
  x = mxGetPr(X_IN);
  if(mxIsComplex(X_IN))
    xpi = mxGetPi(X_IN) + cK.lpN;
/* ------------------------------------------------------------
   Allocate output LAB(diag), eigvec Q(full for psd)
   ------------------------------------------------------------ */
  LAB_OUT = mxCreateDoubleMatrix(lendiag, 1, mxREAL);
  lab = mxGetPr(LAB_OUT);
  if(nlhs > 1){
    if(mxIsComplex(X_IN)){
      Q_OUT = mxCreateDoubleMatrix(cK.rDim, 1, mxCOMPLEX);
      qpi = mxGetPi(Q_OUT);
    }
    else
      Q_OUT = mxCreateDoubleMatrix(cK.rDim + cK.hDim, 1, mxREAL);
    q = mxGetPr(Q_OUT);
  }
/* ------------------------------------------------------------
   Allocate working arrays:
   ------------------------------------------------------------ */
  Xk = mxCreateDoubleMatrix(0,0,mxREAL);
  hXk = mxCreateDoubleMatrix(0,0,mxCOMPLEX);
  if(mxIsComplex(X_IN)){
    xwork = (double *) mxCalloc(MAX(1,2 * SQR(cK.rMaxn)), sizeof(double));
    xpiwork = xwork + SQR(cK.rMaxn);
  }
  else
    xwork =(double *) mxCalloc(MAX(1,SQR(cK.rMaxn)+2*SQR(cK.hMaxn)),
                               sizeof(double));
/* ------------------------------------------------------------
   The actual job is done here:.
   ------------------------------------------------------------ */
  if(cK.lpN){
/* ------------------------------------------------------------
   LP: lab = x
   ------------------------------------------------------------ */
    memcpy(lab, x, cK.lpN * sizeof(double));
    lab += cK.lpN; x += cK.lpN;
  }
/* ------------------------------------------------------------
   CONSIDER FIRST MATLAB-REAL-TYPE:
   ------------------------------------------------------------ */
  if(!mxIsComplex(X_IN)){                  /* Not Matlab-type complex */
/* ------------------------------------------------------------
   LORENTZ:  (I) lab = qeig(x)
   ------------------------------------------------------------ */
    for(k = 0; k < cK.lorN; k++){
      nk = cK.lorNL[k];
      qeig(lab,x,nk);
      lab += 2; x += nk;
    }
/* ------------------------------------------------------------
   RCONE: LAB = eig(X)     (Lorentz-Rcone's are not used internally)
   ------------------------------------------------------------ */
    for(k = 0; k < cK.rconeN; k++){
      nk = cK.rconeNL[k];
      rconeeig(lab,x[0],x[1],realssqr(x+2,nk-2));
      lab += 2; x += nk;
    }
/* ------------------------------------------------------------
   PSD: (I) LAB = eig(X)
   ------------------------------------------------------------ */
    if(nlhs < 2){
      for(k=0; k < cK.rsdpN; k++){                /* real symmetric */
        nk = cK.sdpNL[k];
        symproj(xwork,x,nk);              /* make it symmetric */
        mxSetM(Xk, nk);
        mxSetN(Xk, nk);
        mxSetPr(Xk, xwork);
        mexCallMATLAB(1, output_array, 1, &Xk, "eig");
        memcpy(lab, mxGetPr(output_array[0]), nk * sizeof(double));
/* ------------------------------------------------------------
   With mexCallMATLAB, we invoked the mexFunction "eig", which
   allocates a matrix struct *output_array[0], AND a block for the
   float data of that matrix.
   ==> mxDestroyArray() does not only free the float data, it
   also releases the matrix struct (and this is what we want).
   ------------------------------------------------------------ */
        mxDestroyArray(output_array[0]);
        lab += nk;  x += SQR(nk);
      }
/* ------------------------------------------------------------
   WARNING: Matlab's eig doesn't recognize Hermitian, hence VERY slow
   ------------------------------------------------------------ */
      for(; k < cK.sdpN; k++){                    /* complex Hermitian */
        nk = cK.sdpNL[k]; nksqr = SQR(nk);
        symproj(xwork,x,nk);              /* make it Hermitian */
        skewproj(xwork + nksqr,x+nksqr,nk);
        mxSetM(hXk, nk);
        mxSetN(hXk, nk);
        mxSetPr(hXk, xwork);
        mxSetPi(hXk, xwork + nksqr);     
        mexCallMATLAB(1, output_array, 1, &hXk, "eig");
        memcpy(lab, mxGetPr(output_array[0]), nk * sizeof(double));
        mxDestroyArray(output_array[0]);
        lab += nk;  x += 2 * nksqr;
      }
    }
    else{
/* ------------------------------------------------------------
   SDP: (II) (Q,LAB) = eig(X)
   ------------------------------------------------------------ */
      for(k=0; k < cK.rsdpN; k++){                /* real symmetric */
        nk = cK.sdpNL[k];
        symproj(xwork,x,nk);                      /* make it symmetric */
        mxSetM(Xk, nk);
        mxSetN(Xk, nk);
        mxSetPr(Xk, xwork);
        mexCallMATLAB(2, output_array, 1, &Xk, "eig");
        nksqr = SQR(nk);                                  /* copy Q-matrix */
        memcpy(q, mxGetPr(output_array[0]), nksqr * sizeof(double));
        nkp1 = nk + 1;                                   /* copy diag(Lab) */
        labk = mxGetPr(output_array[1]);
        for(i = 0, ii = 0; i < nk; i++, ii += nkp1)
          lab[i] = labk[ii];
        mxDestroyArray(output_array[0]);
        mxDestroyArray(output_array[1]);
        lab += nk;  x += nksqr; q += nksqr;
      }
      for(; k < cK.sdpN; k++){                    /* complex Hermitian */
        nk = cK.sdpNL[k]; nksqr = SQR(nk);
        symproj(xwork,x,nk);                      /* make it Hermitian */
        skewproj(xwork + nksqr,x+nksqr,nk);
        mxSetM(hXk, nk);
        mxSetN(hXk, nk);
        mxSetPr(hXk, xwork);
        mxSetPi(hXk, xwork+nksqr);
        mexCallMATLAB(2, output_array, 1, &hXk, "eig");
        memcpy(q, mxGetPr(output_array[0]), nksqr * sizeof(double));
        q += nksqr;
        if(mxIsComplex(output_array[0]))     /* if any imaginary part */
          memcpy(q, mxGetPi(output_array[0]), nksqr * sizeof(double));
        nkp1 = nk + 1;                              /* copy diag(Lab) */
        labk = mxGetPr(output_array[1]);
        for(i = 0, ii = 0; i < nk; i++, ii += nkp1)
          lab[i] = labk[ii];
        mxDestroyArray(output_array[0]);
        mxDestroyArray(output_array[1]);
        lab += nk;  x += 2 * nksqr; q += nksqr;
      }
    } /* [lab,q] = eigK */
  } /* !iscomplex */
  else{              /* is MATLAB type complex */
/* ------------------------------------------------------------
   LORENTZ:  (I) lab = qeig(x)
   ------------------------------------------------------------ */
    for(k = 0; k < cK.lorN; k++){
      nk = cK.lorNL[k];
      cxqeig(lab,x,xpi,nk);
      lab += 2; x += nk; xpi += nk;
    }
/* ------------------------------------------------------------
   RCONE: LAB = eig(X)     (Lorentz-Rcone's are not used internally)
   ------------------------------------------------------------ */
    for(k = 0; k < cK.rconeN; k++){
      nk = cK.rconeNL[k];
      rconeeig(lab,x[0],x[1],
               realssqr(x+2,nk-2) + realssqr(xpi+2,nk-2));
      lab += 2; x += nk; xpi += nk;
    }
/* ------------------------------------------------------------
   PSD: (I) LAB = eig(X)
   ------------------------------------------------------------ */
    for(k = 0; k < cK.sdpN; k++){
      nk = cK.sdpNL[k]; nksqr = SQR(nk);
      symproj(xwork,x,nk);              /* make it Hermitian */
      skewproj(xpiwork,xpi,nk);
      mxSetM(hXk, nk);
      mxSetN(hXk, nk);
      mxSetPr(hXk, xwork);
      mxSetPi(hXk, xpiwork);     
      if(nlhs < 2){
        mexCallMATLAB(1, output_array, 1, &hXk, "eig");
        memcpy(lab, mxGetPr(output_array[0]), nk * sizeof(double));
      }
      else{
        mexCallMATLAB(2, output_array, 1, &hXk, "eig");
        memcpy(q, mxGetPr(output_array[0]), nksqr * sizeof(double));
        if(mxIsComplex(output_array[0]))     /* if any imaginary part */
          memcpy(qpi, mxGetPi(output_array[0]), nksqr * sizeof(double));
        nkp1 = nk + 1;                              /* copy diag(Lab) */
        labk = mxGetPr(output_array[1]);
        for(i = 0, ii = 0; i < nk; i++, ii += nkp1)
          lab[i] = labk[ii];
        mxDestroyArray(output_array[1]);
        q += nksqr; qpi += nksqr;
      }
      mxDestroyArray(output_array[0]);
      lab += nk;  x += nksqr; xpi += nksqr;
    }
  } /* iscomplex */
/* ------------------------------------------------------------
   Release PSD-working arrays.
   ------------------------------------------------------------ */
  mxSetM(Xk,0); mxSetN(Xk,0); 
  mxSetPr(Xk, (double *) NULL);
  mxDestroyArray(Xk);
  mxSetM(hXk,0); mxSetN(hXk,0); 
  mxSetPr(hXk, (double *) NULL);   mxSetPi(hXk, (double *) NULL);
  mxDestroyArray(hXk);
  mxFree(xwork);
}
コード例 #30
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    const mxArray *MatlabFunctionHandle;
    direct_algorithm theAlgorithm=DIRECT_ORIGINAL;
    const direct_objective_func f=&MatlabCallback;
    size_t numDim;
    double *lowerBounds;
    double *upperBounds;
    int maxFEval=500;
    int maxIter=100;
    double epsilon=1e-4;
    double epsilonAbs=0;
    double volumeRelTol=1e-10;
    double sigmaRelTol=0;
    double fGlobal=DIRECT_UNKNOWN_FGLOBAL;
    double fGlobalRelTol=DIRECT_UNKNOWN_FGLOBAL;
    int maxDiv=5000;
    //To hold return values
    direct_return_code retVal;
    double *x;
    double fVal;
    
    if(nrhs<3||nrhs>4){
        mexErrMsgTxt("Wrong number of inputs");
    }

    if(nlhs>3) {
        mexErrMsgTxt("Wrong number of outputs.");
    }
    
    //Check that a function handle was passed.
    if(!mxIsClass(prhs[0],"function_handle")) {
        mexErrMsgTxt("The first input must be a function handle.");
    }
    MatlabFunctionHandle=prhs[0];
    
    //Check the lower and upper bounds
    checkRealDoubleArray(prhs[1]);
    checkRealDoubleArray(prhs[2]);
    numDim=mxGetM(prhs[1]);
    if(numDim<1||mxGetN(prhs[1])!=1) {
        mexErrMsgTxt("The lower bounds have the wrong dimensionality.");
    }
    if(mxGetM(prhs[2])!=numDim||mxGetN(prhs[1])!=1) {
        mexErrMsgTxt("The upper bounds have the wrong dimensionality.");
    }
    
    //The function in the library only uses integers for dimensions.
    if(numDim>INT_MAX) {
        mexErrMsgTxt("The problem has too many dimensions to be solved using this function.");
    }
    
    lowerBounds=(double*)mxGetData(prhs[1]);
    upperBounds=(double*)mxGetData(prhs[2]);
    
    //If a structure of options was given
    if(nrhs>3&&!mxIsEmpty(prhs[3])) {
        //We have to check for every possible structure member.
        mxArray *theField;
        
        if(!mxIsStruct(prhs[3])) {
            mexErrMsgTxt("The options must be given in a structure.");
        }
    
        theField=mxGetField(prhs[3],0,"algorithm");
        
        if(theField!=NULL) {//If the field is present.
            int algType=getIntFromMatlab(theField);
            
            //Algorithm type
            switch(algType) {
              case 0:
                  theAlgorithm=DIRECT_ORIGINAL;
                  break;
              case 1:
                  theAlgorithm=DIRECT_GABLONSKY;
                  break;
              default:
                  mexErrMsgTxt("Invalid algorithm specified");
            }
        }
        
        theField=mxGetField(prhs[3],0,"maxDiv");
        if(theField!=NULL) {//If the field is present.
            maxDiv=getIntFromMatlab(theField);
            if(maxDiv<1) {
                mexErrMsgTxt("Invalid maxDiv specified");
            }
        }
        
        theField=mxGetField(prhs[3],0,"maxIter");
        if(theField!=NULL) {//If the field is present.
            maxIter=getIntFromMatlab(theField);
            if(maxIter<1) {
                mexErrMsgTxt("Invalid maxIter specified");
            }
        }
        
        theField=mxGetField(prhs[3],0,"maxFEval");
        if(theField!=NULL) {//If the field is present.
            maxFEval=getIntFromMatlab(theField);
            if(maxIter<1) {
                mexErrMsgTxt("Invalid maxFEval specified");
            }
        }
        
        theField=mxGetField(prhs[3],0,"epsilon");
        if(theField!=NULL) {//If the field is present.
            epsilon=getDoubleFromMatlab(theField);
            if(fabs(epsilon)>=1) {
                mexErrMsgTxt("Invalid epsilon specified");
            }
        }
        
        theField=mxGetField(prhs[3],0,"epsilonAbs");
        if(theField!=NULL) {//If the field is present.
            epsilonAbs=getDoubleFromMatlab(theField);
            if(epsilonAbs>=1||epsilonAbs<0) {
                mexErrMsgTxt("Invalid epsilonAbs specified");
            }
        }
        
        theField=mxGetField(prhs[3],0,"volumeRelTol");
        if(theField!=NULL) {//If the field is present.
            volumeRelTol=getDoubleFromMatlab(theField);
            if(volumeRelTol>=1||volumeRelTol<0) {
                mexErrMsgTxt("Invalid volumeRelTol specified");
            }
        }
        
        theField=mxGetField(prhs[3],0,"sigmaRelTol");
        if(theField!=NULL) {//If the field is present.
            sigmaRelTol=getDoubleFromMatlab(theField);
            if(sigmaRelTol>=1||sigmaRelTol<0) {
                mexErrMsgTxt("Invalid sigmaRelTol specified");
            }
        }
        
        theField=mxGetField(prhs[3],0,"fGlobal");
        if(theField!=NULL) {//If the field is present.
            fGlobal=getDoubleFromMatlab(theField);
            fGlobalRelTol=1e-12;//Default value if fGlobal is given.
        }
        
        theField=mxGetField(prhs[3],0,"fGlobalRelTol");
        if(theField!=NULL) {//If the field is present.
            fGlobalRelTol=getDoubleFromMatlab(theField);
            if(fGlobalRelTol<0) {
                mexErrMsgTxt("Invalid fGlobalRelTol specified");
            }
        }      
    }
    
    //Allocate space for the return values
    x=mxCalloc(numDim, sizeof(double));

    retVal=direct_optimize(f,//Objective function pointer
            (void*)MatlabFunctionHandle,//Data that passed to the objective function. Here, the function handle passed by the user.
            (int)numDim,//The dimensionality of the problem
            lowerBounds,//Array of the lower bound of each dimension.
            upperBounds,//Array of the upper bound of each dimension.
            x,//An array that will be set to the optimum value on return.
            &fVal,//On return, set to the minimum value.
            maxFEval,//Maximum number of function evaluations.
            maxIter,//Maximum number of iterations
            epsilon,//Jones' epsilon parameter (1e-4 is recommended)
            epsilonAbs,//An absolute version of magic_eps
            //Relative tolerance on the hypercube volume (use 0 if none). This is
            //a percentage (0-1) of the volume of the original hypercube
            volumeRelTol,
            //Relative tolerance on the hypercube measure (0 if none)
            sigmaRelTol,
            //A pointer to a variable that can terminate the optimization. This is
            //in the library's code so that an external thread can write to this
            //and force the optimization to stop. Here, we are not using it, so we
            //can pass NULL.
            NULL,
            fGlobal,//Function value of the global optimum, if known. Use DIRECT_UNKNOWN_FGLOBAL if unknown.
            fGlobalRelTol,//Relative tolerance for convergence if fglobal is known. If unknown, use DIRECT_UNKNOWN_FGLOBAL.
            NULL,//There is no output to a file.
            theAlgorithm,//The algorithm to use
            maxDiv);//The maximum number of hyperrectangle divisions

    //If an error occurred
    if(retVal<0) {
        mxFree(x);
        switch(retVal){
            case DIRECT_INVALID_BOUNDS:
                mexErrMsgTxt("Upper bounds are <= lower bounds for one or more dimensions.");
            case DIRECT_MAXFEVAL_TOOBIG:
                mexErrMsgTxt("maxFEval is too large.");
            case DIRECT_INIT_FAILED:
                mexErrMsgTxt("An initialization step failed.");
            case DIRECT_SAMPLEPOINTS_FAILED:
                 mexErrMsgTxt("An error occurred creating sampling points.");
            case DIRECT_SAMPLE_FAILED:
                mexErrMsgTxt("An error occurred while sampling the function.");
            case DIRECT_OUT_OF_MEMORY:
                mexErrMsgTxt("Out of memory");
            case DIRECT_INVALID_ARGS:
                mexErrMsgTxt("Invalid arguments provided");
            //These errors should not occur either because they should
            //never be called or because retVal>=0 so these are not
            //actually errors.
            case DIRECT_FORCED_STOP:
            case DIRECT_MAXFEVAL_EXCEEDED:
            case DIRECT_MAXITER_EXCEEDED:
            case DIRECT_GLOBAL_FOUND:
            case DIRECT_VOLTOL:
            case DIRECT_SIGMATOL:
            case DIRECT_MAXTIME_EXCEEDED:
            default:
                mexErrMsgTxt("An unknown error occurred.");
            
        }
    }
    
    //Set the return values
    plhs[0]=mxCreateNumericMatrix(0, 0, mxDOUBLE_CLASS, mxREAL);
    mxFree(mxGetPr(plhs[0]));
    mxSetPr(plhs[0], x);
    mxSetM(plhs[0], numDim);
    mxSetN(plhs[0], 1);

    if(nlhs>1) {
        plhs[1]=doubleMat2Matlab(&fVal,1,1);
        if(nlhs>2) {
            plhs[2]=intMat2MatlabDoubles(&retVal,1,1);
        }
    }
}