1967
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1967
|
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> |
3121
|
28 #include <cmath> |
1967
|
29 |
3268
|
30 #if defined (HAVE_IEEEFP_H) |
|
31 #include <ieeefp.h> |
|
32 #endif |
|
33 |
|
34 #if defined (HAVE_SUNMATH_H) |
|
35 #include <sunmath.h> |
|
36 #endif |
|
37 |
1967
|
38 #include "lo-error.h" |
|
39 #include "lo-ieee.h" |
|
40 #include "lo-mappers.h" |
3121
|
41 #include "lo-specfun.h" |
1967
|
42 #include "lo-utils.h" |
|
43 #include "oct-cmplx.h" |
|
44 |
|
45 #include "f77-fcn.h" |
|
46 |
|
47 #if defined (_AIX) && defined (__GNUG__) |
|
48 #undef finite |
|
49 #define finite(x) ((x) < DBL_MAX && (x) > -DBL_MAX) |
|
50 #endif |
|
51 |
|
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 |
3248
|
60 // double -> double mappers. |
1967
|
61 |
|
62 double |
|
63 arg (double x) |
|
64 { |
|
65 if (x < 0.0) |
|
66 return M_PI; |
|
67 else |
|
68 #if defined (HAVE_ISNAN) |
|
69 return xisnan (x) ? octave_NaN : 0.0; |
|
70 #else |
|
71 return 0.0; |
|
72 #endif |
|
73 } |
|
74 |
|
75 double |
|
76 conj (double x) |
|
77 { |
|
78 return x; |
|
79 } |
|
80 |
|
81 double |
|
82 fix (double x) |
|
83 { |
3012
|
84 return x > 0 ? floor (x) : ceil (x); |
1967
|
85 } |
|
86 |
|
87 double |
|
88 imag (double x) |
|
89 { |
|
90 #if defined (HAVE_ISNAN) |
|
91 return xisnan (x) ? octave_NaN : 0.0; |
|
92 #else |
|
93 return 0.0; |
|
94 #endif |
|
95 } |
|
96 |
|
97 double |
|
98 real (double x) |
|
99 { |
|
100 return x; |
|
101 } |
|
102 |
|
103 double |
|
104 round (double x) |
|
105 { |
|
106 return D_NINT (x); |
|
107 } |
|
108 |
|
109 double |
|
110 signum (double x) |
|
111 { |
|
112 double tmp = 0.0; |
|
113 if (x < 0.0) |
|
114 tmp = -1.0; |
|
115 else if (x > 0.0) |
|
116 tmp = 1.0; |
|
117 |
|
118 #if defined (HAVE_ISNAN) |
|
119 return xisnan (x) ? octave_NaN : tmp; |
|
120 #else |
|
121 return tmp; |
|
122 #endif |
|
123 } |
|
124 |
|
125 double |
|
126 xerf (double x) |
|
127 { |
|
128 #if defined (HAVE_ERF) |
|
129 return erf (x); |
|
130 #else |
|
131 (*current_liboctave_error_handler) |
|
132 ("erf (x) not available on this system"); |
|
133 #endif |
|
134 } |
|
135 |
|
136 double |
|
137 xerfc (double x) |
|
138 { |
|
139 #if defined (HAVE_ERFC) |
|
140 return erfc (x); |
|
141 #else |
|
142 (*current_liboctave_error_handler) |
|
143 ("erfc (x) not available on this system"); |
|
144 #endif |
|
145 } |
|
146 |
3248
|
147 // double -> bool mappers. |
|
148 |
|
149 bool |
1967
|
150 xisnan (double x) |
|
151 { |
|
152 #if defined (HAVE_ISNAN) |
3069
|
153 return isnan (x) != 0; |
1967
|
154 #else |
3248
|
155 return false; |
1967
|
156 #endif |
|
157 } |
|
158 |
3248
|
159 bool |
1967
|
160 xfinite (double x) |
|
161 { |
|
162 #if defined (HAVE_FINITE) |
3069
|
163 return finite (x) != 0; |
1967
|
164 #elif defined (HAVE_ISINF) && defined (HAVE_ISNAN) |
2800
|
165 return (! isinf (x) && ! isnan (x)); |
1967
|
166 #else |
3248
|
167 return true; |
1967
|
168 #endif |
|
169 } |
|
170 |
3248
|
171 bool |
1967
|
172 xisinf (double x) |
|
173 { |
|
174 #if defined (HAVE_ISINF) |
2800
|
175 return isinf (x); |
1967
|
176 #elif defined (HAVE_FINITE) && defined (HAVE_ISNAN) |
2800
|
177 return (! (finite (x) || isnan (x))); |
1967
|
178 #else |
3248
|
179 return false; |
1967
|
180 #endif |
|
181 } |
|
182 |
3248
|
183 // (double, double) -> double mappers. |
1967
|
184 |
|
185 double |
3248
|
186 xmin (double x, double y) |
1967
|
187 { |
3248
|
188 return x < y ? x : (xisnan (x) ? x : y); |
1967
|
189 } |
|
190 |
|
191 double |
3248
|
192 xmax (double x, double y) |
1967
|
193 { |
3248
|
194 return x > y ? x : (xisnan (x) ? x : y); |
1967
|
195 } |
|
196 |
3248
|
197 // complex -> complex mappers. |
1967
|
198 |
|
199 Complex |
|
200 acos (const Complex& x) |
|
201 { |
|
202 static Complex i (0, 1); |
3056
|
203 |
|
204 return (real (x) * imag (x) < 0.0) ? i * acosh (x) : -i * acosh (x); |
1967
|
205 } |
|
206 |
|
207 Complex |
|
208 acosh (const Complex& x) |
|
209 { |
3056
|
210 return log (x + sqrt (x*x - 1.0)); |
1967
|
211 } |
|
212 |
|
213 Complex |
|
214 asin (const Complex& x) |
|
215 { |
|
216 static Complex i (0, 1); |
3056
|
217 |
|
218 return -i * log (i*x + sqrt (1.0 - x*x)); |
1967
|
219 } |
|
220 |
|
221 Complex |
|
222 asinh (const Complex& x) |
|
223 { |
3056
|
224 return log (x + sqrt (x*x + 1.0)); |
1967
|
225 } |
|
226 |
|
227 Complex |
|
228 atan (const Complex& x) |
|
229 { |
|
230 static Complex i (0, 1); |
3056
|
231 |
|
232 return i * log ((i + x) / (i - x)) / 2.0; |
1967
|
233 } |
|
234 |
|
235 Complex |
|
236 atanh (const Complex& x) |
|
237 { |
3092
|
238 return log ((1.0 + x) / (1.0 - x)) / 2.0; |
1967
|
239 } |
|
240 |
|
241 Complex |
|
242 ceil (const Complex& x) |
|
243 { |
2800
|
244 return Complex (ceil (real (x)), ceil (imag (x))); |
1967
|
245 } |
|
246 |
|
247 Complex |
|
248 fix (const Complex& x) |
|
249 { |
3012
|
250 return Complex (fix (real (x)), fix (imag (x))); |
1967
|
251 } |
|
252 |
|
253 Complex |
|
254 floor (const Complex& x) |
|
255 { |
2800
|
256 return Complex (floor (real (x)), floor (imag (x))); |
1967
|
257 } |
|
258 |
|
259 Complex |
|
260 log10 (const Complex& x) |
|
261 { |
|
262 return M_LOG10E * log (x); |
|
263 } |
|
264 |
|
265 Complex |
|
266 round (const Complex& x) |
|
267 { |
2800
|
268 return Complex (D_NINT (real (x)), D_NINT (imag (x))); |
1967
|
269 } |
|
270 |
|
271 Complex |
|
272 signum (const Complex& x) |
|
273 { |
|
274 return x / abs (x); |
|
275 } |
|
276 |
|
277 Complex |
|
278 tan (const Complex& x) |
|
279 { |
3056
|
280 return sin (x) / cos (x); |
1967
|
281 } |
|
282 |
|
283 Complex |
|
284 tanh (const Complex& x) |
|
285 { |
3056
|
286 return sinh (x) / cosh (x); |
1967
|
287 } |
|
288 |
3248
|
289 // complex -> bool mappers. |
|
290 |
|
291 bool |
|
292 xisnan (const Complex& x) |
|
293 { |
|
294 #if defined (HAVE_ISNAN) |
|
295 return (isnan (real (x)) || isnan (imag (x))); |
|
296 #else |
|
297 return false; |
|
298 #endif |
|
299 } |
|
300 |
|
301 bool |
|
302 xfinite (const Complex& x) |
|
303 { |
|
304 return (xfinite (real (x)) && xfinite (imag (x))); |
|
305 } |
|
306 |
|
307 bool |
|
308 xisinf (const Complex& x) |
|
309 { |
|
310 return (xisinf (real (x)) || xisinf (imag (x))); |
|
311 } |
|
312 |
|
313 // (complex, complex) -> complex mappers. |
|
314 |
|
315 Complex |
|
316 xmin (const Complex& x, const Complex& y) |
|
317 { |
|
318 return abs (x) < abs (y) ? x : (xisnan (x) ? x : y); |
|
319 } |
|
320 |
|
321 Complex |
|
322 xmax (const Complex& x, const Complex& y) |
|
323 { |
|
324 return abs (x) > abs (y) ? x : (xisnan (x) ? x : y); |
|
325 } |
|
326 |
1967
|
327 /* |
|
328 ;;; Local Variables: *** |
|
329 ;;; mode: C++ *** |
|
330 ;;; End: *** |
|
331 */ |