Skip to content

Commit a2ff6df

Browse files
authored
Merge pull request #2436 from stan-dev/bugfix/issue-2435
Fix 2435
2 parents 21ed6f2 + f6fa415 commit a2ff6df

3 files changed

Lines changed: 96 additions & 5 deletions

File tree

stan/math/prim/fun/stan_print.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,34 @@ void stan_print(std::ostream* o, const T& x) {
1616

1717
template <typename EigVec, require_eigen_vector_t<EigVec>* = nullptr>
1818
void stan_print(std::ostream* o, const EigVec& x) {
19+
const auto& x_ref = to_ref(x);
20+
1921
*o << '[';
20-
for (int i = 0; i < x.size(); ++i) {
22+
for (int i = 0; i < x_ref.size(); ++i) {
2123
if (i > 0) {
2224
*o << ',';
2325
}
24-
stan_print(o, x(i));
26+
stan_print(o, x_ref.coeff(i));
2527
}
2628
*o << ']';
2729
}
2830

2931
template <typename EigMat, require_eigen_t<EigMat>* = nullptr,
3032
require_not_eigen_vector_t<EigMat>* = nullptr>
3133
void stan_print(std::ostream* o, const EigMat& x) {
34+
const auto& x_ref = to_ref(x);
35+
3236
*o << '[';
33-
for (int i = 0; i < x.rows(); ++i) {
37+
for (int i = 0; i < x_ref.rows(); ++i) {
3438
if (i > 0) {
3539
*o << ',';
3640
}
3741
*o << '[';
38-
for (int j = 0; j < x.cols(); ++j) {
42+
for (int j = 0; j < x_ref.cols(); ++j) {
3943
if (j > 0) {
4044
*o << ',';
4145
}
42-
stan_print(o, x.coeff(i, j));
46+
stan_print(o, x_ref.coeff(i, j));
4347
}
4448
*o << ']';
4549
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stan/math/prim.hpp>
2+
#include <gtest/gtest.h>
3+
4+
TEST(MathPrim, basic_print) {
5+
int i = 1;
6+
double d = 1.0;
7+
Eigen::VectorXd v = Eigen::VectorXd::Ones(1);
8+
Eigen::RowVectorXd rv = Eigen::RowVectorXd::Ones(1);
9+
Eigen::MatrixXd m = Eigen::MatrixXd::Ones(1, 1);
10+
std::vector<double> a(1, 1);
11+
12+
{
13+
std::stringstream s;
14+
stan::math::stan_print(&s, i);
15+
EXPECT_TRUE(s.str().find("1") != std::string::npos);
16+
}
17+
18+
{
19+
std::stringstream s;
20+
stan::math::stan_print(&s, d);
21+
EXPECT_TRUE(s.str().find("1") != std::string::npos);
22+
}
23+
24+
{
25+
std::stringstream s;
26+
stan::math::stan_print(&s, v);
27+
EXPECT_TRUE(s.str().find("[1]") != std::string::npos);
28+
}
29+
30+
{
31+
std::stringstream s;
32+
stan::math::stan_print(&s, rv);
33+
EXPECT_TRUE(s.str().find("[1]") != std::string::npos);
34+
}
35+
36+
{
37+
std::stringstream s;
38+
stan::math::stan_print(&s, m);
39+
EXPECT_TRUE(s.str().find("[[1]]") != std::string::npos);
40+
}
41+
42+
{
43+
std::stringstream s;
44+
stan::math::stan_print(&s, a);
45+
EXPECT_TRUE(s.str().find("[1]") != std::string::npos);
46+
}
47+
}
48+
49+
TEST(MathPrim, basic_expressions) {
50+
Eigen::VectorXd v = Eigen::VectorXd::Ones(2);
51+
Eigen::RowVectorXd rv = Eigen::RowVectorXd::Ones(2);
52+
Eigen::MatrixXd m = Eigen::MatrixXd::Ones(2, 2);
53+
54+
{
55+
std::stringstream s;
56+
stan::math::stan_print(&s, v * v.transpose());
57+
EXPECT_TRUE(s.str().find("[[1,1],[1,1]]") != std::string::npos);
58+
}
59+
60+
{
61+
std::stringstream s;
62+
stan::math::stan_print(&s, rv * m);
63+
EXPECT_TRUE(s.str().find("[2,2]") != std::string::npos);
64+
}
65+
66+
{
67+
std::stringstream s;
68+
stan::math::stan_print(&s, m * m);
69+
EXPECT_TRUE(s.str().find("[[2,2],[2,2]]") != std::string::npos);
70+
}
71+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stan/math/rev.hpp>
2+
#include <test/unit/math/rev/fun/util.hpp>
3+
#include <test/unit/util.hpp>
4+
#include <gtest/gtest.h>
5+
6+
TEST(AgradRev, stan_print) {
7+
using stan::math::var;
8+
9+
var a = 5.0;
10+
11+
{
12+
std::stringstream s;
13+
stan::math::stan_print(&s, a);
14+
EXPECT_TRUE(s.str().find("5") != std::string::npos);
15+
}
16+
}

0 commit comments

Comments
 (0)