1 #ifndef STAN_MATH_PRIM_FUN_STAN_PRINT_HPP
2 #define STAN_MATH_PRIM_FUN_STAN_PRINT_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 #include <vector>
7 
8 namespace stan {
9 namespace math {
10 // prints used in generator for print() statements in modeling language
11 
12 template <typename T, require_not_container_t<T>* = nullptr>
stan_print(std::ostream * o,const T & x)13 void stan_print(std::ostream* o, const T& x) {
14   *o << x;
15 }
16 
17 template <typename EigVec, require_eigen_vector_t<EigVec>* = nullptr>
stan_print(std::ostream * o,const EigVec & x)18 void stan_print(std::ostream* o, const EigVec& x) {
19   const auto& x_ref = to_ref(x);
20 
21   *o << '[';
22   for (int i = 0; i < x_ref.size(); ++i) {
23     if (i > 0) {
24       *o << ',';
25     }
26     stan_print(o, x_ref.coeff(i));
27   }
28   *o << ']';
29 }
30 
31 template <typename EigMat, require_eigen_t<EigMat>* = nullptr,
32           require_not_eigen_vector_t<EigMat>* = nullptr>
stan_print(std::ostream * o,const EigMat & x)33 void stan_print(std::ostream* o, const EigMat& x) {
34   const auto& x_ref = to_ref(x);
35 
36   *o << '[';
37   for (int i = 0; i < x_ref.rows(); ++i) {
38     if (i > 0) {
39       *o << ',';
40     }
41     *o << '[';
42     for (int j = 0; j < x_ref.cols(); ++j) {
43       if (j > 0) {
44         *o << ',';
45       }
46       stan_print(o, x_ref.coeff(i, j));
47     }
48     *o << ']';
49   }
50   *o << ']';
51 }
52 
53 template <typename T>
stan_print(std::ostream * o,const std::vector<T> & x)54 void stan_print(std::ostream* o, const std::vector<T>& x) {
55   *o << '[';
56   for (size_t i = 0; i < x.size(); ++i) {
57     if (i > 0) {
58       *o << ',';
59     }
60     stan_print(o, x[i]);
61   }
62   *o << ']';
63 }
64 
65 }  // namespace math
66 }  // namespace stan
67 #endif
68