CXXR (C++ R) API
Rmath.h
1 /* -*- C -*-
2  * Mathlib : A C Library of Special Functions
3  * Copyright (C) 1998-2011 The R Core Team
4  * Copyright (C) 2004 The R Foundation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, a copy is available at
18  * http://www.r-project.org/Licenses/
19  *
20 
21  * Rmath.h should contain ALL headers from R's C code in `src/nmath'
22  ------- such that ``the Math library'' can be used by simply
23 
24  ``#include <Rmath.h> ''
25 
26  and nothing else.
27 */
28 #ifndef RMATH_H
29 #define RMATH_H
30 
31 /* Note that on some systems we need to include math.h before the
32  defines below, to avoid redefining ftrunc */
33 #ifndef NO_C_HEADERS
34 # include <math.h>
35 #endif
36 
37 /*-- Mathlib as part of R -- define this for standalone : */
38 /* #undef MATHLIB_STANDALONE */
39 
40 #define R_VERSION_STRING "2.15.1"
41 
42 #ifndef HAVE_EXPM1
43 # define HAVE_EXPM1 1
44 #endif
45 
46 #ifndef HAVE_HYPOT
47 # define HAVE_HYPOT 1
48 #endif
49 
50 #ifndef HAVE_LOG1P
51 # define HAVE_LOG1P 1
52 #endif
53 
54 #ifndef HAVE_WORKING_LOG1P
55 # define HAVE_WORKING_LOG1P 1
56 #endif
57 
58 #if defined(HAVE_LOG1P) && !defined(HAVE_WORKING_LOG1P)
59 /* remap to avoid problems with getting the right entry point */
60 double Rlog1p(double);
61 #define log1p Rlog1p
62 #endif
63 
64 
65  /* Undo SGI Madness */
66 
67 #ifdef ftrunc
68 # undef ftrunc
69 #endif
70 #ifdef qexp
71 # undef qexp
72 #endif
73 #ifdef qgamma
74 # undef qgamma
75 #endif
76 
77 
78 /* ----- The following constants and entry points are part of the R API ---- */
79 
80 /* 30 Decimal-place constants */
81 /* Computed with bc -l (scale=32; proper round) */
82 
83 /* SVID & X/Open Constants */
84 /* Names from Solaris math.h */
85 
86 #ifndef M_E
87 #define M_E 2.718281828459045235360287471353 /* e */
88 #endif
89 
90 #ifndef M_LOG2E
91 #define M_LOG2E 1.442695040888963407359924681002 /* log2(e) */
92 #endif
93 
94 #ifndef M_LOG10E
95 #define M_LOG10E 0.434294481903251827651128918917 /* log10(e) */
96 #endif
97 
98 #ifndef M_LN2
99 #define M_LN2 0.693147180559945309417232121458 /* ln(2) */
100 #endif
101 
102 #ifndef M_LN10
103 #define M_LN10 2.302585092994045684017991454684 /* ln(10) */
104 #endif
105 
106 #ifndef M_PI
107 #define M_PI 3.141592653589793238462643383280 /* pi */
108 #endif
109 
110 #ifndef M_2PI
111 #define M_2PI 6.283185307179586476925286766559 /* 2*pi */
112 #endif
113 
114 #ifndef M_PI_2
115 #define M_PI_2 1.570796326794896619231321691640 /* pi/2 */
116 #endif
117 
118 #ifndef M_PI_4
119 #define M_PI_4 0.785398163397448309615660845820 /* pi/4 */
120 #endif
121 
122 #ifndef M_1_PI
123 #define M_1_PI 0.318309886183790671537767526745 /* 1/pi */
124 #endif
125 
126 #ifndef M_2_PI
127 #define M_2_PI 0.636619772367581343075535053490 /* 2/pi */
128 #endif
129 
130 #ifndef M_2_SQRTPI
131 #define M_2_SQRTPI 1.128379167095512573896158903122 /* 2/sqrt(pi) */
132 #endif
133 
134 #ifndef M_SQRT2
135 #define M_SQRT2 1.414213562373095048801688724210 /* sqrt(2) */
136 #endif
137 
138 #ifndef M_SQRT1_2
139 #define M_SQRT1_2 0.707106781186547524400844362105 /* 1/sqrt(2) */
140 #endif
141 
142 /* R-Specific Constants */
143 
144 #ifndef M_SQRT_3
145 #define M_SQRT_3 1.732050807568877293527446341506 /* sqrt(3) */
146 #endif
147 
148 #ifndef M_SQRT_32
149 #define M_SQRT_32 5.656854249492380195206754896838 /* sqrt(32) */
150 #endif
151 
152 #ifndef M_LOG10_2
153 #define M_LOG10_2 0.301029995663981195213738894724 /* log10(2) */
154 #endif
155 
156 #ifndef M_SQRT_PI
157 #define M_SQRT_PI 1.772453850905516027298167483341 /* sqrt(pi) */
158 #endif
159 
160 #ifndef M_1_SQRT_2PI
161 #define M_1_SQRT_2PI 0.398942280401432677939946059934 /* 1/sqrt(2pi) */
162 #endif
163 
164 #ifndef M_SQRT_2dPI
165 #define M_SQRT_2dPI 0.797884560802865355879892119869 /* sqrt(2/pi) */
166 #endif
167 
168 
169 #ifndef M_LN_SQRT_PI
170 #define M_LN_SQRT_PI 0.572364942924700087071713675677 /* log(sqrt(pi))
171  == log(pi)/2 */
172 #endif
173 
174 #ifndef M_LN_SQRT_2PI
175 #define M_LN_SQRT_2PI 0.918938533204672741780329736406 /* log(sqrt(2*pi))
176  == log(2*pi)/2 */
177 #endif
178 
179 #ifndef M_LN_SQRT_PId2
180 #define M_LN_SQRT_PId2 0.225791352644727432363097614947 /* log(sqrt(pi/2)) */
181 #endif
182 
183 
184 #ifdef MATHLIB_STANDALONE
185 # ifndef R_EXT_BOOLEAN_H_
186 /* "copy-paste" R_ext/Boolean.h if not already included: */
187  #define R_EXT_BOOLEAN_H_
188  #undef FALSE
189  #undef TRUE
190  typedef enum { FALSE = 0, TRUE } Rboolean;
191 # endif
192 #else
193 # include <R_ext/Boolean.h>
194 #endif
195 
196 
197 #ifndef MATHLIB_STANDALONE
198 #define bessel_i Rf_bessel_i
199 #define bessel_j Rf_bessel_j
200 #define bessel_k Rf_bessel_k
201 #define bessel_y Rf_bessel_y
202 #define bessel_i_ex Rf_bessel_i_ex
203 #define bessel_j_ex Rf_bessel_j_ex
204 #define bessel_k_ex Rf_bessel_k_ex
205 #define bessel_y_ex Rf_bessel_y_ex
206 #define beta Rf_beta
207 #define choose Rf_choose
208 #define dbeta Rf_dbeta
209 #define dbinom Rf_dbinom
210 #define dcauchy Rf_dcauchy
211 #define dchisq Rf_dchisq
212 #define dexp Rf_dexp
213 #define df Rf_df
214 #define dgamma Rf_dgamma
215 #define dgeom Rf_dgeom
216 #define dhyper Rf_dhyper
217 #define digamma Rf_digamma
218 #define dlnorm Rf_dlnorm
219 #define dlogis Rf_dlogis
220 #define dnbeta Rf_dnbeta
221 #define dnbinom Rf_dnbinom
222 #define dnchisq Rf_dnchisq
223 #define dnf Rf_dnf
224 #define dnorm4 Rf_dnorm4
225 #define dnt Rf_dnt
226 #define dpois Rf_dpois
227 #define dpsifn Rf_dpsifn
228 #define dsignrank Rf_dsignrank
229 #define dt Rf_dt
230 #define dtukey Rf_dtukey
231 #define dunif Rf_dunif
232 #define dweibull Rf_dweibull
233 #define dwilcox Rf_dwilcox
234 #define fmax2 Rf_fmax2
235 #define fmin2 Rf_fmin2
236 #define fprec Rf_fprec
237 #define fround Rf_fround
238 #define ftrunc Rf_ftrunc
239 #define fsign Rf_fsign
240 #define gammafn Rf_gammafn
241 #define imax2 Rf_imax2
242 #define imin2 Rf_imin2
243 #define lbeta Rf_lbeta
244 #define lchoose Rf_lchoose
245 #define lgammafn Rf_lgammafn
246 #define lgammafn_sign Rf_lgammafn_sign
247 #define lgamma1p Rf_lgamma1p
248 #define log1pmx Rf_log1pmx
249 #define logspace_add Rf_logspace_add
250 #define logspace_sub Rf_logspace_sub
251 #define pbeta Rf_pbeta
252 #define pbeta_raw Rf_pbeta_raw
253 #define pbinom Rf_pbinom
254 #define pcauchy Rf_pcauchy
255 #define pchisq Rf_pchisq
256 #define pentagamma Rf_pentagamma
257 #define pexp Rf_pexp
258 #define pf Rf_pf
259 #define pgamma Rf_pgamma
260 #define pgeom Rf_pgeom
261 #define phyper Rf_phyper
262 #define plnorm Rf_plnorm
263 #define plogis Rf_plogis
264 #define pnbeta Rf_pnbeta
265 #define pnbinom Rf_pnbinom
266 #define pnchisq Rf_pnchisq
267 #define pnf Rf_pnf
268 #define pnorm5 Rf_pnorm5
269 #define pnorm_both Rf_pnorm_both
270 #define pnt Rf_pnt
271 #define ppois Rf_ppois
272 #define psignrank Rf_psignrank
273 #define psigamma Rf_psigamma
274 #define pt Rf_pt
275 #define ptukey Rf_ptukey
276 #define punif Rf_punif
277 #define pythag Rf_pythag
278 #define pweibull Rf_pweibull
279 #define pwilcox Rf_pwilcox
280 #define qbeta Rf_qbeta
281 #define qbinom Rf_qbinom
282 #define qcauchy Rf_qcauchy
283 #define qchisq Rf_qchisq
284 #define qchisq_appr Rf_qchisq_appr
285 #define qexp Rf_qexp
286 #define qf Rf_qf
287 #define qgamma Rf_qgamma
288 #define qgeom Rf_qgeom
289 #define qhyper Rf_qhyper
290 #define qlnorm Rf_qlnorm
291 #define qlogis Rf_qlogis
292 #define qnbeta Rf_qnbeta
293 #define qnbinom Rf_qnbinom
294 #define qnchisq Rf_qnchisq
295 #define qnf Rf_qnf
296 #define qnorm5 Rf_qnorm5
297 #define qnt Rf_qnt
298 #define qpois Rf_qpois
299 #define qsignrank Rf_qsignrank
300 #define qt Rf_qt
301 #define qtukey Rf_qtukey
302 #define qunif Rf_qunif
303 #define qweibull Rf_qweibull
304 #define qwilcox Rf_qwilcox
305 #define rbeta Rf_rbeta
306 #define rbinom Rf_rbinom
307 #define rcauchy Rf_rcauchy
308 #define rchisq Rf_rchisq
309 #define rexp Rf_rexp
310 #define rf Rf_rf
311 #define rgamma Rf_rgamma
312 #define rgeom Rf_rgeom
313 #define rhyper Rf_rhyper
314 #define rlnorm Rf_rlnorm
315 #define rlogis Rf_rlogis
316 #define rnbeta Rf_rnbeta
317 #define rnbinom Rf_rnbinom
318 #define rnchisq Rf_rnchisq
319 #define rnf Rf_rnf
320 #define rnorm Rf_rnorm
321 #define rnt Rf_rnt
322 #define rpois Rf_rpois
323 #define rsignrank Rf_rsignrank
324 #define rt Rf_rt
325 #define rtukey Rf_rtukey
326 #define runif Rf_runif
327 #define rweibull Rf_rweibull
328 #define rwilcox Rf_rwilcox
329 #define sign Rf_sign
330 #define tetragamma Rf_tetragamma
331 #define trigamma Rf_trigamma
332 #endif
333 
334 #define rround fround
335 #define prec fprec
336 #undef trunc
337 #define trunc ftrunc
338 
339 #ifdef __cplusplus
340 extern "C" {
341 #endif
342  /* R's versions with !R_FINITE checks */
343 
344 double R_pow(double x, double y);
345 double R_pow_di(double, int);
346 
347  /* Random Number Generators */
348 
349 double norm_rand(void);
350 double unif_rand(void);
351 double exp_rand(void);
352 #ifdef MATHLIB_STANDALONE
353 void set_seed(unsigned int, unsigned int);
354 void get_seed(unsigned int *, unsigned int *);
355 #endif
356 
357  /* Normal Distribution */
358 
359 #define pnorm pnorm5
360 #define qnorm qnorm5
361 #define dnorm dnorm4
362 
363 double dnorm(double, double, double, int);
364 double pnorm(double, double, double, int, int);
365 double qnorm(double, double, double, int, int);
366 double rnorm(double, double);
367 void pnorm_both(double, double *, double *, int, int);/* both tails */
368 
369  /* Uniform Distribution */
370 
371 double dunif(double, double, double, int);
372 double punif(double, double, double, int, int);
373 double qunif(double, double, double, int, int);
374 double runif(double, double);
375 
376  /* Gamma Distribution */
377 
378 double dgamma(double, double, double, int);
379 double pgamma(double, double, double, int, int);
380 double qgamma(double, double, double, int, int);
381 double rgamma(double, double);
382 
383 double log1pmx(double);
384 double log1pexp(double); // <-- ../nmath/plogis.c
385 double lgamma1p(double);
386 double logspace_add(double, double);
387 double logspace_sub(double, double);
388 
389  /* Beta Distribution */
390 
391 double dbeta(double, double, double, int);
392 double pbeta(double, double, double, int, int);
393 double qbeta(double, double, double, int, int);
394 double rbeta(double, double);
395 
396  /* Lognormal Distribution */
397 
398 double dlnorm(double, double, double, int);
399 double plnorm(double, double, double, int, int);
400 double qlnorm(double, double, double, int, int);
401 double rlnorm(double, double);
402 
403  /* Chi-squared Distribution */
404 
405 double dchisq(double, double, int);
406 double pchisq(double, double, int, int);
407 double qchisq(double, double, int, int);
408 double rchisq(double);
409 
410  /* Non-central Chi-squared Distribution */
411 
412 double dnchisq(double, double, double, int);
413 double pnchisq(double, double, double, int, int);
414 double qnchisq(double, double, double, int, int);
415 double rnchisq(double, double);
416 
417  /* F Distibution */
418 
419 double df(double, double, double, int);
420 double pf(double, double, double, int, int);
421 double qf(double, double, double, int, int);
422 double rf(double, double);
423 
424  /* Student t Distibution */
425 
426 double dt(double, double, int);
427 double pt(double, double, int, int);
428 double qt(double, double, int, int);
429 double rt(double);
430 
431  /* Binomial Distribution */
432 
433 double dbinom(double, double, double, int);
434 double pbinom(double, double, double, int, int);
435 double qbinom(double, double, double, int, int);
436 double rbinom(double, double);
437 
438  /* Multnomial Distribution */
439 
440 void rmultinom(int, double*, int, int*);
441 
442  /* Cauchy Distribution */
443 
444 double dcauchy(double, double, double, int);
445 double pcauchy(double, double, double, int, int);
446 double qcauchy(double, double, double, int, int);
447 double rcauchy(double, double);
448 
449  /* Exponential Distribution */
450 
451 double dexp(double, double, int);
452 double pexp(double, double, int, int);
453 double qexp(double, double, int, int);
454 double rexp(double);
455 
456  /* Geometric Distribution */
457 
458 double dgeom(double, double, int);
459 double pgeom(double, double, int, int);
460 double qgeom(double, double, int, int);
461 double rgeom(double);
462 
463  /* Hypergeometric Distibution */
464 
465 double dhyper(double, double, double, double, int);
466 double phyper(double, double, double, double, int, int);
467 double qhyper(double, double, double, double, int, int);
468 double rhyper(double, double, double);
469 
470  /* Negative Binomial Distribution */
471 
472 double dnbinom(double, double, double, int);
473 double pnbinom(double, double, double, int, int);
474 double qnbinom(double, double, double, int, int);
475 double rnbinom(double, double);
476 
477 double dnbinom_mu(double, double, double, int);
478 double pnbinom_mu(double, double, double, int, int);
479 double qnbinom_mu(double, double, double, int, int);
480 double rnbinom_mu(double, double);
481 
482  /* Poisson Distribution */
483 
484 double dpois(double, double, int);
485 double ppois(double, double, int, int);
486 double qpois(double, double, int, int);
487 double rpois(double);
488 
489  /* Weibull Distribution */
490 
491 double dweibull(double, double, double, int);
492 double pweibull(double, double, double, int, int);
493 double qweibull(double, double, double, int, int);
494 double rweibull(double, double);
495 
496  /* Logistic Distribution */
497 
498 double dlogis(double, double, double, int);
499 double plogis(double, double, double, int, int);
500 double qlogis(double, double, double, int, int);
501 double rlogis(double, double);
502 
503  /* Non-central Beta Distribution */
504 
505 double dnbeta(double, double, double, double, int);
506 double pnbeta(double, double, double, double, int, int);
507 double qnbeta(double, double, double, double, int, int);
508 double rnbeta(double, double, double);
509 
510  /* Non-central F Distribution */
511 
512 double dnf(double, double, double, double, int);
513 double pnf(double, double, double, double, int, int);
514 double qnf(double, double, double, double, int, int);
515 
516  /* Non-central Student t Distribution */
517 
518 double dnt(double, double, double, int);
519 double pnt(double, double, double, int, int);
520 double qnt(double, double, double, int, int);
521 
522  /* Studentized Range Distribution */
523 
524 double ptukey(double, double, double, double, int, int);
525 double qtukey(double, double, double, double, int, int);
526 
527  /* Wilcoxon Rank Sum Distribution */
528 
529 double dwilcox(double, double, double, int);
530 double pwilcox(double, double, double, int, int);
531 double qwilcox(double, double, double, int, int);
532 double rwilcox(double, double);
533 
534  /* Wilcoxon Signed Rank Distribution */
535 
536 double dsignrank(double, double, int);
537 double psignrank(double, double, int, int);
538 double qsignrank(double, double, int, int);
539 double rsignrank(double);
540 
541  /* Gamma and Related Functions */
542 double gammafn(double);
543 double lgammafn(double);
544 double lgammafn_sign(double, int*);
545 void dpsifn(double, int, int, int, double*, int*, int*);
546 double psigamma(double, double);
547 double digamma(double);
548 double trigamma(double);
549 double tetragamma(double);
550 double pentagamma(double);
551 
552 double beta(double, double);
553 double lbeta(double, double);
554 
555 double choose(double, double);
556 double lchoose(double, double);
557 
558  /* Bessel Functions */
559 
560 double bessel_i(double, double, double);
561 double bessel_j(double, double);
562 double bessel_k(double, double, double);
563 double bessel_y(double, double);
564 double bessel_i_ex(double, double, double, double *);
565 double bessel_j_ex(double, double, double *);
566 double bessel_k_ex(double, double, double, double *);
567 double bessel_y_ex(double, double, double *);
568 
569 
570  /* General Support Functions */
571 
572 #ifndef HAVE_HYPOT
573 double hypot(double, double);
574 #endif
575 double pythag(double, double);
576 #ifndef HAVE_EXPM1
577 double expm1(double); /* = exp(x)-1 {care for small x} */
578 #endif
579 #ifndef HAVE_LOG1P
580 double log1p(double); /* = log(1+x) {care for small x} */
581 #endif
582 int imax2(int, int);
583 int imin2(int, int);
584 double fmax2(double, double);
585 double fmin2(double, double);
586 double sign(double);
587 double fprec(double, double);
588 double fround(double, double);
589 double fsign(double, double);
590 double ftrunc(double);
591 
592 double log1pmx(double); /* Accurate log(1+x) - x, {care for small x} */
593 double lgamma1p(double);/* accurate log(gamma(x+1)), small x (0 < x < 0.5) */
594 
595 /* Compute the log of a sum or difference from logs of terms, i.e.,
596  *
597  * log (exp (logx) + exp (logy))
598  * or log (exp (logx) - exp (logy))
599  *
600  * without causing overflows or throwing away too much accuracy:
601  */
602 double logspace_add(double logx, double logy);
603 double logspace_sub(double logx, double logy);
604 
605 
606 
607 
608 /* ----------------- Private part of the header file ------------------- */
609 
610  /* old-R Compatibility */
611 
612 #ifdef OLD_RMATH_COMPAT
613 # define snorm norm_rand
614 # define sunif unif_rand
615 # define sexp exp_rand
616 #endif
617 
618 #if defined(MATHLIB_STANDALONE) && !defined(MATHLIB_PRIVATE_H)
619 /* second is defined by nmath.h */
620 
621 /* If isnan is a macro, as C99 specifies, the C++
622  math header will undefine it. This happens on OS X */
623 # ifdef __cplusplus
624  int R_isnancpp(double); /* in mlutils.c */
625 # define ISNAN(x) R_isnancpp(x)
626 # else
627 # define ISNAN(x) (isnan(x)!=0)
628 # endif
629 
630 # define R_FINITE(x) R_finite(x)
631 int R_finite(double);
632 
633 # ifdef WIN32 /* not Win32 as no config information */
634 # ifdef RMATH_DLL
635 # define R_EXTERN extern __declspec(dllimport)
636 # else
637 # define R_EXTERN extern
638 # endif
639 R_EXTERN double NA_REAL;
640 R_EXTERN double R_PosInf;
641 R_EXTERN double R_NegInf;
642 R_EXTERN int N01_kind;
643 # undef R_EXTERN
644 #else
645 extern int N01_kind;
646 # endif
647 
648 #endif /* MATHLIB_STANDALONE */
649 
650 #ifdef __cplusplus
651 }
652 #endif
653 
654 #endif /* RMATH_H */