Reference documentation for deal.II version 9.0.0
sacado_math.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 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_differentiation_ad_sacado_math_h
17 #define dealii_differentiation_ad_sacado_math_h
18 
19 #include <deal.II/base/config.h>
20 
21 #ifdef DEAL_II_WITH_TRILINOS
22 
23 #include <deal.II/base/numbers.h>
24 #include <deal.II/differentiation/ad/ad_number_traits.h>
25 #include <deal.II/differentiation/ad/sacado_number_types.h>
26 
27 
28 #ifndef DOXYGEN
29 
33 namespace std
34 {
35 
39  template<typename ADNumberType, typename = typename std::enable_if<
42  >::type>
43  inline ADNumberType erf(ADNumberType x)
44  {
45  // Reference:
46  // Handbook of Mathematical Functions: with Formulas, Graphs, and Mathematical Tables
47  // Abramowitz, M. and Stegun, I.
48  // Dover Books on Mathematics. 1972.
49  //
50  // Current implementation source:
51  // https://www.johndcook.com/blog/cpp_erf/
52  // https://www.johndcook.com/blog/2009/01/19/stand-alone-error-function-erf/
53  //
54  // Note: This implementation has a reported maximum round-off error of 1.5e-7.
55 
56  // Constants
57  const double a1 = 0.254829592;
58  const double a2 = -0.284496736;
59  const double a3 = 1.421413741;
60  const double a4 = -1.453152027;
61  const double a5 = 1.061405429;
62  const double p = 0.3275911;
63 
64  // Save the sign of x
65  const bool neg_val = (x < ::internal::NumberType<ADNumberType>::value(0.0) ? true : false);
66  x = std::fabs(x);
67 
68  // Abramowitz1972a equation 7.1.26
69  const ADNumberType t = 1.0/(1.0 + p*x);
70  const ADNumberType y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*std::exp(-x*x);
71 
72  if (!neg_val)
73  return y;
74  else
75  return -y;
76  }
77 
78 
82  template<typename ADNumberType, typename = typename std::enable_if<
84  >::type>
85  inline ADNumberType erfc(const ADNumberType &x)
86  {
87  return 1.0 - std::erf(x);
88  }
89 
90 } // namespace std
91 
92 #endif // DOXYGEN
93 
94 #endif // DEAL_II_WITH_TRILINOS
95 
96 #endif
STL namespace.