1 #ifndef STAN_IO_READER_HPP
2 #define STAN_IO_READER_HPP
4 #include <boost/throw_exception.hpp>
5 #include <stan/math/prim/mat/err/check_cholesky_factor.hpp>
6 #include <stan/math/prim/mat/err/check_cholesky_factor_corr.hpp>
7 #include <stan/math/prim/mat/err/check_corr_matrix.hpp>
8 #include <stan/math/prim/mat/err/check_cov_matrix.hpp>
9 #include <stan/math/prim/mat/err/check_ordered.hpp>
10 #include <stan/math/prim/mat/err/check_positive_ordered.hpp>
11 #include <stan/math/prim/mat/err/check_simplex.hpp>
12 #include <stan/math/prim/mat/err/check_unit_vector.hpp>
13 #include <stan/math/prim/mat/fun/cholesky_corr_constrain.hpp>
14 #include <stan/math/prim/mat/fun/cholesky_factor_constrain.hpp>
15 #include <stan/math/prim/mat/fun/corr_matrix_constrain.hpp>
16 #include <stan/math/prim/mat/fun/cov_matrix_constrain.hpp>
17 #include <stan/math/prim/mat/fun/ordered_constrain.hpp>
18 #include <stan/math/prim/mat/fun/positive_ordered_constrain.hpp>
19 #include <stan/math/prim/mat/fun/simplex_constrain.hpp>
20 #include <stan/math/prim/mat/fun/unit_vector_constrain.hpp>
21 #include <stan/math/prim/scal/err/check_bounded.hpp>
22 #include <stan/math/prim/scal/err/check_greater_or_equal.hpp>
23 #include <stan/math/prim/scal/fun/corr_constrain.hpp>
24 #include <stan/math/prim/scal/fun/lb_constrain.hpp>
25 #include <stan/math/prim/scal/fun/lub_constrain.hpp>
26 #include <stan/math/prim/scal/fun/positive_constrain.hpp>
27 #include <stan/math/prim/scal/fun/prob_constrain.hpp>
28 #include <stan/math/prim/scal/fun/ub_constrain.hpp>
60 std::vector<T>& data_r_;
61 std::vector<int>& data_i_;
65 inline T& scalar_ptr() {
66 return data_r_.at(pos_);
69 inline T& scalar_ptr_increment(
size_t m) {
71 return data_r_.at(pos_ - m);
74 inline int& int_ptr() {
75 return data_i_.at(int_pos_);
78 inline int& int_ptr_increment(
size_t m) {
80 return data_i_.at(int_pos_ - m);
84 typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
matrix_t;
85 typedef Eigen::Matrix<T, Eigen::Dynamic, 1>
vector_t;
105 std::vector<int>& data_i)
123 return data_r_.size() - pos_;
132 return data_i_.size() - int_pos_;
141 if (int_pos_ >= data_i_.size())
142 BOOST_THROW_EXCEPTION(
143 std::runtime_error(
"no more integers to read."));
144 return data_i_[int_pos_++];
177 if (pos_ >= data_r_.size())
178 BOOST_THROW_EXCEPTION(std::runtime_error(
"no more scalars to read"));
179 return data_r_[pos_++];
216 if (m == 0)
return std::vector<T>();
218 T& start = scalar_ptr_increment(m);
219 vec.insert(vec.begin(), &start, &scalar_ptr());
315 inline matrix_t
matrix(
size_t m,
size_t n) {
316 if (m == 0 || n == 0)
332 if (m == 0 || n == 0)
350 if (m == 0 || n == 0)
368 BOOST_THROW_EXCEPTION(
369 std::runtime_error(
"required value greater than or equal to lb"));
411 BOOST_THROW_EXCEPTION(
412 std::runtime_error(
"required value less than or equal to ub"));
457 BOOST_THROW_EXCEPTION(
458 std::runtime_error(
"lower bound must be less than or equal to ub"));
460 BOOST_THROW_EXCEPTION(
461 std::runtime_error(
"required value greater than or equal to lb"));
463 BOOST_THROW_EXCEPTION(
464 std::runtime_error(
"required value less than or equal to ub"));
508 stan::math::check_positive(
"stan::io::scalar_pos",
509 "Constrained scalar", x);
521 return stan::math::positive_constrain(
scalar());
535 return stan::math::positive_constrain(
scalar(), lp);
550 template <
typename TL>
553 stan::math::check_greater_or_equal(
"stan::io::scalar_lb",
554 "Constrained scalar", x, lb);
569 template <
typename TL>
571 return stan::math::lb_constrain(
scalar(), lb);
585 template <
typename TL>
587 return stan::math::lb_constrain(
scalar(), lb, lp);
604 template <
typename TU>
607 stan::math::check_less_or_equal(
"stan::io::scalar_ub",
608 "Constrained scalar", x, ub);
623 template <
typename TU>
625 return stan::math::ub_constrain(
scalar(), ub);
639 template <
typename TU>
641 return stan::math::ub_constrain(
scalar(), ub, lp);
658 template <
typename TL,
typename TU>
661 stan::math::check_bounded<T, TL, TU>
662 (
"stan::io::scalar_lub",
"Constrained scalar", x, lb, ub);
679 template <
typename TL,
typename TU>
681 return stan::math::lub_constrain(
scalar(), lb, ub);
697 template <
typename TL,
typename TU>
699 return stan::math::lub_constrain(
scalar(), lb, ub, lp);
712 stan::math::check_bounded<T, double, double>
713 (
"stan::io::prob",
"Constrained probability", x, 0, 1);
726 return stan::math::prob_constrain(
scalar());
740 return stan::math::prob_constrain(
scalar(), lp);
759 stan::math::check_bounded<T, double, double>
760 (
"stan::io::corr",
"Correlation value", x, -1, 1);
773 return stan::math::corr_constrain(
scalar());
788 return stan::math::corr_constrain(
scalar(), lp);
802 vector_t theta(
vector(k));
803 stan::math::check_unit_vector(
"stan::io::unit_vector",
804 "Constrained vector", theta);
820 return stan::math::unit_vector_constrain(
vector(k));
836 return stan::math::unit_vector_constrain(
vector(k), lp);
850 vector_t theta(
vector(k));
851 stan::math::check_simplex(
"stan::io::simplex",
852 "Constrained vector", theta);
868 return stan::math::simplex_constrain(
vector(k-1));
884 return stan::math::simplex_constrain(
vector(k-1), lp);
899 stan::math::check_ordered(
"stan::io::ordered",
"Constrained vector", x);
913 return stan::math::ordered_constrain(
vector(k));
928 return stan::math::ordered_constrain(
vector(k), lp);
943 stan::math::check_positive_ordered(
"stan::io::positive_ordered",
944 "Constrained vector", x);
958 return stan::math::positive_ordered_constrain(
vector(k));
973 return stan::math::positive_ordered_constrain(
vector(k), lp);
990 stan::math::check_cholesky_factor(
"stan::io::cholesky_factor",
991 "Constrained matrix", y);
1007 return stan::math::cholesky_factor_constrain
1008 (
vector((N * (N + 1)) / 2 + (M - N) * N), M, N);
1025 return stan::math::cholesky_factor_constrain
1026 (
vector((N * (N + 1)) / 2 + (M - N) * N), M, N, lp);
1042 using stan::math::check_cholesky_factor_corr;
1043 matrix_t y(
matrix(K, K));
1044 check_cholesky_factor_corr(
"stan::io::cholesky_factor_corr",
1045 "Constrained matrix", y);
1060 return stan::math::cholesky_corr_constrain(
vector((K * (K - 1)) / 2),
1078 return stan::math::cholesky_corr_constrain(
vector((K * (K - 1)) / 2),
1096 matrix_t y(
matrix(k, k));
1097 stan::math::check_cov_matrix(
"stan::io::cov_matrix",
1098 "Constrained matrix", y);
1111 return stan::math::cov_matrix_constrain(
vector(k + (k * (k - 1)) / 2),
1127 return stan::math::cov_matrix_constrain(
vector(k + (k * (k - 1)) / 2),
1142 matrix_t x(
matrix(k, k));
1143 stan::math::check_corr_matrix(
"stan::math::corr_matrix",
1144 "Constrained matrix", x);
1157 return stan::math::corr_matrix_constrain(
vector((k * (k - 1)) / 2), k);
1172 return stan::math::corr_matrix_constrain(
vector((k * (k - 1)) / 2),
1176 template <
typename TL>
1179 for (
size_t i = 0; i < m; ++i)
1184 template <
typename TL>
1187 for (
size_t i = 0; i < m; ++i)
1192 template <
typename TL>
1195 for (
size_t i = 0; i < m; ++i)
1200 template <
typename TL>
1203 for (
size_t i = 0; i < m; ++i)
1208 template <
typename TL>
1211 for (
size_t i = 0; i < m; ++i)
1216 template <
typename TL>
1220 for (
size_t i = 0; i < m; ++i)
1225 template <
typename TL>
1226 inline matrix_t
matrix_lb(
const TL lb,
size_t m,
size_t n) {
1228 for (
size_t j = 0; j < n; ++j)
1229 for (
size_t i = 0; i < m; ++i)
1234 template <
typename TL>
1237 for (
size_t j = 0; j < n; ++j)
1238 for (
size_t i = 0; i < m; ++i)
1243 template <
typename TL>
1247 for (
size_t j = 0; j < n; ++j)
1248 for (
size_t i = 0; i < m; ++i)
1253 template <
typename TU>
1256 for (
size_t i = 0; i < m; ++i)
1261 template <
typename TU>
1264 for (
size_t i = 0; i < m; ++i)
1269 template <
typename TU>
1272 for (
size_t i = 0; i < m; ++i)
1277 template <
typename TU>
1280 for (
size_t i = 0; i < m; ++i)
1285 template <
typename TU>
1288 for (
size_t i = 0; i < m; ++i)
1293 template <
typename TU>
1297 for (
size_t i = 0; i < m; ++i)
1302 template <
typename TU>
1303 inline matrix_t
matrix_ub(
const TU ub,
size_t m,
size_t n) {
1305 for (
size_t j = 0; j < n; ++j)
1306 for (
size_t i = 0; i < m; ++i)
1311 template <
typename TU>
1314 for (
size_t j = 0; j < n; ++j)
1315 for (
size_t i = 0; i < m; ++i)
1320 template <
typename TU>
1324 for (
size_t j = 0; j < n; ++j)
1325 for (
size_t i = 0; i < m; ++i)
1330 template <
typename TL,
typename TU>
1333 for (
size_t i = 0; i < m; ++i)
1338 template <
typename TL,
typename TU>
1342 for (
size_t i = 0; i < m; ++i)
1347 template <
typename TL,
typename TU>
1351 for (
size_t i = 0; i < m; ++i)
1356 template <
typename TL,
typename TU>
1359 for (
size_t i = 0; i < m; ++i)
1363 template <
typename TL,
typename TU>
1367 for (
size_t i = 0; i < m; ++i)
1372 template <
typename TL,
typename TU>
1376 for (
size_t i = 0; i < m; ++i)
1381 template <
typename TL,
typename TU>
1382 inline matrix_t
matrix_lub(
const TL lb,
const TU ub,
size_t m,
size_t n) {
1384 for (
size_t j = 0; j < n; ++j)
1385 for (
size_t i = 0; i < m; ++i)
1390 template <
typename TL,
typename TU>
1394 for (
size_t j = 0; j < n; ++j)
1395 for (
size_t i = 0; i < m; ++i)
1400 template <
typename TL,
typename TU>
1405 for (
size_t j = 0; j < n; ++j)
1406 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.