1 #ifndef STAN_MATH_PRIM_FUN_GET_BASE1_LHS_HPP
2 #define STAN_MATH_PRIM_FUN_GET_BASE1_LHS_HPP
3
4 #include <stan/math/prim/err.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 #include <vector>
7
8 namespace stan {
9 namespace math {
10
11 /**
12 * Return a reference to the value of the specified vector at the
13 * specified base-one index. If the index is out of range, throw
14 * a <code>std::out_of_range</code> exception with the specified
15 * error message and index indicated.
16 *
17 * @tparam T type of value
18 * @param x Vector from which to get a value.
19 * @param i Index into vector plus 1.
20 * @param error_msg Error message if the index is out of range.
21 * @param idx Nested index level to report in error message if
22 * the index is out of range.
23 * @return Value of vector at <code>i - 1</code>
24 * @throw std::out_of_range if idx is out of range.
25 */
26 template <typename T>
get_base1_lhs(std::vector<T> & x,size_t i,const char * error_msg,size_t idx)27 inline T& get_base1_lhs(std::vector<T>& x, size_t i, const char* error_msg,
28 size_t idx) {
29 check_range("[]", "x", x.size(), i, idx, error_msg);
30 return x[i - 1];
31 }
32
33 /**
34 * Return a reference to the value of the specified vector at the
35 * specified base-one indexes. If an index is out of range, throw
36 * a <code>std::out_of_range</code> exception with the specified
37 * error message and index indicated.
38 *
39 * @tparam T type of value
40 * @param x Vector from which to get a value.
41 * @param i1 First index plus 1.
42 * @param i2 Second index plus 1.
43 * @param error_msg Error message if an index is out of range.
44 * @param idx Nested index level to report in error message if
45 * the index is out of range.
46 * @return Value of vector at indexes.
47 * @throw std::out_of_range if idx is out of range.
48 */
49 template <typename T>
get_base1_lhs(std::vector<std::vector<T>> & x,size_t i1,size_t i2,const char * error_msg,size_t idx)50 inline T& get_base1_lhs(std::vector<std::vector<T> >& x, size_t i1, size_t i2,
51 const char* error_msg, size_t idx) {
52 check_range("[]", "x", x.size(), i1, idx, error_msg);
53 return get_base1_lhs(x[i1 - 1], i2, error_msg, idx + 1);
54 }
55
56 /**
57 * Return a reference to the value of the specified vector at the
58 * specified base-one indexes. If an index is out of range, throw
59 * a <code>std::out_of_range</code> exception with the specified
60 * error message and index indicated.
61 *
62 * @tparam T type of value
63 * @param x Vector from which to get a value.
64 * @param i1 First index plus 1.
65 * @param i2 Second index plus 1.
66 * @param i3 Third index plus 1.
67 * @param error_msg Error message if an index is out of range.
68 * @param idx Nested index level to report in error message if
69 * the index is out of range.
70 * @return Value of vector at indexes.
71 * @throw std::out_of_range if idx is out of range.
72 */
73 template <typename T>
get_base1_lhs(std::vector<std::vector<std::vector<T>>> & x,size_t i1,size_t i2,size_t i3,const char * error_msg,size_t idx)74 inline T& get_base1_lhs(std::vector<std::vector<std::vector<T> > >& x,
75 size_t i1, size_t i2, size_t i3, const char* error_msg,
76 size_t idx) {
77 check_range("[]", "x", x.size(), i1, idx, error_msg);
78 return get_base1_lhs(x[i1 - 1], i2, i3, error_msg, idx + 1);
79 }
80
81 /**
82 * Return a reference to the value of the specified vector at the
83 * specified base-one indexes. If an index is out of range, throw
84 * a <code>std::out_of_range</code> exception with the specified
85 * error message and index indicated.
86 *
87 * @tparam T type of value
88 * @param x Vector from which to get a value.
89 * @param i1 First index plus 1.
90 * @param i2 Second index plus 1.
91 * @param i3 Third index plus 1.
92 * @param i4 Fourth index plus 1.
93 * @param error_msg Error message if an index is out of range.
94 * @param idx Nested index level to report in error message if
95 * the index is out of range.
96 * @return Value of vector at indexes.
97 * @throw std::out_of_range if idx is out of range.
98 */
99 template <typename T>
get_base1_lhs(std::vector<std::vector<std::vector<std::vector<T>>>> & x,size_t i1,size_t i2,size_t i3,size_t i4,const char * error_msg,size_t idx)100 inline T& get_base1_lhs(
101 std::vector<std::vector<std::vector<std::vector<T> > > >& x, size_t i1,
102 size_t i2, size_t i3, size_t i4, const char* error_msg, size_t idx) {
103 check_range("[]", "x", x.size(), i1, idx, error_msg);
104 return get_base1_lhs(x[i1 - 1], i2, i3, i4, error_msg, idx + 1);
105 }
106
107 /**
108 * Return a reference to the value of the specified vector at the
109 * specified base-one indexes. If an index is out of range, throw
110 * a <code>std::out_of_range</code> exception with the specified
111 * error message and index indicated.
112 *
113 * @tparam T type of value
114 * @param x Vector from which to get a value.
115 * @param i1 First index plus 1.
116 * @param i2 Second index plus 1.
117 * @param i3 Third index plus 1.
118 * @param i4 Fourth index plus 1.
119 * @param i5 Fifth index plus 1.
120 * @param error_msg Error message if an index is out of range.
121 * @param idx Nested index level to report in error message if
122 * the index is out of range.
123 * @return Value of vector at indexes.
124 * @throw std::out_of_range if idx is out of range.
125 */
126 template <typename T>
get_base1_lhs(std::vector<std::vector<std::vector<std::vector<std::vector<T>>>>> & x,size_t i1,size_t i2,size_t i3,size_t i4,size_t i5,const char * error_msg,size_t idx)127 inline T& get_base1_lhs(
128 std::vector<std::vector<std::vector<std::vector<std::vector<T> > > > >& x,
129 size_t i1, size_t i2, size_t i3, size_t i4, size_t i5,
130 const char* error_msg, size_t idx) {
131 check_range("[]", "x", x.size(), i1, idx, error_msg);
132 return get_base1_lhs(x[i1 - 1], i2, i3, i4, i5, error_msg, idx + 1);
133 }
134
135 /**
136 * Return a reference to the value of the specified vector at the
137 * specified base-one indexes. If an index is out of range, throw
138 * a <code>std::out_of_range</code> exception with the specified
139 * error message and index indicated.
140 *
141 * @tparam T type of value
142 * @param x Vector from which to get a value.
143 * @param i1 First index plus 1.
144 * @param i2 Second index plus 1.
145 * @param i3 Third index plus 1.
146 * @param i4 Fourth index plus 1.
147 * @param i5 Fifth index plus 1.
148 * @param i6 Sixth index plus 1.
149 * @param error_msg Error message if an index is out of range.
150 * @param idx Nested index level to report in error message if
151 * the index is out of range.
152 * @return Value of vector at indexes.
153 * @throw std::out_of_range if idx is out of range.
154 */
155 template <typename T>
get_base1_lhs(std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<T>>>>>> & x,size_t i1,size_t i2,size_t i3,size_t i4,size_t i5,size_t i6,const char * error_msg,size_t idx)156 inline T& get_base1_lhs(
157 std::vector<std::vector<
158 std::vector<std::vector<std::vector<std::vector<T> > > > > >& x,
159 size_t i1, size_t i2, size_t i3, size_t i4, size_t i5, size_t i6,
160 const char* error_msg, size_t idx) {
161 check_range("[]", "x", x.size(), i1, idx, error_msg);
162 return get_base1_lhs(x[i1 - 1], i2, i3, i4, i5, i6, error_msg, idx + 1);
163 }
164
165 /**
166 * Return a reference to the value of the specified vector at the
167 * specified base-one indexes. If an index is out of range, throw
168 * a <code>std::out_of_range</code> exception with the specified
169 * error message and index indicated.
170 *
171 * @tparam T type of value
172 * @param x Vector from which to get a value.
173 * @param i1 First index plus 1.
174 * @param i2 Second index plus 1.
175 * @param i3 Third index plus 1.
176 * @param i4 Fourth index plus 1.
177 * @param i5 Fifth index plus 1.
178 * @param i6 Sixth index plus 1.
179 * @param i7 Seventh index plus 1.
180 * @param error_msg Error message if an index is out of range.
181 * @param idx Nested index level to report in error message if
182 * the index is out of range.
183 * @return Value of vector at indexes.
184 * @throw std::out_of_range if idx is out of range.
185 */
186 template <typename T>
get_base1_lhs(std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<T>>>>>>> & x,size_t i1,size_t i2,size_t i3,size_t i4,size_t i5,size_t i6,size_t i7,const char * error_msg,size_t idx)187 inline T& get_base1_lhs(
188 std::vector<std::vector<std::vector<
189 std::vector<std::vector<std::vector<std::vector<T> > > > > > >& x,
190 size_t i1, size_t i2, size_t i3, size_t i4, size_t i5, size_t i6, size_t i7,
191 const char* error_msg, size_t idx) {
192 check_range("[]", "x", x.size(), i1, idx, error_msg);
193 return get_base1_lhs(x[i1 - 1], i2, i3, i4, i5, i6, i7, error_msg, idx + 1);
194 }
195
196 /**
197 * Return a reference to the value of the specified vector at the
198 * specified base-one indexes. If an index is out of range, throw
199 * a <code>std::out_of_range</code> exception with the specified
200 * error message and index indicated.
201 *
202 * @tparam T type of value
203 * @param x Vector from which to get a value.
204 * @param i1 First index plus 1.
205 * @param i2 Second index plus 1.
206 * @param i3 Third index plus 1.
207 * @param i4 Fourth index plus 1.
208 * @param i5 Fifth index plus 1.
209 * @param i6 Sixth index plus 1.
210 * @param i7 Seventh index plus 1.
211 * @param i8 Eigth index plus 1.
212 * @param error_msg Error message if an index is out of range.
213 * @param idx Nested index level to report in error message if
214 * the index is out of range.
215 * @return Value of vector at indexes.
216 * @throw std::out_of_range if idx is out of range.
217 */
218 template <typename T>
get_base1_lhs(std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<T>>>>>>>> & x,size_t i1,size_t i2,size_t i3,size_t i4,size_t i5,size_t i6,size_t i7,size_t i8,const char * error_msg,size_t idx)219 inline T& get_base1_lhs(
220 std::vector<std::vector<std::vector<std::vector<
221 std::vector<std::vector<std::vector<std::vector<T> > > > > > > >& x,
222 size_t i1, size_t i2, size_t i3, size_t i4, size_t i5, size_t i6, size_t i7,
223 size_t i8, const char* error_msg, size_t idx) {
224 check_range("[]", "x", x.size(), i1, idx, error_msg);
225 return get_base1_lhs(x[i1 - 1], i2, i3, i4, i5, i6, i7, i8, error_msg,
226 idx + 1);
227 }
228
229 /**
230 * Return a copy of the row of the specified vector at the specified
231 * base-one row index. If the index is out of range, throw a
232 * <code>std::out_of_range</code> exception with the specified
233 * error message and index indicated.
234 *
235 * <b>Warning</b>: Because a copy is involved, it is inefficient
236 * to access element of matrices by first using this method
237 * to get a row then using a second call to get the value at
238 * a specified column.
239 *
240 * @tparam T type of value
241 * @param x Matrix from which to get a row
242 * @param m Index into matrix plus 1.
243 * @param error_msg Error message if the index is out of range.
244 * @param idx Nested index level to report in error message if
245 * the index is out of range.
246 * @return Row of matrix at <code>i - 1</code>.
247 * @throw std::out_of_range if idx is out of range.
248 */
249 template <typename T>
250 inline Eigen::Block<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >
get_base1_lhs(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & x,size_t m,const char * error_msg,size_t idx)251 get_base1_lhs(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& x, size_t m,
252 const char* error_msg, size_t idx) {
253 check_range("[]", "rows of x", x.rows(), m, idx, error_msg);
254 return x.block(m - 1, 0, 1, x.cols());
255 }
256
257 /**
258 * Return a reference to the value of the specified matrix at the specified
259 * base-one row and column indexes. If either index is out of range,
260 * throw a <code>std::out_of_range</code> exception with the
261 * specified error message and index indicated.
262 *
263 * @tparam T type of value
264 * @param x Matrix from which to get a row
265 * @param m Row index plus 1.
266 * @param n Column index plus 1.
267 * @param error_msg Error message if either index is out of range.
268 * @param idx Nested index level to report in error message if
269 * either index is out of range.
270 * @return Value of matrix at row <code>m - 1</code> and column
271 * <code>n - 1</code>.
272 * @throw std::out_of_range if idx is out of range.
273 */
274 template <typename T>
get_base1_lhs(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & x,size_t m,size_t n,const char * error_msg,size_t idx)275 inline T& get_base1_lhs(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& x,
276 size_t m, size_t n, const char* error_msg, size_t idx) {
277 check_range("[]", "rows of x", x.rows(), m, idx, error_msg);
278 check_range("[]", "cols of x", x.cols(), n, idx + 1, error_msg);
279 return x(m - 1, n - 1);
280 }
281
282 /**
283 * Return a reference to the value of the specified column vector
284 * at the specified base-one index. If the index is out of range,
285 * throw a <code>std::out_of_range</code> exception with the
286 * specified error message and index indicated.
287 *
288 * @tparam T type of value
289 * @param x Column vector from which to get a value.
290 * @param m Row index plus 1.
291 * @param error_msg Error message if the index is out of range.
292 * @param idx Nested index level to report in error message if
293 * the index is out of range.
294 * @return Value of column vector at row <code>m - 1</code>.
295 * @throw std::out_of_range if idx is out of range.
296 */
297 template <typename T>
get_base1_lhs(Eigen::Matrix<T,Eigen::Dynamic,1> & x,size_t m,const char * error_msg,size_t idx)298 inline T& get_base1_lhs(Eigen::Matrix<T, Eigen::Dynamic, 1>& x, size_t m,
299 const char* error_msg, size_t idx) {
300 check_range("[]", "x", x.size(), m, idx, error_msg);
301 return x(m - 1);
302 }
303
304 /**
305 * Return a reference to the value of the specified row vector
306 * at the specified base-one index. If the index is out of range,
307 * throw a <code>std::out_of_range</code> exception with the
308 * specified error message and index indicated.
309 *
310 * @tparam T type of value
311 * @param x Row vector from which to get a value.
312 * @param n Column index plus 1.
313 * @param error_msg Error message if the index is out of range.
314 * @param idx Nested index level to report in error message if
315 * the index is out of range.
316 * @return Value of row vector at column <code>n - 1</code>.
317 * @throw std::out_of_range if idx is out of range.
318 */
319 template <typename T>
get_base1_lhs(Eigen::Matrix<T,1,Eigen::Dynamic> & x,size_t n,const char * error_msg,size_t idx)320 inline T& get_base1_lhs(Eigen::Matrix<T, 1, Eigen::Dynamic>& x, size_t n,
321 const char* error_msg, size_t idx) {
322 check_range("[]", "x", x.size(), n, idx, error_msg);
323 return x(n - 1);
324 }
325
326 } // namespace math
327 } // namespace stan
328
329 #endif
330