1 #ifndef STAN_IO_WRITER_HPP
2 #define STAN_IO_WRITER_HPP
4 #include <stan/math/prim/arr/meta/index_type.hpp>
5 #include <stan/math/prim/mat/meta/index_type.hpp>
6 #include <stan/math/prim/scal/meta/index_type.hpp>
7 #include <stan/math/prim/mat/err/check_corr_matrix.hpp>
8 #include <stan/math/prim/mat/err/check_ordered.hpp>
9 #include <stan/math/prim/mat/err/check_positive_ordered.hpp>
10 #include <stan/math/prim/mat/err/check_simplex.hpp>
11 #include <stan/math/prim/mat/err/check_unit_vector.hpp>
12 #include <stan/math/prim/mat/fun/cholesky_corr_free.hpp>
13 #include <stan/math/prim/mat/fun/cholesky_factor_free.hpp>
14 #include <stan/math/prim/mat/fun/factor_cov_matrix.hpp>
15 #include <stan/math/prim/mat/fun/simplex_free.hpp>
16 #include <stan/math/prim/mat/fun/unit_vector_free.hpp>
17 #include <stan/math/prim/scal/fun/prob_free.hpp>
18 #include <stan/math/prim/scal/fun/lb_free.hpp>
19 #include <stan/math/prim/scal/fun/lub_free.hpp>
42 std::vector<T> data_r_;
43 std::vector<int> data_i_;
46 typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
matrix_t;
47 typedef Eigen::Matrix<T, Eigen::Dynamic, 1>
vector_t;
69 CONSTRAINT_TOLERANCE(1E-8) {
106 data_i_.push_back(n);
117 data_r_.push_back(y);
133 BOOST_THROW_EXCEPTION(std::runtime_error(
"y is negative"));
134 data_r_.push_back(log(y));
149 data_r_.push_back(stan::math::lb_free(y, lb));
163 data_r_.push_back(stan::math::ub_free(y, ub));
179 data_r_.push_back(stan::math::lub_free(y, lb, ub));
193 data_r_.push_back(stan::math::corr_free(y));
208 data_r_.push_back(stan::math::prob_free(y));
227 typedef typename stan::math::index_type<vector_t>::type idx_t;
228 if (y.size() == 0)
return;
229 stan::math::check_ordered(
"stan::io::ordered_unconstrain",
"Vector", y);
230 data_r_.push_back(y[0]);
231 for (idx_t i = 1; i < y.size(); ++i) {
232 data_r_.push_back(log(y[i] - y[i-1]));
253 typedef typename stan::math::index_type<vector_t>::type idx_t;
256 if (y.size() == 0)
return;
257 stan::math::check_positive_ordered
258 (
"stan::io::positive_ordered_unconstrain",
"Vector", y);
259 data_r_.push_back(log(y[0]));
260 for (idx_t i = 1; i < y.size(); ++i) {
261 data_r_.push_back(log(y[i] - y[i-1]));
272 typedef typename stan::math::index_type<vector_t>::type idx_t;
273 for (idx_t i = 0; i < y.size(); ++i)
274 data_r_.push_back(y[i]);
283 typedef typename stan::math::index_type<vector_t>::type idx_t;
284 for (idx_t i = 0; i < y.size(); ++i)
285 data_r_.push_back(y[i]);
294 typedef typename stan::math::index_type<matrix_t>::type idx_t;
295 for (idx_t j = 0; j < y.cols(); ++j)
296 for (idx_t i = 0; i < y.rows(); ++i)
297 data_r_.push_back(y(i, j));
301 typedef typename stan::math::index_type<vector_t>::type idx_t;
302 for (idx_t i = 0; i < y.size(); ++i)
306 typedef typename stan::math::index_type<row_vector_t>::type idx_t;
307 for (idx_t i = 0; i < y.size(); ++i)
311 typedef typename stan::math::index_type<matrix_t>::type idx_t;
312 for (idx_t j = 0; j < y.cols(); ++j)
313 for (idx_t i = 0; i < y.rows(); ++i)
318 typedef typename stan::math::index_type<vector_t>::type idx_t;
319 for (idx_t i = 0; i < y.size(); ++i)
323 typedef typename stan::math::index_type<row_vector_t>::type idx_t;
324 for (idx_t i = 0; i < y.size(); ++i)
328 typedef typename stan::math::index_type<matrix_t>::type idx_t;
329 for (idx_t j = 0; j < y.cols(); ++j)
330 for (idx_t i = 0; i < y.rows(); ++i)
336 typedef typename stan::math::index_type<vector_t>::type idx_t;
337 for (idx_t i = 0; i < y.size(); ++i)
341 typedef typename stan::math::index_type<row_vector_t>::type idx_t;
342 for (idx_t i = 0; i < y.size(); ++i)
346 typedef typename stan::math::index_type<matrix_t>::type idx_t;
347 for (idx_t j = 0; j < y.cols(); ++j)
348 for (idx_t i = 0; i < y.rows(); ++i)
370 stan::math::check_unit_vector(
"stan::io::unit_vector_unconstrain",
372 typedef typename stan::math::index_type<vector_t>::type idx_t;
373 vector_t uy = stan::math::unit_vector_free(y);
374 for (idx_t i = 0; i < uy.size(); ++i)
375 data_r_.push_back(uy[i]);
394 typedef typename stan::math::index_type<vector_t>::type idx_t;
396 stan::math::check_simplex(
"stan::io::simplex_unconstrain",
"Vector", y);
397 vector_t uy = stan::math::simplex_free(y);
398 for (idx_t i = 0; i < uy.size(); ++i)
399 data_r_.push_back(uy[i]);
414 typedef typename stan::math::index_type<matrix_t>::type idx_t;
417 Eigen::Matrix<T, Eigen::Dynamic, 1> y_free
418 = stan::math::cholesky_factor_free(y);
419 for (idx_t i = 0; i < y_free.size(); ++i)
420 data_r_.push_back(y_free[i]);
436 typedef typename stan::math::index_type<matrix_t>::type idx_t;
439 Eigen::Matrix<T, Eigen::Dynamic, 1> y_free
440 = stan::math::cholesky_corr_free(y);
441 for (idx_t i = 0; i < y_free.size(); ++i)
442 data_r_.push_back(y_free[i]);
458 typedef typename stan::math::index_type<matrix_t>::type idx_t;
460 if (k == 0 || y.cols() != k)
461 BOOST_THROW_EXCEPTION(
462 std::runtime_error(
"y must have elements and"
463 " y must be a square matrix"));
464 idx_t k_choose_2 = (k * (k-1)) / 2;
465 array_vec_t cpcs(k_choose_2);
467 bool successful = stan::math::factor_cov_matrix(y, cpcs, sds);
469 BOOST_THROW_EXCEPTION
470 (std::runtime_error(
"factor_cov_matrix failed"));
471 for (idx_t i = 0; i < k_choose_2; ++i)
472 data_r_.push_back(cpcs[i]);
473 for (idx_t i = 0; i < k; ++i)
474 data_r_.push_back(sds[i]);
493 typedef typename stan::math::index_type<matrix_t>::type idx_t;
495 stan::math::check_corr_matrix(
"stan::io::corr_matrix_unconstrain",
498 idx_t k_choose_2 = (k * (k-1)) / 2;
499 array_vec_t cpcs(k_choose_2);
501 bool successful = stan::math::factor_cov_matrix(y, cpcs, sds);
503 BOOST_THROW_EXCEPTION
504 (std::runtime_error(
"y cannot be factorized by factor_cov_matrix"));
505 for (idx_t i = 0; i < k; ++i) {
507 if (fabs(sds[i] - 0.0) >= CONSTRAINT_TOLERANCE)
508 BOOST_THROW_EXCEPTION
509 (std::runtime_error(
"sds on log scale are unconstrained"));
511 for (idx_t i = 0; i < k_choose_2; ++i)
512 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.