コード例 #1
0
ファイル: cddmex.c プロジェクト: zhihan/checkmate
void 
file_ine(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
/* Ine file input, V output, similar to extreme */
{
    dd_PolyhedraPtr poly;
    dd_MatrixPtr M;
    dd_ErrorType err;
    char *inputfile;
    FILE *reading=NULL;
    dd_MatrixPtr A, G;
    dd_SetFamilyPtr GI,GA;
    int buflen, status;
    
    dd_set_global_constants();  /* First, this must be called. */
    if (nrhs  == 1 && nlhs <=2 && mxIsChar(prhs[0])) {
        /*  dd_SetInputFile(&reading,inputfile, &err); */
        buflen = mxGetN(prhs[0]) + 1;
        inputfile= mxCalloc(buflen, sizeof(char));
        status = mxGetString(prhs[0], inputfile, buflen);
        if ( (reading = fopen(inputfile,"r") )== NULL) {
            mxErrMsgTxt("Input file not found\n");
            return;
        }
        printf(" Input file opened. \n");    
        M=dd_PolyFile2Matrix(reading, &err);
        
        if (err==dd_NoError) {
            poly=dd_DDMatrix2Poly(M, &err); /* compute the second representation */
            if (err!=dd_NoError) {
                dd_WriteErrorMessages(stdout,err);
                mxErrMsgTxt("CDD internal error\n");
                return;
            }
            A=dd_CopyInequalities(poly);
            G=dd_CopyGenerators(poly);
            GI=dd_CopyInputIncidence(poly);
            GA=dd_CopyAdjacency(poly);
            plhs[0] = FT_set_V_MatrixPtr(G);
            plhs[1] = ZH_set_Vlist(GI,GA);
            dd_FreePolyhedra(poly);
            dd_FreeMatrix(M);
            return;
        }
    }
    else {
        mexErrMsgTxt("file-ine expects an file input");
    }
    return;
}
コード例 #2
0
ファイル: tz_segdetector2.c プロジェクト: Vaa3D/vaa3d_tools
void check_argin(int nrhs, const mxArray *prhs[])
{
  if (nrhs > 2  || nrhs < 1)
    mxErrMsgTxt("TZ_SEGDETECTOR takes 1~3 arguments.");

  if (!mxIsInt8(prhs[0]))
    mxErrMsgTxt("The first argument must be an int8 array.");

  if (!tz_mxIsVector(prhs[0]))
    mxErrMsgTxt("The first argument must be a vector.");

  if (nrhs >= 2)
    if (!mxIsInt8(prhs[1]))
      mxErrMsgTxt("The first argument must be an int8 scalar.");
}
コード例 #3
0
ファイル: tz_fcmp.c プロジェクト: Vaa3D/vaa3d_tools
void check_argin(int nrhs, const mxArray *prhs[])
{
  if (nrhs > 3  || nrhs < 2)
    mxErrMsgTxt("TZ_FCMP takes 2~3 arguments.");

  if (!(mxIsDouble(prhs[0]) && mxIsDouble(prhs[1])))
    mxErrMsgTxt("The first two arguments must be both double arrays.");

  if (!tz_mxSameSize(prhs[0], prhs[1]) && !tz_mxIsScalar(prhs[0]) &&
      !tz_mxIsScalar(prhs[1]))
    mxErrMsgTxt("The first two arguments must have the same size if neither of them is a scalar.");

  if (nrhs == 3) {
    if (!tz_mxIsDoubleScalar(prhs[2]))
      mxErrMsgTxt("The third arguments must be a double scalar.");
  }
}
コード例 #4
0
ファイル: tz_blocksum_dm.c プロジェクト: Vaa3D/vaa3d_tools
static void check_argin(int nrhs, const mxArray *prhs[])
{
  if (nrhs != 2)
    mxErrMsgTxt("TZ_BLOCKSUM_DM takes 2 arguments.");

  if (!mxIsDouble(prhs[0]))
    mxErrMsgTxt("The first argument must be a double matrix.");

  mwSize nd = mxGetNumberOfDimensions(prhs[0]);
  if (nd > 3)
    mexErrMsgTxt("Dimensions too high");

  if (!tz_mxIsDoubleVector(prhs[1]))
    mxErrMsgTxt("The second argument must be a double vector.");

  if (tz_mxGetL(prhs[1]) != nd)
    mxErrMsgTxt("The second argument has wrong size.");
}
コード例 #5
0
ファイル: afwt2_CWT.c プロジェクト: sasif/MASTeR
/* u = fwt2_CWT(x, h0_r, h1_r, h0_c, h1_c, J, symr, symc); */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *u, *w, *h0_r, *h1_r, *h0_c, *h1_c;
    int nR, nC, l, l1, symr, symc, J, JmaxR, JmaxC;

    /* Check for the proper number of arguments. */
    if (nrhs != 8) {
        mexErrMsgTxt("Exactly five inputs required");
    }
    if (nlhs > 1) {
        mexErrMsgTxt("Too many output arguments");
    }

    nR = mxGetM(prhs[0]);
    nC = mxGetN(prhs[0]);
    /*
     * if (n != n1) {
        mexErrMsgTxt("Input w must be a square image.");
    }
     */
    if (mxIsComplex(prhs[0])) {
        mexErrMsgTxt("Input w must be real");
    }
    J = mxGetScalar(prhs[5]);
    JmaxR = checkPowerTwo(nR, J);
    JmaxC = checkPowerTwo(nC, J);
    if ((J < 0) || (J > JmaxR) || (J > JmaxC) ) {
        mxErrMsgTxt("Input J must be an integer between 0 and log2(n), and dyadic for ROW and COL---use smaller J.");
    }
    l = (mxGetM(prhs[1]) > mxGetN(prhs[1])) ? mxGetM(prhs[1]) : mxGetN(prhs[1]);
    l1 = (mxGetM(prhs[2]) > mxGetN(prhs[2])) ? mxGetM(prhs[2]) : mxGetN(prhs[2]);
    if (l != l1) {
        mexErrMsgTxt("Filters must be the same length");
    }

    symr = mxGetScalar(prhs[6]);
    symc = mxGetScalar(prhs[7]);
    if ((symr > 2) || (symc > 2)) {
        mexErrMsgTxt("Symmetry flag must be 0, 1, or 2");
    }

    /* Create matrix for the return argument. */
    plhs[0] = mxCreateDoubleMatrix(nR, nC, mxREAL);

    /* Assign pointers to each input and output. */
    w = mxGetPr(prhs[0]);
    h0_r = mxGetPr(prhs[1]);
    h1_r = mxGetPr(prhs[2]);
    h0_c = mxGetPr(prhs[3]);
    h1_c = mxGetPr(prhs[4]);

    u = mxGetPr(plhs[0]);

    /* Call the C subroutine. */
    adj_wavelet_multi_level2D_CWT(u, w, nR, nC, h0_r, h1_r, h0_c, h1_c, l, J, symr, symc);
    return;
}
コード例 #6
0
ファイル: afwt.c プロジェクト: Pei-jie/L1-homotopy
/* u = afwt(w, h0, h1, J, sym); */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *u, *w, *h0, *h1;
    int n, l, l1, sym, J, Jmax;
    
  /* Check for the proper number of arguments. */
    if (nrhs != 5) {
        mexErrMsgTxt("Exactly five inputs required");
    }
    if (nlhs > 1) {
        mexErrMsgTxt("Too many output arguments");
    }
    
    if (mxGetN(prhs[0]) > 1) {
        mexErrMsgTxt("Input x must be a column vector");
    }
    if (mxIsComplex(prhs[0])) {
        mexErrMsgTxt("Input w must be real");
    }
    n = mxGetM(prhs[0]);
    J = mxGetScalar(prhs[3]);
    Jmax = checkPowerTwo(n, J);
    if ((J < 0) || (J > Jmax)) {
        mxErrMsgTxt("Input J must be an integer between 0 and log2(n)");
    }
    l = (mxGetM(prhs[1]) > mxGetN(prhs[1])) ? mxGetM(prhs[1]) : mxGetN(prhs[1]);
    l1 = (mxGetM(prhs[2]) > mxGetN(prhs[2])) ? mxGetM(prhs[2]) : mxGetN(prhs[2]);
    if (l != l1) {
        mexErrMsgTxt("Filters must be the same length");
    }
    sym = mxGetScalar(prhs[4]);
    if (sym > 2) {
        mexErrMsgTxt("Symmetry flag must be 0, 1, or 2");
    }
    
  /* Create matrix for the return argument. */
    plhs[0] = mxCreateDoubleMatrix(n, 1, mxREAL);
    
  /* Assign pointers to each input and output. */
    w = mxGetPr(prhs[0]);
    h0 = mxGetPr(prhs[1]);
    h1 = mxGetPr(prhs[2]);
    u = mxGetPr(plhs[0]);
    
  /* Call the C subroutine. */
    adj_wavelet_multi_level(u, w, n, h0, h1, l, J, sym);
    return;
}
コード例 #7
0
ファイル: mult_col.c プロジェクト: Field-Lab/matlab
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    
  double *A, *k, *M, *stim;
  int m,n,nm;
  
  int i,j,numtoupdate;
  
  
  if (nrhs != 2) {
      mexErrMsgTxt("need 2 input args: mult_col(A,k)\n");
  }
  
  /*  Read inputs into appropriate local variables */
  m = mxGetM(prhs[0]);
  n = mxGetN(prhs[0]);
  nm = n*m;
  A = mxGetPr(prhs[0]);
  if (mxGetM(prhs[1]) != n || mxGetN(prhs[1]) != 1) {
      mxErrMsgTxt("2nd argument k needs to be a col vector of length equal to num columns of first argument A!\n");
  }
  k = mxGetPr(prhs[1]);
  
  
  /* Initialize the output */
  plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
  M = mxGetPr(plhs[0]);
  
    /* Copy entries of A to M */
  memcpy((void *)(M),(void *)(A),nm*sizeof(double));
  
  for (i=0;i<nm;i++) {
    M[i] = M[i]*k[(i/m)];
  }

}
コード例 #8
0
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ ALLOCATES();

  CreateTicTacToc( CallMatlab );
  CreateTicTacToc( callSort   );
  int               I, J, K, ii, jj, kk;
  int               IJ, IJK, IJK_1;
  int               DI, DJ, DK, DIJK, DIJK_1;
  int               CDI, CDJ, CDK;
  int               result, fevals = 0;
  int               NVOLS, NVOLS_1, n, s, s_start, s_end, v_init;
  real              *volumes, *V, x, y, *DIST, *order, last_distance;
  int               *VV=NULL, nV, v, vv;
  char              skip;
  triplet           *TS=NULL, *DTS=NULL, T;
  mxArray           *INPUT[2]={NULL,NULL}, *OUTPUT[3]={NULL,NULL,NULL};
  double            *MAXs, LAST_MAX;
  double            thisMINx, thisMINy;
  double            *idxs;
  double            *vols;
  double            *ijk;
  char              callSort;
  mwSize            toVec[2]={1,1};
  char              VERBOSE = 0;
  char              STR[1024];
  
  
  if( nlhs > 1 ){
    mxErrMsgTxt("too much outputs");
  }


  if( mxIsChar( prhs[nrhs-1] ) ){
    mxGetString( prhs[nrhs-1], STR, 100 );
    if( ! myStrcmpi(STR,"verbose") ){ 
      VERBOSE = 1;
    } else {
      mxErrMsgTxt("only 'verbose' option allowed.");
    }
    nrhs = nrhs-1;
  }
  
  
  if( nrhs != 3 ){
    mxErrMsgTxt("sintax error. max_min_multiples_erodes( V , F , volumes )");
  }

  if( mxGetClassID( prhs[1] ) != mxFUNCTION_CLASS ){
    mxErrMsgTxt("F have to be a function_handle.");
  }

  if( myNDims( prhs[0] ) > 3  ){
    mxErrMsgTxt("bigger than 3d arrays is not allowed.");
  }
  
  NVOLS   = myNumel( prhs[2] );
  NVOLS_1 = NVOLS - 1;
  volumes = myGetPr( prhs[2] );
  
  
  I     = mySize( prhs[0] , 0 );
  J     = mySize( prhs[0] , 1 );
  K     = mySize( prhs[0] , 2 );
  IJ    = I*J;
  IJK   = IJ*K;
  

  VV = (int     *) mxMalloc( IJK*sizeof( int     ) );
  TS     = (triplet *) mxMalloc( IJK*sizeof( triplet ) );

  V = myGetPr( prhs[0] );

  v  = 0; 
  nV = 0;
  for( kk = 0 ; kk < K ; kk++ ){ for( jj = 0 ; jj < J ; jj++ ){ for( ii = 0 ; ii < I ; ii++ ){
    x = V[ v ];
    if( x == x ){
      VV[ nV ] = v;
      TS[ v ].isnan = 0;
      TS[ v ].i     = ii;
      TS[ v ].j     = jj;
      TS[ v ].k     = kk;
      nV++;
    } else {
      TS[ v ].isnan = 1;
    }
    v++;
  }}}


  INPUT[0] = prhs[1];
  INPUT[1] = mxCreateNumericMatrix( 1 , 3 , mxDOUBLE_CLASS , mxREAL );
  ijk = (double *) mxGetData( INPUT[1] );
  
  
  ijk[0] = TS[ VV[ nV/2] ].i + 1;
  ijk[1] = TS[ VV[ nV/2] ].j + 1;
  ijk[2] = TS[ VV[ nV/2] ].k + 1;
  


  OUTPUT[2] = mexCallMATLABWithTrap( 2 , OUTPUT , 2 , INPUT , "feval" );
  
  if( OUTPUT[2] == NULL ){
    
    callSort = 0;
    if( mxGetClassID( OUTPUT[0] ) != mxDOUBLE_CLASS ){
      if( INPUT[1]  != NULL ){ mxDestroyArray( INPUT[1] );  INPUT[1]=NULL;  }
      if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; }
      if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; }
      if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; }
      mxErrMsgTxt("F debe retornar un double en el primer output.");
    }
    if( mxGetClassID( OUTPUT[1] ) != mxDOUBLE_CLASS ){
      if( INPUT[1]  != NULL ){ mxDestroyArray( INPUT[1] );  INPUT[1]=NULL;  }
      if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; }
      if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; }
      if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; }
      mxErrMsgTxt("F debe retornar un double en el segundo output.");
    }
    
  } else {

    callSort = 1;
    if( VERBOSE ){
      mexPrintf("sort has to be called\n");
    }
    
    mxDestroyArray( OUTPUT[2] ); OUTPUT[2] = NULL;

    result = mexCallMATLAB( 1 , OUTPUT , 2 , INPUT , "feval" );
    if( result ){ mxErrMsgTxt("error computing la funcion."); }

    if( mxGetClassID( OUTPUT[0] ) != mxDOUBLE_CLASS ){
      if( INPUT[1]  != NULL ){ mxDestroyArray( INPUT[1] );  INPUT[1]=NULL;  }
      if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; }
      if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; }
      if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; }
      mxErrMsgTxt("F debe retornar un double en el primer output.");
    }

  }

  DI   = mySize( OUTPUT[0] , 0 );
  DJ   = mySize( OUTPUT[0] , 1 );
  DK   = mySize( OUTPUT[0] , 2 );
  
  DTS  = (triplet *) mxMalloc( 2*DI*DJ*DK*sizeof( triplet ) );
  

  plhs[0] = mxCreateNumericMatrix( NVOLS , 1 , mxREAL_CLASS , mxREAL );
  MAXs    = (real *) mxGetData( plhs[0] );
  for( n = 0 ; n < NVOLS ; n++ ){
    MAXs[n] = -10000;
  }

  
  LAST_MAX = MAXs[ NVOLS_1 ];
  for( v_init = 0 ; v_init < EVERY ; v_init++ ){
    if( utIsInterruptPending() ){
      if( INPUT[1]  != NULL ){ mxDestroyArray( INPUT[1] );  INPUT[1]=NULL;  }
      if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; }
      if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; }
      if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; }
      mexPrintf("USER INTERRUP!!!\n");
      mxErrMsgTxt("USER INTERRUP!!!");
    }
    if( VERBOSE ){
      mexPrintf("v_init:  %d  (%g)  of  %d\n", v_init , LAST_MAX , EVERY );
    }

    for( v = v_init ; v < nV ; v += EVERY ){
      vv = VV[ v ];
      
      thisMINx =   V[ vv ];
      thisMINy =  -thisMINx;
      if( ( thisMINx < LAST_MAX )  && ( thisMINy < LAST_MAX ) ){
        continue;
      }

      T = TS[ vv ];
      
      ijk[0] = T.i + 1;
      ijk[1] = T.j + 1;
      ijk[2] = T.k + 1;
      

      if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; }
      if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; }
      if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; }

      if( !callSort ){
        tic( CallMatlab );
        result = mexCallMATLAB( 2 , OUTPUT , 2 , INPUT , "feval" ); fevals++;
        tac( CallMatlab );
      } else {
        tic( CallMatlab );
        result = mexCallMATLAB( 1 , OUTPUT , 2 , INPUT , "feval" ); fevals++;
        tac( CallMatlab );
      }
      
      DI   = mySize( OUTPUT[0] , 0 );
      DJ   = mySize( OUTPUT[0] , 1 );
      DK   = mySize( OUTPUT[0] , 2 );

      DIJK    = DI*DJ*DK;

      if( volumes[ NVOLS_1 ] > DIJK ){
      if( INPUT[1]  != NULL ){ mxDestroyArray( INPUT[1] );  INPUT[1]=NULL;  }
      if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; }
      if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; }
      if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; }
        mxErrMsgTxt("el maximo volumen debe ser menor que numel(DIST)");
      }

      DIJK_1  = DIJK - 1;

      DIST  = (double  *) mxGetData( OUTPUT[0] );
      
      DTS  = (triplet *) mxRealloc( DTS , DIJK*sizeof( triplet ) );
      s = 0;
      for( kk = 0 ; kk < DK ; kk++ ){ for( jj = 0 ; jj < DJ ; jj++ ){ for( ii = 0 ; ii < DI ; ii++ ){
        DTS[ s ].i = ii;
        DTS[ s ].j = jj;
        DTS[ s ].k = kk;
        s++;
      }}}


      if( !callSort ){
        order = (double  *) mxGetData( OUTPUT[1] );
      } else {
        toVec[0] = mxGetNumberOfElements( OUTPUT[0] );
        mxSetDimensions( OUTPUT[0] ,  toVec  , 2 );
        
        tic( callSort );
        result = mexCallMATLAB( 2 , OUTPUT+1 , 1 , OUTPUT , "sort" );
        tac( callSort );
      
        order = (double  *) mxGetData( OUTPUT[2] );
      }
      
      CDI = DTS[ (int) ( order[0] - 1 ) ].i;
      CDJ = DTS[ (int) ( order[0] - 1 ) ].j;
      CDK = DTS[ (int) ( order[0] - 1 ) ].k;
      
      
      skip   = 0;

      s = 0;
      for( n = 0 ; n < NVOLS ; n++ ){
        s_end = (int) ( volumes[n] - 1 );
        last_distance = DIST[ (int) order[ s_end ] - 1 ];
        
        while( s_end < DIJK_1 && DIST[ (int) ( order[ s_end + 1 ] - 1 ) ] == last_distance ){
          s_end++;
        }
        s_end++;
        
        for( ; s < s_end ; s++ ){
          vv = (int) ( order[ s ] - 1 );
          
          ii = T.i + DTS[ vv ].i - CDI;  if( ii < 0 || ii > I ){ skip = 1; break; }
          jj = T.j + DTS[ vv ].j - CDJ;  if( jj < 0 || jj > J ){ skip = 1; break; }
          kk = T.k + DTS[ vv ].k - CDK;  if( kk < 0 || kk > K ){ skip = 1; break; }
            
          vv = ii + jj*I + kk*IJ;
          if( TS[ vv ].isnan ){  skip = 1; break; }
          x =  V[ vv ];  if( x < thisMINx ){ thisMINx = x; }
          y = -x;        if( y < thisMINy ){ thisMINy = y; }
          if( ( thisMINx < LAST_MAX )  && ( thisMINy < LAST_MAX ) ){
            skip = 1; break; 
          }
        }
        if( skip ){  break; }
        if( thisMINx > MAXs[n] ){ MAXs[n] = thisMINx; }
        if( thisMINy > MAXs[n] ){ MAXs[n] = thisMINy; }
      }
      LAST_MAX = MAXs[ NVOLS_1 ];

    }
    
  }
  if( INPUT[1]  != NULL ){ mxDestroyArray( INPUT[1]  ); INPUT[1] =NULL; }
  if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; }
  if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; }
  if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; }
  
  if( VERBOSE ){
    mexPrintf( "\nfevals: %d  en  tiempo:   CallMatlab: %20.30g    sorting: %20.30g\n" , fevals , toc( CallMatlab ) , toc( callSort ) );
  }
  
  
  if(  VV != NULL ){  mxFree(  VV ); }
  if(  TS != NULL ){  mxFree(  TS ); }
  if( DTS != NULL ){  mxFree( DTS ); }

  myFreeALLOCATES();

}
コード例 #9
0
ファイル: popenw.c プロジェクト: JanaMa/psychophysics
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    int 	i, err, len;
    long   	pvl, pvb[16];

    if (nrhs < 1){
	mexPrintf("popenw     Y=popenw(X[,D[,F]])  Open and write an external process\n");
	mexPrintf("           When X is a string, that string is executed as a new process\n");
	mexPrintf("           and Y is returned as a handle (integer) to that stream.\n");
	mexPrintf("           Subsequent calls to popen(Y,D[,F]) with that handle write\n");
	mexPrintf("           the data in D to the the process, converted to binary\n");
	mexPrintf("           according to the format string F (default: big endian int16).\n");
	mexPrintf("           A call with D empty means to close the stream.\n");
	return;
    }
    /* look at the data */
    /* Check to be sure input argument is a string. */
    if ((mxIsChar(prhs[0]))){
	/* first argument is string - opening a new command */
	FILE *f;
	char *cmd;
	int tabix = findfreetab();
	
	if (tabix < 0) {
	    mexErrMsgTxt("Out of file table slots.");
	} else {
	    cmd = mxArrayToString(prhs[0]);
	    /* fprintf(stderr, "cmd=%s\n", cmd); */
	    f = popen(cmd, "w");
	    mxFree(cmd);
	    if ( f == NULL ) {
		mexErrMsgTxt("Error running external command.");
		return;
	    }
	    /* else have a new command path - save the handle */
	    filetab[tabix] = f;
	    /* return the index */
	    if (nlhs > 0) {
		double *pd;
		mxArray *rslt = mxCreateDoubleMatrix(1,1, mxREAL);
		plhs[0] = rslt;
		pd = mxGetPr(rslt);
		*pd = (double)tabix;
	    }
	}
	return;
    }	

    if (nrhs < 2) {
	mexErrMsgTxt("apparently accessing handle, but no D argument");
	return;
    }
    
    /* get the handle */
    {
	int ix, rows, cols, npts, ngot; 
	int fmt = MXPO_INT16BE;
	int sz = 2;
	FILE *f = NULL;
	double *pd;
	char *data;

	if (mxGetN(prhs[0]) == 0) {
	    mexErrMsgTxt("handle argument is empty");
	    return;
	}

	pd = mxGetPr(prhs[0]);
	ix = (int)*pd;
	if (ix < filetabix) {
	    f = filetab[ix];
	}
	if (f == NULL) {
	    mexErrMsgTxt("invalid handle");
	    return;
	}

	/* how many values to write */
	npts = mxGetN(prhs[1]) * mxGetM(prhs[1]); 
	if (npts == 0) {
	    /* close */
	    pclose(f);
	    filetab[ix] = NULL;
	    return;
	}

	/* what is the format? */
	if ( nrhs > 2 ) {
	    char *fmtstr;

	    if (!mxIsChar(prhs[2])) {
		mexErrMsgTxt("format arg must be a string");
		return;
	    }
	    fmtstr = mxArrayToString(prhs[2]);
	    if (strcmp(fmtstr, "int16n")==0 || strcmp(fmtstr, "int16") == 0) {
		fmt = MXPO_INT16N;
	    } else if (strcmp(fmtstr, "int16r")==0) {
		fmt = MXPO_INT16R;
	    } else if (strcmp(fmtstr, "int16be")==0) {
#if BYTE_ORDER == BIG_ENDIAN
		fmt = MXPO_INT16N;
#else
		fmt = MXPO_INT16R;
#endif
	    } else if (strcmp(fmtstr, "int16le")==0) {
#if BYTE_ORDER == BIG_ENDIAN
		fmt = MXPO_INT16R;
#else
		fmt = MXPO_INT16N;
#endif
	    } else if (strcmp(fmtstr, "uint8")==0) {
		fmt = MXPO_UINT8;
		sz = 1;
	    } else if (strcmp(fmtstr, "char")==0) {
		fmt = MXPO_CHAR;
		sz = 1;
	    } else {
		mexErrMsgTxt("unrecognized format");
	    }
            mxFree(fmtstr);
	}

	data = (char *)malloc(sz*npts+1);

	/* format conversion */
	if (sz == 1) {
	    if (mxIsChar(prhs[1])) {
                if (mxGetString(prhs[1], data, npts+1) != 0) {
                    mxErrMsgTxt("Problems copying string data");
                }
            } else {
       	        int i;
       	        double *pd = mxGetPr(prhs[1]);
       	        char *pc = (char *)data;
       	        pd = pd + npts - 1;
       	        pc = pc + npts - 1;
       	        for(i = npts-1; i >= 0; --i) {
       	  	    *pc-- = (char)*pd--;
                }
            }
	} else if (fmt == MXPO_INT16N) {
	    int i;
	    double *pd = mxGetPr(prhs[1]);
	    short *ps = (short *)data;
	    pd = pd + npts - 1;
	    ps = ps + npts - 1;
	    for(i = npts-1; i >= 0; --i) {
		*ps-- = (short)*pd--;
	    }
	} else if (fmt == MXPO_INT16R) {
	    int i;
	    double *pd = mxGetPr(prhs[1]);
	    short *ps = (short *)data;
	    short v;
	    pd = pd + npts - 1;
	    ps = ps + npts - 1;
	    for(i = npts-1; i >= 0; --i) {
		v = (short)*pd--;
		*ps -- = (0xFF & (v >> 8)) + (0xFF00 & (v << 8));
	    }
	} else {
コード例 #10
0
ファイル: mex_shape.c プロジェクト: aaronzou/mirone
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) { 

	/* Pointer to temporary matlab array */
	const mxArray *mx;
	mxArray *m_shape_type;

	int	nShapeType, nEntities, i, j, k, iPart, nFields, nStructElem, isPoint, firstInPoints = 1;
	int	num_dbf_fields, num_dbf_records; /* number of DBF attributes, records */
	int	buflen;		/* length of input shapefile name */
	int	status;		/* success or failure */
	char	*shapefile;	/* holder for input shapefile name */
	const	char *pszPartType = "";

	/* Not used. */
	double adfMinBound[4], adfMaxBound[4];

	/* pointer to the shapefile */
	SHPHandle	hSHP;
	SHPObject	*psShape;
	DBFHandle	dbh;	/* handle for DBF file */

	/* This structure will hold a description of each field in the DBF file. */
	typedef struct DBF_Field_Descriptor {
		char          pszFieldName[12];
		DBFFieldType  field_type;
	} DBF_Field_Descriptor;

	DBF_Field_Descriptor *dbf_field;

	/* stores individual values from the DBF.  */
	int	dbf_integer_val, dims[2], *p_parts_ptr, nNaNs, c, i_start, i_stop;
	char	*dbf_char_val, error_buffer[500];
	char	*fnames[100];  /* holds name of fields */
	mxArray *out_struct, *x_out, *y_out, *z_out, *bbox, *p_parts;
	double	*x_out_ptr, *y_out_ptr, *z_out_ptr, *bb_ptr, nan, dbf_double_val;
	size_t	sizebuf;

	/*  Initialize the dbf record.  */
	dbf_field = NULL;

	/* Check for proper number of arguments */
	if (nrhs != 1)
		mexErrMsgTxt("One input arguments are required."); 

	if (nlhs != 2)
		mexErrMsgTxt("Two output arguments required."); 

	/* Make sure the input is a proper string. */
	if ( mxIsChar(prhs[0]) != 1 )
		mexErrMsgTxt("Shapefile parameter must be a string\n" );

	if ( mxGetM(prhs[0]) != 1 )
		mexErrMsgTxt("Shapefile parameter must be a row vector, not a column string\n" );

	buflen = mxGetN(prhs[0]) + 1; 
	shapefile = mxCalloc( buflen, sizeof(char) );

	/* copy the string data from prhs[0] into a C string. */
	status = mxGetString( prhs[0], shapefile, buflen );
	if ( status != 0 )
		mxErrMsgTxt( "Not enough space for shapefile argument.\n" );

	/* -------------------------------------------------------------------- */
	/*      Open the passed shapefile.                                      */
	/* -------------------------------------------------------------------- */
	hSHP = SHPOpen( shapefile, "rb" );
	if( hSHP == NULL )
		mxErrMsgTxt( "Unable to open:%s\n", shapefile );

	/* -------------------------------------------------------------------- */
	/*      Get the needed information about the shapefile.                 */
	/* -------------------------------------------------------------------- */
	SHPGetInfo( hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound );

	/* Make sure that we can handle the type. */
	switch ( nShapeType ) {
		case SHPT_POINT:
			break;
		case SHPT_POINTZ:
			break;
		case SHPT_ARC:
			break;
		case SHPT_ARCZ:
			break;
		case SHPT_POLYGON:
			break;
		case SHPT_POLYGONZ:
			break;
		case SHPT_MULTIPOINT:		/* JL */
			break;
		default:
			sprintf ( error_buffer, "Unhandled shape code %d (%s)", nShapeType, SHPTypeName ( nShapeType ) );
	        	mexErrMsgTxt( error_buffer ); 
	}
    
	/* Create the output shape type parameter. */
	plhs[1] = mxCreateString ( SHPTypeName ( nShapeType ) );

	/* Open the DBF, and retrieve the number of fields and records */
	dbh = DBFOpen (shapefile, "rb");
	num_dbf_fields = DBFGetFieldCount ( dbh );
	num_dbf_records = DBFGetRecordCount ( dbh );

	/* Allocate space for a description of each record, and populate it.
	 * I allocate space for two extra "dummy" records that go in positions
	 * 0 and 1.  These I reserve for the xy data. */
	nFields = 3;
	if ( (nShapeType == SHPT_POLYGONZ) || (nShapeType == SHPT_ARCZ) || (nShapeType == SHPT_POINTZ) ) nFields++;
	dbf_field = (DBF_Field_Descriptor *) mxMalloc ( (num_dbf_fields+nFields) * sizeof ( DBF_Field_Descriptor ) );
	if ( dbf_field == NULL )
		mexErrMsgTxt("Memory allocation for DBF_Field_Descriptor failed."); 

	for ( j = 0; j < num_dbf_fields; ++j )
		dbf_field[j+nFields].field_type = DBFGetFieldInfo ( dbh, j, dbf_field[j+nFields].pszFieldName, NULL, NULL );

	fnames[0] = strdup ( "X" );
	fnames[1] = strdup ( "Y" );
	if ( (nShapeType == SHPT_POLYGONZ) || (nShapeType == SHPT_ARCZ) || (nShapeType == SHPT_POINTZ) ) {
		fnames[2] = strdup ( "Z" );
		fnames[3] = strdup ( "BoundingBox" );
	}
	else
		fnames[2] = strdup ( "BoundingBox" );

	for ( j = 0; j < num_dbf_fields; ++j )
		fnames[j+nFields] = strdup ( dbf_field[j+nFields].pszFieldName );

	/* To hold information on eventual polygons with rings */
	/*fnames[num_dbf_fields+3] = strdup ( "nParts" );*/
	/*fnames[num_dbf_fields+4] = strdup ( "PartsIndex" );*/

	/* Allocate space for the output structure. */
	isPoint = ( nShapeType == SHPT_POINT || nShapeType == SHPT_POINTZ ) ? 1: 0;
	nStructElem = ( nShapeType == SHPT_POINT || nShapeType == SHPT_POINTZ ) ? 1: nEntities;
	out_struct = mxCreateStructMatrix ( nStructElem, 1, nFields + num_dbf_fields, (const char **)fnames );

	/* create the BoundingBox */
	dims[0] = 4;		dims[1] = 2;
	bbox = mxCreateNumericArray ( 2, dims, mxDOUBLE_CLASS, mxREAL );
	bb_ptr = mxGetData ( bbox );
	for (i = 0; i < 4; i++) bb_ptr[i]   = adfMinBound[i];
	for (i = 0; i < 4; i++) bb_ptr[i+4] = adfMaxBound[i];
	mxSetField ( out_struct, 0, "BoundingBox", bbox );

	nan = mxGetNaN();
	/* -------------------------------------------------------------------- */
	/*	Skim over the list of shapes, printing all the vertices.	*/
	/* -------------------------------------------------------------------- */
	for( i = 0; i < nEntities; i++ ) {
		psShape = SHPReadObject( hSHP, i );

		/* Create the fields in this struct element.  */
		if ( !isPoint ) {
			nNaNs = psShape->nParts > 1 ? psShape->nParts : 0;
			dims[0] = psShape->nVertices + nNaNs;
			dims[1] = 1;
			x_out = mxCreateNumericArray ( 2, dims, mxDOUBLE_CLASS, mxREAL );
			x_out_ptr = mxGetData ( x_out );
			y_out = mxCreateNumericArray ( 2, dims, mxDOUBLE_CLASS, mxREAL );
			y_out_ptr = mxGetData ( y_out );
			if ( (nShapeType == SHPT_POLYGONZ) || (nShapeType == SHPT_POINTZ) || (nShapeType == SHPT_ARCZ)) {
				z_out = mxCreateNumericArray ( 2, dims, mxDOUBLE_CLASS, mxREAL );
				z_out_ptr = mxGetData ( z_out );
			}
		}
		else if (firstInPoints) {	/* Allocate all memory we'll need */
			x_out = mxCreateDoubleMatrix (nEntities, 1, mxREAL);
			x_out_ptr = mxGetPr ( x_out );
			y_out = mxCreateDoubleMatrix (nEntities, 1, mxREAL);
			y_out_ptr = mxGetPr ( y_out );
			if (nShapeType == SHPT_POINTZ) {
				z_out = mxCreateDoubleMatrix (nEntities, 1, mxREAL);
				z_out_ptr = mxGetPr ( z_out );
			}
			firstInPoints = 0;
		}

		if (!isPoint && psShape->nParts > 1) {
			for (k = c = 0; k < psShape->nParts; k++) {
				i_start = psShape->panPartStart[k];
				if (k < psShape->nParts - 1)
					i_stop = psShape->panPartStart[k+1];
				else
					i_stop = psShape->nVertices;

				if ( nShapeType == SHPT_POLYGONZ ) {
					for (j = i_start; j < i_stop; c++, j++) {
						x_out_ptr[c] = psShape->padfX[j];
						y_out_ptr[c] = psShape->padfY[j];
						z_out_ptr[c] = psShape->padfZ[j];
					}
					x_out_ptr[c] = nan;
					y_out_ptr[c] = nan;
					z_out_ptr[c] = nan;
				}
				else {
					for (j = i_start; j < i_stop; c++, j++) {
						x_out_ptr[c] = psShape->padfX[j];
						y_out_ptr[c] = psShape->padfY[j];
					}
					x_out_ptr[c] = nan;
					y_out_ptr[c] = nan;
				}
				c++;
			}
		}
		else if ( isPoint ) {
			x_out_ptr[i] = *psShape->padfX;
			y_out_ptr[i] = *psShape->padfY;
			if (nShapeType == SHPT_POINTZ) z_out_ptr[i] = *psShape->padfZ;
			if (i > 0) {
        			SHPDestroyObject( psShape );
				continue;
			}
		}
		else {		/* Just copy the vertices over. */
			sizebuf = mxGetElementSize ( x_out ) * psShape->nVertices;
			memcpy ( (void *) x_out_ptr, (void *) psShape->padfX, sizebuf );
			memcpy ( (void *) y_out_ptr, (void *) psShape->padfY, sizebuf );
			if ( (nShapeType == SHPT_POLYGONZ)  || (nShapeType == SHPT_ARCZ))
				memcpy ( (void *) z_out_ptr, (void *) psShape->padfZ, sizebuf );
		}

		mxSetField ( out_struct, i, "X", x_out );
		mxSetField ( out_struct, i, "Y", y_out );
		if ( (nShapeType == SHPT_POLYGONZ) || (nShapeType == SHPT_ARCZ) )
			mxSetField ( out_struct, i, "Z", z_out );

		bbox = mxCreateNumericMatrix ( 4, 2, mxDOUBLE_CLASS, mxREAL );
		bb_ptr = (double *)mxGetData ( bbox );
		bb_ptr[0] = psShape->dfXMin;		bb_ptr[1] = psShape->dfYMin;
		bb_ptr[2] = psShape->dfZMin;		bb_ptr[3] = psShape->dfMMin;
		bb_ptr[4] = psShape->dfXMax;		bb_ptr[5] = psShape->dfYMax;
		bb_ptr[6] = psShape->dfZMax;		bb_ptr[7] = psShape->dfMMax;
		if (i > 0)	/* First BB contains the ensemble extent */
			mxSetField ( out_struct, i, "BoundingBox", bbox );

		for ( j = 0; j < num_dbf_fields; ++j ) {
			switch ( dbf_field[j+nFields].field_type ) {
				case FTString:
					dbf_char_val = (char *) DBFReadStringAttribute ( dbh, i, j );
					mxSetField ( out_struct, i, dbf_field[j+nFields].pszFieldName, mxCreateString ( dbf_char_val ) );
					break;
				case FTDouble:
					dbf_double_val = DBFReadDoubleAttribute ( dbh, i, j );
					mxSetField ( out_struct, i, dbf_field[j+nFields].pszFieldName, mxCreateDoubleScalar ( dbf_double_val ) );
					break;
				case FTInteger:
				case FTLogical:
					dbf_integer_val = DBFReadIntegerAttribute ( dbh, i, j );
					dbf_double_val = dbf_integer_val;
					mxSetField ( out_struct, i, dbf_field[j+nFields].pszFieldName, mxCreateDoubleScalar ( dbf_double_val ) );
					break;
				default:
					sprintf ( error_buffer, "Unhandled code %d, shape %d, record %d\n", dbf_field[j+nFields].field_type, i, j );
	        			mexErrMsgTxt("Unhandled code"); 
			}
		}
        	SHPDestroyObject( psShape );
	}

	if ( isPoint ) {	/* In this case we still need to "send the true data out" */
		mxSetField ( out_struct, 0, "X", x_out );
		mxSetField ( out_struct, 0, "Y", y_out );
		if (nShapeType == SHPT_POINTZ)
			mxSetField ( out_struct, 0, "Z", z_out );
	}

	/* Clean up, close up shop. */
	SHPClose( hSHP );
	DBFClose ( dbh );

	if ( dbf_field != NULL )
		mxFree ( (void *)dbf_field );

	plhs[0] = out_struct;
}
コード例 #11
0
ファイル: ifwt2_CWT.c プロジェクト: sasif/MASTeR
/* x = ifwt2_CWT(w, g0_r, g1_r, g0_c, g1_c, J, symr, symc, rosym_flip, cosym_flip); */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  double *x, *w, *g0_r, *g1_r, *g0_c, *g1_c;
  int nR, nC, l, l1, symr, symc, rosym_flip, cosym_flip, J, JmaxR, JmaxC;
  
  /* Check for the proper number of arguments. */
  if (nrhs != 10) {
    mexErrMsgTxt("Exactly ten inputs required");
  }
  if (nlhs > 1) {
    mexErrMsgTxt("Too many output arguments");
  }
  
  nR = mxGetM(prhs[0]);
  nC = mxGetN(prhs[0]);
  /*
   * if (n != n1) {
    mexErrMsgTxt("Input w must be a square image.");
  }
   */
  if (mxIsComplex(prhs[0])) {
    mexErrMsgTxt("Input w must be real");    
  }
  J = mxGetScalar(prhs[5]);
  JmaxR = checkPowerTwo(nR, J);
  JmaxC = checkPowerTwo(nC, J);
  if ((J < 0) || (J > JmaxR) || (J > JmaxC) ) {
        mxErrMsgTxt("Input J must be an integer between 0 and log2(n), and dyadic for ROW and COL---use smaller J.");
  }
  l = (mxGetM(prhs[1]) > mxGetN(prhs[1])) ? mxGetM(prhs[1]) : mxGetN(prhs[1]);
  l1 = (mxGetM(prhs[2]) > mxGetN(prhs[2])) ? mxGetM(prhs[2]) : mxGetN(prhs[2]);
  if (l != l1) {
    mexErrMsgTxt("Filters must be the same length");
  }
  
  symr = mxGetScalar(prhs[6]);
  symc = mxGetScalar(prhs[7]);
  rosym_flip = mxGetScalar(prhs[8]); /* tells if we need to flip odd symmetry of scaling and wavelet coeffs */
  cosym_flip = mxGetScalar(prhs[9]);
  
  /* mexPrintf("rosym_flip is %d, cosym_flip is %d, \n", rosym_flip, cosym_flip); */
  if ((symr > 2) || (symc > 2)) {
    mexErrMsgTxt("Symmetry flag must be 0, 1, or 2");
  }
  
  /* Create matrix for the return argument. */
  plhs[0] = mxCreateDoubleMatrix(nR, nC, mxREAL);
  
  /* Assign pointers to each input and output. */
  w = mxGetPr(prhs[0]);
  g0_r = mxGetPr(prhs[1]);
  g1_r = mxGetPr(prhs[2]);
  g0_c = mxGetPr(prhs[3]);
  g1_c = mxGetPr(prhs[4]);
  
  x = mxGetPr(plhs[0]);
  
  /* Call the C subroutine. */
  inv_wavelet_multi_level2D_CWT(x, w, nR, nC, g0_r, g1_r, g0_c, g1_c, l, J, symr, symc, rosym_flip, cosym_flip);
  return;
}
コード例 #12
0
ファイル: scooper.c プロジェクト: embotech/ecos-matlab
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  params p;
  pwork *mywork;
  mxArray *xm;
  int i = 0; int j = 0; double *ptr;

  int numerr = 0;
  mxArray *outvar;
  mxArray *tinfos;
    /* change number of infofields according to profiling setting */    
#if PROFILING > 0 
#define NINFOFIELDS 15
#else 
#define NINFOFIELDS 14
#endif
    const char *infofields[NINFOFIELDS] = { "pcost",
                                            "dcost",
                                            "pres",
                                            "dres",
                                            "pinf",
                                            "dinf",
                                            "pinfres",
                                            "dinfres",
                                            "gap",
                                            "relgap",   
                                            "r0",
                                            "numerr",
                                            "iter",
                                            "infostring"
#if PROFILING > 0
                                           ,"timing"
#endif
                                           };

#if PROFILING == 1 
#define NTFIELDS 3
    const char *tinfo[NTFIELDS] = {"runtime", "tsolve", "tsetup"};
#endif            
    
#if PROFILING == 2
#define NTFIELDS 8
    const char *tinfo[NTFIELDS] = {"runtime", "tsolve", "tsetup", "tkktcreate", "tkktfactor", "tkktsolve", "torder", "ttranspose"};
#endif

#ifdef MEXARGMUENTCHECKS
  if( !(nrhs == 1) )
  {
       mexErrMsgTxt("scooper only takes 1 argument: scooper(params)");
  }
  if( nlhs > 2 ) mexErrMsgTxt("scooper has up to 2 output arguments only");
#endif

  xm = mxGetField(prhs[0], 0, "A");
  if (xm == NULL) {
    mexErrMsgTxt("could not find params.A");
  } else {
    if (!( (mxGetM(xm) == 1) && (mxGetN(xm) == 2) )) mexErrMsgTxt("A must be size (1, 2)\n");
    if (mxIsComplex(xm)) mxErrMsgTxt("parameter A must be real\n");
    if (!mxIsClass(xm, "double")) mxErrMsgTxt("parameter A must be full matrix of doubles");
    if (mxIsSparse(xm)) mxErrMsgTxt("parameter A must be full matrix");
    ptr = mxGetPr(xm);
    for(j = 0; j < 2; ++j) {
      for(i = 0; i < 1; ++i) {
        p.A[i][j] = *ptr++;
      }
    }
  }

  xm = mxGetField(prhs[0], 0, "b");
  if (xm == NULL) {
    mexErrMsgTxt("could not find params.b");
  } else {
    if (!( (mxGetM(xm) == 1) && (mxGetN(xm) == 1) )) mexErrMsgTxt("b must be size (1, 1)\n");
    if (mxIsComplex(xm)) mxErrMsgTxt("parameter b must be real\n");
    if (!mxIsClass(xm, "double")) mxErrMsgTxt("parameter b must be full vector of doubles");
    if (mxIsSparse(xm)) mxErrMsgTxt("parameter b must be full vector");
    p.b = *mxGetPr(xm);
  }

  xm = mxGetField(prhs[0], 0, "ct");
  if (xm == NULL) {
    mexErrMsgTxt("could not find params.ct");
  } else {
    if (!( (mxGetM(xm) == 1) && (mxGetN(xm) == 2) )) mexErrMsgTxt("ct must be size (1, 2)\n");
    if (mxIsComplex(xm)) mxErrMsgTxt("parameter ct must be real\n");
    if (!mxIsClass(xm, "double")) mxErrMsgTxt("parameter ct must be full matrix of doubles");
    if (mxIsSparse(xm)) mxErrMsgTxt("parameter ct must be full matrix");
    ptr = mxGetPr(xm);
    for(j = 0; j < 2; ++j) {
      for(i = 0; i < 1; ++i) {
        p.ct[i][j] = *ptr++;
      }
    }
  }

  mywork = setup(&p);
  if(mywork == NULL) {
    mexErrMsgTxt("Internal problem occurred in ECOS while setting up the problem.\nPlease send a bug report with data to Alexander Domahidi.\nEmail: [email protected]");
  }
  int flag = 0;
  flag = solve(mywork, &v);
  const int num_var_names = 1;
  const char *var_names[] = {"x"};
  plhs[0] = mxCreateStructMatrix(1, 1, num_var_names, var_names);
  xm = mxCreateDoubleMatrix(2, 1,mxREAL);
  mxSetField(plhs[0], 0, "x", xm);
  memcpy(mxGetPr(xm), v.x, sizeof(double)*2);


  if( nlhs == 2 ){
      plhs[1] = mxCreateStructMatrix(1, 1, NINFOFIELDS, infofields);
      
      /* 1. primal objective */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = 1.0*((double)mywork->info->pcost);
      mxSetField(plhs[1], 0, "pcost", outvar);
      
      /* 2. dual objective */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->dcost;
      mxSetField(plhs[1], 0, "dcost", outvar);
        
      /* 3. primal residual */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->pres;
      mxSetField(plhs[1], 0, "pres", outvar);
        
      /* 4. dual residual */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->dres;
      mxSetField(plhs[1], 0, "dres", outvar);
      
      /* 5. primal infeasible? */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->pinf;
      mxSetField(plhs[1], 0, "pinf", outvar);
      
      /* 6. dual infeasible? */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->dinf;
      mxSetField(plhs[1], 0, "dinf", outvar);
      
      /* 7. primal infeasibility measure */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->pinfres;
      mxSetField(plhs[1], 0, "pinfres", outvar);
      
      /* 8. dual infeasibility measure */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->dinfres;
      mxSetField(plhs[1], 0, "dinfres", outvar);
      
      /* 9. duality gap */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->gap;
      mxSetField(plhs[1], 0, "gap", outvar);
      
      /* 10. relative duality gap */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->relgap;
      mxSetField(plhs[1], 0, "relgap", outvar);
      
      /* 11. feasibility tolerance??? */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->stgs->feastol;
      mxSetField(plhs[1], 0, "r0", outvar);
      
      /* 12. iterations */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->iter;
      mxSetField(plhs[1], 0, "iter", outvar);
      
      /* 13. infostring */
      switch( flag ){
        case ECOS_OPTIMAL:
              outvar = mxCreateString("Optimal solution found");
              break;
          case ECOS_MAXIT:
              outvar = mxCreateString("Maximum number of iterations reached");
              break;
          case ECOS_PINF:
              outvar = mxCreateString("Primal infeasible");
              break;
          case ECOS_DINF:
              outvar = mxCreateString("Dual infeasible");
              break;
          case ECOS_KKTZERO:
              outvar = mxCreateString("Element of D zero during KKT factorization");
              break;
          case ECOS_OUTCONE:
              outvar = mxCreateString("PROBLEM: Mulitpliers leaving the cone");
              break;
          default:
              outvar = mxCreateString("UNKNOWN PROBLEM IN SOLVER");
      }       
      mxSetField(plhs[1], 0, "infostring", outvar);
        
#if PROFILING > 0        
      /* 14. timing information */
      tinfos = mxCreateStructMatrix(1, 1, NTFIELDS, tinfo);
      
      /* 14.1 --> runtime */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->tsolve + (double)mywork->info->tsetup;
      mxSetField(tinfos, 0, "runtime", outvar);
        
      /* 14.2 --> setup time */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->tsetup;
      mxSetField(tinfos, 0, "tsetup", outvar);
        
      /* 14.3 --> solve time */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->tsolve;
      mxSetField(tinfos, 0, "tsolve", outvar);
#if PROFILING > 1        
        
      /* 14.4 time to create KKT matrix */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->tkktcreate;
      mxSetField(tinfos, 0, "tkktcreate", outvar);
      /* 14.5 time for kkt solve */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->tkktsolve;
      mxSetField(tinfos, 0, "tkktsolve", outvar);
      
      /* 14.6 time for kkt factor */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->tfactor;
      mxSetField(tinfos, 0, "tkktfactor", outvar);
        
      /* 14.7 time for ordering */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->torder;
      mxSetField(tinfos, 0, "torder", outvar);
      
      /* 14.8 time for transposes */
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)mywork->info->ttranspose;
      mxSetField(tinfos, 0, "ttranspose", outvar);
#endif       
        
      mxSetField(plhs[1], 0, "timing", tinfos);        
#endif        
        
      /* 15. numerical error? */
      if( (flag == ECOS_NUMERICS) || (flag == ECOS_OUTCONE) || (flag == ECOS_FATAL) ){
          numerr = 1;
      }
      outvar = mxCreateDoubleMatrix(1, 1, mxREAL);
      *mxGetPr(outvar) = (double)numerr;
      mxSetField(plhs[1], 0, "numerr", outvar);        
  }

  cleanup(mywork);
}
コード例 #13
0
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
  const mxArray *curr_arg;
  union { 
    unsigned char *uint8;
             char *int8;
	unsigned short *uint16;
	         short *int16;
	unsigned int *uint32;
	         int *int32;
  } data_in,data_in_start;

  int dim1, dim2, no_of_in_bytes, compression_type;
  int diff, val_curr;
  double *frame_out, *frame_out_start;
  
    /* initialize return argument*/
  plhs[0] = NULL;
  
    /* Check for proper number of arguments. */
  if (nrhs != 5) 
    mexErrMsgTxt("Five input arguments required: dat_in,dim1,dim2,no_of_in_bytes,compression_type.");
  else if (nlhs != 1) 
    mexErrMsgTxt("One output argument has to be specified.");

  {
    int ind;
    for (ind = 0; ind < nrhs; ind++) {
      if(mxGetNumberOfDimensions(prhs[ind]) != 2) {
        printf("The %d. input argument must have two dimensions.",ind+1);
        mxErrMsgTxt("wrong number of dimensions");
      }
    }
  }
  
    /* check 1st input argument: input data */
  curr_arg = prhs[0];
  if (mxIsUint8(curr_arg) != 1)
    mexErrMsgTxt("Input 1 (input data) must be of type uint8.");
  data_in_start.uint8 = data_in.uint8 = (char *) mxGetPr(curr_arg);

    /* check 2nd input argument: dim1 */
  curr_arg = prhs[1];
  if (mxIsDouble(curr_arg) != 1)
    mexErrMsgTxt("Input 2 (dimension 1) must be of type double.");
  dim1 = mxGetScalar(curr_arg);
  if (dim1 < 1) {
    mexErrMsgTxt("Input 2 (dimension 1) must be at least 1.");
  }

    /* check 3rd input argument: dim2 */
  curr_arg = prhs[2];
  if (mxIsDouble(curr_arg) != 1)
    mexErrMsgTxt("Input 3 (dimension 2) must be of type double.");
  dim2 = mxGetScalar(curr_arg);
  if (dim2 < 1) {
    mexErrMsgTxt("Input 3 (dimension 2) must be at least 1.");
  }
  
    /* check 4th input argument: no_of_in_bytes */
  curr_arg = prhs[3];
  if (mxIsDouble(curr_arg) != 1)
    mexErrMsgTxt("Input 4 (no. of input bytes) must be of type double.");
  no_of_in_bytes = mxGetScalar(curr_arg);
  
    /* check 5th input argument: compression_type */
  curr_arg = prhs[4];
  if (mxIsDouble(curr_arg) != 1)
    mexErrMsgTxt("Input 5 (compression type) must be of type double.");
  compression_type = mxGetScalar(curr_arg);
  if (compression_type != 1) {
    mexErrMsgTxt("currently only compression type 1, byte-offset compression, is supported");
  }
  
    /* allocate memory for the output data */
  plhs[0] = mxCreateNumericMatrix(dim1, dim2, mxDOUBLE_CLASS, mxREAL);
  if (plhs[0] == NULL)
    mexErrMsgTxt("Could not allocate memory for return data.");
  frame_out_start = frame_out = mxGetPr(plhs[0]);

  val_curr = 0;
  while (data_in.uint8-data_in_start.uint8 < no_of_in_bytes) {
	if (*data_in.uint8 != 0x80) {
	  diff = (int) *data_in.int8++;
    } else {
  	  data_in.uint8++;
	  if (*data_in.uint16 != 0x8000) {
		diff = (int) *data_in.int16++;
      } else {
        data_in.uint16++;
		diff = (int) *data_in.int32++;
  	  }
    }
	val_curr += diff;
    *frame_out++ = (double) val_curr;
  }

  if (frame_out-frame_out_start != dim1*dim2) {
    printf("%ld bytes after uncompression, %ld bytes expected",
           frame_out-frame_out_start, dim1*dim2);
    mexErrMsgTxt("mismatch in number of extracted data bytes");
  }
  
  return;
}
コード例 #14
0
ファイル: dilate3d.c プロジェクト: jackjallen/MATLAB2
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){

  int             n, i, j ,k, I, J, K, IJ, I1, J1, K1;
  int             i0,i1,j0,j1,k0,k1,slice;
  mwSize          sizes[3];
  mxLogical       *V, *O;
  int             connect;

  if( nrhs < 1 ){
    mxErrMsgTxt("sintax error\n");
  }
  
  if( !mxIsLogical( prhs[0] ) ){
    mxErrMsgTxt("array have to be logical.\n");
  }
  if( myNDims( prhs[0] ) > 3 ){
    mxErrMsgTxt("bigger than 3d arrays is not allowed.\n");
  }
  
  if( nrhs > 1 ){
    connect = myGetValue( prhs[1] );
    if( connect != 26 && connect != 18 && connect != 6 ){
      mxErrMsgTxt("valid connects are 6, 18, 26.\n");
    }
  } else {
    connect = 26;
  }
  

  I  = mySize( prhs[0] , 0 );
  J  = mySize( prhs[0] , 1 );
  K  = mySize( prhs[0] , 2 );
  IJ = I*J;
  I1 = I-1;
  J1 = J-1;
  K1 = K-1;
  
  
  sizes[0] = I;
  sizes[1] = J;
  sizes[2] = K;
  
  V = (mxLogical *) mxGetData( prhs[0] );

  plhs[0] = mxCreateLogicalArray( 3 , sizes );
  O = (mxLogical *) mxGetData( plhs[0] );
  

  if( connect == 26 ){

    n = 0;
    for( k = 0 ; k < K ; k++ ){
      k0 = k > 0  ? k - 1 : k;
      k1 = k < K1 ? k + 1 : k;

      for( j = 0 ; j < J ; j++ ){
        j0 = j > 0  ? j - 1 : j;
        j1 = j < J1 ? j + 1 : j;

        for( i = 0 ; i < I ; i++ ){

          if( V[n] ){
            i0 = i > 0  ? i - 1 : i;
            i1 = i < I1 ? i + 1 : i;

            O( i0 , j0 , k0 ) = 1;
            O( i  , j0 , k0 ) = 1;
            O( i1 , j0 , k0 ) = 1;
            O( i0 , j  , k0 ) = 1;
            O( i  , j  , k0 ) = 1;
            O( i1 , j  , k0 ) = 1;
            O( i0 , j1 , k0 ) = 1;
            O( i  , j1 , k0 ) = 1;
            O( i1 , j1 , k0 ) = 1;

            O( i0 , j0 , k  ) = 1;
            O( i  , j0 , k  ) = 1;
            O( i1 , j0 , k  ) = 1;
            O( i0 , j  , k  ) = 1;
            O[ n            ] = 1;
            O( i1 , j  , k  ) = 1;
            O( i0 , j1 , k  ) = 1;
            O( i  , j1 , k  ) = 1;
            O( i1 , j1 , k  ) = 1;

            O( i0 , j0 , k1 ) = 1;
            O( i  , j0 , k1 ) = 1;
            O( i1 , j0 , k1 ) = 1;
            O( i0 , j  , k1 ) = 1;
            O( i  , j  , k1 ) = 1;
            O( i1 , j  , k1 ) = 1;
            O( i0 , j1 , k1 ) = 1;
            O( i  , j1 , k1 ) = 1;
            O( i1 , j1 , k1 ) = 1;

          }
          n++;
        }
      }
    }

  } else if( connect == 18 ){

    n = 0;
    for( k = 0 ; k < K ; k++ ){
      k0 = k > 0  ? k - 1 : k;
      k1 = k < K1 ? k + 1 : k;

      for( j = 0 ; j < J ; j++ ){
        j0 = j > 0  ? j - 1 : j;
        j1 = j < J1 ? j + 1 : j;

        for( i = 0 ; i < I ; i++ ){

          if( V[n] ){
            i0 = i > 0  ? i - 1 : i;
            i1 = i < I1 ? i + 1 : i;

            O( i  , j0 , k0 ) = 1;
            O( i0 , j  , k0 ) = 1;
            O( i  , j  , k0 ) = 1;
            O( i1 , j  , k0 ) = 1;
            O( i  , j1 , k0 ) = 1;

            O( i0 , j0 , k  ) = 1;
            O( i  , j0 , k  ) = 1;
            O( i1 , j0 , k  ) = 1;
            O( i0 , j  , k  ) = 1;
            O[ n            ] = 1;
            O( i1 , j  , k  ) = 1;
            O( i0 , j1 , k  ) = 1;
            O( i  , j1 , k  ) = 1;
            O( i1 , j1 , k  ) = 1;

            O( i  , j0 , k1 ) = 1;
            O( i0 , j  , k1 ) = 1;
            O( i  , j  , k1 ) = 1;
            O( i1 , j  , k1 ) = 1;
            O( i  , j1 , k1 ) = 1;

          }
          n++;
        }
      }
    }

  } else if( connect == 6 ){

    n = 0;
    for( k = 0 ; k < K ; k++ ){
      k0 = k > 0   ? k - 1 : k;
      k1 = k < K-1 ? k + 1 : k;

      for( j = 0 ; j < J ; j++ ){
        j0 = j > 0   ? j - 1 : j;
        j1 = j < J-1 ? j + 1 : j;

        for( i = 0 ; i < I ; i++ ){

          if( V[n] ){
            i0 = i > 0   ? i - 1 : i;
            i1 = i < I-1 ? i + 1 : i;

            O( i0 , j  , k  ) = 1;
            O( i  , j  , k  ) = 1;
            O( i1 , j  , k  ) = 1;

            O( i  , j0 , k  ) = 1;
            O[ n            ] = 1;
            O( i  , j1 , k  ) = 1;

            O( i  , j  , k0 ) = 1;
            O( i  , j  , k  ) = 1;
            O( i  , j  , k1 ) = 1;

          }
          n++;
        }
      }
    }

  }

  
}
コード例 #15
0
void mexFunction(
    int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[]
)
{
  int display=0, i=0;
  long *lpenv=NULL ;
  CPXENVptr     env = NULL;
  int           status ;
  double value ;
  char param_name[128] ;
  int param_code=-1, dblfound=0, strfound=0 ;
  
  if (nrhs > 7 || nrhs < 1) {
    mexErrMsgTxt("Usage: [how] "
		 "= lp_set_param(lpenv, param_name, value, display)");
    return;
  }
  switch (nrhs) {
  case 4:
    if (mxGetM(prhs[3]) != 0 || mxGetN(prhs[3]) != 0) {
      if (!mxIsNumeric(prhs[3]) || mxIsComplex(prhs[3]) 
	  ||  mxIsSparse(prhs[3])
	  || !(mxGetM(prhs[3])==1 && mxGetN(prhs[3])==1)) {
	mexErrMsgTxt("4th argument (display) must be "
		     "an integer scalar.");
	return;
      }
      display = *mxGetPr(prhs[3]);
    }
  case 3:
    if (mxGetM(prhs[2]) != 0 || mxGetN(prhs[2]) != 0) {
      if (!mxIsNumeric(prhs[2]) || mxIsComplex(prhs[2]) 
	  ||  mxIsSparse(prhs[2])
	  || !(mxGetM(prhs[2])==1 && mxGetN(prhs[2])==1)) {
	mexErrMsgTxt("3rd argument (value) must be "
		     "an integer scalar.");
	return;
      }
      value = *mxGetPr(prhs[2]);
    }
  case 2:
    if (mxGetM(prhs[1]) != 0 || mxGetN(prhs[1]) != 0) {
      if (mxIsNumeric(prhs[1]) || mxIsComplex(prhs[1]) 
	  ||  mxIsSparse(prhs[1]) || !mxIsChar(prhs[1])
	  || !(mxGetM(prhs[1])==1) && mxGetN(prhs[1])>=1) {
	mexErrMsgTxt("2nd argument (param) must be "
		     "a string.");
	return;
      }
      mxGetString(prhs[1], param_name, 128);
    }
  case 1:
    if (mxGetM(prhs[0]) != 0 || mxGetN(prhs[0]) != 0) {
      if (!mxIsNumeric(prhs[0]) || mxIsComplex(prhs[0]) 
	  ||  mxIsSparse(prhs[0])
	  || !mxIsDouble(prhs[0]) 
	  ||  mxGetN(prhs[0])!=1 ) {
	mexErrMsgTxt("1st argument (lpenv) must be "
		     "a column vector.");
	return;
      }
      if (1 != mxGetM(prhs[0])) {
	mexErrMsgTxt("Dimension error (arg 1).");
	return;
      }
      lpenv = (long*) mxGetPr(prhs[0]);
    }
  }
  
  if (nlhs > 1 || nlhs < 1) {
    mexErrMsgTxt("Usage: [how] "
		 "= lp_set_param(lpenv,param_name,value,disp)");
    return;
  }
  if (display>2) fprintf(STD_OUT, "argument processing finished\n") ;

  /* Initialize the CPLEX environment */
  env = (CPXENVptr) lpenv[0] ;

  for (i=0; i<NUM_PARAMS; i++)
      if (strcmp(param_info[i].name, param_name)==0)
	  param_code=param_info[i].code ;

  if (display>3) 
    fprintf(STD_OUT, "(param=%s(%i), value=%f) \n", param_name, param_code, value) ;
  if (param_code==-1)
    mxErrMsgTxt("illegal parameter name") ;

  for (i=0; i<NUM_DBLPARAMS; i++)
    if (param_code==dblParams[i])
      dblfound=1 ;
  for (i=0; i<NUM_STRPARAMS; i++)
    if (param_code==strParams[i])
      strfound=1 ;
  if (dblfound==1) {
    if (display>2) 
      fprintf(STD_OUT, "calling CPXsetdblparam\n") ;
    status = CPXsetdblparam(env, param_code, value);
    if ( status ) {
      fprintf (STD_OUT, "CPXsetdblparam failed.\n");
      goto TERMINATE;
    } 
  } else if (strfound==1)
  {
	  fprintf(STD_OUT, "sorry not implemented\n") ;
  } else {
    if (display>2) 
      fprintf(STD_OUT, "calling CPXsetintparam\n") ;
    status = CPXsetintparam(env, param_code, (int)value);
    if ( status ) {
      fprintf (STD_OUT, "CPXsetintparam failed.\n");
      goto TERMINATE;
    }
  } ;

 TERMINATE:
  if (status) {
    char  errmsg[1024];
    CPXgeterrorstring (env, status, errmsg);
    fprintf (STD_OUT, "%s", errmsg);
    if (nlhs >= 1) 
      plhs[0] = mxCreateString(errmsg) ;
  } else
    if (nlhs >= 1) 
      plhs[0] = mxCreateString("OK") ;
  ;
  return ;
}