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 |
/* |
34 |
* Contents: Safe calculation module |
35 |
* |
36 |
* Authors: Karl Westerberg, Joseph Zaher |
37 |
* |
38 |
* Dates: 06/90 - original version in solver |
39 |
* 08/93 - removed safe_D0, safe_D1, safe_D2, safe_Dn |
40 |
* and added them as internal functions of the |
41 |
* exprman module. |
42 |
* 04/94 - enhanced error messaging |
43 |
* 06/96 - dealiased and moved into compiler. BAA |
44 |
* |
45 |
* Description: Operations are provided for computing unary and binary |
46 |
* functions. First, second, and nth derivatives may |
47 |
* also be computed for all of the functions featured. A |
48 |
* global boolean variable safe_ok, which is declared |
49 |
* external so that it may be monitored from other |
50 |
* modules, will be set to FALSE in the event of a |
51 |
* calculation error. Also, to suppress printing of error |
52 |
* messages in the event of an error, set the global |
53 |
* variable safe_print_errors to FALSE. |
54 |
* The intent here is to provide safer (if slower) |
55 |
* versions of math operations with our own little |
56 |
* version of errno. |
57 |
* At present, not all functions here are completely safe. |
58 |
* It's floating point. Can they ever be? |
59 |
*/ |
60 |
|
61 |
/* |
62 |
* When #including safe.h, make sure these files are #included first: |
63 |
* #include "compiler.h" |
64 |
* #include "functype.h" |
65 |
*/ |
66 |
|
67 |
|
68 |
#ifndef safe__already_included |
69 |
#define safe__already_included |
70 |
|
71 |
|
72 |
/*extern int safe_print_errors;*/ |
73 |
|
74 |
extern void safe_error_to_stderr(enum safe_err *); |
75 |
|
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 |
90 |
|
91 |
extern double safe_upower(double, unsigned, enum safe_err *); /* buggy */ |
92 |
extern double safe_factorial(unsigned,enum safe_err *); |
93 |
extern double safe_rec(double,enum safe_err *); |
94 |
extern double safe_cube(register double,enum safe_err *); |
95 |
extern double safe_Dn_rec(double,int,enum safe_err *); |
96 |
#ifdef HAVE_ERF |
97 |
extern double safe_erf_inv(double,enum safe_err *); |
98 |
#endif /* HAVE_ERF */ |
99 |
extern double safe_lnm_inv(double,enum safe_err *); |
100 |
extern int safe_is_int(double,enum safe_err *); |
101 |
/* |
102 |
* value = safe_upower(x,n,safe) Compute x^n. Not Verified Code! |
103 |
* value = safe_factorial(n,safe) Compute n!. |
104 |
* value = safe_rec(x,safe) Compute 1/x. |
105 |
* value = safe_cube(x,safe) Compute x^3 with range check |
106 |
* value = safe_Dn_rec(x,n,safe) Compute n-th derivative of 1/x. |
107 |
* value = safe_erf_inv(x,safe) Compute inverse of erf. |
108 |
* isint = safe_is_int(x,safe) 0,1 ==> even/odd int,else ==> not an int. |
109 |
* double value,x; |
110 |
* unsigned n; |
111 |
* enum safe_err *safe |
112 |
* int isint; |
113 |
*/ |
114 |
|
115 |
/* All of the following safe_XXXX_Di must resolve to C functions |
116 |
* rather than macros because safe_func_Di takes the address of them. |
117 |
*/ |
118 |
|
119 |
extern double safe_sin_D0(double,enum safe_err *); |
120 |
extern double safe_sinh_D0(double,enum safe_err *); |
121 |
extern double safe_cos_D0(double,enum safe_err *); |
122 |
extern double safe_cosh_D0(double,enum safe_err *); |
123 |
extern double safe_tan_D0(double,enum safe_err *); |
124 |
extern double safe_tanh_D0(double,enum safe_err *); |
125 |
extern double safe_arctan_D0(double,enum safe_err *); |
126 |
extern double safe_arcsin_D0(double,enum safe_err *); |
127 |
extern double safe_arcsinh_D0(double,enum safe_err *); |
128 |
extern double safe_arccos_D0(double,enum safe_err *); |
129 |
extern double safe_arccosh_D0(double,enum safe_err *); |
130 |
extern double safe_arctanh_D0(double,enum safe_err *); |
131 |
#ifdef HAVE_ERF |
132 |
extern double safe_erf_D0(double,enum safe_err *); |
133 |
#endif /* HAVE_ERF */ |
134 |
extern double safe_exp_D0(double,enum safe_err *); |
135 |
extern double safe_ln_D0(double,enum safe_err *); |
136 |
extern double safe_lnm_D0(double,enum safe_err *); |
137 |
extern double safe_log_D0(double,enum safe_err *); |
138 |
extern double safe_sqr_D0(double,enum safe_err *); |
139 |
extern double safe_sqrt_D0(double,enum safe_err *); |
140 |
extern double safe_cbrt_D0(double,enum safe_err *); |
141 |
extern double safe_fabs_D0(double,enum safe_err *); |
142 |
extern double safe_hold_D0(double,enum safe_err *); |
143 |
/* |
144 |
* value = safe_<uop>_D0(x) |
145 |
* double value,x; |
146 |
* enum safe_err *safe |
147 |
* |
148 |
* Computes value of unary operator. |
149 |
*/ |
150 |
|
151 |
extern double safe_sin_D1(double,enum safe_err *); |
152 |
extern double safe_sinh_D1(double,enum safe_err *); |
153 |
extern double safe_cos_D1(double,enum safe_err *); |
154 |
extern double safe_cosh_D1(double,enum safe_err *); |
155 |
extern double safe_tan_D1(double,enum safe_err *); |
156 |
extern double safe_tanh_D1(double,enum safe_err *); |
157 |
extern double safe_arcsin_D1(double,enum safe_err *); |
158 |
extern double safe_arcsinh_D1(double,enum safe_err *); |
159 |
extern double safe_arccos_D1(double,enum safe_err *); |
160 |
extern double safe_arccosh_D1(double,enum safe_err *); |
161 |
extern double safe_arctan_D1(double,enum safe_err *); |
162 |
extern double safe_arctanh_D1(double,enum safe_err *); |
163 |
#ifdef HAVE_ERF |
164 |
extern double safe_erf_D1(double,enum safe_err *); |
165 |
#endif /* HAVE_ERF */ |
166 |
extern double safe_exp_D1(double,enum safe_err *); |
167 |
extern double safe_ln_D1(double,enum safe_err *); |
168 |
extern double safe_lnm_D1(double,enum safe_err *); |
169 |
extern double safe_log_D1(double,enum safe_err *); |
170 |
extern double safe_sqr_D1(double,enum safe_err *); |
171 |
extern double safe_sqrt_D1(double,enum safe_err *); |
172 |
extern double safe_cube_D0(double,enum safe_err *); |
173 |
extern double safe_cube_D1(double,enum safe_err *); |
174 |
extern double safe_cbrt_D1(double,enum safe_err *); |
175 |
extern double safe_fabs_D1(double,enum safe_err *); |
176 |
extern double safe_hold_D1(double,enum safe_err *); |
177 |
/* |
178 |
* value = safe_<uop>_D1(x) |
179 |
* double value,x; |
180 |
* enum safe_err *safe |
181 |
* |
182 |
* Computes first derivative of unary operator. |
183 |
*/ |
184 |
|
185 |
|
186 |
extern double safe_sin_D2(double,enum safe_err *); |
187 |
extern double safe_sinh_D2(double,enum safe_err *); |
188 |
extern double safe_cos_D2(double,enum safe_err *); |
189 |
extern double safe_cosh_D2(double,enum safe_err *); |
190 |
extern double safe_tan_D2(double,enum safe_err *); |
191 |
extern double safe_tanh_D2(double,enum safe_err *); |
192 |
extern double safe_arcsin_D2(double,enum safe_err *); |
193 |
extern double safe_arcsinh_D2(double,enum safe_err *); |
194 |
extern double safe_arccos_D2(double,enum safe_err *); |
195 |
extern double safe_arccosh_D2(double,enum safe_err *); |
196 |
extern double safe_arctan_D2(double,enum safe_err *); |
197 |
extern double safe_arctanh_D2(double,enum safe_err *); |
198 |
#ifdef HAVE_ERF |
199 |
extern double safe_erf_D2(double,enum safe_err *); |
200 |
#endif /* HAVE_ERF */ |
201 |
extern double safe_exp_D2(double,enum safe_err *); |
202 |
extern double safe_ln_D2(double,enum safe_err *); |
203 |
extern double safe_lnm_D2(double,enum safe_err *); |
204 |
extern double safe_log_D2(double,enum safe_err *); |
205 |
extern double safe_sqr_D2(double,enum safe_err *); |
206 |
extern double safe_sqrt_D2(double,enum safe_err *); |
207 |
extern double safe_cube_D2(double,enum safe_err *); |
208 |
extern double safe_cbrt_D2(double,enum safe_err *); |
209 |
extern double safe_fabs_D2(double,enum safe_err *); |
210 |
#define safe_hold_D2 safe_hold_D1 |
211 |
/* |
212 |
* value = safe_<uop>_D2(x) |
213 |
* double value,x; |
214 |
* enum safe_err *safe |
215 |
* |
216 |
* Computes second derivative of unary operator. |
217 |
*/ |
218 |
|
219 |
extern double safe_arcsin_Dn(double,int,enum safe_err *); |
220 |
extern double safe_arccos_Dn(double,int,enum safe_err *); |
221 |
extern double safe_arctan_Dn(double,int,enum safe_err *); |
222 |
extern double safe_cos_Dn(double,int,enum safe_err *); |
223 |
extern double safe_sin_Dn(double,int,enum safe_err *); |
224 |
#ifdef HAVE_ERF |
225 |
extern double safe_erf_Dn(double,int,enum safe_err *); |
226 |
#endif /* HAVE_ERF */ |
227 |
extern double safe_exp_Dn(double,int,enum safe_err *); |
228 |
extern double safe_ln_Dn(double,int,enum safe_err *); |
229 |
extern double safe_log_Dn(double,int,enum safe_err *); |
230 |
extern double safe_sqr_Dn(double,int,enum safe_err *); |
231 |
extern double safe_sqrt_Dn(double,int,enum safe_err *); |
232 |
extern double safe_tan_Dn(double,int,enum safe_err *); |
233 |
extern double safe_fabs_Dn(double,int,enum safe_err *); |
234 |
#define safe_hold_Dn safe_hold_D1 |
235 |
/* |
236 |
* value = safe_<uop>_Dn(x,n) |
237 |
* double value,x; |
238 |
* int n; n >= 0 |
239 |
* enum safe_err *safe |
240 |
* |
241 |
* Computes n-th derivative of unary operator. |
242 |
*/ |
243 |
|
244 |
#define safe_add_D0(x,y,safe) ((x)+(y)) |
245 |
#define safe_sub_D0(x,y,safe) ((x)-(y)) |
246 |
#define safe_mul_D0(x,y,safe) ((x)*(y)) |
247 |
#define safe_div_D0(x,y,safe) safe_mul_D0((x),safe_rec(y,safe),(safe)) |
248 |
#define safe_ipow_D0(x,y,safe) asc_ipow((x),ascnint(y)) |
249 |
extern double safe_pow_D0(double,double,enum safe_err *); |
250 |
/* |
251 |
* value = safe_<binop>_D0(x,y,safe) |
252 |
* double value,x,y; |
253 |
* enum safe_err *safe |
254 |
* |
255 |
* Computes x <binop> y and returns the value. |
256 |
*/ |
257 |
|
258 |
#define safe_add_D1(x,y,wrt,safe) (1.0) |
259 |
#define safe_sub_D1(x,y,wrt,safe) (1.0 - 2.0*(wrt)) |
260 |
#define safe_mul_D1(x,y,wrt,safe) ((wrt)==0 ? (y) : (x)) |
261 |
extern double safe_div_D1(double,double,int,enum safe_err *); |
262 |
extern double safe_ipow_D1(double,double,int,enum safe_err *); |
263 |
extern double safe_pow_D1(double,double,int,enum safe_err *); |
264 |
/* |
265 |
* diff = safe_<binop>_D1(x,y,wrt) |
266 |
* double diff,x,y; |
267 |
* int wrt; 0 ==> d/dx, 1 ==> d/dy |
268 |
* enum safe_err *safe |
269 |
* |
270 |
* Computes derivative of x <binop> y w.r.t. x or y depending on wrt. |
271 |
*/ |
272 |
|
273 |
#define safe_add_D2(x,y,wrt1,wrt2,safe) (0.0) |
274 |
#define safe_sub_D2(x,y,wrt1,wrt2,safe) (0.0) |
275 |
#define safe_mul_D2(x,y,wrt1,wrt2,safe) ((wrt1)!=(wrt2)?1.0:0.0) |
276 |
extern double safe_div_D2(double,double,int,int,enum safe_err *); |
277 |
extern double safe_ipow_D2(double, double, int, int,enum safe_err *); |
278 |
extern double safe_pow_D2(double, double, int, int,enum safe_err *); |
279 |
/* |
280 |
* diff2 = safe_<binop>_D2(x,y,wrt1,wrt2,safe) |
281 |
* double diff2,x,y; |
282 |
* int wrt1,wrt2; 0 ==> w.r.t 1st arg, 1 ==> w.r.t. 2nd arg |
283 |
* enum safe_err *safe |
284 |
* |
285 |
* Computes 2nd derivative of x <binop> y w.r.t. x or y depending on |
286 |
* wrt1 and wrt2. (so you can get d^2(f)/dx^2, d^2(f)/dy^2 or d^2(f)/dy/dx |
287 |
* |
288 |
* At least in the CASE of pow/ipow |
289 |
* wrt1 = 0 wrt2 = 0 ==> d^2f/dx^2 |
290 |
* wrt1 = 0 wrt2 = 1 ==> d^2f/dy/dx |
291 |
* wrt1 = 1 wrt2 = 0 ==> d^2f/dy/dx |
292 |
* wrt1 = 1 wrt2 = 1 ==> d^2f/dy^2 |
293 |
*/ |
294 |
|
295 |
extern double safe_add_Dn(double,double,int,int,enum safe_err *); |
296 |
extern double safe_sub_Dn(double,double,int,int,enum safe_err *); |
297 |
extern double safe_mul_Dn(double,double,int,int,enum safe_err *); |
298 |
extern double safe_div_Dn(double,double,int,int,enum safe_err *); |
299 |
extern double |
300 |
safe_pow_Dn(double,double,int,int,enum safe_err *); /*this one not verified!*/ |
301 |
/* |
302 |
* diffn = safe_<binop>_Dn(x,y,nwrt0,nwrt1) |
303 |
* double diffn,x,y; |
304 |
* int nwrt0,nwrt1; |
305 |
* |
306 |
* Computes nwrt0'th derivative wrt x of the nwrt1'th derivative wrt y |
307 |
* of x <binop> y. |
308 |
*/ |
309 |
|
310 |
#endif |