• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // REQUIRES: long_tests
11 
12 // <random>
13 
14 // template<class RealType = double>
15 // class piecewise_linear_distribution
16 
17 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
18 
19 #include <random>
20 #include <vector>
21 #include <iterator>
22 #include <numeric>
23 #include <algorithm>   // for sort
24 #include <cassert>
25 #include <limits>
26 
27 template <class T>
28 inline
29 T
sqr(T x)30 sqr(T x)
31 {
32     return x*x;
33 }
34 
35 double
f(double x,double a,double m,double b,double c)36 f(double x, double a, double m, double b, double c)
37 {
38     return a + m*(sqr(x) - sqr(b))/2 + c*(x-b);
39 }
40 
main()41 int main()
42 {
43     {
44         typedef std::piecewise_linear_distribution<> D;
45         typedef D::param_type P;
46         typedef std::mt19937_64 G;
47         G g;
48         double b[] = {10, 14, 16, 17};
49         double p[] = {25, 62.5, 12.5, 0};
50         const size_t Np = sizeof(p) / sizeof(p[0]) - 1;
51         D d;
52         P pa(b, b+Np+1, p);
53         const size_t N = 1000000;
54         std::vector<D::result_type> u;
55         for (size_t i = 0; i < N; ++i)
56         {
57             D::result_type v = d(g, pa);
58             assert(10 <= v && v < 17);
59             u.push_back(v);
60         }
61         std::sort(u.begin(), u.end());
62         int kp = -1;
63         double a = std::numeric_limits<double>::quiet_NaN();
64         double m = std::numeric_limits<double>::quiet_NaN();
65         double bk = std::numeric_limits<double>::quiet_NaN();
66         double c = std::numeric_limits<double>::quiet_NaN();
67         std::vector<double> areas(Np);
68         double S = 0;
69         for (size_t i = 0; i < areas.size(); ++i)
70         {
71             areas[i] = (p[i]+p[i+1])*(b[i+1]-b[i])/2;
72             S += areas[i];
73         }
74         for (size_t i = 0; i < areas.size(); ++i)
75             areas[i] /= S;
76         for (size_t i = 0; i < Np+1; ++i)
77             p[i] /= S;
78         for (size_t i = 0; i < N; ++i)
79         {
80             int k = std::lower_bound(b, b+Np+1, u[i]) - b - 1;
81             if (k != kp)
82             {
83                 a = 0;
84                 for (int j = 0; j < k; ++j)
85                     a += areas[j];
86                 m = (p[k+1] - p[k]) / (b[k+1] - b[k]);
87                 bk = b[k];
88                 c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]);
89                 kp = k;
90             }
91             assert(std::abs(f(u[i], a, m, bk, c) - double(i)/N) < .001);
92         }
93     }
94 }
95