1 | /*************************************************************************
|
---|
2 | Cephes Math Library Release 2.8: June, 2000
|
---|
3 | Copyright by Stephen L. Moshier
|
---|
4 |
|
---|
5 | Contributors:
|
---|
6 | * Sergey Bochkanov (ALGLIB project). Translation from C to
|
---|
7 | pseudocode.
|
---|
8 |
|
---|
9 | See subroutines comments for additional copyrights.
|
---|
10 |
|
---|
11 | >>> SOURCE LICENSE >>>
|
---|
12 | This program is free software; you can redistribute it and/or modify
|
---|
13 | it under the terms of the GNU General Public License as published by
|
---|
14 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
15 | License, or (at your option) any later version.
|
---|
16 |
|
---|
17 | This program is distributed in the hope that it will be useful,
|
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | GNU General Public License for more details.
|
---|
21 |
|
---|
22 | A copy of the GNU General Public License is available at
|
---|
23 | http://www.fsf.org/licensing/licenses
|
---|
24 |
|
---|
25 | >>> END OF LICENSE >>>
|
---|
26 | *************************************************************************/
|
---|
27 |
|
---|
28 | using System;
|
---|
29 |
|
---|
30 | namespace alglib
|
---|
31 | {
|
---|
32 | public class betaf
|
---|
33 | {
|
---|
34 | /*************************************************************************
|
---|
35 | Beta function
|
---|
36 |
|
---|
37 |
|
---|
38 | - -
|
---|
39 | | (a) | (b)
|
---|
40 | beta( a, b ) = -----------.
|
---|
41 | -
|
---|
42 | | (a+b)
|
---|
43 |
|
---|
44 | For large arguments the logarithm of the function is
|
---|
45 | evaluated using lgam(), then exponentiated.
|
---|
46 |
|
---|
47 | ACCURACY:
|
---|
48 |
|
---|
49 | Relative error:
|
---|
50 | arithmetic domain # trials peak rms
|
---|
51 | IEEE 0,30 30000 8.1e-14 1.1e-14
|
---|
52 |
|
---|
53 | Cephes Math Library Release 2.0: April, 1987
|
---|
54 | Copyright 1984, 1987 by Stephen L. Moshier
|
---|
55 | *************************************************************************/
|
---|
56 | public static double beta(double a,
|
---|
57 | double b)
|
---|
58 | {
|
---|
59 | double result = 0;
|
---|
60 | double y = 0;
|
---|
61 | double sg = 0;
|
---|
62 | double s = 0;
|
---|
63 |
|
---|
64 | sg = 1;
|
---|
65 | System.Diagnostics.Debug.Assert((double)(a)>(double)(0) | (double)(a)!=(double)((int)Math.Floor(a)), "Overflow in Beta");
|
---|
66 | System.Diagnostics.Debug.Assert((double)(b)>(double)(0) | (double)(b)!=(double)((int)Math.Floor(b)), "Overflow in Beta");
|
---|
67 | y = a+b;
|
---|
68 | if( (double)(Math.Abs(y))>(double)(171.624376956302725) )
|
---|
69 | {
|
---|
70 | y = gammafunc.lngamma(y, ref s);
|
---|
71 | sg = sg*s;
|
---|
72 | y = gammafunc.lngamma(b, ref s)-y;
|
---|
73 | sg = sg*s;
|
---|
74 | y = gammafunc.lngamma(a, ref s)+y;
|
---|
75 | sg = sg*s;
|
---|
76 | System.Diagnostics.Debug.Assert((double)(y)<=(double)(Math.Log(AP.Math.MaxRealNumber)), "Overflow in Beta");
|
---|
77 | result = sg*Math.Exp(y);
|
---|
78 | return result;
|
---|
79 | }
|
---|
80 | y = gammafunc.gamma(y);
|
---|
81 | System.Diagnostics.Debug.Assert((double)(y)!=(double)(0), "Overflow in Beta");
|
---|
82 | if( (double)(a)>(double)(b) )
|
---|
83 | {
|
---|
84 | y = gammafunc.gamma(a)/y;
|
---|
85 | y = y*gammafunc.gamma(b);
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | y = gammafunc.gamma(b)/y;
|
---|
90 | y = y*gammafunc.gamma(a);
|
---|
91 | }
|
---|
92 | result = y;
|
---|
93 | return result;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|