AMDiS  0.1
The Adaptive Multi-Dimensional Simulation Toolbox
CMath.hpp
1 #pragma once
2 
3 #include <cmath>
4 
5 #include <amdis/operations/Basic.hpp>
6 
8 
13 #ifndef AMDIS_MAKE_UNARY_FUNCTOR
14 #define AMDIS_MAKE_UNARY_FUNCTOR( NAME, FCT, DERIV ) \
15  struct NAME \
16  { \
17  template <class T> \
18  constexpr auto operator() (T const& x) const \
19  { \
20  return FCT ; \
21  } \
22  struct Derivative \
23  { \
24  template <class T> \
25  constexpr auto operator() (T const& x) const \
26  { \
27  return DERIV ; \
28  } \
29  }; \
30  friend constexpr auto partial(NAME const&, index_t<0>) \
31  { \
32  return Derivative{} ; \
33  } \
34  };
35 #endif
36 
37 
38 namespace AMDiS
39 {
40  namespace Operation
41  {
46  struct Signum
48  {
49  template <class T>
50  constexpr auto operator()(T const& x) const
51  {
52  return (x > T{0} ? T{1} : T{-1});
53  }
54 
55  friend constexpr auto partial(Signum const&, index_t<0>)
56  {
57  return Zero{};
58  }
59  };
60 
61  // generated unary functors using a macro...
62  // approximate polynomial order
63 
64 #ifndef DOXYGEN
65  AMDIS_MAKE_UNARY_FUNCTOR( Ceil, std::ceil(x), 0.0 )
66  AMDIS_MAKE_UNARY_FUNCTOR( Floor, std::floor(x), 0.0 )
67  AMDIS_MAKE_UNARY_FUNCTOR( Sqrt, std::sqrt(x), 1.0/(2.0 * std::sqrt(x)) )
68  AMDIS_MAKE_UNARY_FUNCTOR( Exp, std::exp(x), std::exp(x) )
69  AMDIS_MAKE_UNARY_FUNCTOR( Log, std::log(x), 1.0/x )
70  AMDIS_MAKE_UNARY_FUNCTOR( Sin, std::sin(x), std::cos(x) )
71  AMDIS_MAKE_UNARY_FUNCTOR( Cos, std::cos(x), -std::sin(x) )
72  AMDIS_MAKE_UNARY_FUNCTOR( Tan, std::tan(x), 1.0 + Math::sqr(std::tan(x)) )
73  AMDIS_MAKE_UNARY_FUNCTOR( Asin, std::asin(x), 1.0/std::sqrt(1.0 - Math::sqr(x)) )
74  AMDIS_MAKE_UNARY_FUNCTOR( Acos, std::acos(x), -1.0/std::sqrt(1.0 - Math::sqr(x)) )
75  AMDIS_MAKE_UNARY_FUNCTOR( Atan, std::atan(x), 1.0/(1.0 + Math::sqr(x)) )
76  AMDIS_MAKE_UNARY_FUNCTOR( Sinh, std::sinh(x), std::cosh(x) )
77  AMDIS_MAKE_UNARY_FUNCTOR( Cosh, std::cosh(x), std::sinh(x) )
78  AMDIS_MAKE_UNARY_FUNCTOR( Tanh, std::tanh(x), 1.0 - Math::sqr(std::tanh(x)) )
79  AMDIS_MAKE_UNARY_FUNCTOR( Asinh, std::asinh(x), 1.0/std::sqrt(1.0 + Math::sqr(x)) )
80  AMDIS_MAKE_UNARY_FUNCTOR( Acosh, std::acosh(x), 1.0/std::sqrt(-1.0 + Math::sqr(x)) )
81  AMDIS_MAKE_UNARY_FUNCTOR( Atanh, std::atanh(x), 1.0/(1.0 - Math::sqr(x)) )
82 #endif
83 
85  struct Atan2
86  {
87  template <class T1, class T2>
88  constexpr auto operator() (T1 const& x, T2 const& y) const
89  {
90  return std::atan2(x, y);
91  }
92 
93  // -y/(Math::sqr(x) + Math::sqr(y))
94  constexpr friend auto partial(Atan2 const&, index_t<0>)
95  {
96  return compose(Divides{}, compose(Negate{}, Arg<1>{}),
97  compose(Plus{}, compose(Pow<2>{}, Arg<0>{}), compose(Pow<2>{}, Arg<1>{})));
98  }
99 
100  // x/(Math::sqr(x) + Math::sqr(y));
101  constexpr friend auto partial(Atan2 const&, index_t<1>)
102  {
103  return compose(Divides{}, Arg<0>{},
104  compose(Plus{}, compose(Pow<2>{}, Arg<0>{}), compose(Pow<2>{}, Arg<1>{})));
105  }
106  };
107 
108 
109  template <class T>
110  struct Clamp
111  {
112  constexpr Clamp(T const& lo, T const& hi)
113  : lo_(lo)
114  , hi_(hi)
115  {
116  // assert(lo < hi);
117  }
118 
119  constexpr auto operator() (T const& v) const
120  {
121  return v < lo_ ? lo_ : hi_ < v ? hi_ : v;
122  }
123 
124  struct DClamp
125  {
126  constexpr auto operator() (T const& v) const
127  {
128  return v < lo_ ? T(0) : hi_ < v ? T(0) : T(1);
129  }
130 
131  T lo_, hi_;
132  };
133 
134  constexpr friend auto partial(Clamp const& c, index_t<0>)
135  {
136  return DClamp{c.lo_,c.hi_};
137  }
138 
139  T lo_, hi_;
140  };
141 
144  } // end namespace Operation
145 } // end namespace AMDiS
Definition: FieldMatVec.hpp:12
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
Definition: CMath.hpp:124
std::integral_constant< std::size_t, I > index_t
A wrapper for std::size_t type.
Definition: Index.hpp:31
Definition: CMath.hpp:110