Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guards to promote_scalar to allow similar tuples #2786

Merged
merged 1 commit into from
Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion stan/math/prim/fun/promote_scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace math {
*/
template <typename PromotionScalar, typename UnPromotedType,
require_constructible_t<PromotionScalar, UnPromotedType>* = nullptr,
require_not_same_t<PromotionScalar, UnPromotedType>* = nullptr>
require_not_same_t<PromotionScalar, UnPromotedType>* = nullptr,
require_all_not_tuple_t<PromotionScalar, UnPromotedType>* = nullptr>
inline constexpr auto promote_scalar(UnPromotedType&& x) {
return PromotionScalar(std::forward<UnPromotedType>(x));
}
Expand Down
20 changes: 20 additions & 0 deletions test/unit/math/mix/fun/promote_scalar_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void test_promote_scalar() {
inner_promo_type>>(tester);
stan::math::test::expect_same_value_of_rec(tester, result);
}

TEST(mixFun, promote_scalar_tuple) {
using stan::math::fvar;
using stan::math::var;
Expand All @@ -83,3 +84,22 @@ TEST(mixFun, promote_scalar_tuple) {
test_promote_scalar<std::complex<var>, double>();
test_promote_scalar<std::complex<fvar<var>>, double>();
}

template <typename UnPromotedType>
void test_promote_scalar_basic() {
std::tuple<UnPromotedType, UnPromotedType> x{UnPromotedType(3.5),
UnPromotedType(4.5)};
std::tuple<std::complex<UnPromotedType>, UnPromotedType> z
= stan::math::promote_scalar<
std::tuple<std::complex<UnPromotedType>, UnPromotedType>>(x);
stan::math::test::expect_same_value_of_rec(x, z);
}

TEST(mixFun, promote_scalar_tuple_basic) {
using stan::math::fvar;
using stan::math::var;
test_promote_scalar_basic<double>();
test_promote_scalar_basic<var>();
test_promote_scalar_basic<fvar<double>>();
test_promote_scalar_basic<fvar<var>>();
}