TMB Documentation  v1.9.11
nmath.h
1 /*
2  * Mathlib : A C Library of Special Functions
3  * Copyright (C) 1998-2016 The R Core Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, a copy is available at
17  * https://www.R-project.org/Licenses/
18  */
19 
20 /* Private header file for use during compilation of Mathlib */
21 #ifndef MATHLIB_PRIVATE_H
22 #define MATHLIB_PRIVATE_H
23 
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27 
28 /* Required by C99 but might be slow */
29 #ifdef HAVE_LONG_DOUBLE
30 # define LDOUBLE long double
31 #else
32 # define LDOUBLE double
33 #endif
34 
35 /* To ensure atanpi, cospi, sinpi, tanpi are defined */
36 # ifndef __STDC_WANT_IEC_60559_FUNCS_EXT__
37 # define __STDC_WANT_IEC_60559_FUNCS_EXT__ 1
38 # endif
39 
40 #include <math.h>
41 #include <float.h> /* DBL_MIN etc */
42 
43 #include <Rconfig.h>
44 #include <Rmath.h>
45 
46 /* Used internally only */
47 double Rf_d1mach(int);
48 double Rf_gamma_cody(double);
49 
50 #include <R_ext/RS.h>
51 
52 /* possibly needed for debugging */
53 #include <R_ext/Print.h>
54 
55 /* moved from dpq.h */
56 #ifdef HAVE_NEARYINT
57 # define R_forceint(x) nearbyint()
58 #else
59 # define R_forceint(x) round(x)
60 #endif
61 //R >= 3.1.0: # define R_nonint(x) (fabs((x) - R_forceint(x)) > 1e-7)
62 # define R_nonint(x) (fabs((x) - R_forceint(x)) > 1e-7*fmax2(1., fabs(x)))
63 
64 #ifndef MATHLIB_STANDALONE
65 
66 #include <R_ext/Error.h>
67 # define MATHLIB_ERROR(fmt,x) error(fmt,x);
68 # define MATHLIB_WARNING(fmt,x) warning(fmt,x)
69 # define MATHLIB_WARNING2(fmt,x,x2) warning(fmt,x,x2)
70 # define MATHLIB_WARNING3(fmt,x,x2,x3) warning(fmt,x,x2,x3)
71 # define MATHLIB_WARNING4(fmt,x,x2,x3,x4) warning(fmt,x,x2,x3,x4)
72 # define MATHLIB_WARNING5(fmt,x,x2,x3,x4,x5) warning(fmt,x,x2,x3,x4,x5)
73 
74 #include <R_ext/Arith.h>
75 #define ML_POSINF R_PosInf
76 #define ML_NEGINF R_NegInf
77 #define ML_NAN R_NaN
78 
79 
80 void R_CheckUserInterrupt(void);
81 /* Ei-ji Nakama reported that AIX 5.2 has calloc as a macro and objected
82  to redefining it. Tests added for 2.2.1 */
83 #ifdef calloc
84 # undef calloc
85 #endif
86 #define calloc R_chk_calloc
87 #ifdef free
88 # undef free
89 #endif
90 #define free R_chk_free
91 
92 #ifdef ENABLE_NLS
93 #include <libintl.h>
94 #define _(String) gettext (String)
95 #else
96 #define _(String) (String)
97 #endif
98 
99 #else
100 /* Mathlib standalone */
101 
102 #include <stdio.h>
103 #include <stdlib.h> /* for exit */
104 #define MATHLIB_ERROR(fmt,x) { printf(fmt,x); exit(1); }
105 #define MATHLIB_WARNING(fmt,x) printf(fmt,x)
106 #define MATHLIB_WARNING2(fmt,x,x2) printf(fmt,x,x2)
107 #define MATHLIB_WARNING3(fmt,x,x2,x3) printf(fmt,x,x2,x3)
108 #define MATHLIB_WARNING4(fmt,x,x2,x3,x4) printf(fmt,x,x2,x3,x4)
109 #define MATHLIB_WARNING5(fmt,x,x2,x3,x4,x5) printf(fmt,x,x2,x3,x4,x5)
110 
111 #define ISNAN(x) (isnan(x)!=0)
112 // Arith.h defines it
113 #ifndef R_FINITE
114 #ifdef HAVE_WORKING_ISFINITE
115 /* isfinite is defined in <math.h> according to C99 */
116 # define R_FINITE(x) isfinite(x)
117 #else
118 # define R_FINITE(x) R_finite(x)
119 #endif
120 #endif
121 int R_finite(double);
122 
123 #define ML_POSINF (1.0 / 0.0)
124 #define ML_NEGINF ((-1.0) / 0.0)
125 #define ML_NAN (0.0 / 0.0)
126 
127 #define _(String) String
128 #endif /* standalone */
129 
130 #define ML_VALID(x) (!ISNAN(x))
131 
132 #define ME_NONE 0
133 /* no error */
134 #define ME_DOMAIN 1
135 /* argument out of domain */
136 #define ME_RANGE 2
137 /* value out of range */
138 #define ME_NOCONV 4
139 /* process did not converge */
140 #define ME_PRECISION 8
141 /* does not have "full" precision */
142 #define ME_UNDERFLOW 16
143 /* and underflow occured (important for IEEE)*/
144 
145 #define ML_ERR_return_NAN { ML_ERROR(ME_DOMAIN, ""); return ML_NAN; }
146 
147 /* For a long time prior to R 2.3.0 ML_ERROR did nothing.
148  We don't report ME_DOMAIN errors as the callers collect ML_NANs into
149  a single warning.
150  */
151 #define ML_ERROR(x, s) { \
152  if(x > ME_DOMAIN) { \
153  const char *msg = ""; \
154  switch(x) { \
155  case ME_DOMAIN: \
156  msg = _("argument out of domain in '%s'\n"); \
157  break; \
158  case ME_RANGE: \
159  msg = _("value out of range in '%s'\n"); \
160  break; \
161  case ME_NOCONV: \
162  msg = _("convergence failed in '%s'\n"); \
163  break; \
164  case ME_PRECISION: \
165  msg = _("full precision may not have been achieved in '%s'\n"); \
166  break; \
167  case ME_UNDERFLOW: \
168  msg = _("underflow occurred in '%s'\n"); \
169  break; \
170  } \
171  MATHLIB_WARNING(msg, s); \
172  } \
173 }
174 
175 /* Wilcoxon Rank Sum Distribution */
176 
177 #define WILCOX_MAX 50
178 
179 #ifdef HAVE_VISIBILITY_ATTRIBUTE
180 # define attribute_hidden __attribute__ ((visibility ("hidden")))
181 #else
182 # define attribute_hidden
183 #endif
184 
185 /* Formerly private part of Mathlib.h */
186 
187 /* always remap internal functions */
188 #define bd0 Rf_bd0
189 #define chebyshev_eval Rf_chebyshev_eval
190 #define chebyshev_init Rf_chebyshev_init
191 #define gammalims Rf_gammalims
192 #define lfastchoose Rf_lfastchoose
193 #define lgammacor Rf_lgammacor
194 #define stirlerr Rf_stirlerr
195 #define pnchisq_raw Rf_pnchisq_raw
196 #define pgamma_raw Rf_pgamma_raw
197 #define pnbeta_raw Rf_pnbeta_raw
198 #define pnbeta2 Rf_pnbeta2
199 #define bratio Rf_bratio
200 
201  /* Chebyshev Series */
202 
203 int attribute_hidden chebyshev_init(double*, int, double);
204 double attribute_hidden chebyshev_eval(double, const double *, const int);
205 
206  /* Gamma and Related Functions */
207 
208 void attribute_hidden gammalims(double*, double*);
209 double attribute_hidden lgammacor(double); /* log(gamma) correction */
210 double attribute_hidden stirlerr(double); /* Stirling expansion "error" */
211 
212 double attribute_hidden lfastchoose(double, double);
213 
214 double attribute_hidden bd0(double, double);
215 
216 double attribute_hidden pnchisq_raw(double, double, double, double, double,
217  int, bool, bool);
218 double attribute_hidden pgamma_raw(double, double, int, int);
219 double attribute_hidden pbeta_raw(double, double, double, int, int);
220 double attribute_hidden qchisq_appr(double, double, double, int, int, double tol);
221 LDOUBLE attribute_hidden pnbeta_raw(double, double, double, double, double);
222 double attribute_hidden pnbeta2(double, double, double, double, double, int, int);
223 
224 int Rf_i1mach(int);
225 
226 /* From toms708.c */
227 /* void attribute_hidden bratio(double a, double b, double x, double y, */
228 /* double *w, double *w1, int *ierr, int log_p); */
229 
230 
231 #endif /* MATHLIB_PRIVATE_H */
License: GPL v2