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