2005-07-06 13:47:08 +04:00
|
|
|
/* erf.c - public domain implementation of error function erf(3m)
|
2003-06-05 13:38:01 +04:00
|
|
|
|
2005-07-06 13:47:08 +04:00
|
|
|
reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten
|
|
|
|
(New Algorithm handbook in C language) (Gijyutsu hyouron
|
|
|
|
sha, Tokyo, 1991) p.227 [in Japanese] */
|
2010-07-28 12:12:01 +04:00
|
|
|
#include "ruby/missing.h"
|
2005-07-01 10:52:37 +04:00
|
|
|
#include <stdio.h>
|
2005-07-06 13:47:08 +04:00
|
|
|
#include <math.h>
|
2005-07-01 10:37:48 +04:00
|
|
|
|
2005-07-06 13:47:08 +04:00
|
|
|
static double q_gamma(double, double, double);
|
2005-07-01 10:37:48 +04:00
|
|
|
|
2005-07-06 13:47:08 +04:00
|
|
|
/* Incomplete gamma function
|
|
|
|
1 / Gamma(a) * Int_0^x exp(-t) t^(a-1) dt */
|
2005-10-22 05:28:00 +04:00
|
|
|
static double p_gamma(double a, double x, double loggamma_a)
|
2005-07-01 10:37:48 +04:00
|
|
|
{
|
2005-07-06 13:47:08 +04:00
|
|
|
int k;
|
|
|
|
double result, term, previous;
|
|
|
|
|
|
|
|
if (x >= 1 + a) return 1 - q_gamma(a, x, loggamma_a);
|
|
|
|
if (x == 0) return 0;
|
|
|
|
result = term = exp(a * log(x) - x - loggamma_a) / a;
|
|
|
|
for (k = 1; k < 1000; k++) {
|
|
|
|
term *= x / (a + k);
|
|
|
|
previous = result; result += term;
|
|
|
|
if (result == previous) return result;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "erf.c:%d:p_gamma() could not converge.", __LINE__);
|
|
|
|
return result;
|
2005-07-01 10:37:48 +04:00
|
|
|
}
|
|
|
|
|
2005-07-06 13:47:08 +04:00
|
|
|
/* Incomplete gamma function
|
|
|
|
1 / Gamma(a) * Int_x^inf exp(-t) t^(a-1) dt */
|
2005-10-22 05:28:00 +04:00
|
|
|
static double q_gamma(double a, double x, double loggamma_a)
|
2005-07-06 13:47:08 +04:00
|
|
|
{
|
|
|
|
int k;
|
|
|
|
double result, w, temp, previous;
|
|
|
|
double la = 1, lb = 1 + x - a; /* Laguerre polynomial */
|
|
|
|
|
|
|
|
if (x < 1 + a) return 1 - p_gamma(a, x, loggamma_a);
|
|
|
|
w = exp(a * log(x) - x - loggamma_a);
|
|
|
|
result = w / lb;
|
|
|
|
for (k = 2; k < 1000; k++) {
|
|
|
|
temp = ((k - 1 - a) * (lb - la) + (k + x) * lb) / k;
|
|
|
|
la = lb; lb = temp;
|
|
|
|
w *= (k - 1 - a) / k;
|
|
|
|
temp = w / (la * lb);
|
|
|
|
previous = result; result += temp;
|
|
|
|
if (result == previous) return result;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "erf.c:%d:q_gamma() could not converge.", __LINE__);
|
|
|
|
return result;
|
|
|
|
}
|
2003-06-05 13:38:01 +04:00
|
|
|
|
2005-07-06 13:47:08 +04:00
|
|
|
#define LOG_PI_OVER_2 0.572364942924700087071713675675 /* log_e(PI)/2 */
|
2003-06-05 13:38:01 +04:00
|
|
|
|
2005-10-22 05:28:00 +04:00
|
|
|
double erf(double x)
|
2003-06-05 13:38:01 +04:00
|
|
|
{
|
2005-07-06 13:47:08 +04:00
|
|
|
if (!finite(x)) {
|
|
|
|
if (isnan(x)) return x; /* erf(NaN) = NaN */
|
|
|
|
return (x>0 ? 1.0 : -1.0); /* erf(+-inf) = +-1.0 */
|
|
|
|
}
|
|
|
|
if (x >= 0) return p_gamma(0.5, x * x, LOG_PI_OVER_2);
|
|
|
|
else return - p_gamma(0.5, x * x, LOG_PI_OVER_2);
|
2003-06-05 13:38:01 +04:00
|
|
|
}
|
|
|
|
|
2005-10-22 05:28:00 +04:00
|
|
|
double erfc(double x)
|
2003-06-05 13:38:01 +04:00
|
|
|
{
|
2005-07-06 13:47:08 +04:00
|
|
|
if (!finite(x)) {
|
|
|
|
if (isnan(x)) return x; /* erfc(NaN) = NaN */
|
|
|
|
return (x>0 ? 0.0 : 2.0); /* erfc(+-inf) = 0.0, 2.0 */
|
|
|
|
}
|
|
|
|
if (x >= 0) return q_gamma(0.5, x * x, LOG_PI_OVER_2);
|
|
|
|
else return 1 + p_gamma(0.5, x * x, LOG_PI_OVER_2);
|
2003-06-05 13:38:01 +04:00
|
|
|
}
|