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 |
698
|
28 #include <math.h> |
1
|
29 #include <float.h> |
164
|
30 #include <Complex.h> |
1
|
31 |
529
|
32 #include "missing-math.h" |
1173
|
33 #include "f77-uscore.h" |
529
|
34 #include "variables.h" |
1
|
35 #include "mappers.h" |
1308
|
36 #include "sysdep.h" |
699
|
37 #include "error.h" |
1
|
38 #include "utils.h" |
529
|
39 #include "defun.h" |
1
|
40 |
|
41 #if defined (_AIX) && defined (__GNUG__) |
|
42 #undef finite |
|
43 #define finite(x) ((x) < DBL_MAX && (x) > -DBL_MAX) |
|
44 #endif |
|
45 |
1173
|
46 extern "C" |
|
47 { |
1255
|
48 double F77_FCN (dgamma, DGAMMA) (const double&); |
|
49 int F77_FCN (dlgams, DLGAMS) (const double&, double&, double&); |
1173
|
50 } |
|
51 |
65
|
52 #ifndef M_LOG10E |
|
53 #define M_LOG10E 0.43429448190325182765 |
|
54 #endif |
|
55 |
|
56 #ifndef M_PI |
|
57 #define M_PI 3.14159265358979323846 |
|
58 #endif |
|
59 |
706
|
60 #if defined (HAVE_LGAMMA) && ! defined (SIGNGAM_DECLARED) |
|
61 extern int signgam; |
|
62 #endif |
|
63 |
767
|
64 // Double -> double mappers. |
1
|
65 |
|
66 double |
|
67 arg (double x) |
|
68 { |
65
|
69 if (x < 0.0) |
|
70 return M_PI; |
|
71 else |
1308
|
72 #if defined (HAVE_ISNAN) |
|
73 return xisnan (x) ? octave_NaN : 0.0; |
|
74 #else |
65
|
75 return 0.0; |
1308
|
76 #endif |
1
|
77 } |
|
78 |
|
79 double |
|
80 conj (double x) |
|
81 { |
|
82 return x; |
|
83 } |
|
84 |
|
85 double |
|
86 fix (double x) |
|
87 { |
|
88 int tmp; |
|
89 tmp = (int) x; |
|
90 return (double) tmp; |
|
91 } |
|
92 |
|
93 double |
|
94 imag (double x) |
|
95 { |
1308
|
96 #if defined (HAVE_ISNAN) |
|
97 return xisnan (x) ? octave_NaN : 0.0; |
|
98 #else |
1
|
99 return 0.0; |
1308
|
100 #endif |
1
|
101 } |
|
102 |
|
103 double |
|
104 real (double x) |
|
105 { |
|
106 return x; |
|
107 } |
|
108 |
|
109 double |
|
110 round (double x) |
|
111 { |
|
112 return D_NINT (x); |
|
113 } |
|
114 |
|
115 double |
|
116 signum (double x) |
|
117 { |
|
118 double tmp = 0.0; |
|
119 if (x < 0.0) |
|
120 tmp = -1.0; |
|
121 else if (x > 0.0) |
|
122 tmp = 1.0; |
1308
|
123 |
|
124 #if defined (HAVE_ISNAN) |
|
125 return xisnan (x) ? octave_NaN : tmp; |
|
126 #else |
1
|
127 return tmp; |
1308
|
128 #endif |
1
|
129 } |
|
130 |
|
131 double |
624
|
132 xerf (double x) |
|
133 { |
|
134 #if defined (HAVE_ERF) |
|
135 return erf (x); |
|
136 #else |
|
137 error ("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 error ("erfc(x) not available on this system"); |
|
148 #endif |
|
149 } |
|
150 |
|
151 double |
1
|
152 xisnan (double x) |
|
153 { |
|
154 #if defined (HAVE_ISNAN) |
|
155 return (double) isnan (x); |
|
156 #else |
|
157 return 0; |
|
158 #endif |
|
159 } |
|
160 |
|
161 double |
|
162 xfinite (double x) |
|
163 { |
|
164 #if defined (HAVE_FINITE) |
|
165 return (double) finite (x); |
|
166 #elif defined (HAVE_ISINF) && defined (HAVE_ISNAN) |
|
167 return (double) (! isinf (x) && ! isnan (x)); |
|
168 #else |
|
169 return (double) (x > -DBL_MAX && x < DBL_MAX); |
|
170 #endif |
|
171 } |
|
172 |
|
173 double |
624
|
174 xgamma (double x) |
|
175 { |
1255
|
176 return F77_FCN (dgamma, DGAMMA) (x); |
624
|
177 } |
|
178 |
|
179 double |
1
|
180 xisinf (double x) |
|
181 { |
|
182 #if defined (HAVE_ISINF) |
|
183 return (double) isinf (x); |
|
184 #elif defined (HAVE_FINITE) && defined (HAVE_ISNAN) |
|
185 return (double) (! (finite (x) || isnan (x))); |
|
186 #else |
|
187 return (double) (x == DBL_MAX || x == -DBL_MAX); |
|
188 #endif |
|
189 } |
|
190 |
624
|
191 double |
|
192 xlgamma (double x) |
|
193 { |
1173
|
194 double result; |
|
195 double sgngam; |
|
196 |
1255
|
197 F77_FCN (dlgams, DLGAMS) (x, result, sgngam); |
1173
|
198 |
|
199 return result; |
624
|
200 } |
|
201 |
767
|
202 // Complex -> double mappers. |
1
|
203 |
|
204 double |
|
205 xisnan (const Complex& x) |
|
206 { |
|
207 #if defined (HAVE_ISNAN) |
|
208 double rx = real (x); |
|
209 double ix = imag (x); |
|
210 return (double) (isnan (rx) || isnan (ix)); |
|
211 #else |
|
212 return 0; |
|
213 #endif |
|
214 } |
|
215 |
|
216 double |
|
217 xfinite (const Complex& x) |
|
218 { |
|
219 double rx = real (x); |
|
220 double ix = imag (x); |
|
221 return (double) (! ((int) xisinf (rx) || (int) xisinf (ix))); |
|
222 } |
|
223 |
|
224 double |
|
225 xisinf (const Complex& x) |
|
226 { |
|
227 return (double) (! (int) xfinite (x)); |
|
228 } |
|
229 |
767
|
230 // Complex -> complex mappers. |
1
|
231 |
|
232 Complex |
|
233 acos (const Complex& x) |
|
234 { |
|
235 static Complex i (0, 1); |
|
236 Complex retval = -i * log (x + sqrt (x*x - 1.0)); |
|
237 return retval; |
|
238 } |
|
239 |
|
240 Complex |
|
241 acosh (const Complex& x) |
|
242 { |
|
243 Complex retval = log (x + sqrt (x*x - 1.0)); |
|
244 return retval; |
|
245 } |
|
246 |
|
247 Complex |
|
248 asin (const Complex& x) |
|
249 { |
|
250 static Complex i (0, 1); |
|
251 Complex retval = -i * log (i*x + sqrt (1.0 - x*x)); |
|
252 return retval; |
|
253 } |
|
254 |
|
255 Complex |
|
256 asinh (const Complex& x) |
|
257 { |
|
258 Complex retval = log (x + sqrt (x*x + 1.0)); |
|
259 return retval; |
|
260 } |
|
261 |
|
262 Complex |
|
263 atan (const Complex& x) |
|
264 { |
|
265 static Complex i (0, 1); |
|
266 Complex retval = i * log ((i + x) / (i - x)) / 2.0; |
|
267 return retval; |
|
268 } |
|
269 |
|
270 Complex |
|
271 atanh (const Complex& x) |
|
272 { |
|
273 static Complex i (0, 1); |
331
|
274 Complex retval = log ((1 + x) / (1 - x)) / 2.0; |
1
|
275 return retval; |
|
276 } |
|
277 |
|
278 Complex |
|
279 ceil (const Complex& x) |
|
280 { |
|
281 int re = (int) ceil (real (x)); |
|
282 int im = (int) ceil (imag (x)); |
|
283 return Complex (re, im); |
|
284 } |
|
285 |
|
286 Complex |
|
287 fix (const Complex& x) |
|
288 { |
|
289 int re = (int) real (x); |
|
290 int im = (int) imag (x); |
|
291 return Complex (re, im); |
|
292 } |
|
293 |
|
294 Complex |
|
295 floor (const Complex& x) |
|
296 { |
|
297 int re = (int) floor (real (x)); |
|
298 int im = (int) floor (imag (x)); |
|
299 return Complex (re, im); |
|
300 } |
|
301 |
|
302 Complex |
|
303 log10 (const Complex& x) |
|
304 { |
|
305 return M_LOG10E * log (x); |
|
306 } |
|
307 |
|
308 Complex |
|
309 round (const Complex& x) |
|
310 { |
|
311 double re = D_NINT (real (x)); |
|
312 double im = D_NINT (imag (x)); |
|
313 return Complex (re, im); |
|
314 } |
|
315 |
|
316 Complex |
|
317 signum (const Complex& x) |
|
318 { |
|
319 return x / abs (x); |
|
320 } |
|
321 |
|
322 Complex |
|
323 tan (const Complex& x) |
|
324 { |
|
325 Complex retval = sin (x) / cos (x); |
|
326 return retval; |
|
327 } |
|
328 |
|
329 Complex |
|
330 tanh (const Complex& x) |
|
331 { |
|
332 Complex retval = sinh (x) / cosh (x); |
|
333 return retval; |
|
334 } |
|
335 |
529
|
336 void |
|
337 install_mapper_functions (void) |
|
338 { |
|
339 DEFUN_MAPPER ("abs", Sabs, 0, 0.0, 0.0, fabs, abs, 0, |
|
340 "abs (X): compute abs (X) for each element of X"); |
|
341 |
|
342 DEFUN_MAPPER ("acos", Sacos, 1, -1.0, 1.0, acos, 0, acos, |
|
343 "acos (X): compute acos (X) for each element of X"); |
|
344 |
|
345 DEFUN_MAPPER ("acosh", Sacosh, 1, 1.0, DBL_MAX, acosh, 0, acosh, |
|
346 "acosh (X): compute acosh (X) for each element of X"); |
|
347 |
|
348 DEFUN_MAPPER ("angle", Sangle, 0, 0.0, 0.0, arg, arg, 0, |
|
349 "angle (X): compute arg (X) for each element of X"); |
|
350 |
|
351 DEFUN_MAPPER ("arg", Sarg, 0, 0.0, 0.0, arg, arg, 0, |
|
352 "arg (X): compute arg (X) for each element of X"); |
|
353 |
|
354 DEFUN_MAPPER ("asin", Sasin, 1, -1.0, 1.0, asin, 0, asin, |
|
355 "asin (X): compute asin (X) for each element of X"); |
|
356 |
|
357 DEFUN_MAPPER ("asinh", Sasinh, 0, 0.0, 0.0, asinh, 0, asinh, |
|
358 "asinh (X): compute asinh (X) for each element of X"); |
|
359 |
|
360 DEFUN_MAPPER ("atan", Satan, 0, 0.0, 0.0, atan, 0, atan, |
|
361 "atan (X): compute atan (X) for each element of X"); |
|
362 |
|
363 DEFUN_MAPPER ("atanh", Satanh, 1, -1.0, 1.0, atanh, 0, atanh, |
|
364 "atanh (X): compute atanh (X) for each element of X"); |
|
365 |
|
366 DEFUN_MAPPER ("ceil", Sceil, 0, 0.0, 0.0, ceil, 0, ceil, |
|
367 "ceil (X): round elements of X toward +Inf"); |
|
368 |
|
369 DEFUN_MAPPER ("conj", Sconj, 0, 0.0, 0.0, conj, 0, conj, |
|
370 "conj (X): compute complex conjugate for each element of X"); |
|
371 |
|
372 DEFUN_MAPPER ("cos", Scos, 0, 0.0, 0.0, cos, 0, cos, |
|
373 "cos (X): compute cos (X) for each element of X"); |
|
374 |
|
375 DEFUN_MAPPER ("cosh", Scosh, 0, 0.0, 0.0, cosh, 0, cosh, |
|
376 "cosh (X): compute cosh (X) for each element of X"); |
|
377 |
624
|
378 DEFUN_MAPPER ("erf", Serf, 0, 0.0, 0.0, xerf, 0, 0, |
|
379 "erf (X): compute erf (X) for each element of X"); |
|
380 |
|
381 DEFUN_MAPPER ("erfc", Serfc, 0, 0.0, 0.0, xerfc, 0, 0, |
|
382 "erfc (X): compute erfc (X) for each element of X"); |
|
383 |
529
|
384 DEFUN_MAPPER ("exp", Sexp, 0, 0.0, 0.0, exp, 0, exp, |
|
385 "exp (X): compute exp (X) for each element of X"); |
|
386 |
|
387 DEFUN_MAPPER ("finite", Sfinite, 0, 0.0, 0.0, xfinite, xfinite, 0, |
|
388 "finite (X): return 1 for finite elements of X"); |
|
389 |
|
390 DEFUN_MAPPER ("fix", Sfix, 0, 0.0, 0.0, fix, 0, fix, |
|
391 "fix (X): round elements of X toward zero"); |
|
392 |
|
393 DEFUN_MAPPER ("floor", Sfloor, 0, 0.0, 0.0, floor, 0, floor, |
|
394 "floor (X): round elements of X toward -Inf"); |
|
395 |
624
|
396 DEFUN_MAPPER ("gamma", Sgamma, 0, 0.0, 0.0, xgamma, 0, 0, |
|
397 "gamma (X): compute gamma (X) for each element of X"); |
|
398 |
529
|
399 DEFUN_MAPPER ("isinf", Sisinf, 0, 0.0, 0.0, xisinf, xisinf, 0, |
|
400 "isinf (X): return 1 for elements of X infinite"); |
|
401 |
|
402 DEFUN_MAPPER ("imag", Simag, 0, 0.0, 0.0, imag, imag, 0, |
|
403 "imag (X): return imaginary part for each elements of X"); |
|
404 |
|
405 DEFUN_MAPPER ("isnan", Sisnan, 0, 0.0, 0.0, xisnan, xisnan, 0, |
|
406 "isnan (X): return 1 where elements of X are NaNs"); |
624
|
407 |
|
408 DEFUN_MAPPER ("lgamma", Slgamma, 0, 0.0, 0.0, xlgamma, 0, 0, |
|
409 "lgamma (X): compute log gamma (X) for each element of X"); |
529
|
410 |
|
411 DEFUN_MAPPER ("log", Slog, 1, 0.0, DBL_MAX, log, 0, log, |
|
412 "log (X): compute log (X) for each element of X"); |
|
413 |
|
414 DEFUN_MAPPER ("log10", Slog10, 1, 0.0, DBL_MAX, log10, 0, log10, |
|
415 "log10 (X): compute log10 (X) for each element of X"); |
|
416 |
|
417 DEFUN_MAPPER ("real", Sreal, 0, 0.0, 0.0, real, real, 0, |
|
418 "real (X): return real part for each element of X"); |
|
419 |
|
420 DEFUN_MAPPER ("round", Sround, 0, 0.0, 0.0, round, 0, round, |
|
421 "round (X): round elements of X to nearest integer"); |
|
422 |
|
423 DEFUN_MAPPER ("sign", Ssign, 0, 0.0, 0.0, signum, 0, signum, |
|
424 "sign (X): apply signum function to elements of X"); |
|
425 |
|
426 DEFUN_MAPPER ("sin", Ssin, 0, 0.0, 0.0, sin, 0, sin, |
|
427 "sin (X): compute sin (X) for each element of X"); |
|
428 |
|
429 DEFUN_MAPPER ("sinh", Ssinh, 0, 0.0, 0.0, sinh, 0, sinh, |
|
430 "sinh (X): compute sinh (X) for each element of X"); |
|
431 |
|
432 DEFUN_MAPPER ("sqrt", Ssqrt, 1, 0.0, DBL_MAX, sqrt, 0, sqrt, |
|
433 "sqrt (X): compute sqrt (X) for each element of X"); |
|
434 |
|
435 DEFUN_MAPPER ("tan", Stan, 0, 0.0, 0.0, tan, 0, tan, |
|
436 "tan (X): compute tan (X) for each element of X"); |
|
437 |
|
438 DEFUN_MAPPER ("tanh", Stanh, 0, 0.0, 0.0, tanh, 0, tanh, |
|
439 "tanh (X): compute tanh (X) for each element of X"); |
|
440 } |
|
441 |
1
|
442 /* |
|
443 ;;; Local Variables: *** |
|
444 ;;; mode: C++ *** |
|
445 ;;; page-delimiter: "^/\\*" *** |
|
446 ;;; End: *** |
|
447 */ |