Beispiel #1
0
static int
p_get_variable(void)
{
  YAP_Term t;
  mxArray *mat;
  const mwSize *dims;
  int ndims;

  mat = get_array(YAP_ARG1);
  if (!mat)
    return FALSE;
  dims = mxGetDimensions(mat);
  ndims = mxGetNumberOfDimensions(mat);
  if (mxIsInt32(mat)) {
    INT32_T *input = (INT32_T *)mxGetPr(mat);
    t = cp_ints32(ndims, (int *)dims, input, 1, 0, YAP_TermNil());
  } else if (mxIsInt64(mat)) {
    INT64_T *input = (INT64_T *)mxGetPr(mat);
    t = cp_ints64(ndims, (int *)dims, input, 1, 0, YAP_TermNil());
  } else if (mxIsInt32(mat) || mxIsInt64(mat) || mxIsCell(mat)) {
    t = cp_cells(ndims, (int *)dims, mat, 1, 0, YAP_TermNil());
  } else if (mxIsDouble(mat)) {
    double *input = mxGetPr(mat);
    t = cp_floats(ndims, (int *)dims, input, 1, 0, YAP_TermNil());
  } else {
    return FALSE;
  }
  return YAP_Unify(YAP_ARG2, t);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    // Check for proper number of arguments
    if (nrhs != 2)
        mexErrMsgTxt("Two input arguments required.");
    if (nlhs != 4)
        mexErrMsgTxt("Four output arguments required.");
    
    // The input must be noncomplex
    if (mxIsComplex(prhs[0]) || mxIsComplex(prhs[1]) ||
        !mxIsNumeric(prhs[0]) || !mxIsNumeric(prhs[1]))
        mexErrMsgTxt("The input must be noncomplex (and numeric).");

    // The second input must be an unsigned integer
    if (mxIsSingle(prhs[1]) || mxIsDouble(prhs[1]))
        mexErrMsgTxt("The second input must be an integer.");

    if (mxIsSingle(prhs[0])) {
        if (mxIsInt8(prhs[1]))
            compute<float, char>(plhs, prhs);
        else if (mxIsUint8(prhs[1]))
            compute<float, unsigned char>(plhs, prhs);
        else if (mxIsInt16(prhs[1]))
            compute<float, short>(plhs, prhs);
        else if (mxIsUint16(prhs[1]))
            compute<float, unsigned short>(plhs, prhs);
        else if (mxIsInt32(prhs[1]))
            compute<float, int>(plhs, prhs);
        else if (mxIsUint32(prhs[1]))
            compute<float, unsigned int>(plhs, prhs);
        else if (mxIsInt64(prhs[1]))
            compute<float, long long int>(plhs, prhs);
        else if (mxIsUint64(prhs[1]))
            compute<float, unsigned long long int>(plhs, prhs);
    }
    else if (mxIsDouble(prhs[0])) {
        if (mxIsInt8(prhs[1]))
            compute<double, char>(plhs, prhs);
        else if (mxIsUint8(prhs[1]))
            compute<double, unsigned char>(plhs, prhs);
        else if (mxIsInt16(prhs[1]))
            compute<double, short>(plhs, prhs);
        else if (mxIsUint16(prhs[1]))
            compute<double, unsigned short>(plhs, prhs);
        else if (mxIsInt32(prhs[1]))
            compute<double, int>(plhs, prhs);
        else if (mxIsUint32(prhs[1]))
            compute<double, unsigned int>(plhs, prhs);
        else if (mxIsInt64(prhs[1]))
            compute<double, long long int>(plhs, prhs);
        else if (mxIsUint64(prhs[1]))
            compute<double, unsigned long long int>(plhs, prhs);
    }
    else
        mexErrMsgTxt("Input types not supported.");
    
}
Beispiel #3
0
void mx2cmat(int m, int n, cmulti **A, int LDA, const mxArray *src)
{
  mwSize size[2]={1,1};
  mxArray *value=NULL;
  int i,j,k;

  for(j=0; j<n; j++){
    for(i=0; i<m; i++){
      // real part
      // prec
      value=mxGetField(src,j*m+i,"r_prec");
      if(value!=NULL && mxIsInt64(value)){ rround(C_R(MAT(A,i,j,LDA)),(*(int64_t*)mxGetData(value))); }
      else{ mexErrMsgIdAndTxt("MATLAB:mx2cmat","The arg should be Struct with the feild 'prec'."); }
      // sign
      value=mxGetField(src,j*m+i,"r_sign");
      if(value!=NULL && mxIsInt32(value)){ C_R(MAT(A,i,j,LDA))->_mpfr_sign=(*(int32_t*)mxGetData(value)); }
      else{ mexErrMsgIdAndTxt("MATLAB:mx2cmat","The arg should be Struct with the feild 'sign'."); }
      // exp
      value=mxGetField(src,j*m+i,"r_exp");
      if(value!=NULL && mxIsInt64(value)){ C_R(MAT(A,i,j,LDA))->_mpfr_exp=(*(int64_t*)mxGetData(value)); }
      else{ mexErrMsgIdAndTxt("MATLAB:mx2cmat","The arg should be Struct with the feild 'exp'."); }
      // digits
      value=mxGetField(src,j*m+i,"r_digits");
      if(value!=NULL && mxIsUint64(value)){
	for(k=0; k<rget_size(C_R(MAT(A,i,j,LDA))); k++){
	  C_R(MAT(A,i,j,LDA))->_mpfr_d[k]=((uint64_t*)mxGetData(value))[k];
	}
      }
      else{ mexErrMsgIdAndTxt("MATLAB:mx2cmat","The arg should be Struct with the feild 'digits'."); }

      // imaginary part
      // prec
      value=mxGetField(src,j*m+i,"i_prec");
      if(value!=NULL && mxIsInt64(value)){ rround(C_I(MAT(A,i,j,LDA)),(*(int64_t*)mxGetData(value))); }
      else{ mexErrMsgIdAndTxt("MATLAB:mx2cmat","The arg should be Struct with the feild 'prec'."); }
      // sign
      value=mxGetField(src,j*m+i,"i_sign");
      if(value!=NULL && mxIsInt32(value)){ C_I(MAT(A,i,j,LDA))->_mpfr_sign=(*(int32_t*)mxGetData(value)); }
      else{ mexErrMsgIdAndTxt("MATLAB:mx2cmat","The arg should be Struct with the feild 'sign'."); }
      // exp
      value=mxGetField(src,j*m+i,"i_exp");
      if(value!=NULL && mxIsInt64(value)){ C_I(MAT(A,i,j,LDA))->_mpfr_exp=(*(int64_t*)mxGetData(value)); }
      else{ mexErrMsgIdAndTxt("MATLAB:mx2cmat","The arg should be Struct with the feild 'exp'."); }
      // digits
      value=mxGetField(src,j*m+i,"i_digits");
      if(value!=NULL && mxIsUint64(value)){
	for(k=0; k<rget_size(C_I(MAT(A,i,j,LDA))); k++){
	  C_I(MAT(A,i,j,LDA))->_mpfr_d[k]=((uint64_t*)mxGetData(value))[k];
	}
      }
      else{ mexErrMsgIdAndTxt("MATLAB:mx2cmat","The arg should be Struct with the feild 'digits'."); }
    }
  }
  return;
}
Beispiel #4
0
mxLogical isInt( const mxArray *pm ) {
    /* We typedef "integer" to be "long int", and this is not
     * constant across different computers.
     *
     * CHAR_BIT is from limits.h
     * If using gcc, you can run `gcc -dM -E - < /dev/null | grep CHAR_BIT`
     *  and it should define the symbol __CHAR_BIT__, so this is another way.
     *
     *  This will match the typdef "integer" which is what
     *  the lbfgsb codes uses for integers.
     * */
    
    /* debugPrintf("Sizeof(int) is %d\n", sizeof(int) ); */
    switch ( CHAR_BIT * sizeof(integer) ) {
        case 16 :
            return mxIsInt16(pm);
        case 32:
            return mxIsInt32(pm);
        case 64:
            return mxIsInt64(pm);
        default:
            mexErrMsgTxt("You have a weird computer that I don't know how to support");
            return false;
    }
}
Beispiel #5
0
int mxGetElementSize(const mxArray *ptr)
{
    if (mxIsChar(ptr))
    {
        return sizeof(wchar_t*);
    }
    else if (mxIsLogical(ptr))
    {
        return sizeof(int);
    }
    else if (mxIsDouble(ptr))
    {
        return sizeof(double);
    }
    else if (mxIsSparse(ptr))
    {
        return sizeof(double);
    }
    else if (mxIsInt8(ptr))
    {
        return sizeof(char);
    }
    else if (mxIsInt16(ptr))
    {
        return sizeof(short);
    }
    else if (mxIsInt32(ptr))
    {
        return sizeof(int);
    }
    else if (mxIsInt64(ptr))
    {
        return sizeof(long long);
    }
    else if (mxIsUint8(ptr))
    {
        return sizeof(unsigned char);
    }
    else if (mxIsUint16(ptr))
    {
        return sizeof(unsigned short);
    }
    else if (mxIsUint32(ptr))
    {
        return sizeof(unsigned int);
    }
    else if (mxIsUint64(ptr))
    {
        return sizeof(unsigned long long);
    }
    else if (mxIsCell(ptr))
    {
        return sizeof(types::InternalType*);
    }
    else if (mxIsStruct(ptr))
    {
        return sizeof(types::SingleStruct*);
    }
    return 0;
}
Beispiel #6
0
const char *mxGetClassName(const mxArray *ptr)
{
    if (mxIsDouble(ptr))
    {
        return "double";
    }
    if (mxIsChar(ptr))
    {
        return "char";
    }
    if (mxIsLogical(ptr))
    {
        return "bool";
    }
    if (mxIsSparse(ptr))
    {
        return "sparse";
    }
    if (mxIsInt8(ptr))
    {
        return "int8";
    }
    if (mxIsInt16(ptr))
    {
        return "int16";
    }
    if (mxIsInt32(ptr))
    {
        return "int32";
    }
    if (mxIsInt64(ptr))
    {
        return "int64";
    }
    if (mxIsUint8(ptr))
    {
        return "uint8";
    }
    if (mxIsUint16(ptr))
    {
        return "uint16";
    }
    if (mxIsUint32(ptr))
    {
        return "uint32";
    }
    if (mxIsUint64(ptr))
    {
        return "uint64";
    }
    if (mxIsCell(ptr))
    {
        return "cell";
    }
    if (mxIsStruct(ptr))
    {
        return "struct";
    }
    return "unknown";
}
Beispiel #7
0
static int
item2(YAP_Term tvar, YAP_Term titem, int offx, int offy)
{
  mxArray *mat;
  int rows;
  int cols;
  int off;

  mat = get_array(tvar);
  rows = mxGetM(mat);
  cols = mxGetN(mat);
  off = MAT_ACCESS(offx,offy,rows,cols);
  if (!mat)
    return FALSE;
  if (mxIsInt32(mat)) {
    INT32_T *input = (INT32_T *)mxGetPr(mat);
    if (YAP_IsIntTerm(titem)) {
      input[off] = YAP_IntOfTerm(titem);
    } else if (YAP_IsFloatTerm(titem)) {
      input[off] = YAP_FloatOfTerm(titem);
    } else if (YAP_IsVarTerm(titem)) {
      return YAP_Unify(titem, YAP_MkIntTerm(input[off]));
    } else
      return FALSE;
  } else if (mxIsInt64(mat)) {
    INT64_T *input = (INT64_T *)mxGetPr(mat);
    if (YAP_IsIntTerm(titem)) {
      input[off] = YAP_IntOfTerm(titem);
    } else if (YAP_IsFloatTerm(titem)) {
      input[off] = YAP_FloatOfTerm(titem);
    } else if (YAP_IsVarTerm(titem)) {
      return YAP_Unify(titem, YAP_MkIntTerm(input[off]));
    } else
      return FALSE;
  } else if (mxIsCell(mat)) {
    if (YAP_IsVarTerm(titem)) {
      return YAP_Unify(titem, YAP_MkIntTerm((YAP_Int)mxGetCell(mat,off)));
    } else {
      mxArray *mat2 = get_array(titem);
      mxSetCell(mat,off, mat2);
    }
  } else if (mxIsDouble(mat)) {
    double *input = mxGetPr(mat);
    if (YAP_IsFloatTerm(titem)) {
      input[off] = YAP_FloatOfTerm(titem);
    } else if (YAP_IsIntTerm(titem)) {
      input[off] = YAP_IntOfTerm(titem);
    } else {
      return YAP_Unify(titem, YAP_MkFloatTerm(input[off]));
    }
  } else
    return FALSE;
  return cp_back(tvar, mat);
}
Beispiel #8
0
void mxSetImagData(mxArray *array_ptr, void *data_ptr)
{
    if (mxIsChar(array_ptr))
    {
        ((String *)array_ptr)->setImg((wchar_t **)data_ptr);
    }
    else if (mxIsDouble(array_ptr))
    {
        ((Double *)array_ptr)->setImg((double *)data_ptr);
    }
    else if (mxIsInt8(array_ptr))
    {
        ((Int8 *)array_ptr)->setImg((char *)data_ptr);
    }
    else if (mxIsInt16(array_ptr))
    {
        ((Int16 *)array_ptr)->setImg((short *)data_ptr);
    }
    else if (mxIsInt32(array_ptr))
    {
        ((Int32 *)array_ptr)->setImg((int *)data_ptr);
    }
    else if (mxIsInt64(array_ptr))
    {
        ((Int64 *)array_ptr)->setImg((long long *)data_ptr);
    }
    else if (mxIsLogical(array_ptr))
    {
        ((Bool *)array_ptr)->setImg((int *)data_ptr);
    }
    // else if (mxIsSingle(array_ptr)) {
    //   ((Float *) array_ptr)->setImg((float *) data_ptr);
    // }
    else if (mxIsUint8(array_ptr))
    {
        ((UInt8 *)array_ptr)->setImg((unsigned char *)data_ptr);
    }
    else if (mxIsUint16(array_ptr))
    {
        ((UInt16 *)array_ptr)->setImg((unsigned short *)data_ptr);
    }
    else if (mxIsUint32(array_ptr))
    {
        ((UInt32 *)array_ptr)->setImg((unsigned int *)data_ptr);
    }
    else if (mxIsUint64(array_ptr))
    {
        ((UInt64 *)array_ptr)->setImg((unsigned long long *) data_ptr);
    }
}
Beispiel #9
0
static int
item1(YAP_Term tvar, YAP_Term titem, int off)
{
  mxArray *mat;
  mat = get_array(tvar);
  if (!mat)
    return FALSE;
  if (mxIsInt32(mat)) {
    INT32_T *input = (INT32_T *)mxGetPr(mat);
    if (YAP_IsIntTerm(titem)) {
      input[off] = YAP_IntOfTerm(titem);
    } else if (YAP_IsFloatTerm(titem)) {
      input[off] = YAP_FloatOfTerm(titem);
    } else if (YAP_IsVarTerm(titem)) {
      return YAP_Unify(titem, YAP_MkIntTerm(input[off]));
    } else
      return FALSE;
  } else if (mxIsInt64(mat)) {
    INT64_T *input = (INT64_T *)mxGetPr(mat);
    if (YAP_IsIntTerm(titem)) {
      input[off] = YAP_IntOfTerm(titem);
    } else if (YAP_IsFloatTerm(titem)) {
      input[off] = YAP_FloatOfTerm(titem);
    } else if (YAP_IsVarTerm(titem)) {
      return YAP_Unify(titem, YAP_MkIntTerm(input[off]));
    } else
      return FALSE;
  } else if (mxIsCell(mat)) {
    if (YAP_IsVarTerm(titem)) {
      return YAP_Unify(titem, YAP_MkIntTerm((YAP_Int)mxGetCell(mat,off)));
    } else {
      mxArray *mat2 = get_array(titem);
      mxSetCell(mat,off, mat2);
    }
  } else if (mxIsDouble(mat)) {
    double *input = mxGetPr(mat);
    if (YAP_IsFloatTerm(titem)) {
      input[off] = YAP_FloatOfTerm(titem);
    } else if (YAP_IsIntTerm(titem)) {
      input[off] = YAP_IntOfTerm(titem);
    } else {
      return YAP_Unify(titem, YAP_MkFloatTerm(input[off]));
    }
  } else
    return FALSE;
  return cp_back(tvar, mat);
}
Beispiel #10
0
void mxSetImagData(mxArray *array_ptr, void *data_ptr)
{
    if (mxIsChar(array_ptr))
    {
        ((types::String *)array_ptr)->setImg((wchar_t **)data_ptr);
    }
    else if (mxIsDouble(array_ptr))
    {
        ((types::Double *)array_ptr)->setImg((double *)data_ptr);
    }
    else if (mxIsInt8(array_ptr))
    {
        ((types::Int8 *)array_ptr)->setImg((char *)data_ptr);
    }
    else if (mxIsInt16(array_ptr))
    {
        ((types::Int16 *)array_ptr)->setImg((short *)data_ptr);
    }
    else if (mxIsInt32(array_ptr))
    {
        ((types::Int32 *)array_ptr)->setImg((int *)data_ptr);
    }
    else if (mxIsInt64(array_ptr))
    {
        ((types::Int64 *)array_ptr)->setImg((long long *)data_ptr);
    }
    else if (mxIsLogical(array_ptr))
    {
        ((types::Bool *)array_ptr)->setImg((int *)data_ptr);
    }
    else if (mxIsUint8(array_ptr))
    {
        ((types::UInt8 *)array_ptr)->setImg((unsigned char *)data_ptr);
    }
    else if (mxIsUint16(array_ptr))
    {
        ((types::UInt16 *)array_ptr)->setImg((unsigned short *)data_ptr);
    }
    else if (mxIsUint32(array_ptr))
    {
        ((types::UInt32 *)array_ptr)->setImg((unsigned int *)data_ptr);
    }
    else if (mxIsUint64(array_ptr))
    {
        ((types::UInt64 *)array_ptr)->setImg((unsigned long long *) data_ptr);
    }
}
Beispiel #11
0
/* Main mex gateway routine */
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )   { 
    
    integer iprint = (integer)1;
    integer task=(integer)START, csave=(integer)1;
    integer iterations = 0;
    integer total_iterations = 0;

    int  iterMax = 100;
    int  total_iterMax = 200;


    integer   n, m, *nbd=NULL, *iwa=NULL; 
    double  f=0, factr, pgtol, *x, *l, *u, *g, *wa=NULL;
    int     i;
    mxLogical FREE_nbd=false;

    int ndim = 2; /* for lcc compiler, must declare these here, not later ... */
    mwSize dims[2] = { LENGTH_ISAVE, 1 };

    logical lsave[LENGTH_LSAVE];
    integer isave[LENGTH_ISAVE];
    double  dsave[LENGTH_DSAVE];
    
    double *nbd_dbl=NULL;
    long long *nbd_long=NULL;
    
    mxArray *LHS[2];
    mxArray *RHS[3];
    double *tempX, *tempG, *tempIter;
    
    /* Parse inputs. Quite boring */
    
    if (nrhs < 5 ) mexErrMsgTxt("Needs at least 5 input arguments");
    m       = (int)*mxGetPr( prhs[N_m] );
    n       = (integer)mxGetM( prhs[N_x] );
    if ( mxGetN(prhs[N_x]) != 1 ) mexErrMsgTxt("x must be a column vector");
    if ( mxGetM(prhs[N_l]) != n ) mexErrMsgTxt("l must have same size as x");
    if ( mxGetM(prhs[N_u]) != n ) mexErrMsgTxt("u must have same size as x");
    if ( mxGetM(prhs[N_nbd]) != n ) mexErrMsgTxt("nbd must have same size as x");


    if (nlhs < 2 )  mexErrMsgTxt("Should have 2 or 3 output arguments");
    if (!mxIsDouble(prhs[N_x]))
            mexErrMsgTxt("x should be of type double!\n");
    plhs[1] = mxDuplicateArray( prhs[N_x] );
    x       = mxGetPr( plhs[1] );


    l       = mxGetPr( prhs[N_l] );
    u       = mxGetPr( prhs[N_u] );
    if ( isInt( prhs[N_nbd] ) ) {
        nbd     = (integer *)mxGetData( prhs[N_nbd] ); 
    } else {
        debugPrintf("Converting nbd array to integers\n" );
        if (!mxIsDouble(prhs[N_nbd])){
            if (mxIsInt64(prhs[N_nbd])){
                nbd_long = mxGetData( prhs[N_nbd] );
                nbd     = (integer *)mxMalloc( n * sizeof(integer) );
                assert( nbd != NULL );
                FREE_nbd = true;
                /* convert nbd_dbl (in double format) to integers */
                for (i=0;i<n;i++)
                    nbd[i]  = (integer)nbd_long[i];
            } else {
                debugPrintf("Sizeof(int) is %d bits, sizeof(integer) is %d bits\n",
                        CHAR_BIT*sizeof(int),CHAR_BIT*sizeof(integer) );
                /* integer is aliased to 'long int' and should be at least
                 * 32 bits. 'long long' should be at least 64 bits.
                 * On 64-bit Windows, it seems 'long int' is exactly 32 bits,
                 * while on 64-bit linux and Mac, it is 67 bits */
                debugPrintf("Nbd is of type %s\n", mxGetClassName( prhs[N_nbd] ) );
                mexErrMsgTxt("Nbd array not doubles or type int64!\n");
            }
        } else {
            nbd_dbl = mxGetPr( prhs[N_nbd] );
            nbd     = (integer *)mxMalloc( n * sizeof(integer) );
            assert( nbd != NULL );
            FREE_nbd = true;
            /* convert nbd_dbl (in double format) to integers */
            for (i=0;i<n;i++)
                nbd[i]  = (integer)nbd_dbl[i];
        }
    }


    /* some scalar parameters */
    if ( nrhs < N_factr+1 ) 
        factr   = 1.0e7;
    else if (mxGetNumberOfElements( prhs[N_factr] )!=1)
        factr   = 1.0e7;
    else {
        factr   = (double)mxGetScalar( prhs[N_factr] );
        if (factr < 0 )
            mexErrMsgTxt("factr must be >= 0\n");
    }

    if ( nrhs < N_pgtol+1 ) 
        pgtol   = 1.0e-5;
    else if (mxGetNumberOfElements( prhs[N_pgtol] )!=1)
        pgtol   = 1.0e-5;
    else {
        pgtol   = (double)mxGetScalar( prhs[N_pgtol] );
        if (pgtol < 0)
            mexErrMsgTxt("pgtol must be >= 0\n");
    }
    if ( nrhs < N_iprint+1 ) {
        iprint  = (integer)1;
    } else if (mxGetNumberOfElements( prhs[N_iprint] )!=1) {
        iprint  = (integer)1;
    } else {
        iprint = (integer)mxGetScalar( prhs[N_iprint] );
    }
    
    if ( nrhs >= N_iterMax+1 ) 
        iterMax = (int)mxGetScalar( prhs[N_iterMax] );
    if ( nrhs >= N_total_iterMax+1 ) 
        total_iterMax = (int)mxGetScalar( prhs[N_total_iterMax] );
    
    /* allocate memory for arrays */
    g   = (double *)mxMalloc( n * sizeof(double) );
    assert( g != NULL );
    wa      = (double *)mxMalloc( (2*m*n + 5*n + 11*m*m + 8*m ) * sizeof(double) );
    assert( wa != NULL );
    iwa     = (integer *)mxMalloc( (3*n)*sizeof(integer) );
    assert( iwa != NULL );
    

            
    /* -- Finally, done with parsing inputs. Now, call lbfgsb fortran routine */
    
    /* Be careful! This modifies many variables in-place! 
     * Basically, anything without a '&' before it will be changed in the Matlab
     * workspace */
    
    if ( nrhs < N_fcn - 1 )
        mexErrMsgTxt("For this f(x) feature, need more input aguments\n");
    RHS[0] = mxDuplicateArray( prhs[N_fcn] );
    RHS[1] = mxCreateDoubleMatrix(n,1,mxREAL);
    RHS[2] = mxCreateDoubleScalar( 0.0 ); /* The iterations counter */
    tempX = (double*)mxGetPr( RHS[1] );
    if (!mxIsDouble(RHS[2]))
        mexErrMsgTxt("Error trying to create RHS[2]\n");
    tempIter = (double*)mxGetPr( RHS[2] );

    while ( (iterations < iterMax) && (total_iterations < total_iterMax ) ){
        total_iterations++;

        setulb_auto(&n,&m,x,l,u,nbd,&f,g,&factr,&pgtol,wa,iwa,&task,&iprint,
                &csave,lsave,isave,dsave); /* (ftnlen) TASK_LEN, (ftnlen) CSAVE_LEN); */


        if ( IS_FG(task) ) {

            /* copy data from x to RHS[1] or just set pointer with mxSetPr */
            for (i=0;i<n;i++)
                tempX[i] = x[i];
            /*Try being bold: */
            /*mxSetPr( RHS[1], x ); */

            *tempIter = (double)iterations;
            mexCallMATLAB(2,LHS,3,RHS,"feval");
            f = mxGetScalar( LHS[0] );
            if (mxGetM(LHS[1]) != n )
                mexErrMsgTxt("Error with [f,g]=fcn(x) : g wrong size\n");
            if (mxGetN(LHS[1]) != 1 )
                mexErrMsgTxt("Error with [f,g]=fcn(x) : g wrong size (should be column vector)\n");

            /* could use memcpy, or just do it by hand... */
            if (!mxIsDouble(LHS[1]))
                mexErrMsgTxt("[f,g]=fcn(x) did not return g as type double\n");
            tempG = mxGetPr( LHS[1] );
            for (i=0;i<n;i++)
                g[i] = tempG[i];
            /* Or, be a bit bolder: */
            /*g = tempG; // Hmm, crashed */

            continue;
        }
        if ( task==NEW_X ) {
            iterations++;
            continue;
        } else
            break;

    }

    mxDestroyArray( LHS[0] );
    mxDestroyArray( LHS[1] );
    mxDestroyArray( RHS[0] );
    mxDestroyArray( RHS[1] );
            

    
    plhs[0] = mxCreateDoubleScalar( f );
    if ( nlhs >= 3 )
        plhs[2] = mxCreateDoubleScalar( task );
    if ( nlhs >= 4 )
        plhs[3] = mxCreateDoubleScalar( iterations );
    if ( nlhs >= 5 )
        plhs[4] = mxCreateDoubleScalar( total_iterations );
    if ( nlhs >= 6 )
        mexErrMsgTxt("Did not expect more than 5 outputs\n");

    if (FREE_nbd)
        mxFree(nbd);
    mxFree(g);
    mxFree(wa);
    mxFree(iwa);

    return;
}
Beispiel #12
0
int mxSetDimensions(mxArray *array_ptr, const int *dims, int ndim)
{
    if (mxIsCell(array_ptr))
    {
        ((types::Cell *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsChar(array_ptr))
    {
        ((types::String *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsDouble(array_ptr))
    {
        ((types::Double *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsSparse(array_ptr))
    {
        //TODO
    }
    else if (mxIsInt8(array_ptr))
    {
        ((types::Int8 *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsInt16(array_ptr))
    {
        ((types::Int16 *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsInt32(array_ptr))
    {
        ((types::Int32 *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsInt64(array_ptr))
    {
        ((types::Int64 *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsLogical(array_ptr))
    {
        ((types::Bool *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsStruct(array_ptr))
    {
        ((types::Struct *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsUint8(array_ptr))
    {
        ((types::UInt8 *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsUint16(array_ptr))
    {
        ((types::UInt16 *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsUint32(array_ptr))
    {
        ((types::UInt32 *)array_ptr)->resize((int *)dims, ndim);
    }
    else if (mxIsUint64(array_ptr))
    {
        ((types::UInt64 *)array_ptr)->resize((int *)dims, ndim);
    }

    return 0;
}
Beispiel #13
0
int mxIsClass(const mxArray *ptr, const char *name)
{
    if (strcmp(name, "cell") == 0)
    {
        return mxIsCell(ptr);
    }
    if (strcmp(name, "char") == 0)
    {
        return mxIsChar(ptr);
    }
    if (strcmp(name, "double") == 0)
    {
        return mxIsDouble(ptr);
    }
    if (strcmp(name, "int8") == 0)
    {
        return mxIsInt8(ptr);
    }
    if (strcmp(name, "int16") == 0)
    {
        return mxIsInt16(ptr);
    }
    if (strcmp(name, "int32") == 0)
    {
        return mxIsInt32(ptr);
    }
    if (strcmp(name, "int64") == 0)
    {
        return mxIsInt64(ptr);
    }
    if (strcmp(name, "logical") == 0)
    {
        return mxIsLogical(ptr);
    }
    if (strcmp(name, "single") == 0)
    {
        return mxIsSingle(ptr);
    }
    if (strcmp(name, "struct") == 0)
    {
        return mxIsStruct(ptr);
    }
    if (strcmp(name, "uint8") == 0)
    {
        return mxIsUint8(ptr);
    }
    if (strcmp(name, "uint16") == 0)
    {
        return mxIsUint16(ptr);
    }
    if (strcmp(name, "uint32") == 0)
    {
        return mxIsUint32(ptr);
    }
    if (strcmp(name, "uint64") == 0)
    {
        return mxIsUint64(ptr);
    }
    // TODO: how to handle <class_name> and <class_id>?
    return 0;
}
Beispiel #14
0
int mxIsNumeric(const mxArray *ptr)
{
    return mxIsDouble(ptr) || mxIsSingle(ptr) ||
           mxIsInt8(ptr) || mxIsUint8(ptr) ||
           mxIsInt16(ptr) || mxIsUint16(ptr) || mxIsInt32(ptr) || mxIsUint32(ptr) || mxIsInt64(ptr) || mxIsUint64(ptr);
}