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_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 |
|
43 #ifndef M_LOG10E |
|
44 #define M_LOG10E 0.43429448190325182765 |
|
45 #endif |
|
46 |
3248
|
47 // double -> double mappers. |
1967
|
48 |
|
49 double |
|
50 arg (double x) |
|
51 { |
3873
|
52 return atan2 (0.0, x); |
1967
|
53 } |
|
54 |
|
55 double |
|
56 conj (double x) |
|
57 { |
|
58 return x; |
|
59 } |
|
60 |
|
61 double |
|
62 fix (double x) |
|
63 { |
3012
|
64 return x > 0 ? floor (x) : ceil (x); |
1967
|
65 } |
|
66 |
|
67 double |
4662
|
68 imag (double) |
1967
|
69 { |
4313
|
70 return 0.0; |
1967
|
71 } |
|
72 |
|
73 double |
|
74 real (double x) |
|
75 { |
|
76 return x; |
|
77 } |
|
78 |
|
79 double |
4968
|
80 xround (double x) |
1967
|
81 { |
4984
|
82 #if defined (HAVE_ROUND) |
4968
|
83 return round (x); |
|
84 #else |
4984
|
85 return x > 0 ? floor (x + 0.5) : ceil (x - 0.5); |
4968
|
86 #endif |
1967
|
87 } |
|
88 |
|
89 double |
|
90 signum (double x) |
|
91 { |
|
92 double tmp = 0.0; |
4984
|
93 |
1967
|
94 if (x < 0.0) |
|
95 tmp = -1.0; |
|
96 else if (x > 0.0) |
|
97 tmp = 1.0; |
|
98 |
|
99 return xisnan (x) ? octave_NaN : tmp; |
|
100 } |
|
101 |
3248
|
102 // double -> bool mappers. |
|
103 |
|
104 bool |
1967
|
105 xisnan (double x) |
|
106 { |
4074
|
107 return lo_ieee_isnan (x); |
1967
|
108 } |
|
109 |
3248
|
110 bool |
1967
|
111 xfinite (double x) |
|
112 { |
4074
|
113 return lo_ieee_finite (x); |
1967
|
114 } |
|
115 |
3248
|
116 bool |
1967
|
117 xisinf (double x) |
|
118 { |
4074
|
119 return lo_ieee_isinf (x); |
1967
|
120 } |
|
121 |
4025
|
122 bool |
|
123 octave_is_NA (double x) |
|
124 { |
|
125 return lo_ieee_is_NA (x); |
|
126 } |
|
127 |
|
128 bool |
|
129 octave_is_NaN_or_NA (double x) |
|
130 { |
|
131 return lo_ieee_is_NaN_or_NA (x); |
|
132 } |
|
133 |
3248
|
134 // (double, double) -> double mappers. |
1967
|
135 |
4025
|
136 // XXX FIXME XXX -- need to handle NA too? |
|
137 |
1967
|
138 double |
3248
|
139 xmin (double x, double y) |
1967
|
140 { |
4469
|
141 if (x < y) |
|
142 return x; |
|
143 |
|
144 if (y <= x) |
|
145 return y; |
|
146 |
|
147 if (octave_is_NaN_or_NA (x) && ! octave_is_NaN_or_NA (y)) |
|
148 return y; |
|
149 else if (octave_is_NaN_or_NA (y) && ! octave_is_NaN_or_NA (x)) |
|
150 return x; |
|
151 else if (octave_is_NA (x) || octave_is_NA (y)) |
|
152 return octave_NA; |
|
153 else |
|
154 return octave_NaN; |
1967
|
155 } |
|
156 |
|
157 double |
3248
|
158 xmax (double x, double y) |
1967
|
159 { |
4469
|
160 if (x > y) |
|
161 return x; |
|
162 |
|
163 if (y >= x) |
|
164 return y; |
|
165 |
|
166 if (octave_is_NaN_or_NA (x) && ! octave_is_NaN_or_NA (y)) |
|
167 return y; |
|
168 else if (octave_is_NaN_or_NA (y) && ! octave_is_NaN_or_NA (x)) |
|
169 return x; |
|
170 else if (octave_is_NA (x) || octave_is_NA (y)) |
|
171 return octave_NA; |
|
172 else |
|
173 return octave_NaN; |
1967
|
174 } |
|
175 |
3248
|
176 // complex -> complex mappers. |
1967
|
177 |
|
178 Complex |
|
179 acos (const Complex& x) |
|
180 { |
|
181 static Complex i (0, 1); |
3056
|
182 |
|
183 return (real (x) * imag (x) < 0.0) ? i * acosh (x) : -i * acosh (x); |
1967
|
184 } |
|
185 |
|
186 Complex |
|
187 acosh (const Complex& x) |
|
188 { |
3056
|
189 return log (x + sqrt (x*x - 1.0)); |
1967
|
190 } |
|
191 |
|
192 Complex |
|
193 asin (const Complex& x) |
|
194 { |
|
195 static Complex i (0, 1); |
3056
|
196 |
|
197 return -i * log (i*x + sqrt (1.0 - x*x)); |
1967
|
198 } |
|
199 |
|
200 Complex |
|
201 asinh (const Complex& x) |
|
202 { |
3056
|
203 return log (x + sqrt (x*x + 1.0)); |
1967
|
204 } |
|
205 |
|
206 Complex |
|
207 atan (const Complex& x) |
|
208 { |
|
209 static Complex i (0, 1); |
3056
|
210 |
|
211 return i * log ((i + x) / (i - x)) / 2.0; |
1967
|
212 } |
|
213 |
|
214 Complex |
|
215 atanh (const Complex& x) |
|
216 { |
3092
|
217 return log ((1.0 + x) / (1.0 - x)) / 2.0; |
1967
|
218 } |
|
219 |
3777
|
220 #if !defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
221 |
|
222 Complex |
|
223 log10 (const Complex& x) |
|
224 { |
|
225 return M_LOG10E * log (x); |
|
226 } |
|
227 |
|
228 Complex |
|
229 tan (const Complex& x) |
|
230 { |
|
231 return sin (x) / cos (x); |
|
232 } |
|
233 |
|
234 Complex |
|
235 tanh (const Complex& x) |
|
236 { |
|
237 return sinh (x) / cosh (x); |
|
238 } |
|
239 |
|
240 #endif |
|
241 |
1967
|
242 Complex |
|
243 ceil (const Complex& x) |
|
244 { |
2800
|
245 return Complex (ceil (real (x)), ceil (imag (x))); |
1967
|
246 } |
|
247 |
|
248 Complex |
|
249 fix (const Complex& x) |
|
250 { |
3012
|
251 return Complex (fix (real (x)), fix (imag (x))); |
1967
|
252 } |
|
253 |
|
254 Complex |
|
255 floor (const Complex& x) |
|
256 { |
2800
|
257 return Complex (floor (real (x)), floor (imag (x))); |
1967
|
258 } |
|
259 |
|
260 Complex |
4968
|
261 xround (const Complex& x) |
1967
|
262 { |
4968
|
263 return Complex (xround (real (x)), xround (imag (x))); |
1967
|
264 } |
|
265 |
|
266 Complex |
|
267 signum (const Complex& x) |
|
268 { |
|
269 return x / abs (x); |
|
270 } |
|
271 |
3248
|
272 // complex -> bool mappers. |
|
273 |
|
274 bool |
|
275 xisnan (const Complex& x) |
|
276 { |
4025
|
277 return (xisnan (real (x)) || xisnan (imag (x))); |
3248
|
278 } |
|
279 |
|
280 bool |
|
281 xfinite (const Complex& x) |
|
282 { |
4025
|
283 double rx = real (x); |
|
284 double ix = imag (x); |
|
285 |
|
286 return (xfinite (rx) && ! octave_is_NaN_or_NA (rx) |
|
287 && xfinite (ix) && ! octave_is_NaN_or_NA (ix)); |
3248
|
288 } |
|
289 |
|
290 bool |
|
291 xisinf (const Complex& x) |
|
292 { |
|
293 return (xisinf (real (x)) || xisinf (imag (x))); |
|
294 } |
|
295 |
4025
|
296 bool |
|
297 octave_is_NA (const Complex& x) |
|
298 { |
|
299 return (octave_is_NA (real (x)) || octave_is_NA (imag (x))); |
|
300 } |
|
301 |
|
302 bool |
|
303 octave_is_NaN_or_NA (const Complex& x) |
|
304 { |
|
305 return (octave_is_NaN_or_NA (real (x)) || octave_is_NaN_or_NA (imag (x))); |
|
306 } |
|
307 |
3248
|
308 // (complex, complex) -> complex mappers. |
|
309 |
4025
|
310 // XXX FIXME XXX -- need to handle NA too? |
|
311 |
3248
|
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 */ |