Example #1
0
static int
doquery()
{
  int ret = 0;
  char buf[100];

  Uint8 b_lo_buffer[NdbIndexStat::BoundBufferBytes];
  Uint8 b_hi_buffer[NdbIndexStat::BoundBufferBytes];
  NdbIndexStat::Bound b_lo(g_is, b_lo_buffer);
  NdbIndexStat::Bound b_hi(g_is, b_hi_buffer);
  do
  {
    NdbIndexStat::Range r(b_lo, b_hi);
    Uint8 s_buffer[NdbIndexStat::StatBufferBytes];
    NdbIndexStat::Stat s(s_buffer);

    for (int n = 0; n < _query; n++)
    {
      g_is->reset_range(r);
      for (int i = 0; i <= 1; i++)
      {
        NdbIndexStat::Bound& b = (i == 0 ? b_lo : b_hi);

        if (ndb_rand() % 3 != 0)
        {
          if (ndb_rand() % 3 != 0)
          {
            Uint32 x = ndb_rand();
            CHK2(g_is->add_bound(b, &x) == 0, g_is->getNdbError());
          }
          else
          {
            CHK2(g_is->add_bound_null(b) == 0, g_is->getNdbError());
          }
          bool strict = (ndb_rand() % 2 == 0);
          g_is->set_bound_strict(b, strict);
        }
      }
      CHK2(ret == 0, "failed");
      CHK2(g_is->finalize_range(r) == 0, g_is->getNdbError());
      CHK2(g_is->query_stat(r, s) == 0, g_is->getNdbError());
      double rir = -1.0;
      NdbIndexStat::get_rir(s, &rir);
      g_info << "rir: " << format(rir, buf) << endl;
    }
    CHK2(ret == 0, "failed");
  }
  while (0);

  return ret;
}
Example #2
0
/** the gateway function */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    /* [t, b] = ddm_rand_full(mu, sig2, b_lo, b_up, 
                              delta_t, n[, inv_leak[, seed]]) */

    /* Check argument number */
    if (nlhs != 2) {
        mexErrMsgIdAndTxt("ddm_rand_full:WrongOutputs", 
                          "Wrong number of output arguments");
    }
    if (nrhs < 6) {
        mexErrMsgIdAndTxt("ddm_rand_full:WrongInputs",
                          "Too few input arguments");
    }

    /* Process first 8 arguments */
    if (!MEX_ARGIN_IS_REAL_VECTOR(0))
        mexErrMsgIdAndTxt("ddm_rand_full:WrongInput",
                          "First input argument expected to be a vector");
    if (!MEX_ARGIN_IS_REAL_VECTOR(1))
        mexErrMsgIdAndTxt("ddm_rand_full:WrongInput",
                          "Second input argument expected to be a vector");
    if (!MEX_ARGIN_IS_REAL_VECTOR(2))
        mexErrMsgIdAndTxt("ddm_rand_full:WrongInput",
                          "Third input argument expected to be a vector");
    if (!MEX_ARGIN_IS_REAL_VECTOR(3))
        mexErrMsgIdAndTxt("ddm_rand_full:WrongInput",
                          "Fourth input argument expected to be a vector");
    if (!MEX_ARGIN_IS_REAL_DOUBLE(4))
        mexErrMsgIdAndTxt("ddm_rand_full:WrongInput",
                          "Fifth input argument expected to be a double");
    if (!MEX_ARGIN_IS_REAL_DOUBLE(5))
        mexErrMsgIdAndTxt("ddm_rand_full:WrongInput",
                          "Sixth input argument expected to be a double");
    if (nrhs >= 7 && !MEX_ARGIN_IS_REAL_DOUBLE(6))
        mexErrMsgIdAndTxt("ddm_rand_sym:WrongInput",
                          "Seventh input argument expected to be a double");
    if (nrhs >= 8 && !MEX_ARGIN_IS_REAL_DOUBLE(7))
        mexErrMsgIdAndTxt("ddm_rand_sym:WrongInput",
                          "Eighth input argument expected to be a double");
    int mu_size = std::max(mxGetN(prhs[0]), mxGetM(prhs[0]));
    int sig2_size = std::max(mxGetN(prhs[1]), mxGetM(prhs[1]));
    int b_lo_size = std::max(mxGetN(prhs[2]), mxGetM(prhs[2]));
    int b_up_size = std::max(mxGetN(prhs[3]), mxGetM(prhs[3]));
    ExtArray mu(ExtArray::shared_noowner(mxGetPr(prhs[0])), mu_size);
    ExtArray sig2(ExtArray::shared_noowner(mxGetPr(prhs[1])), sig2_size);
    ExtArray b_lo(ExtArray::shared_noowner(mxGetPr(prhs[2])), b_lo_size);
    ExtArray b_up(ExtArray::shared_noowner(mxGetPr(prhs[3])), b_up_size);
    ExtArray b_lo_deriv = ExtArray::const_array(0.0);
    ExtArray b_up_deriv = ExtArray::const_array(0.0);
    double delta_t = mxGetScalar(prhs[4]);
    int n = (int) mxGetScalar(prhs[5]);
    if (delta_t <= 0.0)
        mexErrMsgIdAndTxt("ddm_rand_full:WrongInput",
                          "delta_t needs to be larger than 0.0");
    if (n <= 0)
        mexErrMsgIdAndTxt("ddm_rand_full:WrongInput",
                          "n needs to be larger than 1");
    bool has_leak = false;
    double inv_leak = 0.0;
    if (nrhs >= 7) {
        inv_leak = mxGetScalar(prhs[6]);
        has_leak = (inv_leak != 0.0);
        if (inv_leak < 0.0)
            mexErrMsgIdAndTxt("ddm_rand_full:WrongInput",
                              "inv_leak needs to be non-negative");
    }
    int rngseed = 0;
    if (nrhs >= 8)
        rngseed = (int) mxGetScalar(prhs[7]);
    if (nrhs > 8)
        mexErrMsgIdAndTxt("ddm_rand_full:WrongInputs",
                          "Too many input arguments");

    /* reserve space for output */
    plhs[0] = mxCreateDoubleMatrix(1, n, mxREAL);
    plhs[1] = mxCreateLogicalMatrix(1, n);
    double* t = mxGetPr(plhs[0]);
    mxLogical* b = mxGetLogicals(plhs[1]);

    /* perform sampling */
    DMBase* dm = nullptr;
    if (has_leak)
        dm = DMBase::create(mu, sig2, b_lo, b_up, b_lo_deriv, b_up_deriv,
                            delta_t, inv_leak);
    else
        dm = DMBase::create(mu, sig2, b_lo, b_up, b_lo_deriv, b_up_deriv,
                            delta_t);
    DMBase::rngeng_t rngeng;
    if (rngseed == 0) rngeng.seed(std::random_device()());
    else rngeng.seed(rngseed);
    for (int i = 0; i < n; ++i) {
        DMSample s = dm->rand(rngeng);
        t[i] = s.t();
        b[i] = s.upper_bound() ? 1 : 0;
    }
    delete dm;
}
Example #3
0
/* method (t, b) = rand_asym(mu, b_lo, b_up, dt, n[, seed]) */
static PyObject* ddmmod_rand_asym(PyObject* self, PyObject* args)
{
    /* process arguments */
    PyArrayObject *py_mu, *py_b_lo, *py_b_up;
    double dt;
    int n, rngseed = 0;
    if (!PyArg_ParseTuple(args, "O!O!O!di|i", &PyArray_Type, &py_mu,
                          &PyArray_Type, &py_b_lo, &PyArray_Type, &py_b_up,
                          &dt, &n, &rngseed))
        return NULL;
    if (!(is1DDoubleArray(py_mu) && 
          is1DDoubleArray(py_b_lo) && is1DDoubleArray(py_b_up)))
        return NULL;
    if (dt <= 0.0) {
        PyErr_SetString(PyExc_ValueError, "dt needs to be larger than 0");
        return NULL;
    }
    if (n <= 0) {
        PyErr_SetString(PyExc_ValueError, "n needs to be larger than 0");
        return NULL;
    }
    ExtArray mu(ExtArray::shared_noowner((double*) PyArray_DATA(py_mu)),
                PyArray_DIM(py_mu, 0));
    ExtArray b_lo(ExtArray::shared_noowner((double*) PyArray_DATA(py_b_lo)),
                   PyArray_DIM(py_b_lo, 0));
    ExtArray b_up(ExtArray::shared_noowner((double*) PyArray_DATA(py_b_up)),
                   PyArray_DIM(py_b_up, 0));
    if (b_lo[0] >= 0.0) {
        PyErr_SetString(PyExc_ValueError, "b_lo[0] needs to be negative");
        return NULL;
    }
    if (b_up[0] <= 0.0) {
        PyErr_SetString(PyExc_ValueError, "b_up[0] needs to be positive");
        return NULL;
    }

    /* get output length and reserve space */
    npy_intp out_size[1] = { n };
    PyObject* py_t = PyArray_SimpleNew(1, out_size, NPY_DOUBLE);
    if (py_t == NULL) return NULL;
    PyObject* py_b = PyArray_SimpleNew(1, out_size, NPY_BOOL);
    if (py_b == NULL) {
        Py_DECREF(py_t);
        return NULL;
    }
    npy_double* t = (npy_double*) PyArray_DATA((PyArrayObject*) py_t);
    npy_bool* b = (npy_bool*) PyArray_DATA((PyArrayObject*) py_b);

    /* perform sampling */
    DMBase* dm = DMBase::create(mu, ExtArray::const_array(1.0), b_lo, b_up,
                                ExtArray::const_array(0.0), ExtArray::const_array(0.0), dt);
    DMBase::rngeng_t rngeng;
    if (rngseed == 0) rngeng.seed(std::random_device()());
    else rngeng.seed(rngseed);
    for (int i = 0; i < n; ++i) {
        DMSample s = dm->rand(rngeng);
        t[i] = s.t();
        b[i] = s.upper_bound() ? NPY_TRUE : NPY_FALSE;
    }
    delete dm;

    /* create tuple to return */
    PyObject* py_tuple = PyTuple_New(2);
    if (py_tuple == NULL) goto memory_fail;
    PyTuple_SET_ITEM(py_tuple, 0, py_t);
    PyTuple_SET_ITEM(py_tuple, 1, py_b);
    return py_tuple;

memory_fail:
    Py_DECREF(py_t);
    Py_DECREF(py_b);
    PyErr_SetString(PyExc_MemoryError, "out of memory");
    return NULL;
}
Example #4
0
/* method fpt_full(mu, sig2, b_lo, b_up, b_lo_deriv, b_up_deriv, dt, tmax,
 *                 [inv_leak, mnorm]) */
static PyObject* ddmmod_fpt_full(PyObject* self, PyObject* args, PyObject* keywds)
{
    /* process arguments */
    static char* kwlist[] = {"mu", "sig2", "b_lo", "b_up", "b_lo_deriv",
                             "b_up_deriv", "dt", "tmax", "inv_leak", "mnorm", NULL};
    PyArrayObject *py_mu, *py_sig2, *py_b_lo, *py_b_up, *py_b_lo_deriv, *py_b_up_deriv;
    double dt, t_max, inv_leak = 0.0;
    PyObject* mnorm_obj = NULL;
    if (!PyArg_ParseTupleAndKeywords(args, keywds, "O!O!O!O!O!O!dd|dO", kwlist, 
                                     &PyArray_Type, &py_mu,
                                     &PyArray_Type, &py_sig2,
                                     &PyArray_Type, &py_b_lo,
                                     &PyArray_Type, &py_b_up,
                                     &PyArray_Type, &py_b_lo_deriv,
                                     &PyArray_Type, &py_b_up_deriv,
                                     &dt, &t_max, &inv_leak, &mnorm_obj))
        return NULL;
    if (!(is1DDoubleArray(py_mu) && is1DDoubleArray(py_sig2) &&
          is1DDoubleArray(py_b_lo) && is1DDoubleArray(py_b_up) &&
          is1DDoubleArray(py_b_lo_deriv) && is1DDoubleArray(py_b_up_deriv)))
        return NULL;
    if (dt <= 0.0) {
        PyErr_SetString(PyExc_ValueError, "dt needs to be larger than 0");
        return NULL;
    }
    if (t_max <= 0.0) {
        PyErr_SetString(PyExc_ValueError, "tmax needs to be larger than 0");
        return NULL;
    }
    if (inv_leak < 0.0) {
        PyErr_SetString(PyExc_ValueError, "inv_leak needs to be non-negative");
        return NULL;
    }
    bool mnorm_bool = (mnorm_obj != NULL && PyObject_IsTrue(mnorm_obj) == 1);
    ExtArray mu(ExtArray::shared_noowner((double*) PyArray_DATA(py_mu)),
                PyArray_DIM(py_mu, 0));
    ExtArray sig2(ExtArray::shared_noowner((double*) PyArray_DATA(py_sig2)),
                  PyArray_DIM(py_sig2, 0));
    ExtArray b_lo(ExtArray::shared_noowner((double*) PyArray_DATA(py_b_lo)),
                  PyArray_DIM(py_b_lo, 0));
    ExtArray b_up(ExtArray::shared_noowner((double*) PyArray_DATA(py_b_up)),
                  PyArray_DIM(py_b_up, 0));
    ExtArray b_lo_deriv(ExtArray::shared_noowner((double*) PyArray_DATA(py_b_lo_deriv)),
                        PyArray_DIM(py_b_lo_deriv, 0));
    ExtArray b_up_deriv(ExtArray::shared_noowner((double*) PyArray_DATA(py_b_up_deriv)),
                        PyArray_DIM(py_b_up_deriv, 0));

    /* get output length and reserve space */
    int n_max = (int) ceil(t_max / dt);
    npy_intp out_size[1] = { n_max };
    PyObject* py_g1 = PyArray_SimpleNew(1, out_size, NPY_DOUBLE);
    if (py_g1 == NULL) return NULL;
    PyObject* py_g2 = PyArray_SimpleNew(1, out_size, NPY_DOUBLE);
    if (py_g2 == NULL) {
        Py_DECREF(py_g1);
        return NULL;
    }
    ExtArray g1(ExtArray::shared_noowner(
        (double*) PyArray_DATA((PyArrayObject*) py_g1)), n_max);
    ExtArray g2(ExtArray::shared_noowner(
        (double*) PyArray_DATA((PyArrayObject*) py_g2)), n_max);


    /* compute fpt */
    DMBase* dm = nullptr;
    if (inv_leak > 0.0)
        dm = DMBase::create(mu, sig2, b_lo, b_up, b_lo_deriv, b_up_deriv,
                            dt, inv_leak);
    else
        dm = DMBase::create(mu, sig2, b_lo, b_up, b_lo_deriv, b_up_deriv, dt);
    dm->pdfseq(n_max, g1, g2);
    if (mnorm_bool) dm->mnorm(g1, g2);
    delete dm;

    /* create tuple to return */
    PyObject* py_tuple = PyTuple_New(2);
    if (py_tuple == NULL) goto memory_fail;
    PyTuple_SET_ITEM(py_tuple, 0, py_g1);
    PyTuple_SET_ITEM(py_tuple, 1, py_g2);
    return py_tuple;

memory_fail:
    Py_DECREF(py_g1);
    Py_DECREF(py_g2);
    PyErr_SetString(PyExc_MemoryError, "out of memory");
    return NULL;
}
Example #5
0
/** the gateway function */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    /* [g1, g2] = ddm_fpt_full(mu, sig2, b_lo, b_up, b_lo_deriv, b_up_deriv,
                               delta_t, t_max, [leak]) */

    /* Check argument number */
    if (nlhs != 2) {
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongOutputs", 
                          "Wrong number of output arguments");
    }
    if (nrhs < 8) {
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInputs",
                          "Too few input arguments");
    }

    /* Process first 8 arguments */
    if (!MEX_ARGIN_IS_REAL_VECTOR(0))
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                          "First input argument expected to be a vector");
    if (!MEX_ARGIN_IS_REAL_VECTOR(1))
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                          "Second input argument expected to be a vector");
    if (!MEX_ARGIN_IS_REAL_VECTOR(2))
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                          "Third input argument expected to be a vector");
    if (!MEX_ARGIN_IS_REAL_VECTOR(3))
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                          "Fourth input argument expected to be a vector");
    if (!MEX_ARGIN_IS_REAL_VECTOR(4))
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                          "Fifth input argument expected to be a vector");
    if (!MEX_ARGIN_IS_REAL_VECTOR(5))
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                          "Sixth input argument expected to be a vector");
    if (!MEX_ARGIN_IS_REAL_DOUBLE(6))
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                          "Seventh input argument expected to be a double");
    if (!MEX_ARGIN_IS_REAL_DOUBLE(7))
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                          "Eight input argument expected to be a double");
    int mu_size = std::max(mxGetN(prhs[0]), mxGetM(prhs[0]));
    int sig2_size = std::max(mxGetN(prhs[1]), mxGetM(prhs[1]));
    int b_lo_size = std::max(mxGetN(prhs[2]), mxGetM(prhs[2]));
    int b_up_size = std::max(mxGetN(prhs[3]), mxGetM(prhs[3]));
    int b_lo_deriv_size = std::max(mxGetN(prhs[4]), mxGetM(prhs[4]));
    int b_up_deriv_size = std::max(mxGetN(prhs[5]), mxGetM(prhs[5]));
    ExtArray mu(ExtArray::shared_noowner(mxGetPr(prhs[0])), mu_size);
    ExtArray sig2(ExtArray::shared_noowner(mxGetPr(prhs[1])), sig2_size);
    ExtArray b_lo(ExtArray::shared_noowner(mxGetPr(prhs[2])), b_lo_size);
    ExtArray b_up(ExtArray::shared_noowner(mxGetPr(prhs[3])), b_up_size);
    ExtArray b_lo_deriv(ExtArray::shared_noowner(mxGetPr(prhs[4])), 0.0, b_lo_deriv_size);
    ExtArray b_up_deriv(ExtArray::shared_noowner(mxGetPr(prhs[5])), 0.0, b_up_deriv_size);
    double delta_t = mxGetScalar(prhs[6]);
    double t_max = mxGetScalar(prhs[7]);
    if (delta_t <= 0.0)
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                          "delta_t needs to be larger than 0.0");
    if (t_max <= delta_t)
        mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                          "t_max needs to be at least as large as delta_t");

    /* Process possible 9th non-string argument */
    int cur_argin = 8;
    bool has_leak = false;
    double inv_leak = 0.0;
    if (nrhs > cur_argin && !mxIsChar(prhs[cur_argin])) {
        if (!MEX_ARGIN_IS_REAL_DOUBLE(cur_argin))
            mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                              "Ninth input argument expected to be a double");
        inv_leak = mxGetScalar(prhs[cur_argin]);
        if (inv_leak < 0.0)
            mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                              "inv_leak needs to be non-negative");
        has_leak = (inv_leak != 0.0);
        ++cur_argin;
    }
        
    /* Process string arguments */
    bool normalise_mass = false;
    if (nrhs > cur_argin) {
        char str_arg[6];
        /* current only accept 'mnorm' string argument */
        if (!mxIsChar(prhs[cur_argin]))
            mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                              "String argument expected but not found");
        if (mxGetString(prhs[cur_argin], str_arg, sizeof(str_arg)) == 1 ||
            strcmp(str_arg, "mnorm") != 0)
            mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                              "\"mnorm\" string argument expected");
        /* this needs to be followed by "yes" or "no" */
        if (nrhs <= cur_argin + 1 || !mxIsChar(prhs[cur_argin + 1]))
            mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                              "String expected after \"mnorm\"");
        if (mxGetString(prhs[cur_argin + 1], str_arg, sizeof(str_arg)) == 1 ||
            (strcmp(str_arg, "yes") != 0 && strcmp(str_arg, "no") != 0))
            mexErrMsgIdAndTxt("ddm_fpt_full:WrongInput",
                              "\"yes\" or \"no\" expected after \"mnorm\"");
        normalise_mass = (strcmp(str_arg, "yes") == 0);
        
        /* no arguments allowed after that */
        if (nrhs > cur_argin + 2)
            mexErrMsgIdAndTxt("ddm_fpt_full:WrongInputs",
                              "Too many input arguments");
    }

    /* reserve space for output */
    int n = (int) ceil(t_max / delta_t);
    plhs[0] = mxCreateDoubleMatrix(1, n, mxREAL);
    plhs[1] = mxCreateDoubleMatrix(1, n, mxREAL);
    ExtArray g1(ExtArray::shared_noowner(mxGetPr(plhs[0])), n);
    ExtArray g2(ExtArray::shared_noowner(mxGetPr(plhs[1])), n);
    
    /* compute the pdf's */
    DMBase* dm = nullptr;
    if (has_leak)
        dm = DMBase::create(mu, sig2, b_lo, b_up, b_lo_deriv, b_up_deriv,
                            delta_t, inv_leak);
    else
        dm = DMBase::create(mu, sig2, b_lo, b_up, b_lo_deriv, b_up_deriv,
                            delta_t);
    dm->pdfseq(n, g1, g2);
    if (normalise_mass) dm->mnorm(g1, g2);
    delete dm;
}