Stan  1.0
probability, sampling & optimization
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 
109  // length() should only be applied to primitive or std vector or Eigen vector
110  template <typename T>
111  size_t length(const T& x) {
112  return 1U;
113  }
114  template <typename T>
115  size_t length(const std::vector<T>& x) {
116  return x.size();
117  }
118 
119  template <typename T>
120  size_t length(const Eigen::Matrix<T,Eigen::Dynamic,1>& v) {
121  return v.size();
122  }
123  template <typename T>
124  size_t length(const Eigen::Matrix<T,1,Eigen::Dynamic>& rv) {
125  return rv.size();
126  }
127 
128  template<typename T, bool is_vec>
129  struct size_of_helper {
130  static size_t size_of(const T& x) {
131  return 1U;
132  }
133  };
134 
135  template<typename T>
136  struct size_of_helper<T, true> {
137  static size_t size_of(const T& x) {
138  return x.size();
139  }
140  };
141 
142  template <typename T>
143  size_t size_of(const T& x) {
145  }
146 
147  template <typename T1, typename T2>
148  size_t max_size(const T1& x1, const T2& x2) {
149  size_t result = length(x1);
150  result = result > length(x2) ? result : length(x2);
151  return result;
152  }
153 
154  template <typename T1, typename T2, typename T3>
155  size_t max_size(const T1& x1, const T2& x2, const T3& x3) {
156  size_t result = length(x1);
157  result = result > length(x2) ? result : length(x2);
158  result = result > length(x3) ? result : length(x3);
159  return result;
160  }
161 
162  template <typename T1, typename T2, typename T3, typename T4>
163  size_t max_size(const T1& x1, const T2& x2, const T3& x3, const T4& x4) {
164  size_t result = length(x1);
165  result = result > length(x2) ? result : length(x2);
166  result = result > length(x3) ? result : length(x3);
167  result = result > length(x4) ? result : length(x4);
168  return result;
169  }
170 
171  // ****************** additions for new VV *************************
172  template <typename T>
173  struct scalar_type<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> > {
174  typedef typename scalar_type<T>::type type;
175  };
176 
177  template <typename T>
178  struct scalar_type<T*> {
179  typedef typename scalar_type<T>::type type;
180  };
181 
182 
183  // handles scalar, eigen vec, eigen row vec, std vec
184  template <typename T>
185  struct is_vector_like {
187  };
188  template <typename T>
189  struct is_vector_like<T*> {
190  enum { value = true };
191  };
192  // handles const
193  template <typename T>
194  struct is_vector_like<const T> {
196  };
197  // handles eigen matrix
198  template <typename T>
199  struct is_vector_like<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> > {
200  enum { value = true };
201  };
202 
203 
204  template <typename T,
205  bool is_array = stan::is_vector_like<T>::value>
206  class VectorView {
207  public:
208  typedef typename scalar_type<T>::type scalar_t;
209 
210  VectorView(scalar_t& c) : x_(&c) { }
211 
212  VectorView(std::vector<scalar_t>& v) : x_(&v[0]) { }
213 
214  template <int R, int C>
215  VectorView(Eigen::Matrix<scalar_t,R,C>& m) : x_(&m(0)) { }
216 
217  VectorView(scalar_t* x) : x_(x) { }
218 
220  if (is_array) return x_[i];
221  else return x_[0];
222  }
223  private:
224  scalar_t* x_;
225  };
226 
227  template <typename T, bool is_array>
228  class VectorView<const T, is_array> {
229  public:
230  typedef typename scalar_type<T>::type scalar_t;
231 
232  VectorView(const scalar_t& c) : x_(&c) { }
233 
234  VectorView(const scalar_t* x) : x_(x) { }
235 
236  VectorView(const std::vector<scalar_t>& v) : x_(&v[0]) { }
237 
238  template <int R, int C>
239  VectorView(const Eigen::Matrix<scalar_t,R,C>& m) : x_(&m(0)) { }
240 
241  const scalar_t operator[](int i) const {
242  if (is_array) return x_[i];
243  else return x_[0];
244  }
245  private:
246  const scalar_t* x_;
247  };
248 
249  // simplify to hold value in common case where it's more efficient
250  template <>
251  class VectorView<const double, false> {
252  public:
253  VectorView(double x) : x_(x) { }
254  double operator[](int /* i */) const {
255  return x_;
256  }
257  private:
258  const double x_;
259  };
260 
261 
262 
263  // template<typename T,
264  // bool is_vec = stan::is_vector<T>::value>
265  // class VectorView {
266  // private:
267  // T* x_;
268  // public:
269  // VectorView(T& x) : x_(&x) { }
270  // typename scalar_type<T>::type& operator[](int /*i*/) {
271  // return *x_;
272  // }
273  // };
274 
275  // template<typename T>
276  // class VectorView<T*,false> {
277  // private:
278  // T* x_;
279  // public:
280  // VectorView(T* x) : x_(x) { }
281  // typename scalar_type<T>::type& operator[](int i) {
282  // return *x_;
283  // }
284  // };
285 
286  // template<typename T>
287  // class VectorView<T,true> {
288  // private:
289  // T* x_;
290  // public:
291  // VectorView(T& x) : x_(&x) { }
292  // typename scalar_type<T>::type& operator[](int i) {
293  // return (*x_)[i];
294  // }
295  // };
296 
297  // template<typename T>
298  // class VectorView<T*,true> {
299  // private:
300  // T* x_;
301  // public:
302  // VectorView(T* x) : x_(x) { }
303  // typename scalar_type<T>::type& operator[](int i) {
304  // return x_[i];
305  // }
306  // };
307 
308  // template<typename T>
309  // class VectorView<const T,true> {
310  // private:
311  // const T* x_;
312  // public:
313  // VectorView(const T& x) : x_(&x) { }
314  // typename scalar_type<T>::type operator[](int i) {
315  // return (*x_)[i];
316  // }
317  // };
318 
319  template<bool used, typename T = double, bool is_vec = stan::is_vector<T>::value>
321  public:
322  DoubleVectorView(size_t /* n */) { }
323  double& operator[](size_t /* i */) {
324  throw std::runtime_error("used is false. this should never be called");
325  }
326  };
327 
328  template<typename T>
329  class DoubleVectorView<true, T, false> {
330  private:
331  double x_;
332  public:
333  DoubleVectorView(size_t /* n */) : x_(0.0) { }
334  double& operator[](size_t /* i */) {
335  return x_;
336  }
337  };
338 
339  template<typename T>
340  class DoubleVectorView<true, T, true> {
341  private:
342  std::vector<double> x_;
343  public:
344  DoubleVectorView(size_t n) : x_(n) { }
345  double& operator[](size_t i) {
346  return x_[i];
347  }
348  };
349 
354  template <typename T1,
355  typename T2 = double,
356  typename T3 = double,
357  typename T4 = double,
358  typename T5 = double,
359  typename T6 = double>
360  struct return_type {
361  typedef typename
362  boost::math::tools::promote_args<typename scalar_type<T1>::type,
363  typename scalar_type<T2>::type,
364  typename scalar_type<T3>::type,
365  typename scalar_type<T4>::type,
366  typename scalar_type<T5>::type,
369  };
370 
371 
372 
373 }
374 
375 #endif
internal::traits< Derived >::Scalar value_type
double & operator[](size_t)
Definition: traits.hpp:323
DoubleVectorView(size_t)
Definition: traits.hpp:322
VectorView(const std::vector< scalar_t > &v)
Definition: traits.hpp:236
VectorView(const Eigen::Matrix< scalar_t, R, C > &m)
Definition: traits.hpp:239
scalar_type< T >::type scalar_t
Definition: traits.hpp:230
const scalar_t operator[](int i) const
Definition: traits.hpp:241
VectorView(scalar_t &c)
Definition: traits.hpp:210
scalar_type< T >::type scalar_t
Definition: traits.hpp:208
VectorView(Eigen::Matrix< scalar_t, R, C > &m)
Definition: traits.hpp:215
VectorView(std::vector< scalar_t > &v)
Definition: traits.hpp:212
VectorView(scalar_t *x)
Definition: traits.hpp:217
scalar_t & operator[](int i)
Definition: traits.hpp:219
(Expert) Numerical traits for algorithmic differentiation variables.
Definition: matrix.hpp:53
Probability, optimization and sampling library.
Definition: agrad.cpp:6
size_t length(const T &x)
Definition: traits.hpp:111
size_t size_of(const T &x)
Definition: traits.hpp:143
size_t max_size(const T1 &x1, const T2 &x2)
Definition: traits.hpp:148
Template specification of functions in std for Stan.
Definition: agrad.hpp:2306
Metaprogram to determine if a type has a base scalar type that can be assigned to type double.
Definition: traits.hpp:39
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Definition: traits.hpp:25
Metaprogram to calculate the base scalar return type resulting from promoting all the scalar types of...
Definition: traits.hpp:360
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Definition: traits.hpp:368
scalar_type< T >::type type
Definition: traits.hpp:179
Metaprogram structure to determine the base scalar type of a template argument.
Definition: traits.hpp:104
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: traits.hpp:105
static size_t size_of(const T &x)
Definition: traits.hpp:137
static size_t size_of(const T &x)
Definition: traits.hpp:130

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