Skip to content

Commit f98e093

Browse files
authored
Merge pull request #3228 from stan-dev/fix/exprs-apply
update bessel so that reverse mode is chosen over the perfect forward…
2 parents a2d0372 + 7f655b7 commit f98e093

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

stan/math/prim/fun/bessel_second_kind.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ inline T2 bessel_second_kind(int v, const T2 z) {
5252
* @param b Second input
5353
* @return Bessel second kind function applied to the two inputs.
5454
*/
55-
template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr>
55+
template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr,
56+
require_not_var_matrix_t<T2>* = nullptr>
5657
inline auto bessel_second_kind(T1&& a, T2&& b) {
5758
return apply_scalar_binary(
5859
[](auto&& c, auto&& d) {

stan/math/prim/functor/apply_scalar_unary.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stan/math/prim/fun/Eigen.hpp>
55
#include <stan/math/prim/meta/is_eigen.hpp>
66
#include <stan/math/prim/meta/is_complex.hpp>
7+
#include <stan/math/prim/meta/holder.hpp>
78
#include <stan/math/prim/meta/require_generics.hpp>
89
#include <stan/math/prim/meta/is_vector.hpp>
910
#include <stan/math/prim/meta/is_vector_like.hpp>

stan/math/rev/fun/bessel_second_kind.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
namespace stan {
99
namespace math {
1010

11-
inline var bessel_second_kind(int v, const var& a) {
11+
template <typename T1, typename T2, require_integral_t<T1>* = nullptr,
12+
require_var_t<T2>* = nullptr, require_stan_scalar_t<T2>* = nullptr>
13+
inline var bessel_second_kind(T1&& v, T2&& a) {
1214
double ret_val = bessel_second_kind(v, a.val());
1315
auto precomp_bessel
1416
= v * ret_val / a.val() - bessel_second_kind(v + 1, a.val());
@@ -22,8 +24,8 @@ inline var bessel_second_kind(int v, const var& a) {
2224
* `std::vector<std::vector<int>>`
2325
*/
2426
template <typename T1, typename T2, require_st_integral<T1>* = nullptr,
25-
require_eigen_t<T2>* = nullptr>
26-
inline auto bessel_second_kind(const T1& v, const var_value<T2>& a) {
27+
require_var_matrix_t<T2>* = nullptr>
28+
inline auto bessel_second_kind(T1&& v, T2&& a) {
2729
auto ret_val = bessel_second_kind(v, a.val()).array().eval();
2830
auto v_map = as_array_or_scalar(v);
2931
auto precomp_bessel

test/unit/math/mix/fun/bessel_second_kind_test.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
TEST(mathMixScalFun, besselSecondKind) {
55
// bind integer arg because can't autodiff through
6-
auto f = [](const int x1) {
6+
auto f = [](const auto& x1) {
77
return
88
[=](const auto& x2) { return stan::math::bessel_second_kind(x1, x2); };
99
};
@@ -13,6 +13,13 @@ TEST(mathMixScalFun, besselSecondKind) {
1313
stan::test::expect_ad(f(1), 3.0);
1414
stan::test::expect_ad(f(1), std::numeric_limits<double>::quiet_NaN());
1515
stan::test::expect_ad(f(2), 2.79);
16+
std::vector<int> std_in1{3, 1};
17+
stan::test::expect_ad(f(std_in1), 4.0);
18+
stan::test::expect_ad(f(std_in1), std::numeric_limits<double>::quiet_NaN());
19+
stan::test::expect_ad(f(std_in1), -3.0);
20+
stan::test::expect_ad(f(std_in1), 3.0);
21+
stan::test::expect_ad(f(std_in1), std::numeric_limits<double>::quiet_NaN());
22+
stan::test::expect_ad(f(std_in1), 2.79);
1623
}
1724

1825
TEST(mathMixScalFun, besselSecondKind_vec) {
@@ -25,10 +32,12 @@ TEST(mathMixScalFun, besselSecondKind_vec) {
2532
Eigen::VectorXd in2(2);
2633
in2 << 0.5, 3.4;
2734
stan::test::expect_ad_vectorized_binary(f, std_in1, in2);
35+
stan::test::expect_ad_matvar(f, std_in1, in2);
2836

2937
std::vector<std::vector<int>> std_std_in1{std_in1, std_in1};
3038
Eigen::MatrixXd mat_in2 = in2.replicate(1, 2);
3139
stan::test::expect_ad_vectorized_binary(f, std_std_in1, mat_in2);
40+
stan::test::expect_ad_matvar(f, std_std_in1, mat_in2);
3241
}
3342

3443
TEST(mathMixScalFun, besselSecondKind_matvec) {

0 commit comments

Comments
 (0)