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 |
|
68 imag (double x) |
|
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 { |
3248
|
136 return x < y ? x : (xisnan (x) ? x : y); |
1967
|
137 } |
|
138 |
|
139 double |
3248
|
140 xmax (double x, double y) |
1967
|
141 { |
3248
|
142 return x > y ? x : (xisnan (x) ? x : y); |
1967
|
143 } |
|
144 |
3248
|
145 // complex -> complex mappers. |
1967
|
146 |
|
147 Complex |
|
148 acos (const Complex& x) |
|
149 { |
|
150 static Complex i (0, 1); |
3056
|
151 |
|
152 return (real (x) * imag (x) < 0.0) ? i * acosh (x) : -i * acosh (x); |
1967
|
153 } |
|
154 |
|
155 Complex |
|
156 acosh (const Complex& x) |
|
157 { |
3056
|
158 return log (x + sqrt (x*x - 1.0)); |
1967
|
159 } |
|
160 |
|
161 Complex |
|
162 asin (const Complex& x) |
|
163 { |
|
164 static Complex i (0, 1); |
3056
|
165 |
|
166 return -i * log (i*x + sqrt (1.0 - x*x)); |
1967
|
167 } |
|
168 |
|
169 Complex |
|
170 asinh (const Complex& x) |
|
171 { |
3056
|
172 return log (x + sqrt (x*x + 1.0)); |
1967
|
173 } |
|
174 |
|
175 Complex |
|
176 atan (const Complex& x) |
|
177 { |
|
178 static Complex i (0, 1); |
3056
|
179 |
|
180 return i * log ((i + x) / (i - x)) / 2.0; |
1967
|
181 } |
|
182 |
|
183 Complex |
|
184 atanh (const Complex& x) |
|
185 { |
3092
|
186 return log ((1.0 + x) / (1.0 - x)) / 2.0; |
1967
|
187 } |
|
188 |
3777
|
189 #if !defined (CXX_ISO_COMPLIANT_LIBRARY) |
|
190 |
|
191 Complex |
|
192 log10 (const Complex& x) |
|
193 { |
|
194 return M_LOG10E * log (x); |
|
195 } |
|
196 |
|
197 Complex |
|
198 tan (const Complex& x) |
|
199 { |
|
200 return sin (x) / cos (x); |
|
201 } |
|
202 |
|
203 Complex |
|
204 tanh (const Complex& x) |
|
205 { |
|
206 return sinh (x) / cosh (x); |
|
207 } |
|
208 |
|
209 #endif |
|
210 |
1967
|
211 Complex |
|
212 ceil (const Complex& x) |
|
213 { |
2800
|
214 return Complex (ceil (real (x)), ceil (imag (x))); |
1967
|
215 } |
|
216 |
|
217 Complex |
|
218 fix (const Complex& x) |
|
219 { |
3012
|
220 return Complex (fix (real (x)), fix (imag (x))); |
1967
|
221 } |
|
222 |
|
223 Complex |
|
224 floor (const Complex& x) |
|
225 { |
2800
|
226 return Complex (floor (real (x)), floor (imag (x))); |
1967
|
227 } |
|
228 |
|
229 Complex |
|
230 round (const Complex& x) |
|
231 { |
2800
|
232 return Complex (D_NINT (real (x)), D_NINT (imag (x))); |
1967
|
233 } |
|
234 |
|
235 Complex |
|
236 signum (const Complex& x) |
|
237 { |
|
238 return x / abs (x); |
|
239 } |
|
240 |
3248
|
241 // complex -> bool mappers. |
|
242 |
|
243 bool |
|
244 xisnan (const Complex& x) |
|
245 { |
4025
|
246 return (xisnan (real (x)) || xisnan (imag (x))); |
3248
|
247 } |
|
248 |
|
249 bool |
|
250 xfinite (const Complex& x) |
|
251 { |
4025
|
252 double rx = real (x); |
|
253 double ix = imag (x); |
|
254 |
|
255 return (xfinite (rx) && ! octave_is_NaN_or_NA (rx) |
|
256 && xfinite (ix) && ! octave_is_NaN_or_NA (ix)); |
3248
|
257 } |
|
258 |
|
259 bool |
|
260 xisinf (const Complex& x) |
|
261 { |
|
262 return (xisinf (real (x)) || xisinf (imag (x))); |
|
263 } |
|
264 |
4025
|
265 bool |
|
266 octave_is_NA (const Complex& x) |
|
267 { |
|
268 return (octave_is_NA (real (x)) || octave_is_NA (imag (x))); |
|
269 } |
|
270 |
|
271 bool |
|
272 octave_is_NaN_or_NA (const Complex& x) |
|
273 { |
|
274 return (octave_is_NaN_or_NA (real (x)) || octave_is_NaN_or_NA (imag (x))); |
|
275 } |
|
276 |
3248
|
277 // (complex, complex) -> complex mappers. |
|
278 |
4025
|
279 // XXX FIXME XXX -- need to handle NA too? |
|
280 |
3248
|
281 Complex |
|
282 xmin (const Complex& x, const Complex& y) |
|
283 { |
3757
|
284 return abs (x) <= abs (y) ? x : (xisnan (x) ? x : y); |
3248
|
285 } |
|
286 |
|
287 Complex |
|
288 xmax (const Complex& x, const Complex& y) |
|
289 { |
3757
|
290 return abs (x) >= abs (y) ? x : (xisnan (x) ? x : y); |
3248
|
291 } |
|
292 |
1967
|
293 /* |
|
294 ;;; Local Variables: *** |
|
295 ;;; mode: C++ *** |
|
296 ;;; End: *** |
|
297 */ |