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 safe__already_included |
71 |
#define safe__already_included |
72 |
|
73 |
/*extern int safe_print_errors;*/ |
74 |
|
75 |
extern void safe_error_to_stderr(enum safe_err *err); |
76 |
/**< |
77 |
* The variable safe_err will be set to the appropriate value whenever |
78 |
* an undefined numerical calculation is attempted. |
79 |
* In this case, if the variable safe_print_errors is set to TRUE, |
80 |
* a description of the error will be printed to stderr. |
81 |
* In order to monitor errors effectively, it is |
82 |
* the responsibility of the user to set the safe_err variable to safe_ok |
83 |
* before calling any of the functions below. |
84 |
* (it's our on silly version of errno...) |
85 |
*/ |
86 |
|
87 |
#define safe_ERF_COEF 1.1283791670955130 /**< = 2 / sqrt(PI) */ |
88 |
#define safe_LOG10_COEF 0.4342944819032518 /**< = log10(e) = 1/ln(10) */ |
89 |
#define safe_PI 3.1415926535897930 /**< pi */ |
90 |
|
91 |
extern double safe_upower(double x, unsigned y, enum safe_err *err); /**< buggy */ |
92 |
/**< Compute x^n. Not Verified Code! */ |
93 |
extern double safe_factorial(unsigned x, enum safe_err *err); |
94 |
/**< Compute n!. */ |
95 |
extern double safe_rec(double x, enum safe_err *err); |
96 |
/**<Compute 1/x. */ |
97 |
extern double safe_cube(register double x, enum safe_err *err); |
98 |
/**<Compute x^3 with range check. */ |
99 |
extern double safe_Dn_rec(double x, int y, enum safe_err *err); |
100 |
/**< Compute n-th derivative of 1/x. */ |
101 |
#ifdef HAVE_ERF |
102 |
extern double safe_erf_inv(double x, enum safe_err *err); |
103 |
/**< Compute inverse of erf. */ |
104 |
#endif /* HAVE_ERF */ |
105 |
extern double safe_lnm_inv(double x, enum safe_err *err); |
106 |
extern int safe_is_int(double x, enum safe_err *err); |
107 |
/**< 0,1 ==> even/odd int,else ==> not an int. */ |
108 |
|
109 |
/* All of the following safe_XXXX_Di must resolve to C functions |
110 |
* rather than macros because safe_func_Di takes the address of them. |
111 |
*/ |
112 |
|
113 |
extern double safe_sin_D0(double x, enum safe_err *safe); |
114 |
extern double safe_sinh_D0(double x, enum safe_err *safe); |
115 |
extern double safe_cos_D0(double x, enum safe_err *safe); |
116 |
extern double safe_cosh_D0(double x, enum safe_err *safe); |
117 |
extern double safe_tan_D0(double x, enum safe_err *safe); |
118 |
extern double safe_tanh_D0(double x, enum safe_err *safe); |
119 |
extern double safe_arctan_D0(double x, enum safe_err *safe); |
120 |
extern double safe_arcsin_D0(double x, enum safe_err *safe); |
121 |
extern double safe_arcsinh_D0(double x, enum safe_err *safe); |
122 |
extern double safe_arccos_D0(double x, enum safe_err *safe); |
123 |
extern double safe_arccosh_D0(double x, enum safe_err *safe); |
124 |
extern double safe_arctanh_D0(double x, enum safe_err *safe); |
125 |
#ifdef HAVE_ERF |
126 |
extern double safe_erf_D0(double x, enum safe_err *safe); |
127 |
#endif /* HAVE_ERF */ |
128 |
extern double safe_exp_D0(double x, enum safe_err *safe); |
129 |
extern double safe_ln_D0(double x, enum safe_err *safe); |
130 |
extern double safe_lnm_D0(double x, enum safe_err *safe); |
131 |
extern double safe_log_D0(double x, enum safe_err *safe); |
132 |
extern double safe_sqr_D0(double x, enum safe_err *safe); |
133 |
extern double safe_sqrt_D0(double x, enum safe_err *safe); |
134 |
extern double safe_cbrt_D0(double x, enum safe_err *safe); |
135 |
extern double safe_fabs_D0(double x, enum safe_err *safe); |
136 |
extern double safe_hold_D0(double x, enum safe_err *safe); |
137 |
/* |
138 |
* value = safe_<uop>_D0(x) |
139 |
* double value,x; |
140 |
* enum safe_err *safe |
141 |
* |
142 |
* Computes value of unary operator. |
143 |
*/ |
144 |
|
145 |
extern double safe_sin_D1(double x, enum safe_err *safe); |
146 |
extern double safe_sinh_D1(double x, enum safe_err *safe); |
147 |
extern double safe_cos_D1(double x, enum safe_err *safe); |
148 |
extern double safe_cosh_D1(double x, enum safe_err *safe); |
149 |
extern double safe_tan_D1(double x, enum safe_err *safe); |
150 |
extern double safe_tanh_D1(double x, enum safe_err *safe); |
151 |
extern double safe_arcsin_D1(double x, enum safe_err *safe); |
152 |
extern double safe_arcsinh_D1(double x, enum safe_err *safe); |
153 |
extern double safe_arccos_D1(double x, enum safe_err *safe); |
154 |
extern double safe_arccosh_D1(double x, enum safe_err *safe); |
155 |
extern double safe_arctan_D1(double x, enum safe_err *safe); |
156 |
extern double safe_arctanh_D1(double x, enum safe_err *safe); |
157 |
#ifdef HAVE_ERF |
158 |
extern double safe_erf_D1(double x, enum safe_err *safe); |
159 |
#endif /* HAVE_ERF */ |
160 |
extern double safe_exp_D1(double x, enum safe_err *safe); |
161 |
extern double safe_ln_D1(double x, enum safe_err *safe); |
162 |
extern double safe_lnm_D1(double x, enum safe_err *safe); |
163 |
extern double safe_log_D1(double x, enum safe_err *safe); |
164 |
extern double safe_sqr_D1(double x, enum safe_err *safe); |
165 |
extern double safe_sqrt_D1(double x, enum safe_err *safe); |
166 |
extern double safe_cube_D0(double x, enum safe_err *safe); |
167 |
extern double safe_cube_D1(double x, enum safe_err *safe); |
168 |
extern double safe_cbrt_D1(double x, enum safe_err *safe); |
169 |
extern double safe_fabs_D1(double x, enum safe_err *safe); |
170 |
extern double safe_hold_D1(double x, enum safe_err *safe); |
171 |
/* |
172 |
* value = safe_<uop>_D1(x) |
173 |
* double value,x; |
174 |
* enum safe_err *safe |
175 |
* |
176 |
* Computes first derivative of unary operator. |
177 |
*/ |
178 |
|
179 |
extern double safe_sin_D2(double x, enum safe_err *safe); |
180 |
extern double safe_sinh_D2(double x, enum safe_err *safe); |
181 |
extern double safe_cos_D2(double x, enum safe_err *safe); |
182 |
extern double safe_cosh_D2(double x, enum safe_err *safe); |
183 |
extern double safe_tan_D2(double x, enum safe_err *safe); |
184 |
extern double safe_tanh_D2(double x, enum safe_err *safe); |
185 |
extern double safe_arcsin_D2(double x, enum safe_err *safe); |
186 |
extern double safe_arcsinh_D2(double x, enum safe_err *safe); |
187 |
extern double safe_arccos_D2(double x, enum safe_err *safe); |
188 |
extern double safe_arccosh_D2(double x, enum safe_err *safe); |
189 |
extern double safe_arctan_D2(double x, enum safe_err *safe); |
190 |
extern double safe_arctanh_D2(double x, enum safe_err *safe); |
191 |
#ifdef HAVE_ERF |
192 |
extern double safe_erf_D2(double x, enum safe_err *safe); |
193 |
#endif /* HAVE_ERF */ |
194 |
extern double safe_exp_D2(double x, enum safe_err *safe); |
195 |
extern double safe_ln_D2(double x, enum safe_err *safe); |
196 |
extern double safe_lnm_D2(double x, enum safe_err *safe); |
197 |
extern double safe_log_D2(double x, enum safe_err *safe); |
198 |
extern double safe_sqr_D2(double x, enum safe_err *safe); |
199 |
extern double safe_sqrt_D2(double x, enum safe_err *safe); |
200 |
extern double safe_cube_D2(double x, enum safe_err *safe); |
201 |
extern double safe_cbrt_D2(double x, enum safe_err *safe); |
202 |
extern double safe_fabs_D2(double x, enum safe_err *safe); |
203 |
#define safe_hold_D2 safe_hold_D1 |
204 |
/* |
205 |
* value = safe_<uop>_D2(x) |
206 |
* double value,x; |
207 |
* enum safe_err *safe |
208 |
* |
209 |
* Computes second derivative of unary operator. |
210 |
*/ |
211 |
|
212 |
extern double safe_arcsin_Dn(double x, int n, enum safe_err *safe); |
213 |
extern double safe_arccos_Dn(double x, int n, enum safe_err *safe); |
214 |
extern double safe_arctan_Dn(double x, int n, enum safe_err *safe); |
215 |
extern double safe_cos_Dn(double x, int n, enum safe_err *safe); |
216 |
extern double safe_sin_Dn(double x, int n, enum safe_err *safe); |
217 |
#ifdef HAVE_ERF |
218 |
extern double safe_erf_Dn(double x, int n, enum safe_err *safe); |
219 |
#endif /* HAVE_ERF */ |
220 |
extern double safe_exp_Dn(double x, int n, enum safe_err *safe); |
221 |
extern double safe_ln_Dn(double x, int n, enum safe_err *safe); |
222 |
extern double safe_log_Dn(double x, int n, enum safe_err *safe); |
223 |
extern double safe_sqr_Dn(double x, int n, enum safe_err *safe); |
224 |
extern double safe_sqrt_Dn(double x, int n, enum safe_err *safe); |
225 |
extern double safe_tan_Dn(double x, int n, enum safe_err *safe); |
226 |
extern double safe_fabs_Dn(double x, int n, enum safe_err *safe); |
227 |
#define safe_hold_Dn safe_hold_D1 |
228 |
/* |
229 |
* value = safe_<uop>_Dn(x,n) |
230 |
* double value,x; |
231 |
* int n; n >= 0 |
232 |
* enum safe_err *safe |
233 |
* |
234 |
* Computes n-th derivative of unary operator. |
235 |
*/ |
236 |
|
237 |
#define safe_add_D0(x,y,safe) ((x)+(y)) |
238 |
#define safe_sub_D0(x,y,safe) ((x)-(y)) |
239 |
#define safe_mul_D0(x,y,safe) ((x)*(y)) |
240 |
#define safe_div_D0(x,y,safe) safe_mul_D0((x),safe_rec(y,safe),(safe)) |
241 |
#define safe_ipow_D0(x,y,safe) asc_ipow((x),ascnint(y)) |
242 |
extern double safe_pow_D0(double x, double y, enum safe_err *safe); |
243 |
/**< |
244 |
* <!-- value = safe_<binop>_D0(x,y,safe) --> |
245 |
* <!-- double value,x,y; --> |
246 |
* <!-- enum safe_err *safe --> |
247 |
* |
248 |
* Computes x <binop> y and returns the value. |
249 |
*/ |
250 |
|
251 |
#define safe_add_D1(x,y,wrt,safe) (1.0) |
252 |
#define safe_sub_D1(x,y,wrt,safe) (1.0 - 2.0*(wrt)) |
253 |
#define safe_mul_D1(x,y,wrt,safe) ((wrt)==0 ? (y) : (x)) |
254 |
extern double safe_div_D1(double x, double y, int wrt, enum safe_err *safe); |
255 |
extern double safe_ipow_D1(double x, double y, int wrt, enum safe_err *safe); |
256 |
extern double safe_pow_D1(double x, double y, int wrt, enum safe_err *safe); |
257 |
/* |
258 |
* diff = safe_<binop>_D1(x,y,wrt) |
259 |
* double diff,x,y; |
260 |
* int wrt; 0 ==> d/dx, 1 ==> d/dy |
261 |
* enum safe_err *safe |
262 |
* |
263 |
* Computes derivative of x <binop> y w.r.t. x or y depending on wrt. |
264 |
*/ |
265 |
|
266 |
#define safe_add_D2(x,y,wrt1,wrt2,safe) (0.0) |
267 |
#define safe_sub_D2(x,y,wrt1,wrt2,safe) (0.0) |
268 |
#define safe_mul_D2(x,y,wrt1,wrt2,safe) ((wrt1)!=(wrt2)?1.0:0.0) |
269 |
extern double safe_div_D2(double x, double y, int wrt1, int wrt2, enum safe_err *safe); |
270 |
extern double safe_ipow_D2(double x, double y, int wrt1, int wrt2, enum safe_err *safe); |
271 |
extern double safe_pow_D2(double x, double y, int wrt1, int wrt2, enum safe_err *safe); |
272 |
/* |
273 |
* diff2 = safe_<binop>_D2(x,y,wrt1,wrt2,safe) |
274 |
* double diff2,x,y; |
275 |
* int wrt1,wrt2; 0 ==> w.r.t 1st arg, 1 ==> w.r.t. 2nd arg |
276 |
* enum safe_err *safe |
277 |
* |
278 |
* Computes 2nd derivative of x <binop> y w.r.t. x or y depending on |
279 |
* wrt1 and wrt2. (so you can get d^2(f)/dx^2, d^2(f)/dy^2 or d^2(f)/dy/dx |
280 |
* |
281 |
* At least in the CASE of pow/ipow |
282 |
* wrt1 = 0 wrt2 = 0 ==> d^2f/dx^2 |
283 |
* wrt1 = 0 wrt2 = 1 ==> d^2f/dy/dx |
284 |
* wrt1 = 1 wrt2 = 0 ==> d^2f/dy/dx |
285 |
* wrt1 = 1 wrt2 = 1 ==> d^2f/dy^2 |
286 |
*/ |
287 |
|
288 |
extern double safe_add_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
289 |
extern double safe_sub_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
290 |
extern double safe_mul_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
291 |
extern double safe_div_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
292 |
extern double |
293 |
safe_pow_Dn(double x, double y, int nwrt0, int nwrt1, enum safe_err *safe); |
294 |
/**< @todo this one not verified!*/ |
295 |
/* |
296 |
* diffn = safe_<binop>_Dn(x,y,nwrt0,nwrt1) |
297 |
* double diffn,x,y; |
298 |
* int nwrt0,nwrt1; |
299 |
* |
300 |
* Computes nwrt0'th derivative wrt x of the nwrt1'th derivative wrt y |
301 |
* of x <binop> y. |
302 |
*/ |
303 |
|
304 |
#endif /* safe__already_included */ |
305 |
|