| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
VectorView.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_META_VECTORVIEW_HPP
2 #define STAN_MATH_PRIM_SCAL_META_VECTORVIEW_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/scal/meta/scalar_type.hpp>
6 #include <stan/math/prim/mat/meta/is_vector_like.hpp>
7 #include <stan/math/prim/scal/meta/is_vector_like.hpp>
8 #include <stdexcept>
9 #include <vector>
10
11 namespace stan {
12
38 template <typename T,
39 bool is_array = stan::is_vector_like<T>::value,
40 bool throw_if_accessed = false>
41 class VectorView {
42 public:
43 typedef typename scalar_type<T>::type scalar_t;
44
45 explicit VectorView(scalar_t& c) : x_(&c) { }
46
47 explicit VectorView(std::vector<scalar_t>& v) : x_(&v[0]) { }
48
49 template <int R, int C>
50 explicit VectorView(Eigen::Matrix<scalar_t, R, C>& m) : x_(&m(0)) { }
51
52 explicit VectorView(scalar_t* x) : x_(x) { }
53
54 scalar_t& operator[](int i) {
55 if (throw_if_accessed)
56 throw std::out_of_range("VectorView: this cannot be accessed");
57 if (is_array)
58 return x_[i];
59 else
60 return x_[0];
61 }
62 private:
63 scalar_t* x_;
64 };
65
66
71 template <typename T, bool is_array, bool throw_if_accessed>
72 class VectorView<const T, is_array, throw_if_accessed> {
73 public:
74 typedef typename scalar_type<T>::type scalar_t;
75
76 explicit VectorView(const scalar_t& c) : x_(&c) { }
77
78 explicit VectorView(const scalar_t* x) : x_(x) { }
79
80 explicit VectorView(const std::vector<scalar_t>& v) : x_(&v[0]) { }
81
82 template <int R, int C>
83 explicit VectorView(const Eigen::Matrix<scalar_t, R, C>& m) : x_(&m(0)) { }
84
85 const scalar_t& operator[](int i) const {
86 if (throw_if_accessed)
87 throw std::out_of_range("VectorView: this cannot be accessed");
88 if (is_array)
89 return x_[i];
90 else
91 return x_[0];
92 }
93 private:
94 const scalar_t* x_;
95 };
96
97 // simplify to hold value in common case where it's more efficient
98 template <>
99 class VectorView<const double, false, false> {
100 public:
101 explicit VectorView(double x) : x_(x) { }
102 double operator[](int /* i */) const {
103 return x_;
104 }
105 private:
106 const double x_;
107 };
108
109
110
111 }
112 #endif
113
stan::VectorView::operator[]
scalar_t & operator[](int i)
Definition: VectorView.hpp:54
stan::VectorView::VectorView
VectorView(scalar_t &c)
Definition: VectorView.hpp:45
stan::VectorView::scalar_t
scalar_type< T >::type scalar_t
Definition: VectorView.hpp:43
stan::is_vector_like
Definition: is_vector_like.hpp:12
is_vector_like.hpp
stan::scalar_type::type
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:38
stan::VectorView::VectorView
VectorView(std::vector< scalar_t > &v)
Definition: VectorView.hpp:47
stan::VectorView< const T, is_array, throw_if_accessed >::VectorView
VectorView(const scalar_t *x)
Definition: VectorView.hpp:78
stan::VectorView< const T, is_array, throw_if_accessed >::scalar_t
scalar_type< T >::type scalar_t
Definition: VectorView.hpp:74
is_vector_like.hpp
stan::VectorView< const double, false, false >::VectorView
VectorView(double x)
Definition: VectorView.hpp:101
stan::VectorView< const T, is_array, throw_if_accessed >::VectorView
VectorView(const std::vector< scalar_t > &v)
Definition: VectorView.hpp:80
scalar_type.hpp
stan::VectorView::VectorView
VectorView(Eigen::Matrix< scalar_t, R, C > &m)
Definition: VectorView.hpp:50
stan::math::out_of_range
void out_of_range(const char *function, const int max, const int index, const char *msg1="", const char *msg2="")
Throw an out_of_range exception with a consistently formatted message.
Definition: out_of_range.hpp:30
Eigen.hpp
stan::VectorView< const T, is_array, throw_if_accessed >::operator[]
const scalar_t & operator[](int i) const
Definition: VectorView.hpp:85
stan::VectorView< const double, false, false >::operator[]
double operator[](int) const
Definition: VectorView.hpp:102
stan::VectorView< const T, is_array, throw_if_accessed >::VectorView
VectorView(const scalar_t &c)
Definition: VectorView.hpp:76
stan::VectorView::VectorView
VectorView(scalar_t *x)
Definition: VectorView.hpp:52
stan::VectorView
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
stan::VectorView< const T, is_array, throw_if_accessed >::VectorView
VectorView(const Eigen::Matrix< scalar_t, R, C > &m)
Definition: VectorView.hpp:83

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