Reference documentation for deal.II version 8.5.1
numbers.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2006 - 2017 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 
29 // forward declarations to support abs or sqrt operations on VectorizedArray
30 template <typename Number> class VectorizedArray;
31 template <typename T> struct EnableIfScalar;
32 
33 DEAL_II_NAMESPACE_CLOSE
34 
35 namespace std
36 {
37  template <typename Number> DEAL_II_ALWAYS_INLINE ::VectorizedArray<Number>
38  sqrt(const ::VectorizedArray<Number> &);
39  template <typename Number> DEAL_II_ALWAYS_INLINE ::VectorizedArray<Number>
40  abs(const ::VectorizedArray<Number> &);
41  template <typename Number> DEAL_II_ALWAYS_INLINE ::VectorizedArray<Number>
42  max(const ::VectorizedArray<Number> &, const ::VectorizedArray<Number> &);
43  template <typename Number> DEAL_II_ALWAYS_INLINE ::VectorizedArray<Number>
44  min (const ::VectorizedArray<Number> &, const ::VectorizedArray<Number> &);
45 }
46 
47 DEAL_II_NAMESPACE_OPEN
48 
64 namespace numbers
65 {
69  static const double E = 2.7182818284590452354;
70 
74  static const double LOG2E = 1.4426950408889634074;
75 
79  static const double LOG10E = 0.43429448190325182765;
80 
84  static const double LN2 = 0.69314718055994530942;
85 
89  static const double LN10 = 2.30258509299404568402;
90 
94  static const double PI = 3.14159265358979323846;
95 
99  static const double PI_2 = 1.57079632679489661923;
100 
104  static const double PI_4 = 0.78539816339744830962;
105 
109  static const double SQRT2 = 1.41421356237309504880;
110 
114  static const double SQRT1_2 = 0.70710678118654752440;
115 
126  bool is_nan (const double x);
127 
137  bool is_finite (const double x);
138 
143  bool is_finite (const std::complex<double> &x);
144 
149  bool is_finite (const std::complex<float> &x);
150 
159  bool is_finite (const std::complex<long double> &x);
160 
171  template <typename number>
173  {
179  static const bool is_complex = false;
180 
187  typedef number real_type;
188 
194  static
195  const number &conjugate (const number &x);
196 
202  static
203  real_type abs_square (const number &x);
204 
208  static
209  real_type abs (const number &x);
210  };
211 
212 
219  template <typename number>
220  struct NumberTraits<std::complex<number> >
221  {
227  static const bool is_complex = true;
228 
235  typedef number real_type;
236 
240  static
241  std::complex<number> conjugate (const std::complex<number> &x);
242 
249  static
250  real_type abs_square (const std::complex<number> &x);
251 
252 
256  static
257  real_type abs (const std::complex<number> &x);
258  };
259 
260  // --------------- inline and template functions ---------------- //
261 
262  inline bool is_nan (const double x)
263  {
264 #ifdef DEAL_II_HAVE_STD_ISNAN
265  return std::isnan(x);
266 #elif defined(DEAL_II_HAVE_ISNAN)
267  return isnan(x);
268 #elif defined(DEAL_II_HAVE_UNDERSCORE_ISNAN)
269  return _isnan(x);
270 #else
271  return false;
272 #endif
273  }
274 
275  inline bool is_finite (const double x)
276  {
277 #ifdef DEAL_II_HAVE_ISFINITE
278  return !is_nan(x) && std::isfinite (x);
279 #else
280  // Check against infinities. Note
281  // that if x is a NaN, then both
282  // comparisons will be false
283  return ((x >= -std::numeric_limits<double>::max())
284  &&
285  (x <= std::numeric_limits<double>::max()));
286 #endif
287  }
288 
289 
290 
291  inline bool is_finite (const std::complex<double> &x)
292  {
293  // Check complex numbers for infinity
294  // by testing real and imaginary part
295  return ( is_finite (x.real())
296  &&
297  is_finite (x.imag()) );
298  }
299 
300 
301 
302  inline bool is_finite (const std::complex<float> &x)
303  {
304  // Check complex numbers for infinity
305  // by testing real and imaginary part
306  return ( is_finite (x.real())
307  &&
308  is_finite (x.imag()) );
309  }
310 
311 
312 
313  inline bool is_finite (const std::complex<long double> &x)
314  {
315  // Same for std::complex<long double>
316  return ( is_finite (x.real())
317  &&
318  is_finite (x.imag()) );
319  }
320 
321 
322  template <typename number>
323  const number &
325  {
326  return x;
327  }
328 
329 
330 
331  template <typename number>
334  {
335  return x * x;
336  }
337 
338 
339 
340  template <typename number>
342  NumberTraits<number>::abs (const number &x)
343  {
344  return std::abs(x);
345  }
346 
347 
348 
349  template <typename number>
350  std::complex<number>
351  NumberTraits<std::complex<number> >::conjugate (const std::complex<number> &x)
352  {
353  return std::conj(x);
354  }
355 
356 
357 
358  template <typename number>
359  typename NumberTraits<std::complex<number> >::real_type
360  NumberTraits<std::complex<number> >::abs (const std::complex<number> &x)
361  {
362  return std::abs(x);
363  }
364 
365 
366 
367  template <typename number>
368  typename NumberTraits<std::complex<number> >::real_type
369  NumberTraits<std::complex<number> >::abs_square (const std::complex<number> &x)
370  {
371  return std::norm (x);
372  }
373 
374 }
375 
376 namespace internal
377 {
388  template <typename T>
389  struct NumberType
390  {
391  static T value (const T &t)
392  {
393  return t;
394  }
395  };
396 
397  template <typename T>
398  struct NumberType<std::complex<T> >
399  {
400  static std::complex<T> value (const T &t)
401  {
402  return std::complex<T>(t);
403  }
404  };
405 }
406 
407 DEAL_II_NAMESPACE_CLOSE
408 
409 #endif
static const number & conjugate(const number &x)
Definition: numbers.h:324
static const double SQRT2
Definition: numbers.h:109
static const double PI_4
Definition: numbers.h:104
STL namespace.
static real_type abs(const number &x)
Definition: numbers.h:342
bool is_finite(const double x)
Definition: numbers.h:275
static const double PI
Definition: numbers.h:94
static const double LN2
Definition: numbers.h:84
static real_type abs_square(const number &x)
Definition: numbers.h:333
static const double E
Definition: numbers.h:69
static const bool is_complex
Definition: numbers.h:179
bool is_nan(const double x)
Definition: numbers.h:262
static const double PI_2
Definition: numbers.h:99
T min(const T &t, const MPI_Comm &mpi_communicator)
static const double LN10
Definition: numbers.h:89
static const double LOG2E
Definition: numbers.h:74
static const double SQRT1_2
Definition: numbers.h:114
T max(const T &t, const MPI_Comm &mpi_communicator)
static const double LOG10E
Definition: numbers.h:79