1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1343
|
27 #include <cfloat> |
|
28 |
1651
|
29 #include "oct-cmplx.h" |
1769
|
30 #include "oct-math.h" |
1
|
31 |
1352
|
32 #include "defun.h" |
|
33 #include "error.h" |
1847
|
34 #include "f77-fcn.h" |
1
|
35 #include "mappers.h" |
1308
|
36 #include "sysdep.h" |
1
|
37 #include "utils.h" |
1352
|
38 #include "variables.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 { |
1255
|
47 double F77_FCN (dgamma, DGAMMA) (const double&); |
|
48 int F77_FCN (dlgams, DLGAMS) (const double&, double&, double&); |
1173
|
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 |
1308
|
71 #if defined (HAVE_ISNAN) |
|
72 return xisnan (x) ? octave_NaN : 0.0; |
|
73 #else |
65
|
74 return 0.0; |
1308
|
75 #endif |
1
|
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 { |
1308
|
95 #if defined (HAVE_ISNAN) |
|
96 return xisnan (x) ? octave_NaN : 0.0; |
|
97 #else |
1
|
98 return 0.0; |
1308
|
99 #endif |
1
|
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; |
1308
|
122 |
|
123 #if defined (HAVE_ISNAN) |
|
124 return xisnan (x) ? octave_NaN : tmp; |
|
125 #else |
1
|
126 return tmp; |
1308
|
127 #endif |
1
|
128 } |
|
129 |
|
130 double |
624
|
131 xerf (double x) |
|
132 { |
|
133 #if defined (HAVE_ERF) |
|
134 return erf (x); |
|
135 #else |
|
136 error ("erf(x) not available on this system"); |
|
137 #endif |
|
138 } |
|
139 |
|
140 double |
|
141 xerfc (double x) |
|
142 { |
|
143 #if defined (HAVE_ERFC) |
|
144 return erfc (x); |
|
145 #else |
|
146 error ("erfc(x) not available on this system"); |
|
147 #endif |
|
148 } |
|
149 |
|
150 double |
1
|
151 xisnan (double x) |
|
152 { |
|
153 #if defined (HAVE_ISNAN) |
|
154 return (double) isnan (x); |
|
155 #else |
|
156 return 0; |
|
157 #endif |
|
158 } |
|
159 |
|
160 double |
|
161 xfinite (double x) |
|
162 { |
|
163 #if defined (HAVE_FINITE) |
|
164 return (double) finite (x); |
|
165 #elif defined (HAVE_ISINF) && defined (HAVE_ISNAN) |
|
166 return (double) (! isinf (x) && ! isnan (x)); |
|
167 #else |
1384
|
168 return 1; |
1
|
169 #endif |
|
170 } |
|
171 |
|
172 double |
624
|
173 xgamma (double x) |
|
174 { |
1255
|
175 return F77_FCN (dgamma, DGAMMA) (x); |
624
|
176 } |
|
177 |
|
178 double |
1
|
179 xisinf (double x) |
|
180 { |
|
181 #if defined (HAVE_ISINF) |
|
182 return (double) isinf (x); |
|
183 #elif defined (HAVE_FINITE) && defined (HAVE_ISNAN) |
|
184 return (double) (! (finite (x) || isnan (x))); |
|
185 #else |
1384
|
186 return 0; |
1
|
187 #endif |
|
188 } |
|
189 |
624
|
190 double |
|
191 xlgamma (double x) |
|
192 { |
1173
|
193 double result; |
|
194 double sgngam; |
|
195 |
1255
|
196 F77_FCN (dlgams, DLGAMS) (x, result, sgngam); |
1173
|
197 |
|
198 return result; |
624
|
199 } |
|
200 |
767
|
201 // Complex -> double mappers. |
1
|
202 |
|
203 double |
|
204 xisnan (const Complex& x) |
|
205 { |
|
206 #if defined (HAVE_ISNAN) |
|
207 double rx = real (x); |
|
208 double ix = imag (x); |
|
209 return (double) (isnan (rx) || isnan (ix)); |
|
210 #else |
|
211 return 0; |
|
212 #endif |
|
213 } |
|
214 |
|
215 double |
|
216 xfinite (const Complex& x) |
|
217 { |
|
218 double rx = real (x); |
|
219 double ix = imag (x); |
|
220 return (double) (! ((int) xisinf (rx) || (int) xisinf (ix))); |
|
221 } |
|
222 |
|
223 double |
|
224 xisinf (const Complex& x) |
|
225 { |
|
226 return (double) (! (int) xfinite (x)); |
|
227 } |
|
228 |
767
|
229 // Complex -> complex mappers. |
1
|
230 |
|
231 Complex |
|
232 acos (const Complex& x) |
|
233 { |
|
234 static Complex i (0, 1); |
|
235 Complex retval = -i * log (x + sqrt (x*x - 1.0)); |
|
236 return retval; |
|
237 } |
|
238 |
|
239 Complex |
|
240 acosh (const Complex& x) |
|
241 { |
|
242 Complex retval = log (x + sqrt (x*x - 1.0)); |
|
243 return retval; |
|
244 } |
|
245 |
|
246 Complex |
|
247 asin (const Complex& x) |
|
248 { |
|
249 static Complex i (0, 1); |
|
250 Complex retval = -i * log (i*x + sqrt (1.0 - x*x)); |
|
251 return retval; |
|
252 } |
|
253 |
|
254 Complex |
|
255 asinh (const Complex& x) |
|
256 { |
|
257 Complex retval = log (x + sqrt (x*x + 1.0)); |
|
258 return retval; |
|
259 } |
|
260 |
|
261 Complex |
|
262 atan (const Complex& x) |
|
263 { |
|
264 static Complex i (0, 1); |
|
265 Complex retval = i * log ((i + x) / (i - x)) / 2.0; |
|
266 return retval; |
|
267 } |
|
268 |
|
269 Complex |
|
270 atanh (const Complex& x) |
|
271 { |
|
272 static Complex i (0, 1); |
331
|
273 Complex retval = log ((1 + x) / (1 - x)) / 2.0; |
1
|
274 return retval; |
|
275 } |
|
276 |
|
277 Complex |
|
278 ceil (const Complex& x) |
|
279 { |
|
280 int re = (int) ceil (real (x)); |
|
281 int im = (int) ceil (imag (x)); |
|
282 return Complex (re, im); |
|
283 } |
|
284 |
|
285 Complex |
|
286 fix (const Complex& x) |
|
287 { |
|
288 int re = (int) real (x); |
|
289 int im = (int) imag (x); |
|
290 return Complex (re, im); |
|
291 } |
|
292 |
|
293 Complex |
|
294 floor (const Complex& x) |
|
295 { |
|
296 int re = (int) floor (real (x)); |
|
297 int im = (int) floor (imag (x)); |
|
298 return Complex (re, im); |
|
299 } |
|
300 |
|
301 Complex |
|
302 log10 (const Complex& x) |
|
303 { |
|
304 return M_LOG10E * log (x); |
|
305 } |
|
306 |
|
307 Complex |
|
308 round (const Complex& x) |
|
309 { |
|
310 double re = D_NINT (real (x)); |
|
311 double im = D_NINT (imag (x)); |
|
312 return Complex (re, im); |
|
313 } |
|
314 |
|
315 Complex |
|
316 signum (const Complex& x) |
|
317 { |
|
318 return x / abs (x); |
|
319 } |
|
320 |
|
321 Complex |
|
322 tan (const Complex& x) |
|
323 { |
|
324 Complex retval = sin (x) / cos (x); |
|
325 return retval; |
|
326 } |
|
327 |
|
328 Complex |
|
329 tanh (const Complex& x) |
|
330 { |
|
331 Complex retval = sinh (x) / cosh (x); |
|
332 return retval; |
|
333 } |
|
334 |
529
|
335 void |
|
336 install_mapper_functions (void) |
|
337 { |
1957
|
338 DEFUN_MAPPER (abs, 0, 0.0, 0.0, fabs, abs, 0, |
529
|
339 "abs (X): compute abs (X) for each element of X"); |
|
340 |
1957
|
341 DEFUN_MAPPER (acos, 1, -1.0, 1.0, acos, 0, acos, |
529
|
342 "acos (X): compute acos (X) for each element of X"); |
|
343 |
1957
|
344 DEFUN_MAPPER (acosh, 1, 1.0, DBL_MAX, acosh, 0, acosh, |
529
|
345 "acosh (X): compute acosh (X) for each element of X"); |
|
346 |
1957
|
347 DEFUN_MAPPER (angle, 0, 0.0, 0.0, arg, arg, 0, |
529
|
348 "angle (X): compute arg (X) for each element of X"); |
|
349 |
1957
|
350 DEFUN_MAPPER (arg, 0, 0.0, 0.0, arg, arg, 0, |
529
|
351 "arg (X): compute arg (X) for each element of X"); |
|
352 |
1957
|
353 DEFUN_MAPPER (asin, 1, -1.0, 1.0, asin, 0, asin, |
529
|
354 "asin (X): compute asin (X) for each element of X"); |
|
355 |
1957
|
356 DEFUN_MAPPER (asinh, 0, 0.0, 0.0, asinh, 0, asinh, |
529
|
357 "asinh (X): compute asinh (X) for each element of X"); |
|
358 |
1957
|
359 DEFUN_MAPPER (atan, 0, 0.0, 0.0, atan, 0, atan, |
529
|
360 "atan (X): compute atan (X) for each element of X"); |
|
361 |
1957
|
362 DEFUN_MAPPER (atanh, 1, -1.0, 1.0, atanh, 0, atanh, |
529
|
363 "atanh (X): compute atanh (X) for each element of X"); |
|
364 |
1957
|
365 DEFUN_MAPPER (ceil, 0, 0.0, 0.0, ceil, 0, ceil, |
529
|
366 "ceil (X): round elements of X toward +Inf"); |
|
367 |
1957
|
368 DEFUN_MAPPER (conj, 0, 0.0, 0.0, conj, 0, conj, |
529
|
369 "conj (X): compute complex conjugate for each element of X"); |
|
370 |
1957
|
371 DEFUN_MAPPER (cos, 0, 0.0, 0.0, cos, 0, cos, |
529
|
372 "cos (X): compute cos (X) for each element of X"); |
|
373 |
1957
|
374 DEFUN_MAPPER (cosh, 0, 0.0, 0.0, cosh, 0, cosh, |
529
|
375 "cosh (X): compute cosh (X) for each element of X"); |
|
376 |
1957
|
377 DEFUN_MAPPER (erf, 0, 0.0, 0.0, xerf, 0, 0, |
624
|
378 "erf (X): compute erf (X) for each element of X"); |
|
379 |
1957
|
380 DEFUN_MAPPER (erfc, 0, 0.0, 0.0, xerfc, 0, 0, |
624
|
381 "erfc (X): compute erfc (X) for each element of X"); |
|
382 |
1957
|
383 DEFUN_MAPPER (exp, 0, 0.0, 0.0, exp, 0, exp, |
529
|
384 "exp (X): compute exp (X) for each element of X"); |
|
385 |
1957
|
386 DEFUN_MAPPER (finite, 0, 0.0, 0.0, xfinite, xfinite, 0, |
529
|
387 "finite (X): return 1 for finite elements of X"); |
|
388 |
1957
|
389 DEFUN_MAPPER (fix, 0, 0.0, 0.0, fix, 0, fix, |
529
|
390 "fix (X): round elements of X toward zero"); |
|
391 |
1957
|
392 DEFUN_MAPPER (floor, 0, 0.0, 0.0, floor, 0, floor, |
529
|
393 "floor (X): round elements of X toward -Inf"); |
|
394 |
1957
|
395 DEFUN_MAPPER (gamma, 0, 0.0, 0.0, xgamma, 0, 0, |
624
|
396 "gamma (X): compute gamma (X) for each element of X"); |
|
397 |
1957
|
398 DEFUN_MAPPER (isinf, 0, 0.0, 0.0, xisinf, xisinf, 0, |
529
|
399 "isinf (X): return 1 for elements of X infinite"); |
|
400 |
1957
|
401 DEFUN_MAPPER (imag, 0, 0.0, 0.0, imag, imag, 0, |
529
|
402 "imag (X): return imaginary part for each elements of X"); |
|
403 |
1957
|
404 DEFUN_MAPPER (isnan, 0, 0.0, 0.0, xisnan, xisnan, 0, |
529
|
405 "isnan (X): return 1 where elements of X are NaNs"); |
624
|
406 |
1957
|
407 DEFUN_MAPPER (lgamma, 0, 0.0, 0.0, xlgamma, 0, 0, |
624
|
408 "lgamma (X): compute log gamma (X) for each element of X"); |
529
|
409 |
1957
|
410 DEFUN_MAPPER (log, 1, 0.0, DBL_MAX, log, 0, log, |
529
|
411 "log (X): compute log (X) for each element of X"); |
|
412 |
1957
|
413 DEFUN_MAPPER (log10, 1, 0.0, DBL_MAX, log10, 0, log10, |
529
|
414 "log10 (X): compute log10 (X) for each element of X"); |
|
415 |
1957
|
416 DEFUN_MAPPER (real, 0, 0.0, 0.0, real, real, 0, |
529
|
417 "real (X): return real part for each element of X"); |
|
418 |
1957
|
419 DEFUN_MAPPER (round, 0, 0.0, 0.0, round, 0, round, |
529
|
420 "round (X): round elements of X to nearest integer"); |
|
421 |
1957
|
422 DEFUN_MAPPER (sign, 0, 0.0, 0.0, signum, 0, signum, |
529
|
423 "sign (X): apply signum function to elements of X"); |
|
424 |
1957
|
425 DEFUN_MAPPER (sin, 0, 0.0, 0.0, sin, 0, sin, |
529
|
426 "sin (X): compute sin (X) for each element of X"); |
|
427 |
1957
|
428 DEFUN_MAPPER (sinh, 0, 0.0, 0.0, sinh, 0, sinh, |
529
|
429 "sinh (X): compute sinh (X) for each element of X"); |
|
430 |
1957
|
431 DEFUN_MAPPER (sqrt, 1, 0.0, DBL_MAX, sqrt, 0, sqrt, |
529
|
432 "sqrt (X): compute sqrt (X) for each element of X"); |
|
433 |
1957
|
434 DEFUN_MAPPER (tan, 0, 0.0, 0.0, tan, 0, tan, |
529
|
435 "tan (X): compute tan (X) for each element of X"); |
|
436 |
1957
|
437 DEFUN_MAPPER (tanh, 0, 0.0, 0.0, tanh, 0, tanh, |
529
|
438 "tanh (X): compute tanh (X) for each element of X"); |
1562
|
439 |
|
440 DEFALIAS (gammaln, lgamma); |
529
|
441 } |
|
442 |
1
|
443 /* |
|
444 ;;; Local Variables: *** |
|
445 ;;; mode: C++ *** |
|
446 ;;; End: *** |
|
447 */ |