1 // Copyright John Maddock 2014.
2 
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 
8 // Caution: this file contains Quickbook markup as well as code
9 // and comments, don't change any of the special comment markups!
10 
11 #ifdef _MSC_VER
12 #  pragma warning (disable : 4996) // disable -D_SCL_SECURE_NO_WARNINGS C++ 'Checked Iterators'
13 #endif
14 
15 #include <boost/math/distributions/hyperexponential.hpp>
16 #include <iostream>
17 
18 #ifndef BOOST_NO_CXX11_HDR_ARRAY
19 #include <array>
20 #endif
21 
main()22 int main()
23 {
24    {
25 //[hyperexponential_snip1
26 //=#include <boost/math/distributions/hyperexponential.hpp>
27 //=#include <iostream>
28 //=int main()
29 //={
30    const double rates[] = { 1.0 / 10.0, 1.0 / 12.0 };
31 
32    boost::math::hyperexponential he(rates);
33 
34    std::cout << "Average lifetime: "
35       << boost::math::mean(he)
36       << " years" << std::endl;
37    std::cout << "Probability that the appliance will work for more than 15 years: "
38       << boost::math::cdf(boost::math::complement(he, 15.0))
39       << std::endl;
40 //=}
41 //]
42    }
43    using namespace boost::math;
44 #ifndef BOOST_NO_CXX11_HDR_ARRAY
45    {
46    //[hyperexponential_snip2
47    std::array<double, 2> phase_prob = { 0.5, 0.5 };
48    std::array<double, 2> rates = { 1.0 / 10, 1.0 / 12 };
49 
50    hyperexponential he(phase_prob.begin(), phase_prob.end(), rates.begin(), rates.end());
51    //]
52    }
53 
54    {
55    //[hyperexponential_snip3
56    // We could be using any standard library container here... vector, deque, array, list etc:
57    std::array<double, 2> phase_prob = { 0.5, 0.5 };
58    std::array<double, 2> rates      = { 1.0 / 10, 1.0 / 12 };
59 
60    hyperexponential he1(phase_prob, rates);    // Construct from standard library container.
61 
62    double phase_probs2[] = { 0.5, 0.5 };
63    double rates2[]       = { 1.0 / 10, 1.0 / 12 };
64 
65    hyperexponential he2(phase_probs2, rates2);  // Construct from native C++ array.
66    //]
67    }
68    {
69    //[hyperexponential_snip4
70    // We could be using any standard library container here... vector, deque, array, list etc:
71    std::array<double, 2> rates = { 1.0 / 10, 1.0 / 12 };
72 
73    hyperexponential he(rates.begin(), rates.end());
74 
75    assert(he.probabilities()[0] == 0.5); // Phase probabilities will be equal and normalised to unity.
76    //]
77    }
78    {
79    //[hyperexponential_snip5
80    std::array<double, 2> rates = { 1.0 / 10, 1.0 / 12 };
81 
82    hyperexponential he(rates);
83 
84    assert(he.probabilities()[0] == 0.5); // Phase probabilities will be equal and normalised to unity.
85    //]
86    }
87 #endif
88 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !(defined(BOOST_GCC_VERSION) && (BOOST_GCC_VERSION < 40500))
89    {
90    //[hyperexponential_snip6
91    hyperexponential he = { { 0.5, 0.5 }, { 1.0 / 10, 1.0 / 12 } };
92    //]
93    }
94    {
95    //[hyperexponential_snip7
96    hyperexponential he = { 1.0 / 10, 1.0 / 12 };
97 
98    assert(he.probabilities()[0] == 0.5);
99    //]
100    }
101 #endif
102    return 0;
103 }
104