//A. J. Kinderman and J. F. Monahan (1977)
 //"Computer Generation of Random Variables Using the Ratio of Uniform Deviates,"
 //ACM Transactions on Mathematical Software 3: 257-260.
 return_type
 kinderman_monahan_method() const
 {
     const final_type u = e_();
     const final_type v = ( e_() - 0.5L ) * 4.0L * u * std::sqrt( - std::log( u ) );
     return static_cast<return_type>( v / u );
 }
            return_type
            cauchy_impl( const final_type c ) const 
            {
                final_type u = e_();

                while ( final_type( 0 ) == u || final_type(1) == u )
                    { u = e_(); }

                return c * std::tan( ( u - final_type(0.5) ) * 3.1415926535897932384626433 );
            }
            return_type
            direct_impl( const return_type a, const return_type b ) const
            {
                final_type x = e_();

                while ( final_type( 0 ) == x )
                {
                    x = e_();
                }

                const final_type ans = std::pow( -b / std::log( x ), final_type( 1 ) / a );

                return ans;
            }
 return_type
 direct_impl_method() const 
 {
     const final_type u = e_();
     const final_type pi_2 = 3.1415926535897932384626433L / 2.0L;
     const final_type ans = std::log( std::tan( u * pi_2 ) );
     return ans;
 }
 return_type
 gamma_impl( const final_type A, const final_type B ) const
 {
     const final_type x = e_();
     const final_type y = gamm_type::do_generation( final_type( 1 ) / B, final_type( 1 ) );
     const final_type z = A * std::pow( y, final_type( 1 ) / B );
     return x > final_type( 0.5 ) ? z : -z;
 }
            return_type
            coin_flip_method( const final_type P ) const noexcept
            {
                if ( e_() < p_ )
                    { return 1; }

                return 0;
            }
 return_type
 direct_impl( const return_type delta ) const
 {
     const final_type u = e_();
     const final_type tmp1 = - std::log( u );
     const final_type tmp2 = std::sqrt( tmp1 );
     const final_type ans = delta * tmp2;
     return ans;
 }
 final_type
 direct_burr_impl( const size_type N, const final_type C, const final_type K, const final_type R ) const noexcept
 {
     const final_type u = e_();
     static const final_type l(1);
     static const final_type ll(2);
     
     // Note: c++0x support required here
     //                                  u           c            k           r
     // Burr, I. W. (1942). "Cumulative frequency functions". Annals of Mathematical Statistics 13 (2): 215¨C232. doi:10.1214/aoms/1177731607. JSTOR 2235756.
     const std::function< final_type( final_type, final_type, final_type, final_type ) > inverse_function[12] = 
     {
         // F1(x) = x
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { return u; },
         // F2(x) = (1+e^{-x})^{-r}
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { return - std::log( -l + std::pow(u, -l/r) ); },
         // F3(x) = (1+x^{-k})^{-r}
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { return std::pow( (std::pow( u, -l/r ) - l ), -l/k); },
         // F4(x) = (1+((c-x)/x)^(1/c))^(-r)
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { return c / ( l + std::pow(std::pow(u, -l/r) - l, c) ); },
         // F5(x) = ( 1 + k e^{-tan(x)} )^r    
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { return std::atan( - std::log( (std::pow(u, -l/r)-l ) / k ) );},
         // F6(x) = ( 1 + k e^{-sinh(x)} )^r    
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { return std::asinh( - std::log( (std::pow(u, -l/r)-l ) / k ) );},
         // F7(x) = 2^{-r}(1+tanh(x))^{r}    
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { return std::atanh( std::pow(std::pow(ll, r)*u, l/r) - l);},
         // F8(x) = \frac{2}{pi} arctan(e^x)   
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { const final_type pi_2 = 1.5707963267948966142313216L; return std::log(std::tan(pi_2*std::pow(u, l/r)));},
         // F9(x) = 1-\frac{2}{1+k((1+e^x)^r-1)}
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { return std::log(std::pow(l+(ll/(l-u)-l)/k, l/r)-l);},
         // F10(x) = (1-e^{-x^2})^r
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { return std::sqrt(-std::log(-std::pow(u,l/r)+l));},
         // F11(x) = ( x - \frac{\sin 2 \pi x}{2\pi})^r
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         {
             const final_type two_pi = final_type(2) * 3.1415926535897932384626433;
             std::function<final_type(final_type)> f = [&](final_type x) { return std::pow( x - std::sin(two_pi*x)/two_pi ,r); };
             std::function<final_type(final_type)> df =[&](final_type x) {return std::pow( x - std::sin(two_pi*x)/two_pi ,r-l) * ( l - std::cos(two_pi*x) ); };
             const final_type ans = (newton_raphson<final_type>(final_type(0), final_type(1), f, df))();
             return ans;
         },
         // F12(x) = 1 - ( 1 + x^c ) ^ {-k}
         []( const final_type u, const final_type c, const final_type k, const final_type r ) noexcept -> final_type
         { return std::pow( std::pow(l-u, -l/k)-l, l/c);}
     };
     return inverse_function[N-1]( u, C, K, R );
 }
 return_type
 gaussian_impl( const final_type c ) const
 {
     const final_type u = ( e_() - final_type(0.5) ) * 3.1415926535897932384626433;
     final_type v = exponential_type::do_generation( final_type( 1 ) );
     while ( final_type( 0 ) == v )
         { v = exponential_type::do_generation( final_type( 1 ) ); }
     const final_type ans = c * std::sin( u ) * std::sqrt( v );
     return ans + ans;
 }
            return_type
            laplace_reject_impl( const final_type A, const final_type B ) const
            {
                const final_type b = std::pow( final_type( 1 ) / B, final_type( 1 ) / B );

                for ( ;; )
                {
                    const final_type x = laplace_type::do_generation( final_type(), b );
                    final_type y = e_();

                    while ( final_type( 0 ) == y )
                        { y = e_(); }

                    const final_type z = std::fabs( x ) / b - final_type( 1 ) + final_type( 1 ) / B - std::pow( std::fabs( x ), B );

                    if ( std::log( y ) > z )
                        { return A * x; }
                }
            }
            return_type
            gaussian_reject_impl( const final_type A, const final_type B ) const
            {
                const final_type b = std::pow( final_type( 1 ) / B, final_type( 1 ) / B );

                for ( ;; )
                {
                    const final_type x = normal_type::do_generation() * b;
                    final_type y = e_();

                    while ( final_type( 0 ) == y )
                        { y = e_(); }

                    const final_type z = x * x / ( final_type( 2 ) * b * b ) + final_type( 1 ) / B - final_type( 0.5 ) - std::pow( std::fabs( x ), B );

                    if ( std::log( y ) > z )
                        { return A * x; }
                }
            }
            return_type
            rectangle_wedge_tail_method( const final_type A, const final_type Variance ) const
            {
                const final_type s = A / Variance;

                for ( ;; )
                {
                    const final_type u = e_();
                    const final_type v = e_();

                    if ( final_type( 0 ) == u || final_type( 0 ) == v )
                        { continue; }

                    const final_type x = std::sqrt( s * s - std::log( v ) );

                    if ( s >= x * u )
                        { return x * Variance; }
                }
            }
Example #13
0
            //Marsaglia and Tsang, "A Simple Method for generating gamma variables",
            //ACM Transactions on Mathematical Software, Vol 26, No 3 (2000), p363-372.
            return_type
            marsaglia_tsang_method( const final_type A ) const
            {
                const final_type a = A < final_type( 1 ) ? A + final_type( 1 ) : A;
                const final_type d = a - final_type( 1 ) / final_type( 3 );
                const final_type c = final_type( 1 ) / final_type( 3 ) / std::sqrt( d );
                final_type u( 0 );
                final_type v( 0 );
                final_type x( 0 );

                for ( ;; )
                {
                    for ( ;; )
                    {
                        x = normal_type::do_generation(); //normal distribution
                        v = final_type( 1 ) + c * x;

                        if ( v > 0 )
                            { break; }
                    }

                    const final_type xx = x * x;

                    v *= v * v;

                    u = e_();

                    if ( u < final_type( 1 ) - final_type( 0.0331 ) * xx * xx )
                        { break; }

                    if ( std::log( u ) < 0.5 * xx + d *( final_type( 1 ) - v + std::log( v ) ) )
                        { break; }
                }

                final_type ans = d * v;

                if ( A < 1 )
                    { ans *= std::pow( e_(), final_type( 1 ) / A ); }

                return ans;
            }
Example #14
0
            return_type
            large_c_rejection_impl( const value_type c ) const
            {
                for ( ;; )
                    {
                        const final_type x = final_type( 1 ) + GHgB3_type::do_generation( final_type( 1 ), final_type( 1 ), c - final_type( 1 ) );
                        const final_type u = e_();

                        if ( x * u < final_type( 1 ) )
                            { return x; }
                    }
            }
Example #15
0
void pedgelist_ (wgraph_s *g)
{
    if (*(uint16_t *)GTNEXT()->lexeme == *(uint16_t *)"E")   /*if (!strcmp(__GTNEXT()->lexeme, "E"))*/
    if (GTNEXT()->type == T_EQU)
    if (GTNEXT()->type == T_OPENBRACE)
        e_(g);
    pedgeparam_(g);
    if (stream_->type == T_CLOSEBRACE)
        return;
    printf ("Syntax Error while parsing in edge list (edge #%d): %s\n", g->nedges, stream_->lexeme);
    exit(EXIT_FAILURE);
}
            //G. E. P. Box and Mervin E. Muller,
            //A Note on the Generation of Random Normal Deviates,
            //The Annals of Mathematical Statistics (1958), Vol. 29, No. 2 pp. 610¨C611
            return_type
            box_muller_method() const
            {
                final_type u1(0), u2(0);
                final_type v1(0), v2(0);
                final_type s(2);

                while ( s > final_type( 1 ) )
                {
                    u1 = e_();
                    u2 = e_();
                    v1 = u1 + u1 - final_type( 1 );
                    v2 = u2 + u2 - final_type( 1 );
                    s = v1 * v1 + v2 * v2;
                }

                const final_type tmp = -std::log( s );

                const final_type ans = v1 * std::sqrt(( tmp + tmp ) / s );

                return static_cast<return_type>( ans );
            }
            return_type
            direct_invertion_method( const return_type mu, const return_type b ) const
            {
                for ( ;; )
                {
                    const final_type u = e_() - final_type( 0.5 );

                    if ( final_type( 0 ) == u )
                        { continue; }

                    const final_type ans =  u > final_type( 0 ) ?
                                            mu - b * std::log( 1 - u - u ) :
                                            mu + b * std::log( 1 + u + u );

                    return ans;
                }
            }
            return_type
            direct_impl( const final_type c, const final_type alpha ) const 
            {
                const final_type u = ( e_() - final_type(0.5) ) * 3.1415926535897932384626433;
                final_type v = exponential_type::do_generation( final_type( 1 ) );

                while ( final_type( 0 ) == v )
                    { v = exponential_type::do_generation( final_type( 1 ) ); }

                const final_type r_alpha = final_type( 1 ) / alpha;

                const final_type u_alpha = u * alpha;

                const final_type ans = c * std::pow( std::cos( u - u_alpha ) / v, r_alpha - final_type( 1 ) )  * std::sin( u_alpha ) * std::pow( std::cos( u ), -r_alpha );

                return ans;
            }
Example #19
0
Function::Evaluators &Function::Evaluators::operator=(const Evaluators &e)
{
  Function::Evaluators e_(e);
  
  FunctionEvaluator *oldF = f;
  f  = e_.f;
  GradientEvaluator *oldG = g;
  g = e_.g;
  HessianEvaluator *oldH = H;
  H = e_.H;
  
  e_.f = oldF;
  e_.g = oldG;
  e_.H = oldH;
  
  return *this;
}
Example #20
0
            return_type
            second_waiting_time_method( const size_type N, const final_type P )
            {
                const final_type p  = P > 0.5 ? 1 - P : P;
                const final_type q  = -std::log( 1.0 - p );
                return_type ans     = 0;
                final_type sum      = 0;

                for ( ;; )
                {
                    const final_type e = -std::log( e_() );
                    sum += e / ( N - ans );

                    if ( sum > q )
                        { break; }

                    ++ans;
                }

                return p_ > 0.5 ? N - ans : ans;
            }//second_waiting_time_method
Example #21
0
 void enable( bool f )
 {
         if( f != enabled_ ) { enabled_ = f ; e_( f ) ; }
 }
Example #22
0
void pedgeparam_ (wgraph_s *g)
{
    while (GTNEXT()->type == T_COMMA)
        e_(g);
}
Example #23
0
 int operator()() {
     return (e_() >> shift_) + off_;
 }
Example #24
0
 int operator()() {
     return e_() / div_ + off_;
 }
 virtual double error() const {return e_(p_);}