Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
traits.hpp
Go to the documentation of this file.
1 #ifndef __STAN__META__TRAITS_HPP__
2 #define __STAN__META__TRAITS_HPP__
3 
4 #include <vector>
5 #include <boost/type_traits.hpp>
6 #include <boost/math/tools/promotion.hpp>
7 #include <stan/math/matrix.hpp>
8 
9 namespace stan {
10 
24  template <typename T>
25  struct is_constant {
30  enum { value = boost::is_convertible<T,double>::value };
31  };
32 
33 
38  template <typename T>
41  };
42 
43 
44  template <typename T>
45  struct is_constant_struct<std::vector<T> > {
47  };
48 
49  template <typename T, int R, int C>
50  struct is_constant_struct<Eigen::Matrix<T,R,C> > {
52  };
53 
54 
55  // FIXME: use boost::type_traits::remove_all_extents to extend to array/ptr types
56 
57  template <typename T>
58  struct is_vector {
59  enum { value = 0 };
60  typedef T type;
61  };
62  template <typename T>
63  struct is_vector<const T> {
65  typedef T type;
66  };
67  template <typename T>
68  struct is_vector<std::vector<T> > {
69  enum { value = 1 };
70  typedef T type;
71  };
72 
73  template <typename T>
74  struct is_vector<Eigen::Matrix<T,Eigen::Dynamic,1> > {
75  enum { value = 1 };
76  typedef T type;
77  };
78  template <typename T>
79  struct is_vector<Eigen::Matrix<T,1,Eigen::Dynamic> > {
80  enum { value = 1 };
81  typedef T type;
82  };
83 
84  namespace {
85  template <bool is_vec, typename T>
86  struct scalar_type_helper {
87  typedef T type;
88  };
89 
90  template <typename T>
91  struct scalar_type_helper<true, T> {
92  typedef typename scalar_type_helper<is_vector<typename T::value_type>::value, typename T::value_type>::type type;
93  };
94  }
103  template <typename T>
104  struct scalar_type {
105  typedef typename scalar_type_helper<is_vector<T>::value, T>::type type;
106  };
107 
108  // length() should only be applied to primitive or std vector or Eigen vector
109  template <typename T>
110  size_t length(const T& x) {
111  return 1U;
112  }
113  template <typename T>
114  size_t length(const std::vector<T>& x) {
115  return x.size();
116  }
117 
118  template <typename T>
119  size_t length(const Eigen::Matrix<T,Eigen::Dynamic,1>& v) {
120  return v.size();
121  }
122  template <typename T>
123  size_t length(const Eigen::Matrix<T,1,Eigen::Dynamic>& rv) {
124  return rv.size();
125  }
126 
127  template<typename T, bool is_vec>
128  struct size_of_helper {
129  static size_t size_of(const T& x) {
130  return 1U;
131  }
132  };
133 
134  template<typename T>
135  struct size_of_helper<T, true> {
136  static size_t size_of(const T& x) {
137  return x.size();
138  }
139  };
140 
141  template <typename T>
142  size_t size_of(const T& x) {
144  }
145 
146  template <typename T1, typename T2>
147  size_t max_size(const T1& x1, const T2& x2) {
148  size_t result = length(x1);
149  result = result > length(x2) ? result : length(x2);
150  return result;
151  }
152 
153  template <typename T1, typename T2, typename T3>
154  size_t max_size(const T1& x1, const T2& x2, const T3& x3) {
155  size_t result = length(x1);
156  result = result > length(x2) ? result : length(x2);
157  result = result > length(x3) ? result : length(x3);
158  return result;
159  }
160 
161  template <typename T1, typename T2, typename T3, typename T4>
162  size_t max_size(const T1& x1, const T2& x2, const T3& x3, const T4& x4) {
163  size_t result = length(x1);
164  result = result > length(x2) ? result : length(x2);
165  result = result > length(x3) ? result : length(x3);
166  result = result > length(x4) ? result : length(x4);
167  return result;
168  }
169 
170 
171  template<typename T,
172  bool is_vec = stan::is_vector<T>::value>
173  class VectorView {
174  private:
175  T* x_;
176  public:
177  VectorView(T& x) : x_(&x) { }
178  typename scalar_type<T>::type& operator[](int /*i*/) {
179  return *x_;
180  }
181  };
182 
183  template<typename T>
184  class VectorView<T*,false> {
185  private:
186  T* x_;
187  public:
188  VectorView(T* x) : x_(x) { }
189  typename scalar_type<T>::type& operator[](int i) {
190  return *x_;
191  }
192  };
193 
194  template<typename T>
195  class VectorView<T,true> {
196  private:
197  T* x_;
198  public:
199  VectorView(T& x) : x_(&x) { }
200  typename scalar_type<T>::type& operator[](int i) {
201  return (*x_)[i];
202  }
203  };
204 
205  template<typename T>
206  class VectorView<T*,true> {
207  private:
208  T* x_;
209  public:
210  VectorView(T* x) : x_(x) { }
211  typename scalar_type<T>::type& operator[](int i) {
212  return x_[i];
213  }
214  };
215 
216  template<typename T>
217  class VectorView<const T,true> {
218  private:
219  const T* x_;
220  public:
221  VectorView(const T& x) : x_(&x) { }
223  return (*x_)[i];
224  }
225  };
226 
227  template<bool used, typename T = double, bool is_vec = stan::is_vector<T>::value>
229  public:
230  DoubleVectorView(const size_t /* n */) { }
231  double& operator[](size_t /* i */) {
232  throw std::runtime_error("used is false. this should never be called");
233  }
234  };
235 
236  template<typename T>
237  class DoubleVectorView<true, T, false> {
238  private:
239  double x_;
240  public:
241  DoubleVectorView(const size_t /* n */) : x_(0.0) { }
242  double& operator[](size_t /* i */) {
243  return x_;
244  }
245  };
246 
247  template<typename T>
248  class DoubleVectorView<true, T, true> {
249  private:
250  std::vector<double> x_;
251  public:
252  DoubleVectorView(const size_t n) : x_(n) { }
253  double& operator[](size_t i) {
254  return x_[i];
255  }
256  };
257 
262  template <typename T1,
263  typename T2 = double,
264  typename T3 = double,
265  typename T4 = double,
266  typename T5 = double,
267  typename T6 = double>
268  struct return_type {
269  typedef typename
270  boost::math::tools::promote_args<typename scalar_type<T1>::type,
271  typename scalar_type<T2>::type,
272  typename scalar_type<T3>::type,
273  typename scalar_type<T4>::type,
274  typename scalar_type<T5>::type,
277  };
278 
279 
280 
281 }
282 
283 #endif

     [ Stan Home Page ] © 2011–2012, Stan Development Team.