Skip to content

Commit 987ffae

Browse files
authored
Merge pull request #1644 from bstatcomp/fix_returned_expressions
fix returned expressions
2 parents fd21187 + 9689cb6 commit 987ffae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+415
-30
lines changed

stan/math/prim/fun/acos.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto acos(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto acos(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().acos().matrix();
48+
return x.derived().array().acos().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/fun/asin.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto asin(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto asin(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().asin().matrix();
48+
return x.derived().array().asin().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/fun/atan.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline typename apply_scalar_unary<atan_fun, T>::return_t atan(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto atan(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().atan().matrix();
48+
return x.derived().array().atan().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/fun/ceil.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto ceil(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto ceil(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().ceil().matrix();
48+
return x.derived().array().ceil().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/fun/cos.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto cos(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto cos(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().cos().matrix();
48+
return x.derived().array().cos().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/fun/cosh.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline typename apply_scalar_unary<cosh_fun, T>::return_t cosh(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto cosh(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().cosh().matrix();
48+
return x.derived().array().cosh().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/fun/exp.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ inline auto exp(const T& x) {
6060
template <typename Derived,
6161
typename = require_eigen_vt<std::is_arithmetic, Derived>>
6262
inline auto exp(const Eigen::MatrixBase<Derived>& x) {
63-
return x.derived().array().exp().matrix();
63+
return x.derived().array().exp().matrix().eval();
6464
}
6565

6666
} // namespace math

stan/math/prim/fun/fabs.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline typename apply_scalar_unary<fabs_fun, T>::return_t fabs(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto fabs(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().abs().matrix();
48+
return x.derived().array().abs().matrix().eval();
4949
}
5050

5151
/**
@@ -57,7 +57,7 @@ inline auto fabs(const Eigen::MatrixBase<Derived>& x) {
5757
template <typename Derived,
5858
typename = require_eigen_vt<std::is_arithmetic, Derived>>
5959
inline auto fabs(const Eigen::ArrayBase<Derived>& x) {
60-
return x.derived().abs();
60+
return x.derived().abs().eval();
6161
}
6262

6363
} // namespace math

stan/math/prim/fun/floor.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto floor(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto floor(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().floor().matrix();
48+
return x.derived().array().floor().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/fun/inv.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto inv(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto inv(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().inverse().matrix();
48+
return x.derived().array().inverse().matrix().eval();
4949
}
5050

5151
/**
@@ -57,7 +57,7 @@ inline auto inv(const Eigen::MatrixBase<Derived>& x) {
5757
template <typename Derived,
5858
typename = require_eigen_vt<std::is_arithmetic, Derived>>
5959
inline auto inv(const Eigen::ArrayBase<Derived>& x) {
60-
return x.derived().inverse();
60+
return x.derived().inverse().eval();
6161
}
6262

6363
} // namespace math

stan/math/prim/fun/inv_cloglog.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ inline auto inv_cloglog(const T& x) {
8787
template <typename Derived,
8888
typename = require_eigen_vt<std::is_arithmetic, Derived>>
8989
inline auto inv_cloglog(const Eigen::MatrixBase<Derived>& x) {
90-
return (1 - exp(-exp(x.derived().array()))).matrix();
90+
return (1 - exp(-exp(x.derived().array()))).matrix().eval();
9191
}
9292

9393
/**
@@ -99,7 +99,7 @@ inline auto inv_cloglog(const Eigen::MatrixBase<Derived>& x) {
9999
template <typename Derived,
100100
typename = require_eigen_vt<std::is_arithmetic, Derived>>
101101
inline auto inv_cloglog(const Eigen::ArrayBase<Derived>& x) {
102-
return 1 - exp(-exp(x.derived()));
102+
return (1 - exp(-exp(x.derived()))).eval();
103103
}
104104

105105
} // namespace math

stan/math/prim/fun/inv_sqrt.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ inline auto inv_sqrt(const T& x) {
5050
template <typename Derived,
5151
typename = require_eigen_vt<std::is_arithmetic, Derived>>
5252
inline auto inv_sqrt(const Eigen::MatrixBase<Derived>& x) {
53-
return x.derived().array().rsqrt().matrix();
53+
return x.derived().array().rsqrt().matrix().eval();
5454
}
5555

5656
/**
@@ -62,7 +62,7 @@ inline auto inv_sqrt(const Eigen::MatrixBase<Derived>& x) {
6262
template <typename Derived,
6363
typename = require_eigen_vt<std::is_arithmetic, Derived>>
6464
inline auto inv_sqrt(const Eigen::ArrayBase<Derived>& x) {
65-
return x.derived().rsqrt();
65+
return x.derived().rsqrt().eval();
6666
}
6767

6868
} // namespace math

stan/math/prim/fun/log.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ inline auto log(const T& x) {
6060
template <typename Derived,
6161
typename = require_eigen_vt<std::is_arithmetic, Derived>>
6262
inline auto log(const Eigen::MatrixBase<Derived>& x) {
63-
return x.derived().array().log().matrix();
63+
return x.derived().array().log().matrix().eval();
6464
}
6565

6666
} // namespace math

stan/math/prim/fun/log10.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto log10(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto log10(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().log10().matrix();
48+
return x.derived().array().log10().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/fun/log_softmax.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ template <typename T, require_t<std::is_arithmetic<scalar_type_t<T>>>...>
4141
inline auto log_softmax(const T& x) {
4242
return apply_vector_unary<T>::apply(x, [&](const auto& v) {
4343
check_nonzero_size("log_softmax", "v", v);
44-
return (v.array() - log_sum_exp(v)).matrix();
44+
return (v.array() - log_sum_exp(v)).matrix().eval();
4545
});
4646
}
4747
} // namespace math

stan/math/prim/fun/minus.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef STAN_MATH_PRIM_FUN_MINUS_HPP
22
#define STAN_MATH_PRIM_FUN_MINUS_HPP
33

4+
#include <stan/math/prim/meta.hpp>
5+
46
namespace stan {
57
namespace math {
68

@@ -12,7 +14,7 @@ namespace math {
1214
* @return Negation of subtrahend.
1315
*/
1416
template <typename T>
15-
inline auto minus(const T& x) {
17+
inline plain_type_t<T> minus(const T& x) {
1618
return -x;
1719
}
1820

stan/math/prim/fun/round.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ inline auto round(const T& x) {
6262
template <typename Derived,
6363
typename = require_eigen_vt<std::is_arithmetic, Derived>>
6464
inline auto round(const Eigen::MatrixBase<Derived>& x) {
65-
return x.derived().array().round().matrix();
65+
return x.derived().array().round().matrix().eval();
6666
}
6767

6868
} // namespace math

stan/math/prim/fun/sin.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto sin(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto sin(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().sin().matrix();
48+
return x.derived().array().sin().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/fun/sinh.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto sinh(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto sinh(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().sinh().matrix();
48+
return x.derived().array().sinh().matrix().eval();
4949
}
5050

5151
/**
@@ -57,7 +57,7 @@ inline auto sinh(const Eigen::MatrixBase<Derived>& x) {
5757
template <typename Derived,
5858
typename = require_eigen_vt<std::is_arithmetic, Derived>>
5959
inline auto sinh(const Eigen::ArrayBase<Derived>& x) {
60-
return x.derived().sinh();
60+
return x.derived().sinh().eval();
6161
}
6262

6363
} // namespace math

stan/math/prim/fun/sqrt.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ inline auto sqrt(const T& x) {
5454
template <typename Derived,
5555
typename = require_eigen_vt<std::is_arithmetic, Derived>>
5656
inline auto sqrt(const Eigen::MatrixBase<Derived>& x) {
57-
return x.derived().array().sqrt().matrix();
57+
return x.derived().array().sqrt().matrix().eval();
5858
}
5959

6060
} // namespace math

stan/math/prim/fun/tan.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto tan(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto tan(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().tan().matrix();
48+
return x.derived().array().tan().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/fun/tanh.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline auto tanh(const T& x) {
4545
template <typename Derived,
4646
typename = require_eigen_vt<std::is_arithmetic, Derived>>
4747
inline auto tanh(const Eigen::MatrixBase<Derived>& x) {
48-
return x.derived().array().tanh().matrix();
48+
return x.derived().array().tanh().matrix().eval();
4949
}
5050

5151
} // namespace math

stan/math/prim/vectorize/apply_scalar_unary.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ struct apply_scalar_unary<F, T, require_eigen_t<T>> {
5959
* by F to the specified matrix.
6060
*/
6161
static inline auto apply(const T& x) {
62-
return x.unaryExpr(
63-
[](scalar_t x) { return apply_scalar_unary<F, scalar_t>::apply(x); });
62+
return x
63+
.unaryExpr([](scalar_t x) {
64+
return apply_scalar_unary<F, scalar_t>::apply(x);
65+
})
66+
.eval();
6467
}
6568

6669
/**

test/unit/math/prim/fun/Phi_approx_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ TEST(MathFunctions, Phi_approx_nan) {
1414

1515
EXPECT_TRUE(std::isnan(stan::math::Phi_approx(nan)));
1616
}
17+
18+
TEST(MathFunctions, Phi_approx__works_with_other_functions) {
19+
Eigen::VectorXd a(5);
20+
a << 1.1, 1.2, 1.3, 1.4, 1.5;
21+
Eigen::RowVectorXd b(5);
22+
b << 1.1, 1.2, 1.3, 1.4, 1.5;
23+
stan::math::multiply(a, stan::math::Phi_approx(b));
24+
}

test/unit/math/prim/fun/Phi_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,11 @@ TEST(MathFunctions, Phi_nan) {
118118
double nan = std::numeric_limits<double>::quiet_NaN();
119119
EXPECT_THROW(stan::math::Phi(nan), std::domain_error);
120120
}
121+
122+
TEST(MathFunctions, Phi_works_with_other_functions) {
123+
Eigen::VectorXd a(5);
124+
a << 1.1, 1.2, 1.3, 1.4, 1.5;
125+
Eigen::RowVectorXd b(5);
126+
b << 1.1, 1.2, 1.3, 1.4, 1.5;
127+
stan::math::multiply(a, stan::math::Phi(b));
128+
}

test/unit/math/prim/fun/acos_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ TEST(primScalFun, acos) {
55
stan::test::expect_common_prim([](auto x) { return std::acos(x); },
66
[](auto x) { return stan::math::acos(x); });
77
}
8+
9+
TEST(MathFunctions, acos_works_with_other_functions) {
10+
Eigen::VectorXd a(5);
11+
a << 0.1, 0.2, 0.3, 0.4, 0.5;
12+
Eigen::RowVectorXd b(5);
13+
b << 0.1, 0.2, 0.3, 0.4, 0.5;
14+
stan::math::multiply(a, stan::math::acos(b));
15+
}

test/unit/math/prim/fun/acosh_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ TEST(MathFunctions, acosh_nan) {
2626
double nan = std::numeric_limits<double>::quiet_NaN();
2727
EXPECT_TRUE(std::isnan(stan::math::acosh(nan)));
2828
}
29+
30+
TEST(MathFunctions, acosh_works_with_other_functions) {
31+
Eigen::VectorXd a(5);
32+
a << 1.1, 1.2, 1.3, 1.4, 1.5;
33+
Eigen::RowVectorXd b(5);
34+
b << 1.1, 1.2, 1.3, 1.4, 1.5;
35+
stan::math::multiply(a, stan::math::acosh(b));
36+
}

test/unit/math/prim/fun/asin_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ TEST(primScalFun, asin) {
55
stan::test::expect_common_prim([](auto x) { return std::asin(x); },
66
[](auto x) { return stan::math::asin(x); });
77
}
8+
9+
TEST(MathFunctions, asin_works_with_other_functions) {
10+
Eigen::VectorXd a(5);
11+
a << 0.1, 0.2, 0.3, 0.4, 0.5;
12+
Eigen::RowVectorXd b(5);
13+
b << 0.1, 0.2, 0.3, 0.4, 0.5;
14+
stan::math::multiply(a, stan::math::asin(b));
15+
}

test/unit/math/prim/fun/asinh_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ TEST(MathFunctions, asinh_nan) {
2222
double nan = std::numeric_limits<double>::quiet_NaN();
2323
EXPECT_TRUE(std::isnan(stan::math::asinh(nan)));
2424
}
25+
26+
TEST(MathFunctions, asinh_works_with_other_functions) {
27+
Eigen::VectorXd a(5);
28+
a << 1.1, 1.2, 1.3, 1.4, 1.5;
29+
Eigen::RowVectorXd b(5);
30+
b << 1.1, 1.2, 1.3, 1.4, 1.5;
31+
stan::math::multiply(a, stan::math::asinh(b));
32+
}

test/unit/math/prim/fun/atan_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ TEST(primScalFun, atan) {
55
stan::test::expect_common_prim([](auto x) { return std::atan(x); },
66
[](auto x) { return stan::math::atan(x); });
77
}
8+
9+
TEST(MathFunctions, atan_works_with_other_functions) {
10+
Eigen::VectorXd a(5);
11+
a << 1.1, 1.2, 1.3, 1.4, 1.5;
12+
Eigen::RowVectorXd b(5);
13+
b << 1.1, 1.2, 1.3, 1.4, 1.5;
14+
stan::math::multiply(a, stan::math::atan(b));
15+
}

test/unit/math/prim/fun/atanh_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ TEST(MathFunctions, atanh_nan) {
2727
double nan = std::numeric_limits<double>::quiet_NaN();
2828
EXPECT_TRUE(std::isnan(stan::math::atanh(nan)));
2929
}
30+
31+
TEST(MathFunctions, atanh_works_with_other_functions) {
32+
Eigen::VectorXd a(5);
33+
a << 0.1, 0.2, 0.3, 0.4, 0.5;
34+
Eigen::RowVectorXd b(5);
35+
b << 0.1, 0.2, 0.3, 0.4, 0.5;
36+
stan::math::multiply(a, stan::math::atanh(b));
37+
}

test/unit/math/prim/fun/cbrt_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ TEST(MathFunctions, cbrt_nan) {
2424
double nan = std::numeric_limits<double>::quiet_NaN();
2525
EXPECT_TRUE(std::isnan(stan::math::cbrt(nan)));
2626
}
27+
28+
TEST(MathFunctions, cbrt_works_with_other_functions) {
29+
Eigen::VectorXd a(5);
30+
a << 1.1, 1.2, 1.3, 1.4, 1.5;
31+
Eigen::RowVectorXd b(5);
32+
b << 1.1, 1.2, 1.3, 1.4, 1.5;
33+
stan::math::multiply(a, stan::math::cbrt(b));
34+
}

test/unit/math/prim/fun/ceil_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ TEST(primScalFun, ceil) {
44
stan::test::expect_common_prim([](auto x) { return std::ceil(x); },
55
[](auto x) { return stan::math::ceil(x); });
66
}
7+
8+
TEST(MathFunctions, ceil_works_with_other_functions) {
9+
Eigen::VectorXd a(5);
10+
a << 1.1, 1.2, 1.3, 1.4, 1.5;
11+
Eigen::RowVectorXd b(5);
12+
b << 1.1, 1.2, 1.3, 1.4, 1.5;
13+
stan::math::multiply(a, stan::math::ceil(b));
14+
}

test/unit/math/prim/fun/cos_test.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ TEST(primScalFun, cos) {
55
stan::test::expect_common_prim([](auto x) { return std::cos(x); },
66
[](auto x) { return stan::math::cos(x); });
77
}
8+
9+
TEST(MathFunctions, cos_works_with_other_functions) {
10+
Eigen::VectorXd a(5);
11+
a << 1.1, 1.2, 1.3, 1.4, 1.5;
12+
Eigen::RowVectorXd b(5);
13+
b << 1.1, 1.2, 1.3, 1.4, 1.5;
14+
stan::math::multiply(a, stan::math::cos(b));
15+
}

0 commit comments

Comments
 (0)