1 #ifndef STAN__IO__READER_HPP
2 #define STAN__IO__READER_HPP
4 #include <boost/throw_exception.hpp>
38 std::vector<T>& data_r_;
39 std::vector<int>& data_i_;
43 inline T& scalar_ptr() {
44 return data_r_.at(pos_);
47 inline T& scalar_ptr_increment(
size_t m) {
49 return data_r_.at(pos_ - m);
52 inline int& int_ptr() {
53 return data_i_.at(int_pos_);
56 inline int& int_ptr_increment(
size_t m) {
58 return data_i_.at(int_pos_ - m);
63 typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
matrix_t;
64 typedef Eigen::Matrix<T,Eigen::Dynamic,1>
vector_t;
84 std::vector<int>& data_i)
102 return data_r_.size() - pos_;
111 return data_i_.size() - int_pos_;
120 if (int_pos_ >= data_i_.size())
121 BOOST_THROW_EXCEPTION(
122 std::runtime_error(
"no more integers to read."));
123 return data_i_[int_pos_++];
156 if (pos_ >= data_r_.size())
157 BOOST_THROW_EXCEPTION(std::runtime_error(
"no more scalars to read"));
158 return data_r_[pos_++];
196 T& start = scalar_ptr_increment(m);
197 vec.insert(vec.begin(), &start, &scalar_ptr());
334 BOOST_THROW_EXCEPTION(
335 std::runtime_error(
"required value greater than or equal to lb"));
377 BOOST_THROW_EXCEPTION(
378 std::runtime_error(
"required value less than or equal to ub"));
423 BOOST_THROW_EXCEPTION(
424 std::runtime_error(
"lower bound must be less than or equal to ub"));
426 BOOST_THROW_EXCEPTION(
427 std::runtime_error(
"required value greater than or equal to lb"));
429 BOOST_THROW_EXCEPTION(
430 std::runtime_error(
"required value less than or equal to ub"));
475 "Constrained scalar", (
double*)0);
516 template <
typename TL>
520 x, lb,
"Constrained scalar",
536 template <
typename TL>
552 template <
typename TL>
571 template <
typename TU>
575 "Constrained scalar", (
double*)0);
590 template <
typename TU>
606 template <
typename TU>
625 template <
typename TL,
typename TU>
628 stan::math::check_bounded<T,TL,TU,T>
629 (
"stan::io::scalar_lub(%1%)", x, lb, ub,
"Constrained scalar",0);
646 template <
typename TL,
typename TU>
664 template <
typename TL,
typename TU>
679 stan::math::check_bounded<T,double,double,double>
680 (
"stan::io::prob(%1%)", x, 0, 1,
"Constrained probability", 0);
726 stan::math::check_bounded<T,double,double,double>
727 (
"stan::io::corr(%1%)", x, -1, 1,
"Correlation value",0);
911 x,
"Constrained vector",
959 y,
"Constrained matrix",
1110 x,
"Constrained matrix",
1143 template <
typename TL>
1146 for (
size_t i = 0; i < m; ++i)
1150 template <
typename TL>
1153 for (
size_t i = 0; i < m; ++i)
1157 template <
typename TL>
1160 for (
size_t i = 0; i < m; ++i)
1165 template <
typename TL>
1168 for (
size_t i = 0; i < m; ++i)
1172 template <
typename TL>
1175 for (
size_t i = 0; i < m; ++i)
1179 template <
typename TL>
1182 for (
size_t i = 0; i < m; ++i)
1187 template <
typename TL>
1190 for (
size_t j = 0; j < n; ++j)
1191 for (
size_t i = 0; i < m; ++i)
1195 template <
typename TL>
1198 for (
size_t j = 0; j < n; ++j)
1199 for (
size_t i = 0; i < m; ++i)
1203 template <
typename TL>
1206 for (
size_t j = 0; j < n; ++j)
1207 for (
size_t i = 0; i < m; ++i)
1212 template <
typename TU>
1215 for (
size_t i = 0; i < m; ++i)
1219 template <
typename TU>
1222 for (
size_t i = 0; i < m; ++i)
1226 template <
typename TU>
1229 for (
size_t i = 0; i < m; ++i)
1234 template <
typename TU>
1237 for (
size_t i = 0; i < m; ++i)
1241 template <
typename TU>
1244 for (
size_t i = 0; i < m; ++i)
1248 template <
typename TU>
1251 for (
size_t i = 0; i < m; ++i)
1256 template <
typename TU>
1259 for (
size_t j = 0; j < n; ++j)
1260 for (
size_t i = 0; i < m; ++i)
1264 template <
typename TU>
1267 for (
size_t j = 0; j < n; ++j)
1268 for (
size_t i = 0; i < m; ++i)
1272 template <
typename TU>
1275 for (
size_t j = 0; j < n; ++j)
1276 for (
size_t i = 0; i < m; ++i)
1282 template <
typename TL,
typename TU>
1285 for (
size_t i = 0; i < m; ++i)
1289 template <
typename TL,
typename TU>
1292 for (
size_t i = 0; i < m; ++i)
1296 template <
typename TL,
typename TU>
1299 for (
size_t i = 0; i < m; ++i)
1304 template <
typename TL,
typename TU>
1307 for (
size_t i = 0; i < m; ++i)
1311 template <
typename TL,
typename TU>
1314 for (
size_t i = 0; i < m; ++i)
1318 template <
typename TL,
typename TU>
1321 for (
size_t i = 0; i < m; ++i)
1326 template <
typename TL,
typename TU>
1329 for (
size_t j = 0; j < n; ++j)
1330 for (
size_t i = 0; i < m; ++i)
1334 template <
typename TL,
typename TU>
1337 for (
size_t j = 0; j < n; ++j)
1338 for (
size_t i = 0; i < m; ++i)
1342 template <
typename TL,
typename TU>
1345 for (
size_t j = 0; j < n; ++j)
1346 for (
size_t i = 0; i < m; ++i)
bool check_cov_matrix(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is a valid covariance matrix.
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...
bool check_ordered(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, 1 > &y, const char *name, T_result *result)
Return true if the specified vector is sorted into increasing order.
T corr_constrain(const T x)
Return the result of transforming the specified scalar to have a valid correlation value between -1 a...
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...
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.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > cholesky_factor_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, int M, int N)
Return the Cholesky factor of the specified size read from the specified vector.
vector_t vector_lub_constrain(const TL lb, const TU ub, size_t m, T &lp)
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.
Eigen::Matrix< T, Eigen::Dynamic, 1 > ordered_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x)
Return an increasing ordered vector derived from the specified free vector.
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.
bool check_simplex(const char *function, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const char *name, T_result *result)
Return true if the specified vector is simplex.
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.
Eigen::Matrix< T, Eigen::Dynamic, 1 > simplex_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &y)
Return the simplex corresponding to the specified free vector.
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, Eigen::Dynamic, 1 > 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.
T prob_constrain(const T x)
Return a probability value constrained to fall between 0 and 1 (inclusive) for the specified free sca...
bool check_unit_vector(const char *function, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const char *name, T_result *result)
Return true if the specified vector is unit vector.
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.
boost::math::tools::promote_args< T, TU >::type ub_constrain(const T x, const TU ub)
Return the upper-bounded value for the specified unconstrained scalar and upper bound.
row_vector_t row_vector_lb_constrain(const TL lb, size_t m)
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix_t
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...
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...
T lb_constrain(const T x, const TL lb)
Return the lower-bounded value for the specified unconstrained input and specified lower 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 ...
bool check_cholesky_factor(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is a valid Cholesky factor.
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 > positive_ordered_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x)
Return an increasing positive ordered vector derived from the specified free vector.
Eigen::Matrix< T, Eigen::Dynamic, 1 > unit_vector_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &y)
Return the unit length vector corresponding to the free vector y.
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...
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result)
bool check_corr_matrix(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is a valid correlation matrix.
vector_t vector_lb_constrain(const TL lb, size_t m)
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > cov_matrix_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, typename math::index_type< Eigen::Matrix< T, Eigen::Dynamic, 1 > >::type K)
Return the symmetric, positive-definite matrix of dimensions K by K resulting from transforming the s...
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, Eigen::Dynamic > corr_matrix_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, typename math::index_type< Eigen::Matrix< T, Eigen::Dynamic, 1 > >::type k)
Return the correlation matrix of the specified dimensionality derived from the specified vector of un...
~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.
Eigen::Matrix< T, 1, Eigen::Dynamic > row_vector_t
bool check_less_or_equal(const char *function, const T_y &y, const T_high &high, const char *name, T_result *result)
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)
boost::math::tools::promote_args< T, TL, TU >::type lub_constrain(const T x, TL lb, TU ub)
Return the lower- and upper-bounded scalar derived by transforming the specified free scalar given th...
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)
bool check_positive_ordered(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, 1 > &y, const char *name, T_result *result)
Return true if the specified vector contains only non-negative values and is sorted into increasing o...
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.
Eigen::Matrix< T, Eigen::Dynamic, 1 > unit_vector_constrain(size_t k)
Return the next unit_vector transformed vector of the specified length.
T positive_constrain(const T x)
Return the positive value for the specified unconstrained input.
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.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > cholesky_corr_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &y, int K)
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)
bool check_greater_or_equal(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result)
vector_t vector_ub_constrain(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.
bool check_cholesky_factor_corr(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is a valid Cholesky factor.
vector_t vector_constrain(size_t m, T &)
Return a column vector of specified dimensionality made up of the next scalars.