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