1967
|
1 // lo-mappers.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1996 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cfloat> |
|
29 |
|
30 #include "lo-error.h" |
|
31 #include "lo-ieee.h" |
|
32 #include "lo-mappers.h" |
|
33 #include "lo-utils.h" |
|
34 #include "oct-cmplx.h" |
|
35 #include "oct-math.h" |
|
36 |
|
37 #include "f77-fcn.h" |
|
38 |
|
39 #if defined (_AIX) && defined (__GNUG__) |
|
40 #undef finite |
|
41 #define finite(x) ((x) < DBL_MAX && (x) > -DBL_MAX) |
|
42 #endif |
|
43 |
|
44 extern "C" |
|
45 { |
|
46 double F77_FCN (dgamma, DGAMMA) (const double&); |
|
47 |
|
48 int F77_FCN (dlgams, DLGAMS) (const double&, double&, double&); |
|
49 } |
|
50 |
|
51 #ifndef M_LOG10E |
|
52 #define M_LOG10E 0.43429448190325182765 |
|
53 #endif |
|
54 |
|
55 #ifndef M_PI |
|
56 #define M_PI 3.14159265358979323846 |
|
57 #endif |
|
58 |
|
59 #if defined (HAVE_LGAMMA) && ! defined (SIGNGAM_DECLARED) |
|
60 extern int signgam; |
|
61 #endif |
|
62 |
|
63 // Double -> double mappers. |
|
64 |
|
65 double |
|
66 arg (double x) |
|
67 { |
|
68 if (x < 0.0) |
|
69 return M_PI; |
|
70 else |
|
71 #if defined (HAVE_ISNAN) |
|
72 return xisnan (x) ? octave_NaN : 0.0; |
|
73 #else |
|
74 return 0.0; |
|
75 #endif |
|
76 } |
|
77 |
|
78 double |
|
79 conj (double x) |
|
80 { |
|
81 return x; |
|
82 } |
|
83 |
|
84 double |
|
85 fix (double x) |
|
86 { |
|
87 int tmp; |
|
88 tmp = (int) x; |
|
89 return (double) tmp; |
|
90 } |
|
91 |
|
92 double |
|
93 imag (double x) |
|
94 { |
|
95 #if defined (HAVE_ISNAN) |
|
96 return xisnan (x) ? octave_NaN : 0.0; |
|
97 #else |
|
98 return 0.0; |
|
99 #endif |
|
100 } |
|
101 |
|
102 double |
|
103 real (double x) |
|
104 { |
|
105 return x; |
|
106 } |
|
107 |
|
108 double |
|
109 round (double x) |
|
110 { |
|
111 return D_NINT (x); |
|
112 } |
|
113 |
|
114 double |
|
115 signum (double x) |
|
116 { |
|
117 double tmp = 0.0; |
|
118 if (x < 0.0) |
|
119 tmp = -1.0; |
|
120 else if (x > 0.0) |
|
121 tmp = 1.0; |
|
122 |
|
123 #if defined (HAVE_ISNAN) |
|
124 return xisnan (x) ? octave_NaN : tmp; |
|
125 #else |
|
126 return tmp; |
|
127 #endif |
|
128 } |
|
129 |
|
130 double |
|
131 xerf (double x) |
|
132 { |
|
133 #if defined (HAVE_ERF) |
|
134 return erf (x); |
|
135 #else |
|
136 (*current_liboctave_error_handler) |
|
137 ("erf (x) not available on this system"); |
|
138 #endif |
|
139 } |
|
140 |
|
141 double |
|
142 xerfc (double x) |
|
143 { |
|
144 #if defined (HAVE_ERFC) |
|
145 return erfc (x); |
|
146 #else |
|
147 (*current_liboctave_error_handler) |
|
148 ("erfc (x) not available on this system"); |
|
149 #endif |
|
150 } |
|
151 |
|
152 double |
|
153 xisnan (double x) |
|
154 { |
|
155 #if defined (HAVE_ISNAN) |
|
156 return (double) isnan (x); |
|
157 #else |
|
158 return 0; |
|
159 #endif |
|
160 } |
|
161 |
|
162 double |
|
163 xfinite (double x) |
|
164 { |
|
165 #if defined (HAVE_FINITE) |
|
166 return (double) finite (x); |
|
167 #elif defined (HAVE_ISINF) && defined (HAVE_ISNAN) |
|
168 return (double) (! isinf (x) && ! isnan (x)); |
|
169 #else |
|
170 return 1; |
|
171 #endif |
|
172 } |
|
173 |
|
174 double |
|
175 xgamma (double x) |
|
176 { |
|
177 return F77_FCN (dgamma, DGAMMA) (x); |
|
178 } |
|
179 |
|
180 double |
|
181 xisinf (double x) |
|
182 { |
|
183 #if defined (HAVE_ISINF) |
|
184 return (double) isinf (x); |
|
185 #elif defined (HAVE_FINITE) && defined (HAVE_ISNAN) |
|
186 return (double) (! (finite (x) || isnan (x))); |
|
187 #else |
|
188 return 0; |
|
189 #endif |
|
190 } |
|
191 |
|
192 double |
|
193 xlgamma (double x) |
|
194 { |
|
195 double result; |
|
196 double sgngam; |
|
197 |
|
198 F77_FCN (dlgams, DLGAMS) (x, result, sgngam); |
|
199 |
|
200 return result; |
|
201 } |
|
202 |
|
203 // Complex -> double mappers. |
|
204 |
|
205 double |
|
206 xisnan (const Complex& x) |
|
207 { |
|
208 #if defined (HAVE_ISNAN) |
|
209 double rx = real (x); |
|
210 double ix = imag (x); |
|
211 return (double) (isnan (rx) || isnan (ix)); |
|
212 #else |
|
213 return 0; |
|
214 #endif |
|
215 } |
|
216 |
|
217 double |
|
218 xfinite (const Complex& x) |
|
219 { |
|
220 double rx = real (x); |
|
221 double ix = imag (x); |
|
222 return (double) (! ((int) xisinf (rx) || (int) xisinf (ix))); |
|
223 } |
|
224 |
|
225 double |
|
226 xisinf (const Complex& x) |
|
227 { |
|
228 return (double) (! (int) xfinite (x)); |
|
229 } |
|
230 |
|
231 // Complex -> complex mappers. |
|
232 |
|
233 Complex |
|
234 acos (const Complex& x) |
|
235 { |
|
236 static Complex i (0, 1); |
|
237 Complex retval = -i * log (x + sqrt (x*x - 1.0)); |
|
238 return retval; |
|
239 } |
|
240 |
|
241 Complex |
|
242 acosh (const Complex& x) |
|
243 { |
|
244 Complex retval = log (x + sqrt (x*x - 1.0)); |
|
245 return retval; |
|
246 } |
|
247 |
|
248 Complex |
|
249 asin (const Complex& x) |
|
250 { |
|
251 static Complex i (0, 1); |
|
252 Complex retval = -i * log (i*x + sqrt (1.0 - x*x)); |
|
253 return retval; |
|
254 } |
|
255 |
|
256 Complex |
|
257 asinh (const Complex& x) |
|
258 { |
|
259 Complex retval = log (x + sqrt (x*x + 1.0)); |
|
260 return retval; |
|
261 } |
|
262 |
|
263 Complex |
|
264 atan (const Complex& x) |
|
265 { |
|
266 static Complex i (0, 1); |
|
267 Complex retval = i * log ((i + x) / (i - x)) / 2.0; |
|
268 return retval; |
|
269 } |
|
270 |
|
271 Complex |
|
272 atanh (const Complex& x) |
|
273 { |
|
274 static Complex i (0, 1); |
|
275 Complex retval = log ((1 + x) / (1 - x)) / 2.0; |
|
276 return retval; |
|
277 } |
|
278 |
|
279 Complex |
|
280 ceil (const Complex& x) |
|
281 { |
|
282 int re = (int) ceil (real (x)); |
|
283 int im = (int) ceil (imag (x)); |
|
284 return Complex (re, im); |
|
285 } |
|
286 |
|
287 Complex |
|
288 fix (const Complex& x) |
|
289 { |
|
290 int re = (int) real (x); |
|
291 int im = (int) imag (x); |
|
292 return Complex (re, im); |
|
293 } |
|
294 |
|
295 Complex |
|
296 floor (const Complex& x) |
|
297 { |
|
298 int re = (int) floor (real (x)); |
|
299 int im = (int) floor (imag (x)); |
|
300 return Complex (re, im); |
|
301 } |
|
302 |
|
303 Complex |
|
304 log10 (const Complex& x) |
|
305 { |
|
306 return M_LOG10E * log (x); |
|
307 } |
|
308 |
|
309 Complex |
|
310 round (const Complex& x) |
|
311 { |
|
312 double re = D_NINT (real (x)); |
|
313 double im = D_NINT (imag (x)); |
|
314 return Complex (re, im); |
|
315 } |
|
316 |
|
317 Complex |
|
318 signum (const Complex& x) |
|
319 { |
|
320 return x / abs (x); |
|
321 } |
|
322 |
|
323 Complex |
|
324 tan (const Complex& x) |
|
325 { |
|
326 Complex retval = sin (x) / cos (x); |
|
327 return retval; |
|
328 } |
|
329 |
|
330 Complex |
|
331 tanh (const Complex& x) |
|
332 { |
|
333 Complex retval = sinh (x) / cosh (x); |
|
334 return retval; |
|
335 } |
|
336 |
|
337 /* |
|
338 ;;; Local Variables: *** |
|
339 ;;; mode: C++ *** |
|
340 ;;; page-delimiter: "^/\\*" *** |
|
341 ;;; End: *** |
|
342 */ |