Stan  2.10.0
probability, sampling & optimization
reader.hpp
Go to the documentation of this file.
1 #ifndef STAN_IO_READER_HPP
2 #define STAN_IO_READER_HPP
3 
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>
29 #include <vector>
30 
31 namespace stan {
32 
33  namespace io {
34 
35 
57  template <typename T>
58  class reader {
59  private:
60  std::vector<T>& data_r_;
61  std::vector<int>& data_i_;
62  size_t pos_;
63  size_t int_pos_;
64 
65  inline T& scalar_ptr() {
66  return data_r_.at(pos_);
67  }
68 
69  inline T& scalar_ptr_increment(size_t m) {
70  pos_ += m;
71  return data_r_.at(pos_ - m);
72  }
73 
74  inline int& int_ptr() {
75  return data_i_.at(int_pos_);
76  }
77 
78  inline int& int_ptr_increment(size_t m) {
79  int_pos_ += m;
80  return data_i_.at(int_pos_ - m);
81  }
82 
83  public:
84  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrix_t;
85  typedef Eigen::Matrix<T, Eigen::Dynamic, 1> vector_t;
86  typedef Eigen::Matrix<T, 1, Eigen::Dynamic> row_vector_t;
87 
88  typedef Eigen::Map<matrix_t> map_matrix_t;
89  typedef Eigen::Map<vector_t> map_vector_t;
90  typedef Eigen::Map<row_vector_t> map_row_vector_t;
91 
92 
104  reader(std::vector<T>& data_r,
105  std::vector<int>& data_i)
106  : data_r_(data_r),
107  data_i_(data_i),
108  pos_(0),
109  int_pos_(0) {
110  }
111 
115  ~reader() { }
116 
122  inline size_t available() {
123  return data_r_.size() - pos_;
124  }
125 
131  inline size_t available_i() {
132  return data_i_.size() - int_pos_;
133  }
134 
140  inline int integer() {
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_++];
145  }
146 
154  inline int integer_constrain() {
155  return integer();
156  }
157 
165  inline int integer_constrain(T& /*log_prob*/) {
166  return integer();
167  }
168 
169 
170 
176  inline T scalar() {
177  if (pos_ >= data_r_.size())
178  BOOST_THROW_EXCEPTION(std::runtime_error("no more scalars to read"));
179  return data_r_[pos_++];
180  }
181 
188  inline T scalar_constrain() {
189  return scalar();
190  }
191 
203  T scalar_constrain(T& /*log_prob*/) {
204  return scalar();
205  }
206 
207 
215  inline std::vector<T> std_vector(size_t m) {
216  if (m == 0) return std::vector<T>();
217  std::vector<T> vec;
218  T& start = scalar_ptr_increment(m);
219  vec.insert(vec.begin(), &start, &scalar_ptr());
220  return vec;
221  }
222 
230  inline vector_t vector(size_t m) {
231  if (m == 0) return vector_t();
232  return map_vector_t(&scalar_ptr_increment(m), m);
233  }
241  inline vector_t vector_constrain(size_t m) {
242  if (m == 0) return vector_t();
243  return map_vector_t(&scalar_ptr_increment(m), m);
244  }
253  inline vector_t vector_constrain(size_t m, T& /*lp*/) {
254  if (m == 0) return vector_t();
255  return map_vector_t(&scalar_ptr_increment(m), m);
256  }
257 
258 
259 
267  inline row_vector_t row_vector(size_t m) {
268  if (m == 0) return row_vector_t();
269  return map_row_vector_t(&scalar_ptr_increment(m), m);
270  }
271 
279  inline row_vector_t row_vector_constrain(size_t m) {
280  if (m == 0) return row_vector_t();
281  return map_row_vector_t(&scalar_ptr_increment(m), m);
282  }
283 
293  inline row_vector_t row_vector_constrain(size_t m, T& /*lp*/) {
294  if (m == 0) return row_vector_t();
295  return map_row_vector_t(&scalar_ptr_increment(m), m);
296  }
297 
315  inline matrix_t matrix(size_t m, size_t n) {
316  if (m == 0 || n == 0)
317  return matrix_t(m, n);
318  return map_matrix_t(&scalar_ptr_increment(m*n), m, n);
319  }
320 
331  inline matrix_t matrix_constrain(size_t m, size_t n) {
332  if (m == 0 || n == 0)
333  return matrix_t(m, n);
334  return map_matrix_t(&scalar_ptr_increment(m*n), m, n);
335  }
336 
349  inline matrix_t matrix_constrain(size_t m, size_t n, T& /*lp*/) {
350  if (m == 0 || n == 0)
351  return matrix_t(m, n);
352  return map_matrix_t(&scalar_ptr_increment(m*n), m, n);
353  }
354 
355 
365  inline int integer_lb(int lb) {
366  int i = integer();
367  if (!(i >= lb))
368  BOOST_THROW_EXCEPTION(
369  std::runtime_error("required value greater than or equal to lb"));
370  return i;
371  }
381  inline int integer_lb_constrain(int lb) {
382  return integer_lb(lb);
383  }
394  inline int integer_lb_constrain(int lb, T& /*lp*/) {
395  return integer_lb(lb);
396  }
397 
398 
408  inline int integer_ub(int ub) {
409  int i = integer();
410  if (!(i <= ub))
411  BOOST_THROW_EXCEPTION(
412  std::runtime_error("required value less than or equal to ub"));
413  return i;
414  }
424  inline int integer_ub_constrain(int ub) {
425  return integer_ub(ub);
426  }
437  int integer_ub_constrain(int ub, T& /*lp*/) {
438  return integer_ub(ub);
439  }
440 
453  inline int integer_lub(int lb, int ub) {
454  // read first to make position deterministic [arbitrary choice]
455  int i = integer();
456  if (lb > ub)
457  BOOST_THROW_EXCEPTION(
458  std::runtime_error("lower bound must be less than or equal to ub"));
459  if (!(i >= lb))
460  BOOST_THROW_EXCEPTION(
461  std::runtime_error("required value greater than or equal to lb"));
462  if (!(i <= ub))
463  BOOST_THROW_EXCEPTION(
464  std::runtime_error("required value less than or equal to ub"));
465  return i;
466  }
477  inline int integer_lub_constrain(int lb, int ub) {
478  return integer_lub(lb, ub);
479  }
491  inline int integer_lub_constrain(int lb, int ub, T& /*lp*/) {
492  return integer_lub(lb, ub);
493  }
494 
495 
496 
506  inline T scalar_pos() {
507  T x(scalar());
508  stan::math::check_positive("stan::io::scalar_pos",
509  "Constrained scalar", x);
510  return x;
511  }
512 
520  inline T scalar_pos_constrain() {
521  return stan::math::positive_constrain(scalar());
522  }
523 
534  inline T scalar_pos_constrain(T& lp) {
535  return stan::math::positive_constrain(scalar(), lp);
536  }
537 
550  template <typename TL>
551  inline T scalar_lb(const TL lb) {
552  T x(scalar());
553  stan::math::check_greater_or_equal("stan::io::scalar_lb",
554  "Constrained scalar", x, lb);
555  return x;
556  }
557 
569  template <typename TL>
570  inline T scalar_lb_constrain(const TL lb) {
571  return stan::math::lb_constrain(scalar(), lb);
572  }
573 
585  template <typename TL>
586  inline T scalar_lb_constrain(const TL lb, T& lp) {
587  return stan::math::lb_constrain(scalar(), lb, lp);
588  }
589 
590 
591 
604  template <typename TU>
605  inline T scalar_ub(TU ub) {
606  T x(scalar());
607  stan::math::check_less_or_equal("stan::io::scalar_ub",
608  "Constrained scalar", x, ub);
609  return x;
610  }
611 
623  template <typename TU>
624  inline T scalar_ub_constrain(const TU ub) {
625  return stan::math::ub_constrain(scalar(), ub);
626  }
627 
639  template <typename TU>
640  inline T scalar_ub_constrain(const TU ub, T& lp) {
641  return stan::math::ub_constrain(scalar(), ub, lp);
642  }
643 
658  template <typename TL, typename TU>
659  inline T scalar_lub(const TL lb, const TU ub) {
660  T x(scalar());
661  stan::math::check_bounded<T, TL, TU>
662  ("stan::io::scalar_lub", "Constrained scalar", x, lb, ub);
663  return x;
664  }
665 
679  template <typename TL, typename TU>
680  inline T scalar_lub_constrain(const TL lb, const TU ub) {
681  return stan::math::lub_constrain(scalar(), lb, ub);
682  }
683 
697  template <typename TL, typename TU>
698  inline T scalar_lub_constrain(TL lb, TU ub, T& lp) {
699  return stan::math::lub_constrain(scalar(), lb, ub, lp);
700  }
701 
710  inline T prob() {
711  T x(scalar());
712  stan::math::check_bounded<T, double, double>
713  ("stan::io::prob", "Constrained probability", x, 0, 1);
714  return x;
715  }
716 
725  inline T prob_constrain() {
726  return stan::math::prob_constrain(scalar());
727  }
728 
739  inline T prob_constrain(T& lp) {
740  return stan::math::prob_constrain(scalar(), lp);
741  }
742 
743 
744 
745 
757  inline T corr() {
758  T x(scalar());
759  stan::math::check_bounded<T, double, double>
760  ("stan::io::corr", "Correlation value", x, -1, 1);
761  return x;
762  }
763 
772  inline T corr_constrain() {
773  return stan::math::corr_constrain(scalar());
774  }
775 
787  inline T corr_constrain(T& lp) {
788  return stan::math::corr_constrain(scalar(), lp);
789  }
790 
801  inline vector_t unit_vector(size_t k) {
802  vector_t theta(vector(k));
803  stan::math::check_unit_vector("stan::io::unit_vector",
804  "Constrained vector", theta);
805  return theta;
806  }
807 
818  inline
819  Eigen::Matrix<T, Eigen::Dynamic, 1> unit_vector_constrain(size_t k) {
820  return stan::math::unit_vector_constrain(vector(k));
821  }
822 
835  inline vector_t unit_vector_constrain(size_t k, T& lp) {
836  return stan::math::unit_vector_constrain(vector(k), lp);
837  }
838 
849  inline vector_t simplex(size_t k) {
850  vector_t theta(vector(k));
851  stan::math::check_simplex("stan::io::simplex",
852  "Constrained vector", theta);
853  return theta;
854  }
855 
866  inline
867  Eigen::Matrix<T, Eigen::Dynamic, 1> simplex_constrain(size_t k) {
868  return stan::math::simplex_constrain(vector(k-1));
869  }
870 
883  inline vector_t simplex_constrain(size_t k, T& lp) {
884  return stan::math::simplex_constrain(vector(k-1), lp);
885  }
886 
897  inline vector_t ordered(size_t k) {
898  vector_t x(vector(k));
899  stan::math::check_ordered("stan::io::ordered", "Constrained vector", x);
900  return x;
901  }
902 
912  inline vector_t ordered_constrain(size_t k) {
913  return stan::math::ordered_constrain(vector(k));
914  }
915 
927  inline vector_t ordered_constrain(size_t k, T& lp) {
928  return stan::math::ordered_constrain(vector(k), lp);
929  }
930 
941  inline vector_t positive_ordered(size_t k) {
942  vector_t x(vector(k));
943  stan::math::check_positive_ordered("stan::io::positive_ordered",
944  "Constrained vector", x);
945  return x;
946  }
947 
957  inline vector_t positive_ordered_constrain(size_t k) {
958  return stan::math::positive_ordered_constrain(vector(k));
959  }
960 
972  inline vector_t positive_ordered_constrain(size_t k, T& lp) {
973  return stan::math::positive_ordered_constrain(vector(k), lp);
974  }
975 
976 
977 
988  inline matrix_t cholesky_factor(size_t M, size_t N) {
989  matrix_t y(matrix(M, N));
990  stan::math::check_cholesky_factor("stan::io::cholesky_factor",
991  "Constrained matrix", y);
992  return y;
993  }
994 
1006  inline matrix_t cholesky_factor_constrain(size_t M, size_t N) {
1007  return stan::math::cholesky_factor_constrain
1008  (vector((N * (N + 1)) / 2 + (M - N) * N), M, N);
1009  }
1010 
1024  inline matrix_t cholesky_factor_constrain(size_t M, size_t N, T& lp) {
1025  return stan::math::cholesky_factor_constrain
1026  (vector((N * (N + 1)) / 2 + (M - N) * N), M, N, lp);
1027  }
1028 
1029 
1030 
1041  inline matrix_t cholesky_corr(size_t K) {
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);
1046  return y;
1047  }
1048 
1059  inline matrix_t cholesky_corr_constrain(size_t K) {
1060  return stan::math::cholesky_corr_constrain(vector((K * (K - 1)) / 2),
1061  K);
1062  }
1063 
1077  inline matrix_t cholesky_corr_constrain(size_t K, T& lp) {
1078  return stan::math::cholesky_corr_constrain(vector((K * (K - 1)) / 2),
1079  K, lp);
1080  }
1081 
1082 
1083 
1095  inline matrix_t cov_matrix(size_t k) {
1096  matrix_t y(matrix(k, k));
1097  stan::math::check_cov_matrix("stan::io::cov_matrix",
1098  "Constrained matrix", y);
1099  return y;
1100  }
1101 
1110  inline matrix_t cov_matrix_constrain(size_t k) {
1111  return stan::math::cov_matrix_constrain(vector(k + (k * (k - 1)) / 2),
1112  k);
1113  }
1114 
1126  inline matrix_t cov_matrix_constrain(size_t k, T& lp) {
1127  return stan::math::cov_matrix_constrain(vector(k + (k * (k - 1)) / 2),
1128  k, lp);
1129  }
1130 
1131 
1141  inline matrix_t corr_matrix(size_t k) {
1142  matrix_t x(matrix(k, k));
1143  stan::math::check_corr_matrix("stan::math::corr_matrix",
1144  "Constrained matrix", x);
1145  return x;
1146  }
1147 
1156  inline matrix_t corr_matrix_constrain(size_t k) {
1157  return stan::math::corr_matrix_constrain(vector((k * (k - 1)) / 2), k);
1158  }
1159 
1171  inline matrix_t corr_matrix_constrain(size_t k, T& lp) {
1172  return stan::math::corr_matrix_constrain(vector((k * (k - 1)) / 2),
1173  k, lp);
1174  }
1175 
1176  template <typename TL>
1177  inline vector_t vector_lb(const TL lb, size_t m) {
1178  vector_t v(m);
1179  for (size_t i = 0; i < m; ++i)
1180  v(i) = scalar_lb(lb);
1181  return v;
1182  }
1183 
1184  template <typename TL>
1185  inline vector_t vector_lb_constrain(const TL lb, size_t m) {
1186  vector_t v(m);
1187  for (size_t i = 0; i < m; ++i)
1188  v(i) = scalar_lb_constrain(lb);
1189  return v;
1190  }
1191 
1192  template <typename TL>
1193  inline vector_t vector_lb_constrain(const TL lb, size_t m, T& lp) {
1194  vector_t v(m);
1195  for (size_t i = 0; i < m; ++i)
1196  v(i) = scalar_lb_constrain(lb, lp);
1197  return v;
1198  }
1199 
1200  template <typename TL>
1201  inline row_vector_t row_vector_lb(const TL lb, size_t m) {
1202  row_vector_t v(m);
1203  for (size_t i = 0; i < m; ++i)
1204  v(i) = scalar_lb(lb);
1205  return v;
1206  }
1207 
1208  template <typename TL>
1209  inline row_vector_t row_vector_lb_constrain(const TL lb, size_t m) {
1210  row_vector_t v(m);
1211  for (size_t i = 0; i < m; ++i)
1212  v(i) = scalar_lb_constrain(lb);
1213  return v;
1214  }
1215 
1216  template <typename TL>
1217  inline row_vector_t
1218  row_vector_lb_constrain(const TL lb, size_t m, T& lp) {
1219  row_vector_t v(m);
1220  for (size_t i = 0; i < m; ++i)
1221  v(i) = scalar_lb_constrain(lb, lp);
1222  return v;
1223  }
1224 
1225  template <typename TL>
1226  inline matrix_t matrix_lb(const TL lb, size_t m, size_t n) {
1227  matrix_t v(m, n);
1228  for (size_t j = 0; j < n; ++j)
1229  for (size_t i = 0; i < m; ++i)
1230  v(i, j) = scalar_lb(lb);
1231  return v;
1232  }
1233 
1234  template <typename TL>
1235  inline matrix_t matrix_lb_constrain(const TL lb, size_t m, size_t n) {
1236  matrix_t v(m, n);
1237  for (size_t j = 0; j < n; ++j)
1238  for (size_t i = 0; i < m; ++i)
1239  v(i, j) = scalar_lb_constrain(lb);
1240  return v;
1241  }
1242 
1243  template <typename TL>
1244  inline matrix_t
1245  matrix_lb_constrain(const TL lb, size_t m, size_t n, T& lp) {
1246  matrix_t v(m, n);
1247  for (size_t j = 0; j < n; ++j)
1248  for (size_t i = 0; i < m; ++i)
1249  v(i, j) = scalar_lb_constrain(lb, lp);
1250  return v;
1251  }
1252 
1253  template <typename TU>
1254  inline vector_t vector_ub(const TU ub, size_t m) {
1255  vector_t v(m);
1256  for (size_t i = 0; i < m; ++i)
1257  v(i) = scalar_ub(ub);
1258  return v;
1259  }
1260 
1261  template <typename TU>
1262  inline vector_t vector_ub_constrain(const TU ub, size_t m) {
1263  vector_t v(m);
1264  for (size_t i = 0; i < m; ++i)
1265  v(i) = scalar_ub_constrain(ub);
1266  return v;
1267  }
1268 
1269  template <typename TU>
1270  inline vector_t vector_ub_constrain(const TU ub, size_t m, T& lp) {
1271  vector_t v(m);
1272  for (size_t i = 0; i < m; ++i)
1273  v(i) = scalar_ub_constrain(ub, lp);
1274  return v;
1275  }
1276 
1277  template <typename TU>
1278  inline row_vector_t row_vector_ub(const TU ub, size_t m) {
1279  row_vector_t v(m);
1280  for (size_t i = 0; i < m; ++i)
1281  v(i) = scalar_ub(ub);
1282  return v;
1283  }
1284 
1285  template <typename TU>
1286  inline row_vector_t row_vector_ub_constrain(const TU ub, size_t m) {
1287  row_vector_t v(m);
1288  for (size_t i = 0; i < m; ++i)
1289  v(i) = scalar_ub_constrain(ub);
1290  return v;
1291  }
1292 
1293  template <typename TU>
1294  inline row_vector_t
1295  row_vector_ub_constrain(const TU ub, size_t m, T& lp) {
1296  row_vector_t v(m);
1297  for (size_t i = 0; i < m; ++i)
1298  v(i) = scalar_ub_constrain(ub, lp);
1299  return v;
1300  }
1301 
1302  template <typename TU>
1303  inline matrix_t matrix_ub(const TU ub, size_t m, size_t n) {
1304  matrix_t v(m, n);
1305  for (size_t j = 0; j < n; ++j)
1306  for (size_t i = 0; i < m; ++i)
1307  v(i, j) = scalar_ub(ub);
1308  return v;
1309  }
1310 
1311  template <typename TU>
1312  inline matrix_t matrix_ub_constrain(const TU ub, size_t m, size_t n) {
1313  matrix_t v(m, n);
1314  for (size_t j = 0; j < n; ++j)
1315  for (size_t i = 0; i < m; ++i)
1316  v(i, j) = scalar_ub_constrain(ub);
1317  return v;
1318  }
1319 
1320  template <typename TU>
1321  inline matrix_t
1322  matrix_ub_constrain(const TU ub, size_t m, size_t n, T& lp) {
1323  matrix_t v(m, n);
1324  for (size_t j = 0; j < n; ++j)
1325  for (size_t i = 0; i < m; ++i)
1326  v(i, j) = scalar_ub_constrain(ub, lp);
1327  return v;
1328  }
1329 
1330  template <typename TL, typename TU>
1331  inline vector_t vector_lub(const TL lb, const TU ub, size_t m) {
1332  vector_t v(m);
1333  for (size_t i = 0; i < m; ++i)
1334  v(i) = scalar_lub(lb, ub);
1335  return v;
1336  }
1337 
1338  template <typename TL, typename TU>
1339  inline vector_t
1340  vector_lub_constrain(const TL lb, const TU ub, size_t m) {
1341  vector_t v(m);
1342  for (size_t i = 0; i < m; ++i)
1343  v(i) = scalar_lub_constrain(lb, ub);
1344  return v;
1345  }
1346 
1347  template <typename TL, typename TU>
1348  inline vector_t
1349  vector_lub_constrain(const TL lb, const TU ub, size_t m, T& lp) {
1350  vector_t v(m);
1351  for (size_t i = 0; i < m; ++i)
1352  v(i) = scalar_lub_constrain(lb, ub, lp);
1353  return v;
1354  }
1355 
1356  template <typename TL, typename TU>
1357  inline row_vector_t row_vector_lub(const TL lb, const TU ub, size_t m) {
1358  row_vector_t v(m);
1359  for (size_t i = 0; i < m; ++i)
1360  v(i) = scalar_lub(lb, ub);
1361  return v;
1362  }
1363  template <typename TL, typename TU>
1364  inline row_vector_t
1365  row_vector_lub_constrain(const TL lb, const TU ub, size_t m) {
1366  row_vector_t v(m);
1367  for (size_t i = 0; i < m; ++i)
1368  v(i) = scalar_lub_constrain(lb, ub);
1369  return v;
1370  }
1371 
1372  template <typename TL, typename TU>
1373  inline row_vector_t
1374  row_vector_lub_constrain(const TL lb, const TU ub, size_t m, T& lp) {
1375  row_vector_t v(m);
1376  for (size_t i = 0; i < m; ++i)
1377  v(i) = scalar_lub_constrain(lb, ub, lp);
1378  return v;
1379  }
1380 
1381  template <typename TL, typename TU>
1382  inline matrix_t matrix_lub(const TL lb, const TU ub, size_t m, size_t n) {
1383  matrix_t v(m, n);
1384  for (size_t j = 0; j < n; ++j)
1385  for (size_t i = 0; i < m; ++i)
1386  v(i, j) = scalar_lub(lb, ub);
1387  return v;
1388  }
1389 
1390  template <typename TL, typename TU>
1391  inline matrix_t
1392  matrix_lub_constrain(const TL lb, const TU ub, size_t m, size_t n) {
1393  matrix_t v(m, n);
1394  for (size_t j = 0; j < n; ++j)
1395  for (size_t i = 0; i < m; ++i)
1396  v(i, j) = scalar_lub_constrain(lb, ub);
1397  return v;
1398  }
1399 
1400  template <typename TL, typename TU>
1401  inline matrix_t
1402  matrix_lub_constrain(const TL lb, const TU ub, size_t m, size_t n,
1403  T& lp) {
1404  matrix_t v(m, n);
1405  for (size_t j = 0; j < n; ++j)
1406  for (size_t i = 0; i < m; ++i)
1407  v(i, j) = scalar_lub_constrain(lb, ub, lp);
1408  return v;
1409  }
1410  };
1411 
1412  }
1413 
1414 }
1415 
1416 #endif
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.
Definition: reader.hpp:1077
vector_t positive_ordered_constrain(size_t k)
Return the next positive ordered vector of the specified length.
Definition: reader.hpp:957
row_vector_t row_vector_ub_constrain(const TU ub, size_t m)
Definition: reader.hpp:1286
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.
Definition: reader.hpp:883
T scalar_lub_constrain(const TL lb, const TU ub)
Return the next scalar transformed to be between the specified lower and upper bounds.
Definition: reader.hpp:680
matrix_t matrix_lub(const TL lb, const TU ub, size_t m, size_t n)
Definition: reader.hpp:1382
T scalar_ub(TU ub)
Return the next scalar, checking that it is less than or equal to the specified upper bound...
Definition: reader.hpp:605
T scalar_pos()
Return the next scalar, checking that it is positive.
Definition: reader.hpp:506
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.
Definition: reader.hpp:835
int integer_ub_constrain(int ub)
Return the next integer, checking that it is less than or equal to the specified upper bound...
Definition: reader.hpp:424
matrix_t matrix_ub(const TU ub, size_t m, size_t n)
Definition: reader.hpp:1303
matrix_t matrix_lub_constrain(const TL lb, const TU ub, size_t m, size_t n)
Definition: reader.hpp:1392
std::vector< T > std_vector(size_t m)
Return a standard library vector of the specified dimensionality made up of the next scalars...
Definition: reader.hpp:215
T scalar_lb(const TL lb)
Return the next scalar, checking that it is greater than or equal to the specified lower bound...
Definition: reader.hpp:551
Eigen::Matrix< T, Eigen::Dynamic, 1 > vector_t
Definition: reader.hpp:85
row_vector_t row_vector_constrain(size_t m)
Return a row vector of specified dimensionality made up of the next scalars.
Definition: reader.hpp:279
int integer_ub_constrain(int ub, T &)
Return the next integer, checking that it is less than or equal to the specified upper bound...
Definition: reader.hpp:437
T scalar_constrain()
Return the next scalar.
Definition: reader.hpp:188
vector_t vector_lub_constrain(const TL lb, const TU ub, size_t m, T &lp)
Definition: reader.hpp:1349
Probability, optimization and sampling library.
Eigen::Map< row_vector_t > map_row_vector_t
Definition: reader.hpp:90
int integer_lb_constrain(int lb, T &)
Return the next integer, checking that it is greater than or equal to the specified lower bound...
Definition: reader.hpp:394
vector_t vector_constrain(size_t m)
Return a column vector of specified dimensionality made up of the next scalars.
Definition: reader.hpp:241
row_vector_t row_vector_ub(const TU ub, size_t m)
Definition: reader.hpp:1278
matrix_t cov_matrix(size_t k)
Return the next covariance matrix with the specified dimensionality.
Definition: reader.hpp:1095
matrix_t matrix_lb_constrain(const TL lb, size_t m, size_t n, T &lp)
Definition: reader.hpp:1245
T corr_constrain()
Return the next scalar transformed to be a correlation between -1 and 1.
Definition: reader.hpp:772
T scalar_constrain(T &)
Return the next scalar in the sequence, incrementing the specified reference with the log absolute Ja...
Definition: reader.hpp:203
matrix_t cov_matrix_constrain(size_t k)
Return the next covariance matrix of the specified dimensionality.
Definition: reader.hpp:1110
T prob_constrain(T &lp)
Return the next scalar transformed to be a probability between 0 and 1, incrementing the specified re...
Definition: reader.hpp:739
matrix_t corr_matrix_constrain(size_t k, T &lp)
Return the next correlation matrix of the specified dimensionality, incrementing the specified refere...
Definition: reader.hpp:1171
int integer_constrain(T &)
Return the next integer in the integer sequence.
Definition: reader.hpp:165
T corr()
Return the next scalar, checking that it is a valid value for a correlation, between -1 (inclusive) a...
Definition: reader.hpp:757
int integer_ub(int ub)
Return the next integer, checking that it is less than or equal to the specified upper bound...
Definition: reader.hpp:408
int integer_lb_constrain(int lb)
Return the next integer, checking that it is greater than or equal to the specified lower bound...
Definition: reader.hpp:381
T scalar_lb_constrain(const TL lb)
Return the next scalar transformed to have the specified lower bound.
Definition: reader.hpp:570
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.
Definition: reader.hpp:1041
T scalar_pos_constrain()
Return the next scalar, transformed to be positive.
Definition: reader.hpp:520
Eigen::Matrix< T, 1, Eigen::Dynamic > row_vector_t
Definition: reader.hpp:86
vector_t vector_ub(const TU ub, size_t m)
Definition: reader.hpp:1254
row_vector_t row_vector(size_t m)
Return a row vector of specified dimensionality made up of the next scalars.
Definition: reader.hpp:267
vector_t vector(size_t m)
Return a column vector of specified dimensionality made up of the next scalars.
Definition: reader.hpp:230
vector_t vector_lub(const TL lb, const TU ub, size_t m)
Definition: reader.hpp:1331
row_vector_t row_vector_constrain(size_t m, T &)
Return a row vector of specified dimensionality made up of the next scalars.
Definition: reader.hpp:293
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 ...
Definition: reader.hpp:331
vector_t unit_vector(size_t k)
Return a unit_vector of the specified size made up of the next scalars.
Definition: reader.hpp:801
row_vector_t row_vector_lb_constrain(const TL lb, size_t m)
Definition: reader.hpp:1209
vector_t vector_ub_constrain(const TU ub, size_t m, T &lp)
Definition: reader.hpp:1270
Eigen::Map< vector_t > map_vector_t
Definition: reader.hpp:89
matrix_t cholesky_factor(size_t M, size_t N)
Return the next Cholesky factor with the specified dimensionality, reading it directly without transf...
Definition: reader.hpp:988
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix_t
Definition: reader.hpp:84
vector_t ordered(size_t k)
Return the next vector of specified size containing values in ascending order.
Definition: reader.hpp:897
A stream-based reader for integer, scalar, vector, matrix and array data types, with Jacobian calcula...
Definition: reader.hpp:58
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...
Definition: reader.hpp:104
T scalar_ub_constrain(const TU ub)
Return the next scalar transformed to have the specified upper bound.
Definition: reader.hpp:624
matrix_t matrix_lub_constrain(const TL lb, const TU ub, size_t m, size_t n, T &lp)
Definition: reader.hpp:1402
int integer_lub(int lb, int ub)
Return the next integer, checking that it is less than or equal to the specified upper bound...
Definition: reader.hpp:453
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 ...
Definition: reader.hpp:315
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.
Definition: reader.hpp:787
row_vector_t row_vector_lub_constrain(const TL lb, const TU ub, size_t m, T &lp)
Definition: reader.hpp:1374
T scalar()
Return the next scalar in the sequence.
Definition: reader.hpp:176
T scalar_pos_constrain(T &lp)
Return the next scalar transformed to be positive, incrementing the specified reference with the log ...
Definition: reader.hpp:534
vector_t ordered_constrain(size_t k)
Return the next ordered vector of the specified length.
Definition: reader.hpp:912
matrix_t matrix_lb(const TL lb, size_t m, size_t n)
Definition: reader.hpp:1226
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...
Definition: reader.hpp:477
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...
Definition: reader.hpp:1024
T prob_constrain()
Return the next scalar transformed to be a probability between 0 and 1.
Definition: reader.hpp:725
vector_t simplex(size_t k)
Return a simplex of the specified size made up of the next scalars.
Definition: reader.hpp:849
Eigen::Matrix< T, Eigen::Dynamic, 1 > unit_vector_constrain(size_t k)
Return the next unit_vector transformed vector of the specified length.
Definition: reader.hpp:819
matrix_t corr_matrix(size_t k)
Returns the next correlation matrix of the specified dimensionality.
Definition: reader.hpp:1141
vector_t vector_lb(const TL lb, size_t m)
Definition: reader.hpp:1177
T scalar_lb_constrain(const TL lb, T &lp)
Return the next scalar transformed to have the specified lower bound, incrementing the specified refe...
Definition: reader.hpp:586
vector_t vector_lb_constrain(const TL lb, size_t m)
Definition: reader.hpp:1185
vector_t positive_ordered(size_t k)
Return the next vector of specified size containing positive values in ascending order.
Definition: reader.hpp:941
size_t available()
Return the number of scalars remaining to be read.
Definition: reader.hpp:122
matrix_t cov_matrix_constrain(size_t k, T &lp)
Return the next covariance matrix of the specified dimensionality, incrementing the specified referen...
Definition: reader.hpp:1126
row_vector_t row_vector_lub_constrain(const TL lb, const TU ub, size_t m)
Definition: reader.hpp:1365
Eigen::Matrix< T, Eigen::Dynamic, 1 > simplex_constrain(size_t k)
Return the next simplex transformed vector of the specified length.
Definition: reader.hpp:867
~reader()
Destroy this variable reader.
Definition: reader.hpp:115
row_vector_t row_vector_lub(const TL lb, const TU ub, size_t m)
Definition: reader.hpp:1357
int integer()
Return the next integer in the integer sequence.
Definition: reader.hpp:140
matrix_t matrix_ub_constrain(const TU ub, size_t m, size_t n, T &lp)
Definition: reader.hpp:1322
matrix_t matrix_ub_constrain(const TU ub, size_t m, size_t n)
Definition: reader.hpp:1312
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...
Definition: reader.hpp:1006
int integer_constrain()
Return the next integer in the integer sequence.
Definition: reader.hpp:154
size_t available_i()
Return the number of integers remaining to be read.
Definition: reader.hpp:131
row_vector_t row_vector_lb(const TL lb, size_t m)
Definition: reader.hpp:1201
vector_t vector_lub_constrain(const TL lb, const TU ub, size_t m)
Definition: reader.hpp:1340
Eigen::Map< matrix_t > map_matrix_t
Definition: reader.hpp:88
int integer_lb(int lb)
Return the next integer, checking that it is greater than or equal to the specified lower bound...
Definition: reader.hpp:365
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...
Definition: reader.hpp:491
T scalar_ub_constrain(const TU ub, T &lp)
Return the next scalar transformed to have the specified upper bound, incrementing the specified refe...
Definition: reader.hpp:640
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...
Definition: reader.hpp:698
matrix_t corr_matrix_constrain(size_t k)
Return the next correlation matrix of the specified dimensionality.
Definition: reader.hpp:1156
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 ...
Definition: reader.hpp:349
vector_t vector_lb_constrain(const TL lb, size_t m, T &lp)
Definition: reader.hpp:1193
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...
Definition: reader.hpp:927
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.
Definition: reader.hpp:1059
T prob()
Return the next scalar, checking that it is a valid value for a probability, between 0 (inclusive) an...
Definition: reader.hpp:710
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...
Definition: reader.hpp:972
row_vector_t row_vector_ub_constrain(const TU ub, size_t m, T &lp)
Definition: reader.hpp:1295
T scalar_lub(const TL lb, const TU ub)
Return the next scalar, checking that it is between the specified lower and upper bound...
Definition: reader.hpp:659
row_vector_t row_vector_lb_constrain(const TL lb, size_t m, T &lp)
Definition: reader.hpp:1218
matrix_t matrix_lb_constrain(const TL lb, size_t m, size_t n)
Definition: reader.hpp:1235
vector_t vector_ub_constrain(const TU ub, size_t m)
Definition: reader.hpp:1262
vector_t vector_constrain(size_t m, T &)
Return a column vector of specified dimensionality made up of the next scalars.
Definition: reader.hpp:253

     [ Stan Home Page ] © 2011–2016, Stan Development Team.