1 #ifndef STAN_IO_READER_HPP 2 #define STAN_IO_READER_HPP 4 #include <boost/throw_exception.hpp> 5 #include <stan/math/prim/mat.hpp> 37 std::vector<T>& data_r_;
38 std::vector<int>& data_i_;
42 inline T& scalar_ptr() {
43 return data_r_.at(pos_);
46 inline T& scalar_ptr_increment(
size_t m) {
48 return data_r_.at(pos_ - m);
51 inline int& int_ptr() {
52 return data_i_.at(int_pos_);
55 inline int& int_ptr_increment(
size_t m) {
57 return data_i_.at(int_pos_ - m);
61 typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
matrix_t;
62 typedef Eigen::Matrix<T, Eigen::Dynamic, 1>
vector_t;
82 std::vector<int>& data_i)
100 return data_r_.size() - pos_;
109 return data_i_.size() - int_pos_;
118 if (int_pos_ >= data_i_.size())
119 BOOST_THROW_EXCEPTION(
120 std::runtime_error(
"no more integers to read."));
121 return data_i_[int_pos_++];
154 if (pos_ >= data_r_.size())
155 BOOST_THROW_EXCEPTION(std::runtime_error(
"no more scalars to read"));
156 return data_r_[pos_++];
193 if (m == 0)
return std::vector<T>();
195 T& start = scalar_ptr_increment(m);
196 vec.insert(vec.begin(), &start, &scalar_ptr());
292 inline matrix_t
matrix(
size_t m,
size_t n) {
293 if (m == 0 || n == 0)
309 if (m == 0 || n == 0)
327 if (m == 0 || n == 0)
345 BOOST_THROW_EXCEPTION(
346 std::runtime_error(
"required value greater than or equal to lb"));
388 BOOST_THROW_EXCEPTION(
389 std::runtime_error(
"required value less than or equal to ub"));
434 BOOST_THROW_EXCEPTION(
435 std::runtime_error(
"lower bound must be less than or equal to ub"));
437 BOOST_THROW_EXCEPTION(
438 std::runtime_error(
"required value greater than or equal to lb"));
440 BOOST_THROW_EXCEPTION(
441 std::runtime_error(
"required value less than or equal to ub"));
485 stan::math::check_positive(
"stan::io::scalar_pos",
486 "Constrained scalar", x);
498 return stan::math::positive_constrain(
scalar());
512 return stan::math::positive_constrain(
scalar(), lp);
527 template <
typename TL>
530 stan::math::check_greater_or_equal(
"stan::io::scalar_lb",
531 "Constrained scalar", x, lb);
546 template <
typename TL>
548 return stan::math::lb_constrain(
scalar(), lb);
562 template <
typename TL>
564 return stan::math::lb_constrain(
scalar(), lb, lp);
581 template <
typename TU>
584 stan::math::check_less_or_equal(
"stan::io::scalar_ub",
585 "Constrained scalar", x, ub);
600 template <
typename TU>
602 return stan::math::ub_constrain(
scalar(), ub);
616 template <
typename TU>
618 return stan::math::ub_constrain(
scalar(), ub, lp);
635 template <
typename TL,
typename TU>
638 stan::math::check_bounded<T, TL, TU>
639 (
"stan::io::scalar_lub",
"Constrained scalar", x, lb, ub);
656 template <
typename TL,
typename TU>
658 return stan::math::lub_constrain(
scalar(), lb, ub);
674 template <
typename TL,
typename TU>
676 return stan::math::lub_constrain(
scalar(), lb, ub, lp);
689 stan::math::check_bounded<T, double, double>
690 (
"stan::io::prob",
"Constrained probability", x, 0, 1);
703 return stan::math::prob_constrain(
scalar());
717 return stan::math::prob_constrain(
scalar(), lp);
736 stan::math::check_bounded<T, double, double>
737 (
"stan::io::corr",
"Correlation value", x, -1, 1);
750 return stan::math::corr_constrain(
scalar());
765 return stan::math::corr_constrain(
scalar(), lp);
779 vector_t theta(
vector(k));
780 stan::math::check_unit_vector(
"stan::io::unit_vector",
781 "Constrained vector", theta);
797 return stan::math::unit_vector_constrain(
vector(k));
813 return stan::math::unit_vector_constrain(
vector(k), lp);
827 vector_t theta(
vector(k));
828 stan::math::check_simplex(
"stan::io::simplex",
829 "Constrained vector", theta);
845 return stan::math::simplex_constrain(
vector(k-1));
861 return stan::math::simplex_constrain(
vector(k-1), lp);
876 stan::math::check_ordered(
"stan::io::ordered",
"Constrained vector", x);
890 return stan::math::ordered_constrain(
vector(k));
905 return stan::math::ordered_constrain(
vector(k), lp);
920 stan::math::check_positive_ordered(
"stan::io::positive_ordered",
921 "Constrained vector", x);
935 return stan::math::positive_ordered_constrain(
vector(k));
950 return stan::math::positive_ordered_constrain(
vector(k), lp);
967 stan::math::check_cholesky_factor(
"stan::io::cholesky_factor",
968 "Constrained matrix", y);
984 return stan::math::cholesky_factor_constrain
985 (
vector((N * (N + 1)) / 2 + (M - N) * N), M, N);
1002 return stan::math::cholesky_factor_constrain
1003 (
vector((N * (N + 1)) / 2 + (M - N) * N), M, N, lp);
1019 using stan::math::check_cholesky_factor_corr;
1020 matrix_t y(
matrix(K, K));
1021 check_cholesky_factor_corr(
"stan::io::cholesky_factor_corr",
1022 "Constrained matrix", y);
1037 return stan::math::cholesky_corr_constrain(
vector((K * (K - 1)) / 2),
1055 return stan::math::cholesky_corr_constrain(
vector((K * (K - 1)) / 2),
1073 matrix_t y(
matrix(k, k));
1074 stan::math::check_cov_matrix(
"stan::io::cov_matrix",
1075 "Constrained matrix", y);
1088 return stan::math::cov_matrix_constrain(
vector(k + (k * (k - 1)) / 2),
1104 return stan::math::cov_matrix_constrain(
vector(k + (k * (k - 1)) / 2),
1119 matrix_t x(
matrix(k, k));
1120 stan::math::check_corr_matrix(
"stan::math::corr_matrix",
1121 "Constrained matrix", x);
1134 return stan::math::corr_matrix_constrain(
vector((k * (k - 1)) / 2), k);
1149 return stan::math::corr_matrix_constrain(
vector((k * (k - 1)) / 2),
1153 template <
typename TL>
1156 for (
size_t i = 0; i < m; ++i)
1161 template <
typename TL>
1164 for (
size_t i = 0; i < m; ++i)
1169 template <
typename TL>
1172 for (
size_t i = 0; i < m; ++i)
1177 template <
typename TL>
1180 for (
size_t i = 0; i < m; ++i)
1185 template <
typename TL>
1188 for (
size_t i = 0; i < m; ++i)
1193 template <
typename TL>
1197 for (
size_t i = 0; i < m; ++i)
1202 template <
typename TL>
1203 inline matrix_t
matrix_lb(
const TL lb,
size_t m,
size_t n) {
1205 for (
size_t j = 0; j < n; ++j)
1206 for (
size_t i = 0; i < m; ++i)
1211 template <
typename TL>
1214 for (
size_t j = 0; j < n; ++j)
1215 for (
size_t i = 0; i < m; ++i)
1220 template <
typename TL>
1224 for (
size_t j = 0; j < n; ++j)
1225 for (
size_t i = 0; i < m; ++i)
1230 template <
typename TU>
1233 for (
size_t i = 0; i < m; ++i)
1238 template <
typename TU>
1241 for (
size_t i = 0; i < m; ++i)
1246 template <
typename TU>
1249 for (
size_t i = 0; i < m; ++i)
1254 template <
typename TU>
1257 for (
size_t i = 0; i < m; ++i)
1262 template <
typename TU>
1265 for (
size_t i = 0; i < m; ++i)
1270 template <
typename TU>
1274 for (
size_t i = 0; i < m; ++i)
1279 template <
typename TU>
1280 inline matrix_t
matrix_ub(
const TU ub,
size_t m,
size_t n) {
1282 for (
size_t j = 0; j < n; ++j)
1283 for (
size_t i = 0; i < m; ++i)
1288 template <
typename TU>
1291 for (
size_t j = 0; j < n; ++j)
1292 for (
size_t i = 0; i < m; ++i)
1297 template <
typename TU>
1301 for (
size_t j = 0; j < n; ++j)
1302 for (
size_t i = 0; i < m; ++i)
1307 template <
typename TL,
typename TU>
1310 for (
size_t i = 0; i < m; ++i)
1315 template <
typename TL,
typename TU>
1319 for (
size_t i = 0; i < m; ++i)
1324 template <
typename TL,
typename TU>
1328 for (
size_t i = 0; i < m; ++i)
1333 template <
typename TL,
typename TU>
1336 for (
size_t i = 0; i < m; ++i)
1340 template <
typename TL,
typename TU>
1344 for (
size_t i = 0; i < m; ++i)
1349 template <
typename TL,
typename TU>
1353 for (
size_t i = 0; i < m; ++i)
1358 template <
typename TL,
typename TU>
1359 inline matrix_t
matrix_lub(
const TL lb,
const TU ub,
size_t m,
size_t n) {
1361 for (
size_t j = 0; j < n; ++j)
1362 for (
size_t i = 0; i < m; ++i)
1367 template <
typename TL,
typename TU>
1371 for (
size_t j = 0; j < n; ++j)
1372 for (
size_t i = 0; i < m; ++i)
1377 template <
typename TL,
typename TU>
1382 for (
size_t j = 0; j < n; ++j)
1383 for (
size_t i = 0; i < m; ++i)
matrix_t cholesky_corr_constrain(size_t K, T &lp)
Return the next Cholesky factor for a correlation matrix with the specified dimensionality, reading from an unconstrained vector of the appropriate size, and increment the log probability reference with the log Jacobian adjustment for the transform.
vector_t positive_ordered_constrain(size_t k)
Return the next positive ordered vector of the specified length.
row_vector_t row_vector_ub_constrain(const TU ub, size_t m)
vector_t simplex_constrain(size_t k, T &lp)
Return the next simplex of the specified size (using one fewer unconstrained scalars), incrementing the specified reference with the log absolute Jacobian determinant.
T scalar_lub_constrain(const TL lb, const TU ub)
Return the next scalar transformed to be between the specified lower and upper bounds.
matrix_t matrix_lub(const TL lb, const TU ub, size_t m, size_t n)
T scalar_ub(TU ub)
Return the next scalar, checking that it is less than or equal to the specified upper bound...
T scalar_pos()
Return the next scalar, checking that it is positive.
vector_t unit_vector_constrain(size_t k, T &lp)
Return the next unit_vector of the specified size (using one fewer unconstrained scalars), incrementing the specified reference with the log absolute Jacobian determinant.
int integer_ub_constrain(int ub)
Return the next integer, checking that it is less than or equal to the specified upper bound...
matrix_t matrix_ub(const TU ub, size_t m, size_t n)
matrix_t matrix_lub_constrain(const TL lb, const TU ub, size_t m, size_t n)
std::vector< T > std_vector(size_t m)
Return a standard library vector of the specified dimensionality made up of the next scalars...
T scalar_lb(const TL lb)
Return the next scalar, checking that it is greater than or equal to the specified lower bound...
Eigen::Matrix< T, Eigen::Dynamic, 1 > vector_t
row_vector_t row_vector_constrain(size_t m)
Return a row vector of specified dimensionality made up of the next scalars.
int integer_ub_constrain(int ub, T &)
Return the next integer, checking that it is less than or equal to the specified upper bound...
T scalar_constrain()
Return the next scalar.
vector_t vector_lub_constrain(const TL lb, const TU ub, size_t m, T &lp)
Probability, optimization and sampling library.
Eigen::Map< row_vector_t > map_row_vector_t
int integer_lb_constrain(int lb, T &)
Return the next integer, checking that it is greater than or equal to the specified lower bound...
vector_t vector_constrain(size_t m)
Return a column vector of specified dimensionality made up of the next scalars.
row_vector_t row_vector_ub(const TU ub, size_t m)
matrix_t cov_matrix(size_t k)
Return the next covariance matrix with the specified dimensionality.
matrix_t matrix_lb_constrain(const TL lb, size_t m, size_t n, T &lp)
T corr_constrain()
Return the next scalar transformed to be a correlation between -1 and 1.
T scalar_constrain(T &)
Return the next scalar in the sequence, incrementing the specified reference with the log absolute Ja...
matrix_t cov_matrix_constrain(size_t k)
Return the next covariance matrix of the specified dimensionality.
T prob_constrain(T &lp)
Return the next scalar transformed to be a probability between 0 and 1, incrementing the specified re...
matrix_t corr_matrix_constrain(size_t k, T &lp)
Return the next correlation matrix of the specified dimensionality, incrementing the specified refere...
int integer_constrain(T &)
Return the next integer in the integer sequence.
T corr()
Return the next scalar, checking that it is a valid value for a correlation, between -1 (inclusive) a...
int integer_ub(int ub)
Return the next integer, checking that it is less than or equal to the specified upper bound...
int integer_lb_constrain(int lb)
Return the next integer, checking that it is greater than or equal to the specified lower bound...
T scalar_lb_constrain(const TL lb)
Return the next scalar transformed to have the specified lower bound.
matrix_t cholesky_corr(size_t K)
Return the next Cholesky factor for a correlation matrix with the specified dimensionality, reading it directly without transforms.
T scalar_pos_constrain()
Return the next scalar, transformed to be positive.
Eigen::Matrix< T, 1, Eigen::Dynamic > row_vector_t
vector_t vector_ub(const TU ub, size_t m)
row_vector_t row_vector(size_t m)
Return a row vector of specified dimensionality made up of the next scalars.
vector_t vector(size_t m)
Return a column vector of specified dimensionality made up of the next scalars.
vector_t vector_lub(const TL lb, const TU ub, size_t m)
row_vector_t row_vector_constrain(size_t m, T &)
Return a row vector of specified dimensionality made up of the next scalars.
matrix_t matrix_constrain(size_t m, size_t n)
Return a matrix of the specified dimensionality made up of the next scalars arranged in column-major ...
vector_t unit_vector(size_t k)
Return a unit_vector of the specified size made up of the next scalars.
row_vector_t row_vector_lb_constrain(const TL lb, size_t m)
vector_t vector_ub_constrain(const TU ub, size_t m, T &lp)
Eigen::Map< vector_t > map_vector_t
matrix_t cholesky_factor(size_t M, size_t N)
Return the next Cholesky factor with the specified dimensionality, reading it directly without transf...
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix_t
vector_t ordered(size_t k)
Return the next vector of specified size containing values in ascending order.
A stream-based reader for integer, scalar, vector, matrix and array data types, with Jacobian calcula...
reader(std::vector< T > &data_r, std::vector< int > &data_i)
Construct a variable reader using the specified vectors as the source of scalar and integer values fo...
T scalar_ub_constrain(const TU ub)
Return the next scalar transformed to have the specified upper bound.
matrix_t matrix_lub_constrain(const TL lb, const TU ub, size_t m, size_t n, T &lp)
int integer_lub(int lb, int ub)
Return the next integer, checking that it is less than or equal to the specified upper bound...
matrix_t matrix(size_t m, size_t n)
Return a matrix of the specified dimensionality made up of the next scalars arranged in column-major ...
T corr_constrain(T &lp)
Return the next scalar transformed to be a (partial) correlation between -1 and 1, incrementing the specified reference with the log of the absolute Jacobian determinant.
row_vector_t row_vector_lub_constrain(const TL lb, const TU ub, size_t m, T &lp)
T scalar()
Return the next scalar in the sequence.
T scalar_pos_constrain(T &lp)
Return the next scalar transformed to be positive, incrementing the specified reference with the log ...
vector_t ordered_constrain(size_t k)
Return the next ordered vector of the specified length.
matrix_t matrix_lb(const TL lb, size_t m, size_t n)
int integer_lub_constrain(int lb, int ub)
Return the next integer, checking that it is less than or equal to the specified upper bound...
matrix_t cholesky_factor_constrain(size_t M, size_t N, T &lp)
Return the next Cholesky factor with the specified dimensionality, reading from an unconstrained vect...
T prob_constrain()
Return the next scalar transformed to be a probability between 0 and 1.
vector_t simplex(size_t k)
Return a simplex of the specified size made up of the next scalars.
Eigen::Matrix< T, Eigen::Dynamic, 1 > unit_vector_constrain(size_t k)
Return the next unit_vector transformed vector of the specified length.
matrix_t corr_matrix(size_t k)
Returns the next correlation matrix of the specified dimensionality.
vector_t vector_lb(const TL lb, size_t m)
T scalar_lb_constrain(const TL lb, T &lp)
Return the next scalar transformed to have the specified lower bound, incrementing the specified refe...
vector_t vector_lb_constrain(const TL lb, size_t m)
vector_t positive_ordered(size_t k)
Return the next vector of specified size containing positive values in ascending order.
size_t available()
Return the number of scalars remaining to be read.
matrix_t cov_matrix_constrain(size_t k, T &lp)
Return the next covariance matrix of the specified dimensionality, incrementing the specified referen...
row_vector_t row_vector_lub_constrain(const TL lb, const TU ub, size_t m)
Eigen::Matrix< T, Eigen::Dynamic, 1 > simplex_constrain(size_t k)
Return the next simplex transformed vector of the specified length.
~reader()
Destroy this variable reader.
row_vector_t row_vector_lub(const TL lb, const TU ub, size_t m)
int integer()
Return the next integer in the integer sequence.
matrix_t matrix_ub_constrain(const TU ub, size_t m, size_t n, T &lp)
matrix_t matrix_ub_constrain(const TU ub, size_t m, size_t n)
matrix_t cholesky_factor_constrain(size_t M, size_t N)
Return the next Cholesky factor with the specified dimensionality, reading from an unconstrained vect...
int integer_constrain()
Return the next integer in the integer sequence.
size_t available_i()
Return the number of integers remaining to be read.
row_vector_t row_vector_lb(const TL lb, size_t m)
vector_t vector_lub_constrain(const TL lb, const TU ub, size_t m)
Eigen::Map< matrix_t > map_matrix_t
int integer_lb(int lb)
Return the next integer, checking that it is greater than or equal to the specified lower bound...
int integer_lub_constrain(int lb, int ub, T &)
Return the next integer, checking that it is less than or equal to the specified upper bound...
T scalar_ub_constrain(const TU ub, T &lp)
Return the next scalar transformed to have the specified upper bound, incrementing the specified refe...
T scalar_lub_constrain(TL lb, TU ub, T &lp)
Return the next scalar transformed to be between the the specified lower and upper bounds...
matrix_t corr_matrix_constrain(size_t k)
Return the next correlation matrix of the specified dimensionality.
matrix_t matrix_constrain(size_t m, size_t n, T &)
Return a matrix of the specified dimensionality made up of the next scalars arranged in column-major ...
vector_t vector_lb_constrain(const TL lb, size_t m, T &lp)
vector_t ordered_constrain(size_t k, T &lp)
Return the next ordered vector of the specified size, incrementing the specified reference with the l...
matrix_t cholesky_corr_constrain(size_t K)
Return the next Cholesky factor for a correlation matrix with the specified dimensionality, reading from an unconstrained vector of the appropriate size.
T prob()
Return the next scalar, checking that it is a valid value for a probability, between 0 (inclusive) an...
vector_t positive_ordered_constrain(size_t k, T &lp)
Return the next positive_ordered vector of the specified size, incrementing the specified reference w...
row_vector_t row_vector_ub_constrain(const TU ub, size_t m, T &lp)
T scalar_lub(const TL lb, const TU ub)
Return the next scalar, checking that it is between the specified lower and upper bound...
row_vector_t row_vector_lb_constrain(const TL lb, size_t m, T &lp)
matrix_t matrix_lb_constrain(const TL lb, size_t m, size_t n)
vector_t vector_ub_constrain(const TU ub, size_t m)
vector_t vector_constrain(size_t m, T &)
Return a column vector of specified dimensionality made up of the next scalars.