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 InputIterator>
     16 //     piecewise_linear_distribution(InputIteratorB firstB,
     17 //                                     InputIteratorB lastB,
     18 //                                     InputIteratorW firstW);
     19 
     20 #include <random>
     21 #include <cassert>
     22 
     23 int main()
     24 {
     25     {
     26         typedef std::piecewise_linear_distribution<> D;
     27         double b[] = {10};
     28         double p[] = {12};
     29         D d(b, b, p);
     30         std::vector<double> iv = d.intervals();
     31         assert(iv.size() == 2);
     32         assert(iv[0] == 0);
     33         assert(iv[1] == 1);
     34         std::vector<double> dn = d.densities();
     35         assert(dn.size() == 2);
     36         assert(dn[0] == 1);
     37         assert(dn[1] == 1);
     38     }
     39     {
     40         typedef std::piecewise_linear_distribution<> D;
     41         double b[] = {10};
     42         double p[] = {12};
     43         D d(b, b+1, p);
     44         std::vector<double> iv = d.intervals();
     45         assert(iv.size() == 2);
     46         assert(iv[0] == 0);
     47         assert(iv[1] == 1);
     48         std::vector<double> dn = d.densities();
     49         assert(dn.size() == 2);
     50         assert(dn[0] == 1);
     51         assert(dn[1] == 1);
     52     }
     53     {
     54         typedef std::piecewise_linear_distribution<> D;
     55         double b[] = {10, 15};
     56         double p[] = {20, 20};
     57         D d(b, b+2, p);
     58         std::vector<double> iv = d.intervals();
     59         assert(iv.size() == 2);
     60         assert(iv[0] == 10);
     61         assert(iv[1] == 15);
     62         std::vector<double> dn = d.densities();
     63         assert(dn.size() == 2);
     64         assert(dn[0] == 1/5.);
     65         assert(dn[1] == 1/5.);
     66     }
     67     {
     68         typedef std::piecewise_linear_distribution<> D;
     69         double b[] = {10, 15, 16};
     70         double p[] = {.25, .75, .25};
     71         D d(b, b+3, p);
     72         std::vector<double> iv = d.intervals();
     73         assert(iv.size() == 3);
     74         assert(iv[0] == 10);
     75         assert(iv[1] == 15);
     76         assert(iv[2] == 16);
     77         std::vector<double> dn = d.densities();
     78         assert(dn.size() == 3);
     79         assert(dn[0] == .25/3);
     80         assert(dn[1] == .75/3);
     81         assert(dn[2] == .25/3);
     82     }
     83     {
     84         typedef std::piecewise_linear_distribution<> D;
     85         double b[] = {10, 14, 16, 17};
     86         double p[] = {0, 1, 1, 0};
     87         D d(b, b+4, p);
     88         std::vector<double> iv = d.intervals();
     89         assert(iv.size() == 4);
     90         assert(iv[0] == 10);
     91         assert(iv[1] == 14);
     92         assert(iv[2] == 16);
     93         assert(iv[3] == 17);
     94         std::vector<double> dn = d.densities();
     95         assert(dn.size() == 4);
     96         assert(dn[0] == 0);
     97         assert(dn[1] == 1/4.5);
     98         assert(dn[2] == 1/4.5);
     99         assert(dn[3] == 0);
    100     }
    101 }
    102