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