Reference documentation for deal.II version 8.4.1
numbers.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2006 - 2015 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__numbers_h
17 #define dealii__numbers_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/types.h>
22 
23 #include <cmath>
24 #include <cstdlib>
25 #include <complex>
26 
27 DEAL_II_NAMESPACE_OPEN
28 
44 namespace numbers
45 {
49  static const double E = 2.7182818284590452354;
50 
54  static const double LOG2E = 1.4426950408889634074;
55 
59  static const double LOG10E = 0.43429448190325182765;
60 
64  static const double LN2 = 0.69314718055994530942;
65 
69  static const double LN10 = 2.30258509299404568402;
70 
74  static const double PI = 3.14159265358979323846;
75 
79  static const double PI_2 = 1.57079632679489661923;
80 
84  static const double PI_4 = 0.78539816339744830962;
85 
89  static const double SQRT2 = 1.41421356237309504880;
90 
94  static const double SQRT1_2 = 0.70710678118654752440;
95 
106  bool is_nan (const double x);
107 
117  bool is_finite (const double x);
118 
123  bool is_finite (const std::complex<double> &x);
124 
129  bool is_finite (const std::complex<float> &x);
130 
139  bool is_finite (const std::complex<long double> &x);
140 
151  template <typename number>
153  {
159  static const bool is_complex = false;
160 
167  typedef number real_type;
168 
174  static
175  const number &conjugate (const number &x);
176 
182  static
183  real_type abs_square (const number &x);
184 
188  static
189  real_type abs (const number &x);
190  };
191 
192 
199  template <typename number>
200  struct NumberTraits<std::complex<number> >
201  {
207  static const bool is_complex = true;
208 
215  typedef number real_type;
216 
220  static
221  std::complex<number> conjugate (const std::complex<number> &x);
222 
229  static
230  real_type abs_square (const std::complex<number> &x);
231 
232 
236  static
237  real_type abs (const std::complex<number> &x);
238  };
239 
240  // --------------- inline and template functions ---------------- //
241 
242  inline bool is_nan (const double x)
243  {
244 #ifdef DEAL_II_HAVE_STD_ISNAN
245  return std::isnan(x);
246 #elif defined(DEAL_II_HAVE_ISNAN)
247  return isnan(x);
248 #elif defined(DEAL_II_HAVE_UNDERSCORE_ISNAN)
249  return _isnan(x);
250 #else
251  return false;
252 #endif
253  }
254 
255  inline bool is_finite (const double x)
256  {
257 #ifdef DEAL_II_HAVE_ISFINITE
258  return !is_nan(x) && std::isfinite (x);
259 #else
260  // Check against infinities. Note
261  // that if x is a NaN, then both
262  // comparisons will be false
263  return ((x >= -std::numeric_limits<double>::max())
264  &&
265  (x <= std::numeric_limits<double>::max()));
266 #endif
267  }
268 
269 
270 
271  inline bool is_finite (const std::complex<double> &x)
272  {
273  // Check complex numbers for infinity
274  // by testing real and imaginary part
275  return ( is_finite (x.real())
276  &&
277  is_finite (x.imag()) );
278  }
279 
280 
281 
282  inline bool is_finite (const std::complex<float> &x)
283  {
284  // Check complex numbers for infinity
285  // by testing real and imaginary part
286  return ( is_finite (x.real())
287  &&
288  is_finite (x.imag()) );
289  }
290 
291 
292 
293  inline bool is_finite (const std::complex<long double> &x)
294  {
295  // Same for std::complex<long double>
296  return ( is_finite (x.real())
297  &&
298  is_finite (x.imag()) );
299  }
300 
301 
302  template <typename number>
303  const number &
305  {
306  return x;
307  }
308 
309 
310 
311  template <typename number>
314  {
315  return x * x;
316  }
317 
318 
319 
320  template <typename number>
322  NumberTraits<number>::abs (const number &x)
323  {
324  return std::abs(x);
325  }
326 
327 
328 
329  template <typename number>
330  std::complex<number>
331  NumberTraits<std::complex<number> >::conjugate (const std::complex<number> &x)
332  {
333  return std::conj(x);
334  }
335 
336 
337 
338  template <typename number>
339  typename NumberTraits<std::complex<number> >::real_type
340  NumberTraits<std::complex<number> >::abs (const std::complex<number> &x)
341  {
342  return std::abs(x);
343  }
344 
345 
346 
347  template <typename number>
348  typename NumberTraits<std::complex<number> >::real_type
349  NumberTraits<std::complex<number> >::abs_square (const std::complex<number> &x)
350  {
351  return std::norm (x);
352  }
353 
354 }
355 
356 
357 
358 DEAL_II_NAMESPACE_CLOSE
359 
360 #endif
static const number & conjugate(const number &x)
Definition: numbers.h:304
static const double SQRT2
Definition: numbers.h:89
static const double PI_4
Definition: numbers.h:84
STL namespace.
static real_type abs(const number &x)
Definition: numbers.h:322
bool is_finite(const double x)
Definition: numbers.h:255
static const double PI
Definition: numbers.h:74
static const double LN2
Definition: numbers.h:64
static real_type abs_square(const number &x)
Definition: numbers.h:313
static const double E
Definition: numbers.h:49
static const bool is_complex
Definition: numbers.h:159
bool is_nan(const double x)
Definition: numbers.h:242
static const double PI_2
Definition: numbers.h:79
static const double LN10
Definition: numbers.h:69
static const double LOG2E
Definition: numbers.h:54
static const double SQRT1_2
Definition: numbers.h:94
static const double LOG10E
Definition: numbers.h:59