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