该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
/* Main mex gateway routine */
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) {
int_F iprint = 1;
char task[LENGTH_STRING], csave[LENGTH_STRING];
char *task_m, *csave_m;
double *isave_m, *lsave_m, *dsave_m; /* temporary pointers */
int_F *isave_int;
mxLogical *lsave_bool;
fortranLogical lsave[LENGTH_LSAVE];
int_F n, m, *nbd, *iwa, isave[LENGTH_ISAVE];
double *nbd_dbl, *iwa_dbl;
double f, factr, pgtol, *x, *l, *u, *g, dsave[LENGTH_DSAVE], *wa;
int i;
mxClassID classID;
int ndim = 2; /* for lcc compiler, must declare these here, not later ... */
mwSize dims[2] = { LENGTH_ISAVE, 1 };
/* for copying the data */
mxLogical *lsave_out;
int *isave_out;
double *dsave_out;
/* Parse inputs. Quite boring */
if (nrhs < 5 ) mexErrMsgTxt("Needs at least 5 input arguments");
m = (int)*mxGetPr( prhs[N_m] );
n = 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");
x = mxGetPr( prhs[N_x] );
l = mxGetPr( prhs[N_l] );
u = mxGetPr( prhs[N_u] );
if ( isInt( prhs[N_nbd] ) ) {
nbd = (int_F *)mxGetData( prhs[N_nbd] );
} else {
nbd_dbl = mxGetPr( prhs[N_nbd] );
nbd = (int_F *)mxMalloc( n * sizeof(int) );
assert( nbd != NULL );
/* convert nbd_dbl (in double format) to integers */
for (i=0;i
nbd[i] = (int)nbd_dbl[i];
}
/* f and g, the function and its gradient */
if ( nrhs < N_f+1 )
f = 0.0;
else if (mxGetNumberOfElements( prhs[N_f] )!=1)
f = 0.0;
else{
f = *mxGetPr( prhs[N_f] );
}
if (nrhs < N_g-1 ) {
g = (double *)mxMalloc( n * sizeof(double) );
assert( g != NULL );
}else{
if ( mxGetM(prhs[N_g]) != n ) mexErrMsgTxt("g must have same size as x");
g = mxGetPr( prhs[N_g] );
}
/* some scalar parameters */
if ( nrhs < N_factr+1 )
factr = 1.0e7;
else if (mxGetNumberOfElements( prhs[N_factr] )!=1)
factr = 1.0e7;
else
factr = *mxGetPr( prhs[N_factr] );
if ( nrhs < N_pgtol+1 )
pgtol = 1.0e-5;
else if (mxGetNumberOfElements( prhs[N_pgtol] )!=1)
pgtol = 1.0e-5;
else
pgtol = *mxGetPr( prhs[N_pgtol] );
/* the work arrays 'wa' and 'iwa' */
if ( nrhs < N_wa+1 ){
debugPrintf("Allocating memory for wa variable\n");
wa = (double *)mxMalloc( (2*m*n + 5*n + 11*m*m + 8*m ) * sizeof(double) );
assert( wa != NULL );
} else {
if ( mxGetM(prhs[N_wa]) < (2*m*n + 5*n + 11*m*m + 8*m ) )
mexErrMsgTxt("work array is too small; make it 2*m*n + 5*n + 11*m*m + 8*m x 1");
wa = mxGetPr( prhs[N_wa] );
}
if ( nrhs < N_iwa+1 ){
debugPrintf("Allocating memory for iwa variable\n");
iwa = (int_F *)mxMalloc( (3*n)*sizeof(int) );
assert( iwa != NULL );
}else {
if ( mxGetM(prhs[N_iwa]) < (3*n) )
mexErrMsgTxt("i_work array is too small; make it 3*n x 1");
if (isInt( prhs[N_iwa] )) {
iwa = (int_F *)mxGetData( prhs[N_iwa] );
}else{
debugPrintf("Converting iwa array to integers\n" );
iwa_dbl = mxGetPr( prhs[N_iwa] );
iwa = (int_F *)mxMalloc( (3*n)*sizeof(int) );
for (i=0;i
iwa[i] = (int)iwa_dbl[i];
}
}
}
/* the 'task' string */
if ( nrhs < N_task+1 )
copyCStrToCharArray("START", task, LENGTH_STRING );
else{
task_m = mxArrayToString( prhs[N_task] );
assert(task_m != NULL);
copyCStrToCharArray(task_m, task, LENGTH_STRING );
/* debugPrintf("String2 is: %s, and strlen is %d\n", task_m, strlen(task_m) ); */
}
/* debugPrintf("String is: %s, and strlen is %d\n", task, strlen(task) ); */
if ( nrhs < N_iprint+1 )
iprint = 1;
else if (mxGetNumberOfElements( prhs[N_iprint] )!=1)
iprint = 1;
else
iprint = (int)*(int*)mxGetData( prhs[N_iprint] );
/* === Deal with the csave, lsave, isave and dsave variables === */
if ( nrhs < N_csave+1 ){
/* do nothing */
}else {
csave_m = mxArrayToString( prhs[N_csave] );
assert(csave_m != NULL);
copyCStrToCharArray(csave_m, csave, LENGTH_STRING );
}
if ( nrhs < N_lsave+1 ) {
/* do nothing, leave at default value */
}else if (mxGetNumberOfElements( prhs[N_lsave] )!=LENGTH_LSAVE){
/* do nothing, leave at default value */
debugPrintf("warning: ignoring the lsave variable, since it has wrong length\n");
}else{
/* copy them */
if (mxIsLogical( prhs[N_lsave] ) ) {
lsave_bool = (mxLogical*)mxGetData(prhs[N_lsave]);
for(i=0;i
lsave[i] = (fortranLogical)lsave_bool[i];
} else {
lsave_m = (double*)mxGetData(prhs[N_lsave]);
for(i=0;i
lsave[i] = (fortranLogical)lsave_m[i];
}
}
if ( nrhs < N_isave+1 ) {
/* do nothing, leave at default value */
}else if (mxGetNumberOfElements( prhs[N_isave] )!=LENGTH_ISAVE){
/* do nothing, leave at default value */
debugPrintf("warning: ignoring the isave variable, since it has wrong length\n");
}else{
/* copy them */
if (mxIsInt32( prhs[N_isave] )) {
/* debugPrintf("isave input is type int32\n"); */
isave_int = (int_F *)mxGetData(prhs[N_isave]);
for(i=0;i
isave[i] = isave_int[i];
} else {
isave_m = (double*)mxGetData(prhs[N_isave]);
for(i=0;i
isave[i] = (int)isave_m[i];
}
}
if ( nrhs < N_dsave+1 ) {
/* do nothing, leave at default value */
}else if (mxGetNumberOfElements( prhs[N_dsave] )!=LENGTH_DSAVE){
/* do nothing, leave at default value */
}else{
/* we don't have to re-cast, but we should copy
* the data anyhow, since 'dsave' is a stack variable */
dsave_m = (double*)mxGetData(prhs[N_dsave]);
for(i=0;i
dsave[i] = dsave_m[i];
}