]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [section:beta_function Beta] |
2 | ||
3 | [h4 Synopsis] | |
4 | ||
5 | `` | |
6 | #include <boost/math/special_functions/beta.hpp> | |
7 | `` | |
8 | ||
9 | namespace boost{ namespace math{ | |
10 | ||
11 | template <class T1, class T2> | |
12 | ``__sf_result`` beta(T1 a, T2 b); | |
13 | ||
14 | template <class T1, class T2, class ``__Policy``> | |
15 | ``__sf_result`` beta(T1 a, T2 b, const ``__Policy``&); | |
16 | ||
17 | }} // namespaces | |
18 | ||
19 | [h4 Description] | |
20 | ||
21 | The beta function is defined by: | |
22 | ||
23 | [equation beta1] | |
24 | ||
25 | [graph beta] | |
26 | ||
27 | [optional_policy] | |
28 | ||
29 | There are effectively two versions of this function internally: a fully | |
30 | generic version that is slow, but reasonably accurate, and a much more | |
31 | efficient approximation that is used where the number of digits in the significand | |
32 | of T correspond to a certain __lanczos. In practice any built-in | |
33 | floating-point type you will encounter has an appropriate __lanczos | |
34 | defined for it. It is also possible, given enough machine time, to generate | |
35 | further __lanczos's using the program libs/math/tools/lanczos_generator.cpp. | |
36 | ||
37 | The return type of these functions is computed using the __arg_promotion_rules | |
38 | when T1 and T2 are different types. | |
39 | ||
40 | [h4 Accuracy] | |
41 | ||
42 | The following table shows peak errors for various domains of input arguments, | |
43 | along with comparisons to the __gsl and __cephes libraries. Note that | |
44 | only results for the widest floating point type on the system are given as | |
45 | narrower types have __zero_error. | |
46 | ||
47 | [table_beta] | |
48 | ||
49 | Note that the worst errors occur when a or b are large, and that | |
50 | when this is the case the result is very close to zero, so absolute | |
51 | errors will be very small. | |
52 | ||
53 | [h4 Testing] | |
54 | ||
55 | A mixture of spot tests of exact values, and randomly generated test data are | |
56 | used: the test data was computed using | |
57 | [@http://shoup.net/ntl/doc/RR.txt NTL::RR] at 1000-bit precision. | |
58 | ||
59 | [h4 Implementation] | |
60 | ||
61 | Traditional methods of evaluating the beta function either involve evaluating | |
62 | the gamma functions directly, or taking logarithms and then | |
63 | exponentiating the result. However, the former is prone to overflows | |
64 | for even very modest arguments, while the latter is prone to cancellation | |
65 | errors. As an alternative, if we regard the gamma function as a white-box | |
66 | containing the __lanczos, then we can combine the power terms: | |
67 | ||
68 | [equation beta2] | |
69 | ||
70 | which is almost the ideal solution, however almost all of the error occurs | |
71 | in evaluating the power terms when /a/ or /b/ are large. If we assume that /a > b/ | |
72 | then the larger of the two power terms can be reduced by a factor of /b/, which | |
73 | immediately cuts the maximum error in half: | |
74 | ||
75 | [equation beta3] | |
76 | ||
77 | This may not be the final solution, but it is very competitive compared to | |
78 | other implementation methods. | |
79 | ||
80 | The generic implementation - where no __lanczos approximation is available - is | |
81 | implemented in a very similar way to the generic version of the gamma function. | |
82 | Again in order to avoid numerical overflow the power terms that prefix the series and | |
83 | continued fraction parts are collected together into: | |
84 | ||
85 | [equation beta8] | |
86 | ||
87 | where la, lb and lc are the integration limits used for a, b, and a+b. | |
88 | ||
89 | There are a few special cases worth mentioning: | |
90 | ||
91 | When /a/ or /b/ are less than one, we can use the recurrence relations: | |
92 | ||
93 | [equation beta4] | |
94 | ||
95 | [equation beta5] | |
96 | ||
97 | to move to a more favorable region where they are both greater than 1. | |
98 | ||
99 | In addition: | |
100 | ||
101 | [equation beta7] | |
102 | ||
103 | [endsect][/section:beta_function The Beta Function] | |
104 | [/ | |
105 | Copyright 2006 John Maddock and Paul A. Bristow. | |
106 | Distributed under the Boost Software License, Version 1.0. | |
107 | (See accompanying file LICENSE_1_0.txt or copy at | |
108 | http://www.boost.org/LICENSE_1_0.txt). | |
109 | ] | |
110 |