STK++ 1.0
Rmath.h
Go to the documentation of this file.
00001 /* -*- C -*-
00002  *  Mathlib : A C Library of Special Functions
00003  *  Copyright (C) 1998-2003  The R Development Core Team
00004  *  Copyright (C) 2004       The R Foundation
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU Lesser General Public License as published by
00008  *  the Free Software Foundation; either version 2.1 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU Lesser General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Lesser General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020 
00021  * Rmath.h  should contain ALL headers from R's C code in `src/nmath'
00022    -------  such that ``the Math library'' can be used by simply
00023 
00024    ``#include <Rmath.h> ''
00025 
00026    and nothing else.
00027 */
00028 #ifndef RMATH_H
00029 #define RMATH_H
00030 
00031 #ifdef  __cplusplus
00032 extern "C" {
00033 #endif
00034 
00035 /*-- Mathlib as part of R --  define this for standalone : */
00036 /* #undef MATHLIB_STANDALONE */
00037 
00038 #define R_VERSION_STRING "2.2.1"
00039 
00040 #ifndef HAVE_LOG1P
00041 # define HAVE_LOG1P 1
00042 #endif
00043 
00044 #ifndef HAVE_EXPM1
00045 # define HAVE_EXPM1 1
00046 #endif
00047 
00048 #ifndef HAVE_WORKING_LOG1P
00049 # define HAVE_WORKING_LOG1P 1
00050 #endif
00051 
00052 #ifndef HAVE_WORKING_LOG
00053 # define HAVE_WORKING_LOG 1
00054 #endif
00055 
00056 #include <errno.h>
00057 #include <limits.h>
00058 #include <float.h>
00059 #include <math.h>
00060 
00061 #if defined(HAVE_LOG1P) && !defined(HAVE_WORKING_LOG1P)
00062 /* remap to avoid problems with getting the right entry point */
00063 double  Rlog1p(double);
00064 #define log1p Rlog1p
00065 #endif
00066 
00067 #include <stdlib.h>
00068 
00069   /* Undo SGI Madness */
00070 
00071 #ifdef ftrunc
00072 # undef ftrunc
00073 #endif
00074 #ifdef qexp
00075 # undef qexp
00076 #endif
00077 #ifdef qgamma
00078 # undef qgamma
00079 #endif
00080 
00081 
00082 /* ----- The following constants and entry points are part of the R API ---- */
00083 
00084 /* 30 Decimal-place constants */
00085 /* Computed with bc -l (scale=32; proper round) */
00086 
00087 /* SVID & X/Open Constants */
00088 /* Names from Solaris math.h */
00089 
00090 #ifndef M_E
00091 #define M_E   2.718281828459045235360287471353  /* e */
00092 #endif
00093 
00094 #ifndef M_LOG2E
00095 #define M_LOG2E   1.442695040888963407359924681002  /* log2(e) */
00096 #endif
00097 
00098 #ifndef M_LOG10E
00099 #define M_LOG10E  0.434294481903251827651128918917  /* log10(e) */
00100 #endif
00101 
00102 #ifndef M_LN2
00103 #define M_LN2   0.693147180559945309417232121458  /* ln(2) */
00104 #endif
00105 
00106 #ifndef M_LN10
00107 #define M_LN10    2.302585092994045684017991454684  /* ln(10) */
00108 #endif
00109 
00110 #ifndef M_PI
00111 #define M_PI    3.141592653589793238462643383280  /* pi */
00112 #endif
00113 
00114 #ifndef M_2PI
00115 #define M_2PI   6.283185307179586476925286766559  /* 2*pi */
00116 #endif
00117 
00118 #ifndef M_PI_2
00119 #define M_PI_2    1.570796326794896619231321691640  /* pi/2 */
00120 #endif
00121 
00122 #ifndef M_PI_4
00123 #define M_PI_4    0.785398163397448309615660845820  /* pi/4 */
00124 #endif
00125 
00126 #ifndef M_1_PI
00127 #define M_1_PI    0.318309886183790671537767526745  /* 1/pi */
00128 #endif
00129 
00130 #ifndef M_2_PI
00131 #define M_2_PI    0.636619772367581343075535053490  /* 2/pi */
00132 #endif
00133 
00134 #ifndef M_2_SQRTPI
00135 #define M_2_SQRTPI  1.128379167095512573896158903122  /* 2/sqrt(pi) */
00136 #endif
00137 
00138 #ifndef M_SQRT2
00139 #define M_SQRT2   1.414213562373095048801688724210  /* sqrt(2) */
00140 #endif
00141 
00142 #ifndef M_SQRT1_2
00143 #define M_SQRT1_2 0.707106781186547524400844362105  /* 1/sqrt(2) */
00144 #endif
00145 
00146 /* R-Specific Constants */
00147 
00148 #ifndef M_SQRT_3
00149 #define M_SQRT_3  1.732050807568877293527446341506  /* sqrt(3) */
00150 #endif
00151 
00152 #ifndef M_SQRT_32
00153 #define M_SQRT_32 5.656854249492380195206754896838  /* sqrt(32) */
00154 #endif
00155 
00156 #ifndef M_LOG10_2
00157 #define M_LOG10_2 0.301029995663981195213738894724  /* log10(2) */
00158 #endif
00159 
00160 #ifndef M_SQRT_PI
00161 #define M_SQRT_PI 1.772453850905516027298167483341  /* sqrt(pi) */
00162 #endif
00163 
00164 #ifndef M_1_SQRT_2PI
00165 #define M_1_SQRT_2PI  0.398942280401432677939946059934  /* 1/sqrt(2pi) */
00166 #endif
00167 
00168 #ifndef M_SQRT_2dPI
00169 #define M_SQRT_2dPI 0.797884560802865355879892119869  /* sqrt(2/pi) */
00170 #endif
00171 
00172 
00173 #ifndef M_LN_SQRT_PI
00174 #define M_LN_SQRT_PI  0.572364942924700087071713675677  /* log(sqrt(pi)) */
00175 #endif
00176 
00177 #ifndef M_LN_SQRT_2PI
00178 #define M_LN_SQRT_2PI 0.918938533204672741780329736406  /* log(sqrt(2*pi)) */
00179 #endif
00180 
00181 #ifndef M_LN_SQRT_PId2
00182 #define M_LN_SQRT_PId2  0.225791352644727432363097614947  /* log(sqrt(pi/2)) */
00183 #endif
00184 
00185 
00186  #undef FALSE
00187  #undef TRUE
00188  typedef enum { FALSE = 0, TRUE } Rboolean;
00189 
00190 
00191 #define rround  fround
00192 #define prec  fprec
00193 #undef trunc
00194 #define trunc ftrunc
00195 
00196 
00197 /* log(1 - exp(x))  in stable form: */
00198 #define R_Log1_Exp(x)   ((x) > -M_LN2 ? log(-expm1(x)) : log1p(-exp(x)))
00199 
00200   /* R's versions with !R_FINITE checks */
00201 
00202 #define R_log log
00203 
00204 double R_pow(double x, double y);
00205 double R_pow_di(double, int);
00206 
00207   /* Random Number Generators */
00208 
00209 double  norm_rand(void);
00210 double  unif_rand(void);
00211 double  exp_rand(void);
00212 void  set_seed(unsigned int, unsigned int);
00213 void  get_seed(unsigned int *, unsigned int *);
00214 
00215   /* Normal Distribution */
00216 
00217 #define pnorm pnorm5
00218 #define qnorm qnorm5
00219 #define dnorm dnorm4
00220 
00221 double  dnorm(double, double, double, int);
00222 double  pnorm(double, double, double, int, int);
00223 double  qnorm(double, double, double, int, int);
00224 double  rnorm(double, double);
00225 void  pnorm_both(double, double *, double *, int, int);/* both tails */
00226 
00227   /* Uniform Distribution */
00228 
00229 double  dunif(double, double, double, int);
00230 double  punif(double, double, double, int, int);
00231 double  qunif(double, double, double, int, int);
00232 double  runif(double, double);
00233 
00234   /* Gamma Distribution */
00235 
00236 double  dgamma(double, double, double, int);
00237 double  pgamma(double, double, double, int, int);
00238 double  qgamma(double, double, double, int, int);
00239 double  rgamma(double, double);
00240 
00241 double  log1pmx(double);
00242 double  lgamma1p(double);
00243 double  logspace_add(double, double);
00244 double  logspace_sub(double, double);
00245 
00246   /* Beta Distribution */
00247 
00248 double  dbeta(double, double, double, int);
00249 double  pbeta(double, double, double, int, int);
00250 double  qbeta(double, double, double, int, int);
00251 double  rbeta(double, double);
00252 double  pbeta_raw(double, double, double, int);
00253 
00254   /* Lognormal Distribution */
00255 
00256 double  dlnorm(double, double, double, int);
00257 double  plnorm(double, double, double, int, int);
00258 double  qlnorm(double, double, double, int, int);
00259 double  rlnorm(double, double);
00260 
00261   /* Chi-squared Distribution */
00262 
00263 double  dchisq(double, double, int);
00264 double  pchisq(double, double, int, int);
00265 double  qchisq(double, double, int, int);
00266 double  rchisq(double);
00267 double  qchisq_appr(double, double, double, int, int, double tol);
00268 
00269   /* Non-central Chi-squared Distribution */
00270 
00271 double  dnchisq(double, double, double, int);
00272 double  pnchisq(double, double, double, int, int);
00273 double  qnchisq(double, double, double, int, int);
00274 double  rnchisq(double, double);
00275 
00276   /* F Distibution */
00277 
00278 double  df(double, double, double, int);
00279 double  pf(double, double, double, int, int);
00280 double  qf(double, double, double, int, int);
00281 double  rf(double, double);
00282 
00283   /* Student t Distibution */
00284 
00285 double  dt(double, double, int);
00286 double  pt(double, double, int, int);
00287 double  qt(double, double, int, int);
00288 double  rt(double);
00289 
00290   /* Binomial Distribution */
00291 
00292 double  dbinom(double, double, double, int);
00293 double  pbinom(double, double, double, int, int);
00294 double  qbinom(double, double, double, int, int);
00295 double  rbinom(double, double);
00296 
00297   /* Multnomial Distribution */
00298 
00299 void  rmultinom(int, double*, int, int*);
00300 
00301   /* Cauchy Distribution */
00302 
00303 double  dcauchy(double, double, double, int);
00304 double  pcauchy(double, double, double, int, int);
00305 double  qcauchy(double, double, double, int, int);
00306 double  rcauchy(double, double);
00307 
00308   /* Exponential Distribution */
00309 
00310 double  dexp(double, double, int);
00311 double  pexp(double, double, int, int);
00312 double  qexp(double, double, int, int);
00313 double  rexp(double);
00314 
00315   /* Geometric Distribution */
00316 
00317 double  dgeom(double, double, int);
00318 double  pgeom(double, double, int, int);
00319 double  qgeom(double, double, int, int);
00320 double  rgeom(double);
00321 
00322   /* Hypergeometric Distibution */
00323 
00324 double  dhyper(double, double, double, double, int);
00325 double  phyper(double, double, double, double, int, int);
00326 double  qhyper(double, double, double, double, int, int);
00327 double  rhyper(double, double, double);
00328 
00329   /* Negative Binomial Distribution */
00330 
00331 double  dnbinom(double, double, double, int);
00332 double  pnbinom(double, double, double, int, int);
00333 double  qnbinom(double, double, double, int, int);
00334 double  rnbinom(double, double);
00335 
00336   /* Poisson Distribution */
00337 
00338 double  dpois(double, double, int);
00339 double  ppois(double, double, int, int);
00340 double  qpois(double, double, int, int);
00341 double  rpois(double);
00342 
00343   /* Weibull Distribution */
00344 
00345 double  dweibull(double, double, double, int);
00346 double  pweibull(double, double, double, int, int);
00347 double  qweibull(double, double, double, int, int);
00348 double  rweibull(double, double);
00349 
00350   /* Logistic Distribution */
00351 
00352 double  dlogis(double, double, double, int);
00353 double  plogis(double, double, double, int, int);
00354 double  qlogis(double, double, double, int, int);
00355 double  rlogis(double, double);
00356 
00357   /* Non-central Beta Distribution */
00358 
00359 double  dnbeta(double, double, double, double, int);
00360 double  pnbeta(double, double, double, double, int, int);
00361 double  qnbeta(double, double, double, double, int, int);
00362 double  rnbeta(double, double, double);
00363 
00364   /* Non-central F Distribution */
00365 
00366 double  pnf(double, double, double, double, int, int);
00367 double  qnf(double, double, double, double, int, int);
00368 
00369   /* Non-central Student t Distribution */
00370 
00371 double  dnt(double, double, double, int);
00372 double  pnt(double, double, double, int, int);
00373 double  qnt(double, double, double, int, int);
00374 
00375   /* Studentized Range Distribution */
00376 
00377 double  ptukey(double, double, double, double, int, int);
00378 double  qtukey(double, double, double, double, int, int);
00379 
00380   /* Wilcoxon Rank Sum Distribution */
00381 
00382 double dwilcox(double, double, double, int);
00383 double pwilcox(double, double, double, int, int);
00384 double qwilcox(double, double, double, int, int);
00385 double rwilcox(double, double);
00386 
00387   /* Wilcoxon Signed Rank Distribution */
00388 
00389 double dsignrank(double, double, int);
00390 double psignrank(double, double, int, int);
00391 double qsignrank(double, double, int, int);
00392 double rsignrank(double);
00393 
00394   /* Gamma and Related Functions */
00395 double  gammafn(double);
00396 double  lgammafn(double);
00397 void    dpsifn(double, int, int, int, double*, int*, int*);
00398 double  psigamma(double, double);
00399 double  digamma(double);
00400 double  trigamma(double);
00401 double  tetragamma(double);
00402 double  pentagamma(double);
00403 
00404 double  beta(double, double);
00405 double  lbeta(double, double);
00406 
00407 double  choose(double, double);
00408 double  lchoose(double, double);
00409 
00410   /* Bessel Functions */
00411 
00412 double  bessel_i(double, double, double);
00413 double  bessel_j(double, double);
00414 double  bessel_k(double, double, double);
00415 double  bessel_y(double, double);
00416 
00417 
00418   /* General Support Functions */
00419 
00420 double  pythag(double, double);
00421 double  expm1(double); /* = exp(x)-1 {care for small x} */
00422 double  log1p(double); /* = log(1+x) {care for small x} */
00423 int imax2(int, int);
00424 int imin2(int, int);
00425 double  fmax2(double, double);
00426 double  fmin2(double, double);
00427 double  sign(double);
00428 double  fprec(double, double);
00429 double  fround(double, double);
00430 double  fsign(double, double);
00431 double  ftrunc(double);
00432 
00433 double  log1pmx(double); /* Accurate log(1+x) - x, {care for small x} */
00434 double  lgamma1p(double);/* accurate log(gamma(x+1)), small x (0 < x < 0.5) */
00435 
00436 /* Compute the log of a sum or difference from logs of terms, i.e.,
00437  *
00438  *     log (exp (logx) + exp (logy))
00439  * or  log (exp (logx) - exp (logy))
00440  *
00441  * without causing overflows or throwing away too much accuracy:
00442  */
00443 double  logspace_add(double logx, double logy);
00444 double  logspace_sub(double logx, double logy);
00445 
00446 
00447 
00448 
00449 /* ----------------- Private part of the header file ------------------- */
00450 
00451   /* old-R Compatibility */
00452 
00453 #define snorm norm_rand
00454 #define sunif unif_rand
00455 #define sexp  exp_rand
00456 
00457 #ifdef MATHLIB_PRIVATE
00458 #define d1mach    Rf_d1mach
00459 #define gamma_cody  Rf_gamma_cody
00460 
00461 double  d1mach(int);
00462 double  gamma_cody(double);
00463 
00464 #endif /* MATHLIB_PRIVATE */
00465 
00466 #ifndef MATHLIB_PRIVATE_H
00467 
00468 /* If isnan is a macro, as C99 specifies, the C++
00469    math header will undefine it. This happens on OS X */
00470 #ifdef __cplusplus
00471   int R_isnancpp(double); /* in mlutils.c */
00472 #  define ISNAN(x)     R_isnancpp(x)
00473 #else
00474 #  define ISNAN(x)     (isnan(x)!=0)
00475 #endif
00476 
00477 
00478 /* We don't have config information available to do anything else */
00479 #define R_FINITE(x)    R_finite(x)
00480 int R_finite(double);
00481 
00482 #endif /* not MATHLIB_PRIVATE_H */
00483 
00484 #ifndef R_EXT_PRINT_H_
00485 void REprintf(char*, ...);
00486 #endif
00487 
00488 #ifdef  __cplusplus
00489 }
00490 #endif
00491 
00492 #endif /* RMATH_H */