alglib 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/Manifest.txt +253 -0
- data/README.txt +33 -0
- data/Rakefile +27 -0
- data/ext/Rakefile +24 -0
- data/ext/alglib.i +24 -0
- data/ext/alglib/Makefile +157 -0
- data/ext/alglib/airyf.cpp +372 -0
- data/ext/alglib/airyf.h +81 -0
- data/ext/alglib/alglib.cpp +8558 -0
- data/ext/alglib/alglib_util.cpp +19 -0
- data/ext/alglib/alglib_util.h +14 -0
- data/ext/alglib/ap.cpp +877 -0
- data/ext/alglib/ap.english.html +364 -0
- data/ext/alglib/ap.h +666 -0
- data/ext/alglib/ap.russian.html +442 -0
- data/ext/alglib/apvt.h +754 -0
- data/ext/alglib/bdss.cpp +1500 -0
- data/ext/alglib/bdss.h +251 -0
- data/ext/alglib/bdsvd.cpp +1339 -0
- data/ext/alglib/bdsvd.h +164 -0
- data/ext/alglib/bessel.cpp +1226 -0
- data/ext/alglib/bessel.h +331 -0
- data/ext/alglib/betaf.cpp +105 -0
- data/ext/alglib/betaf.h +74 -0
- data/ext/alglib/bidiagonal.cpp +1328 -0
- data/ext/alglib/bidiagonal.h +350 -0
- data/ext/alglib/binomialdistr.cpp +247 -0
- data/ext/alglib/binomialdistr.h +153 -0
- data/ext/alglib/blas.cpp +576 -0
- data/ext/alglib/blas.h +132 -0
- data/ext/alglib/cblas.cpp +226 -0
- data/ext/alglib/cblas.h +57 -0
- data/ext/alglib/cdet.cpp +138 -0
- data/ext/alglib/cdet.h +92 -0
- data/ext/alglib/chebyshev.cpp +216 -0
- data/ext/alglib/chebyshev.h +76 -0
- data/ext/alglib/chisquaredistr.cpp +157 -0
- data/ext/alglib/chisquaredistr.h +144 -0
- data/ext/alglib/cholesky.cpp +285 -0
- data/ext/alglib/cholesky.h +86 -0
- data/ext/alglib/cinverse.cpp +298 -0
- data/ext/alglib/cinverse.h +111 -0
- data/ext/alglib/clu.cpp +337 -0
- data/ext/alglib/clu.h +120 -0
- data/ext/alglib/correlation.cpp +280 -0
- data/ext/alglib/correlation.h +77 -0
- data/ext/alglib/correlationtests.cpp +726 -0
- data/ext/alglib/correlationtests.h +134 -0
- data/ext/alglib/crcond.cpp +826 -0
- data/ext/alglib/crcond.h +148 -0
- data/ext/alglib/creflections.cpp +310 -0
- data/ext/alglib/creflections.h +165 -0
- data/ext/alglib/csolve.cpp +312 -0
- data/ext/alglib/csolve.h +99 -0
- data/ext/alglib/ctrinverse.cpp +387 -0
- data/ext/alglib/ctrinverse.h +98 -0
- data/ext/alglib/ctrlinsolve.cpp +297 -0
- data/ext/alglib/ctrlinsolve.h +81 -0
- data/ext/alglib/dawson.cpp +234 -0
- data/ext/alglib/dawson.h +74 -0
- data/ext/alglib/descriptivestatistics.cpp +436 -0
- data/ext/alglib/descriptivestatistics.h +112 -0
- data/ext/alglib/det.cpp +140 -0
- data/ext/alglib/det.h +94 -0
- data/ext/alglib/dforest.cpp +1819 -0
- data/ext/alglib/dforest.h +316 -0
- data/ext/alglib/elliptic.cpp +497 -0
- data/ext/alglib/elliptic.h +217 -0
- data/ext/alglib/estnorm.cpp +429 -0
- data/ext/alglib/estnorm.h +107 -0
- data/ext/alglib/expintegrals.cpp +422 -0
- data/ext/alglib/expintegrals.h +108 -0
- data/ext/alglib/faq.english.html +258 -0
- data/ext/alglib/faq.russian.html +272 -0
- data/ext/alglib/fdistr.cpp +202 -0
- data/ext/alglib/fdistr.h +163 -0
- data/ext/alglib/fresnel.cpp +211 -0
- data/ext/alglib/fresnel.h +91 -0
- data/ext/alglib/gammaf.cpp +338 -0
- data/ext/alglib/gammaf.h +104 -0
- data/ext/alglib/gqgengauss.cpp +235 -0
- data/ext/alglib/gqgengauss.h +92 -0
- data/ext/alglib/gqgenhermite.cpp +268 -0
- data/ext/alglib/gqgenhermite.h +63 -0
- data/ext/alglib/gqgenjacobi.cpp +297 -0
- data/ext/alglib/gqgenjacobi.h +72 -0
- data/ext/alglib/gqgenlaguerre.cpp +265 -0
- data/ext/alglib/gqgenlaguerre.h +69 -0
- data/ext/alglib/gqgenlegendre.cpp +300 -0
- data/ext/alglib/gqgenlegendre.h +62 -0
- data/ext/alglib/gqgenlobatto.cpp +305 -0
- data/ext/alglib/gqgenlobatto.h +97 -0
- data/ext/alglib/gqgenradau.cpp +232 -0
- data/ext/alglib/gqgenradau.h +95 -0
- data/ext/alglib/hbisinv.cpp +480 -0
- data/ext/alglib/hbisinv.h +183 -0
- data/ext/alglib/hblas.cpp +228 -0
- data/ext/alglib/hblas.h +64 -0
- data/ext/alglib/hcholesky.cpp +339 -0
- data/ext/alglib/hcholesky.h +91 -0
- data/ext/alglib/hermite.cpp +114 -0
- data/ext/alglib/hermite.h +49 -0
- data/ext/alglib/hessenberg.cpp +370 -0
- data/ext/alglib/hessenberg.h +152 -0
- data/ext/alglib/hevd.cpp +247 -0
- data/ext/alglib/hevd.h +107 -0
- data/ext/alglib/hsschur.cpp +1316 -0
- data/ext/alglib/hsschur.h +108 -0
- data/ext/alglib/htridiagonal.cpp +734 -0
- data/ext/alglib/htridiagonal.h +180 -0
- data/ext/alglib/ialglib.cpp +6 -0
- data/ext/alglib/ialglib.h +9 -0
- data/ext/alglib/ibetaf.cpp +960 -0
- data/ext/alglib/ibetaf.h +125 -0
- data/ext/alglib/igammaf.cpp +430 -0
- data/ext/alglib/igammaf.h +157 -0
- data/ext/alglib/inv.cpp +274 -0
- data/ext/alglib/inv.h +115 -0
- data/ext/alglib/inverseupdate.cpp +480 -0
- data/ext/alglib/inverseupdate.h +185 -0
- data/ext/alglib/jacobianelliptic.cpp +164 -0
- data/ext/alglib/jacobianelliptic.h +94 -0
- data/ext/alglib/jarquebera.cpp +2271 -0
- data/ext/alglib/jarquebera.h +80 -0
- data/ext/alglib/kmeans.cpp +356 -0
- data/ext/alglib/kmeans.h +76 -0
- data/ext/alglib/laguerre.cpp +94 -0
- data/ext/alglib/laguerre.h +48 -0
- data/ext/alglib/lbfgs.cpp +1167 -0
- data/ext/alglib/lbfgs.h +218 -0
- data/ext/alglib/lda.cpp +434 -0
- data/ext/alglib/lda.h +133 -0
- data/ext/alglib/ldlt.cpp +1130 -0
- data/ext/alglib/ldlt.h +124 -0
- data/ext/alglib/leastsquares.cpp +1252 -0
- data/ext/alglib/leastsquares.h +290 -0
- data/ext/alglib/legendre.cpp +107 -0
- data/ext/alglib/legendre.h +49 -0
- data/ext/alglib/linreg.cpp +1185 -0
- data/ext/alglib/linreg.h +380 -0
- data/ext/alglib/logit.cpp +1523 -0
- data/ext/alglib/logit.h +333 -0
- data/ext/alglib/lq.cpp +399 -0
- data/ext/alglib/lq.h +160 -0
- data/ext/alglib/lu.cpp +462 -0
- data/ext/alglib/lu.h +119 -0
- data/ext/alglib/mannwhitneyu.cpp +4490 -0
- data/ext/alglib/mannwhitneyu.h +115 -0
- data/ext/alglib/minlm.cpp +918 -0
- data/ext/alglib/minlm.h +312 -0
- data/ext/alglib/mlpbase.cpp +3375 -0
- data/ext/alglib/mlpbase.h +589 -0
- data/ext/alglib/mlpe.cpp +1369 -0
- data/ext/alglib/mlpe.h +552 -0
- data/ext/alglib/mlptrain.cpp +1056 -0
- data/ext/alglib/mlptrain.h +283 -0
- data/ext/alglib/nearunityunit.cpp +91 -0
- data/ext/alglib/nearunityunit.h +17 -0
- data/ext/alglib/normaldistr.cpp +377 -0
- data/ext/alglib/normaldistr.h +175 -0
- data/ext/alglib/nsevd.cpp +1869 -0
- data/ext/alglib/nsevd.h +140 -0
- data/ext/alglib/pca.cpp +168 -0
- data/ext/alglib/pca.h +87 -0
- data/ext/alglib/poissondistr.cpp +143 -0
- data/ext/alglib/poissondistr.h +130 -0
- data/ext/alglib/polinterpolation.cpp +685 -0
- data/ext/alglib/polinterpolation.h +206 -0
- data/ext/alglib/psif.cpp +173 -0
- data/ext/alglib/psif.h +88 -0
- data/ext/alglib/qr.cpp +414 -0
- data/ext/alglib/qr.h +168 -0
- data/ext/alglib/ratinterpolation.cpp +134 -0
- data/ext/alglib/ratinterpolation.h +72 -0
- data/ext/alglib/rcond.cpp +705 -0
- data/ext/alglib/rcond.h +140 -0
- data/ext/alglib/reflections.cpp +504 -0
- data/ext/alglib/reflections.h +165 -0
- data/ext/alglib/rotations.cpp +473 -0
- data/ext/alglib/rotations.h +128 -0
- data/ext/alglib/rsolve.cpp +221 -0
- data/ext/alglib/rsolve.h +99 -0
- data/ext/alglib/sbisinv.cpp +217 -0
- data/ext/alglib/sbisinv.h +171 -0
- data/ext/alglib/sblas.cpp +185 -0
- data/ext/alglib/sblas.h +64 -0
- data/ext/alglib/schur.cpp +156 -0
- data/ext/alglib/schur.h +102 -0
- data/ext/alglib/sdet.cpp +193 -0
- data/ext/alglib/sdet.h +101 -0
- data/ext/alglib/sevd.cpp +116 -0
- data/ext/alglib/sevd.h +99 -0
- data/ext/alglib/sinverse.cpp +672 -0
- data/ext/alglib/sinverse.h +138 -0
- data/ext/alglib/spddet.cpp +138 -0
- data/ext/alglib/spddet.h +96 -0
- data/ext/alglib/spdgevd.cpp +842 -0
- data/ext/alglib/spdgevd.h +200 -0
- data/ext/alglib/spdinverse.cpp +509 -0
- data/ext/alglib/spdinverse.h +122 -0
- data/ext/alglib/spdrcond.cpp +421 -0
- data/ext/alglib/spdrcond.h +118 -0
- data/ext/alglib/spdsolve.cpp +275 -0
- data/ext/alglib/spdsolve.h +105 -0
- data/ext/alglib/spline2d.cpp +1192 -0
- data/ext/alglib/spline2d.h +301 -0
- data/ext/alglib/spline3.cpp +1264 -0
- data/ext/alglib/spline3.h +290 -0
- data/ext/alglib/srcond.cpp +595 -0
- data/ext/alglib/srcond.h +127 -0
- data/ext/alglib/ssolve.cpp +895 -0
- data/ext/alglib/ssolve.h +139 -0
- data/ext/alglib/stdafx.h +0 -0
- data/ext/alglib/stest.cpp +131 -0
- data/ext/alglib/stest.h +94 -0
- data/ext/alglib/studenttdistr.cpp +222 -0
- data/ext/alglib/studenttdistr.h +115 -0
- data/ext/alglib/studentttests.cpp +377 -0
- data/ext/alglib/studentttests.h +178 -0
- data/ext/alglib/svd.cpp +620 -0
- data/ext/alglib/svd.h +126 -0
- data/ext/alglib/tdbisinv.cpp +2608 -0
- data/ext/alglib/tdbisinv.h +228 -0
- data/ext/alglib/tdevd.cpp +1229 -0
- data/ext/alglib/tdevd.h +115 -0
- data/ext/alglib/tridiagonal.cpp +594 -0
- data/ext/alglib/tridiagonal.h +171 -0
- data/ext/alglib/trigintegrals.cpp +490 -0
- data/ext/alglib/trigintegrals.h +131 -0
- data/ext/alglib/trinverse.cpp +345 -0
- data/ext/alglib/trinverse.h +98 -0
- data/ext/alglib/trlinsolve.cpp +926 -0
- data/ext/alglib/trlinsolve.h +73 -0
- data/ext/alglib/tsort.cpp +405 -0
- data/ext/alglib/tsort.h +54 -0
- data/ext/alglib/variancetests.cpp +245 -0
- data/ext/alglib/variancetests.h +134 -0
- data/ext/alglib/wsr.cpp +6285 -0
- data/ext/alglib/wsr.h +96 -0
- data/ext/ap.i +97 -0
- data/ext/correlation.i +24 -0
- data/ext/extconf.rb +6 -0
- data/ext/logit.i +89 -0
- data/lib/alglib.rb +71 -0
- data/lib/alglib/correlation.rb +26 -0
- data/lib/alglib/linearregression.rb +63 -0
- data/lib/alglib/logit.rb +42 -0
- data/test/test_alglib.rb +52 -0
- data/test/test_correlation.rb +44 -0
- data/test/test_correlationtest.rb +45 -0
- data/test/test_linreg.rb +35 -0
- data/test/test_logit.rb +43 -0
- data/test/test_pca.rb +27 -0
- metadata +326 -0
@@ -0,0 +1,180 @@
|
|
1
|
+
/*************************************************************************
|
2
|
+
Copyright (c) 1992-2007 The University of Tennessee. All rights reserved.
|
3
|
+
|
4
|
+
Contributors:
|
5
|
+
* Sergey Bochkanov (ALGLIB project). Translation from FORTRAN to
|
6
|
+
pseudocode.
|
7
|
+
|
8
|
+
See subroutines comments for additional copyrights.
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
11
|
+
modification, are permitted provided that the following conditions are
|
12
|
+
met:
|
13
|
+
|
14
|
+
- Redistributions of source code must retain the above copyright
|
15
|
+
notice, this list of conditions and the following disclaimer.
|
16
|
+
|
17
|
+
- Redistributions in binary form must reproduce the above copyright
|
18
|
+
notice, this list of conditions and the following disclaimer listed
|
19
|
+
in this license in the documentation and/or other materials
|
20
|
+
provided with the distribution.
|
21
|
+
|
22
|
+
- Neither the name of the copyright holders nor the names of its
|
23
|
+
contributors may be used to endorse or promote products derived from
|
24
|
+
this software without specific prior written permission.
|
25
|
+
|
26
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
27
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
28
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
29
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
30
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
31
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
32
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
33
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
34
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
35
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
36
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
37
|
+
*************************************************************************/
|
38
|
+
|
39
|
+
#ifndef _htridiagonal_h
|
40
|
+
#define _htridiagonal_h
|
41
|
+
|
42
|
+
#include "ap.h"
|
43
|
+
#include "ialglib.h"
|
44
|
+
|
45
|
+
#include "cblas.h"
|
46
|
+
#include "creflections.h"
|
47
|
+
#include "hblas.h"
|
48
|
+
|
49
|
+
|
50
|
+
/*************************************************************************
|
51
|
+
Reduction of a Hermitian matrix which is given by its higher or lower
|
52
|
+
triangular part to a real tridiagonal matrix using unitary similarity
|
53
|
+
transformation: Q'*A*Q = T.
|
54
|
+
|
55
|
+
Input parameters:
|
56
|
+
A - matrix to be transformed
|
57
|
+
array with elements [0..N-1, 0..N-1].
|
58
|
+
N - size of matrix A.
|
59
|
+
IsUpper - storage format. If IsUpper = True, then matrix A is given
|
60
|
+
by its upper triangle, and the lower triangle is not used
|
61
|
+
and not modified by the algorithm, and vice versa
|
62
|
+
if IsUpper = False.
|
63
|
+
|
64
|
+
Output parameters:
|
65
|
+
A - matrices T and Q in compact form (see lower)
|
66
|
+
Tau - array of factors which are forming matrices H(i)
|
67
|
+
array with elements [0..N-2].
|
68
|
+
D - main diagonal of real symmetric matrix T.
|
69
|
+
array with elements [0..N-1].
|
70
|
+
E - secondary diagonal of real symmetric matrix T.
|
71
|
+
array with elements [0..N-2].
|
72
|
+
|
73
|
+
|
74
|
+
If IsUpper=True, the matrix Q is represented as a product of elementary
|
75
|
+
reflectors
|
76
|
+
|
77
|
+
Q = H(n-2) . . . H(2) H(0).
|
78
|
+
|
79
|
+
Each H(i) has the form
|
80
|
+
|
81
|
+
H(i) = I - tau * v * v'
|
82
|
+
|
83
|
+
where tau is a complex scalar, and v is a complex vector with
|
84
|
+
v(i+1:n-1) = 0, v(i) = 1, v(0:i-1) is stored on exit in
|
85
|
+
A(0:i-1,i+1), and tau in TAU(i).
|
86
|
+
|
87
|
+
If IsUpper=False, the matrix Q is represented as a product of elementary
|
88
|
+
reflectors
|
89
|
+
|
90
|
+
Q = H(0) H(2) . . . H(n-2).
|
91
|
+
|
92
|
+
Each H(i) has the form
|
93
|
+
|
94
|
+
H(i) = I - tau * v * v'
|
95
|
+
|
96
|
+
where tau is a complex scalar, and v is a complex vector with
|
97
|
+
v(0:i) = 0, v(i+1) = 1, v(i+2:n-1) is stored on exit in A(i+2:n-1,i),
|
98
|
+
and tau in TAU(i).
|
99
|
+
|
100
|
+
The contents of A on exit are illustrated by the following examples
|
101
|
+
with n = 5:
|
102
|
+
|
103
|
+
if UPLO = 'U': if UPLO = 'L':
|
104
|
+
|
105
|
+
( d e v1 v2 v3 ) ( d )
|
106
|
+
( d e v2 v3 ) ( e d )
|
107
|
+
( d e v3 ) ( v0 e d )
|
108
|
+
( d e ) ( v0 v1 e d )
|
109
|
+
( d ) ( v0 v1 v2 e d )
|
110
|
+
|
111
|
+
where d and e denote diagonal and off-diagonal elements of T, and vi
|
112
|
+
denotes an element of the vector defining H(i).
|
113
|
+
|
114
|
+
-- LAPACK routine (version 3.0) --
|
115
|
+
Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
|
116
|
+
Courant Institute, Argonne National Lab, and Rice University
|
117
|
+
October 31, 1992
|
118
|
+
*************************************************************************/
|
119
|
+
void hmatrixtd(ap::complex_2d_array& a,
|
120
|
+
int n,
|
121
|
+
bool isupper,
|
122
|
+
ap::complex_1d_array& tau,
|
123
|
+
ap::real_1d_array& d,
|
124
|
+
ap::real_1d_array& e);
|
125
|
+
|
126
|
+
|
127
|
+
/*************************************************************************
|
128
|
+
Unpacking matrix Q which reduces a Hermitian matrix to a real tridiagonal
|
129
|
+
form.
|
130
|
+
|
131
|
+
Input parameters:
|
132
|
+
A - the result of a HMatrixTD subroutine
|
133
|
+
N - size of matrix A.
|
134
|
+
IsUpper - storage format (a parameter of HMatrixTD subroutine)
|
135
|
+
Tau - the result of a HMatrixTD subroutine
|
136
|
+
|
137
|
+
Output parameters:
|
138
|
+
Q - transformation matrix.
|
139
|
+
array with elements [0..N-1, 0..N-1].
|
140
|
+
|
141
|
+
-- ALGLIB --
|
142
|
+
Copyright 2005, 2007, 2008 by Bochkanov Sergey
|
143
|
+
*************************************************************************/
|
144
|
+
void hmatrixtdunpackq(const ap::complex_2d_array& a,
|
145
|
+
const int& n,
|
146
|
+
const bool& isupper,
|
147
|
+
const ap::complex_1d_array& tau,
|
148
|
+
ap::complex_2d_array& q);
|
149
|
+
|
150
|
+
|
151
|
+
/*************************************************************************
|
152
|
+
Obsolete 1-based subroutine
|
153
|
+
|
154
|
+
-- LAPACK routine (version 3.0) --
|
155
|
+
Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
|
156
|
+
Courant Institute, Argonne National Lab, and Rice University
|
157
|
+
October 31, 1992
|
158
|
+
*************************************************************************/
|
159
|
+
void hermitiantotridiagonal(ap::complex_2d_array& a,
|
160
|
+
int n,
|
161
|
+
bool isupper,
|
162
|
+
ap::complex_1d_array& tau,
|
163
|
+
ap::real_1d_array& d,
|
164
|
+
ap::real_1d_array& e);
|
165
|
+
|
166
|
+
|
167
|
+
/*************************************************************************
|
168
|
+
Obsolete 1-based subroutine
|
169
|
+
|
170
|
+
-- ALGLIB --
|
171
|
+
Copyright 2005, 2007 by Bochkanov Sergey
|
172
|
+
*************************************************************************/
|
173
|
+
void unpackqfromhermitiantridiagonal(const ap::complex_2d_array& a,
|
174
|
+
const int& n,
|
175
|
+
const bool& isupper,
|
176
|
+
const ap::complex_1d_array& tau,
|
177
|
+
ap::complex_2d_array& q);
|
178
|
+
|
179
|
+
|
180
|
+
#endif
|
@@ -0,0 +1,9 @@
|
|
1
|
+
/********************************************************************
|
2
|
+
Stub file for assembly optimized ALGLIB subroutines.
|
3
|
+
********************************************************************/
|
4
|
+
#ifndef IALGLIB_H
|
5
|
+
#define IALGLIB_H
|
6
|
+
|
7
|
+
#include "ap.h"
|
8
|
+
|
9
|
+
#endif
|
@@ -0,0 +1,960 @@
|
|
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
|
+
Redistribution and use in source and binary forms, with or without
|
12
|
+
modification, are permitted provided that the following conditions are
|
13
|
+
met:
|
14
|
+
|
15
|
+
- Redistributions of source code must retain the above copyright
|
16
|
+
notice, this list of conditions and the following disclaimer.
|
17
|
+
|
18
|
+
- Redistributions in binary form must reproduce the above copyright
|
19
|
+
notice, this list of conditions and the following disclaimer listed
|
20
|
+
in this license in the documentation and/or other materials
|
21
|
+
provided with the distribution.
|
22
|
+
|
23
|
+
- Neither the name of the copyright holders nor the names of its
|
24
|
+
contributors may be used to endorse or promote products derived from
|
25
|
+
this software without specific prior written permission.
|
26
|
+
|
27
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
28
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
29
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
30
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
31
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
32
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
33
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
34
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
35
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
36
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
37
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
38
|
+
*************************************************************************/
|
39
|
+
|
40
|
+
#include <stdafx.h>
|
41
|
+
#include "ibetaf.h"
|
42
|
+
|
43
|
+
static double incompletebetafe(double a,
|
44
|
+
double b,
|
45
|
+
double x,
|
46
|
+
double big,
|
47
|
+
double biginv);
|
48
|
+
static double incompletebetafe2(double a,
|
49
|
+
double b,
|
50
|
+
double x,
|
51
|
+
double big,
|
52
|
+
double biginv);
|
53
|
+
static double incompletebetaps(double a, double b, double x, double maxgam);
|
54
|
+
|
55
|
+
/*************************************************************************
|
56
|
+
Incomplete beta integral
|
57
|
+
|
58
|
+
Returns incomplete beta integral of the arguments, evaluated
|
59
|
+
from zero to x. The function is defined as
|
60
|
+
|
61
|
+
x
|
62
|
+
- -
|
63
|
+
| (a+b) | | a-1 b-1
|
64
|
+
----------- | t (1-t) dt.
|
65
|
+
- - | |
|
66
|
+
| (a) | (b) -
|
67
|
+
0
|
68
|
+
|
69
|
+
The domain of definition is 0 <= x <= 1. In this
|
70
|
+
implementation a and b are restricted to positive values.
|
71
|
+
The integral from x to 1 may be obtained by the symmetry
|
72
|
+
relation
|
73
|
+
|
74
|
+
1 - incbet( a, b, x ) = incbet( b, a, 1-x ).
|
75
|
+
|
76
|
+
The integral is evaluated by a continued fraction expansion
|
77
|
+
or, when b*x is small, by a power series.
|
78
|
+
|
79
|
+
ACCURACY:
|
80
|
+
|
81
|
+
Tested at uniformly distributed random points (a,b,x) with a and b
|
82
|
+
in "domain" and x between 0 and 1.
|
83
|
+
Relative error
|
84
|
+
arithmetic domain # trials peak rms
|
85
|
+
IEEE 0,5 10000 6.9e-15 4.5e-16
|
86
|
+
IEEE 0,85 250000 2.2e-13 1.7e-14
|
87
|
+
IEEE 0,1000 30000 5.3e-12 6.3e-13
|
88
|
+
IEEE 0,10000 250000 9.3e-11 7.1e-12
|
89
|
+
IEEE 0,100000 10000 8.7e-10 4.8e-11
|
90
|
+
Outputs smaller than the IEEE gradual underflow threshold
|
91
|
+
were excluded from these statistics.
|
92
|
+
|
93
|
+
Cephes Math Library, Release 2.8: June, 2000
|
94
|
+
Copyright 1984, 1995, 2000 by Stephen L. Moshier
|
95
|
+
*************************************************************************/
|
96
|
+
double incompletebeta(double a, double b, double x)
|
97
|
+
{
|
98
|
+
double result;
|
99
|
+
double t;
|
100
|
+
double xc;
|
101
|
+
double w;
|
102
|
+
double y;
|
103
|
+
int flag;
|
104
|
+
double sg;
|
105
|
+
double big;
|
106
|
+
double biginv;
|
107
|
+
double maxgam;
|
108
|
+
double minlog;
|
109
|
+
double maxlog;
|
110
|
+
|
111
|
+
big = 4.503599627370496e15;
|
112
|
+
biginv = 2.22044604925031308085e-16;
|
113
|
+
maxgam = 171.624376956302725;
|
114
|
+
minlog = log(ap::minrealnumber);
|
115
|
+
maxlog = log(ap::maxrealnumber);
|
116
|
+
ap::ap_error::make_assertion(a>0&&b>0, "Domain error in IncompleteBeta");
|
117
|
+
ap::ap_error::make_assertion(x>=0&&x<=1, "Domain error in IncompleteBeta");
|
118
|
+
if( x==0 )
|
119
|
+
{
|
120
|
+
result = 0;
|
121
|
+
return result;
|
122
|
+
}
|
123
|
+
if( x==1 )
|
124
|
+
{
|
125
|
+
result = 1;
|
126
|
+
return result;
|
127
|
+
}
|
128
|
+
flag = 0;
|
129
|
+
if( b*x<=1.0&&x<=0.95 )
|
130
|
+
{
|
131
|
+
result = incompletebetaps(a, b, x, maxgam);
|
132
|
+
return result;
|
133
|
+
}
|
134
|
+
w = 1.0-x;
|
135
|
+
if( x>a/(a+b) )
|
136
|
+
{
|
137
|
+
flag = 1;
|
138
|
+
t = a;
|
139
|
+
a = b;
|
140
|
+
b = t;
|
141
|
+
xc = x;
|
142
|
+
x = w;
|
143
|
+
}
|
144
|
+
else
|
145
|
+
{
|
146
|
+
xc = w;
|
147
|
+
}
|
148
|
+
if( flag==1&&b*x<=1.0&&x<=0.95 )
|
149
|
+
{
|
150
|
+
t = incompletebetaps(a, b, x, maxgam);
|
151
|
+
if( t<=ap::machineepsilon )
|
152
|
+
{
|
153
|
+
result = 1.0-ap::machineepsilon;
|
154
|
+
}
|
155
|
+
else
|
156
|
+
{
|
157
|
+
result = 1.0-t;
|
158
|
+
}
|
159
|
+
return result;
|
160
|
+
}
|
161
|
+
y = x*(a+b-2.0)-(a-1.0);
|
162
|
+
if( y<0.0 )
|
163
|
+
{
|
164
|
+
w = incompletebetafe(a, b, x, big, biginv);
|
165
|
+
}
|
166
|
+
else
|
167
|
+
{
|
168
|
+
w = incompletebetafe2(a, b, x, big, biginv)/xc;
|
169
|
+
}
|
170
|
+
y = a*log(x);
|
171
|
+
t = b*log(xc);
|
172
|
+
if( a+b<maxgam&&fabs(y)<maxlog&&fabs(t)<maxlog )
|
173
|
+
{
|
174
|
+
t = pow(xc, b);
|
175
|
+
t = t*pow(x, a);
|
176
|
+
t = t/a;
|
177
|
+
t = t*w;
|
178
|
+
t = t*(gamma(a+b)/(gamma(a)*gamma(b)));
|
179
|
+
if( flag==1 )
|
180
|
+
{
|
181
|
+
if( t<=ap::machineepsilon )
|
182
|
+
{
|
183
|
+
result = 1.0-ap::machineepsilon;
|
184
|
+
}
|
185
|
+
else
|
186
|
+
{
|
187
|
+
result = 1.0-t;
|
188
|
+
}
|
189
|
+
}
|
190
|
+
else
|
191
|
+
{
|
192
|
+
result = t;
|
193
|
+
}
|
194
|
+
return result;
|
195
|
+
}
|
196
|
+
y = y+t+lngamma(a+b, sg)-lngamma(a, sg)-lngamma(b, sg);
|
197
|
+
y = y+log(w/a);
|
198
|
+
if( y<minlog )
|
199
|
+
{
|
200
|
+
t = 0.0;
|
201
|
+
}
|
202
|
+
else
|
203
|
+
{
|
204
|
+
t = exp(y);
|
205
|
+
}
|
206
|
+
if( flag==1 )
|
207
|
+
{
|
208
|
+
if( t<=ap::machineepsilon )
|
209
|
+
{
|
210
|
+
t = 1.0-ap::machineepsilon;
|
211
|
+
}
|
212
|
+
else
|
213
|
+
{
|
214
|
+
t = 1.0-t;
|
215
|
+
}
|
216
|
+
}
|
217
|
+
result = t;
|
218
|
+
return result;
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
/*************************************************************************
|
223
|
+
Inverse of imcomplete beta integral
|
224
|
+
|
225
|
+
Given y, the function finds x such that
|
226
|
+
|
227
|
+
incbet( a, b, x ) = y .
|
228
|
+
|
229
|
+
The routine performs interval halving or Newton iterations to find the
|
230
|
+
root of incbet(a,b,x) - y = 0.
|
231
|
+
|
232
|
+
|
233
|
+
ACCURACY:
|
234
|
+
|
235
|
+
Relative error:
|
236
|
+
x a,b
|
237
|
+
arithmetic domain domain # trials peak rms
|
238
|
+
IEEE 0,1 .5,10000 50000 5.8e-12 1.3e-13
|
239
|
+
IEEE 0,1 .25,100 100000 1.8e-13 3.9e-15
|
240
|
+
IEEE 0,1 0,5 50000 1.1e-12 5.5e-15
|
241
|
+
With a and b constrained to half-integer or integer values:
|
242
|
+
IEEE 0,1 .5,10000 50000 5.8e-12 1.1e-13
|
243
|
+
IEEE 0,1 .5,100 100000 1.7e-14 7.9e-16
|
244
|
+
With a = .5, b constrained to half-integer or integer values:
|
245
|
+
IEEE 0,1 .5,10000 10000 8.3e-11 1.0e-11
|
246
|
+
|
247
|
+
Cephes Math Library Release 2.8: June, 2000
|
248
|
+
Copyright 1984, 1996, 2000 by Stephen L. Moshier
|
249
|
+
*************************************************************************/
|
250
|
+
double invincompletebeta(double a, double b, double y)
|
251
|
+
{
|
252
|
+
double result;
|
253
|
+
double aaa;
|
254
|
+
double bbb;
|
255
|
+
double y0;
|
256
|
+
double d;
|
257
|
+
double yyy;
|
258
|
+
double x;
|
259
|
+
double x0;
|
260
|
+
double x1;
|
261
|
+
double lgm;
|
262
|
+
double yp;
|
263
|
+
double di;
|
264
|
+
double dithresh;
|
265
|
+
double yl;
|
266
|
+
double yh;
|
267
|
+
double xt;
|
268
|
+
int i;
|
269
|
+
int rflg;
|
270
|
+
int dir;
|
271
|
+
int nflg;
|
272
|
+
double s;
|
273
|
+
int mainlooppos;
|
274
|
+
int ihalve;
|
275
|
+
int ihalvecycle;
|
276
|
+
int newt;
|
277
|
+
int newtcycle;
|
278
|
+
int breaknewtcycle;
|
279
|
+
int breakihalvecycle;
|
280
|
+
|
281
|
+
i = 0;
|
282
|
+
ap::ap_error::make_assertion(y>=0&&y<=1, "Domain error in InvIncompleteBeta");
|
283
|
+
if( y==0 )
|
284
|
+
{
|
285
|
+
result = 0;
|
286
|
+
return result;
|
287
|
+
}
|
288
|
+
if( y==1.0 )
|
289
|
+
{
|
290
|
+
result = 1;
|
291
|
+
return result;
|
292
|
+
}
|
293
|
+
x0 = 0.0;
|
294
|
+
yl = 0.0;
|
295
|
+
x1 = 1.0;
|
296
|
+
yh = 1.0;
|
297
|
+
nflg = 0;
|
298
|
+
mainlooppos = 0;
|
299
|
+
ihalve = 1;
|
300
|
+
ihalvecycle = 2;
|
301
|
+
newt = 3;
|
302
|
+
newtcycle = 4;
|
303
|
+
breaknewtcycle = 5;
|
304
|
+
breakihalvecycle = 6;
|
305
|
+
while(true)
|
306
|
+
{
|
307
|
+
|
308
|
+
//
|
309
|
+
// start
|
310
|
+
//
|
311
|
+
if( mainlooppos==0 )
|
312
|
+
{
|
313
|
+
if( a<=1.0||b<=1.0 )
|
314
|
+
{
|
315
|
+
dithresh = 1.0e-6;
|
316
|
+
rflg = 0;
|
317
|
+
aaa = a;
|
318
|
+
bbb = b;
|
319
|
+
y0 = y;
|
320
|
+
x = aaa/(aaa+bbb);
|
321
|
+
yyy = incompletebeta(aaa, bbb, x);
|
322
|
+
mainlooppos = ihalve;
|
323
|
+
continue;
|
324
|
+
}
|
325
|
+
else
|
326
|
+
{
|
327
|
+
dithresh = 1.0e-4;
|
328
|
+
}
|
329
|
+
yp = -invnormaldistribution(y);
|
330
|
+
if( y>0.5 )
|
331
|
+
{
|
332
|
+
rflg = 1;
|
333
|
+
aaa = b;
|
334
|
+
bbb = a;
|
335
|
+
y0 = 1.0-y;
|
336
|
+
yp = -yp;
|
337
|
+
}
|
338
|
+
else
|
339
|
+
{
|
340
|
+
rflg = 0;
|
341
|
+
aaa = a;
|
342
|
+
bbb = b;
|
343
|
+
y0 = y;
|
344
|
+
}
|
345
|
+
lgm = (yp*yp-3.0)/6.0;
|
346
|
+
x = 2.0/(1.0/(2.0*aaa-1.0)+1.0/(2.0*bbb-1.0));
|
347
|
+
d = yp*sqrt(x+lgm)/x-(1.0/(2.0*bbb-1.0)-1.0/(2.0*aaa-1.0))*(lgm+5.0/6.0-2.0/(3.0*x));
|
348
|
+
d = 2.0*d;
|
349
|
+
if( d<log(ap::minrealnumber) )
|
350
|
+
{
|
351
|
+
x = 0;
|
352
|
+
break;
|
353
|
+
}
|
354
|
+
x = aaa/(aaa+bbb*exp(d));
|
355
|
+
yyy = incompletebeta(aaa, bbb, x);
|
356
|
+
yp = (yyy-y0)/y0;
|
357
|
+
if( fabs(yp)<0.2 )
|
358
|
+
{
|
359
|
+
mainlooppos = newt;
|
360
|
+
continue;
|
361
|
+
}
|
362
|
+
mainlooppos = ihalve;
|
363
|
+
continue;
|
364
|
+
}
|
365
|
+
|
366
|
+
//
|
367
|
+
// ihalve
|
368
|
+
//
|
369
|
+
if( mainlooppos==ihalve )
|
370
|
+
{
|
371
|
+
dir = 0;
|
372
|
+
di = 0.5;
|
373
|
+
i = 0;
|
374
|
+
mainlooppos = ihalvecycle;
|
375
|
+
continue;
|
376
|
+
}
|
377
|
+
|
378
|
+
//
|
379
|
+
// ihalvecycle
|
380
|
+
//
|
381
|
+
if( mainlooppos==ihalvecycle )
|
382
|
+
{
|
383
|
+
if( i<=99 )
|
384
|
+
{
|
385
|
+
if( i!=0 )
|
386
|
+
{
|
387
|
+
x = x0+di*(x1-x0);
|
388
|
+
if( x==1.0 )
|
389
|
+
{
|
390
|
+
x = 1.0-ap::machineepsilon;
|
391
|
+
}
|
392
|
+
if( x==0.0 )
|
393
|
+
{
|
394
|
+
di = 0.5;
|
395
|
+
x = x0+di*(x1-x0);
|
396
|
+
if( x==0.0 )
|
397
|
+
{
|
398
|
+
break;
|
399
|
+
}
|
400
|
+
}
|
401
|
+
yyy = incompletebeta(aaa, bbb, x);
|
402
|
+
yp = (x1-x0)/(x1+x0);
|
403
|
+
if( fabs(yp)<dithresh )
|
404
|
+
{
|
405
|
+
mainlooppos = newt;
|
406
|
+
continue;
|
407
|
+
}
|
408
|
+
yp = (yyy-y0)/y0;
|
409
|
+
if( fabs(yp)<dithresh )
|
410
|
+
{
|
411
|
+
mainlooppos = newt;
|
412
|
+
continue;
|
413
|
+
}
|
414
|
+
}
|
415
|
+
if( yyy<y0 )
|
416
|
+
{
|
417
|
+
x0 = x;
|
418
|
+
yl = yyy;
|
419
|
+
if( dir<0 )
|
420
|
+
{
|
421
|
+
dir = 0;
|
422
|
+
di = 0.5;
|
423
|
+
}
|
424
|
+
else
|
425
|
+
{
|
426
|
+
if( dir>3 )
|
427
|
+
{
|
428
|
+
di = 1.0-(1.0-di)*(1.0-di);
|
429
|
+
}
|
430
|
+
else
|
431
|
+
{
|
432
|
+
if( dir>1 )
|
433
|
+
{
|
434
|
+
di = 0.5*di+0.5;
|
435
|
+
}
|
436
|
+
else
|
437
|
+
{
|
438
|
+
di = (y0-yyy)/(yh-yl);
|
439
|
+
}
|
440
|
+
}
|
441
|
+
}
|
442
|
+
dir = dir+1;
|
443
|
+
if( x0>0.75 )
|
444
|
+
{
|
445
|
+
if( rflg==1 )
|
446
|
+
{
|
447
|
+
rflg = 0;
|
448
|
+
aaa = a;
|
449
|
+
bbb = b;
|
450
|
+
y0 = y;
|
451
|
+
}
|
452
|
+
else
|
453
|
+
{
|
454
|
+
rflg = 1;
|
455
|
+
aaa = b;
|
456
|
+
bbb = a;
|
457
|
+
y0 = 1.0-y;
|
458
|
+
}
|
459
|
+
x = 1.0-x;
|
460
|
+
yyy = incompletebeta(aaa, bbb, x);
|
461
|
+
x0 = 0.0;
|
462
|
+
yl = 0.0;
|
463
|
+
x1 = 1.0;
|
464
|
+
yh = 1.0;
|
465
|
+
mainlooppos = ihalve;
|
466
|
+
continue;
|
467
|
+
}
|
468
|
+
}
|
469
|
+
else
|
470
|
+
{
|
471
|
+
x1 = x;
|
472
|
+
if( rflg==1&&x1<ap::machineepsilon )
|
473
|
+
{
|
474
|
+
x = 0.0;
|
475
|
+
break;
|
476
|
+
}
|
477
|
+
yh = yyy;
|
478
|
+
if( dir>0 )
|
479
|
+
{
|
480
|
+
dir = 0;
|
481
|
+
di = 0.5;
|
482
|
+
}
|
483
|
+
else
|
484
|
+
{
|
485
|
+
if( dir<-3 )
|
486
|
+
{
|
487
|
+
di = di*di;
|
488
|
+
}
|
489
|
+
else
|
490
|
+
{
|
491
|
+
if( dir<-1 )
|
492
|
+
{
|
493
|
+
di = 0.5*di;
|
494
|
+
}
|
495
|
+
else
|
496
|
+
{
|
497
|
+
di = (yyy-y0)/(yh-yl);
|
498
|
+
}
|
499
|
+
}
|
500
|
+
}
|
501
|
+
dir = dir-1;
|
502
|
+
}
|
503
|
+
i = i+1;
|
504
|
+
mainlooppos = ihalvecycle;
|
505
|
+
continue;
|
506
|
+
}
|
507
|
+
else
|
508
|
+
{
|
509
|
+
mainlooppos = breakihalvecycle;
|
510
|
+
continue;
|
511
|
+
}
|
512
|
+
}
|
513
|
+
|
514
|
+
//
|
515
|
+
// breakihalvecycle
|
516
|
+
//
|
517
|
+
if( mainlooppos==breakihalvecycle )
|
518
|
+
{
|
519
|
+
if( x0>=1.0 )
|
520
|
+
{
|
521
|
+
x = 1.0-ap::machineepsilon;
|
522
|
+
break;
|
523
|
+
}
|
524
|
+
if( x<=0.0 )
|
525
|
+
{
|
526
|
+
x = 0.0;
|
527
|
+
break;
|
528
|
+
}
|
529
|
+
mainlooppos = newt;
|
530
|
+
continue;
|
531
|
+
}
|
532
|
+
|
533
|
+
//
|
534
|
+
// newt
|
535
|
+
//
|
536
|
+
if( mainlooppos==newt )
|
537
|
+
{
|
538
|
+
if( nflg!=0 )
|
539
|
+
{
|
540
|
+
break;
|
541
|
+
}
|
542
|
+
nflg = 1;
|
543
|
+
lgm = lngamma(aaa+bbb, s)-lngamma(aaa, s)-lngamma(bbb, s);
|
544
|
+
i = 0;
|
545
|
+
mainlooppos = newtcycle;
|
546
|
+
continue;
|
547
|
+
}
|
548
|
+
|
549
|
+
//
|
550
|
+
// newtcycle
|
551
|
+
//
|
552
|
+
if( mainlooppos==newtcycle )
|
553
|
+
{
|
554
|
+
if( i<=7 )
|
555
|
+
{
|
556
|
+
if( i!=0 )
|
557
|
+
{
|
558
|
+
yyy = incompletebeta(aaa, bbb, x);
|
559
|
+
}
|
560
|
+
if( yyy<yl )
|
561
|
+
{
|
562
|
+
x = x0;
|
563
|
+
yyy = yl;
|
564
|
+
}
|
565
|
+
else
|
566
|
+
{
|
567
|
+
if( yyy>yh )
|
568
|
+
{
|
569
|
+
x = x1;
|
570
|
+
yyy = yh;
|
571
|
+
}
|
572
|
+
else
|
573
|
+
{
|
574
|
+
if( yyy<y0 )
|
575
|
+
{
|
576
|
+
x0 = x;
|
577
|
+
yl = yyy;
|
578
|
+
}
|
579
|
+
else
|
580
|
+
{
|
581
|
+
x1 = x;
|
582
|
+
yh = yyy;
|
583
|
+
}
|
584
|
+
}
|
585
|
+
}
|
586
|
+
if( x==1.0||x==0.0 )
|
587
|
+
{
|
588
|
+
mainlooppos = breaknewtcycle;
|
589
|
+
continue;
|
590
|
+
}
|
591
|
+
d = (aaa-1.0)*log(x)+(bbb-1.0)*log(1.0-x)+lgm;
|
592
|
+
if( d<log(ap::minrealnumber) )
|
593
|
+
{
|
594
|
+
break;
|
595
|
+
}
|
596
|
+
if( d>log(ap::maxrealnumber) )
|
597
|
+
{
|
598
|
+
mainlooppos = breaknewtcycle;
|
599
|
+
continue;
|
600
|
+
}
|
601
|
+
d = exp(d);
|
602
|
+
d = (yyy-y0)/d;
|
603
|
+
xt = x-d;
|
604
|
+
if( xt<=x0 )
|
605
|
+
{
|
606
|
+
yyy = (x-x0)/(x1-x0);
|
607
|
+
xt = x0+0.5*yyy*(x-x0);
|
608
|
+
if( xt<=0.0 )
|
609
|
+
{
|
610
|
+
mainlooppos = breaknewtcycle;
|
611
|
+
continue;
|
612
|
+
}
|
613
|
+
}
|
614
|
+
if( xt>=x1 )
|
615
|
+
{
|
616
|
+
yyy = (x1-x)/(x1-x0);
|
617
|
+
xt = x1-0.5*yyy*(x1-x);
|
618
|
+
if( xt>=1.0 )
|
619
|
+
{
|
620
|
+
mainlooppos = breaknewtcycle;
|
621
|
+
continue;
|
622
|
+
}
|
623
|
+
}
|
624
|
+
x = xt;
|
625
|
+
if( fabs(d/x)<128.0*ap::machineepsilon )
|
626
|
+
{
|
627
|
+
break;
|
628
|
+
}
|
629
|
+
i = i+1;
|
630
|
+
mainlooppos = newtcycle;
|
631
|
+
continue;
|
632
|
+
}
|
633
|
+
else
|
634
|
+
{
|
635
|
+
mainlooppos = breaknewtcycle;
|
636
|
+
continue;
|
637
|
+
}
|
638
|
+
}
|
639
|
+
|
640
|
+
//
|
641
|
+
// breaknewtcycle
|
642
|
+
//
|
643
|
+
if( mainlooppos==breaknewtcycle )
|
644
|
+
{
|
645
|
+
dithresh = 256.0*ap::machineepsilon;
|
646
|
+
mainlooppos = ihalve;
|
647
|
+
continue;
|
648
|
+
}
|
649
|
+
}
|
650
|
+
|
651
|
+
//
|
652
|
+
// done
|
653
|
+
//
|
654
|
+
if( rflg!=0 )
|
655
|
+
{
|
656
|
+
if( x<=ap::machineepsilon )
|
657
|
+
{
|
658
|
+
x = 1.0-ap::machineepsilon;
|
659
|
+
}
|
660
|
+
else
|
661
|
+
{
|
662
|
+
x = 1.0-x;
|
663
|
+
}
|
664
|
+
}
|
665
|
+
result = x;
|
666
|
+
return result;
|
667
|
+
}
|
668
|
+
|
669
|
+
|
670
|
+
/*************************************************************************
|
671
|
+
Continued fraction expansion #1 for incomplete beta integral
|
672
|
+
|
673
|
+
Cephes Math Library, Release 2.8: June, 2000
|
674
|
+
Copyright 1984, 1995, 2000 by Stephen L. Moshier
|
675
|
+
*************************************************************************/
|
676
|
+
static double incompletebetafe(double a,
|
677
|
+
double b,
|
678
|
+
double x,
|
679
|
+
double big,
|
680
|
+
double biginv)
|
681
|
+
{
|
682
|
+
double result;
|
683
|
+
double xk;
|
684
|
+
double pk;
|
685
|
+
double pkm1;
|
686
|
+
double pkm2;
|
687
|
+
double qk;
|
688
|
+
double qkm1;
|
689
|
+
double qkm2;
|
690
|
+
double k1;
|
691
|
+
double k2;
|
692
|
+
double k3;
|
693
|
+
double k4;
|
694
|
+
double k5;
|
695
|
+
double k6;
|
696
|
+
double k7;
|
697
|
+
double k8;
|
698
|
+
double r;
|
699
|
+
double t;
|
700
|
+
double ans;
|
701
|
+
double thresh;
|
702
|
+
int n;
|
703
|
+
|
704
|
+
k1 = a;
|
705
|
+
k2 = a+b;
|
706
|
+
k3 = a;
|
707
|
+
k4 = a+1.0;
|
708
|
+
k5 = 1.0;
|
709
|
+
k6 = b-1.0;
|
710
|
+
k7 = k4;
|
711
|
+
k8 = a+2.0;
|
712
|
+
pkm2 = 0.0;
|
713
|
+
qkm2 = 1.0;
|
714
|
+
pkm1 = 1.0;
|
715
|
+
qkm1 = 1.0;
|
716
|
+
ans = 1.0;
|
717
|
+
r = 1.0;
|
718
|
+
n = 0;
|
719
|
+
thresh = 3.0*ap::machineepsilon;
|
720
|
+
do
|
721
|
+
{
|
722
|
+
xk = -x*k1*k2/(k3*k4);
|
723
|
+
pk = pkm1+pkm2*xk;
|
724
|
+
qk = qkm1+qkm2*xk;
|
725
|
+
pkm2 = pkm1;
|
726
|
+
pkm1 = pk;
|
727
|
+
qkm2 = qkm1;
|
728
|
+
qkm1 = qk;
|
729
|
+
xk = x*k5*k6/(k7*k8);
|
730
|
+
pk = pkm1+pkm2*xk;
|
731
|
+
qk = qkm1+qkm2*xk;
|
732
|
+
pkm2 = pkm1;
|
733
|
+
pkm1 = pk;
|
734
|
+
qkm2 = qkm1;
|
735
|
+
qkm1 = qk;
|
736
|
+
if( qk!=0 )
|
737
|
+
{
|
738
|
+
r = pk/qk;
|
739
|
+
}
|
740
|
+
if( r!=0 )
|
741
|
+
{
|
742
|
+
t = fabs((ans-r)/r);
|
743
|
+
ans = r;
|
744
|
+
}
|
745
|
+
else
|
746
|
+
{
|
747
|
+
t = 1.0;
|
748
|
+
}
|
749
|
+
if( t<thresh )
|
750
|
+
{
|
751
|
+
break;
|
752
|
+
}
|
753
|
+
k1 = k1+1.0;
|
754
|
+
k2 = k2+1.0;
|
755
|
+
k3 = k3+2.0;
|
756
|
+
k4 = k4+2.0;
|
757
|
+
k5 = k5+1.0;
|
758
|
+
k6 = k6-1.0;
|
759
|
+
k7 = k7+2.0;
|
760
|
+
k8 = k8+2.0;
|
761
|
+
if( fabs(qk)+fabs(pk)>big )
|
762
|
+
{
|
763
|
+
pkm2 = pkm2*biginv;
|
764
|
+
pkm1 = pkm1*biginv;
|
765
|
+
qkm2 = qkm2*biginv;
|
766
|
+
qkm1 = qkm1*biginv;
|
767
|
+
}
|
768
|
+
if( fabs(qk)<biginv||fabs(pk)<biginv )
|
769
|
+
{
|
770
|
+
pkm2 = pkm2*big;
|
771
|
+
pkm1 = pkm1*big;
|
772
|
+
qkm2 = qkm2*big;
|
773
|
+
qkm1 = qkm1*big;
|
774
|
+
}
|
775
|
+
n = n+1;
|
776
|
+
}
|
777
|
+
while(n!=300);
|
778
|
+
result = ans;
|
779
|
+
return result;
|
780
|
+
}
|
781
|
+
|
782
|
+
|
783
|
+
/*************************************************************************
|
784
|
+
Continued fraction expansion #2
|
785
|
+
for incomplete beta integral
|
786
|
+
|
787
|
+
Cephes Math Library, Release 2.8: June, 2000
|
788
|
+
Copyright 1984, 1995, 2000 by Stephen L. Moshier
|
789
|
+
*************************************************************************/
|
790
|
+
static double incompletebetafe2(double a,
|
791
|
+
double b,
|
792
|
+
double x,
|
793
|
+
double big,
|
794
|
+
double biginv)
|
795
|
+
{
|
796
|
+
double result;
|
797
|
+
double xk;
|
798
|
+
double pk;
|
799
|
+
double pkm1;
|
800
|
+
double pkm2;
|
801
|
+
double qk;
|
802
|
+
double qkm1;
|
803
|
+
double qkm2;
|
804
|
+
double k1;
|
805
|
+
double k2;
|
806
|
+
double k3;
|
807
|
+
double k4;
|
808
|
+
double k5;
|
809
|
+
double k6;
|
810
|
+
double k7;
|
811
|
+
double k8;
|
812
|
+
double r;
|
813
|
+
double t;
|
814
|
+
double ans;
|
815
|
+
double z;
|
816
|
+
double thresh;
|
817
|
+
int n;
|
818
|
+
|
819
|
+
k1 = a;
|
820
|
+
k2 = b-1.0;
|
821
|
+
k3 = a;
|
822
|
+
k4 = a+1.0;
|
823
|
+
k5 = 1.0;
|
824
|
+
k6 = a+b;
|
825
|
+
k7 = a+1.0;
|
826
|
+
k8 = a+2.0;
|
827
|
+
pkm2 = 0.0;
|
828
|
+
qkm2 = 1.0;
|
829
|
+
pkm1 = 1.0;
|
830
|
+
qkm1 = 1.0;
|
831
|
+
z = x/(1.0-x);
|
832
|
+
ans = 1.0;
|
833
|
+
r = 1.0;
|
834
|
+
n = 0;
|
835
|
+
thresh = 3.0*ap::machineepsilon;
|
836
|
+
do
|
837
|
+
{
|
838
|
+
xk = -z*k1*k2/(k3*k4);
|
839
|
+
pk = pkm1+pkm2*xk;
|
840
|
+
qk = qkm1+qkm2*xk;
|
841
|
+
pkm2 = pkm1;
|
842
|
+
pkm1 = pk;
|
843
|
+
qkm2 = qkm1;
|
844
|
+
qkm1 = qk;
|
845
|
+
xk = z*k5*k6/(k7*k8);
|
846
|
+
pk = pkm1+pkm2*xk;
|
847
|
+
qk = qkm1+qkm2*xk;
|
848
|
+
pkm2 = pkm1;
|
849
|
+
pkm1 = pk;
|
850
|
+
qkm2 = qkm1;
|
851
|
+
qkm1 = qk;
|
852
|
+
if( qk!=0 )
|
853
|
+
{
|
854
|
+
r = pk/qk;
|
855
|
+
}
|
856
|
+
if( r!=0 )
|
857
|
+
{
|
858
|
+
t = fabs((ans-r)/r);
|
859
|
+
ans = r;
|
860
|
+
}
|
861
|
+
else
|
862
|
+
{
|
863
|
+
t = 1.0;
|
864
|
+
}
|
865
|
+
if( t<thresh )
|
866
|
+
{
|
867
|
+
break;
|
868
|
+
}
|
869
|
+
k1 = k1+1.0;
|
870
|
+
k2 = k2-1.0;
|
871
|
+
k3 = k3+2.0;
|
872
|
+
k4 = k4+2.0;
|
873
|
+
k5 = k5+1.0;
|
874
|
+
k6 = k6+1.0;
|
875
|
+
k7 = k7+2.0;
|
876
|
+
k8 = k8+2.0;
|
877
|
+
if( fabs(qk)+fabs(pk)>big )
|
878
|
+
{
|
879
|
+
pkm2 = pkm2*biginv;
|
880
|
+
pkm1 = pkm1*biginv;
|
881
|
+
qkm2 = qkm2*biginv;
|
882
|
+
qkm1 = qkm1*biginv;
|
883
|
+
}
|
884
|
+
if( fabs(qk)<biginv||fabs(pk)<biginv )
|
885
|
+
{
|
886
|
+
pkm2 = pkm2*big;
|
887
|
+
pkm1 = pkm1*big;
|
888
|
+
qkm2 = qkm2*big;
|
889
|
+
qkm1 = qkm1*big;
|
890
|
+
}
|
891
|
+
n = n+1;
|
892
|
+
}
|
893
|
+
while(n!=300);
|
894
|
+
result = ans;
|
895
|
+
return result;
|
896
|
+
}
|
897
|
+
|
898
|
+
|
899
|
+
/*************************************************************************
|
900
|
+
Power series for incomplete beta integral.
|
901
|
+
Use when b*x is small and x not too close to 1.
|
902
|
+
|
903
|
+
Cephes Math Library, Release 2.8: June, 2000
|
904
|
+
Copyright 1984, 1995, 2000 by Stephen L. Moshier
|
905
|
+
*************************************************************************/
|
906
|
+
static double incompletebetaps(double a, double b, double x, double maxgam)
|
907
|
+
{
|
908
|
+
double result;
|
909
|
+
double s;
|
910
|
+
double t;
|
911
|
+
double u;
|
912
|
+
double v;
|
913
|
+
double n;
|
914
|
+
double t1;
|
915
|
+
double z;
|
916
|
+
double ai;
|
917
|
+
double sg;
|
918
|
+
|
919
|
+
ai = 1.0/a;
|
920
|
+
u = (1.0-b)*x;
|
921
|
+
v = u/(a+1.0);
|
922
|
+
t1 = v;
|
923
|
+
t = u;
|
924
|
+
n = 2.0;
|
925
|
+
s = 0.0;
|
926
|
+
z = ap::machineepsilon*ai;
|
927
|
+
while(fabs(v)>z)
|
928
|
+
{
|
929
|
+
u = (n-b)*x/n;
|
930
|
+
t = t*u;
|
931
|
+
v = t/(a+n);
|
932
|
+
s = s+v;
|
933
|
+
n = n+1.0;
|
934
|
+
}
|
935
|
+
s = s+t1;
|
936
|
+
s = s+ai;
|
937
|
+
u = a*log(x);
|
938
|
+
if( a+b<maxgam&&fabs(u)<log(ap::maxrealnumber) )
|
939
|
+
{
|
940
|
+
t = gamma(a+b)/(gamma(a)*gamma(b));
|
941
|
+
s = s*t*pow(x, a);
|
942
|
+
}
|
943
|
+
else
|
944
|
+
{
|
945
|
+
t = lngamma(a+b, sg)-lngamma(a, sg)-lngamma(b, sg)+u+log(s);
|
946
|
+
if( t<log(ap::minrealnumber) )
|
947
|
+
{
|
948
|
+
s = 0.0;
|
949
|
+
}
|
950
|
+
else
|
951
|
+
{
|
952
|
+
s = exp(t);
|
953
|
+
}
|
954
|
+
}
|
955
|
+
result = s;
|
956
|
+
return result;
|
957
|
+
}
|
958
|
+
|
959
|
+
|
960
|
+
|