Exemplo n.º 1
0
static BOOL Insert(Baum *Wurzel, BaumKnoten *Zweig, BaumKeyType Key, BaumDataType Daten)
{  BaumKnoten Element;

   if (*Zweig == NULL)
   {
      Element = (BaumKnoten)malloc(sizeof(BaumElement));
      if (Element != NULL)
      {
         Element->Key = Key;
         Element->Daten = Daten;
         Element->Right = NULL;
         Element->Left = NULL;
         *Zweig = Element;
         return(TRUE);
      }
      else
      {
         return(FALSE);
      }
   }
   else if (SMALLER(Wurzel->Compare(Key, (*Zweig)->Key)))
   {
      return(Insert(Wurzel, &((*Zweig)->Left), Key, Daten));
   }
   else
   {
      return(Insert(Wurzel, &((*Zweig)->Right), Key, Daten));
   }
}
Exemplo n.º 2
0
real sid_peek_eval(Model *m, PeekSet *ps, int c, int beg, int end, int *pn,
                   int *npass) {
  // evaluate on peek set
  // to speed up, we constrain competitive target words from top 10k words
  // evaluation on perplexity
  int i, j, k, s, lt, rt, md, tpass = 0;
  int *ids, l, h0n;
  real *p = (real *)malloc(ps->top_k * sizeof(real));
  real h0[NUP], h[NUP], hw, all = 0;
  *pn = 0;
  for (i = beg; i < end; i++) {
    tpass++;
    if (tpass == 5 || i == end - 1) {
      *npass += tpass;
      tpass = 0;
      LOGCLR(2);
      LOG(2, "[PEEK]: Eval completed %.2lf%% (sentences %d / %d, words %d)",
          (real)(*npass) * 100 / ps->size, *npass, ps->size, *pn);
    }
    ids = ps->wids[i];
    l = ps->wnum[i];
    h0n = 0;
    NumFillZeroVec(h0, m->n);
    for (j = 0; j < SMALLER(l, c); j++) {
      NumVecAddCVec(h0, m->scr + ids[j] * m->n, 1, m->n);
      h0n++;
    }
    for (j = 0; j < l; j++) {
      lt = j - c - 1;
      rt = j + c;
      md = j;
      if (rt < l) {
        NumVecAddCVec(h0, m->scr + ids[rt] * m->n, 1, m->n);
        h0n++;
      }
      if (lt >= 0) {
        NumVecAddCVec(h0, m->scr + ids[lt] * m->n, -1, m->n);
        h0n--;
      }
      hw = 1.0 / (h0n - 1.0);
      NumAddCVecDVec(h0, m->scr + ids[md] * m->n, hw, -hw, m->n, h);
      for (k = 0; k < ps->top_k; k++)
        if (ids[j] == ps->top_w[k]) break;
      if (k != ps->top_k) {  // only measure for the top_k words
        for (s = 0; s < ps->top_k; s++)
          p[s] = NumVecDot(h, m->tar + ps->top_w[s] * m->n, m->n);
        NumSoftMax(p, 1, ps->top_k);
        /* all -= sid_peek_log_likelihood(k, p, ps->top_k); */
        /* NumSoftMax(p, 1, ps->top_k); */
        /* printf("%e \n", p[k]); */
        all += p[k];
        (*pn)++;
      }
    }
  }
  all /= (*pn);
  free(p);
  return all;
}
Exemplo n.º 3
0
static BaumKnoten Finde(Baum *Wurzel, BaumKnoten Zweig, BaumKeyType Key)
{
   if (Zweig == NULL)
   {
      return(NULL);
   }
   else if (SMALLER(Wurzel->Compare(Key, Zweig->Key)))
   {
      return(Finde(Wurzel, Zweig->Left, Key));
   }
   else if (GREATER(Wurzel->Compare(Key, Zweig->Key)))
   {
      return(Finde(Wurzel, Zweig->Right, Key));
   }
   else
   {
      return(Zweig);
   }
}
Exemplo n.º 4
0
BOOL
FASTCALL
IntEngGradientFillTriangle(
    IN SURFOBJ  *psoDest,
    IN CLIPOBJ  *pco,
    IN XLATEOBJ  *pxlo,
    IN TRIVERTEX  *pVertex,
    IN ULONG  nVertex,
    IN PGRADIENT_TRIANGLE gTriangle,
    IN RECTL  *prclExtents,
    IN POINTL  *pptlDitherOrg)
{
    SURFOBJ *psoOutput;
    PTRIVERTEX v1, v2, v3;
    //RECT_ENUM RectEnum;
    //BOOL EnumMore;
    //ULONG i;
    POINTL Translate;
    INTENG_ENTER_LEAVE EnterLeave;
    RECTL FillRect = { 0, 0, 0, 0 };
    //ULONG Color;

    //BOOL sx[NLINES];
    //LONG x[NLINES], dx[NLINES], dy[NLINES], incx[NLINES], ex[NLINES], destx[NLINES];
    //LONG c[NLINES][3], dc[NLINES][3], ec[NLINES][3], ic[NLINES][3]; /* colors on lines */
    //LONG g, gx, gxi, gc[3], gd[3], ge[3], gi[3]; /* colors in triangle */
    //LONG sy, y, bt;

    v1 = (pVertex + gTriangle->Vertex1);
    v2 = (pVertex + gTriangle->Vertex2);
    v3 = (pVertex + gTriangle->Vertex3);

    /* bubble sort */
    if (SMALLER(v2, v1))
    {
        TRIVERTEX *t;
        SWAP(v1, v2, t);
    }

    if (SMALLER(v3, v2))
    {
        TRIVERTEX *t;
        SWAP(v2, v3, t);
        if (SMALLER(v2, v1))
        {
            SWAP(v1, v2, t);
        }
    }

    DPRINT("Triangle: (%i,%i) (%i,%i) (%i,%i)\n", v1->x, v1->y, v2->x, v2->y, v3->x, v3->y);
    /* FIXME: commented out because of an endless loop - fix triangles first */
    DPRINT("FIXME: IntEngGradientFillTriangle is broken\n");

    if (!IntEngEnter(&EnterLeave, psoDest, &FillRect, FALSE, &Translate, &psoOutput))
    {
        return FALSE;
    }

    //if (VCMPCLRS(v1, v2, v3))
    //{
    //  CLIPOBJ_cEnumStart(pco, FALSE, CT_RECTANGLES, CD_RIGHTDOWN, 0);
    //  do
    //  {
    //    EnumMore = CLIPOBJ_bEnum(pco, (ULONG) sizeof(RectEnum), (PVOID) &RectEnum);
    //    for (i = 0; i < RectEnum.c && RectEnum.arcl[i].top <= prclExtents->bottom; i++)
    //    {
    //      if (RECTL_bIntersectRect(&FillRect, &RectEnum.arcl[i], prclExtents))
    //      {
    //        BOOL InY;

    //        DOINIT(v1, v3, 0);
    //        DOINIT(v1, v2, 1);
    //        DOINIT(v2, v3, 2);

    //        y = v1->y;
    //        sy = v1->y + pptlDitherOrg->y;
    //        bt = min(v3->y + pptlDitherOrg->y, FillRect.bottom);

    //        while (sy < bt)
    //        {
    //          InY = !(sy < FillRect.top || sy >= FillRect.bottom);
    //          GOLINE(v1, v3, 0);
    //          DOLINE(v1, v3, 0);
    //          ENDLINE(v1, v3, 0);

    //          GOLINE(v1, v2, 1);
    //          DOLINE(v1, v2, 1);
    //          FILLLINE(0, 1);
    //          ENDLINE(v1, v2, 1);

    //          GOLINE(v2, v3, 2);
    //          DOLINE(v2, v3, 2);
    //          FILLLINE(0, 2);
    //          ENDLINE(23, v3, 2);

    //          y++;
    //          sy++;
    //        }
    //      }
    //    }
    //  } while (EnumMore);

    //  return IntEngLeave(&EnterLeave);
    //}

    ///* fill triangle with one solid color */

    //Color = XLATEOBJ_iXlate(pxlo, RGB(v1->Red >> 8, v1->Green >> 8, v1->Blue >> 8));
    //CLIPOBJ_cEnumStart(pco, FALSE, CT_RECTANGLES, CD_RIGHTDOWN, 0);
    //do
    //{
    //  EnumMore = CLIPOBJ_bEnum(pco, (ULONG) sizeof(RectEnum), (PVOID) &RectEnum);
    //  for (i = 0; i < RectEnum.c && RectEnum.arcl[i].top <= prclExtents->bottom; i++)
    //  {
    //    if (RECTL_bIntersectRect(&FillRect, &RectEnum.arcl[i], prclExtents))
    //    {
    //      S_INITLINE(v1, v3, 0);
    //      S_INITLINE(v1, v2, 1);
    //      S_INITLINE(v2, v3, 2);

    //      y = v1->y;
    //      sy = v1->y + pptlDitherOrg->y;
    //      bt = min(v3->y + pptlDitherOrg->y, FillRect.bottom);

    //      while (sy < bt)
    //      {
    //        S_GOLINE(v1, v3, 0);
    //        S_DOLINE(v1, v3, 0);
    //        S_ENDLINE(v1, v3, 0);

    //        S_GOLINE(v1, v2, 1);
    //        S_DOLINE(v1, v2, 1);
    //        S_FILLLINE(0, 1);
    //        S_ENDLINE(v1, v2, 1);

    //        S_GOLINE(v2, v3, 2);
    //        S_DOLINE(v2, v3, 2);
    //        S_FILLLINE(0, 2);
    //        S_ENDLINE(23, v3, 2);

    //        y++;
    //        sy++;
    //      }
    //    }
    //  }
    //} while (EnumMore);

    return IntEngLeave(&EnterLeave);
}
Exemplo n.º 5
0
int DcmeUpdate(int* ids, int l, DcmeBookkeeping* b, heap* twh) {
  int i, j, k, lt, rt, md, zz, h0n = 0, flag, offline_done = 0;
  real c, h0[NUP], h[NUP], hw[SUP], wd[NUP * SUP], w0[NUP], w[NUP];
  int window = C;  // int window = NumRand() * C + 1;
  NumFillZeroVec(h0, N);
  NumFillZeroVec(w0, N);
  for (i = 0; i < SMALLER(l, window); i++) {
    NumVecAddCVec(h0, model->scr + ids[i] * N, 1, N);  // h0++
    h0n++;                                             // h0n++
  }
  for (i = 0; i < l; i++) {
    lt = i - window - 1;
    rt = i + window;
    md = i;
    if (rt < l) {
      NumVecAddCVec(h0, model->scr + ids[rt] * N, 1, N);   // h0++
      h0n++;                                               // h0n++
    }                                                      //
    if (lt >= 0) {                                         //
      NumVecAddCVec(h0, model->scr + ids[lt] * N, -1, N);  // h0--
      h0n--;                                               // h0n--
    }                                                      //
    hw[md] = 1.0 / (h0n - 1.0);                            // hw
    NumAddCVecDVec(h0, model->scr + ids[md] * N, hw[md], -hw[md], N, h);  // h
    ///////////////////////////////////////////////////////////////////////////
    zz = DcmeDualDecode(h, b);  // dcd z
    if (V_DUAL_HI) {            // b->hi
      for (k = lt + 1; k <= rt; k++)
        if (k >= 0 && k < l && k != i) b->hi[zz * V + ids[k]] += hw[i];
    } else
      NumVecAddCVec(b->hh + zz * N, h, 1.0, N);  // b->hh
    b->hn[zz] += 1;                              // b->hn
    if (V_MICRO_ME) {                            // micro ME
      DcmeMicroME(zz, ids[i], h, wd + i * N, b);
    } else {
      NumAddCVecDVec(model->tar + ids[i] * N, b->ww + zz * N, 1, -1, N,
                     wd + i * N);  // wd[i] <- w - ww
      flag = 1;                    // current word not in top words
      for (k = 0; k < Q; k++) {    // up tar (pos, neg top words)
        j = b->tw[zz * Q + k];
        if (j == ids[i]) flag = 0;
        c = b->dd[zz * V + j] - (j == ids[i] ? 1 : 0);
        ModelGradUpdate(model, 1, j, c * gd_ss, h);
        DcmeAdjustWW(j, zz, c * gd_ss, h, b);
        ModelVecRegularize(model, 1, j, V_MODEL_PROJ_BALL_NORM, -1);
      }
      if (flag) {
        ModelGradUpdate(model, 1, ids[i], -gd_ss, h);
        DcmeAdjustWW(ids[i], zz, -gd_ss, h, b);
        ModelVecRegularize(model, 1, ids[i], V_MODEL_PROJ_BALL_NORM, -1);
      }
    }
    ///////////////////////////////////////////////////////////////////////////
    lt = i - 2 * window - 1;
    rt = i;
    md = i - window;
    NumVecAddCVec(w0, wd + rt * N, hw[rt], N);                  // w0++
    while (md < l) {                                            //
      if (lt >= 0) NumVecAddCVec(w0, wd + lt * N, -hw[lt], N);  // w0--
      if (md >= 0) {                                            //
        NumAddCVecDVec(w0, wd + md * N, 1, -hw[md], N, w);      // w
        ModelGradUpdate(model, 0, ids[md], -gd_ss, w);          // up m->scr
        ModelVecRegularize(model, 0, ids[md], V_MODEL_PROJ_BALL_NORM, -1);
      }
      if (i == l - 1) {
        md++;
        lt++;
      } else
        break;
    }
    ///////////////////////////////////////////////////////////////////////////
    if (b->hn[zz] >= V * V_OFFLINE_INTERVAL_VOCAB_RATIO) {  // update offline
      DcmeOfflineUpdate(zz, b, twh);
      offline_done = 1;
    }
  }
  return offline_done;
}
Exemplo n.º 6
0
input_t get_input(int nlhs,int nrhs,const mxArray *prhs[]) {
  input_t input;
  int n,i;
  double *x,*ry_temp,*iy_temp,*third,fourth,fifth;
  COMPLEX_T *y;
  
  input.stop_params.threshold = DEFAULT_THRESHOLD;
  input.stop_params.tolerance = DEFAULT_TOLERANCE;
  input.allocated_x=0;
  #ifdef _ALT_MEXERRMSGTXT_
  input.error_flag=0;
  #endif
  input.max_imfs=0;
  input.nbphases=DEFAULT_NBPHASES;
  
  /* argument checking*/
  if (nrhs>5)
    mexErrMsgTxt("Too many arguments");
  if (nrhs<2)
    mexErrMsgTxt("Not enough arguments");
  if (nlhs>2)
    mexErrMsgTxt("Too many output arguments");
  if (!mxIsEmpty(prhs[0]))
    if (!mxIsNumeric(prhs[0]) || mxIsComplex(prhs[0]) ||
    mxIsSparse(prhs[0]) || !mxIsDouble(prhs[0]) ||
    (mxGetNumberOfDimensions(prhs[0]) > 2))
      mexErrMsgTxt("X must be either empty or a double precision real vector.");
  
  if (!mxIsNumeric(prhs[1]) || !mxIsComplex(prhs[1]) ||
  mxIsSparse(prhs[1]) || !mxIsDouble(prhs[1]) ||/* length of vector x */
  (mxGetNumberOfDimensions(prhs[1]) > 2))
    mexErrMsgTxt("Y must be a double precision complex vector.");
  
  /* input reading: x and y */
  n=GREATER(mxGetN(prhs[1]),mxGetM(prhs[1])); /* length of vector x */
  if (mxIsEmpty(prhs[0])) {
    input.allocated_x = 1;
    x = (double *)malloc(n*sizeof(double));
    for(i=0;i<n;i++) x[i] = i;
  }
  else
    x=mxGetPr(prhs[0]);
  ry_temp=mxGetPr(prhs[1]);
  iy_temp=mxGetPi(prhs[1]);
  
  /* third argument */
  if (nrhs>=3) {
    if(!mxIsEmpty(prhs[2])) {
      if (!mxIsNumeric(prhs[2]) || mxIsComplex(prhs[2]) || mxIsSparse(prhs[2])
      || !mxIsDouble(prhs[2]) || (mxGetN(prhs[2])!=1 && mxGetM(prhs[2])!=1))
        mexPrintf("STOP must be a real vector of 1 or 2 elements");
      i = GREATER(mxGetN(prhs[2]),mxGetM(prhs[2]));
      if (i>2)
        mexErrMsgTxt("STOP must be a vector of 1 or 2 elements");
      third=mxGetPr(prhs[2]);
      switch (i) {
        case 1 : {
          input.stop_params.threshold=*third;
          break;
        }
        case 2 : {
          input.stop_params.threshold=third[0];
          input.stop_params.tolerance=third[1];
        }
      }
      /* input checking */
      if (input.stop_params.threshold <= 0)
        mexErrMsgTxt("threshold must be a positive number");
      if (input.stop_params.threshold >= 1)
        mexWarnMsgTxt("threshold should be lower than 1");
      if (input.stop_params.tolerance < 0 || input.stop_params.tolerance >= 1)
        mexErrMsgTxt("tolerance must be a real number in [O,1]");
    }
  }
  
  
  /* fourth argument */
  if (nrhs>=4) {
    if (!mxIsEmpty(prhs[3])) { /* if empty -> do nothing */
      if (!mxIsNumeric(prhs[3]) || mxIsComplex(prhs[3]) || mxIsSparse(prhs[3])
      || !mxIsDouble(prhs[3]) || mxGetN(prhs[3])!=1 || mxGetM(prhs[3])!=1)
        mexErrMsgTxt("NB_IMFS must be a positive integer");
      fourth=*mxGetPr(prhs[3]);
      if ((unsigned int)fourth != fourth)
        mexErrMsgTxt("NB_IMFS must be a positive integer");
      input.max_imfs=(int)fourth;
    }
  }
  
  /* fifth argument */
  if (nrhs==5) {
    if(!mxIsNumeric(prhs[4]) || mxIsComplex(prhs[4]) || mxIsSparse(prhs[4])
    || mxGetN(prhs[4])!=1 || mxGetM(prhs[4])!=1)
      mexErrMsgTxt("NBPHASES must be a positive integer");
    fifth=*mxGetPr(prhs[4]);
    if ((int)fifth != fifth)
      mexErrMsgTxt("NBPHASES must be a positive integer");
    input.nbphases = (int)fifth;
  }
  
  
  /* more input checking */
  if (!input.allocated_x && SMALLER(mxGetN(prhs[0]),mxGetM(prhs[0]))!=1 ||
  SMALLER(mxGetN(prhs[1]),mxGetM(prhs[1]))!=1)
    mexErrMsgTxt("X and Y must be vectors");
  if (GREATER(mxGetN(prhs[1]),mxGetM(prhs[1]))!=n)
    mexErrMsgTxt("X and Y must have the same length");
  i=1;
  while (i<n && x[i]>x[i-1]) i++;
  if (i<n) mexErrMsgTxt("Values in X must be non decreasing");
  
  /* copy vector y to avoid erasing input data */
  y=(COMPLEX_T *)malloc(n*sizeof(COMPLEX_T));
  #ifdef C99_OK
  for (i=0;i<n;i++) y[i]=ry_temp[i]+I*iy_temp[i];
  #else
  for (i=0;i<n;i++) {
    y[i].r=ry_temp[i];
    y[i].i=iy_temp[i];
  }
  #endif
  
  input.n=n;
  input.x=x;
  input.y=y;
  return input;
}