/* incbil()
*
* Inverse of imcomplete beta integral
*
*
*
* SYNOPSIS :
*
* long double a , b , x , y , incbil ( ) ;
*
* x = incbil ( a , b , y ) ;
*
*
*
* DESCRIPTION :
*
* Given y , the function finds x such that
*
* incbet ( a , b , x ) = y .
*
* the routine performs up to 10 Newton iterations to find the
* root of incbet ( a , b , x ) - y = 0 .
*
*
* ACCURACY :
*
* Relative error :
* x a , b
* arithmetic domain domain # trials peak rms
* IEEE 0 , 1 . 5 , 10000 10000 1 . 1 e - 14 1 . 4 e - 16
*/
/*
Cephes Math Library Release 2 . 3 : March , 1995
Copyright 1984 , 1995 by Stephen L . Moshier
*/
#include "mconf.h"
extern long double MACHEPL, MAXNUML, MAXLOGL, MINLOGL;
#ifdef ANSIPROT
extern long double incbetl ( long double , long double , long double );
extern long double expl ( long double );
extern long double fabsl ( long double );
extern long double logl ( long double );
extern long double sqrtl ( long double );
extern long double lgaml ( long double );
extern long double ndtril ( long double );
#else
long double incbetl(), expl(), fabsl(), logl(), sqrtl(), lgaml();
long double ndtril();
#endif
long double incbil( aa, bb, yy0 )
long double aa, bb, yy0;
{
long double a, b, y0, d, y, x, x0, x1, lgm, yp, di, dithresh, yl, yh, xt;
int i, rflg, dir, nflg;
if ( yy0 <= 0 .0 L )
return (0 .0 L);
if ( yy0 >= 1 .0 L )
return (1 .0 L);
x0 = 0 .0 L;
yl = 0 .0 L;
x1 = 1 .0 L;
yh = 1 .0 L;
if ( aa <= 1 .0 L || bb <= 1 .0 L )
{
dithresh = 1 .0 e-7 L;
rflg = 0 ;
a = aa;
b = bb;
y0 = yy0;
x = a/(a+b);
y = incbetl( a, b, x );
nflg = 0 ;
goto ihalve;
}
else
{
nflg = 0 ;
dithresh = 1 .0 e-4 L;
}
/* approximation to inverse function */
yp = -ndtril( yy0 );
if ( yy0 > 0 .5 L )
{
rflg = 1 ;
a = bb;
b = aa;
y0 = 1 .0 L - yy0;
yp = -yp;
}
else
{
rflg = 0 ;
a = aa;
b = bb;
y0 = yy0;
}
lgm = (yp * yp - 3 .0 L)/6 .0 L;
x = 2 .0 L/( 1 .0 L/(2 .0 L * a-1 .0 L) + 1 .0 L/(2 .0 L * b - 1 .0 L) );
d = yp * sqrtl( x + lgm ) / x
- ( 1 .0 L/(2 .0 L * b - 1 .0 L) - 1 .0 L/(2 .0 L * a - 1 .0 L) )
* (lgm + (5 .0 L/6 .0 L) - 2 .0 L/(3 .0 L * x));
d = 2 .0 L * d;
if ( d < MINLOGL )
{
x = 1 .0 L;
goto under;
}
x = a/( a + b * expl(d) );
y = incbetl( a, b, x );
yp = (y - y0)/y0;
if ( fabsl(yp) < 0 .2 )
goto newt;
/* Resort to interval halving if not close enough. */
ihalve:
dir = 0 ;
di = 0 .5 L;
for ( i=0 ; i<400 ; i++ )
{
if ( i != 0 )
{
x = x0 + di * (x1 - x0);
if ( x == 1 .0 L )
x = 1 .0 L - MACHEPL;
if ( x == 0 .0 L )
{
di = 0 .5 ;
x = x0 + di * (x1 - x0);
if ( x == 0 .0 )
goto under;
}
y = incbetl( a, b, x );
yp = (x1 - x0)/(x1 + x0);
if ( fabsl(yp) < dithresh )
goto newt;
yp = (y-y0)/y0;
if ( fabsl(yp) < dithresh )
goto newt;
}
if ( y < y0 )
{
x0 = x;
yl = y;
if ( dir < 0 )
{
dir = 0 ;
di = 0 .5 L;
}
else if ( dir > 3 )
di = 1 .0 L - (1 .0 L - di) * (1 .0 L - di);
else if ( dir > 1 )
di = 0 .5 L * di + 0 .5 L;
else
di = (y0 - y)/(yh - yl);
dir += 1 ;
if ( x0 > 0 .95 L )
{
if ( rflg == 1 )
{
rflg = 0 ;
a = aa;
b = bb;
y0 = yy0;
}
else
{
rflg = 1 ;
a = bb;
b = aa;
y0 = 1 .0 - yy0;
}
x = 1 .0 L - x;
y = incbetl( a, b, x );
x0 = 0 .0 ;
yl = 0 .0 ;
x1 = 1 .0 ;
yh = 1 .0 ;
goto ihalve;
}
}
else
{
x1 = x;
if ( rflg == 1 && x1 < MACHEPL )
{
x = 0 .0 L;
goto done;
}
yh = y;
if ( dir > 0 )
{
dir = 0 ;
di = 0 .5 L;
}
else if ( dir < -3 )
di = di * di;
else if ( dir < -1 )
di = 0 .5 L * di;
else
di = (y - y0)/(yh - yl);
dir -= 1 ;
}
}
mtherr( "incbil" , PLOSS );
if ( x0 >= 1 .0 L )
{
x = 1 .0 L - MACHEPL;
goto done;
}
if ( x <= 0 .0 L )
{
under:
mtherr( "incbil" , UNDERFLOW );
x = 0 .0 L;
goto done;
}
newt:
if ( nflg )
goto done;
nflg = 1 ;
lgm = lgaml(a+b) - lgaml(a) - lgaml(b);
for ( i=0 ; i<15 ; i++ )
{
/* Compute the function at this point. */
if ( i != 0 )
y = incbetl(a,b,x);
if ( y < yl )
{
x = x0;
y = yl;
}
else if ( y > yh )
{
x = x1;
y = yh;
}
else if ( y < y0 )
{
x0 = x;
yl = y;
}
else
{
x1 = x;
yh = y;
}
if ( x == 1 .0 L || x == 0 .0 L )
break ;
/* Compute the derivative of the function at this point. */
d = (a - 1 .0 L) * logl(x) + (b - 1 .0 L) * logl(1 .0 L - x) + lgm;
if ( d < MINLOGL )
goto done;
if ( d > MAXLOGL )
break ;
d = expl(d);
/* Compute the step to the next approximation of x. */
d = (y - y0)/d;
xt = x - d;
if ( xt <= x0 )
{
y = (x - x0) / (x1 - x0);
xt = x0 + 0 .5 L * y * (x - x0);
if ( xt <= 0 .0 L )
break ;
}
if ( xt >= x1 )
{
y = (x1 - x) / (x1 - x0);
xt = x1 - 0 .5 L * y * (x1 - x);
if ( xt >= 1 .0 L )
break ;
}
x = xt;
if ( fabsl(d/x) < (128 .0 L * MACHEPL) )
goto done;
}
/* Did not converge. */
dithresh = 256 .0 L * MACHEPL;
goto ihalve;
done:
if ( rflg )
{
if ( x <= MACHEPL )
x = 1 .0 L - MACHEPL;
else
x = 1 .0 L - x;
}
return ( x );
}
Messung V0.5 in Prozent C=98 H=76 G=87
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-14)
¤
*© Formatika GbR, Deutschland