static void do_worm(struct state *st, struct worm *w) { int type = w->s / 4; int dir = w->s % 4; w->c = (w->c+w->cc) % st->ncolors; if (PROB(st->disorder)) type=PROB(st->handedness); switch(type) { case 0: /* CCW */ TRY(CCW) TRY(STR) TRY(CW) RANDOM; break; case 1: /* CW */ TRY(CW) TRY(STR) TRY(CCW) RANDOM; break; } w->s = type*4+dir; w->h = w->h % st->width; w->v = w->v % st->height; }
static void squiral_init_1 (struct state *st) { int i; if (st->worms) free (st->worms); if (st->fill) free (st->fill); st->worms=calloc(st->count, sizeof(struct worm)); st->fill=calloc(st->width*st->height, sizeof(int)); st->dirh[0]=0; st->dirh[1]=1; st->dirh[2]=0; st->dirh[3]=st->width-1; st->dirv[0]=st->height-1; st->dirv[1]=0; st->dirv[2]=1; st->dirv[3]=0; for(i=0; i<st->count; i++) { st->worms[i].h=R(st->width); st->worms[i].v=R(st->height); st->worms[i].s=R(4)+4*PROB(st->handedness); st->worms[i].c=R(st->ncolors); if(st->cycle) { st->worms[i].cc=R(3)+st->ncolors; } else st->worms[i].cc=0; } }
void tmodel<COUNT, PROB>::printProbTableInverse(const char *filename, const Vector<WordEntry>& evlist, const Vector<WordEntry>& fvlist, const double, const double, const bool actual) const // this function dumps the inverse t table. Each line is of the format: // // target_word_id source_word_id p(source_word/target_word) // // if flag "actual " is true then print actual word entries instead of // token ids { cerr << "Dumping the t table inverse to file: " << filename << '\n'; ofstream of(filename); typename hash_map<wordPairIds, CPPair, hashpair, equal_to<wordPairIds> >::const_iterator i; PROB p_inv = 0 ; // static const PROB ratio(double(fTotal)/eTotal); WordIndex e, f ; int no_errors(0); vector<PROB> total(fvlist.size(),PROB(0)) ; // Sum over all e of P(f/e) * p(e) - needed for normalization for(i = ef.begin(); i != ef.end(); i++){ e = ((*i).first).first ; f = ((*i).first).second ; total[f] += (PROB) evlist[e].freq * ((*i).second.prob); //add P(f/ei) * F(ei) } for(i = ef.begin(); i != ef.end(); i++){ e = ((*i).first).first ; f = ((*i).first).second ; p_inv = ((*i).second.prob) * (PROB) evlist[e].freq / total[f] ; if (p_inv > 1.0001 || p_inv < 0){ no_errors++; if (no_errors <= 10){ cerr << "printProbTableInverse(): Error - P("<<evlist[e].word<<"("<< e<<") / "<<fvlist[f].word << "("<<f<<")) = " << p_inv <<'\n'; cerr << "f(e) = "<<evlist[e].freq << " Sum(p(f/e).f(e)) = " << total[f] << " P(f/e) = " <<((*i).second.prob) <<'\n'; if (no_errors == 10) cerr<<"printProbTableInverse(): Too many P inverse errors ..\n"; } } if (actual) of << fvlist[f].word << ' ' << evlist[e].word << ' ' << p_inv << '\n'; else of << f << ' ' << e << ' ' << p_inv << '\n'; } }
void poisson_prob(double lambda, int left, int right, double *prob, double *weight) { // work variables int mode, j, t, s; mode = (int) lambda; if (mode >= 1) { PROB(mode) = exp(-lambda + mode * log(lambda) - LOG2PIOVER2 - (mode + 1.0/2.0) * log(mode) + mode); } else { PROB(mode) = exp(-lambda); } // -- down -- for (j=mode; j>=left+1; j--) { PROB(j-1) = PROB(j) * j / lambda; } // -- up -- for (j=mode; j<=right-1; j++) { PROB(j+1) = PROB(j) * lambda / (j+1); } // -- compute W -- *weight = 0.0; s = left; t = right; while (s < t) { if (PROB(s) <= PROB(t)) { *weight += PROB(s); s++; } else { *weight += PROB(t); t--; } } *weight += PROB(s); }