ALPSCore reference
real.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 /* $Id$ */
8 
9 #ifndef ALPS_NUMERIC_REAL_HPP
10 #define ALPS_NUMERIC_REAL_HPP
11 
12 #include <type_traits>
13 #include <boost/static_assert.hpp>
14 #include <boost/type_traits/is_fundamental.hpp>
15 #include <algorithm>
16 #include <complex>
17 #include <vector>
18 
19 namespace alps { namespace numeric {
20 
21 // *********************************************************** REAL_TYPE TRAITS
22 template <class T>
23 struct real_type
24 {
25  BOOST_STATIC_ASSERT((boost::is_fundamental<T>::value));
26  typedef T type;
27 };
28 
29 template <class T>
30 struct real_type<std::complex<T> >
31 {
32  typedef T type;
33 };
34 
35 template <class T>
36 struct real_type<std::vector<T> >
37 {
38  typedef std::vector<typename real_type<T>::type> type;
39 };
40 
41 
42 
43 // ************************************************************** REAL FUNCTION
44 
45 template <class T> inline typename real_type<T>::type real(T );
46 
47 namespace detail {
48 
49  // generic hook
50  template <typename T>
51  struct real_hook;
52 
53  // generic impl
54  template <typename T, bool>
55  struct real_impl;
56 
57  // overload for fundamental
58  template <typename T>
59  struct real_impl<T, true>{
60  static inline T apply(T x)
61  {
62  return x;
63  }
64  };
65 
66  // forward to hooks
67  template <typename T>
68  struct real_impl<T, false>{
69  static inline typename real_type<T>::type apply(T const& x)
70  {
71  return real_hook<T>::apply(x);
72  }
73  };
74 
75 
76 
77  // overload for complex of fundamental
78  template <typename T>
79  struct real_hook<std::complex<T> >{
80  static inline T apply(std::complex<T> const& x)
81  {
82  BOOST_STATIC_ASSERT((std::is_fundamental<T>::value));
83  return std::real(x);
84  }
85  };
86 
87  // overload for std::vector
88  template <typename T>
89  struct real_hook<std::vector<T> >{
90  static inline std::vector<T> const& apply(std::vector<T> const& x)
91  {
92  BOOST_STATIC_ASSERT((std::is_fundamental<T>::value));
93  return x;
94  }
95  };
96  template <typename T>
97  struct real_hook<std::vector<std::complex<T> > >{
98  static inline std::vector<T> apply(std::vector<std::complex<T> > const& x)
99  {
100  std::vector<T> re;
101  re.reserve(x.size());
102  std::transform(x.begin(),x.end(),std::back_inserter(re),
103  static_cast<T (*)(std::complex<T> const &)>(&real_hook<std::complex<T> >::apply));
104  return re;
105  }
106  };
107 
108  // overload for std::vector<std::vector>
109  template <typename T>
110  struct real_hook<std::vector<std::vector<T> > >{
111  static inline std::vector<std::vector<T> > const& apply(std::vector<std::vector<T> > const& x)
112  {
113  BOOST_STATIC_ASSERT((std::is_fundamental<T>::value));
114  return x;
115  }
116  };
117  template <typename T>
118  struct real_hook<std::vector<std::vector<std::complex<T> > > >{
119  static inline std::vector<std::vector<T> > apply(std::vector<std::vector<std::complex<T> > > const& x)
120  {
121  std::vector<std::vector<T> > re;
122  re.reserve(x.size());
123  std::transform(x.begin(),x.end(),std::back_inserter(re),
124  static_cast<std::vector<T> (*)(std::vector<std::complex<T> > const &)>(&real_hook<std::vector<std::complex<T> > >::apply));
125  return re;
126  }
127  };
128 
129 } // end namespace detail
130 
131 
132 template <class T>
133 inline typename real_type<T>::type real(T x)
134 {
135  return detail::real_impl<T, std::is_fundamental<T>::value>::apply(x);
136 }
137 
138 
139 
140 } } // end namespace alps::numeric
141 
142 #endif // ALPS_MATH_HPP
STL namespace.
mean_result< T > transform(no_prop, const transformer< T > &tf, const InResult &in)
Definition: transform.hpp:27
real_type< T >::type real(T x)
Definition: real.hpp:133
real_type< T >::type real(T)
BOOST_STATIC_ASSERT((boost::is_fundamental< T >::value))
std::vector< typename real_type< T >::type > type
Definition: real.hpp:38