|
1 | 1 | #ifndef STAN_IO_READER_HPP |
2 | 2 | #define STAN_IO_READER_HPP |
3 | 3 |
|
4 | | -#include <stan/math/prim.hpp> |
| 4 | +#include <stan/math/rev.hpp> |
5 | 5 | #include <stdexcept> |
6 | 6 | #include <string> |
7 | 7 | #include <vector> |
@@ -54,13 +54,20 @@ class reader { |
54 | 54 | } |
55 | 55 |
|
56 | 56 | public: |
57 | | - typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrix_t; |
58 | | - typedef Eigen::Matrix<T, Eigen::Dynamic, 1> vector_t; |
59 | | - typedef Eigen::Matrix<T, 1, Eigen::Dynamic> row_vector_t; |
| 57 | + using matrix_t = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>; |
| 58 | + using vector_t = Eigen::Matrix<T, Eigen::Dynamic, 1>; |
| 59 | + using row_vector_t = Eigen::Matrix<T, 1, Eigen::Dynamic>; |
60 | 60 |
|
61 | | - typedef Eigen::Map<matrix_t> map_matrix_t; |
62 | | - typedef Eigen::Map<vector_t> map_vector_t; |
63 | | - typedef Eigen::Map<row_vector_t> map_row_vector_t; |
| 61 | + using map_matrix_t = Eigen::Map<matrix_t>; |
| 62 | + using map_vector_t = Eigen::Map<vector_t>; |
| 63 | + using map_row_vector_t = Eigen::Map<row_vector_t>; |
| 64 | + |
| 65 | + using var_matrix_t = stan::math::var_value< |
| 66 | + Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>>; |
| 67 | + using var_vector_t |
| 68 | + = stan::math::var_value<Eigen::Matrix<double, Eigen::Dynamic, 1>>; |
| 69 | + using var_row_vector_t |
| 70 | + = stan::math::var_value<Eigen::Matrix<double, 1, Eigen::Dynamic>>; |
64 | 71 |
|
65 | 72 | /** |
66 | 73 | * Construct a variable reader using the specified vectors |
@@ -186,6 +193,33 @@ class reader { |
186 | 193 | return vector_t(); |
187 | 194 | return map_vector_t(&scalar_ptr_increment(m), m); |
188 | 195 | } |
| 196 | + |
| 197 | + /** |
| 198 | + * Return a `var_value` with inner type column vector with specified |
| 199 | + * dimensionality made up of the next scalars. |
| 200 | + * |
| 201 | + * @param m Number of rows in the vector to read. |
| 202 | + * @return Column vector made up of the next scalars. |
| 203 | + */ |
| 204 | + template <typename T_ = T, require_st_var<T_> * = nullptr> |
| 205 | + inline var_vector_t var_vector(size_t m) { |
| 206 | + if (m == 0) |
| 207 | + return var_vector_t(Eigen::VectorXd(0)); |
| 208 | + return stan::math::to_var_value(map_vector_t(&scalar_ptr_increment(m), m)); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Return a column vector of specified dimensionality made up of |
| 213 | + * the next scalars. |
| 214 | + * |
| 215 | + * @param m Number of rows in the vector to read. |
| 216 | + * @return Column vector made up of the next scalars. |
| 217 | + */ |
| 218 | + template <typename T_ = T, require_st_arithmetic<T_> * = nullptr> |
| 219 | + inline vector_t var_vector(size_t m) { |
| 220 | + return this->vector(m); |
| 221 | + } |
| 222 | + |
189 | 223 | /** |
190 | 224 | * Return a column vector of specified dimensionality made up of |
191 | 225 | * the next scalars. The constraint is a no-op. |
@@ -225,6 +259,33 @@ class reader { |
225 | 259 | return map_row_vector_t(&scalar_ptr_increment(m), m); |
226 | 260 | } |
227 | 261 |
|
| 262 | + /** |
| 263 | + * Return a `var_value` with inner type as a row vector with specified |
| 264 | + * dimensionality made up of the next scalars. |
| 265 | + * |
| 266 | + * @param m Number of rows in the vector to read. |
| 267 | + * @return Column vector made up of the next scalars. |
| 268 | + */ |
| 269 | + template <typename T_ = T, require_st_var<T_> * = nullptr> |
| 270 | + inline var_row_vector_t var_row_vector(size_t m) { |
| 271 | + if (m == 0) |
| 272 | + return var_row_vector_t(Eigen::RowVectorXd(0)); |
| 273 | + return stan::math::to_var_value( |
| 274 | + map_row_vector_t(&scalar_ptr_increment(m), m)); |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * Return a row vector of specified dimensionality made up of |
| 279 | + * the next scalars. |
| 280 | + * |
| 281 | + * @param m Number of rows in the vector to read. |
| 282 | + * @return Column vector made up of the next scalars. |
| 283 | + */ |
| 284 | + template <typename T_ = T, require_st_arithmetic<T_> * = nullptr> |
| 285 | + inline row_vector_t var_row_vector(size_t m) { |
| 286 | + return this->row_vector(m); |
| 287 | + } |
| 288 | + |
228 | 289 | /** |
229 | 290 | * Return a row vector of specified dimensionality made up of |
230 | 291 | * the next scalars. The constraint is a no-op. |
@@ -276,6 +337,53 @@ class reader { |
276 | 337 | return map_matrix_t(&scalar_ptr_increment(m * n), m, n); |
277 | 338 | } |
278 | 339 |
|
| 340 | + /** |
| 341 | + * Return a `var_value` with inner type matrix with the specified |
| 342 | + * dimensionality made up of the next scalars arranged in column-major order. |
| 343 | + * |
| 344 | + * Row-major reading means that if a matrix of <code>m=2</code> |
| 345 | + * rows and <code>n=3</code> columns is read and the next |
| 346 | + * scalar values are <code>1,2,3,4,5,6</code>, the result is |
| 347 | + * |
| 348 | + * <pre> |
| 349 | + * a = 1 4 |
| 350 | + * 2 5 |
| 351 | + * 3 6</pre> |
| 352 | + * |
| 353 | + * @param m Number of rows. |
| 354 | + * @param n Number of columns. |
| 355 | + * @return Eigen::Matrix made up of the next scalars. |
| 356 | + */ |
| 357 | + template <typename T_ = T, require_st_var<T_> * = nullptr> |
| 358 | + inline var_matrix_t var_matrix(size_t m, size_t n) { |
| 359 | + if (m == 0 || n == 0) |
| 360 | + return var_matrix_t(Eigen::MatrixXd(0, 0)); |
| 361 | + return stan::math::to_var_value( |
| 362 | + map_matrix_t(&scalar_ptr_increment(m * n), m, n)); |
| 363 | + } |
| 364 | + |
| 365 | + /** |
| 366 | + * Return a matrix of the specified dimensionality made up of |
| 367 | + * the next scalars arranged in column-major order. |
| 368 | + * |
| 369 | + * Row-major reading means that if a matrix of <code>m=2</code> |
| 370 | + * rows and <code>n=3</code> columns is read and the next |
| 371 | + * scalar values are <code>1,2,3,4,5,6</code>, the result is |
| 372 | + * |
| 373 | + * <pre> |
| 374 | + * a = 1 4 |
| 375 | + * 2 5 |
| 376 | + * 3 6</pre> |
| 377 | + * |
| 378 | + * @param m Number of rows. |
| 379 | + * @param n Number of columns. |
| 380 | + * @return Eigen::Matrix made up of the next scalars. |
| 381 | + */ |
| 382 | + template <typename T_ = T, require_st_arithmetic<T_> * = nullptr> |
| 383 | + inline matrix_t var_matrix(size_t m, size_t n) { |
| 384 | + return this->matrix(m, n); |
| 385 | + } |
| 386 | + |
279 | 387 | /** |
280 | 388 | * Return a matrix of the specified dimensionality made up of |
281 | 389 | * the next scalars arranged in column-major order. The |
|
0 commit comments