1 #ifndef STAN_IO_WRITER_HPP 2 #define STAN_IO_WRITER_HPP 4 #include <stan/math/prim/mat.hpp> 27 std::vector<T> data_r_;
28 std::vector<int> data_i_;
31 typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
matrix_t;
32 typedef Eigen::Matrix<T, Eigen::Dynamic, 1>
vector_t;
54 CONSTRAINT_TOLERANCE(1E-8) {
102 data_r_.push_back(y);
118 BOOST_THROW_EXCEPTION(std::runtime_error(
"y is negative"));
119 data_r_.push_back(log(y));
134 data_r_.push_back(stan::math::lb_free(y, lb));
148 data_r_.push_back(stan::math::ub_free(y, ub));
164 data_r_.push_back(stan::math::lub_free(y, lb, ub));
178 data_r_.push_back(stan::math::corr_free(y));
193 data_r_.push_back(stan::math::prob_free(y));
212 typedef typename stan::math::index_type<vector_t>::type idx_t;
213 if (y.size() == 0)
return;
214 stan::math::check_ordered(
"stan::io::ordered_unconstrain",
"Vector", y);
215 data_r_.push_back(y[0]);
216 for (idx_t i = 1; i < y.size(); ++i) {
217 data_r_.push_back(log(y[i] - y[i-1]));
238 typedef typename stan::math::index_type<vector_t>::type idx_t;
241 if (y.size() == 0)
return;
242 stan::math::check_positive_ordered
243 (
"stan::io::positive_ordered_unconstrain",
"Vector", y);
244 data_r_.push_back(log(y[0]));
245 for (idx_t i = 1; i < y.size(); ++i) {
246 data_r_.push_back(log(y[i] - y[i-1]));
257 typedef typename stan::math::index_type<vector_t>::type idx_t;
258 for (idx_t i = 0; i < y.size(); ++i)
259 data_r_.push_back(y[i]);
268 typedef typename stan::math::index_type<vector_t>::type idx_t;
269 for (idx_t i = 0; i < y.size(); ++i)
270 data_r_.push_back(y[i]);
279 typedef typename stan::math::index_type<matrix_t>::type idx_t;
280 for (idx_t j = 0; j < y.cols(); ++j)
281 for (idx_t i = 0; i < y.rows(); ++i)
282 data_r_.push_back(y(i, j));
286 typedef typename stan::math::index_type<vector_t>::type idx_t;
287 for (idx_t i = 0; i < y.size(); ++i)
291 typedef typename stan::math::index_type<row_vector_t>::type idx_t;
292 for (idx_t i = 0; i < y.size(); ++i)
296 typedef typename stan::math::index_type<matrix_t>::type idx_t;
297 for (idx_t j = 0; j < y.cols(); ++j)
298 for (idx_t i = 0; i < y.rows(); ++i)
303 typedef typename stan::math::index_type<vector_t>::type idx_t;
304 for (idx_t i = 0; i < y.size(); ++i)
308 typedef typename stan::math::index_type<row_vector_t>::type idx_t;
309 for (idx_t i = 0; i < y.size(); ++i)
313 typedef typename stan::math::index_type<matrix_t>::type idx_t;
314 for (idx_t j = 0; j < y.cols(); ++j)
315 for (idx_t i = 0; i < y.rows(); ++i)
321 typedef typename stan::math::index_type<vector_t>::type idx_t;
322 for (idx_t i = 0; i < y.size(); ++i)
326 typedef typename stan::math::index_type<row_vector_t>::type idx_t;
327 for (idx_t i = 0; i < y.size(); ++i)
331 typedef typename stan::math::index_type<matrix_t>::type idx_t;
332 for (idx_t j = 0; j < y.cols(); ++j)
333 for (idx_t i = 0; i < y.rows(); ++i)
355 stan::math::check_unit_vector(
"stan::io::unit_vector_unconstrain",
357 typedef typename stan::math::index_type<vector_t>::type idx_t;
358 vector_t uy = stan::math::unit_vector_free(y);
359 for (idx_t i = 0; i < uy.size(); ++i)
360 data_r_.push_back(uy[i]);
379 typedef typename stan::math::index_type<vector_t>::type idx_t;
381 stan::math::check_simplex(
"stan::io::simplex_unconstrain",
"Vector", y);
382 vector_t uy = stan::math::simplex_free(y);
383 for (idx_t i = 0; i < uy.size(); ++i)
384 data_r_.push_back(uy[i]);
399 typedef typename stan::math::index_type<matrix_t>::type idx_t;
402 Eigen::Matrix<T, Eigen::Dynamic, 1> y_free
403 = stan::math::cholesky_factor_free(y);
404 for (idx_t i = 0; i < y_free.size(); ++i)
405 data_r_.push_back(y_free[i]);
421 typedef typename stan::math::index_type<matrix_t>::type idx_t;
424 Eigen::Matrix<T, Eigen::Dynamic, 1> y_free
425 = stan::math::cholesky_corr_free(y);
426 for (idx_t i = 0; i < y_free.size(); ++i)
427 data_r_.push_back(y_free[i]);
443 typedef typename stan::math::index_type<matrix_t>::type idx_t;
445 if (k == 0 || y.cols() != k)
446 BOOST_THROW_EXCEPTION(
447 std::runtime_error(
"y must have elements and" 448 " y must be a square matrix"));
449 idx_t k_choose_2 = (k * (k-1)) / 2;
450 array_vec_t cpcs(k_choose_2);
452 bool successful = stan::math::factor_cov_matrix(y, cpcs, sds);
454 BOOST_THROW_EXCEPTION
455 (std::runtime_error(
"factor_cov_matrix failed"));
456 for (idx_t i = 0; i < k_choose_2; ++i)
457 data_r_.push_back(cpcs[i]);
458 for (idx_t i = 0; i < k; ++i)
459 data_r_.push_back(sds[i]);
478 typedef typename stan::math::index_type<matrix_t>::type idx_t;
480 stan::math::check_corr_matrix(
"stan::io::corr_matrix_unconstrain",
483 idx_t k_choose_2 = (k * (k-1)) / 2;
484 array_vec_t cpcs(k_choose_2);
486 bool successful = stan::math::factor_cov_matrix(y, cpcs, sds);
488 BOOST_THROW_EXCEPTION
489 (std::runtime_error(
"y cannot be factorized by factor_cov_matrix"));
490 for (idx_t i = 0; i < k; ++i) {
492 if (fabs(sds[i] - 0.0) >= CONSTRAINT_TOLERANCE)
493 BOOST_THROW_EXCEPTION
494 (std::runtime_error(
"sds on log scale are unconstrained"));
496 for (idx_t i = 0; i < k_choose_2; ++i)
497 data_r_.push_back(cpcs[i]);
void corr_matrix_unconstrain(matrix_t &y)
Writes the unconstrained correlation matrix corresponding to the specified constrained correlation ma...
void row_vector_ub_unconstrain(double ub, row_vector_t &y)
void matrix_unconstrain(const matrix_t &y)
Write the specified unconstrained matrix.
Probability, optimization and sampling library.
void integer(int n)
Write the specified integer to the sequence of integer values.
std::vector< T > & data_r()
Return a reference to the underlying vector of real values that have been written.
void scalar_lub_unconstrain(double lb, double ub, T &y)
Write the unconstrained value corresponding to the specified value with the specified bounds...
void cholesky_factor_unconstrain(matrix_t &y)
Writes the unconstrained Cholesky factor corresponding to the specified constrained matrix...
void positive_ordered_unconstrain(vector_t &y)
Write the unconstrained vector that corresponds to the specified postiive ascendingly ordered vector...
void unit_vector_unconstrain(vector_t &y)
Write the unconstrained vector corresponding to the specified unit_vector value.
void scalar_lb_unconstrain(double lb, T &y)
Return the unconstrained version of the specified input, which is constrained to be above the specifi...
void scalar_unconstrain(T &y)
Write the unconstrained value corresponding to the specified scalar.
void row_vector_unconstrain(const vector_t &y)
Write the specified unconstrained vector.
void simplex_unconstrain(vector_t &y)
Write the unconstrained vector corresponding to the specified simplex value.
void row_vector_lub_unconstrain(double lb, double ub, row_vector_t &y)
void vector_ub_unconstrain(double ub, vector_t &y)
void matrix_lub_unconstrain(double lb, double ub, matrix_t &y)
void vector_lb_unconstrain(double lb, vector_t &y)
void cholesky_corr_unconstrain(matrix_t &y)
Writes the unconstrained Cholesky factor for a correlation matrix corresponding to the specified cons...
void cov_matrix_unconstrain(matrix_t &y)
Writes the unconstrained covariance matrix corresponding to the specified constrained correlation mat...
void vector_lub_unconstrain(double lb, double ub, vector_t &y)
void matrix_ub_unconstrain(double ub, matrix_t &y)
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix_t
void matrix_lb_unconstrain(double lb, matrix_t &y)
Eigen::Matrix< T, Eigen::Dynamic, 1 > vector_t
Eigen::Matrix< T, 1, Eigen::Dynamic > row_vector_t
std::vector< int > & data_i()
Return a reference to the underlying vector of integer values that have been written.
void scalar_pos_unconstrain(T &y)
Write the unconstrained value corresponding to the specified positive-constrained scalar...
~writer()
Destroy this writer.
void vector_unconstrain(const vector_t &y)
Write the specified unconstrained vector.
void prob_unconstrain(T &y)
Write the unconstrained value corresponding to the specified probability value.
void corr_unconstrain(T &y)
Write the unconstrained value corresponding to the specified correlation-constrained variable...
void ordered_unconstrain(vector_t &y)
Write the unconstrained vector that corresponds to the specified ascendingly ordered vector...
void row_vector_lb_unconstrain(double lb, row_vector_t &y)
writer(std::vector< T > &data_r, std::vector< int > &data_i)
Construct a writer that writes to the specified scalar and integer vectors.
void scalar_ub_unconstrain(double ub, T &y)
Write the unconstrained value corresponding to the specified lower-bounded value. ...
A stream-based writer for integer, scalar, vector, matrix and array data types, which transforms from...
Eigen::Array< T, Eigen::Dynamic, 1 > array_vec_t
const double CONSTRAINT_TOLERANCE
This is the tolerance for checking arithmetic bounds in rank and in simplexes.