Automatic Differentiation
 
Loading...
Searching...
No Matches
stan_print.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_STAN_PRINT_HPP
2#define STAN_MATH_PRIM_FUN_STAN_PRINT_HPP
3
7#include <vector>
8
9namespace stan {
10namespace math {
11// prints used in generator for print() statements in modeling language
12
13template <typename T, require_not_container_t<T>* = nullptr,
14 require_not_tuple_t<T>* = nullptr>
15void stan_print(std::ostream* o, const T& x) {
16 *o << x;
17}
18
19template <typename EigVec, require_eigen_vector_t<EigVec>* = nullptr>
20void stan_print(std::ostream* o, const EigVec& x) {
21 const auto& x_ref = to_ref(x);
22
23 *o << '[';
24 for (int i = 0; i < x_ref.size(); ++i) {
25 if (i > 0) {
26 *o << ',';
27 }
28 stan_print(o, x_ref.coeff(i));
29 }
30 *o << ']';
31}
32
33template <typename EigMat, require_eigen_t<EigMat>* = nullptr,
34 require_not_eigen_vector_t<EigMat>* = nullptr>
35void stan_print(std::ostream* o, const EigMat& x) {
36 const auto& x_ref = to_ref(x);
37
38 *o << '[';
39 for (int i = 0; i < x_ref.rows(); ++i) {
40 if (i > 0) {
41 *o << ',';
42 }
43 *o << '[';
44 for (int j = 0; j < x_ref.cols(); ++j) {
45 if (j > 0) {
46 *o << ',';
47 }
48 stan_print(o, x_ref.coeff(i, j));
49 }
50 *o << ']';
51 }
52 *o << ']';
53}
54
55// forward decl to allow the next two overloads to call each other
56template <typename T, require_tuple_t<T>* = nullptr>
57void stan_print(std::ostream* o, const T& x);
58
59template <typename T, require_std_vector_t<T>* = nullptr>
60void stan_print(std::ostream* o, const T& x) {
61 *o << '[';
62 for (size_t i = 0; i < x.size(); ++i) {
63 if (i > 0) {
64 *o << ',';
65 }
66 stan_print(o, x[i]);
67 }
68 *o << ']';
69}
70
71template <typename T, require_tuple_t<T>*>
72void stan_print(std::ostream* o, const T& x) {
73 *o << '(';
74 size_t i = 0;
76 [&i, o](auto&& elt) {
77 if (i > 0) {
78 *o << ',';
79 }
80 stan_print(o, elt);
81 i++;
82 },
83 x);
84 *o << ')';
85}
86
87} // namespace math
88} // namespace stan
89#endif
constexpr auto for_each(F &&f, T &&t)
Apply a function to each element of a tuple.
Definition for_each.hpp:66
void stan_print(std::ostream *o, const T &x)
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...