Chemical Data Processing Library C++ API - Version 1.1.1
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
CDPL::Math::ComplexTraits::imag
static RealType imag(ConstReference t)
Definition: TypeTraits.hpp:132
CDPL::Math::TypeTraits
Definition: TypeTraits.hpp:171
CDPL::Math::VectorTemporaryTraits
Definition: TypeTraits.hpp:179
CDPL::Math::ScalarTraits::ValueType
T ValueType
Definition: TypeTraits.hpp:69
CDPL::Math::ScalarTraits::norm2
static RealType norm2(ConstReference t)
Definition: TypeTraits.hpp:104
CDPL::Math::ComplexTraits::ConstReference
const T & ConstReference
Definition: TypeTraits.hpp:124
CDPL::Math::ScalarTraits
Definition: TypeTraits.hpp:67
CDPL::Math::ComplexTraits::SelfType
ComplexTraits< T > SelfType
Definition: TypeTraits.hpp:125
CDPL::Math::ScalarTraits::abs
static RealType abs(ConstReference t)
Definition: TypeTraits.hpp:89
CDPL::Math::QuaternionTemporaryTraits::Type
Q::QuaternionTemporaryType Type
Definition: TypeTraits.hpp:195
CDPL::Math::ScalarTraits::imag
static RealType imag(ConstReference)
Definition: TypeTraits.hpp:79
CDPL::Math::real
GridUnaryTraits< E, ScalarReal< typename E::ValueType > >::ResultType real(const GridExpression< E > &e)
Definition: GridExpression.hpp:378
CDPL::Math::ComplexTraits::RealType
T::value_type RealType
Definition: TypeTraits.hpp:123
CDPL::Math::GridTemporaryTraits
Definition: TypeTraits.hpp:200
CDPL::Math::ScalarTraits::SelfType
ScalarTraits< T > SelfType
Definition: TypeTraits.hpp:72
CDPL::Math::ScalarTraits::conj
static RealType conj(ConstReference t)
Definition: TypeTraits.hpp:84
CDPL::Math::ComplexTraits
Definition: TypeTraits.hpp:120
CDPL::Math::ComplexTraits::normInf
static RealType normInf(ConstReference t)
Definition: TypeTraits.hpp:162
CDPL::Math::ScalarAbsImpl< false >::abs
static const T & abs(const T &t)
Definition: TypeTraits.hpp:59
CDPL::Math::ComplexTraits::sqrt
static ValueType sqrt(ConstReference t)
Definition: TypeTraits.hpp:147
CDPL::Math::ComplexTraits::abs
static RealType abs(ConstReference t)
Definition: TypeTraits.hpp:142
CDPL::Math::ScalarTraits::norm1
static RealType norm1(ConstReference t)
Definition: TypeTraits.hpp:99
CDPL::Math::MatrixTemporaryTraits::Type
M::MatrixTemporaryType Type
Definition: TypeTraits.hpp:188
CDPL::Math::conj
GridUnaryTraits< E, ScalarConjugation< typename E::ValueType > >::ResultType conj(const GridExpression< E > &e)
Definition: GridExpression.hpp:360
CDPL::Math::MatrixTemporaryTraits
Definition: TypeTraits.hpp:186
CDPL::Math::QuaternionTemporaryTraits
Definition: TypeTraits.hpp:193
CDPL::Chem::AtomType::T
const unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
CDPL::Math::ComplexTraits::norm2
static RealType norm2(ConstReference t)
Definition: TypeTraits.hpp:157
CDPL::Math::ComplexTraits::real
static RealType real(ConstReference t)
Definition: TypeTraits.hpp:127
CDPL::Math::ScalarTraits::normInf
static RealType normInf(ConstReference t)
Definition: TypeTraits.hpp:109
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Math::ScalarTraits::sqrt
static ValueType sqrt(ConstReference t)
Definition: TypeTraits.hpp:94
CDPL::Math::ScalarTraits::real
static RealType real(ConstReference t)
Definition: TypeTraits.hpp:74
CDPL::Math::ComplexTraits::norm1
static RealType norm1(ConstReference t)
Definition: TypeTraits.hpp:152
CDPL::Math::ComplexTraits::ValueType
T ValueType
Definition: TypeTraits.hpp:122
CDPL::Math::imag
GridUnaryTraits< E, ScalarImaginary< typename E::ValueType > >::ResultType imag(const GridExpression< E > &e)
Definition: GridExpression.hpp:387
CDPL::Math::IsScalar
Definition: TypeTraits.hpp:207
CDPL::Math::GridTemporaryTraits::Type
G::GridTemporaryType Type
Definition: TypeTraits.hpp:202
CDPL::Math::ScalarAbsImpl
Definition: TypeTraits.hpp:45
CDPL::Math::ComplexTraits::conj
static ValueType conj(ConstReference t)
Definition: TypeTraits.hpp:137
CDPL::Math::ScalarTraits::ConstReference
const T & ConstReference
Definition: TypeTraits.hpp:71
CDPL::Math::ScalarTraits::RealType
T RealType
Definition: TypeTraits.hpp:70
CDPL::Math::VectorTemporaryTraits::Type
V::VectorTemporaryType Type
Definition: TypeTraits.hpp:181
CDPL::Math::ScalarAbsImpl::abs
static T abs(const T &t)
Definition: TypeTraits.hpp:48