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