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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1967
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cfloat> |
3121
|
29 #include <cmath> |
1967
|
30 |
3268
|
31 #if defined (HAVE_SUNMATH_H) |
|
32 #include <sunmath.h> |
|
33 #endif |
|
34 |
1967
|
35 #include "lo-error.h" |
|
36 #include "lo-ieee.h" |
|
37 #include "lo-mappers.h" |
3121
|
38 #include "lo-specfun.h" |
1967
|
39 #include "lo-utils.h" |
|
40 #include "oct-cmplx.h" |
|
41 |
|
42 #include "f77-fcn.h" |
|
43 |
3248
|
44 // double -> double mappers. |
1967
|
45 |
|
46 double |
|
47 arg (double x) |
|
48 { |
3873
|
49 return atan2 (0.0, x); |
1967
|
50 } |
|
51 |
|
52 double |
|
53 conj (double x) |
|
54 { |
|
55 return x; |
|
56 } |
|
57 |
|
58 double |
|
59 fix (double x) |
|
60 { |
3012
|
61 return x > 0 ? floor (x) : ceil (x); |
1967
|
62 } |
|
63 |
|
64 double |
4662
|
65 imag (double) |
1967
|
66 { |
4313
|
67 return 0.0; |
1967
|
68 } |
|
69 |
|
70 double |
|
71 real (double x) |
|
72 { |
|
73 return x; |
|
74 } |
|
75 |
|
76 double |
4968
|
77 xround (double x) |
1967
|
78 { |
4984
|
79 #if defined (HAVE_ROUND) |
4968
|
80 return round (x); |
|
81 #else |
4984
|
82 return x > 0 ? floor (x + 0.5) : ceil (x - 0.5); |
4968
|
83 #endif |
1967
|
84 } |
|
85 |
|
86 double |
|
87 signum (double x) |
|
88 { |
|
89 double tmp = 0.0; |
4984
|
90 |
1967
|
91 if (x < 0.0) |
|
92 tmp = -1.0; |
|
93 else if (x > 0.0) |
|
94 tmp = 1.0; |
|
95 |
|
96 return xisnan (x) ? octave_NaN : tmp; |
|
97 } |
|
98 |
5904
|
99 double |
|
100 xlog2 (double x) |
|
101 { |
|
102 #if defined (HAVE_LOG2) |
|
103 return log2 (x); |
|
104 #else |
|
105 #if defined (M_LN2) |
|
106 static double ln2 = M_LN2; |
|
107 #else |
|
108 static double ln2 = log2 (2); |
|
109 #endif |
|
110 |
|
111 return log (x) / ln2; |
|
112 #endif |
|
113 } |
|
114 |
|
115 double |
|
116 xexp2 (double x) |
|
117 { |
|
118 #if defined (HAVE_EXP2) |
|
119 return exp2 (x); |
|
120 #else |
|
121 #if defined (M_LN2) |
|
122 static double ln2 = M_LN2; |
|
123 #else |
|
124 static double ln2 = log2 (2); |
|
125 #endif |
|
126 |
|
127 return exp (x * ln2); |
|
128 #endif |
|
129 } |
|
130 |
3248
|
131 // double -> bool mappers. |
|
132 |
|
133 bool |
1967
|
134 xisnan (double x) |
|
135 { |
4074
|
136 return lo_ieee_isnan (x); |
1967
|
137 } |
|
138 |
3248
|
139 bool |
1967
|
140 xfinite (double x) |
|
141 { |
4074
|
142 return lo_ieee_finite (x); |
1967
|
143 } |
|
144 |
3248
|
145 bool |
1967
|
146 xisinf (double x) |
|
147 { |
4074
|
148 return lo_ieee_isinf (x); |
1967
|
149 } |
|
150 |
4025
|
151 bool |
|
152 octave_is_NA (double x) |
|
153 { |
|
154 return lo_ieee_is_NA (x); |
|
155 } |
|
156 |
|
157 bool |
|
158 octave_is_NaN_or_NA (double x) |
|
159 { |
5389
|
160 return lo_ieee_isnan (x); |
4025
|
161 } |
|
162 |
3248
|
163 // (double, double) -> double mappers. |
1967
|
164 |
5775
|
165 // FIXME -- need to handle NA too? |
4025
|
166 |
1967
|
167 double |
3248
|
168 xmin (double x, double y) |
1967
|
169 { |
4469
|
170 if (x < y) |
|
171 return x; |
|
172 |
|
173 if (y <= x) |
|
174 return y; |
|
175 |
5389
|
176 if (xisnan (x) && ! xisnan (y)) |
4469
|
177 return y; |
5389
|
178 else if (xisnan (y) && ! xisnan (x)) |
4469
|
179 return x; |
|
180 else if (octave_is_NA (x) || octave_is_NA (y)) |
|
181 return octave_NA; |
|
182 else |
|
183 return octave_NaN; |
1967
|
184 } |
|
185 |
|
186 double |
3248
|
187 xmax (double x, double y) |
1967
|
188 { |
4469
|
189 if (x > y) |
|
190 return x; |
|
191 |
|
192 if (y >= x) |
|
193 return y; |
|
194 |
5389
|
195 if (xisnan (x) && ! xisnan (y)) |
4469
|
196 return y; |
5389
|
197 else if (xisnan (y) && ! xisnan (x)) |
4469
|
198 return x; |
|
199 else if (octave_is_NA (x) || octave_is_NA (y)) |
|
200 return octave_NA; |
|
201 else |
|
202 return octave_NaN; |
1967
|
203 } |
|
204 |
3248
|
205 // complex -> complex mappers. |
1967
|
206 |
|
207 Complex |
|
208 acos (const Complex& x) |
|
209 { |
|
210 static Complex i (0, 1); |
3056
|
211 |
|
212 return (real (x) * imag (x) < 0.0) ? i * acosh (x) : -i * acosh (x); |
1967
|
213 } |
|
214 |
|
215 Complex |
|
216 acosh (const Complex& x) |
|
217 { |
3056
|
218 return log (x + sqrt (x*x - 1.0)); |
1967
|
219 } |
|
220 |
|
221 Complex |
|
222 asin (const Complex& x) |
|
223 { |
|
224 static Complex i (0, 1); |
3056
|
225 |
|
226 return -i * log (i*x + sqrt (1.0 - x*x)); |
1967
|
227 } |
|
228 |
|
229 Complex |
|
230 asinh (const Complex& x) |
|
231 { |
3056
|
232 return log (x + sqrt (x*x + 1.0)); |
1967
|
233 } |
|
234 |
|
235 Complex |
|
236 atan (const Complex& x) |
|
237 { |
|
238 static Complex i (0, 1); |
3056
|
239 |
|
240 return i * log ((i + x) / (i - x)) / 2.0; |
1967
|
241 } |
|
242 |
|
243 Complex |
|
244 atanh (const Complex& x) |
|
245 { |
3092
|
246 return log ((1.0 + x) / (1.0 - x)) / 2.0; |
1967
|
247 } |
|
248 |
|
249 Complex |
|
250 ceil (const Complex& x) |
|
251 { |
2800
|
252 return Complex (ceil (real (x)), ceil (imag (x))); |
1967
|
253 } |
|
254 |
|
255 Complex |
|
256 fix (const Complex& x) |
|
257 { |
3012
|
258 return Complex (fix (real (x)), fix (imag (x))); |
1967
|
259 } |
|
260 |
|
261 Complex |
|
262 floor (const Complex& x) |
|
263 { |
2800
|
264 return Complex (floor (real (x)), floor (imag (x))); |
1967
|
265 } |
|
266 |
|
267 Complex |
4968
|
268 xround (const Complex& x) |
1967
|
269 { |
4968
|
270 return Complex (xround (real (x)), xround (imag (x))); |
1967
|
271 } |
|
272 |
|
273 Complex |
|
274 signum (const Complex& x) |
|
275 { |
|
276 return x / abs (x); |
|
277 } |
|
278 |
3248
|
279 // complex -> bool mappers. |
|
280 |
|
281 bool |
|
282 xisnan (const Complex& x) |
|
283 { |
4025
|
284 return (xisnan (real (x)) || xisnan (imag (x))); |
3248
|
285 } |
|
286 |
|
287 bool |
|
288 xfinite (const Complex& x) |
|
289 { |
4025
|
290 double rx = real (x); |
|
291 double ix = imag (x); |
|
292 |
5389
|
293 return (xfinite (rx) && ! xisnan (rx) |
|
294 && xfinite (ix) && ! xisnan (ix)); |
3248
|
295 } |
|
296 |
|
297 bool |
|
298 xisinf (const Complex& x) |
|
299 { |
|
300 return (xisinf (real (x)) || xisinf (imag (x))); |
|
301 } |
|
302 |
4025
|
303 bool |
|
304 octave_is_NA (const Complex& x) |
|
305 { |
|
306 return (octave_is_NA (real (x)) || octave_is_NA (imag (x))); |
|
307 } |
|
308 |
|
309 bool |
|
310 octave_is_NaN_or_NA (const Complex& x) |
|
311 { |
5389
|
312 return (xisnan (real (x)) || xisnan (imag (x))); |
4025
|
313 } |
|
314 |
3248
|
315 // (complex, complex) -> complex mappers. |
|
316 |
5775
|
317 // FIXME -- need to handle NA too? |
4025
|
318 |
3248
|
319 Complex |
|
320 xmin (const Complex& x, const Complex& y) |
|
321 { |
3757
|
322 return abs (x) <= abs (y) ? x : (xisnan (x) ? x : y); |
3248
|
323 } |
|
324 |
|
325 Complex |
|
326 xmax (const Complex& x, const Complex& y) |
|
327 { |
3757
|
328 return abs (x) >= abs (y) ? x : (xisnan (x) ? x : y); |
3248
|
329 } |
|
330 |
1967
|
331 /* |
|
332 ;;; Local Variables: *** |
|
333 ;;; mode: C++ *** |
|
334 ;;; End: *** |
|
335 */ |