CMath.hpp 4.29 KB
Newer Older
1
2
3
4
#pragma once

#include <cmath>

5
#include <amdis/operations/Basic.hpp>
6
7
8
9
10
11
12
13

/// Macro that generates a unary functor with explicitly given function and derivative.
/**
 *  \p NAME    Name of the class.
 *  \p FCT     Name of a unary c++-function that represents the functor.
 *  \p DERIV   Name of a unary c++-function that represents the derivative of this functor.
 */
#ifndef AMDIS_MAKE_UNARY_FUNCTOR
14
#define AMDIS_MAKE_UNARY_FUNCTOR( NAME, FCT, DERIV )          \
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    struct NAME                                               \
    {                                                         \
      template <class T>                                      \
      constexpr auto operator() (T const& x) const            \
      {                                                       \
        return FCT ;                                          \
      }                                                       \
      struct Derivative                                       \
      {                                                       \
        template <class T>                                    \
        constexpr auto operator() (T const& x) const          \
        {                                                     \
          return DERIV ;                                      \
        }                                                     \
      };                                                      \
      friend constexpr auto partial(NAME, index_t<0>)         \
      {                                                       \
        return Derivative{} ;                                 \
      }                                                       \
    };
#endif


namespace AMDiS
{
  namespace Operation
  {
    /** \addtogroup operations
     *  @{
     **/

    /// Functor that represents the signum function
    struct Signum
    {
      template <class T>
      constexpr auto operator()(T const& x) const
      {
        return (x > T{0} ? T{1} : T{-1});
      }

      constexpr friend auto partial(Signum, index_t<0>)
      {
        return Zero{};
      }
    };

    // generated unary functors using a macro...
    // approximate polynomial order

64
#ifndef DXOYGEN
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    AMDIS_MAKE_UNARY_FUNCTOR( Ceil,  std::ceil(x),  0.0 )
    AMDIS_MAKE_UNARY_FUNCTOR( Floor, std::floor(x), 0.0 )
    AMDIS_MAKE_UNARY_FUNCTOR( Sqrt,  std::sqrt(x),  1.0/(2.0 * std::sqrt(x)) )
    AMDIS_MAKE_UNARY_FUNCTOR( Exp,   std::exp(x),   std::exp(x) )
    AMDIS_MAKE_UNARY_FUNCTOR( Log,   std::log(x),   1.0/x )
    AMDIS_MAKE_UNARY_FUNCTOR( Sin,   std::sin(x),   std::cos(x) )
    AMDIS_MAKE_UNARY_FUNCTOR( Cos,   std::cos(x),   -std::sin(x) )
    AMDIS_MAKE_UNARY_FUNCTOR( Tan,   std::tan(x),   1.0 + Math::sqr(std::tan(x)) )
    AMDIS_MAKE_UNARY_FUNCTOR( Asin,  std::asin(x),  1.0/std::sqrt(1.0 - Math::sqr(x)) )
    AMDIS_MAKE_UNARY_FUNCTOR( Acos,  std::acos(x), -1.0/std::sqrt(1.0 - Math::sqr(x)) )
    AMDIS_MAKE_UNARY_FUNCTOR( Atan,  std::atan(x),  1.0/(1.0 + Math::sqr(x)) )
    AMDIS_MAKE_UNARY_FUNCTOR( Sinh,  std::sinh(x),  std::cosh(x) )
    AMDIS_MAKE_UNARY_FUNCTOR( Cosh,  std::cosh(x),  std::sinh(x) )
    AMDIS_MAKE_UNARY_FUNCTOR( Tanh,  std::tanh(x),  1.0 - Math::sqr(std::tanh(x)) )
    AMDIS_MAKE_UNARY_FUNCTOR( Asinh, std::asinh(x), 1.0/std::sqrt(1.0 + Math::sqr(x)) )
    AMDIS_MAKE_UNARY_FUNCTOR( Acosh, std::acosh(x), 1.0/std::sqrt(-1.0 + Math::sqr(x)) )
    AMDIS_MAKE_UNARY_FUNCTOR( Atanh, std::atanh(x), 1.0/(1.0 - Math::sqr(x)) )
82
#endif
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

    /// Binary functor representing the cmath function std::atan2(v)
    struct Atan2
    {
      template <class T1, class T2>
      constexpr auto operator() (T1 const& x, T2 const& y) const
      {
        return std::atan2(x, y);
      }

      struct D0 {
        template <class T1, class T2>
        constexpr auto operator() (T1 const& x, T2 const& y) const
        {
          return -y/(Math::sqr(x) + Math::sqr(y));
        }
      };
100

101
102
103
104
105
106
107
108
109
110
111
112
      constexpr friend auto partial(Atan2, index_t<0>)
      {
        return D0{};
      }

      struct D1 {
        template <class T1, class T2>
        constexpr auto operator() (T1 const& x, T2 const& y) const
        {
          return x/(Math::sqr(x) + Math::sqr(y));
        }
      };
113

114
115
116
117
118
119
120
121
122
123
      constexpr friend auto partial(Atan2, index_t<1>)
      {
        return D1{};
      }
    };

    /** @} **/

  } // end namespace Operation
} // end namespace AMDiS