Пример #1
0
int get_QuadOptions(const mxArray *options, cvmPbData thisPb, booleantype fwd,
                    long int Nq, booleantype *rhs_s,
                    booleantype *errconQ,
                    int *itolQ, double *reltolQ, double *SabstolQ, double **VabstolQ)
{
  mxArray *opt;
  long int i, m, n;
  double *tmp;
  char *fctName;
  char *fwd_fctName = "CVodeQuadInit/CVodeQuadReInit";
  char *bck_fctName = "CVodeQuadInitB/CVodeQuadReInitB";

  if (fwd) fctName = fwd_fctName;
  else     fctName = bck_fctName;

  *errconQ = FALSE;
  *itolQ = CV_SS;
  *reltolQ = 1.0e-4;
  *SabstolQ = 1.0e-6;
  *VabstolQ = NULL;

  *rhs_s = FALSE;

  /* Return now if options was empty */

  if (mxIsEmpty(options)) return(0);

  /* For backward problems only, check dependency on forward sensitivities */

  if (!fwd) {

    opt = mxGetField(options,0,"SensDependent");
    if ( !mxIsEmpty(opt) ) {
      if (!mxIsLogical(opt)) {
        cvmErrHandler(-999, "CVODES", fctName, "SensDependent is not a logical scalar.", NULL);
        return(-1);
      }
      if (mxIsLogicalScalarTrue(opt)) *rhs_s = TRUE;
      else                            *rhs_s = FALSE;
    }

  }

  /* Quadrature error control and tolerances */

  opt = mxGetField(options,0,"ErrControl");

  if ( mxIsEmpty(opt) ) return(0);

  if (!mxIsLogical(opt)) {
    cvmErrHandler(-999, "CVODES", fctName, "ErrControl is not a logical scalar.", NULL);
    return(-1);
  }

  if (!mxIsLogicalScalarTrue(opt)) return(0);
  
  /* the remining options are interpreted only if quadratures are included in error control */

  *errconQ = TRUE;

  opt = mxGetField(options,0,"RelTol");
  if ( !mxIsEmpty(opt) ) {
    *reltolQ = *mxGetPr(opt);
    if (*reltolQ < 0.0) {
      cvmErrHandler(-999, "CVODES", fctName, "RelTol is negative.", NULL);
      return(-1);
    }
  } 

  opt = mxGetField(options,0,"AbsTol");
  if ( !mxIsEmpty(opt) ) {

    m = mxGetN(opt);
    n = mxGetM(opt);
    if ( (n != 1) && (m != 1) ) {
      cvmErrHandler(-999, "CVODES", fctName, "AbsTol is not a scalar or a vector.", NULL);
      return(-1);
    }
    if ( m > n ) n = m;
    tmp = mxGetPr(opt);

    if (n == 1) {
      *itolQ = CV_SS;
      *SabstolQ = *tmp;
      if (*SabstolQ < 0.0) {
        cvmErrHandler(-999, "CVODES", fctName, "AbsTol is negative.", NULL);
        return(-1);
      }
    } else if (n == Nq) {
      *itolQ = CV_SV;
      *VabstolQ = (double *)malloc(Nq*sizeof(double));
      for(i=0;i<Nq;i++) {
        (*VabstolQ)[i] = tmp[i];
        if (tmp[i] < 0.0) {
          cvmErrHandler(-999, "CVODES", fctName, "AbsTol has a negative component.", NULL);
          return(-1);
        }
      }
    } else {
      cvmErrHandler(-999, "CVODES", fctName, "AbsTol does not contain Nq elements.", NULL);
      return(-1);
    }

  }

  /* We made it here without problems */

  return(0);
}
Пример #2
0
int get_FSAOptions(const mxArray *options, cvmPbData thisPb,
                   int *ism,
                   char **pfield_name, int **plist, double **pbar,
                   int *dqtype, double *rho,
                   booleantype *errconS, int *itolS, double *reltolS, 
                   double **SabstolS, double **VabstolS)
{
  mxArray *opt;
  char *bufval;
  int i, is, m, n, buflen, status, this_plist;
  double *tmp;

  /* Set default values */

  *ism = CV_STAGGERED;

  *dqtype = CV_CENTERED;
  *rho = 0.0;

  *errconS = TRUE;

  *itolS = CV_EE;
  *SabstolS = NULL;
  *VabstolS = NULL;

  *pfield_name = NULL;
  *plist = NULL;
  *pbar  = NULL;

  /* Return now if options was empty */

  if (mxIsEmpty(options)) return(0);

  /* Sensitivity method */

  opt = mxGetField(options,0,"method");
  if ( !mxIsEmpty(opt) ) {
  
    buflen = mxGetM(opt) * mxGetN(opt) + 1;
    bufval = mxCalloc(buflen, sizeof(char));
    status = mxGetString(opt, bufval, buflen);
    if(status != 0) {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "Could not parse method.", NULL);
      return(-1);
    }
    if(!strcmp(bufval,"Simultaneous"))   *ism = CV_SIMULTANEOUS;
    else if(!strcmp(bufval,"Staggered")) *ism = CV_STAGGERED;
    else {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "method has an illegal value.", NULL);
      return(-1);
    }
  }


  /* Field name in data structure for params. */

  opt = mxGetField(options,0,"ParamField");
  if ( !mxIsEmpty(opt) ) {

    buflen = mxGetM(opt) * mxGetN(opt) + 1;
    bufval = mxCalloc(buflen, sizeof(char));
    status = mxGetString(opt, bufval, buflen);
    if(status != 0) {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "Could not parse ParamField.", NULL);
      return(-1);
    }
    *pfield_name = mxCalloc(buflen, sizeof(char));
    strcpy((*pfield_name), bufval);

  }  

  /* PLIST */

  opt = mxGetField(options,0,"ParamList");
  if ( !mxIsEmpty(opt) ) {

    tmp = mxGetPr(opt);
    m = mxGetM(opt);
    n = mxGetN(opt);
    if ( (n != 1) && (m != 1) ) {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "ParamList is not a vector.", NULL);
      return(-1);
    }
    if (m > n) n = m;
    if ( n != Ns) {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "ParamList does not contain Ns elements.", NULL);
      return(-1);
    }
    *plist = (int *) malloc(Ns*sizeof(int));
    for (is=0;is<Ns;is++) {
      this_plist = (int) tmp[is];
      if (this_plist <= 0) {
        cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "ParamList must contain only positive integers.", NULL);
        return(-1);
      }
      (*plist)[is] = this_plist - 1;
    }

  }

  /* PBAR */

  opt = mxGetField(options,0,"ParamScales");
  if ( !mxIsEmpty(opt) ) {

    m = mxGetM(opt);
    n = mxGetN(opt);
    if ( (n != 1) && (m != 1) ) {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "ParamScales is not a vector.", NULL);
      return(-1);
    }
    if ( m > n ) n = m;
    if ( n != Ns) {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "ParamScales does not contain Ns elements.", NULL);
      return(-1);
    }
    tmp = mxGetPr(opt);
    *pbar = (double *) malloc(Ns*sizeof(double));
    for(i=0;i<Ns;i++)
      (*pbar)[i] = tmp[i];

  }

  /* DQ type */

  opt = mxGetField(options,0,"DQtype");
  if ( !mxIsEmpty(opt) ) {

    buflen = mxGetM(opt) * mxGetN(opt) + 1;
    bufval = mxCalloc(buflen, sizeof(char));
    status = mxGetString(opt, bufval, buflen);
    if(status != 0) {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "Cannot parse DQtype.", NULL);
      return(-1);
    }
    if(!strcmp(bufval,"Centered")) *dqtype = CV_CENTERED;
    else if(!strcmp(bufval,"Forward")) *dqtype = CV_FORWARD;
    else {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "DQtype has an illegal value.", NULL);
      return(-1);
    }
  }
  
  /* DQ parameter */

  opt = mxGetField(options,0,"DQparam");
  if ( !mxIsEmpty(opt) )
    *rho = *mxGetPr(opt);

  /* Error control */

  opt = mxGetField(options,0,"ErrControl");
  if ( !mxIsEmpty(opt) ) {
    if (!mxIsLogical(opt)) {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "ErrControl is not a logical scalar.", NULL);
      return(-1);
    }
    if (mxIsLogicalScalarTrue(opt)) *errconS = TRUE;
    else                            *errconS = FALSE;
  }

  /* Tolerances */
  
  opt = mxGetField(options,0,"RelTol");
  if ( !mxIsEmpty(opt) ) {

    *reltolS = *mxGetPr(opt);
    if (*reltolS < 0.0) {
      cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "RelTol is negative.", NULL);
      return(-1);
    }
    opt = mxGetField(options,0,"AbsTol");
    if ( !mxIsEmpty(opt) ) {

      m = mxGetM(opt);
      n = mxGetN(opt);
      if ( (m == 1) && (n == Ns) ) {
        *itolS = CV_SS;
        tmp = mxGetPr(opt);
        *SabstolS = (double *) malloc(Ns*sizeof(double));
        for (is=0; is<Ns; is++) {
          (*SabstolS)[is] = tmp[is];
          if ( tmp[is] < 0.0 ) {
            cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "AbsTol has a negative component.", NULL);
            return(-1);
          }
        }
      } else if ( (m == N) && (n == Ns) ) {
        *itolS = CV_SV;
        tmp = mxGetPr(opt);
        *VabstolS = (double *)malloc(Ns*N*sizeof(double));
        for (i=0; i<Ns*N; i++) {
          (*VabstolS)[i] = tmp[i];
          if ( tmp[i] < 0.0 ) {
            cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "AbsTol has a negative component.", NULL);
            return(-1);
          }
        }
      } else {
        cvmErrHandler(-999, "CVODES", "CVodeSensInit/CVodeSensReInit", "AbsTol must be either a 1xNs vector or an NxNs matrix.", NULL);
        return(-1);
      }

    } else {

      *itolS = CV_EE;

    }

  }

  /* We made it here without problems */

  return(0);

}
Пример #3
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    // Get the command string
    char cmd[64];
    if(nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))
    {
        mexErrMsgTxt("GaussianKernel: First input should be a command string less than 64 characters long.");
    }
    
    // New
    if (!strcmp(cmd, "new"))
    {
        // Check output parameters
        if (nlhs != 1)
        {
            mexErrMsgTxt("GaussianKernel: new: One output expected.");
        }
        
        // Check input parameters
        if (nrhs == 1)
        {
            plhs[0] = convertPtr2Mat(new GaussianKernel());
        }
        else if (nrhs == 3)
        {
            if(!mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2])) {
                mexErrMsgTxt("GaussianKernel: new: Invalid data type.");
            }
            
            double lowerBound = mxGetScalar(prhs[1]);
            double upperBound = mxGetScalar(prhs[2]);
            
            plhs[0] = convertPtr2Mat(new GaussianKernel(lowerBound, upperBound));
        }
        else if (nrhs == 4)
        {
            if(!mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2]) || !mxIsLogicalScalar(prhs[3])) {
                mexErrMsgTxt("GaussianKernel: new: Invalid data type.");
            }
            
            double lowerBound = mxGetScalar(prhs[1]);
            double upperBound = mxGetScalar(prhs[2]);
            bool interpolation = mxIsLogicalScalarTrue(prhs[3]);
            
            plhs[0] = convertPtr2Mat(new GaussianKernel(lowerBound, upperBound, interpolation));
        }
        else
            mexErrMsgTxt("GaussianKernel: new: Invalid parameter number.");
        
        return;
    }
    
    // Check there is a second input, which should be the class instance handle
    if (nrhs < 2)
		mexErrMsgTxt("GaussianKernel: Second input should be a class instance handle.");
    
    // Delete
    if (!strcmp(cmd, "delete"))
    {
        // Destroy the C++ object
        destroyObject<GaussianKernel>(prhs[1]);
        
        // Warn if other commands were ignored
        if (nlhs != 0 || nrhs != 2)
            mexWarnMsgTxt("Delete: Unexpected arguments ignored.");
        
        return;
    }
}
Пример #4
0
int get_IntgrOptions(const mxArray *options, cvmPbData thisPb, booleantype fwd, int lmm,
                     int *maxord, booleantype *sld, booleantype *errmsg,
                     long int *mxsteps,
                     int *itol, realtype *reltol, double *Sabstol, double **Vabstol,
                     double *hin, double *hmax, double *hmin, double *tstop,
                     booleantype *rhs_s)
{
  mxArray *opt;
  int q;
  long int i, m, n;
  double *tmp;
  char *fctName;
  char *fwd_fctName = "CVodeInit/CVodeReInit";
  char *bck_fctName = "CVodeInitB/CVodeReInitB";

  if (fwd) fctName = fwd_fctName;
  else     fctName = bck_fctName;
  
  /* Set default values */
  
  *maxord = (lmm == CV_ADAMS) ? 12 : 5;
  
  *sld = FALSE;

  *mxsteps = 0;

  *itol = CV_SS;
  *reltol = 1.0e-3;
  *Sabstol = 1.0e-6;
  *Vabstol = NULL;

  *hin = 0.0;
  *hmax = 0.0;
  *hmin = 0.0;

  *rhs_s = FALSE;

  Ng = 0;
  tstopSet = FALSE;
  mon = FALSE;

  *errmsg = TRUE;


  /* Return now if options was empty */

  if (mxIsEmpty(options)) return(0);

  /* User data */

  opt = mxGetField(options,0,"UserData");
  if ( !mxIsEmpty(opt) ) {
    mxDestroyArray(mtlb_data);
    mtlb_data = mxDuplicateArray(opt);
  }
  
  /* Tolerances */

  opt = mxGetField(options,0,"RelTol");
  if ( !mxIsEmpty(opt) ) {
    *reltol = *mxGetPr(opt);
    if (*reltol < 0.0 ) {
      cvmErrHandler(-999, "CVODES", fctName, "RelTol is negative.", NULL);
      return(-1);
    }
  }
    
  opt = mxGetField(options,0,"AbsTol");
  if ( !mxIsEmpty(opt) ) {
    m = mxGetM(opt);
    n = mxGetN(opt);
    if ( (n != 1) && (m != 1) ) {
      cvmErrHandler(-999, "CVODES", fctName, "AbsTol is not a scalar or a vector.", NULL);
      return(-1);
    }
    if ( m > n ) n = m;
    tmp = mxGetPr(opt);
    if (n == 1) {
      *itol = CV_SS;
      *Sabstol = *tmp;
      if (*Sabstol < 0.0) {
        cvmErrHandler(-999, "CVODES", fctName, "AbsTol is negative.", NULL);
      return(-1);
      }
    } else if (n == N) {
      *itol = CV_SV;
      *Vabstol = (double *) malloc(N*sizeof(double));
      for(i=0;i<N;i++) {
        (*Vabstol)[i] = tmp[i];
        if (tmp[i] < 0.0) {
          cvmErrHandler(-999, "CVODES", fctName, "AbsTol has a negative component.", NULL);
          return(-1);
        }
      }
    } else {
      cvmErrHandler(-999, "CVODES", fctName, "AbsTol does not contain N elements.", NULL);
      return(-1);
    }
  }

  /* Maximum number of steps */

  opt = mxGetField(options,0,"MaxNumSteps");
  if ( !mxIsEmpty(opt) ) {
    *mxsteps = (int)*mxGetPr(opt);
    if (*mxsteps < 0) {
      cvmErrHandler(-999, "CVODES", fctName, "MaxNumSteps is negative.", NULL);
      return(-1);
    }
  }

  /* Maximum order */

  opt = mxGetField(options,0,"MaxOrder");
  if ( !mxIsEmpty(opt) ) {
    q = (int)*mxGetPr(opt);
    if (q <= 0) {
      cvmErrHandler(-999, "CVODES", fctName, "MaxOrder must be positive.", NULL);
      return(-1);
    }
    if (q > *maxord) {
      cvmErrHandler(-999, "CVODES", fctName, "MaxOrder is too large for the Method specified.", NULL);
      return(-1);
    }
    *maxord = q;
  }

  /* Initial step size */

  opt = mxGetField(options,0,"InitialStep");
  if ( !mxIsEmpty(opt) ) {
    *hin = *mxGetPr(opt);
  }

  /* Maximum step size */

  opt = mxGetField(options,0,"MaxStep");
  if ( !mxIsEmpty(opt) ) {
    tmp = mxGetPr(opt);
    if (*tmp < 0.0) {
      cvmErrHandler(-999, "CVODES", fctName, "MaxStep is negative.", NULL);
      return(-1);
    }
    if ( mxIsInf(*tmp) ) *hmax = 0.0;
    else                 *hmax = *tmp;
  }

  /* Minimum step size */

  opt = mxGetField(options,0,"MinStep");
  if ( !mxIsEmpty(opt) ) {
    *hmin = *mxGetPr(opt);
    if (*hmin < 0.0) {
      cvmErrHandler(-999, "CVODES", fctName, "MinStep is negative.", NULL);
      return(-1);
    }
  }

  /* Stability Limit Detection */

  opt = mxGetField(options,0,"StabilityLimDet");
  if ( !mxIsEmpty(opt) ) {
    if (!mxIsLogical(opt)) {
      cvmErrHandler(-999, "CVODES", fctName, "StabilityLimDet is not a logical scalar.", NULL);
      return(-1);
    }
    if (mxIsLogicalScalarTrue(opt)) *sld = TRUE;
    else                            *sld = FALSE;
  }

  /* Monitor? */

  opt = mxGetField(options,0,"MonitorFn");
  if ( !mxIsEmpty(opt) ) {
    mon = TRUE;
    mxDestroyArray(mtlb_MONfct);
    mtlb_MONfct = mxDuplicateArray(opt);
    opt = mxGetField(options,0,"MonitorData");
    if ( !mxIsEmpty(opt) ) {
      mxDestroyArray(mtlb_MONdata);
      mtlb_MONdata  = mxDuplicateArray(opt);
    }
  }

  /* The remaining options are interpreted either for 
   * forward problems only or backward problems only */

  if (fwd) {   /* FORWARD PROBLEM ONLY */

    /* Disable error/warning messages? */

    opt = mxGetField(options,0,"ErrorMessages");
    if ( !mxIsEmpty(opt) ) {
      if (!mxIsLogical(opt)) {
        cvmErrHandler(-999, "CVODES", fctName, "ErrorMessages is not a logical scalar.", NULL);
        return(-1);
      }
      if (mxIsLogicalScalarTrue(opt)) *errmsg = TRUE;
      else                            *errmsg = FALSE;
    }

    /* Stopping time */
    opt = mxGetField(options,0,"StopTime");
    if ( !mxIsEmpty(opt) ) {
      *tstop = *mxGetPr(opt);
      tstopSet = TRUE;
    }

    /* Number of root functions */
    opt = mxGetField(options,0,"NumRoots");
    if ( !mxIsEmpty(opt) ) {

      Ng = (int)*mxGetPr(opt);
      if (Ng < 0) {
        cvmErrHandler(-999, "CVODES", fctName, "NumRoots is negative.", NULL);
        return(-1);
      }
      if (Ng > 0) {
        /* Roots function */
        opt = mxGetField(options,0,"RootsFn");
        if ( !mxIsEmpty(opt) ) {
          mxDestroyArray(mtlb_Gfct);
          mtlb_Gfct = mxDuplicateArray(opt);
        } else {
          cvmErrHandler(-999, "CVODES", fctName, "RootsFn required for NumRoots > 0", NULL);
          return(-1);
        }
      }
      
    }

  } else {   /* BACKWARD PROBLEM ONLY */

    /* Dependency on forward sensitivities */

    opt = mxGetField(options,0,"SensDependent");
    if ( !mxIsEmpty(opt) ) {
      if (!mxIsLogical(opt)) {
        cvmErrHandler(-999, "CVODES", fctName, "SensDependent is not a logical scalar.", NULL);
        return(-1);
      }
      if (mxIsLogicalScalarTrue(opt)) *rhs_s = TRUE;
      else                            *rhs_s = FALSE;
    }

  }

  /* We made it here without problems */

  return(0);
}
int get_SolverOptions(const mxArray *options,
                      booleantype *verbose,
                      int *mxiter, int *msbset, int *msbsetsub, 
                      int *etachoice, int *mxnbcf,
                      double *eta, double *egamma, double *ealpha, double *mxnewtstep, 
                      double *relfunc, double *fnormtol, double *scsteptol,
                      double **constraints,
                      booleantype *noInitSetup, booleantype *noMinEps)
{
  mxArray *opt;
  char *bufval;
  int i, buflen, status, n;
  double *tmp;

  /* Set default values (pass 0 values. KINSOL does the rest) */

  *mxiter = 0;
  *msbset = 0;
  *msbsetsub = 0;
  *mxnbcf = 0;
  *etachoice = KIN_ETACHOICE1;

  *eta = 0.0;
  *egamma = 0.0;
  *ealpha = 0.0;
  *mxnewtstep = 0.0;
  *relfunc = 0.0;
  *fnormtol = 0.0;
  *scsteptol = 0.0;

  *noInitSetup = FALSE;
  *noMinEps = FALSE;
  *verbose = FALSE;

  *constraints = NULL;

  /* Return now if options was empty */

  if (mxIsEmpty(options)) return(0);

  /* Integer values */

  opt = mxGetField(options,0,"MaxNumIter");
  if ( !mxIsEmpty(opt) )
    *mxiter = (int)*mxGetPr(opt);
  
  opt = mxGetField(options,0,"MaxNumSetups");
  if ( !mxIsEmpty(opt) )
    *msbset = (int)*mxGetPr(opt);
  
  opt = mxGetField(options,0,"MaxNumSubSetups");
  if ( !mxIsEmpty(opt) )
    *msbsetsub = (int)*mxGetPr(opt);
  
  opt = mxGetField(options,0,"MaxNumBetaFails");
  if ( !mxIsEmpty(opt) )
    *mxnbcf = (int)*mxGetPr(opt);
  
  opt = mxGetField(options,0,"EtaForm");
  if ( !mxIsEmpty(opt) ) {
    buflen = mxGetM(opt) * mxGetN(opt) + 1;
    bufval = mxCalloc(buflen, sizeof(char));
    status = mxGetString(opt, bufval, buflen);
    if(status != 0) return(status);
    if(strcmp(bufval,"Type1"))         *etachoice = KIN_ETACHOICE1;
    else if(strcmp(bufval,"Type2"))    *etachoice = KIN_ETACHOICE2;
    else if(strcmp(bufval,"Constant")) *etachoice = KIN_ETACONSTANT;
  }
  
  /* Real values */

  opt = mxGetField(options,0,"Eta");
  if ( !mxIsEmpty(opt) )
    *eta = (double)*mxGetPr(opt);
  
  opt = mxGetField(options,0,"EtaAlpha");
  if ( !mxIsEmpty(opt) )
    *ealpha = (double)*mxGetPr(opt);
  
  opt = mxGetField(options,0,"EtaGamma");
  if ( !mxIsEmpty(opt) )
    *egamma = (double)*mxGetPr(opt);
  
  opt = mxGetField(options,0,"MaxNewtonStep");
  if ( !mxIsEmpty(opt) )
    *mxnewtstep = (double)*mxGetPr(opt);
  
  opt = mxGetField(options,0,"FuncRelErr");
  if ( !mxIsEmpty(opt) )
    *relfunc = (double)*mxGetPr(opt);

  opt = mxGetField(options,0,"FuncNormTol");
  if ( !mxIsEmpty(opt) )
    *fnormtol = (double)*mxGetPr(opt);

  opt = mxGetField(options,0,"ScaledStepTol");
  if ( !mxIsEmpty(opt) )
    *scsteptol = (double)*mxGetPr(opt);

  /* Boolean values */

  opt = mxGetField(options,0,"Verbose");
  if ( !mxIsEmpty(opt) )
    *verbose = mxIsLogicalScalarTrue(opt);

  opt = mxGetField(options,0,"InitialSetup");
  if ( !mxIsEmpty(opt) )
    *noInitSetup = !mxIsLogicalScalarTrue(opt);

  opt = mxGetField(options,0,"MinBoundEps");
  if ( !mxIsEmpty(opt) )
    *noMinEps = !mxIsLogicalScalarTrue(opt);


  /* Constraints */

  opt = mxGetField(options,0,"Constraints");
  if ( !mxIsEmpty(opt) ) {
    tmp = mxGetPr(opt);
    n = mxGetN(opt);
    if (n == N) {
      *constraints = (double *) malloc(N*sizeof(int));
      for (i=0;i<N;i++)
        (*constraints)[i] = tmp[i];
    } else {
      return(1); /* Error */
    }
  }

  /* We made it here without problems */

  return(0);
}
Пример #6
0
int pfput_mxArray( Pf *pf, char *name, const mxArray *array )
{
	Pf	*sub_pf;
	double	number;
	char	*string;
	mxArray *in[2], *out[1];
	char	warning[STRSZ];
	char	*fieldname;
	mxArray	*mxfield;
	mxArray	*mxcell;
	int	M,N;
	int	rc;
	int	i;

	if( mxIsClass( array, "dbpf" ) )
	{
		if( ! get_pf( array, &sub_pf ) )
		{
			return PFINVALID;
		}

		if( sub_pf->type == PFFILE) 
		{
			/* Don't embed a PFFILE in something else */
			sub_pf->type = PFARR;
		}

    		switch (pf->type) {
        	case PFFILE:
        	case PFARR:
            		setarr ( pf->value.arr, name, sub_pf ) ;
            		break ;

        	case PFTBL:
            		settbl ( pf->value.tbl, (int) name, sub_pf ) ;
            		break ;
	
        	default :
			return PFINVALID;
        	}
		antelope_mex_clear_register( 1 );
	}
	else if( mxIsDouble( array ) )
	{
		if( ! get_scalar( array, &number ) )
		{
			return PFINVALID;
		}
	
		in[0] = (mxArray *) array; /* Input scalar */
		mexCallMATLAB( 1, out, 1, in, "floor" );
		in[1] = out[0];  /* floor( Input scalar ) */
		mexCallMATLAB( 1, out, 2, in, "eq" );
		mxDestroyArray( in[1] );
	
		if( mxIsLogicalScalarTrue( out[0] ) ) 
		{
			pfput_int( pf, name, (int) number );
		} 
		else 
		{
			pfput_double( pf, name, number );
		}
		antelope_mex_clear_register( 1 );

		mxDestroyArray( out[0] );
	}
	else if( mxIsChar( array ) )
	{
		if( ! mtlb_get_string( array, &string ) )
		{
			sprintf( warning, 
			  "failed to extract string for parameter %s\n",
			  name );
			mexWarnMsgTxt( warning );
			return PFINVALID;
		}

		pfput_string( pf, name, string );

		antelope_mex_clear_register( 1 );

		mxFree( string );
	}
	else if( mxIsStruct( array ) )
	{
		if( mxGetNumberOfDimensions( array ) > 2 ) 
		{
			sprintf( warning,
			  "structure has too many dimensions for parameter %s\n",
			  name );
			mexWarnMsgTxt( warning );
			return PFINVALID;
		}
		else if( mxGetM( array ) != 1 || mxGetN( array ) != 1 )
		{
			sprintf( warning,
			  "structure has too many elements for parameter %s\n",
			  name );
			mexWarnMsgTxt( warning );
			return PFINVALID;
		}
		N = mxGetNumberOfFields( array );

		sub_pf = pfnew( PFARR );

		for( i = 0; i < N; i++ ) 
		{
			fieldname = (char *) mxGetFieldNameByNumber( array, i );
			mxfield = mxGetFieldByNumber( array, 0, i );
			rc = pfput_mxArray( sub_pf, fieldname, mxfield );

			if( rc == PFINVALID )
			{
				pffree( sub_pf );
				return PFINVALID;
			}
		}

    		switch (pf->type) {
        	case PFFILE:
        	case PFARR:
            		setarr ( pf->value.arr, name, sub_pf ) ;
            		break ;

        	case PFTBL:
            		settbl ( pf->value.tbl, (int) name, sub_pf ) ;
            		break ;
	
        	default :
            		pffree( sub_pf );
			return PFINVALID;
        	}
		antelope_mex_clear_register( 1 );
	}
	else if( mxIsCell( array ) )
	{
                if( mxGetNumberOfDimensions( array ) > 2 )
                {
                        sprintf( warning,
                          "cell array has too many dimensions for parameter %s\n",
                          name );
                        mexWarnMsgTxt( warning );
                        return PFINVALID;
                }
                M = mxGetM( array );
                N = mxGetN( array );

		sub_pf = pfnew( PFTBL );
		for( i = 0; i < M * N; i++ ) 
		{
			mxcell = mxGetCell( array, i );
			rc = pfput_mxArray( sub_pf, (char *) i, mxcell );

			if( rc == PFINVALID )
			{
				pffree( sub_pf );
				return PFINVALID;
			}
		} 
    		switch (pf->type) {
        	case PFFILE:
        	case PFARR:
            		setarr ( pf->value.arr, name, sub_pf ) ;
            		break ;

        	case PFTBL:
            		settbl ( pf->value.tbl, (int) name, sub_pf ) ;
            		break ;
	
        	default :
            		pffree( sub_pf );
			return PFINVALID;
        	}
		antelope_mex_clear_register( 1 );
	}
	else 
	{
		return PFINVALID;
	}
	
	return 0;
}
Пример #7
0
/**
 * parse use_bdtree
 */
inline bool getopt_use_bdtree(const mxArray* mxUseBdTree)
{
    return mxIsLogicalScalarTrue(mxUseBdTree);
}
Пример #8
0
void parse(mxArray *ma, json_object **jo){

    int i = 0;
    size_t num_el = mxGetNumberOfElements(ma);
    size_t m = mxGetM(ma);
    size_t n = mxGetN(ma);

    json_object *lobj;
    json_object *tmpobj;
    mxArray *tmpma;

    /* was char - convert to string*/
    if(mxIsChar(ma)){
        char *str = mxArrayToString(ma);
        *jo = json_object_new_string(str);
    }
    /* was struct - convert to object */
    else if(mxIsStruct(ma)){
        if(num_el == 1){
            object(ma, 0, jo);
        }
        else{
            /* it was a list of objects! */
            *jo = json_object_new_array();

            if(n == 1){
                n = m;
                m = 1;
            }

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

                if(i % m == 0 && m > 1){
                    lobj = json_object_new_array();
                    json_object_array_add(*jo,lobj);
                }

                object(ma, i, &tmpobj);

                if(m > 1)
                    json_object_array_add(lobj, tmpobj);
                else
                    json_object_array_add(*jo, tmpobj);
            }
        }


    }
    /* was cell array - convert to list */
    else if(mxIsCell(ma)){

        *jo = json_object_new_array();

        if(n == 1){
            n = m;
            m = 1;
        }

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

            if(i % m == 0 && m > 1){
                lobj = json_object_new_array();
                json_object_array_add(*jo,lobj);
            }

            tmpma = mxGetCell(ma, i);

            if(tmpma){
                parse(tmpma, &tmpobj);

                if(m > 1)
                    json_object_array_add(lobj, tmpobj);
                else
                    json_object_array_add(*jo, tmpobj);
            }
        }
    }
    /* was numeric or bool - convert to number or list */
    else{

        /* single number or bool */
        if(num_el == 1){
            /* mxINT32_CLASS */

            if(mxIsComplex(ma)){
                mexErrMsgTxt("Data cannot be complex.");
            }

            if(mxIsNumeric(ma)){
                double dbl = mxGetScalar(ma);
                numeric(dbl, jo);
            }
            else if(mxIsLogical(ma)){
                *jo = json_object_new_boolean(mxIsLogicalScalarTrue(ma));
            }
            else{
                printf("Type is: %s\n", mxGetClassName(ma));
                mexErrMsgTxt("Encountered an unknown type.");
            }

        }
        else{

            double *dbl;
            int *intgr;
            mxLogical *bl;

            if(mxIsDouble(ma)){
                dbl = mxGetData(ma);
            }
            else if(mxIsClass(ma, "mxINT32_CLASS")){
                intgr = mxGetData(ma);
            }
            else if(mxIsLogical(ma)){
                bl = mxGetData(ma);
            }
            else{
                printf("Type is: %s\n", mxGetClassName(ma));
                mexErrMsgTxt("Encountered an unsupported type.");
            }

            *jo = json_object_new_array();

            /* vector - convert to list */
            if(n == 1){
                n = m;
                m = 1;
            }

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

                if(i % m == 0 && m > 1){
                    lobj = json_object_new_array();
                    json_object_array_add(*jo,lobj);
                }

                if(mxIsLogical(ma))
                    tmpobj = json_object_new_boolean(bl[i]);
                else if(mxIsDouble(ma))
                    numeric(dbl[i], &tmpobj);
                else
                    numeric((double)intgr[i], &tmpobj);

                if(m > 1)
                    json_object_array_add(lobj, tmpobj);
                else
                    json_object_array_add(*jo, tmpobj);

            }


        }

    }


}
void mexFunction( const int nlhs, mxArray *plhs[], const int nrhs, const mxArray *prhs[]) {
	/* Check for proper number of arguments. */
	if (nrhs != 1) {
		mexErrMsgIdAndTxt( "MATLAB:Graph:IsConnected:invalidNumInputs",
			"One input required.");
	} else if (nlhs > 1) {
		mexErrMsgIdAndTxt( "MATLAB:Graph:IsConnected:invalidNumOutputs",
			"At most one output is allowed.");
	}

	const mxArray *numberOfNodesArray = mxGetProperty(prhs[0], 0, "numberOfNodes");
	if (!mxIsNumeric(numberOfNodesArray) || mxGetNumberOfElements(numberOfNodesArray) != 1) {
		mexErrMsgIdAndTxt( "MATLAB:Graph:IsConnected:invalidNumberOfNodes", "Number of nodes is not numeric scalar.");
	}
	const int numberOfNodes = mxGetScalar(numberOfNodesArray);
	if (numberOfNodes < 1) {
		mexErrMsgIdAndTxt( "MATLAB:Graph:IsConnected:emptyGraph",
			"Graph is empty.");
	}

	const mxArray *pAdjacencyMatrix = mxGetProperty(prhs[0], 0, "pAdjacencyMatrix");
	if (!mxIsLogical(pAdjacencyMatrix) || mxGetN(pAdjacencyMatrix) != numberOfNodes || mxGetM(pAdjacencyMatrix) != numberOfNodes) {
		mexErrMsgIdAndTxt("MATLAB:Graph:IsConnected:invalidAdjacencyMatrix", "Adjacency matrix is not logical square matrix");
	}
	const bool *adjacencyMatrix = mxGetLogicals(pAdjacencyMatrix);

	const mxArray *pIsDirectedArray = mxGetProperty(prhs[0], 0, "pIsDirected");
	if (!mxIsLogicalScalar(pIsDirectedArray)) {
		mexErrMsgIdAndTxt( "MATLAB:Graph:IsConnected:invalidIsDirected", "Direction is not logical scalar");
	}
	const bool isDirected = mxIsLogicalScalarTrue(pIsDirectedArray);

	bool *reachable = new bool[numberOfNodes];


	if (mxIsSparse(pAdjacencyMatrix)) {
		// Sparse adjacency matrix
		std::vector<std::vector<mwIndex> > graph(numberOfNodes), rgraph(numberOfNodes);
		const mwIndex* row = mxGetIr(pAdjacencyMatrix);
		const mwIndex* col = mxGetJc(pAdjacencyMatrix);
		const size_t nz = col[mxGetN(pAdjacencyMatrix)];
		mwIndex c = 0;
		for(size_t i = 0; i<nz; ++i) {
			while(col[c+1]<=i) ++c;
			if(adjacencyMatrix[i]) {
				graph[row[i]].push_back(c);
				rgraph[c].push_back(row[i]);
			}
		}
		memset(reachable, false, numberOfNodes * sizeof(bool));
		reachable[0] = true;
		int nReachable = 1;
		std::queue<int> nodeQueue;
		nodeQueue.push(0);

		while(!nodeQueue.empty() && nReachable < numberOfNodes) {
			const int i = nodeQueue.front(); nodeQueue.pop();
			std::vector<mwIndex>::const_iterator it = graph[i].begin();

			for (; it != graph[i].end(); ++it) {
				if(!reachable[*it]) {
					reachable[*it] = true;
					nReachable++;
					nodeQueue.push(*it);
				}
			}
		}
		if (!isDirected || nReachable != numberOfNodes) {
			plhs[0] = mxCreateLogicalScalar(nReachable == numberOfNodes);
		} else {
			memset(reachable, false, numberOfNodes * sizeof(bool));
			reachable[0] = true;
			int nReachable2 = 1;
			nodeQueue = std::queue<int>(); // Clear queue
			nodeQueue.push(0);
			while(!nodeQueue.empty() && nReachable2 < numberOfNodes) {
				const int i = nodeQueue.front(); nodeQueue.pop();
				std::vector<mwIndex>::const_iterator it = rgraph[i].begin();
				for (; it != rgraph[i].end(); ++it) {
					if(!reachable[*it]) {
						mexPrintf("Reachable 2 is %lu\n", *it);
						reachable[*it] = true;
						nReachable2++;
						nodeQueue.push(*it);
					}
				}
			}
			plhs[0] = mxCreateLogicalScalar(nReachable2 == numberOfNodes);
		}
	} else {
		// Full adjacency matrix
		memset(reachable, false, numberOfNodes * sizeof(bool));
		reachable[0] = true;
		int nReachable = 1;
		std::queue<int> nodeQueue;
		nodeQueue.push(0);

		while(!nodeQueue.empty() && nReachable < numberOfNodes) {
			const int i = nodeQueue.front(); nodeQueue.pop();
			for (int j=0; j<numberOfNodes; ++j) {
				if(adjacencyMatrix[i*numberOfNodes+j] && !reachable[j]) {
					reachable[j] = true;
					nReachable++;
					nodeQueue.push(j);
				}
			}
		}
		if (!isDirected || nReachable != numberOfNodes) {
			plhs[0] = mxCreateLogicalScalar(nReachable == numberOfNodes);
		} else {
			memset(reachable, false, numberOfNodes * sizeof(bool));
			reachable[0] = true;
			int nReachable2 = 1;
			nodeQueue = std::queue<int>(); // Clear queue
			nodeQueue.push(0);
			while(!nodeQueue.empty() && nReachable2 < numberOfNodes) {
				const int i = nodeQueue.front(); nodeQueue.pop();
				for (int j=0; j<numberOfNodes; ++j) {
					if(adjacencyMatrix[j*numberOfNodes+i] && !reachable[j]) {
						reachable[j] = true;
						nReachable2++;
						nodeQueue.push(j);
					}
				}
			}
			plhs[0] = mxCreateLogicalScalar(nReachable2 == numberOfNodes);
		}
	}

	delete [] reachable;
}