Home | History | Annotate | Download | only in rand.dist.samp.plinear
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <random>
     11 
     12 // template<class RealType = double>
     13 // class piecewise_linear_distribution
     14 
     15 // template<class UnaryOperation>
     16 //     piecewise_linear_distribution(size_t nw, result_type xmin,
     17 //                                     result_type xmax, UnaryOperation fw);
     18 
     19 #include <iostream>
     20 
     21 #include <random>
     22 #include <cassert>
     23 
     24 double fw(double x)
     25 {
     26     return 2*x;
     27 }
     28 
     29 int main()
     30 {
     31     {
     32         typedef std::piecewise_linear_distribution<> D;
     33         D d(0, 0, 1, fw);
     34         std::vector<double> iv = d.intervals();
     35         assert(iv.size() == 2);
     36         assert(iv[0] == 0);
     37         assert(iv[1] == 1);
     38         std::vector<double> dn = d.densities();
     39         assert(dn.size() == 2);
     40         assert(dn[0] == 0);
     41         assert(dn[1] == 2);
     42     }
     43     {
     44         typedef std::piecewise_linear_distribution<> D;
     45         D d(1, 10, 12, fw);
     46         std::vector<double> iv = d.intervals();
     47         assert(iv.size() == 2);
     48         assert(iv[0] == 10);
     49         assert(iv[1] == 12);
     50         std::vector<double> dn = d.densities();
     51         assert(dn.size() == 2);
     52         assert(dn[0] == 20./44);
     53         assert(dn[1] == 24./44);
     54     }
     55     {
     56         typedef std::piecewise_linear_distribution<> D;
     57         D d(2, 6, 14, fw);
     58         std::vector<double> iv = d.intervals();
     59         assert(iv.size() == 3);
     60         assert(iv[0] == 6);
     61         assert(iv[1] == 10);
     62         assert(iv[2] == 14);
     63         std::vector<double> dn = d.densities();
     64         assert(dn.size() == 3);
     65         assert(dn[0] == 0.075);
     66         assert(dn[1] == 0.125);
     67         assert(dn[2] == 0.175);
     68     }
     69 }
     70