GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
lo-mappers.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 John W. Eaton
4 Copyright (C) 2010 VZLU Prague
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
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
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
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if !defined (octave_lo_mappers_h)
25 #define octave_lo_mappers_h 1
26 
27 #include <limits>
28 
29 #include "oct-cmplx.h"
30 #include "lo-math.h"
31 
32 // Double Precision
33 extern OCTAVE_API double xtrunc (double x);
34 extern OCTAVE_API double xcopysign (double x, double y);
35 inline double xceil (double x) { return ceil (x); }
36 extern OCTAVE_API double xfloor (double x);
37 inline double arg (double x) { return atan2 (0.0, x); }
38 inline double conj (double x) { return x; }
39 inline double fix (double x) { return xtrunc (x); }
40 inline double imag (double) { return 0.0; }
41 inline double real (double x) { return x; }
42 extern OCTAVE_API double xround (double x);
43 extern OCTAVE_API double xroundb (double x);
44 extern OCTAVE_API double signum (double x);
45 extern OCTAVE_API double xlog2 (double x);
46 extern OCTAVE_API Complex xlog2 (const Complex& x);
47 extern OCTAVE_API double xlog2 (double x, int& exp);
48 extern OCTAVE_API Complex xlog2 (const Complex& x, int& exp);
49 extern OCTAVE_API double xexp2 (double x);
50 
51 // These are used by the BOOL_OP macros in mx-op-defs.h.
52 inline bool xisnan (bool) { return false; }
53 inline bool xisnan (char) { return false; }
54 
55 #if defined (HAVE_CMATH_ISNAN)
56 inline bool xisnan (double x)
57 { return std::isnan (x); }
58 #else
59 extern OCTAVE_API bool xisnan (double x);
60 #endif
61 #if defined (HAVE_CMATH_ISFINITE)
62 inline bool xfinite (double x)
63 { return std::isfinite (x); }
64 #else
65 extern OCTAVE_API bool xfinite (double x);
66 #endif
67 #if defined (HAVE_CMATH_ISINF)
68 inline bool xisinf (double x)
69 { return std::isinf (x); }
70 #else
71 extern OCTAVE_API bool xisinf (double x);
72 #endif
73 
74 extern OCTAVE_API bool octave_is_NA (double x);
75 
76 // Generic xmin, xmax definitions
77 template <class T>
78 inline T xmin (T x, T y)
79 {
80  return x <= y ? x : y;
81 }
82 
83 template <class T>
84 inline T xmax (T x, T y)
85 {
86  return x >= y ? x : y;
87 }
88 
89 // This form is favorable. GCC will translate (x <= y ? x : y) without a
90 // jump, hence the only conditional jump involved will be the first
91 // (xisnan), infrequent and hence friendly to branch prediction.
92 inline double
93 xmin (double x, double y)
94 {
95  return xisnan (y) ? x : (x <= y ? x : y);
96 }
97 
98 inline double
99 xmax (double x, double y)
100 {
101  return xisnan (y) ? x : (x >= y ? x : y);
102 }
103 
104 extern OCTAVE_API Complex acos (const Complex& x);
105 extern OCTAVE_API Complex acosh (const Complex& x);
106 extern OCTAVE_API Complex asin (const Complex& x);
107 extern OCTAVE_API Complex asinh (const Complex& x);
108 extern OCTAVE_API Complex atan (const Complex& x);
109 extern OCTAVE_API Complex atanh (const Complex& x);
110 
111 extern OCTAVE_API bool octave_is_NA (const Complex& x);
112 extern OCTAVE_API bool octave_is_NaN_or_NA (const Complex& x);
113 
114 extern OCTAVE_API Complex xmin (const Complex& x, const Complex& y);
115 extern OCTAVE_API Complex xmax (const Complex& x, const Complex& y);
116 
117 // Single Precision
118 extern OCTAVE_API float xtrunc (float x);
119 extern OCTAVE_API float xcopysign (float x, float y);
120 inline float xceil (float x) { return ceilf (x); }
121 extern OCTAVE_API float xfloor (float x);
122 inline float arg (float x) { return atan2f (0.0f, x); }
123 inline float conj (float x) { return x; }
124 inline float fix (float x) { return xtrunc (x); }
125 inline float imag (float) { return 0.0f; }
126 inline float real (float x) { return x; }
127 extern OCTAVE_API float xround (float x);
128 extern OCTAVE_API float xroundb (float x);
129 extern OCTAVE_API float signum (float x);
130 extern OCTAVE_API float xlog2 (float x);
131 extern OCTAVE_API FloatComplex xlog2 (const FloatComplex& x);
132 extern OCTAVE_API float xlog2 (float x, int& exp);
133 extern OCTAVE_API FloatComplex xlog2 (const FloatComplex& x, int& exp);
134 extern OCTAVE_API float xexp2 (float x);
135 
136 #if defined (HAVE_CMATH_ISNANF)
137 inline bool xisnan (float x)
138 { return std::isnan (x); }
139 #else
140 extern OCTAVE_API bool xisnan (float x);
141 #endif
142 #if defined (HAVE_CMATH_ISFINITEF)
143 inline bool xfinite (float x)
144 { return std::isfinite (x); }
145 #else
146 extern OCTAVE_API bool xfinite (float x);
147 #endif
148 #if defined (HAVE_CMATH_ISINFF)
149 inline bool xisinf (float x)
150 { return std::isinf (x); }
151 #else
152 extern OCTAVE_API bool xisinf (float x);
153 #endif
154 
155 extern OCTAVE_API bool octave_is_NA (float x);
156 
157 inline float
158 xmin (float x, float y)
159 {
160  return xisnan (y) ? x : (x <= y ? x : y);
161 }
162 
163 inline float
164 xmax (float x, float y)
165 {
166  return xisnan (y) ? x : (x >= y ? x : y);
167 }
168 
169 extern OCTAVE_API FloatComplex acos (const FloatComplex& x);
170 extern OCTAVE_API FloatComplex acosh (const FloatComplex& x);
171 extern OCTAVE_API FloatComplex asin (const FloatComplex& x);
172 extern OCTAVE_API FloatComplex asinh (const FloatComplex& x);
173 extern OCTAVE_API FloatComplex atan (const FloatComplex& x);
174 extern OCTAVE_API FloatComplex atanh (const FloatComplex& x);
175 
176 extern OCTAVE_API bool octave_is_NA (const FloatComplex& x);
177 extern OCTAVE_API bool octave_is_NaN_or_NA (const FloatComplex& x);
178 
179 extern OCTAVE_API FloatComplex xmin (const FloatComplex& x,
180  const FloatComplex& y);
181 extern OCTAVE_API FloatComplex xmax (const FloatComplex& x,
182  const FloatComplex& y);
183 
184 // These map reals to Complex.
185 
186 extern OCTAVE_API Complex rc_acos (double);
187 extern OCTAVE_API FloatComplex rc_acos (float);
188 extern OCTAVE_API Complex rc_acosh (double);
189 extern OCTAVE_API FloatComplex rc_acosh (float);
190 extern OCTAVE_API Complex rc_asin (double);
191 extern OCTAVE_API FloatComplex rc_asin (float);
192 extern OCTAVE_API Complex rc_atanh (double);
193 extern OCTAVE_API FloatComplex rc_atanh (float);
194 extern OCTAVE_API Complex rc_log (double);
195 extern OCTAVE_API FloatComplex rc_log (float);
196 extern OCTAVE_API Complex rc_log2 (double);
197 extern OCTAVE_API FloatComplex rc_log2 (float);
198 extern OCTAVE_API Complex rc_log10 (double);
199 extern OCTAVE_API FloatComplex rc_log10 (float);
200 extern OCTAVE_API Complex rc_sqrt (double);
201 extern OCTAVE_API FloatComplex rc_sqrt (float);
202 
203 // Some useful tests, that are commonly repeated.
204 // Test for a finite integer.
205 inline bool
206 xisinteger (double x)
207 {
208  return xfinite (x) && x == xround (x);
209 }
210 
211 inline bool
212 xisinteger (float x)
213 {
214  return xfinite (x) && x == xround (x);
215 }
216 
217 // Test for negative sign.
218 extern OCTAVE_API bool xnegative_sign (double x);
219 extern OCTAVE_API bool xnegative_sign (float x);
220 
221 // Test for positive sign.
222 inline bool xpositive_sign (double x) { return ! xnegative_sign (x); }
223 inline bool xpositive_sign (float x) { return ! xnegative_sign (x); }
224 
225 // Some old rounding functions.
226 
227 extern OCTAVE_API octave_idx_type NINTbig (double x);
228 extern OCTAVE_API octave_idx_type NINTbig (float x);
229 
230 extern OCTAVE_API int NINT (double x);
231 extern OCTAVE_API int NINT (float x);
232 
233 template <typename T>
234 T
236 {
237  return (xfinite (x) ? xfloor (x + 0.5) : x);
238 }
239 
240 inline OCTAVE_API double D_NINT (double x) { return X_NINT (x); }
241 inline OCTAVE_API float F_NINT (float x) { return X_NINT (x); }
242 
243 // Template functions can have either float or double arguments.
244 
245 template <typename T>
246 bool
247 xisnan (const std::complex<T>& x)
248 {
249  return (xisnan (real (x)) || xisnan (imag (x)));
250 }
251 
252 template <typename T>
253 bool
254 xfinite (const std::complex<T>& x)
255 {
256  return (xfinite (real (x)) && xfinite (imag (x)));
257 }
258 
259 template <typename T>
260 bool
261 xisinf (const std::complex<T>& x)
262 {
263  return (xisinf (real (x)) || xisinf (imag (x)));
264 }
265 
266 template <typename T>
267 std::complex<T>
268 fix (const std::complex<T>& x)
269 {
270  return std::complex<T> (fix (real (x)), fix (imag (x)));
271 }
272 
273 template <typename T>
274 std::complex<T>
275 ceil (const std::complex<T>& x)
276 {
277  return std::complex<T> (xceil (real (x)), xceil (imag (x)));
278 }
279 
280 template <typename T>
281 std::complex<T>
282 floor (const std::complex<T>& x)
283 {
284  return std::complex<T> (xfloor (real (x)), xfloor (imag (x)));
285 }
286 
287 template <typename T>
288 std::complex<T>
289 xround (const std::complex<T>& x)
290 {
291  return std::complex<T> (xround (real (x)), xround (imag (x)));
292 }
293 
294 template <typename T>
295 std::complex<T>
296 xroundb (const std::complex<T>& x)
297 {
298  return std::complex<T> (xroundb (real (x)), xroundb (imag (x)));
299 }
300 
301 template <typename T>
302 std::complex<T>
303 signum (const std::complex<T>& x)
304 {
305  T tmp = abs (x);
306 
307  return tmp == 0 ? 0.0 : x / tmp;
308 }
309 
310 template <typename T>
311 T
312 xmod (T x, T y)
313 {
314  T retval;
315 
316  if (y == 0)
317  retval = x;
318  else
319  {
320  T q = x / y;
321 
322  if (X_NINT (y) != y
323  && (std::abs ((q - X_NINT (q)) / X_NINT (q))
324  < std::numeric_limits<T>::epsilon ()))
325  retval = 0;
326  else
327  {
328  T n = xfloor (q);
329 
330  // Prevent use of extra precision.
331  volatile T tmp = y * n;
332 
333  retval = x - tmp;
334  }
335  }
336 
337  if (x != y && y != 0 && retval != 0)
338  retval = xcopysign (retval, y);
339 
340  return retval;
341 }
342 
343 template <typename T>
344 T
345 xrem (T x, T y)
346 {
347  T retval;
348 
349  if (y == 0)
350  retval = x;
351  else
352  {
353  T q = x / y;
354 
355  if (X_NINT (y) != y
356  && (std::abs ((q - X_NINT (q)) / X_NINT (q))
357  < std::numeric_limits<T>::epsilon ()))
358  retval = 0;
359  else
360  {
361  T n = xtrunc (q);
362 
363  // Prevent use of extra precision.
364  volatile T tmp = y * n;
365 
366  retval = x - tmp;
367  }
368  }
369 
370  if (x != y && y != 0 && retval != 0)
371  retval = xcopysign (retval, x);
372 
373  return retval;
374 }
375 
376 template <typename T>
377 T
379 {
380  return signbit (x);
381 }
382 
383 #endif
OCTAVE_API Complex atan(const Complex &x)
Definition: lo-mappers.cc:231
bool xisnan(bool)
Definition: lo-mappers.h:52
T xsignbit(T x)
Definition: lo-mappers.h:378
double conj(double x)
Definition: lo-mappers.h:38
T xmax(T x, T y)
Definition: lo-mappers.h:84
OCTAVE_API Complex asinh(const Complex &x)
Definition: lo-mappers.cc:225
OCTAVE_API int NINT(double x)
Definition: lo-mappers.cc:657
OCTAVE_API double xroundb(double x)
Definition: lo-mappers.cc:69
OCTAVE_API double xcopysign(double x, double y)
Definition: lo-mappers.cc:52
OCTAVE_API Complex rc_acos(double)
Definition: lo-mappers.cc:512
T X_NINT(T x)
Definition: lo-mappers.h:235
OCTAVE_API Complex acosh(const Complex &x)
Definition: lo-mappers.cc:198
OCTAVE_API double signum(double x)
Definition: lo-mappers.cc:80
T xrem(T x, T y)
Definition: lo-mappers.h:345
OCTAVE_API bool octave_is_NA(double x)
Definition: lo-mappers.cc:167
OCTAVE_API Complex acos(const Complex &x)
Definition: lo-mappers.cc:177
OCTAVE_API float F_NINT(float x)
Definition: lo-mappers.h:241
double xceil(double x)
Definition: lo-mappers.h:35
T xmod(T x, T y)
Definition: lo-mappers.h:312
F77_RET_T const double const double * f
std::complex< T > ceil(const std::complex< T > &x)
Definition: lo-mappers.h:275
OCTAVE_API bool octave_is_NaN_or_NA(const Complex &x)
Definition: lo-mappers.cc:253
OCTAVE_API double xlog2(double x)
Definition: lo-mappers.cc:93
OCTAVE_API octave_idx_type NINTbig(double x)
Definition: lo-mappers.cc:635
OCTAVE_API double xtrunc(double x)
Definition: lo-mappers.cc:46
double real(double x)
Definition: lo-mappers.h:41
OCTAVE_API Complex rc_asin(double)
Definition: lo-mappers.cc:536
OCTAVE_API double D_NINT(double x)
Definition: lo-mappers.h:240
OCTAVE_API double xexp2(double x)
Definition: lo-mappers.cc:111
OCTAVE_API Complex rc_sqrt(double)
Definition: lo-mappers.cc:606
OCTAVE_API double xfloor(double x)
Definition: lo-mappers.cc:57
double arg(double x)
Definition: lo-mappers.h:37
OCTAVE_API Complex rc_acosh(double)
Definition: lo-mappers.cc:524
SparseMatrix atan2(const double &x, const SparseMatrix &y)
Definition: dSparse.cc:689
OCTAVE_API bool xfinite(double x)
Definition: lo-mappers.cc:152
bool xisinteger(double x)
Definition: lo-mappers.h:206
bool xpositive_sign(double x)
Definition: lo-mappers.h:222
OCTAVE_API double xround(double x)
Definition: lo-mappers.cc:63
double fix(double x)
Definition: lo-mappers.h:39
OCTAVE_API bool xisinf(double x)
Definition: lo-mappers.cc:160
OCTAVE_API Complex rc_log2(double)
Definition: lo-mappers.cc:577
std::complex< float > FloatComplex
Definition: oct-cmplx.h:30
T xmin(T x, T y)
Definition: lo-mappers.h:78
OCTAVE_API Complex rc_log(double)
Definition: lo-mappers.cc:561
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:282
std::complex< double > Complex
Definition: oct-cmplx.h:29
OCTAVE_API Complex atanh(const Complex &x)
Definition: lo-mappers.cc:239
double imag(double)
Definition: lo-mappers.h:40
T abs(T x)
Definition: pr-output.cc:3062
OCTAVE_API Complex rc_log10(double)
Definition: lo-mappers.cc:591
OCTAVE_API Complex rc_atanh(double)
Definition: lo-mappers.cc:548
F77_RET_T const double * x
OCTAVE_API Complex asin(const Complex &x)
Definition: lo-mappers.cc:204
OCTAVE_API bool xnegative_sign(double x)
Definition: lo-mappers.cc:618