Пример #1
0
void parse1(){
  unsigned int i;
  expr e;
  double x;

  e = parse("x^2+1");
  for(i = 0; i<1000; i++){
    x = random_();
    ASSERT_SIM(eval(e, x, 0), f1(x));
  }

  e = parse("sin(x)^4");
  for(i = 0; i<1000; i++){
    x = random_();
    ASSERT_SIM(eval(e, x, 0), f2(x));
  }

  e = parse("2^(sin(x)^2)");
  for(i = 0; i<1000; i++){
    x = random_();
    ASSERT_SIM(eval(e, x, 0), f3(x));
  }

  e = parse("(x-2)^2 + 1");
  for(i = 0; i<1000; i++){
    x = random_();
    ASSERT_SIM(eval(e, x, 0), f4(x));
  }
}
Пример #2
0
    double likelihood_z(int s_id, int q_id) {
        double s_id_sample = 0;
        double q_id_sample = 0;

        double sum_new = 0;
        for (int i=0; i<o.size(); i++) {
            double s_ = s[o[i].get_sid()].last();
            double q_ = q[o[i].get_qid()].last();
            if (o[i].get_sid() == s_id) {
                s_ = s[s_id].sample();
                s_id_sample = s_;
            }
            if (o[i].get_qid() == q_id) {
                q_ = q[q_id].sample();
                q_id_sample = q_;
            }
            sum_new -= (log(1 + exp(-(s_-q_))) + (1-o[i].get_response())*(s_-q_));
            sum_new -= 0.5*s_*s_;
            sum_new -= 0.5*q_*q_;
        }

        double sum_old = 0;
        for (int i=0; i<o.size(); i++) {
            double s_ = s[o[i].get_sid()].last();
            double q_ = q[o[i].get_qid()].last();
            sum_old -= (log(1 + exp(-(s_-q_))) + (1-o[i].get_response())*(s_-q_));
            sum_old -= 0.5*s_*s_;
            sum_old -= 0.5*q_*q_;
        }

        double r = exp(sum_new - sum_old);
        if (random_(0, 1) < r) {
            for (int i=0; i<s.size(); i++) {
                if (i==s_id)
                    s[i].insert(s_id_sample);
                else
                    s[i].insert(s[i].last());
            }
            for (int i=0; i<q.size(); i++) {
                if (i==q_id)
                    q[i].insert(q_id_sample);
                else
                    q[i].insert(q[i].last());
            }
        }
        else {
            for (int i=0; i<s.size(); i++) {
                s[i].insert(s[i].last());
            }
            for (int i=0; i<q.size(); i++) {
                q[i].insert(q[i].last());
            }

        }
    }
Пример #3
0
    void generate_random_clusters( const std::vector<sample_type>& samples)
    {
        typedef boost::variate_generator<boost::mt19937&, boost::uniform_int<> > random_type;
        random_type random_( random_generator_, boost::uniform_int<>(0, samples.size() - 1));

        for( int i = 0; i < K_; ++i)
        {
            while( 1)
            {
                int indx = random_();

                if( labels_[indx] == 0)
                {
                    clusters_.push_back( cluster_t( samples[indx]));
                    labels_[indx] = &(clusters_.back());
                    break;
                }
            }
        }
    }
Пример #4
0
std::tuple<Move, Value> 
AperyBook::probe(const Position &pos, const std::string &fname, bool pick_best) 
{
  AperyBookEntry entry;
  uint16_t best = 0;
  uint32_t sum = 0;
  Move move = kMoveNone;
  Key key = book_key(pos);
  Value min_book_score = static_cast<Value>(static_cast<int>(Options["Min_Book_Score"]));
  Value score = kValueZero;

  if (file_name_ != fname && !open(fname.c_str()))
    return std::make_tuple(kMoveNone, kValueZero);

  binary_search(key);
                                                                                                                                              
  while (read(reinterpret_cast<char*>(&entry), sizeof(entry)), entry.key == key && good()) 
  {
    best = std::max(best, entry.count);
    sum += entry.count;
                                                                                                                                              
    if 
    (
      min_book_score <= entry.score
      &&
      (
        (random_() % sum < entry.count)
        ||
        (pick_best && entry.count == best)
      )
    )
    {
      Square to = to_square(entry.from_to_pro & 0x007fU);
      int from_raw = (entry.from_to_pro >> 7) & 0x007fU;
      if (from_raw >= kBoardSquare) 
      {
        move = move_init(to, to_drop_piece_type(static_cast<Square>(from_raw)));
      }
      else
      {
        Square from = to_square(from_raw);
        PieceType pt_from = type_of(pos.square(from));
        if (entry.from_to_pro & kPromoted)
          move = move_init(from, to, pt_from, type_of(pos.square(to)), true);
        else
          move = move_init(from, to, pt_from, type_of(pos.square(to)), false);
      }
      score = entry.score;
    }
  }
Пример #5
0
/* Subroutine */ int bsynz_(real *coef, integer *ip, integer *iv, 
	real *sout, real *rms, real *ratio, real *g2pass,
			    struct lpc10_decoder_state *st)
{
    /* Initialized data */

    integer *ipo;
    real *rmso;
    static integer kexc[25] = { 8,-16,26,-48,86,-162,294,-502,718,-728,184,
	    672,-610,-672,184,728,718,502,294,162,86,48,26,16,8 };
    real *exc;
    real *exc2;
    real *lpi1;
    real *lpi2;
    real *lpi3;
    real *hpi1;
    real *hpi2;
    real *hpi3;

    /* System generated locals */
    integer i__1, i__2;
    real r__1, r__2;

    /* Builtin functions */
    double sqrt(doublereal);

    /* Local variables */
    real gain, xssq;
    integer i__, j, k;
    real noise[166], pulse;
    integer px;
    real sscale;
    extern integer random_(struct lpc10_decoder_state *);
    real xy, sum, ssq;
    real lpi0, hpi0;

/* $Log: bsynz.c,v $
/* Revision 1.1  2007/10/22 07:40:49  shorne
/* *** empty log message ***
/*
/* Revision 1.2  2006/08/01 13:06:49  rjongbloed
/* Added a raft of unvalidated audio codecs from OpenH323 tree
/*
/* Revision 1.1.2.1  2006/07/22 14:03:14  rjongbloed
/* Added more plug ins
/*
/* Revision 1.1.2.1  2006/05/08 13:49:57  rjongbloed
/* Imported all the audio codec plug ins from OpenH323
/*
/* Revision 1.1  2004/05/04 11:16:42  csoutheren
/* Initial version
/*
/* Revision 1.2  2002/02/15 03:57:55  yurik
/* Warnings removed during compilation, patch courtesy of Jehan Bing, [email protected]
/*
/* Revision 1.1  2000/06/05 04:45:12  robertj
/* Added LPC-10 2400bps codec
/*
 * Revision 1.2  1996/08/20  20:18:55  jaf
 * Removed all static local variables that were SAVE'd in the Fortran
 * code, and put them in struct lpc10_decoder_state that is passed as an
 * argument.
 *
 * Removed init function, since all initialization is now done in
 * init_lpc10_decoder_state().
 *
 * Revision 1.1  1996/08/19  22:32:58  jaf
 * Initial revision
 * */
/* Revision 1.3  1996/03/29  22:03:47  jaf */
/* Removed definitions for any constants that were no longer used. */

/* Revision 1.2  1996/03/26  19:34:33  jaf */
/* Added comments indicating which constants are not needed in an */
/* application that uses the LPC-10 coder. */

/* Revision 1.1  1996/02/07  14:43:51  jaf */
/* Initial revision */

/*   LPC Configuration parameters: */
/* Frame size, Prediction order, Pitch period */
/*       Arguments */
/* $Log: bsynz.c,v $
/* Revision 1.1  2007/10/22 07:40:49  shorne
/* *** empty log message ***
/*
/* Revision 1.2  2006/08/01 13:06:49  rjongbloed
/* Added a raft of unvalidated audio codecs from OpenH323 tree
/*
/* Revision 1.1.2.1  2006/07/22 14:03:14  rjongbloed
/* Added more plug ins
/*
/* Revision 1.1.2.1  2006/05/08 13:49:57  rjongbloed
/* Imported all the audio codec plug ins from OpenH323
/*
/* Revision 1.1  2004/05/04 11:16:42  csoutheren
/* Initial version
/*
/* Revision 1.2  2002/02/15 03:57:55  yurik
/* Warnings removed during compilation, patch courtesy of Jehan Bing, [email protected]
/*
/* Revision 1.1  2000/06/05 04:45:12  robertj
/* Added LPC-10 2400bps codec
/*
 * Revision 1.2  1996/08/20  20:18:55  jaf
 * Removed all static local variables that were SAVE'd in the Fortran
 * code, and put them in struct lpc10_decoder_state that is passed as an
 * argument.
 *
 * Removed init function, since all initialization is now done in
 * init_lpc10_decoder_state().
 *
 * Revision 1.1  1996/08/19  22:32:58  jaf
 * Initial revision
 * */
/* Revision 1.3  1996/03/29  22:05:55  jaf */
/* Commented out the common block variables that are not needed by the */
/* embedded version. */

/* Revision 1.2  1996/03/26  19:34:50  jaf */
/* Added comments indicating which constants are not needed in an */
/* application that uses the LPC-10 coder. */

/* Revision 1.1  1996/02/07  14:44:09  jaf */
/* Initial revision */

/*   LPC Processing control variables: */

/* *** Read-only: initialized in setup */

/*  Files for Speech, Parameter, and Bitstream Input & Output, */
/*    and message and debug outputs. */

/* Here are the only files which use these variables: */

/* lpcsim.f setup.f trans.f error.f vqsetup.f */

/* Many files which use fdebug are not listed, since it is only used in */
/* those other files conditionally, to print trace statements. */
/* 	integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
/*  LPC order, Frame size, Quantization rate, Bits per frame, */
/*    Error correction */
/* Subroutine SETUP is the only place where order is assigned a value, */
/* and that value is 10.  It could increase efficiency 1% or so to */
/* declare order as a constant (i.e., a Fortran PARAMETER) instead of as 
*/
/* a variable in a COMMON block, since it is used in many places in the */
/* core of the coding and decoding routines.  Actually, I take that back. 
*/
/* At least when compiling with f2c, the upper bound of DO loops is */
/* stored in a local variable before the DO loop begins, and then that is 
*/
/* compared against on each iteration. */
/* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
/* Similarly for quant, which is given a value of 2400 in SETUP.  quant */
/* is used in only a few places, and never in the core coding and */
/* decoding routines, so it could be eliminated entirely. */
/* nbits is similar to quant, and is given a value of 54 in SETUP. */
/* corrp is given a value of .TRUE. in SETUP, and is only used in the */
/* subroutines ENCODE and DECODE.  It doesn't affect the speed of the */
/* coder significantly whether it is .TRUE. or .FALSE., or whether it is 
*/
/* a constant or a variable, since it is only examined once per frame. */
/* Leaving it as a variable that is set to .TRUE.  seems like a good */
/* idea, since it does enable some error-correction capability for */
/* unvoiced frames, with no change in the coding rate, and no noticeable 
*/
/* quality difference in the decoded speech. */
/* 	integer quant, nbits */
/* *** Read/write: variables for debugging, not needed for LPC algorithm 
*/

/*  Current frame, Unstable frames, Output clip count, Max onset buffer, 
*/
/*    Debug listing detail level, Line count on listing page */

/* nframe is not needed for an embedded LPC10 at all. */
/* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
/* ERROR, which is only called from RCCHK.  When LPC10 is embedded into */
/* an application, I would recommend removing the call to ERROR in RCCHK, 
*/
/* and remove ERROR and nunsfm completely. */
/* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in 
*/
/* sread.f.  When LPC10 is embedded into an application, one might want */
/* to cause it to be incremented in a routine that takes the output of */
/* SYNTHS and sends it to an audio device.  It could be optionally */
/* displayed, for those that might want to know what it is. */
/* maxosp is never initialized to 0 in SETUP, although it probably should 
*/
/* be, and it is updated in subroutine ANALYS.  I doubt that its value */
/* would be of much interest to an application in which LPC10 is */
/* embedded. */
/* listl and lincnt are not needed for an embedded LPC10 at all. */
/* 	integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
/* 	common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
/* 	common /contrl/ quant, nbits */
/* 	common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
/*       Function return value definitions */
/* 	Parameters/constants */
/*       KEXC is not a Fortran PARAMETER, but it is an array initialized 
*/
/*       with a DATA statement that is never modified. */
/*       Local variables that need not be saved */
/*       NOISE is declared with range (1:MAXPIT+MAXORD), but only indices 
*/
/*       ORDER+1 through ORDER+IP are ever used, and I think that IP */
/*       .LE. MAXPIT.  Why not declare it to be in the range (1:MAXPIT) */
/*       and use that range? */
/*       Local state */
/*       I believe that only indices 1 through ORDER of EXC need to be */
/*       saved from one invocation to the next, but we may as well save */
/*       the whole array. */
/*       None of these local variables were given initial values in the */
/*       original code.  I'm guessing that 0 is a reasonable initial */
/*       value for all of them. */
    /* Parameter adjustments */
    if (coef) {
	--coef;
	}
    if (sout) {
	--sout;
	}

    /* Function Body */
    ipo = &(st->ipo);
    exc = &(st->exc[0]);
    exc2 = &(st->exc2[0]);
    lpi1 = &(st->lpi1);
    lpi2 = &(st->lpi2);
    lpi3 = &(st->lpi3);
    hpi1 = &(st->hpi1);
    hpi2 = &(st->hpi2);
    hpi3 = &(st->hpi3);
    rmso = &(st->rmso_bsynz);

/*                  MAXPIT+MAXORD=166 */
/*  Calculate history scale factor XY and scale filter state */
/* Computing MIN */
    r__1 = *rmso / (*rms + 1e-6f);
    xy = min(r__1,8.f);
    *rmso = *rms;
    i__1 = contrl_1.order;
    for (i__ = 1; i__ <= i__1; ++i__) {
	exc2[i__ - 1] = exc2[*ipo + i__ - 1] * xy;
    }
    *ipo = *ip;
    if (*iv == 0) {
/*  Generate white noise for unvoiced */
	i__1 = *ip;
	for (i__ = 1; i__ <= i__1; ++i__) {
	    exc[contrl_1.order + i__ - 1] = (real) (random_(st) / 64);
	}
/*  Impulse doublet excitation for plosives */
/*       (RANDOM()+32768) is in the range 0 to 2**16-1.  Therefore the
 */
/*       following expression should be evaluated using integers with 
at */
/*       least 32 bits (16 isn't enough), and PX should be in the rang
e */
/*       ORDER+1+0 through ORDER+1+(IP-2) .EQ. ORDER+IP-1. */
	px = (random_(st) + 32768) * (*ip - 1) / 65536 + contrl_1.order + 1;
	r__1 = *ratio / 4 * 1.f;
	pulse = r__1 * 342;
	if (pulse > 2e3f) {
	    pulse = 2e3f;
	}
	exc[px - 1] += pulse;
	exc[px] -= pulse;
/*  Load voiced excitation */
    } else {
	sscale = (real)sqrt((real) (*ip)) / 6.928f;
	i__1 = *ip;
	for (i__ = 1; i__ <= i__1; ++i__) {
	    exc[contrl_1.order + i__ - 1] = 0.f;
	    if (i__ <= 25) {
		exc[contrl_1.order + i__ - 1] = sscale * kexc[i__ - 1];
	    }
	    lpi0 = exc[contrl_1.order + i__ - 1];
	    r__2 = exc[contrl_1.order + i__ - 1] * .125f + *lpi1 * .75f;
	    r__1 = r__2 + *lpi2 * .125f;
	    exc[contrl_1.order + i__ - 1] = r__1 + *lpi3 * 0.f;
	    *lpi3 = *lpi2;
	    *lpi2 = *lpi1;
	    *lpi1 = lpi0;
	}
	i__1 = *ip;
	for (i__ = 1; i__ <= i__1; ++i__) {
	    noise[contrl_1.order + i__ - 1] = random_(st) * 1.f / 64;
	    hpi0 = noise[contrl_1.order + i__ - 1];
	    r__2 = noise[contrl_1.order + i__ - 1] * -.125f + *hpi1 * .25f;
	    r__1 = r__2 + *hpi2 * -.125f;
	    noise[contrl_1.order + i__ - 1] = r__1 + *hpi3 * 0.f;
	    *hpi3 = *hpi2;
	    *hpi2 = *hpi1;
	    *hpi1 = hpi0;
	}
	i__1 = *ip;
	for (i__ = 1; i__ <= i__1; ++i__) {
	    exc[contrl_1.order + i__ - 1] += noise[contrl_1.order + i__ - 1];
	}
    }
/*   Synthesis filters: */
/*    Modify the excitation with all-zero filter  1 + G*SUM */
    xssq = 0.f;
    i__1 = *ip;
    for (i__ = 1; i__ <= i__1; ++i__) {
	k = contrl_1.order + i__;
	sum = 0.f;
	i__2 = contrl_1.order;
	for (j = 1; j <= i__2; ++j) {
	    sum += coef[j] * exc[k - j - 1];
	}
	sum *= *g2pass;
	exc2[k - 1] = sum + exc[k - 1];
    }
/*   Synthesize using the all pole filter  1 / (1 - SUM) */
    i__1 = *ip;
    for (i__ = 1; i__ <= i__1; ++i__) {
	k = contrl_1.order + i__;
	sum = 0.f;
	i__2 = contrl_1.order;
	for (j = 1; j <= i__2; ++j) {
	    sum += coef[j] * exc2[k - j - 1];
	}
	exc2[k - 1] = sum + exc2[k - 1];
	xssq += exc2[k - 1] * exc2[k - 1];
    }
/*  Save filter history for next epoch */
    i__1 = contrl_1.order;
    for (i__ = 1; i__ <= i__1; ++i__) {
	exc[i__ - 1] = exc[*ip + i__ - 1];
	exc2[i__ - 1] = exc2[*ip + i__ - 1];
    }
/*  Apply gain to match RMS */
    r__1 = *rms * *rms;
    ssq = r__1 * *ip;
    gain = (real)sqrt(ssq / xssq);
    i__1 = *ip;
    for (i__ = 1; i__ <= i__1; ++i__) {
	sout[i__] = gain * exc2[contrl_1.order + i__ - 1];
    }
    return 0;
} /* bsynz_ */
Пример #6
0
int main()
{
    const int n_steps = 20000;
    const int n_burn = 4000;

    std::cout << "Students, Questions, Errors, Confidence " << std::endl;

    for (int n_students=10; n_students<11; n_students++) {
        for (int n_questions=5; n_questions<20; n_questions++) {
            s.clear();
            q.clear();
            o.clear();

            for (int i=0; i<n_students; i++) {
                Normal norm;
                norm.set_params(random_(0, 5), 0.1);
                s.push_back(norm);
            }
            for (int i=0; i<n_questions; i++) {
                Normal norm;
                norm.set_params(random_(0, 5), 0.1);
                q.push_back(norm);
            }

            for (int i=0; i<n_students; i++) {
                for (int j=0; j<n_questions; j++) {
                    Observation obs;
                    if (s[i].last() > q[j].last())
                        obs.set(i, j, 1);
                    else
                        obs.set(i, j, 0);
                    o.push_back(obs);
                }
            }

            Gibbs h;
            h.run(n_steps, n_burn);

            int error_sum = 0;
            for (int i=0; i<o.size(); i++) {
                double proficiency = s[o[i].get_sid()].mean();
                double hardness = q[o[i].get_qid()].mean();
                if (proficiency > hardness && o[i].get_response() == 0)
                    error_sum++;
                if (proficiency < hardness && o[i].get_response() == 1)
                    error_sum++;
            }

            double var = 0;
            for (int i=0; i<s.size(); i++) {
                var += (1.0/s[i].mean_variance());
            }
            for (int i=0; i<q.size(); i++) {
                var += (1.0/q[i].mean_variance());
            }
            var = sqrt(1.0/var);

            std::cout << n_students << ", ";
            std::cout << n_questions << ", ";
            std::cout << error_sum << ", ";
            std::cout << var << std::endl;

        }  // n_questions
    } // n_students

}
Пример #7
0
int Random::sampleU(int max) {
  return random_() % (max + 1);
}
Пример #8
0
int Random::sampleU(int min, int max) {
  return random_() % (max - min + 1) + min;
}
Пример #9
0
double Random::sampleU(double min, double max) {
  return (double)random_()/random_.max() * (max - min) + min;
}
Пример #10
0
double Random::sampleU(double max) {
  return (double)random_()/random_.max() * max;
}
Пример #11
0
 unsigned int operator()(unsigned int N) {
   return static_cast<unsigned int>(N * random_());
 }
Пример #12
0
	void GetArray( boost::array<T, N>& array ) {
		for( size_t i = 0; i < N; ++i ) {
			array[i] = static_cast<T>( random_() );
		}
	}
Пример #13
0
	typename Distribution::result_type operator()() { return random_(); }