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