| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// REQUIRES: long_tests |
| 10 |
|
| 11 |
// <random> |
| 12 |
|
| 13 |
// template<class RealType = double> |
| 14 |
// class cauchy_distribution |
| 15 |
|
| 16 |
// template<class _URNG> result_type operator()(_URNG& g, const param_type& parm); |
| 17 |
|
| 18 |
#include <random> |
| 19 |
|
| 20 |
#include <cassert> |
| 21 |
#include <cmath> |
| 22 |
#include <vector> |
| 23 |
#include <algorithm> |
| 24 |
|
| 25 |
#include "test_macros.h" |
| 26 |
|
| 27 |
double |
| 28 |
f(double x, double a, double b) |
| 29 |
{ |
| 30 |
return 1/3.1415926535897932 * std::atan((x - a)/b) + .5; |
| 31 |
} |
| 32 |
|
| 33 |
int main(int, char**) |
| 34 |
{ |
| 35 |
{ |
| 36 |
typedef std::cauchy_distribution<> D; |
| 37 |
typedef D::param_type P; |
| 38 |
typedef std::mt19937 G; |
| 39 |
G g; |
| 40 |
const double a = 10; |
| 41 |
const double b = .5; |
| 42 |
D d; |
| 43 |
P p(a, b); |
| 44 |
const int N = 1000000; |
| 45 |
std::vector<D::result_type> u; |
| 46 |
for (int i = 0; i < N; ++i) |
| 47 |
u.push_back(d(g, p)); |
| 48 |
std::sort(u.begin(), u.end()); |
| 49 |
for (int i = 0; i < N; ++i) |
| 50 |
assert(std::abs(f(u[i], a, b) - double(i)/N) < .001); |
| 51 |
} |
| 52 |
{ |
| 53 |
typedef std::cauchy_distribution<> D; |
| 54 |
typedef D::param_type P; |
| 55 |
typedef std::mt19937 G; |
| 56 |
G g; |
| 57 |
const double a = -1.5; |
| 58 |
const double b = 1; |
| 59 |
D d; |
| 60 |
P p(a, b); |
| 61 |
const int N = 1000000; |
| 62 |
std::vector<D::result_type> u; |
| 63 |
for (int i = 0; i < N; ++i) |
| 64 |
u.push_back(d(g, p)); |
| 65 |
std::sort(u.begin(), u.end()); |
| 66 |
for (int i = 0; i < N; ++i) |
| 67 |
assert(std::abs(f(u[i], a, b) - double(i)/N) < .001); |
| 68 |
} |
| 69 |
{ |
| 70 |
typedef std::cauchy_distribution<> D; |
| 71 |
typedef D::param_type P; |
| 72 |
typedef std::mt19937 G; |
| 73 |
G g; |
| 74 |
const double a = .5; |
| 75 |
const double b = 2; |
| 76 |
D d; |
| 77 |
P p(a, b); |
| 78 |
const int N = 1000000; |
| 79 |
std::vector<D::result_type> u; |
| 80 |
for (int i = 0; i < N; ++i) |
| 81 |
u.push_back(d(g, p)); |
| 82 |
std::sort(u.begin(), u.end()); |
| 83 |
for (int i = 0; i < N; ++i) |
| 84 |
assert(std::abs(f(u[i], a, b) - double(i)/N) < .001); |
| 85 |
} |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |