Chemical Data Processing Library C++ API - Version 1.3.0
TypeTraits.hpp
Go to the documentation of this file.
1 /*
2  * TypeTraits.hpp
3  *
4  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #ifndef CDPL_MATH_TYPETRAITS_HPP
28 #define CDPL_MATH_TYPETRAITS_HPP
29 
30 #include <cmath>
31 #include <complex>
32 #include <algorithm>
33 #include <limits>
34 #include <type_traits>
35 
36 
37 namespace CDPL
38 {
39 
40  namespace Math
41  {
42 
43  template <bool Signed>
45  {
46 
47  template <typename T>
48  static T abs(const T& t)
49  {
50  return std::abs(t);
51  }
52  };
53 
54  template <>
55  struct ScalarAbsImpl<false>
56  {
57 
58  template <typename T>
59  static const T& abs(const T& t)
60  {
61  return t;
62  }
63  };
64 
65  template <typename T>
66  struct ScalarTraits
67  {
68 
69  typedef T ValueType;
70  typedef T RealType;
71  typedef const T& ConstReference;
73 
75  {
76  return t;
77  }
78 
80  {
81  return RealType();
82  }
83 
85  {
86  return t;
87  }
88 
90  {
92  }
93 
95  {
96  return ValueType(std::sqrt(t));
97  }
98 
100  {
101  return SelfType::abs(t);
102  }
103 
105  {
106  return SelfType::abs(t);
107  }
108 
110  {
111  return SelfType::abs(t);
112  }
113  };
114 
115  template <typename T>
116  struct TypeTraits;
117 
118  template <typename T>
120  {
121 
122  typedef T ValueType;
123  typedef typename T::value_type RealType;
124  typedef const T& ConstReference;
126 
128  {
129  return std::real(t);
130  }
131 
133  {
134  return std::imag(t);
135  }
136 
138  {
139  return std::conj(t);
140  }
141 
143  {
144  return std::abs(t);
145  }
146 
148  {
149  return std::sqrt(t);
150  }
151 
153  {
155  }
156 
158  {
159  return SelfType::abs(t);
160  }
161 
163  {
164  return std::max(TypeTraits<RealType>::abs(SelfType::real(t)),
166  }
167  };
168 
169  template <typename T>
170  struct TypeTraits : public ScalarTraits<T>
171  {};
172 
173  template <typename T>
174  struct TypeTraits<std::complex<T> > : public ComplexTraits<std::complex<T> >
175  {};
176 
177  template <typename V>
179  {
180 
181  typedef typename V::VectorTemporaryType Type;
182  };
183 
184  template <typename M>
186  {
187 
188  typedef typename M::MatrixTemporaryType Type;
189  };
190 
191  template <typename Q>
193  {
194 
195  typedef typename Q::QuaternionTemporaryType Type;
196  };
197 
198  template <typename G>
200  {
201 
202  typedef typename G::GridTemporaryType Type;
203  };
204 
205  template <typename T>
206  struct IsScalar : public std::is_arithmetic<T>
207  {};
208 
209  template <typename T>
210  struct IsScalar<std::complex<T> > : public std::is_arithmetic<T>
211  {};
212  } // namespace Math
213 } // namespace CDPL
214 
215 #endif // CDPL_MATH_TYPETRAITS_HPP
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
GridUnaryTraits< E, ScalarConjugation< typename E::ValueType > >::ResultType conj(const GridExpression< E > &e)
Definition: GridExpression.hpp:360
GridUnaryTraits< E, ScalarReal< typename E::ValueType > >::ResultType real(const GridExpression< E > &e)
Definition: GridExpression.hpp:378
GridUnaryTraits< E, ScalarImaginary< typename E::ValueType > >::ResultType imag(const GridExpression< E > &e)
Definition: GridExpression.hpp:387
The namespace of the Chemical Data Processing Library.
Definition: TypeTraits.hpp:120
static RealType norm1(ConstReference t)
Definition: TypeTraits.hpp:152
static RealType norm2(ConstReference t)
Definition: TypeTraits.hpp:157
static RealType abs(ConstReference t)
Definition: TypeTraits.hpp:142
ComplexTraits< T > SelfType
Definition: TypeTraits.hpp:125
static ValueType sqrt(ConstReference t)
Definition: TypeTraits.hpp:147
static RealType real(ConstReference t)
Definition: TypeTraits.hpp:127
const T & ConstReference
Definition: TypeTraits.hpp:124
T ValueType
Definition: TypeTraits.hpp:122
static ValueType conj(ConstReference t)
Definition: TypeTraits.hpp:137
T::value_type RealType
Definition: TypeTraits.hpp:123
static RealType imag(ConstReference t)
Definition: TypeTraits.hpp:132
static RealType normInf(ConstReference t)
Definition: TypeTraits.hpp:162
Definition: TypeTraits.hpp:200
G::GridTemporaryType Type
Definition: TypeTraits.hpp:202
Definition: TypeTraits.hpp:207
Definition: TypeTraits.hpp:186
M::MatrixTemporaryType Type
Definition: TypeTraits.hpp:188
Definition: TypeTraits.hpp:193
Q::QuaternionTemporaryType Type
Definition: TypeTraits.hpp:195
static const T & abs(const T &t)
Definition: TypeTraits.hpp:59
Definition: TypeTraits.hpp:45
static T abs(const T &t)
Definition: TypeTraits.hpp:48
Definition: TypeTraits.hpp:67
ScalarTraits< T > SelfType
Definition: TypeTraits.hpp:72
static RealType real(ConstReference t)
Definition: TypeTraits.hpp:74
static RealType conj(ConstReference t)
Definition: TypeTraits.hpp:84
static RealType norm1(ConstReference t)
Definition: TypeTraits.hpp:99
static RealType normInf(ConstReference t)
Definition: TypeTraits.hpp:109
static RealType abs(ConstReference t)
Definition: TypeTraits.hpp:89
T ValueType
Definition: TypeTraits.hpp:69
static RealType imag(ConstReference)
Definition: TypeTraits.hpp:79
static ValueType sqrt(ConstReference t)
Definition: TypeTraits.hpp:94
static RealType norm2(ConstReference t)
Definition: TypeTraits.hpp:104
const T & ConstReference
Definition: TypeTraits.hpp:71
T RealType
Definition: TypeTraits.hpp:70
Definition: TypeTraits.hpp:171
Definition: TypeTraits.hpp:179
V::VectorTemporaryType Type
Definition: TypeTraits.hpp:181