rvalue_return.hpp
Go to the documentation of this file.
1 #ifndef STAN_MODEL_INDEXING_RVALUE_RETURN_HPP
2 #define STAN_MODEL_INDEXING_RVALUE_RETURN_HPP
3 
4 #include <Eigen/Dense>
7 #include <vector>
8 
9 namespace stan {
10 
11  namespace model {
12 
13  /**
14  * Primary template class for metaprogram to calculate return
15  * value for <code>model::rvalue()</code> for the container or
16  * scalar type and index list type specified in the template
17  * parameters.
18  *
19  * <p>Specializations of this class will define a typedef
20  * <code>type</code> for the return type.
21  *
22  * @tparam C Type of container or scalar.
23  * @tparam L Type of index list.
24  */
25  template <typename C, typename L>
26  struct rvalue_return {
27  };
28 
29  /**
30  * Template class specialization for nil indexes, which provide
31  * the container type as the return type. The container type may
32  * be a scalar type or a container.
33  *
34  * @tparam C Container or scalar type.
35  */
36  template <typename C>
38  /**
39  * Return type is the container or scalar type.
40  */
41  typedef C type;
42  };
43 
44  // SINGLE INDEX
45 
46  /**
47  * Template class specialization for an Eigen matrix, vector or
48  * rwo vector and one multiple index.
49  *
50  * @tparam T Type of scalar in matrix.
51  * @tparam I Type of first index (only instantiated to multi
52  * indexes).
53  * @tparam R Rows for matrix.
54  * @tparam C Columns for matrix.
55  */
56  template <typename T, typename I, int R, int C>
57  struct rvalue_return<Eigen::Matrix<T, R, C>,
58  cons_index_list<I, nil_index_list> > {
59  /**
60  * Return type is the matrix container type.
61  */
62  typedef Eigen::Matrix<T, R, C> type;
63  };
64 
65  /**
66  * Template class specialization for an Eigen matrix and one single
67  * index.
68  *
69  * @tparam T Type of scalar in matrix.
70  */
71  template <typename T>
72  struct rvalue_return<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>,
73  cons_index_list<index_uni, nil_index_list> > {
74  /**
75  * Return type is row vector.
76  */
77  typedef Eigen::Matrix<T, 1, Eigen::Dynamic> type;
78  };
79 
80  /**
81  * Template class specialization for an Eigen vector and one single
82  * index.
83  *
84  * @tparam T Type of scalar in vector.
85  */
86  template <typename T>
87  struct rvalue_return<Eigen::Matrix<T, Eigen::Dynamic, 1>,
88  cons_index_list<index_uni, nil_index_list> > {
89  /**
90  * Return type is scalar type of vector.
91  */
92  typedef T type;
93  };
94 
95  /**
96  * Template class specialization for an Eigen row vector and one
97  * single index.
98  *
99  * @tparam T Type of scalar in row vector.
100  */
101  template <typename T>
102  struct rvalue_return<Eigen::Matrix<T, 1, Eigen::Dynamic>,
103  cons_index_list<index_uni, nil_index_list> > {
104  /**
105  * Return type is scalar type of row vector.
106  */
107  typedef T type;
108  };
109 
110  /**
111  * Template specialization for an Eigen matrix and two multiple
112  * indexes.
113  *
114  * @tparam T Type of scalar in matrix.
115  * @tparam I1 Type of first multiple index.
116  * @tparam I2 Type of second multiple index.
117  */
118  template <typename T, typename I1, typename I2>
119  struct rvalue_return<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>,
120  cons_index_list<I1,
121  cons_index_list<I2,
122  nil_index_list> > > {
123  /**
124  * Return type is matrix container type.
125  */
126  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> type;
127  };
128 
129  /**
130  * Template specialization for an Eigen matrix with one multiple
131  * index followed by one single index.
132  *
133  * @tparam T Type of scalar in matrix.
134  * @tparam I Type of multiple index.
135  */
136  template <typename T, typename I>
137  struct rvalue_return<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>,
138  cons_index_list<I,
139  cons_index_list<index_uni,
140  nil_index_list> > > {
141  /**
142  * Return type is vector with same scalar type as matrix container.
143  */
144  typedef Eigen::Matrix<T, Eigen::Dynamic, 1> type;
145  };
146 
147  /**
148  * Template specialization for an Eigen matrix with one single
149  * index followed by one multiple index.
150  *
151  * @tparam T Type of scalar in matrix.
152  * @tparam I Type of multiple index.
153  */
154  template <typename T, typename I>
155  struct rvalue_return<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>,
156  cons_index_list<index_uni,
157  cons_index_list<I,
158  nil_index_list> > > {
159  /**
160  * Return type is row vector with same scalar type as matrix container.
161  */
162  typedef Eigen::Matrix<T, 1, Eigen::Dynamic> type;
163  };
164 
165  /**
166  * Template specialization for an Eigen matrix with two single
167  * indexes.
168  *
169  * @tparam T Type of scalar in matrix.
170  */
171  template <typename T>
172  struct rvalue_return<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>,
173  cons_index_list<index_uni,
174  cons_index_list<index_uni,
175  nil_index_list> > > {
176  /**
177  * Return type is scalar type of matrix.
178  */
179  typedef T type;
180  };
181 
182  /**
183  * Template specialization for a standard vector whose index list
184  * starts with a multiple index.
185  *
186  * @tparam C Element type for standard vector (container or
187  * scalar).
188  * @tparam I Multiple index type.
189  * @tparam L Following index types.
190  */
191  template <typename C, typename I, typename L>
192  struct rvalue_return<std::vector<C>, cons_index_list<I, L> > {
193  /**
194  * Return type is calculated recursively as a standard vector of
195  * the rvalue return for the element type C and following index
196  * types L.
197  */
199  };
200 
201 
202  /**
203  * Template specialization for a standard vector whose index list
204  * starts with a single index.
205  *
206  * @tparam C Element type for standard vector (container or
207  * scalar).
208  * @tparam L Following index types.
209  */
210  template <typename C, typename L>
211  struct rvalue_return<std::vector<C>, cons_index_list<index_uni, L> > {
212  /**
213  * Return type is calculated recursively as the rvalue return
214  * for the element type C and following index types L.
215  */
216  typedef typename rvalue_return<C, L>::type type;
217  };
218 
219  }
220 }
221 #endif
Definition: StanUtils.h:6
const double C
std::vector< typename rvalue_return< C, L >::type > type
double T
Definition: Xdiff_gwt.C:5
const XML_Char XML_Content * model
Definition: expat.h:151