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