1967
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cfloat> |
|
28 |
|
29 #include "lo-error.h" |
|
30 #include "lo-ieee.h" |
|
31 #include "lo-mappers.h" |
|
32 #include "lo-utils.h" |
|
33 #include "oct-cmplx.h" |
|
34 #include "oct-math.h" |
|
35 |
|
36 #include "f77-fcn.h" |
|
37 |
|
38 #if defined (_AIX) && defined (__GNUG__) |
|
39 #undef finite |
|
40 #define finite(x) ((x) < DBL_MAX && (x) > -DBL_MAX) |
|
41 #endif |
|
42 |
|
43 extern "C" |
|
44 { |
|
45 double F77_FCN (dgamma, DGAMMA) (const double&); |
|
46 |
|
47 int F77_FCN (dlgams, DLGAMS) (const double&, double&, double&); |
|
48 } |
|
49 |
|
50 #ifndef M_LOG10E |
|
51 #define M_LOG10E 0.43429448190325182765 |
|
52 #endif |
|
53 |
|
54 #ifndef M_PI |
|
55 #define M_PI 3.14159265358979323846 |
|
56 #endif |
|
57 |
|
58 #if defined (HAVE_LGAMMA) && ! defined (SIGNGAM_DECLARED) |
|
59 extern int signgam; |
|
60 #endif |
|
61 |
|
62 // Double -> double mappers. |
|
63 |
|
64 double |
|
65 arg (double x) |
|
66 { |
|
67 if (x < 0.0) |
|
68 return M_PI; |
|
69 else |
|
70 #if defined (HAVE_ISNAN) |
|
71 return xisnan (x) ? octave_NaN : 0.0; |
|
72 #else |
|
73 return 0.0; |
|
74 #endif |
|
75 } |
|
76 |
|
77 double |
|
78 conj (double x) |
|
79 { |
|
80 return x; |
|
81 } |
|
82 |
|
83 double |
|
84 fix (double x) |
|
85 { |
|
86 int tmp; |
|
87 tmp = (int) x; |
|
88 return (double) tmp; |
|
89 } |
|
90 |
|
91 double |
|
92 imag (double x) |
|
93 { |
|
94 #if defined (HAVE_ISNAN) |
|
95 return xisnan (x) ? octave_NaN : 0.0; |
|
96 #else |
|
97 return 0.0; |
|
98 #endif |
|
99 } |
|
100 |
|
101 double |
|
102 real (double x) |
|
103 { |
|
104 return x; |
|
105 } |
|
106 |
|
107 double |
|
108 round (double x) |
|
109 { |
|
110 return D_NINT (x); |
|
111 } |
|
112 |
|
113 double |
|
114 signum (double x) |
|
115 { |
|
116 double tmp = 0.0; |
|
117 if (x < 0.0) |
|
118 tmp = -1.0; |
|
119 else if (x > 0.0) |
|
120 tmp = 1.0; |
|
121 |
|
122 #if defined (HAVE_ISNAN) |
|
123 return xisnan (x) ? octave_NaN : tmp; |
|
124 #else |
|
125 return tmp; |
|
126 #endif |
|
127 } |
|
128 |
|
129 double |
|
130 xerf (double x) |
|
131 { |
|
132 #if defined (HAVE_ERF) |
|
133 return erf (x); |
|
134 #else |
|
135 (*current_liboctave_error_handler) |
|
136 ("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 (*current_liboctave_error_handler) |
|
147 ("erfc (x) not available on this system"); |
|
148 #endif |
|
149 } |
|
150 |
|
151 double |
|
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 1; |
|
170 #endif |
|
171 } |
|
172 |
|
173 double |
|
174 xgamma (double x) |
|
175 { |
|
176 return F77_FCN (dgamma, DGAMMA) (x); |
|
177 } |
|
178 |
|
179 double |
|
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 0; |
|
188 #endif |
|
189 } |
|
190 |
|
191 double |
|
192 xlgamma (double x) |
|
193 { |
|
194 double result; |
|
195 double sgngam; |
|
196 |
|
197 F77_FCN (dlgams, DLGAMS) (x, result, sgngam); |
|
198 |
|
199 return result; |
|
200 } |
|
201 |
|
202 // Complex -> double mappers. |
|
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 |
|
230 // Complex -> complex mappers. |
|
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); |
|
274 Complex retval = log ((1 + x) / (1 - x)) / 2.0; |
|
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 |
|
336 /* |
|
337 ;;; Local Variables: *** |
|
338 ;;; mode: C++ *** |
|
339 ;;; End: *** |
|
340 */ |