1 |
/* |
2 |
* Safe function evaluator. |
3 |
* by Karl Michael Westerberg |
4 |
* Created: 2/6/90 |
5 |
* Version: $Revision: 1.10 $ |
6 |
* Version control file: $RCSfile: safe.h,v $ |
7 |
* Date last modified: $Date: 1997/07/21 16:56:51 $ |
8 |
* Last modified by: $Author: kt2g $ |
9 |
* |
10 |
* This file is part of the SLV solver. |
11 |
* |
12 |
* Copyright (C) 1990 Karl Michael Westerberg |
13 |
* Copyright (C) 1993 Joseph Zaher |
14 |
* Copyright (C) 1994 Joseph Zaher, Benjamin Andrew Allan |
15 |
* |
16 |
* The SLV solver is free software; you can redistribute |
17 |
* it and/or modify it under the terms of the GNU General Public License as |
18 |
* published by the Free Software Foundation; either version 2 of the |
19 |
* License, or (at your option) any later version. |
20 |
* |
21 |
* The SLV solver is distributed in hope that it will be |
22 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
24 |
* General Public License for more details. |
25 |
* |
26 |
* You should have received a copy of the GNU General Public License |
27 |
* along with the program; if not, write to the Free Software Foundation, |
28 |
* Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named |
29 |
* COPYING. |
30 |
* |
31 |
*/ |
32 |
|
33 |
/** @file |
34 |
* Safe function evaluator. |
35 |
* <pre> |
36 |
* Contents: Safe calculation module |
37 |
* |
38 |
* Authors: Karl Westerberg, Joseph Zaher |
39 |
* |
40 |
* Dates: 06/90 - original version in solver |
41 |
* 08/93 - removed safe_D0, safe_D1, safe_D2, safe_Dn |
42 |
* and added them as internal functions of the |
43 |
* exprman module. |
44 |
* 04/94 - enhanced error messaging |
45 |
* 06/96 - dealiased and moved into compiler. BAA |
46 |
* |
47 |
* Description: Operations are provided for computing unary and binary |
48 |
* functions. First, second, and nth derivatives may |
49 |
* also be computed for all of the functions featured. A |
50 |
* global boolean variable safe_ok, which is declared |
51 |
* external so that it may be monitored from other |
52 |
* modules, will be set to FALSE in the event of a |
53 |
* calculation error. Also, to suppress printing of error |
54 |
* messages in the event of an error, set the global |
55 |
* variable safe_print_errors to FALSE. |
56 |
* The intent here is to provide safer (if slower) |
57 |
* versions of math operations with our own little |
58 |
* version of errno. |
59 |
* At present, not all functions here are completely safe. |
60 |
* It's floating point. Can they ever be? |
61 |
* |
62 |
* When #including safe.h, make sure these files are #included first: |
63 |
* #include "utilities/ascConfig.h" |
64 |
* #include "compiler.h" |
65 |
* #include "functype.h" |
66 |
* </pre> |
67 |
* @todo Complete documentation of compiler/safe.h. |
68 |
*/ |
69 |
|
70 |
#ifndef ASC_SAFE_H |
71 |
#define ASC_SAFE_H |
72 |
|
73 |
/** addtogroup compiler Compiler |
74 |
@{ |
75 |
*/ |
76 |
|
77 |
/*extern int safe_print_errors;*/ |
78 |
|
79 |
extern void safe_error_to_stderr(enum safe_err *err); |
80 |
/**< |
81 |
* The variable safe_err will be set to the appropriate value whenever |
82 |
* an undefined numerical calculation is attempted. |
83 |
* In this case, if the variable safe_print_errors is set to TRUE, |
84 |
* a description of the error will be printed to stderr. |
85 |
* In order to monitor errors effectively, it is |
86 |
* the responsibility of the user to set the safe_err variable to safe_ok |
87 |
* before calling any of the functions below. |
88 |
* (it's our on silly version of errno...) |
89 |
*/ |
90 |
|
91 |
#define safe_ERF_COEF 1.1283791670955130 /**< = 2 / sqrt(PI) */ |
92 |
#define safe_LOG10_COEF 0.4342944819032518 /**< = log10(e) = 1/ln(10) */ |
93 |
#define safe_PI 3.1415926535897930 /**< pi */ |
94 |
|
95 |
extern double safe_upower(double x, unsigned y, enum safe_err *err); /**< buggy */ |
96 |
/**< Compute x^n. Not Verified Code! */ |
97 |
extern double safe_factorial(unsigned x, enum safe_err *err); |
98 |
/**< Compute n!. */ |
99 |
extern double safe_rec(double x, enum safe_err *err); |
100 |
/**<Compute 1/x. */ |
101 |
extern double safe_cube(register double x, enum safe_err *err); |
102 |
/**<Compute x^3 with range check. */ |
103 |
extern double safe_Dn_rec(double x, int y, enum safe_err *err); |
104 |
/**< Compute n-th derivative of 1/x. */ |
105 |
#ifdef HAVE_ERF |
106 |
extern double safe_erf_inv(double x, enum safe_err *err); |
107 |
/**< Compute inverse of erf. */ |
108 |
#endif /* HAVE_ERF */ |
109 |
extern double safe_lnm_inv(double x, enum safe_err *err); |
110 |
extern int safe_is_int(double x, enum safe_err *err); |
111 |
/**< 0,1 ==> even/odd int,else ==> not an int. */ |
112 |
|
113 |
/* All of the following safe_XXXX_Di must resolve to C functions |
114 |
* rather than macros because safe_func_Di takes the address of them. |
115 |
*/ |
116 |
|
117 |
extern double safe_sin_D0(double x, enum safe_err *safe); |
118 |
extern double safe_sinh_D0(double x, enum safe_err *safe); |
119 |
extern double safe_cos_D0(double x, enum safe_err *safe); |
120 |
extern double safe_cosh_D0(double x, enum safe_err *safe); |
121 |
extern double safe_tan_D0(double x, enum safe_err *safe); |
122 |
extern double safe_tanh_D0(double x, enum safe_err *safe); |
123 |
extern double safe_arctan_D0(double x, enum safe_err *safe); |
124 |
extern double safe_arcsin_D0(double x, enum safe_err *safe); |
125 |
extern double safe_arcsinh_D0(double x, enum safe_err *safe); |
126 |
extern double safe_arccos_D0(double x, enum safe_err *safe); |
127 |
extern double safe_arccosh_D0(double x, enum safe_err *safe); |
128 |
extern double safe_arctanh_D0(double x, enum safe_err *safe); |
129 |
#ifdef HAVE_ERF |
130 |
extern double safe_erf_D0(double x, enum safe_err *safe); |
131 |
#endif /* HAVE_ERF */ |
132 |
extern double safe_exp_D0(double x, enum safe_err *safe); |
133 |
extern double safe_ln_D0(double x, enum safe_err *safe); |
134 |
extern double safe_lnm_D0(double x, enum safe_err *safe); |
135 |
extern double safe_log10_D0(double x, enum safe_err *safe); |
136 |
extern double safe_sqr_D0(double x, enum safe_err *safe); |
137 |
extern double safe_sqrt_D0(double x, enum safe_err *safe); |
138 |
extern double safe_cbrt_D0(double x, enum safe_err *safe); |
139 |
extern double safe_fabs_D0(double x, enum safe_err *safe); |
140 |
extern double safe_hold_D0(double x, enum safe_err *safe); |
141 |
/* |
142 |
* value = safe_<uop>_D0(x) |
143 |
* double value,x; |
144 |
* enum safe_err *safe |
145 |
* |
146 |
* Computes value of unary operator. |
147 |
*/ |
148 |
|
149 |
extern double safe_sin_D1(double x, enum safe_err *safe); |
150 |
extern double safe_sinh_D1(double x, enum safe_err *safe); |
151 |
extern double safe_cos_D1(double x, enum safe_err *safe); |
152 |
extern double safe_cosh_D1(double x, enum safe_err *safe); |
153 |
extern double safe_tan_D1(double x, enum safe_err *safe); |
154 |
extern double safe_tanh_D1(double x, enum safe_err *safe); |
155 |
extern double safe_arcsin_D1(double x, enum safe_err *safe); |
156 |
extern double safe_arcsinh_D1(double x, enum safe_err *safe); |
157 |
extern double safe_arccos_D1(double x, enum safe_err *safe); |
158 |
extern double safe_arccosh_D1(double x, enum safe_err *safe); |
159 |
extern double safe_arctan_D1(double x, enum safe_err *safe); |
160 |
extern double safe_arctanh_D1(double x, enum safe_err *safe); |
161 |
#ifdef HAVE_ERF |
162 |
extern double safe_erf_D1(double x, enum safe_err *safe); |
163 |
#endif /* HAVE_ERF */ |
164 |
extern double safe_exp_D1(double x, enum safe_err *safe); |
165 |
extern double safe_ln_D1(double x, enum safe_err *safe); |
166 |
extern double safe_lnm_D1(double x, enum safe_err *safe); |
167 |
extern double safe_log10_D1(double x, enum safe_err *safe); |
168 |
extern double safe_sqr_D1(double x, enum safe_err *safe); |
169 |
extern double safe_sqrt_D1(double x, enum safe_err *safe); |
170 |
extern double safe_cube_D0(double x, enum safe_err *safe); |
171 |
extern double safe_cube_D1(double x, enum safe_err *safe); |
172 |
extern double safe_cbrt_D1(double x, enum safe_err *safe); |
173 |
extern double safe_fabs_D1(double x, enum safe_err *safe); |
174 |
extern double safe_hold_D1(double x, enum safe_err *safe); |
175 |
/* |
176 |
* value = safe_<uop>_D1(x) |
177 |
* double value,x; |
178 |
* enum safe_err *safe |
179 |
* |
180 |
* Computes first derivative of unary operator. |
181 |
*/ |
182 |
|
183 |
extern double safe_sin_D2(double x, enum safe_err *safe); |
184 |
extern double safe_sinh_D2(double x, enum safe_err *safe); |
185 |
extern double safe_cos_D2(double x, enum safe_err *safe); |
186 |
extern double safe_cosh_D2(double x, enum safe_err *safe); |
187 |
extern double safe_tan_D2(double x, enum safe_err *safe); |
188 |
extern double safe_tanh_D2(double x, enum safe_err *safe); |
189 |
extern double safe_arcsin_D2(double x, enum safe_err *safe); |
190 |
extern double safe_arcsinh_D2(double x, enum safe_err *safe); |
191 |
extern double safe_arccos_D2(double x, enum safe_err *safe); |
192 |
extern double safe_arccosh_D2(double x, enum safe_err *safe); |
193 |
extern double safe_arctan_D2(double x, enum safe_err *safe); |
194 |
extern double safe_arctanh_D2(double x, enum safe_err *safe); |
195 |
#ifdef HAVE_ERF |
196 |
extern double safe_erf_D2(double x, enum safe_err *safe); |
197 |
#endif /* HAVE_ERF */ |
198 |
extern double safe_exp_D2(double x, enum safe_err *safe); |
199 |
extern double safe_ln_D2(double x, enum safe_err *safe); |
200 |
extern double safe_lnm_D2(double x, enum safe_err *safe); |
201 |
extern double safe_log10_D2(double x, enum safe_err *safe); |
202 |
extern double safe_sqr_D2(double x, enum safe_err *safe); |
203 |
extern double safe_sqrt_D2(double x, enum safe_err *safe); |
204 |
extern double safe_cube_D2(double x, enum safe_err *safe); |
205 |
extern double safe_cbrt_D2(double x, enum safe_err *safe); |
206 |
extern double safe_fabs_D2(double x, enum safe_err *safe); |
207 |
#define safe_hold_D2 safe_hold_D1 |
208 |
/* |
209 |
* value = safe_<uop>_D2(x) |
210 |
* double value,x; |
211 |
* enum safe_err *safe |
212 |
* |
213 |
* Computes second derivative of unary operator. |
214 |
*/ |
215 |
|
216 |
extern double safe_arcsin_Dn(double x, int n, enum safe_err *safe); |
217 |
extern double safe_arccos_Dn(double x, int n, enum safe_err *safe); |
218 |
extern double safe_arctan_Dn(double x, int n, enum safe_err *safe); |
219 |
extern double safe_cos_Dn(double x, int n, enum safe_err *safe); |
220 |
extern double safe_sin_Dn(double x, int n, enum safe_err *safe); |
221 |
#ifdef HAVE_ERF |
222 |
extern double safe_erf_Dn(double x, int n, enum safe_err *safe); |
223 |
#endif /* HAVE_ERF */ |
224 |
extern double safe_exp_Dn(double x, int n, enum safe_err *safe); |
225 |
extern double safe_ln_Dn(double x, int n, enum safe_err *safe); |
226 |
extern double safe_log10_Dn(double x, int n, enum safe_err *safe); |
227 |
extern double safe_sqr_Dn(double x, int n, enum safe_err *safe); |
228 |
extern double safe_sqrt_Dn(double x, int n, enum safe_err *safe); |
229 |
extern double safe_tan_Dn(double x, int n, enum safe_err *safe); |
230 |
extern double safe_fabs_Dn(double x, int n, enum safe_err *safe); |
231 |
#define safe_hold_Dn safe_hold_D1 |
232 |
/* |
233 |
* value = safe_<uop>_Dn(x,n) |
234 |
* double value,x; |
235 |
* int n; n >= 0 |
236 |
* enum safe_err *safe |
237 |
* |
238 |
* Computes n-th derivative of unary operator. |
239 |
*/ |
240 |
|
241 |
#define safe_add_D0(x,y,safe) ((x)+(y)) |
242 |
#define safe_sub_D0(x,y,safe) ((x)-(y)) |
243 |
#define safe_mul_D0(x,y,safe) ((x)*(y)) |
244 |
#define safe_div_D0(x,y,safe) safe_mul_D0((x),safe_rec(y,safe),(safe)) |
245 |
#define safe_ipow_D0(x,y,safe) asc_ipow((x),ascnint(y)) |
246 |
extern double safe_pow_D0(double x, double y, enum safe_err *safe); |
247 |
/**< |
248 |
* <!-- value = safe_<binop>_D0(x,y,safe) --> |
249 |
* <!-- double value,x,y; --> |
250 |
* <!-- enum safe_err *safe --> |
251 |
* |
252 |
* Computes x <binop> y and returns the value. |
253 |
*/ |
254 |
|
255 |
#define safe_add_D1(x,y,wrt,safe) (1.0) |
256 |
#define safe_sub_D1(x,y,wrt,safe) (1.0 - 2.0*(wrt)) |
257 |
#define safe_mul_D1(x,y,wrt,safe) ((wrt)==0 ? (y) : (x)) |
258 |
extern double safe_div_D1(double x, double y, int wrt, enum safe_err *safe); |
259 |
extern double safe_ipow_D1(double x, double y, int wrt, enum safe_err *safe); |
260 |
extern double safe_pow_D1(double x, double y, int wrt, enum safe_err *safe); |
261 |
/* |
262 |
* diff = safe_<binop>_D1(x,y,wrt) |
263 |
* double diff,x,y; |
264 |
* int wrt; 0 ==> d/dx, 1 ==> d/dy |
265 |
* enum safe_err *safe |
266 |
* |
267 |
* Computes derivative of x <binop> y w.r.t. x or y depending on wrt. |
268 |
*/ |
269 |
|
270 |
#define safe_add_D2(x,y,wrt1,wrt2,safe) (0.0) |
271 |
#define safe_sub_D2(x,y,wrt1,wrt2,safe) (0.0) |
272 |
#define safe_mul_D2(x,y,wrt1,wrt2,safe) ((wrt1)!=(wrt2)?1.0:0.0) |
273 |
extern double safe_div_D2(double x, double y, int wrt1, int wrt2, enum safe_err *safe); |
274 |
extern double safe_ipow_D2(double x, double y, int wrt1, int wrt2, enum safe_err *safe); |
275 |
extern double safe_pow_D2(double x, double y, int wrt1, int wrt2, enum safe_err *safe); |
276 |
/* |
277 |
* diff2 = safe_<binop>_D2(x,y,wrt1,wrt2,safe) |
278 |
* double diff2,x,y; |
279 |
* int wrt1,wrt2; 0 ==> w.r.t 1st arg, 1 ==> w.r.t. 2nd arg |
280 |
* enum safe_err *safe |
281 |
* |
282 |
* Computes 2nd derivative of x <binop> y w.r.t. x or y depending on |
283 |
* wrt1 and wrt2. (so you can get d^2(f)/dx^2, d^2(f)/dy^2 or d^2(f)/dy/dx |
284 |
* |
285 |
* At least in the CASE of pow/ipow |
286 |
* wrt1 = 0 wrt2 = 0 ==> d^2f/dx^2 |
287 |
* wrt1 = 0 wrt2 = 1 ==> d^2f/dy/dx |
288 |
* wrt1 = 1 wrt2 = 0 ==> d^2f/dy/dx |
289 |
* wrt1 = 1 wrt2 = 1 ==> d^2f/dy^2 |
290 |
*/ |
291 |
|
292 |
extern double safe_add_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
293 |
extern double safe_sub_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
294 |
extern double safe_mul_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
295 |
extern double safe_div_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
296 |
extern double |
297 |
safe_pow_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
298 |
/**< @todo this one not verified!*/ |
299 |
/* |
300 |
* diffn = safe_<binop>_Dn(x,y,nwrt0,nwrt1) |
301 |
* double diffn,x,y; |
302 |
* int nwrt0,nwrt1; |
303 |
* |
304 |
* Computes nwrt0'th derivative wrt x of the nwrt1'th derivative wrt y |
305 |
* of x <binop> y. |
306 |
*/ |
307 |
|
308 |
/* @} */ |
309 |
|
310 |
#endif /* ASC_SAFE_H */ |
311 |
|