1
|
1 // mappers.cc -*- C++ -*- |
|
2 /* |
|
3 |
1173
|
4 Copyright (C) 1992, 1993, 1995 John W. Eaton |
1
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <cmath> |
|
29 #include <cfloat> |
|
30 |
164
|
31 #include <Complex.h> |
1
|
32 |
1352
|
33 #include "defun.h" |
|
34 #include "error.h" |
1173
|
35 #include "f77-uscore.h" |
1
|
36 #include "mappers.h" |
1352
|
37 #include "missing-math.h" |
1308
|
38 #include "sysdep.h" |
1
|
39 #include "utils.h" |
1352
|
40 #include "variables.h" |
1
|
41 |
|
42 #if defined (_AIX) && defined (__GNUG__) |
|
43 #undef finite |
|
44 #define finite(x) ((x) < DBL_MAX && (x) > -DBL_MAX) |
|
45 #endif |
|
46 |
1173
|
47 extern "C" |
|
48 { |
1255
|
49 double F77_FCN (dgamma, DGAMMA) (const double&); |
|
50 int F77_FCN (dlgams, DLGAMS) (const double&, double&, double&); |
1173
|
51 } |
|
52 |
65
|
53 #ifndef M_LOG10E |
|
54 #define M_LOG10E 0.43429448190325182765 |
|
55 #endif |
|
56 |
|
57 #ifndef M_PI |
|
58 #define M_PI 3.14159265358979323846 |
|
59 #endif |
|
60 |
706
|
61 #if defined (HAVE_LGAMMA) && ! defined (SIGNGAM_DECLARED) |
|
62 extern int signgam; |
|
63 #endif |
|
64 |
767
|
65 // Double -> double mappers. |
1
|
66 |
|
67 double |
|
68 arg (double x) |
|
69 { |
65
|
70 if (x < 0.0) |
|
71 return M_PI; |
|
72 else |
1308
|
73 #if defined (HAVE_ISNAN) |
|
74 return xisnan (x) ? octave_NaN : 0.0; |
|
75 #else |
65
|
76 return 0.0; |
1308
|
77 #endif |
1
|
78 } |
|
79 |
|
80 double |
|
81 conj (double x) |
|
82 { |
|
83 return x; |
|
84 } |
|
85 |
|
86 double |
|
87 fix (double x) |
|
88 { |
|
89 int tmp; |
|
90 tmp = (int) x; |
|
91 return (double) tmp; |
|
92 } |
|
93 |
|
94 double |
|
95 imag (double x) |
|
96 { |
1308
|
97 #if defined (HAVE_ISNAN) |
|
98 return xisnan (x) ? octave_NaN : 0.0; |
|
99 #else |
1
|
100 return 0.0; |
1308
|
101 #endif |
1
|
102 } |
|
103 |
|
104 double |
|
105 real (double x) |
|
106 { |
|
107 return x; |
|
108 } |
|
109 |
|
110 double |
|
111 round (double x) |
|
112 { |
|
113 return D_NINT (x); |
|
114 } |
|
115 |
|
116 double |
|
117 signum (double x) |
|
118 { |
|
119 double tmp = 0.0; |
|
120 if (x < 0.0) |
|
121 tmp = -1.0; |
|
122 else if (x > 0.0) |
|
123 tmp = 1.0; |
1308
|
124 |
|
125 #if defined (HAVE_ISNAN) |
|
126 return xisnan (x) ? octave_NaN : tmp; |
|
127 #else |
1
|
128 return tmp; |
1308
|
129 #endif |
1
|
130 } |
|
131 |
|
132 double |
624
|
133 xerf (double x) |
|
134 { |
|
135 #if defined (HAVE_ERF) |
|
136 return erf (x); |
|
137 #else |
|
138 error ("erf(x) not available on this system"); |
|
139 #endif |
|
140 } |
|
141 |
|
142 double |
|
143 xerfc (double x) |
|
144 { |
|
145 #if defined (HAVE_ERFC) |
|
146 return erfc (x); |
|
147 #else |
|
148 error ("erfc(x) not available on this system"); |
|
149 #endif |
|
150 } |
|
151 |
|
152 double |
1
|
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 |
1384
|
170 return 1; |
1
|
171 #endif |
|
172 } |
|
173 |
|
174 double |
624
|
175 xgamma (double x) |
|
176 { |
1255
|
177 return F77_FCN (dgamma, DGAMMA) (x); |
624
|
178 } |
|
179 |
|
180 double |
1
|
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 |
1384
|
188 return 0; |
1
|
189 #endif |
|
190 } |
|
191 |
624
|
192 double |
|
193 xlgamma (double x) |
|
194 { |
1173
|
195 double result; |
|
196 double sgngam; |
|
197 |
1255
|
198 F77_FCN (dlgams, DLGAMS) (x, result, sgngam); |
1173
|
199 |
|
200 return result; |
624
|
201 } |
|
202 |
767
|
203 // Complex -> double mappers. |
1
|
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 |
767
|
231 // Complex -> complex mappers. |
1
|
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); |
331
|
275 Complex retval = log ((1 + x) / (1 - x)) / 2.0; |
1
|
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 |
529
|
337 void |
|
338 install_mapper_functions (void) |
|
339 { |
|
340 DEFUN_MAPPER ("abs", Sabs, 0, 0.0, 0.0, fabs, abs, 0, |
|
341 "abs (X): compute abs (X) for each element of X"); |
|
342 |
|
343 DEFUN_MAPPER ("acos", Sacos, 1, -1.0, 1.0, acos, 0, acos, |
|
344 "acos (X): compute acos (X) for each element of X"); |
|
345 |
|
346 DEFUN_MAPPER ("acosh", Sacosh, 1, 1.0, DBL_MAX, acosh, 0, acosh, |
|
347 "acosh (X): compute acosh (X) for each element of X"); |
|
348 |
|
349 DEFUN_MAPPER ("angle", Sangle, 0, 0.0, 0.0, arg, arg, 0, |
|
350 "angle (X): compute arg (X) for each element of X"); |
|
351 |
|
352 DEFUN_MAPPER ("arg", Sarg, 0, 0.0, 0.0, arg, arg, 0, |
|
353 "arg (X): compute arg (X) for each element of X"); |
|
354 |
|
355 DEFUN_MAPPER ("asin", Sasin, 1, -1.0, 1.0, asin, 0, asin, |
|
356 "asin (X): compute asin (X) for each element of X"); |
|
357 |
|
358 DEFUN_MAPPER ("asinh", Sasinh, 0, 0.0, 0.0, asinh, 0, asinh, |
|
359 "asinh (X): compute asinh (X) for each element of X"); |
|
360 |
|
361 DEFUN_MAPPER ("atan", Satan, 0, 0.0, 0.0, atan, 0, atan, |
|
362 "atan (X): compute atan (X) for each element of X"); |
|
363 |
|
364 DEFUN_MAPPER ("atanh", Satanh, 1, -1.0, 1.0, atanh, 0, atanh, |
|
365 "atanh (X): compute atanh (X) for each element of X"); |
|
366 |
|
367 DEFUN_MAPPER ("ceil", Sceil, 0, 0.0, 0.0, ceil, 0, ceil, |
|
368 "ceil (X): round elements of X toward +Inf"); |
|
369 |
|
370 DEFUN_MAPPER ("conj", Sconj, 0, 0.0, 0.0, conj, 0, conj, |
|
371 "conj (X): compute complex conjugate for each element of X"); |
|
372 |
|
373 DEFUN_MAPPER ("cos", Scos, 0, 0.0, 0.0, cos, 0, cos, |
|
374 "cos (X): compute cos (X) for each element of X"); |
|
375 |
|
376 DEFUN_MAPPER ("cosh", Scosh, 0, 0.0, 0.0, cosh, 0, cosh, |
|
377 "cosh (X): compute cosh (X) for each element of X"); |
|
378 |
624
|
379 DEFUN_MAPPER ("erf", Serf, 0, 0.0, 0.0, xerf, 0, 0, |
|
380 "erf (X): compute erf (X) for each element of X"); |
|
381 |
|
382 DEFUN_MAPPER ("erfc", Serfc, 0, 0.0, 0.0, xerfc, 0, 0, |
|
383 "erfc (X): compute erfc (X) for each element of X"); |
|
384 |
529
|
385 DEFUN_MAPPER ("exp", Sexp, 0, 0.0, 0.0, exp, 0, exp, |
|
386 "exp (X): compute exp (X) for each element of X"); |
|
387 |
|
388 DEFUN_MAPPER ("finite", Sfinite, 0, 0.0, 0.0, xfinite, xfinite, 0, |
|
389 "finite (X): return 1 for finite elements of X"); |
|
390 |
|
391 DEFUN_MAPPER ("fix", Sfix, 0, 0.0, 0.0, fix, 0, fix, |
|
392 "fix (X): round elements of X toward zero"); |
|
393 |
|
394 DEFUN_MAPPER ("floor", Sfloor, 0, 0.0, 0.0, floor, 0, floor, |
|
395 "floor (X): round elements of X toward -Inf"); |
|
396 |
624
|
397 DEFUN_MAPPER ("gamma", Sgamma, 0, 0.0, 0.0, xgamma, 0, 0, |
|
398 "gamma (X): compute gamma (X) for each element of X"); |
|
399 |
529
|
400 DEFUN_MAPPER ("isinf", Sisinf, 0, 0.0, 0.0, xisinf, xisinf, 0, |
|
401 "isinf (X): return 1 for elements of X infinite"); |
|
402 |
|
403 DEFUN_MAPPER ("imag", Simag, 0, 0.0, 0.0, imag, imag, 0, |
|
404 "imag (X): return imaginary part for each elements of X"); |
|
405 |
|
406 DEFUN_MAPPER ("isnan", Sisnan, 0, 0.0, 0.0, xisnan, xisnan, 0, |
|
407 "isnan (X): return 1 where elements of X are NaNs"); |
624
|
408 |
|
409 DEFUN_MAPPER ("lgamma", Slgamma, 0, 0.0, 0.0, xlgamma, 0, 0, |
|
410 "lgamma (X): compute log gamma (X) for each element of X"); |
529
|
411 |
|
412 DEFUN_MAPPER ("log", Slog, 1, 0.0, DBL_MAX, log, 0, log, |
|
413 "log (X): compute log (X) for each element of X"); |
|
414 |
|
415 DEFUN_MAPPER ("log10", Slog10, 1, 0.0, DBL_MAX, log10, 0, log10, |
|
416 "log10 (X): compute log10 (X) for each element of X"); |
|
417 |
|
418 DEFUN_MAPPER ("real", Sreal, 0, 0.0, 0.0, real, real, 0, |
|
419 "real (X): return real part for each element of X"); |
|
420 |
|
421 DEFUN_MAPPER ("round", Sround, 0, 0.0, 0.0, round, 0, round, |
|
422 "round (X): round elements of X to nearest integer"); |
|
423 |
|
424 DEFUN_MAPPER ("sign", Ssign, 0, 0.0, 0.0, signum, 0, signum, |
|
425 "sign (X): apply signum function to elements of X"); |
|
426 |
|
427 DEFUN_MAPPER ("sin", Ssin, 0, 0.0, 0.0, sin, 0, sin, |
|
428 "sin (X): compute sin (X) for each element of X"); |
|
429 |
|
430 DEFUN_MAPPER ("sinh", Ssinh, 0, 0.0, 0.0, sinh, 0, sinh, |
|
431 "sinh (X): compute sinh (X) for each element of X"); |
|
432 |
|
433 DEFUN_MAPPER ("sqrt", Ssqrt, 1, 0.0, DBL_MAX, sqrt, 0, sqrt, |
|
434 "sqrt (X): compute sqrt (X) for each element of X"); |
|
435 |
|
436 DEFUN_MAPPER ("tan", Stan, 0, 0.0, 0.0, tan, 0, tan, |
|
437 "tan (X): compute tan (X) for each element of X"); |
|
438 |
|
439 DEFUN_MAPPER ("tanh", Stanh, 0, 0.0, 0.0, tanh, 0, tanh, |
|
440 "tanh (X): compute tanh (X) for each element of X"); |
|
441 } |
|
442 |
1
|
443 /* |
|
444 ;;; Local Variables: *** |
|
445 ;;; mode: C++ *** |
|
446 ;;; page-delimiter: "^/\\*" *** |
|
447 ;;; End: *** |
|
448 */ |