diff --git a/test/unit/math/prim/functor/ode_rk45_prim_test.cpp b/test/unit/math/prim/functor/ode_rk45_prim_test.cpp index 5821c3cf18e..d0703a7c22d 100644 --- a/test/unit/math/prim/functor/ode_rk45_prim_test.cpp +++ b/test/unit/math/prim/functor/ode_rk45_prim_test.cpp @@ -1,68 +1,10 @@ #include #include #include +#include #include #include -namespace ode_rk45_prim_test { - -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - -struct CosArgWrongSize { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(2); - out << stan::math::cos(sum_(vec) * t), 0; - return out; - } -}; -} // namespace ode_rk45_prim_test - TEST(ode_rk45_prim, y0_errors) { Eigen::VectorXd y0 = Eigen::VectorXd::Zero(1); Eigen::VectorXd y0inf(1); @@ -75,20 +17,20 @@ TEST(ode_rk45_prim, y0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0inf, t0, - ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0inf, t0, ts, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0NaN, t0, - ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0NaN, t0, ts, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0_empty, t0, - ts, nullptr, a), - std::invalid_argument); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0_empty, t0, ts, nullptr, a), + std::invalid_argument); } TEST(ode_rk45_prim, t0_errors) { @@ -100,16 +42,16 @@ TEST(ode_rk45_prim, t0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0inf, - ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0inf, ts, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0NaN, - ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0NaN, ts, nullptr, a), + std::domain_error); } TEST(ode_rk45_prim, ts_errors) { @@ -127,38 +69,38 @@ TEST(ode_rk45_prim, ts_errors) { double a = 1.5; std::vector out; - EXPECT_NO_THROW(out = stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, - t0, ts, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, + nullptr, a)); EXPECT_EQ(out.size(), ts.size()); - EXPECT_NO_THROW(out = stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, - t0, ts_repeat, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, + ts_repeat, nullptr, a)); EXPECT_EQ(out.size(), ts_repeat.size()); EXPECT_MATRIX_FLOAT_EQ(out[0], out[1]); - EXPECT_NO_THROW(out = stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, - t0, ts_lots, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, + ts_lots, nullptr, a)); EXPECT_EQ(out.size(), ts_lots.size()); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts_empty, nullptr, a), - std::invalid_argument); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts_empty, nullptr, a), + std::invalid_argument); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts_early, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts_early, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, + EXPECT_THROW(stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts_decreasing, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - tsinf, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, tsinf, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - tsNaN, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, tsNaN, nullptr, a), + std::domain_error); } TEST(ode_rk45_prim, one_arg_errors) { @@ -189,60 +131,60 @@ TEST(ode_rk45_prim, one_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, ts, - nullptr, ainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, ainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, ts, - nullptr, aNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, aNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts, nullptr, va)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, va)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, ts, - nullptr, vainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, vainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, ts, - nullptr, vaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, vaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts, nullptr, ea)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, ea)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, ts, - nullptr, eainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, eainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, ts, - nullptr, eaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, eaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts, nullptr, vva)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, vva)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, ts, - nullptr, vvainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, vvainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, ts, - nullptr, vvaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, vvaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts, nullptr, vea)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, vea)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, ts, - nullptr, veainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, veainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, ts, - nullptr, veaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, veaNaN), + std::domain_error); } TEST(ode_rk45_prim, two_arg_errors) { @@ -273,59 +215,59 @@ TEST(ode_rk45_prim, two_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, - ts, nullptr, a, a)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, a)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, ts, - nullptr, a, ainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, ainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, ts, - nullptr, a, aNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, aNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, - ts, nullptr, a, va)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, va)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, ts, - nullptr, a, vainf), + EXPECT_THROW(stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, vainf), std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, ts, - nullptr, a, vaNaN), + EXPECT_THROW(stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, vaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, - ts, nullptr, a, ea)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, ea)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, ts, - nullptr, a, eainf), + EXPECT_THROW(stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, eainf), std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, ts, - nullptr, a, eaNaN), + EXPECT_THROW(stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, eaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, - ts, nullptr, a, vva)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, vva)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, ts, - nullptr, a, vvainf), + EXPECT_THROW(stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, vvainf), std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, ts, - nullptr, a, vvaNaN), + EXPECT_THROW(stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, vvaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, - ts, nullptr, a, vea)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, vea)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, ts, - nullptr, a, veainf), + EXPECT_THROW(stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, veainf), std::domain_error); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::Cos2Arg(), y0, t0, ts, - nullptr, a, veaNaN), + EXPECT_THROW(stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, veaNaN), std::domain_error); } @@ -336,11 +278,11 @@ TEST(ode_rk45_prim, rhs_wrong_size_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_rk45(ode_rk45_prim_test::CosArgWrongSize(), y0, - t0, ts, nullptr, a), + EXPECT_THROW(stan::math::ode_rk45(stan::test::CosArgWrongSize(), y0, t0, ts, + nullptr, a), std::invalid_argument); } @@ -351,7 +293,7 @@ TEST(ode_rk45_prim, error_name) { double ainf = stan::math::INFTY; - EXPECT_THROW_MSG(stan::math::ode_rk45(ode_rk45_prim_test::CosArg1(), y0, t0, - ts, nullptr, ainf), - std::domain_error, "ode_rk45"); + EXPECT_THROW_MSG( + stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, ainf), + std::domain_error, "ode_rk45"); } diff --git a/test/unit/math/prim/functor/ode_rk45_tol_prim_test.cpp b/test/unit/math/prim/functor/ode_rk45_tol_prim_test.cpp index 74c95c10605..a1fd6c07e19 100644 --- a/test/unit/math/prim/functor/ode_rk45_tol_prim_test.cpp +++ b/test/unit/math/prim/functor/ode_rk45_tol_prim_test.cpp @@ -1,65 +1,9 @@ #include #include #include +#include #include #include -namespace ode_rk45_tol_prim_test { -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - -struct CosArgWrongSize { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(2); - out << stan::math::cos(sum_(vec) * t), 0; - return out; - } -}; -} // namespace ode_rk45_tol_prim_test TEST(ode_rk45_tol_prim, y0_errors) { Eigen::VectorXd y0 = Eigen::VectorXd::Zero(1); @@ -73,24 +17,20 @@ TEST(ode_rk45_tol_prim, y0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0inf, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0inf, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0NaN, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0NaN, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0_empty, t0, - ts, 1e-10, 1e-10, 1e6, nullptr, a), - std::invalid_argument); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0_empty, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a), + std::invalid_argument); } TEST(ode_rk45_tol_prim, t0_errors) { @@ -102,19 +42,16 @@ TEST(ode_rk45_tol_prim, t0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0inf, ts, - 1e-10, 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0inf, ts, + 1e-10, 1e-10, 1e6, nullptr, a), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0NaN, ts, - 1e-10, 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0NaN, ts, + 1e-10, 1e-10, 1e6, nullptr, a), + std::domain_error); } TEST(ode_rk45_tol_prim, ts_errors) { @@ -132,46 +69,42 @@ TEST(ode_rk45_tol_prim, ts_errors) { double a = 1.5; std::vector out; - EXPECT_NO_THROW( - out = stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, - ts, 1e-10, 1e-10, 1e6, nullptr, a)); + EXPECT_NO_THROW(out + = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); EXPECT_EQ(out.size(), ts.size()); - EXPECT_NO_THROW( - out = stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, - ts_repeat, 1e-10, 1e-10, 1e6, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, + ts_repeat, 1e-10, 1e-10, 1e6, + nullptr, a)); EXPECT_EQ(out.size(), ts_repeat.size()); EXPECT_MATRIX_FLOAT_EQ(out[0], out[1]); - EXPECT_NO_THROW( - out = stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, - ts_lots, 1e-10, 1e-10, 1e6, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, + ts_lots, 1e-10, 1e-10, 1e6, + nullptr, a)); EXPECT_EQ(out.size(), ts_lots.size()); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, - ts_empty, 1e-10, 1e-10, 1e6, nullptr, a), - std::invalid_argument); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts_empty, + 1e-10, 1e-10, 1e6, nullptr, a), + std::invalid_argument); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, - ts_early, 1e-10, 1e-10, 1e6, nullptr, a), - std::domain_error); - - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, - ts_decreasing, 1e-10, 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts_early, + 1e-10, 1e-10, 1e6, nullptr, a), + std::domain_error); EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, tsinf, + stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts_decreasing, 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, tsNaN, - 1e-10, 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, tsinf, + 1e-10, 1e-10, 1e6, nullptr, a), + std::domain_error); + + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, tsNaN, + 1e-10, 1e-10, 1e6, nullptr, a), + std::domain_error); } TEST(ode_rk45_tol_prim, one_arg_errors) { @@ -202,75 +135,60 @@ TEST(ode_rk45_tol_prim, one_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, ainf), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, ainf), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, aNaN), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, aNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, va)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, va)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, vainf), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vainf), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, vaNaN), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, ea)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, ea)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, eainf), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, eainf), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, eaNaN), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, eaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, vva)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vva)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, vvainf), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vvainf), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, vvaNaN), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vvaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, vea)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vea)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, veainf), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, veainf), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, veaNaN), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, veaNaN), + std::domain_error); } TEST(ode_rk45_tol_prim, two_arg_errors) { @@ -301,75 +219,60 @@ TEST(ode_rk45_tol_prim, two_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, a)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, a)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a, ainf), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, ainf), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a, aNaN), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, aNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, va)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, va)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a, vainf), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vainf), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a, vaNaN), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, ea)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, ea)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a, eainf), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, eainf), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a, eaNaN), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, eaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, vva)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vva)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a, vvainf), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vvainf), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a, vvaNaN), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vvaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), - y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, vea)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vea)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a, veainf), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, veainf), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::Cos2Arg(), y0, t0, ts, - 1e-10, 1e-10, 1e6, nullptr, a, veaNaN), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, veaNaN), + std::domain_error); } TEST(ode_rk45_tol_prim, rtol_errors) { @@ -384,24 +287,20 @@ TEST(ode_rk45_tol_prim, rtol_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, rtol, 1e-10, 1e6, - nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + rtol, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - rtol_negative, 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + rtol_negative, 1e-10, 1e6, nullptr, a), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - rtolinf, 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + rtolinf, 1e-10, 1e6, nullptr, a), + std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - rtolNaN, 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + rtolNaN, 1e-10, 1e6, nullptr, a), + std::domain_error); } TEST(ode_rk45_tol_prim, atol_errors) { @@ -416,21 +315,19 @@ TEST(ode_rk45_tol_prim, atol_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, 1e-6, atol, 1e6, nullptr, - a)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, atol, 1e6, nullptr, a)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-6, atol_negative, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, + atol_negative, 1e6, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, - t0, ts, 1e-6, atolinf, 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, + atolinf, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, - t0, ts, 1e-6, atolNaN, 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, + atolNaN, 1e6, nullptr, a), std::domain_error); } @@ -445,19 +342,18 @@ TEST(ode_rk45_tol_prim, max_num_steps_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, 1e-6, 1e-6, - max_num_steps, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, max_num_steps, nullptr, + a)); EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-6, 1e-6, max_num_steps_negative, nullptr, a), + stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, 1e-6, + max_num_steps_negative, nullptr, a), std::domain_error); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-6, 1e-6, max_num_steps_zero, nullptr, a), - std::domain_error); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, + 1e-6, max_num_steps_zero, nullptr, a), + std::domain_error); } TEST(ode_rk45_tol_prim, rhs_wrong_size_errors) { @@ -467,14 +363,12 @@ TEST(ode_rk45_tol_prim, rhs_wrong_size_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), - y0, t0, ts, 1e-6, 1e-6, 100, nullptr, - a)); + EXPECT_NO_THROW(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, a)); - EXPECT_THROW( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArgWrongSize(), y0, - t0, ts, 1e-6, 1e-6, 100, nullptr, a), - std::invalid_argument); + EXPECT_THROW(stan::math::ode_rk45_tol(stan::test::CosArgWrongSize(), y0, t0, + ts, 1e-6, 1e-6, 100, nullptr, a), + std::invalid_argument); } TEST(ode_rk45_tol_prim, error_name) { @@ -484,10 +378,9 @@ TEST(ode_rk45_tol_prim, error_name) { double ainf = stan::math::INFTY; - EXPECT_THROW_MSG( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-6, 1e-6, 100, nullptr, ainf), - std::domain_error, "ode_rk45_tol"); + EXPECT_THROW_MSG(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, ainf), + std::domain_error, "ode_rk45_tol"); } TEST(ode_rk45_tol_prim, too_much_work) { @@ -497,9 +390,8 @@ TEST(ode_rk45_tol_prim, too_much_work) { double a = 1.0; - EXPECT_THROW_MSG( - stan::math::ode_rk45_tol(ode_rk45_tol_prim_test::CosArg1(), y0, t0, ts, - 1e-6, 1e-6, 100, nullptr, a), - std::domain_error, - "ode_rk45_tol: Failed to integrate to next output time"); + EXPECT_THROW_MSG(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, a), + std::domain_error, + "ode_rk45_tol: Failed to integrate to next output time"); } diff --git a/test/unit/math/prim/functor/ode_test_functors.hpp b/test/unit/math/prim/functor/ode_test_functors.hpp new file mode 100644 index 00000000000..633f04c1634 --- /dev/null +++ b/test/unit/math/prim/functor/ode_test_functors.hpp @@ -0,0 +1,76 @@ +#ifndef TEST_UNIT_MATH_PRIM_FUNCTOR_ODE_TEST_FUNCTORS_HPP +#define TEST_UNIT_MATH_PRIM_FUNCTOR_ODE_TEST_FUNCTORS_HPP + +#include + +namespace stan { +namespace test { + +template * = nullptr> +T sum_(T arg) { + return arg; +} + +template * = nullptr> +auto sum_(EigMat&& arg) { + return stan::math::sum(arg); +} + +template * = nullptr> +auto sum_(Vec&& arg) { + stan::scalar_type_t sum = 0; + for (size_t i = 0; i < arg.size(); ++i) { + sum += sum_(arg[i]); + } + return sum; +} + +struct CosArg1 { + template + inline Eigen::Matrix, Eigen::Dynamic, 1> + operator()(const T0& t, const Eigen::Matrix& y, + std::ostream* msgs, const T_Args&... a) const { + std::vector::type> vec + = {sum_(a)...}; + Eigen::Matrix, Eigen::Dynamic, 1> out(1); + out << stan::math::cos(sum_(vec) * t); + return out; + } +}; + +struct Cos2Arg { + template + inline Eigen::Matrix, Eigen::Dynamic, 1> + operator()(const T0& t, const Eigen::Matrix& y, + std::ostream* msgs, const T2& a, const T3& b) const { + Eigen::Matrix, Eigen::Dynamic, 1> out(1); + out << stan::math::cos((sum_(a) + sum_(b)) * t); + return out; + } +}; + +struct CosArgWrongSize { + template + inline Eigen::Matrix, Eigen::Dynamic, 1> + operator()(const T0& t, const Eigen::Matrix& y, + std::ostream* msgs, const T_Args&... a) const { + std::vector::type> vec + = {sum_(a)...}; + Eigen::Matrix, Eigen::Dynamic, 1> out(2); + out << stan::math::cos(sum_(vec) * t), 0; + return out; + } +}; + +struct ayt { + template + inline Eigen::Matrix, Eigen::Dynamic, 1> + operator()(const T0& t, const T_y& y, std::ostream* msgs, const T2& a) const { + return -a * y * t; + } +}; + +} // namespace test +} // namespace stan + +#endif diff --git a/test/unit/math/rev/functor/ode_adams_prim_test.cpp b/test/unit/math/rev/functor/ode_adams_prim_test.cpp index 5476aea7da2..25dd37641a4 100644 --- a/test/unit/math/rev/functor/ode_adams_prim_test.cpp +++ b/test/unit/math/rev/functor/ode_adams_prim_test.cpp @@ -1,65 +1,10 @@ #include #include #include +#include #include #include -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - -struct CosArgWrongSize { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(2); - out << stan::math::cos(sum_(vec) * t), 0; - return out; - } -}; - TEST(ode_adams_prim, y0_errors) { Eigen::VectorXd y0 = Eigen::VectorXd::Zero(1); Eigen::VectorXd y0inf(1); @@ -72,15 +17,19 @@ TEST(ode_adams_prim, y0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0inf, t0, ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0inf, t0, ts, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0NaN, t0, ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0NaN, t0, ts, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0_empty, t0, ts, nullptr, a), + EXPECT_THROW(stan::math::ode_adams(stan::test::CosArg1(), y0_empty, t0, ts, + nullptr, a), std::invalid_argument); } @@ -93,13 +42,16 @@ TEST(ode_adams_prim, t0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0inf, ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0inf, ts, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0NaN, ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0NaN, ts, nullptr, a), + std::domain_error); } TEST(ode_adams_prim, ts_errors) { @@ -117,34 +69,38 @@ TEST(ode_adams_prim, ts_errors) { double a = 1.5; std::vector out; - EXPECT_NO_THROW(out - = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, + nullptr, a)); EXPECT_EQ(out.size(), ts.size()); - EXPECT_NO_THROW( - out = stan::math::ode_adams(CosArg1(), y0, t0, ts_repeat, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, + ts_repeat, nullptr, a)); EXPECT_EQ(out.size(), ts_repeat.size()); EXPECT_MATRIX_FLOAT_EQ(out[0], out[1]); - EXPECT_NO_THROW( - out = stan::math::ode_adams(CosArg1(), y0, t0, ts_lots, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, + ts_lots, nullptr, a)); EXPECT_EQ(out.size(), ts_lots.size()); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts_empty, nullptr, a), + EXPECT_THROW(stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts_empty, + nullptr, a), std::invalid_argument); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts_early, nullptr, a), + EXPECT_THROW(stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts_early, + nullptr, a), + std::domain_error); + + EXPECT_THROW(stan::math::ode_adams(stan::test::CosArg1(), y0, t0, + ts_decreasing, nullptr, a), std::domain_error); EXPECT_THROW( - stan::math::ode_adams(CosArg1(), y0, t0, ts_decreasing, nullptr, a), + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, tsinf, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, tsinf, nullptr, a), - std::domain_error); - - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, tsNaN, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, tsNaN, nullptr, a), + std::domain_error); } TEST(ode_adams_prim, one_arg_errors) { @@ -175,45 +131,60 @@ TEST(ode_adams_prim, one_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, ainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, ainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, aNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, aNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, va)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, va)); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, vainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, vainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, vaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, vaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, ea)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, ea)); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, eainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, eainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, eaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, eaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, vva)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, vva)); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, vvainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, vvainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, vvaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, vvaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, vea)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, vea)); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, veainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, veainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, veaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, veaNaN), + std::domain_error); } TEST(ode_adams_prim, two_arg_errors) { @@ -244,46 +215,59 @@ TEST(ode_adams_prim, two_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, a)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, a)); - EXPECT_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, ainf), + EXPECT_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, ainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, aNaN), + EXPECT_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, aNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, va)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, va)); - EXPECT_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, vainf), + EXPECT_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, vainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, vaNaN), + EXPECT_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, vaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, ea)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, ea)); - EXPECT_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, eainf), + EXPECT_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, eainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, eaNaN), + EXPECT_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, eaNaN), std::domain_error); - EXPECT_NO_THROW( - stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, vva)); + EXPECT_NO_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, + nullptr, a, vva)); - EXPECT_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, vvainf), + EXPECT_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, vvainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, vvaNaN), + EXPECT_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, vvaNaN), std::domain_error); - EXPECT_NO_THROW( - stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, vea)); + EXPECT_NO_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, + nullptr, a, vea)); - EXPECT_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, veainf), + EXPECT_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, veainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a, veaNaN), + EXPECT_THROW(stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, veaNaN), std::domain_error); } @@ -294,9 +278,11 @@ TEST(ode_adams_prim, rhs_wrong_size_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_adams(CosArgWrongSize(), y0, t0, ts, nullptr, a), + EXPECT_THROW(stan::math::ode_adams(stan::test::CosArgWrongSize(), y0, t0, ts, + nullptr, a), std::invalid_argument); } @@ -307,6 +293,7 @@ TEST(ode_adams_prim, error_name) { double ainf = stan::math::INFTY; - EXPECT_THROW_MSG(stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, ainf), - std::domain_error, "ode_adams"); + EXPECT_THROW_MSG( + stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, ainf), + std::domain_error, "ode_adams"); } diff --git a/test/unit/math/rev/functor/ode_adams_rev_test.cpp b/test/unit/math/rev/functor/ode_adams_rev_test.cpp index 8a1958b8d64..45e10d258ed 100644 --- a/test/unit/math/rev/functor/ode_adams_rev_test.cpp +++ b/test/unit/math/rev/functor/ode_adams_rev_test.cpp @@ -1,54 +1,9 @@ #include #include +#include #include #include -using stan::math::var; - -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const T_y& y, std::ostream* msgs, - const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out( - 1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - TEST(StanMathOde_ode_adams, int_t0) { using stan::math::var; @@ -59,7 +14,7 @@ TEST(StanMathOde_ode_adams, int_t0) { double a = 1.5; std::vector> output - = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, a); EXPECT_NEAR(output[0][0], 0.4165982112, 1e-5); EXPECT_NEAR(output[1][0], 0.66457668563, 1e-5); @@ -75,7 +30,7 @@ TEST(StanMathOde_ode_adams, int_ts) { double a = 1.5; std::vector> output - = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, a); EXPECT_NEAR(output[0][0], 0.6649966577, 1e-5); EXPECT_NEAR(output[1][0], 0.09408000537, 1e-5); @@ -91,7 +46,7 @@ TEST(StanMathOde_ode_adams, t0) { double a = 1.5; std::vector> output - = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, a); output[0][0].grad(); @@ -116,7 +71,7 @@ TEST(StanMathOde_ode_adams, ts) { double a = 1.5; std::vector> output - = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, a); output[0][0].grad(); @@ -141,7 +96,7 @@ TEST(StanMathOde_ode_adams, ts_repeat) { double a = 1.5; std::vector> output - = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, a); EXPECT_EQ(output.size(), ts.size()); @@ -181,7 +136,8 @@ TEST(StanMathOde_ode_adams, scalar_arg) { var a = 1.5; - var output = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -199,7 +155,7 @@ TEST(StanMathOde_ode_adams, scalar_arg_multi_time) { var a = 1.5; std::vector> output - = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, a); output[0](0).grad(); @@ -223,7 +179,8 @@ TEST(StanMathOde_ode_adams, std_vector_arg) { std::vector a = {1.5}; - var output = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -241,7 +198,8 @@ TEST(StanMathOde_ode_adams, vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -259,7 +217,8 @@ TEST(StanMathOde_ode_adams, row_vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -277,7 +236,8 @@ TEST(StanMathOde_ode_adams, matrix_arg) { Eigen::Matrix a(1, 1); a << 1.5; - var output = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -295,8 +255,8 @@ TEST(StanMathOde_ode_adams, scalar_std_vector_args) { var a0 = 0.75; std::vector a1 = {0.75}; - var output - = stan::math::ode_adams(Cos2Arg(), y0, t0, ts, nullptr, a0, a1)[0][0]; + var output = stan::math::ode_adams(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a0, a1)[0][0]; output.grad(); @@ -316,7 +276,8 @@ TEST(StanMathOde_ode_adams, std_vector_std_vector_args) { std::vector a1(1, a0); std::vector> a2(1, a1); - var output = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -336,7 +297,8 @@ TEST(StanMathOde_ode_adams, std_vector_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -356,7 +318,8 @@ TEST(StanMathOde_ode_adams, std_vector_row_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -376,7 +339,8 @@ TEST(StanMathOde_ode_adams, std_vector_matrix_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_adams(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_adams(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -384,15 +348,8 @@ TEST(StanMathOde_ode_adams, std_vector_matrix_args) { EXPECT_NEAR(a2[0](0).adj(), -0.50107310888, 1e-5); } -struct ayt { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const T_y& y, std::ostream* msgs, const T2& a) const { - return -a * y * t; - } -}; - TEST(StanMathOde_ode_adams, arg_combos_test) { + using stan::math::var; var t0 = 0.5; var a = 0.2; std::vector ts = {1.25}; @@ -435,49 +392,57 @@ TEST(StanMathOde_ode_adams, arg_combos_test) { 1e-5); }; - double yT1 = stan::math::ode_adams(ayt(), y0d, t0d, tsd, nullptr, ad)[0](0); + double yT1 = stan::math::ode_adams(stan::test::ayt(), y0d, t0d, tsd, nullptr, + ad)[0](0); check_yT(yT1); - var yT2 = stan::math::ode_adams(ayt(), y0d, t0d, tsd, nullptr, a)[0](0); + var yT2 = stan::math::ode_adams(stan::test::ayt(), y0d, t0d, tsd, nullptr, + a)[0](0); stan::math::set_zero_all_adjoints(); yT2.grad(); check_yT(yT2); check_a(a); - var yT3 = stan::math::ode_adams(ayt(), y0d, t0d, ts, nullptr, ad)[0](0); + var yT3 = stan::math::ode_adams(stan::test::ayt(), y0d, t0d, ts, nullptr, + ad)[0](0); stan::math::set_zero_all_adjoints(); yT3.grad(); check_yT(yT3); check_ts(ts); - var yT4 = stan::math::ode_adams(ayt(), y0d, t0d, ts, nullptr, a)[0](0); + var yT4 = stan::math::ode_adams(stan::test::ayt(), y0d, t0d, ts, nullptr, + a)[0](0); stan::math::set_zero_all_adjoints(); yT4.grad(); check_yT(yT4); check_ts(ts); check_a(a); - var yT5 = stan::math::ode_adams(ayt(), y0d, t0, tsd, nullptr, ad)[0](0); + var yT5 = stan::math::ode_adams(stan::test::ayt(), y0d, t0, tsd, nullptr, + ad)[0](0); stan::math::set_zero_all_adjoints(); yT5.grad(); check_yT(yT5); check_t0(t0); - var yT6 = stan::math::ode_adams(ayt(), y0d, t0, tsd, nullptr, a)[0](0); + var yT6 = stan::math::ode_adams(stan::test::ayt(), y0d, t0, tsd, nullptr, + a)[0](0); stan::math::set_zero_all_adjoints(); yT6.grad(); check_yT(yT6); check_t0(t0); check_a(a); - var yT7 = stan::math::ode_adams(ayt(), y0d, t0, ts, nullptr, ad)[0](0); + var yT7 = stan::math::ode_adams(stan::test::ayt(), y0d, t0, ts, nullptr, + ad)[0](0); stan::math::set_zero_all_adjoints(); yT7.grad(); check_yT(yT7); check_t0(t0); check_ts(ts); - var yT8 = stan::math::ode_adams(ayt(), y0d, t0, ts, nullptr, a)[0](0); + var yT8 + = stan::math::ode_adams(stan::test::ayt(), y0d, t0, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT8.grad(); check_yT(yT8); @@ -485,27 +450,31 @@ TEST(StanMathOde_ode_adams, arg_combos_test) { check_ts(ts); check_a(a); - var yT9 = stan::math::ode_adams(ayt(), y0, t0d, tsd, nullptr, ad)[0](0); + var yT9 = stan::math::ode_adams(stan::test::ayt(), y0, t0d, tsd, nullptr, + ad)[0](0); stan::math::set_zero_all_adjoints(); yT9.grad(); check_yT(yT9); check_y0(y0); - var yT10 = stan::math::ode_adams(ayt(), y0, t0d, tsd, nullptr, a)[0](0); + var yT10 = stan::math::ode_adams(stan::test::ayt(), y0, t0d, tsd, nullptr, + a)[0](0); stan::math::set_zero_all_adjoints(); yT10.grad(); check_yT(yT10); check_y0(y0); check_a(a); - var yT11 = stan::math::ode_adams(ayt(), y0, t0d, ts, nullptr, ad)[0](0); + var yT11 = stan::math::ode_adams(stan::test::ayt(), y0, t0d, ts, nullptr, + ad)[0](0); stan::math::set_zero_all_adjoints(); yT11.grad(); check_yT(yT11); check_y0(y0); check_ts(ts); - var yT12 = stan::math::ode_adams(ayt(), y0, t0d, ts, nullptr, a)[0](0); + var yT12 + = stan::math::ode_adams(stan::test::ayt(), y0, t0d, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT12.grad(); check_yT(yT12); @@ -513,14 +482,16 @@ TEST(StanMathOde_ode_adams, arg_combos_test) { check_ts(ts); check_a(a); - var yT13 = stan::math::ode_adams(ayt(), y0, t0, tsd, nullptr, ad)[0](0); + var yT13 = stan::math::ode_adams(stan::test::ayt(), y0, t0, tsd, nullptr, + ad)[0](0); stan::math::set_zero_all_adjoints(); yT13.grad(); check_yT(yT13); check_y0(y0); check_t0(t0); - var yT14 = stan::math::ode_adams(ayt(), y0, t0, tsd, nullptr, a)[0](0); + var yT14 + = stan::math::ode_adams(stan::test::ayt(), y0, t0, tsd, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT14.grad(); check_yT(yT14); @@ -528,7 +499,8 @@ TEST(StanMathOde_ode_adams, arg_combos_test) { check_t0(t0); check_a(a); - var yT15 = stan::math::ode_adams(ayt(), y0, t0, ts, nullptr, ad)[0](0); + var yT15 + = stan::math::ode_adams(stan::test::ayt(), y0, t0, ts, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT15.grad(); check_yT(yT15); @@ -536,7 +508,8 @@ TEST(StanMathOde_ode_adams, arg_combos_test) { check_t0(t0); check_ts(ts); - var yT16 = stan::math::ode_adams(ayt(), y0, t0, ts, nullptr, a)[0](0); + var yT16 + = stan::math::ode_adams(stan::test::ayt(), y0, t0, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT16.grad(); check_yT(yT16); diff --git a/test/unit/math/rev/functor/ode_adams_tol_prim_test.cpp b/test/unit/math/rev/functor/ode_adams_tol_prim_test.cpp index b21f7a15142..3c49ef454d2 100644 --- a/test/unit/math/rev/functor/ode_adams_tol_prim_test.cpp +++ b/test/unit/math/rev/functor/ode_adams_tol_prim_test.cpp @@ -1,65 +1,10 @@ #include #include #include +#include #include #include -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - -struct CosArgWrongSize { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(2); - out << stan::math::cos(sum_(vec) * t), 0; - return out; - } -}; - TEST(ode_adams_tol_prim, y0_errors) { Eigen::VectorXd y0 = Eigen::VectorXd::Zero(1); Eigen::VectorXd y0inf(1); @@ -72,19 +17,19 @@ TEST(ode_adams_tol_prim, y0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0inf, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0inf, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0NaN, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0NaN, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0_empty, t0, ts, 1e-10, - 1e-10, 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0_empty, t0, + ts, 1e-10, 1e-10, 1e6, nullptr, a), std::invalid_argument); } @@ -97,15 +42,15 @@ TEST(ode_adams_tol_prim, t0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0inf, ts, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0inf, ts, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0NaN, ts, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0NaN, ts, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); } @@ -124,39 +69,43 @@ TEST(ode_adams_tol_prim, ts_errors) { double a = 1.5; std::vector out; - EXPECT_NO_THROW(out = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, - 1e-10, 1e6, nullptr, a)); - EXPECT_EQ(out.size(), ts.size()); - EXPECT_NO_THROW(out - = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts_repeat, + = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, nullptr, a)); + EXPECT_EQ(out.size(), ts.size()); + + EXPECT_NO_THROW(out = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, + ts_repeat, 1e-10, 1e-10, 1e6, + nullptr, a)); EXPECT_EQ(out.size(), ts_repeat.size()); EXPECT_MATRIX_FLOAT_EQ(out[0], out[1]); - EXPECT_NO_THROW(out - = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts_lots, 1e-10, - 1e-10, 1e6, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, + ts_lots, 1e-10, 1e-10, 1e6, + nullptr, a)); EXPECT_EQ(out.size(), ts_lots.size()); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts_empty, 1e-10, - 1e-10, 1e6, nullptr, a), - std::invalid_argument); + EXPECT_THROW( + stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts_empty, 1e-10, + 1e-10, 1e6, nullptr, a), + std::invalid_argument); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts_early, 1e-10, - 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts_early, 1e-10, + 1e-10, 1e6, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts_decreasing, - 1e-10, 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts_decreasing, + 1e-10, 1e-10, 1e6, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, tsinf, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, tsinf, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, tsNaN, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, tsNaN, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); } @@ -188,59 +137,59 @@ TEST(ode_adams_tol_prim, one_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, ainf), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, ainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, aNaN), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, aNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, va)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, va)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, vainf), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, vaNaN), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, ea)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, ea)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, eainf), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, eainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, eaNaN), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, eaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, vva)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vva)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, vvainf), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vvainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, vvaNaN), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vvaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, vea)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vea)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, veainf), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, veainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, veaNaN), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, veaNaN), std::domain_error); } @@ -272,59 +221,59 @@ TEST(ode_adams_tol_prim, two_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, a)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, a)); - EXPECT_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, ainf), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, ainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, aNaN), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, aNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, va)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, va)); - EXPECT_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, vainf), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, vaNaN), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, ea)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, ea)); - EXPECT_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, eainf), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, eainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, eaNaN), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, eaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, vva)); + EXPECT_NO_THROW(stan::math::ode_adams_tol( + stan::test::Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, nullptr, a, vva)); - EXPECT_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, vvainf), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vvainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, vvaNaN), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vvaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, vea)); + EXPECT_NO_THROW(stan::math::ode_adams_tol( + stan::test::Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, nullptr, a, vea)); - EXPECT_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, veainf), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, veainf), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, veaNaN), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, veaNaN), std::domain_error); } @@ -340,19 +289,19 @@ TEST(ode_adams_tol_prim, rtol_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, rtol, 1e-10, - 1e6, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + rtol, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, rtol_negative, - 1e-10, 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + rtol_negative, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, rtolinf, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + rtolinf, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, rtolNaN, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + rtolNaN, 1e-10, 1e6, nullptr, a), std::domain_error); } @@ -368,19 +317,19 @@ TEST(ode_adams_tol_prim, atol_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, atol, - 1e6, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, atol, 1e6, nullptr, a)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, - atol_negative, 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, atol_negative, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, atolinf, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, atolinf, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, atolNaN, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, atolNaN, 1e6, nullptr, a), std::domain_error); } @@ -395,16 +344,19 @@ TEST(ode_adams_tol_prim, max_num_steps_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - max_num_steps, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, max_num_steps, nullptr, + a)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - max_num_steps_negative, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, 1e-6, + max_num_steps_negative, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - max_num_steps_zero, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, 1e-6, + max_num_steps_zero, nullptr, a), + std::domain_error); } TEST(ode_adams_tol_prim, rhs_wrong_size_errors) { @@ -414,11 +366,11 @@ TEST(ode_adams_tol_prim, rhs_wrong_size_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - 100, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, a)); - EXPECT_THROW(stan::math::ode_adams_tol(CosArgWrongSize(), y0, t0, ts, 1e-6, - 1e-6, 100, nullptr, a), + EXPECT_THROW(stan::math::ode_adams_tol(stan::test::CosArgWrongSize(), y0, t0, + ts, 1e-6, 1e-6, 100, nullptr, a), std::invalid_argument); } @@ -429,8 +381,8 @@ TEST(ode_adams_tol_prim, error_name) { double ainf = stan::math::INFTY; - EXPECT_THROW_MSG(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - 100, nullptr, ainf), + EXPECT_THROW_MSG(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, ainf), std::domain_error, "ode_adams_tol"); } @@ -441,8 +393,8 @@ TEST(ode_adams_tol_prim, too_much_work) { double a = 1.0; - EXPECT_THROW_MSG(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - 100, nullptr, a), + EXPECT_THROW_MSG(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, a), std::domain_error, "ode_adams_tol: Failed to integrate to next output time"); } diff --git a/test/unit/math/rev/functor/ode_adams_tol_rev_test.cpp b/test/unit/math/rev/functor/ode_adams_tol_rev_test.cpp index d92be94a14b..88176f33e5f 100644 --- a/test/unit/math/rev/functor/ode_adams_tol_rev_test.cpp +++ b/test/unit/math/rev/functor/ode_adams_tol_rev_test.cpp @@ -1,54 +1,10 @@ #include #include #include +#include #include #include -using stan::math::var; - -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - TEST(StanMathOde_ode_adams_tol, int_t0) { using stan::math::var; @@ -59,8 +15,8 @@ TEST(StanMathOde_ode_adams_tol, int_t0) { double a = 1.5; std::vector> output - = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); EXPECT_FLOAT_EQ(output[0][0], 0.4165982112); EXPECT_FLOAT_EQ(output[1][0], 0.66457668563); @@ -76,8 +32,8 @@ TEST(StanMathOde_ode_adams_tol, int_ts) { double a = 1.5; std::vector> output - = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); EXPECT_FLOAT_EQ(output[0][0], 0.6649966577); EXPECT_FLOAT_EQ(output[1][0], 0.09408000537); @@ -93,8 +49,8 @@ TEST(StanMathOde_ode_adams_tol, t0) { double a = 1.5; std::vector> output - = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); output[0][0].grad(); @@ -119,8 +75,8 @@ TEST(StanMathOde_ode_adams_tol, ts) { double a = 1.5; std::vector> output - = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); output[0][0].grad(); @@ -145,8 +101,8 @@ TEST(StanMathOde_ode_adams_tol, ts_repeat) { double a = 1.5; std::vector> output - = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); EXPECT_EQ(output.size(), ts.size()); @@ -186,8 +142,8 @@ TEST(StanMathOde_ode_adams_tol, scalar_arg) { var a = 1.5; - var output = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, - 1e6, nullptr, a)[0][0]; + var output = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-8, 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -205,8 +161,8 @@ TEST(StanMathOde_ode_adams_tol, scalar_arg_multi_time) { var a = 1.5; std::vector> output - = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); output[0](0).grad(); @@ -230,8 +186,8 @@ TEST(StanMathOde_ode_adams_tol, std_vector_arg) { std::vector a = {1.5}; - var output = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, - 1e6, nullptr, a)[0][0]; + var output = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-8, 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -249,8 +205,8 @@ TEST(StanMathOde_ode_adams_tol, vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, - 1e6, nullptr, a)[0][0]; + var output = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-8, 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -268,8 +224,8 @@ TEST(StanMathOde_ode_adams_tol, row_vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, - 1e6, nullptr, a)[0][0]; + var output = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-8, 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -287,8 +243,8 @@ TEST(StanMathOde_ode_adams_tol, matrix_arg) { Eigen::Matrix a(1, 1); a << 1.5; - var output = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, - 1e6, nullptr, a)[0][0]; + var output = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-8, 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -306,8 +262,9 @@ TEST(StanMathOde_ode_adams_tol, scalar_std_vector_args) { var a0 = 0.75; std::vector a1 = {0.75}; - var output = stan::math::ode_adams_tol(Cos2Arg(), y0, t0, ts, 1e-8, 1e-10, - 1e6, nullptr, a0, a1)[0][0]; + var output + = stan::math::ode_adams_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a0, a1)[0][0]; output.grad(); @@ -327,8 +284,8 @@ TEST(StanMathOde_ode_adams_tol, std_vector_std_vector_args) { std::vector a1(1, a0); std::vector> a2(1, a1); - var output = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, - 1e6, nullptr, a2)[0][0]; + var output = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-8, 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -348,8 +305,8 @@ TEST(StanMathOde_ode_adams_tol, std_vector_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, - 1e6, nullptr, a2)[0][0]; + var output = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-8, 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -369,8 +326,8 @@ TEST(StanMathOde_ode_adams_tol, std_vector_row_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, - 1e6, nullptr, a2)[0][0]; + var output = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-8, 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -390,8 +347,8 @@ TEST(StanMathOde_ode_adams_tol, std_vector_matrix_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, - 1e6, nullptr, a2)[0][0]; + var output = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-8, 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -399,16 +356,9 @@ TEST(StanMathOde_ode_adams_tol, std_vector_matrix_args) { EXPECT_FLOAT_EQ(a2[0](0).adj(), -0.50107310888); } -struct ayt { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a) const { - return -a * y * t; - } -}; - TEST(StanMathOde_ode_adams_tol, arg_combos_test) { + using stan::math::var; + var t0 = 0.5; var a = 0.2; std::vector ts = {1.25}; @@ -448,57 +398,57 @@ TEST(StanMathOde_ode_adams_tol, arg_combos_test) { exp(-0.5 * ad * (tsd[0] * tsd[0] - t0d * t0d))); }; - double yT1 = stan::math::ode_adams_tol(ayt(), y0d, t0d, tsd, 1e-10, 1e-10, - 1e6, nullptr, ad)[0](0); + double yT1 = stan::math::ode_adams_tol(stan::test::ayt(), y0d, t0d, tsd, + 1e-10, 1e-10, 1e6, nullptr, ad)[0](0); check_yT(yT1); - var yT2 = stan::math::ode_adams_tol(ayt(), y0d, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT2 = stan::math::ode_adams_tol(stan::test::ayt(), y0d, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT2.grad(); check_yT(yT2); check_a(a); - var yT3 = stan::math::ode_adams_tol(ayt(), y0d, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT3 = stan::math::ode_adams_tol(stan::test::ayt(), y0d, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT3.grad(); check_yT(yT3); check_ts(ts); - var yT4 = stan::math::ode_adams_tol(ayt(), y0d, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT4 = stan::math::ode_adams_tol(stan::test::ayt(), y0d, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT4.grad(); check_yT(yT4); check_ts(ts); check_a(a); - var yT5 = stan::math::ode_adams_tol(ayt(), y0d, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT5 = stan::math::ode_adams_tol(stan::test::ayt(), y0d, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT5.grad(); check_yT(yT5); check_t0(t0); - var yT6 = stan::math::ode_adams_tol(ayt(), y0d, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT6 = stan::math::ode_adams_tol(stan::test::ayt(), y0d, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT6.grad(); check_yT(yT6); check_t0(t0); check_a(a); - var yT7 = stan::math::ode_adams_tol(ayt(), y0d, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT7 = stan::math::ode_adams_tol(stan::test::ayt(), y0d, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT7.grad(); check_yT(yT7); check_t0(t0); check_ts(ts); - var yT8 = stan::math::ode_adams_tol(ayt(), y0d, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT8 = stan::math::ode_adams_tol(stan::test::ayt(), y0d, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT8.grad(); check_yT(yT8); @@ -506,31 +456,31 @@ TEST(StanMathOde_ode_adams_tol, arg_combos_test) { check_ts(ts); check_a(a); - var yT9 = stan::math::ode_adams_tol(ayt(), y0, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT9 = stan::math::ode_adams_tol(stan::test::ayt(), y0, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT9.grad(); check_yT(yT9); check_y0(y0); - var yT10 = stan::math::ode_adams_tol(ayt(), y0, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT10 = stan::math::ode_adams_tol(stan::test::ayt(), y0, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT10.grad(); check_yT(yT10); check_y0(y0); check_a(a); - var yT11 = stan::math::ode_adams_tol(ayt(), y0, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT11 = stan::math::ode_adams_tol(stan::test::ayt(), y0, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT11.grad(); check_yT(yT11); check_y0(y0); check_ts(ts); - var yT12 = stan::math::ode_adams_tol(ayt(), y0, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT12 = stan::math::ode_adams_tol(stan::test::ayt(), y0, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT12.grad(); check_yT(yT12); @@ -538,16 +488,16 @@ TEST(StanMathOde_ode_adams_tol, arg_combos_test) { check_ts(ts); check_a(a); - var yT13 = stan::math::ode_adams_tol(ayt(), y0, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT13 = stan::math::ode_adams_tol(stan::test::ayt(), y0, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT13.grad(); check_yT(yT13); check_y0(y0); check_t0(t0); - var yT14 = stan::math::ode_adams_tol(ayt(), y0, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT14 = stan::math::ode_adams_tol(stan::test::ayt(), y0, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT14.grad(); check_yT(yT14); @@ -555,8 +505,8 @@ TEST(StanMathOde_ode_adams_tol, arg_combos_test) { check_t0(t0); check_a(a); - var yT15 = stan::math::ode_adams_tol(ayt(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT15 = stan::math::ode_adams_tol(stan::test::ayt(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT15.grad(); check_yT(yT15); @@ -564,8 +514,8 @@ TEST(StanMathOde_ode_adams_tol, arg_combos_test) { check_t0(t0); check_ts(ts); - var yT16 = stan::math::ode_adams_tol(ayt(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT16 = stan::math::ode_adams_tol(stan::test::ayt(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT16.grad(); check_yT(yT16); @@ -576,6 +526,8 @@ TEST(StanMathOde_ode_adams_tol, arg_combos_test) { } TEST(StanMathOde_ode_adams_tol, too_much_work) { + using stan::math::var; + Eigen::Matrix y0 = Eigen::VectorXd::Zero(1).template cast(); var t0 = 0; @@ -583,8 +535,8 @@ TEST(StanMathOde_ode_adams_tol, too_much_work) { var a = 1.0; - EXPECT_THROW_MSG(stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - 100, nullptr, a), + EXPECT_THROW_MSG(stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, a), std::domain_error, "ode_adams_tol: Failed to integrate to next output time"); } diff --git a/test/unit/math/rev/functor/ode_bdf_prim_test.cpp b/test/unit/math/rev/functor/ode_bdf_prim_test.cpp index 8afd89965ab..5323977ae1c 100644 --- a/test/unit/math/rev/functor/ode_bdf_prim_test.cpp +++ b/test/unit/math/rev/functor/ode_bdf_prim_test.cpp @@ -1,65 +1,10 @@ #include #include #include +#include #include #include -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - -struct CosArgWrongSize { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(2); - out << stan::math::cos(sum_(vec) * t), 0; - return out; - } -}; - TEST(ode_bdf_prim, y0_errors) { Eigen::VectorXd y0 = Eigen::VectorXd::Zero(1); Eigen::VectorXd y0inf(1); @@ -72,16 +17,20 @@ TEST(ode_bdf_prim, y0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0inf, t0, ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0inf, t0, ts, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0NaN, t0, ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0NaN, t0, ts, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0_empty, t0, ts, nullptr, a), - std::invalid_argument); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0_empty, t0, ts, nullptr, a), + std::invalid_argument); } TEST(ode_bdf_prim, t0_errors) { @@ -93,13 +42,16 @@ TEST(ode_bdf_prim, t0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0inf, ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0inf, ts, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0NaN, ts, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0NaN, ts, nullptr, a), + std::domain_error); } TEST(ode_bdf_prim, ts_errors) { @@ -117,33 +69,38 @@ TEST(ode_bdf_prim, ts_errors) { double a = 1.5; std::vector out; - EXPECT_NO_THROW(out = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a)); + EXPECT_NO_THROW( + out = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); EXPECT_EQ(out.size(), ts.size()); - EXPECT_NO_THROW( - out = stan::math::ode_bdf(CosArg1(), y0, t0, ts_repeat, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, + ts_repeat, nullptr, a)); EXPECT_EQ(out.size(), ts_repeat.size()); EXPECT_MATRIX_FLOAT_EQ(out[0], out[1]); - EXPECT_NO_THROW( - out = stan::math::ode_bdf(CosArg1(), y0, t0, ts_lots, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, + ts_lots, nullptr, a)); EXPECT_EQ(out.size(), ts_lots.size()); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts_empty, nullptr, a), - std::invalid_argument); - - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts_early, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts_empty, nullptr, a), + std::invalid_argument); EXPECT_THROW( - stan::math::ode_bdf(CosArg1(), y0, t0, ts_decreasing, nullptr, a), + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts_early, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, tsinf, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts_decreasing, + nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, tsNaN, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, tsinf, nullptr, a), + std::domain_error); + + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, tsNaN, nullptr, a), + std::domain_error); } TEST(ode_bdf_prim, one_arg_errors) { @@ -174,45 +131,60 @@ TEST(ode_bdf_prim, one_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, ainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, ainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, aNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, aNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, va)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, va)); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, vainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, vainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, vaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, vaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, ea)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, ea)); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, eainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, eainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, eaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, eaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, vva)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, vva)); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, vvainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, vvainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, vvaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, vvaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, vea)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, vea)); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, veainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, veainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, veaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, veaNaN), + std::domain_error); } TEST(ode_bdf_prim, two_arg_errors) { @@ -243,44 +215,59 @@ TEST(ode_bdf_prim, two_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, a)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, a)); - EXPECT_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, ainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, ainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, aNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, aNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, va)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, va)); - EXPECT_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, vainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, vainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, vaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, vaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, ea)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, ea)); - EXPECT_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, eainf), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, eainf), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, eaNaN), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, eaNaN), + std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, vva)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, vva)); - EXPECT_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, vvainf), + EXPECT_THROW(stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, vvainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, vvaNaN), + EXPECT_THROW(stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, vvaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, vea)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, a, vea)); - EXPECT_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, veainf), + EXPECT_THROW(stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, veainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a, veaNaN), + EXPECT_THROW(stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a, veaNaN), std::domain_error); } @@ -291,9 +278,11 @@ TEST(ode_bdf_prim, rhs_wrong_size_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a)); + EXPECT_NO_THROW( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a)); - EXPECT_THROW(stan::math::ode_bdf(CosArgWrongSize(), y0, t0, ts, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf(stan::test::CosArgWrongSize(), y0, t0, ts, + nullptr, a), std::invalid_argument); } @@ -304,6 +293,7 @@ TEST(ode_bdf_prim, error_name) { double ainf = stan::math::INFTY; - EXPECT_THROW_MSG(stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, ainf), - std::domain_error, "ode_bdf"); + EXPECT_THROW_MSG( + stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, ainf), + std::domain_error, "ode_bdf"); } diff --git a/test/unit/math/rev/functor/ode_bdf_rev_test.cpp b/test/unit/math/rev/functor/ode_bdf_rev_test.cpp index 7fc537d4185..5a756d2e55d 100644 --- a/test/unit/math/rev/functor/ode_bdf_rev_test.cpp +++ b/test/unit/math/rev/functor/ode_bdf_rev_test.cpp @@ -1,54 +1,9 @@ #include #include +#include #include #include -using stan::math::var; - -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const T_y& y, std::ostream* msgs, - const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out( - 1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - TEST(StanMathOde_ode_bdf, int_t0) { using stan::math::var; @@ -59,7 +14,7 @@ TEST(StanMathOde_ode_bdf, int_t0) { double a = 1.5; std::vector> output - = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a); EXPECT_NEAR(output[0][0], 0.4165982112, 1e-5); EXPECT_NEAR(output[1][0], 0.66457668563, 1e-5); @@ -75,7 +30,7 @@ TEST(StanMathOde_ode_bdf, int_ts) { double a = 1.5; std::vector> output - = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a); EXPECT_NEAR(output[0][0], 0.6649966577, 1e-5); EXPECT_NEAR(output[1][0], 0.09408000537, 1e-5); @@ -91,7 +46,7 @@ TEST(StanMathOde_ode_bdf, t0) { double a = 1.5; std::vector> output - = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a); output[0][0].grad(); @@ -116,7 +71,7 @@ TEST(StanMathOde_ode_bdf, ts) { double a = 1.5; std::vector> output - = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a); output[0][0].grad(); @@ -141,7 +96,7 @@ TEST(StanMathOde_ode_bdf, ts_repeat) { double a = 1.5; std::vector> output - = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a); EXPECT_EQ(output.size(), ts.size()); @@ -181,7 +136,8 @@ TEST(StanMathOde_ode_bdf, scalar_arg) { var a = 1.5; - var output = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -199,7 +155,7 @@ TEST(StanMathOde_ode_bdf, scalar_arg_multi_time) { var a = 1.5; std::vector> output - = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, a); output[0](0).grad(); @@ -223,7 +179,8 @@ TEST(StanMathOde_ode_bdf, std_vector_arg) { std::vector a = {1.5}; - var output = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -241,7 +198,8 @@ TEST(StanMathOde_ode_bdf, vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -259,7 +217,8 @@ TEST(StanMathOde_ode_bdf, row_vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -277,7 +236,8 @@ TEST(StanMathOde_ode_bdf, matrix_arg) { Eigen::Matrix a(1, 1); a << 1.5; - var output = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -295,8 +255,8 @@ TEST(StanMathOde_ode_bdf, scalar_std_vector_args) { var a0 = 0.75; std::vector a1 = {0.75}; - var output - = stan::math::ode_bdf(Cos2Arg(), y0, t0, ts, nullptr, a0, a1)[0][0]; + var output = stan::math::ode_bdf(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a0, a1)[0][0]; output.grad(); @@ -316,7 +276,8 @@ TEST(StanMathOde_ode_bdf, std_vector_std_vector_args) { std::vector a1(1, a0); std::vector> a2(1, a1); - var output = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -336,7 +297,8 @@ TEST(StanMathOde_ode_bdf, std_vector_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -356,7 +318,8 @@ TEST(StanMathOde_ode_bdf, std_vector_row_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -376,7 +339,8 @@ TEST(StanMathOde_ode_bdf, std_vector_matrix_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_bdf(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_bdf(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -384,15 +348,9 @@ TEST(StanMathOde_ode_bdf, std_vector_matrix_args) { EXPECT_NEAR(a2[0](0).adj(), -0.50107310888, 1e-5); } -struct ayt { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const T_y& y, std::ostream* msgs, const T2& a) const { - return -a * y * t; - } -}; - TEST(StanMathOde_ode_bdf, arg_combos_test) { + using stan::math::var; + var t0 = 0.5; var a = 0.2; std::vector ts = {1.25}; @@ -435,49 +393,57 @@ TEST(StanMathOde_ode_bdf, arg_combos_test) { 1e-5); }; - double yT1 = stan::math::ode_bdf(ayt(), y0d, t0d, tsd, nullptr, ad)[0](0); + double yT1 = stan::math::ode_bdf(stan::test::ayt(), y0d, t0d, tsd, nullptr, + ad)[0](0); check_yT(yT1); - var yT2 = stan::math::ode_bdf(ayt(), y0d, t0d, tsd, nullptr, a)[0](0); + var yT2 + = stan::math::ode_bdf(stan::test::ayt(), y0d, t0d, tsd, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT2.grad(); check_yT(yT2); check_a(a); - var yT3 = stan::math::ode_bdf(ayt(), y0d, t0d, ts, nullptr, ad)[0](0); + var yT3 + = stan::math::ode_bdf(stan::test::ayt(), y0d, t0d, ts, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT3.grad(); check_yT(yT3); check_ts(ts); - var yT4 = stan::math::ode_bdf(ayt(), y0d, t0d, ts, nullptr, a)[0](0); + var yT4 + = stan::math::ode_bdf(stan::test::ayt(), y0d, t0d, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT4.grad(); check_yT(yT4); check_ts(ts); check_a(a); - var yT5 = stan::math::ode_bdf(ayt(), y0d, t0, tsd, nullptr, ad)[0](0); + var yT5 + = stan::math::ode_bdf(stan::test::ayt(), y0d, t0, tsd, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT5.grad(); check_yT(yT5); check_t0(t0); - var yT6 = stan::math::ode_bdf(ayt(), y0d, t0, tsd, nullptr, a)[0](0); + var yT6 + = stan::math::ode_bdf(stan::test::ayt(), y0d, t0, tsd, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT6.grad(); check_yT(yT6); check_t0(t0); check_a(a); - var yT7 = stan::math::ode_bdf(ayt(), y0d, t0, ts, nullptr, ad)[0](0); + var yT7 + = stan::math::ode_bdf(stan::test::ayt(), y0d, t0, ts, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT7.grad(); check_yT(yT7); check_t0(t0); check_ts(ts); - var yT8 = stan::math::ode_bdf(ayt(), y0d, t0, ts, nullptr, a)[0](0); + var yT8 + = stan::math::ode_bdf(stan::test::ayt(), y0d, t0, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT8.grad(); check_yT(yT8); @@ -485,27 +451,31 @@ TEST(StanMathOde_ode_bdf, arg_combos_test) { check_ts(ts); check_a(a); - var yT9 = stan::math::ode_bdf(ayt(), y0, t0d, tsd, nullptr, ad)[0](0); + var yT9 + = stan::math::ode_bdf(stan::test::ayt(), y0, t0d, tsd, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT9.grad(); check_yT(yT9); check_y0(y0); - var yT10 = stan::math::ode_bdf(ayt(), y0, t0d, tsd, nullptr, a)[0](0); + var yT10 + = stan::math::ode_bdf(stan::test::ayt(), y0, t0d, tsd, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT10.grad(); check_yT(yT10); check_y0(y0); check_a(a); - var yT11 = stan::math::ode_bdf(ayt(), y0, t0d, ts, nullptr, ad)[0](0); + var yT11 + = stan::math::ode_bdf(stan::test::ayt(), y0, t0d, ts, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT11.grad(); check_yT(yT11); check_y0(y0); check_ts(ts); - var yT12 = stan::math::ode_bdf(ayt(), y0, t0d, ts, nullptr, a)[0](0); + var yT12 + = stan::math::ode_bdf(stan::test::ayt(), y0, t0d, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT12.grad(); check_yT(yT12); @@ -513,14 +483,16 @@ TEST(StanMathOde_ode_bdf, arg_combos_test) { check_ts(ts); check_a(a); - var yT13 = stan::math::ode_bdf(ayt(), y0, t0, tsd, nullptr, ad)[0](0); + var yT13 + = stan::math::ode_bdf(stan::test::ayt(), y0, t0, tsd, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT13.grad(); check_yT(yT13); check_y0(y0); check_t0(t0); - var yT14 = stan::math::ode_bdf(ayt(), y0, t0, tsd, nullptr, a)[0](0); + var yT14 + = stan::math::ode_bdf(stan::test::ayt(), y0, t0, tsd, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT14.grad(); check_yT(yT14); @@ -528,7 +500,8 @@ TEST(StanMathOde_ode_bdf, arg_combos_test) { check_t0(t0); check_a(a); - var yT15 = stan::math::ode_bdf(ayt(), y0, t0, ts, nullptr, ad)[0](0); + var yT15 + = stan::math::ode_bdf(stan::test::ayt(), y0, t0, ts, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT15.grad(); check_yT(yT15); @@ -536,7 +509,8 @@ TEST(StanMathOde_ode_bdf, arg_combos_test) { check_t0(t0); check_ts(ts); - var yT16 = stan::math::ode_bdf(ayt(), y0, t0, ts, nullptr, a)[0](0); + var yT16 + = stan::math::ode_bdf(stan::test::ayt(), y0, t0, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT16.grad(); check_yT(yT16); diff --git a/test/unit/math/rev/functor/ode_bdf_tol_prim_test.cpp b/test/unit/math/rev/functor/ode_bdf_tol_prim_test.cpp index 4497e90b773..69d039c4084 100644 --- a/test/unit/math/rev/functor/ode_bdf_tol_prim_test.cpp +++ b/test/unit/math/rev/functor/ode_bdf_tol_prim_test.cpp @@ -1,65 +1,10 @@ #include #include #include +#include #include #include -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - -struct CosArgWrongSize { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(2); - out << stan::math::cos(sum_(vec) * t), 0; - return out; - } -}; - TEST(ode_bdf_tol_prim, y0_errors) { Eigen::VectorXd y0 = Eigen::VectorXd::Zero(1); Eigen::VectorXd y0inf(1); @@ -72,19 +17,19 @@ TEST(ode_bdf_tol_prim, y0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0inf, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0inf, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0NaN, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0NaN, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0_empty, t0, ts, 1e-10, - 1e-10, 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0_empty, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a), std::invalid_argument); } @@ -97,15 +42,15 @@ TEST(ode_bdf_tol_prim, t0_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0inf, ts, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0inf, ts, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0NaN, ts, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0NaN, ts, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); } @@ -124,37 +69,41 @@ TEST(ode_bdf_tol_prim, ts_errors) { double a = 1.5; std::vector out; - EXPECT_NO_THROW(out = stan::math::ode_adams_tol(CosArg1(), y0, t0, ts, 1e-10, - 1e-10, 1e6, nullptr, a)); + EXPECT_NO_THROW(out + = stan::math::ode_adams_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); EXPECT_EQ(out.size(), ts.size()); - EXPECT_NO_THROW(out = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts_repeat, - 1e-10, 1e-10, 1e6, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, + ts_repeat, 1e-10, 1e-10, 1e6, + nullptr, a)); EXPECT_EQ(out.size(), ts_repeat.size()); EXPECT_MATRIX_FLOAT_EQ(out[0], out[1]); - EXPECT_NO_THROW(out = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts_lots, - 1e-10, 1e-10, 1e6, nullptr, a)); + EXPECT_NO_THROW(out = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, + ts_lots, 1e-10, 1e-10, 1e6, + nullptr, a)); EXPECT_EQ(out.size(), ts_lots.size()); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts_empty, 1e-10, - 1e-10, 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts_empty, + 1e-10, 1e-10, 1e6, nullptr, a), std::invalid_argument); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts_early, 1e-10, - 1e-10, 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts_early, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts_decreasing, 1e-10, - 1e-10, 1e6, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts_decreasing, + 1e-10, 1e-10, 1e6, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, tsinf, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, tsinf, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, tsNaN, 1e-10, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, tsNaN, + 1e-10, 1e-10, 1e6, nullptr, a), std::domain_error); } @@ -186,59 +135,59 @@ TEST(ode_bdf_tol_prim, one_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, ainf), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, ainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, aNaN), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, aNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, va)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, va)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, vainf), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, vainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, vaNaN), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, vaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, ea)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, ea)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, eainf), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, eainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, eaNaN), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, eaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, vva)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vva)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, vvainf), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, vvainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, vvaNaN), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, vvaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, vea)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, vea)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, veainf), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, veainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, veaNaN), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, veaNaN), std::domain_error); } @@ -270,59 +219,59 @@ TEST(ode_bdf_tol_prim, two_arg_errors) { std::vector veainf = {eainf}; std::vector veaNaN = {eaNaN}; - EXPECT_NO_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, a)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, a)); - EXPECT_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, ainf), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a, ainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, aNaN), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a, aNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, va)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, va)); - EXPECT_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, vainf), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a, vainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, vaNaN), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a, vaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, ea)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, ea)); - EXPECT_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, eainf), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a, eainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, eaNaN), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a, eaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, vva)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vva)); - EXPECT_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, vvainf), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a, vvainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, vvaNaN), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a, vvaNaN), std::domain_error); - EXPECT_NO_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, - 1e6, nullptr, a, vea)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, + 1e-10, 1e-10, 1e6, nullptr, a, vea)); - EXPECT_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, veainf), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a, veainf), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a, veaNaN), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a, veaNaN), std::domain_error); } @@ -338,19 +287,19 @@ TEST(ode_bdf_tol_prim, rtol_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, rtol, 1e-10, - 1e6, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + rtol, 1e-10, 1e6, nullptr, a)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, rtol_negative, - 1e-10, 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + rtol_negative, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, rtolinf, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + rtolinf, 1e-10, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, rtolNaN, 1e-10, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + rtolNaN, 1e-10, 1e6, nullptr, a), std::domain_error); } @@ -366,19 +315,19 @@ TEST(ode_bdf_tol_prim, atol_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, atol, - 1e6, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, atol, 1e6, nullptr, a)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, atol_negative, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, atolinf, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, + atolinf, 1e6, nullptr, a), std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, atolNaN, - 1e6, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, + atolNaN, 1e6, nullptr, a), std::domain_error); } @@ -393,15 +342,17 @@ TEST(ode_bdf_tol_prim, max_num_steps_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - max_num_steps, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, max_num_steps, nullptr, + a)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - max_num_steps_negative, nullptr, a), - std::domain_error); + EXPECT_THROW( + stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, 1e-6, + max_num_steps_negative, nullptr, a), + std::domain_error); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - max_num_steps_zero, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-6, + 1e-6, max_num_steps_zero, nullptr, a), std::domain_error); } @@ -412,11 +363,11 @@ TEST(ode_bdf_tol_prim, rhs_wrong_size_errors) { double a = 1.5; - EXPECT_NO_THROW(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - 100, nullptr, a)); + EXPECT_NO_THROW(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, a)); - EXPECT_THROW(stan::math::ode_bdf_tol(CosArgWrongSize(), y0, t0, ts, 1e-6, - 1e-6, 100, nullptr, a), + EXPECT_THROW(stan::math::ode_bdf_tol(stan::test::CosArgWrongSize(), y0, t0, + ts, 1e-6, 1e-6, 100, nullptr, a), std::invalid_argument); } @@ -427,8 +378,8 @@ TEST(ode_bdf_tol_prim, error_name) { double ainf = stan::math::INFTY; - EXPECT_THROW_MSG(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - 100, nullptr, ainf), + EXPECT_THROW_MSG(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, ainf), std::domain_error, "ode_bdf_tol"); } @@ -439,8 +390,8 @@ TEST(ode_bdf_tol_prim, too_much_work) { double a = 1.0; - EXPECT_THROW_MSG(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - 100, nullptr, a), + EXPECT_THROW_MSG(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, a), std::domain_error, "ode_bdf_tol: Failed to integrate to next output time"); } diff --git a/test/unit/math/rev/functor/ode_bdf_tol_rev_test.cpp b/test/unit/math/rev/functor/ode_bdf_tol_rev_test.cpp index c1536d3ddd2..7ea65aab0ef 100644 --- a/test/unit/math/rev/functor/ode_bdf_tol_rev_test.cpp +++ b/test/unit/math/rev/functor/ode_bdf_tol_rev_test.cpp @@ -1,54 +1,10 @@ #include #include +#include #include #include #include -using stan::math::var; - -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - TEST(StanMathOde_ode_bdf_tol, int_t0) { using stan::math::var; @@ -59,8 +15,8 @@ TEST(StanMathOde_ode_bdf_tol, int_t0) { double a = 1.5; std::vector> output - = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, 1e-10, + 1e6, nullptr, a); EXPECT_FLOAT_EQ(output[0][0], 0.4165982112); EXPECT_FLOAT_EQ(output[1][0], 0.66457668563); @@ -76,8 +32,8 @@ TEST(StanMathOde_ode_bdf_tol, int_ts) { double a = 1.5; std::vector> output - = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, 1e-10, + 1e6, nullptr, a); EXPECT_FLOAT_EQ(output[0][0], 0.6649966577); EXPECT_FLOAT_EQ(output[1][0], 0.09408000537); @@ -93,8 +49,8 @@ TEST(StanMathOde_ode_bdf_tol, t0) { double a = 1.5; std::vector> output - = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, 1e-10, + 1e6, nullptr, a); output[0][0].grad(); @@ -119,8 +75,8 @@ TEST(StanMathOde_ode_bdf_tol, ts) { double a = 1.5; std::vector> output - = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, 1e-10, + 1e6, nullptr, a); output[0][0].grad(); @@ -145,8 +101,8 @@ TEST(StanMathOde_ode_bdf_tol, ts_repeat) { double a = 1.5; std::vector> output - = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, 1e-10, + 1e6, nullptr, a); EXPECT_EQ(output.size(), ts.size()); @@ -186,8 +142,8 @@ TEST(StanMathOde_ode_bdf_tol, scalar_arg) { var a = 1.5; - var output = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a)[0][0]; + var output = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -205,8 +161,8 @@ TEST(StanMathOde_ode_bdf_tol, scalar_arg_multi_time) { var a = 1.5; std::vector> output - = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, 1e-10, + 1e6, nullptr, a); output[0](0).grad(); @@ -230,8 +186,8 @@ TEST(StanMathOde_ode_bdf_tol, std_vector_arg) { std::vector a = {1.5}; - var output = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a)[0][0]; + var output = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -249,8 +205,8 @@ TEST(StanMathOde_ode_bdf_tol, vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a)[0][0]; + var output = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -268,8 +224,8 @@ TEST(StanMathOde_ode_bdf_tol, row_vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a)[0][0]; + var output = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -287,8 +243,8 @@ TEST(StanMathOde_ode_bdf_tol, matrix_arg) { Eigen::Matrix a(1, 1); a << 1.5; - var output = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a)[0][0]; + var output = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -306,8 +262,8 @@ TEST(StanMathOde_ode_bdf_tol, scalar_std_vector_args) { var a0 = 0.75; std::vector a1 = {0.75}; - var output = stan::math::ode_bdf_tol(Cos2Arg(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a0, a1)[0][0]; + var output = stan::math::ode_bdf_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a0, a1)[0][0]; output.grad(); @@ -327,8 +283,8 @@ TEST(StanMathOde_ode_bdf_tol, std_vector_std_vector_args) { std::vector a1(1, a0); std::vector> a2(1, a1); - var output = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a2)[0][0]; + var output = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -348,8 +304,8 @@ TEST(StanMathOde_ode_bdf_tol, std_vector_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a2)[0][0]; + var output = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -369,8 +325,8 @@ TEST(StanMathOde_ode_bdf_tol, std_vector_row_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a2)[0][0]; + var output = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -390,8 +346,8 @@ TEST(StanMathOde_ode_bdf_tol, std_vector_matrix_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a2)[0][0]; + var output = stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -399,16 +355,9 @@ TEST(StanMathOde_ode_bdf_tol, std_vector_matrix_args) { EXPECT_FLOAT_EQ(a2[0](0).adj(), -0.50107310888); } -struct ayt { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a) const { - return -a * y * t; - } -}; - TEST(StanMathOde_ode_bdf_tol, arg_combos_test) { + using stan::math::var; + var t0 = 0.5; var a = 0.2; std::vector ts = {1.25}; @@ -448,57 +397,57 @@ TEST(StanMathOde_ode_bdf_tol, arg_combos_test) { exp(-0.5 * ad * (tsd[0] * tsd[0] - t0d * t0d))); }; - double yT1 = stan::math::ode_bdf_tol(ayt(), y0d, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + double yT1 = stan::math::ode_bdf_tol(stan::test::ayt(), y0d, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); check_yT(yT1); - var yT2 = stan::math::ode_bdf_tol(ayt(), y0d, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT2 = stan::math::ode_bdf_tol(stan::test::ayt(), y0d, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT2.grad(); check_yT(yT2); check_a(a); - var yT3 = stan::math::ode_bdf_tol(ayt(), y0d, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT3 = stan::math::ode_bdf_tol(stan::test::ayt(), y0d, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT3.grad(); check_yT(yT3); check_ts(ts); - var yT4 = stan::math::ode_bdf_tol(ayt(), y0d, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT4 = stan::math::ode_bdf_tol(stan::test::ayt(), y0d, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT4.grad(); check_yT(yT4); check_ts(ts); check_a(a); - var yT5 = stan::math::ode_bdf_tol(ayt(), y0d, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT5 = stan::math::ode_bdf_tol(stan::test::ayt(), y0d, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT5.grad(); check_yT(yT5); check_t0(t0); - var yT6 = stan::math::ode_bdf_tol(ayt(), y0d, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT6 = stan::math::ode_bdf_tol(stan::test::ayt(), y0d, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT6.grad(); check_yT(yT6); check_t0(t0); check_a(a); - var yT7 = stan::math::ode_bdf_tol(ayt(), y0d, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT7 = stan::math::ode_bdf_tol(stan::test::ayt(), y0d, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT7.grad(); check_yT(yT7); check_t0(t0); check_ts(ts); - var yT8 = stan::math::ode_bdf_tol(ayt(), y0d, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT8 = stan::math::ode_bdf_tol(stan::test::ayt(), y0d, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT8.grad(); check_yT(yT8); @@ -506,31 +455,31 @@ TEST(StanMathOde_ode_bdf_tol, arg_combos_test) { check_ts(ts); check_a(a); - var yT9 = stan::math::ode_bdf_tol(ayt(), y0, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT9 = stan::math::ode_bdf_tol(stan::test::ayt(), y0, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT9.grad(); check_yT(yT9); check_y0(y0); - var yT10 = stan::math::ode_bdf_tol(ayt(), y0, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT10 = stan::math::ode_bdf_tol(stan::test::ayt(), y0, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT10.grad(); check_yT(yT10); check_y0(y0); check_a(a); - var yT11 = stan::math::ode_bdf_tol(ayt(), y0, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT11 = stan::math::ode_bdf_tol(stan::test::ayt(), y0, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT11.grad(); check_yT(yT11); check_y0(y0); check_ts(ts); - var yT12 = stan::math::ode_bdf_tol(ayt(), y0, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT12 = stan::math::ode_bdf_tol(stan::test::ayt(), y0, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT12.grad(); check_yT(yT12); @@ -538,16 +487,16 @@ TEST(StanMathOde_ode_bdf_tol, arg_combos_test) { check_ts(ts); check_a(a); - var yT13 = stan::math::ode_bdf_tol(ayt(), y0, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT13 = stan::math::ode_bdf_tol(stan::test::ayt(), y0, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT13.grad(); check_yT(yT13); check_y0(y0); check_t0(t0); - var yT14 = stan::math::ode_bdf_tol(ayt(), y0, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT14 = stan::math::ode_bdf_tol(stan::test::ayt(), y0, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT14.grad(); check_yT(yT14); @@ -555,8 +504,8 @@ TEST(StanMathOde_ode_bdf_tol, arg_combos_test) { check_t0(t0); check_a(a); - var yT15 = stan::math::ode_bdf_tol(ayt(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT15 = stan::math::ode_bdf_tol(stan::test::ayt(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT15.grad(); check_yT(yT15); @@ -564,8 +513,8 @@ TEST(StanMathOde_ode_bdf_tol, arg_combos_test) { check_t0(t0); check_ts(ts); - var yT16 = stan::math::ode_bdf_tol(ayt(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT16 = stan::math::ode_bdf_tol(stan::test::ayt(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT16.grad(); check_yT(yT16); @@ -576,6 +525,8 @@ TEST(StanMathOde_ode_bdf_tol, arg_combos_test) { } TEST(StanMathOde_ode_bdf_tol, too_much_work) { + using stan::math::var; + Eigen::Matrix y0 = Eigen::VectorXd::Zero(1).template cast(); var t0 = 0; @@ -583,8 +534,8 @@ TEST(StanMathOde_ode_bdf_tol, too_much_work) { var a = 1.0; - EXPECT_THROW_MSG(stan::math::ode_bdf_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - 100, nullptr, a), + EXPECT_THROW_MSG(stan::math::ode_bdf_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, a), std::domain_error, "ode_bdf_tol: Failed to integrate to next output time"); } diff --git a/test/unit/math/rev/functor/ode_rk45_rev_test.cpp b/test/unit/math/rev/functor/ode_rk45_rev_test.cpp index 40291d726f3..a1665e53e35 100644 --- a/test/unit/math/rev/functor/ode_rk45_rev_test.cpp +++ b/test/unit/math/rev/functor/ode_rk45_rev_test.cpp @@ -1,54 +1,9 @@ #include #include +#include #include #include -using stan::math::var; - -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const T_y& y, std::ostream* msgs, - const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out( - 1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - TEST(StanMathOde_ode_rk45, int_t0) { using stan::math::var; @@ -59,7 +14,7 @@ TEST(StanMathOde_ode_rk45, int_t0) { double a = 1.5; std::vector> output - = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, a); EXPECT_NEAR(output[0][0], 0.4165982112, 1e-5); EXPECT_NEAR(output[1][0], 0.66457668563, 1e-5); @@ -75,7 +30,7 @@ TEST(StanMathOde_ode_rk45, int_ts) { double a = 1.5; std::vector> output - = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, a); EXPECT_NEAR(output[0][0], 0.6649966577, 1e-5); EXPECT_NEAR(output[1][0], 0.09408000537, 1e-5); @@ -91,7 +46,7 @@ TEST(StanMathOde_ode_rk45, t0) { double a = 1.5; std::vector> output - = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, a); output[0][0].grad(); @@ -116,7 +71,7 @@ TEST(StanMathOde_ode_rk45, ts) { double a = 1.5; std::vector> output - = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, a); output[0][0].grad(); @@ -141,7 +96,7 @@ TEST(StanMathOde_ode_rk45, ts_repeat) { double a = 1.5; std::vector> output - = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, a); EXPECT_EQ(output.size(), ts.size()); @@ -181,7 +136,8 @@ TEST(StanMathOde_ode_rk45, scalar_arg) { var a = 1.5; - var output = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -199,7 +155,7 @@ TEST(StanMathOde_ode_rk45, scalar_arg_multi_time) { var a = 1.5; std::vector> output - = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a); + = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, a); output[0](0).grad(); @@ -223,7 +179,8 @@ TEST(StanMathOde_ode_rk45, std_vector_arg) { std::vector a = {1.5}; - var output = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -241,7 +198,8 @@ TEST(StanMathOde_ode_rk45, vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -259,7 +217,8 @@ TEST(StanMathOde_ode_rk45, row_vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -277,7 +236,8 @@ TEST(StanMathOde_ode_rk45, matrix_arg) { Eigen::Matrix a(1, 1); a << 1.5; - var output = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a)[0][0]; + var output = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, + a)[0][0]; output.grad(); @@ -295,8 +255,8 @@ TEST(StanMathOde_ode_rk45, scalar_std_vector_args) { var a0 = 0.75; std::vector a1 = {0.75}; - var output - = stan::math::ode_rk45(Cos2Arg(), y0, t0, ts, nullptr, a0, a1)[0][0]; + var output = stan::math::ode_rk45(stan::test::Cos2Arg(), y0, t0, ts, nullptr, + a0, a1)[0][0]; output.grad(); @@ -316,7 +276,8 @@ TEST(StanMathOde_ode_rk45, std_vector_std_vector_args) { std::vector a1(1, a0); std::vector> a2(1, a1); - var output = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -336,7 +297,8 @@ TEST(StanMathOde_ode_rk45, std_vector_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -356,7 +318,8 @@ TEST(StanMathOde_ode_rk45, std_vector_row_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -376,7 +339,8 @@ TEST(StanMathOde_ode_rk45, std_vector_matrix_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_rk45(CosArg1(), y0, t0, ts, nullptr, a2)[0][0]; + var output = stan::math::ode_rk45(stan::test::CosArg1(), y0, t0, ts, nullptr, + a2)[0][0]; output.grad(); @@ -384,15 +348,8 @@ TEST(StanMathOde_ode_rk45, std_vector_matrix_args) { EXPECT_NEAR(a2[0](0).adj(), -0.50107310888, 1e-5); } -struct ayt { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const T_y& y, std::ostream* msgs, const T2& a) const { - return -a * y * t; - } -}; - TEST(StanMathOde_ode_rk45, arg_combos_test) { + using stan::math::var; var t0 = 0.5; var a = 0.2; std::vector ts = {1.25}; @@ -435,49 +392,57 @@ TEST(StanMathOde_ode_rk45, arg_combos_test) { 1e-5); }; - double yT1 = stan::math::ode_rk45(ayt(), y0d, t0d, tsd, nullptr, ad)[0](0); + double yT1 = stan::math::ode_rk45(stan::test::ayt(), y0d, t0d, tsd, nullptr, + ad)[0](0); check_yT(yT1); - var yT2 = stan::math::ode_rk45(ayt(), y0d, t0d, tsd, nullptr, a)[0](0); + var yT2 = stan::math::ode_rk45(stan::test::ayt(), y0d, t0d, tsd, nullptr, + a)[0](0); stan::math::set_zero_all_adjoints(); yT2.grad(); check_yT(yT2); check_a(a); - var yT3 = stan::math::ode_rk45(ayt(), y0d, t0d, ts, nullptr, ad)[0](0); + var yT3 = stan::math::ode_rk45(stan::test::ayt(), y0d, t0d, ts, nullptr, + ad)[0](0); stan::math::set_zero_all_adjoints(); yT3.grad(); check_yT(yT3); check_ts(ts); - var yT4 = stan::math::ode_rk45(ayt(), y0d, t0d, ts, nullptr, a)[0](0); + var yT4 + = stan::math::ode_rk45(stan::test::ayt(), y0d, t0d, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT4.grad(); check_yT(yT4); check_ts(ts); check_a(a); - var yT5 = stan::math::ode_rk45(ayt(), y0d, t0, tsd, nullptr, ad)[0](0); + var yT5 = stan::math::ode_rk45(stan::test::ayt(), y0d, t0, tsd, nullptr, + ad)[0](0); stan::math::set_zero_all_adjoints(); yT5.grad(); check_yT(yT5); check_t0(t0); - var yT6 = stan::math::ode_rk45(ayt(), y0d, t0, tsd, nullptr, a)[0](0); + var yT6 + = stan::math::ode_rk45(stan::test::ayt(), y0d, t0, tsd, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT6.grad(); check_yT(yT6); check_t0(t0); check_a(a); - var yT7 = stan::math::ode_rk45(ayt(), y0d, t0, ts, nullptr, ad)[0](0); + var yT7 + = stan::math::ode_rk45(stan::test::ayt(), y0d, t0, ts, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT7.grad(); check_yT(yT7); check_t0(t0); check_ts(ts); - var yT8 = stan::math::ode_rk45(ayt(), y0d, t0, ts, nullptr, a)[0](0); + var yT8 + = stan::math::ode_rk45(stan::test::ayt(), y0d, t0, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT8.grad(); check_yT(yT8); @@ -485,27 +450,31 @@ TEST(StanMathOde_ode_rk45, arg_combos_test) { check_ts(ts); check_a(a); - var yT9 = stan::math::ode_rk45(ayt(), y0, t0d, tsd, nullptr, ad)[0](0); + var yT9 = stan::math::ode_rk45(stan::test::ayt(), y0, t0d, tsd, nullptr, + ad)[0](0); stan::math::set_zero_all_adjoints(); yT9.grad(); check_yT(yT9); check_y0(y0); - var yT10 = stan::math::ode_rk45(ayt(), y0, t0d, tsd, nullptr, a)[0](0); + var yT10 + = stan::math::ode_rk45(stan::test::ayt(), y0, t0d, tsd, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT10.grad(); check_yT(yT10); check_y0(y0); check_a(a); - var yT11 = stan::math::ode_rk45(ayt(), y0, t0d, ts, nullptr, ad)[0](0); + var yT11 + = stan::math::ode_rk45(stan::test::ayt(), y0, t0d, ts, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT11.grad(); check_yT(yT11); check_y0(y0); check_ts(ts); - var yT12 = stan::math::ode_rk45(ayt(), y0, t0d, ts, nullptr, a)[0](0); + var yT12 + = stan::math::ode_rk45(stan::test::ayt(), y0, t0d, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT12.grad(); check_yT(yT12); @@ -513,14 +482,16 @@ TEST(StanMathOde_ode_rk45, arg_combos_test) { check_ts(ts); check_a(a); - var yT13 = stan::math::ode_rk45(ayt(), y0, t0, tsd, nullptr, ad)[0](0); + var yT13 + = stan::math::ode_rk45(stan::test::ayt(), y0, t0, tsd, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT13.grad(); check_yT(yT13); check_y0(y0); check_t0(t0); - var yT14 = stan::math::ode_rk45(ayt(), y0, t0, tsd, nullptr, a)[0](0); + var yT14 + = stan::math::ode_rk45(stan::test::ayt(), y0, t0, tsd, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT14.grad(); check_yT(yT14); @@ -528,7 +499,8 @@ TEST(StanMathOde_ode_rk45, arg_combos_test) { check_t0(t0); check_a(a); - var yT15 = stan::math::ode_rk45(ayt(), y0, t0, ts, nullptr, ad)[0](0); + var yT15 + = stan::math::ode_rk45(stan::test::ayt(), y0, t0, ts, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT15.grad(); check_yT(yT15); @@ -536,7 +508,8 @@ TEST(StanMathOde_ode_rk45, arg_combos_test) { check_t0(t0); check_ts(ts); - var yT16 = stan::math::ode_rk45(ayt(), y0, t0, ts, nullptr, a)[0](0); + var yT16 + = stan::math::ode_rk45(stan::test::ayt(), y0, t0, ts, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT16.grad(); check_yT(yT16); diff --git a/test/unit/math/rev/functor/ode_rk45_tol_rev_test.cpp b/test/unit/math/rev/functor/ode_rk45_tol_rev_test.cpp index 7db3e1e2aee..4c2b1e7d083 100644 --- a/test/unit/math/rev/functor/ode_rk45_tol_rev_test.cpp +++ b/test/unit/math/rev/functor/ode_rk45_tol_rev_test.cpp @@ -1,54 +1,10 @@ #include #include #include +#include #include #include -using stan::math::var; - -template * = nullptr> -T sum_(T arg) { - return arg; -} - -template * = nullptr> -auto sum_(EigMat&& arg) { - return stan::math::sum(arg); -} - -template * = nullptr> -auto sum_(Vec&& arg) { - stan::scalar_type_t sum = 0; - for (size_t i = 0; i < arg.size(); ++i) { - sum += sum_(arg[i]); - } - return sum; -} - -struct CosArg1 { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T_Args&... a) const { - std::vector::type> vec - = {sum_(a)...}; - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos(sum_(vec) * t); - return out; - } -}; - -struct Cos2Arg { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a, const T3& b) const { - Eigen::Matrix, Eigen::Dynamic, 1> out(1); - out << stan::math::cos((sum_(a) + sum_(b)) * t); - return out; - } -}; - TEST(StanMathOde_ode_rk45_tol, int_t0) { using stan::math::var; @@ -59,8 +15,8 @@ TEST(StanMathOde_ode_rk45_tol, int_t0) { double a = 1.5; std::vector> output - = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); EXPECT_FLOAT_EQ(output[0][0], 0.4165982112); EXPECT_FLOAT_EQ(output[1][0], 0.66457668563); @@ -76,8 +32,8 @@ TEST(StanMathOde_ode_rk45_tol, int_ts) { double a = 1.5; std::vector> output - = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); EXPECT_FLOAT_EQ(output[0][0], 0.6649966577); EXPECT_FLOAT_EQ(output[1][0], 0.09408000537); @@ -93,8 +49,8 @@ TEST(StanMathOde_ode_rk45_tol, t0) { double a = 1.5; std::vector> output - = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); output[0][0].grad(); @@ -119,8 +75,8 @@ TEST(StanMathOde_ode_rk45_tol, ts) { double a = 1.5; std::vector> output - = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); output[0][0].grad(); @@ -145,8 +101,8 @@ TEST(StanMathOde_ode_rk45_tol, ts_repeat) { double a = 1.5; std::vector> output - = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); EXPECT_EQ(output.size(), ts.size()); @@ -186,8 +142,8 @@ TEST(StanMathOde_ode_rk45_tol, scalar_arg) { var a = 1.5; - var output = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a)[0][0]; + var output = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -205,8 +161,8 @@ TEST(StanMathOde_ode_rk45_tol, scalar_arg_multi_time) { var a = 1.5; std::vector> output - = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a); + = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a); output[0](0).grad(); @@ -230,8 +186,8 @@ TEST(StanMathOde_ode_rk45_tol, std_vector_arg) { std::vector a = {1.5}; - var output = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a)[0][0]; + var output = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -249,8 +205,8 @@ TEST(StanMathOde_ode_rk45_tol, vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a)[0][0]; + var output = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -268,8 +224,8 @@ TEST(StanMathOde_ode_rk45_tol, row_vector_arg) { Eigen::Matrix a(1); a << 1.5; - var output = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a)[0][0]; + var output = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -287,8 +243,8 @@ TEST(StanMathOde_ode_rk45_tol, matrix_arg) { Eigen::Matrix a(1, 1); a << 1.5; - var output = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a)[0][0]; + var output = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a)[0][0]; output.grad(); @@ -306,8 +262,8 @@ TEST(StanMathOde_ode_rk45_tol, scalar_std_vector_args) { var a0 = 0.75; std::vector a1 = {0.75}; - var output = stan::math::ode_rk45_tol(Cos2Arg(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a0, a1)[0][0]; + var output = stan::math::ode_rk45_tol(stan::test::Cos2Arg(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a0, a1)[0][0]; output.grad(); @@ -327,8 +283,8 @@ TEST(StanMathOde_ode_rk45_tol, std_vector_std_vector_args) { std::vector a1(1, a0); std::vector> a2(1, a1); - var output = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a2)[0][0]; + var output = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -348,8 +304,8 @@ TEST(StanMathOde_ode_rk45_tol, std_vector_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a2)[0][0]; + var output = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -369,8 +325,8 @@ TEST(StanMathOde_ode_rk45_tol, std_vector_row_vector_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a2)[0][0]; + var output = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -390,8 +346,8 @@ TEST(StanMathOde_ode_rk45_tol, std_vector_matrix_args) { a1 << a0; std::vector> a2(1, a1); - var output = stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-8, 1e-10, 1e6, - nullptr, a2)[0][0]; + var output = stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, 1e-8, + 1e-10, 1e6, nullptr, a2)[0][0]; output.grad(); @@ -399,16 +355,8 @@ TEST(StanMathOde_ode_rk45_tol, std_vector_matrix_args) { EXPECT_FLOAT_EQ(a2[0](0).adj(), -0.50107310888); } -struct ayt { - template - inline Eigen::Matrix, Eigen::Dynamic, 1> - operator()(const T0& t, const Eigen::Matrix& y, - std::ostream* msgs, const T2& a) const { - return -a * y * t; - } -}; - TEST(StanMathOde_ode_rk45_tol, arg_combos_test) { + using stan::math::var; var t0 = 0.5; var a = 0.2; std::vector ts = {1.25}; @@ -448,57 +396,57 @@ TEST(StanMathOde_ode_rk45_tol, arg_combos_test) { exp(-0.5 * ad * (tsd[0] * tsd[0] - t0d * t0d))); }; - double yT1 = stan::math::ode_rk45_tol(ayt(), y0d, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + double yT1 = stan::math::ode_rk45_tol(stan::test::ayt(), y0d, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); check_yT(yT1); - var yT2 = stan::math::ode_rk45_tol(ayt(), y0d, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT2 = stan::math::ode_rk45_tol(stan::test::ayt(), y0d, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT2.grad(); check_yT(yT2); check_a(a); - var yT3 = stan::math::ode_rk45_tol(ayt(), y0d, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT3 = stan::math::ode_rk45_tol(stan::test::ayt(), y0d, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT3.grad(); check_yT(yT3); check_ts(ts); - var yT4 = stan::math::ode_rk45_tol(ayt(), y0d, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT4 = stan::math::ode_rk45_tol(stan::test::ayt(), y0d, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT4.grad(); check_yT(yT4); check_ts(ts); check_a(a); - var yT5 = stan::math::ode_rk45_tol(ayt(), y0d, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT5 = stan::math::ode_rk45_tol(stan::test::ayt(), y0d, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT5.grad(); check_yT(yT5); check_t0(t0); - var yT6 = stan::math::ode_rk45_tol(ayt(), y0d, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT6 = stan::math::ode_rk45_tol(stan::test::ayt(), y0d, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT6.grad(); check_yT(yT6); check_t0(t0); check_a(a); - var yT7 = stan::math::ode_rk45_tol(ayt(), y0d, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT7 = stan::math::ode_rk45_tol(stan::test::ayt(), y0d, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT7.grad(); check_yT(yT7); check_t0(t0); check_ts(ts); - var yT8 = stan::math::ode_rk45_tol(ayt(), y0d, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT8 = stan::math::ode_rk45_tol(stan::test::ayt(), y0d, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT8.grad(); check_yT(yT8); @@ -506,31 +454,31 @@ TEST(StanMathOde_ode_rk45_tol, arg_combos_test) { check_ts(ts); check_a(a); - var yT9 = stan::math::ode_rk45_tol(ayt(), y0, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT9 = stan::math::ode_rk45_tol(stan::test::ayt(), y0, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT9.grad(); check_yT(yT9); check_y0(y0); - var yT10 = stan::math::ode_rk45_tol(ayt(), y0, t0d, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT10 = stan::math::ode_rk45_tol(stan::test::ayt(), y0, t0d, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT10.grad(); check_yT(yT10); check_y0(y0); check_a(a); - var yT11 = stan::math::ode_rk45_tol(ayt(), y0, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT11 = stan::math::ode_rk45_tol(stan::test::ayt(), y0, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT11.grad(); check_yT(yT11); check_y0(y0); check_ts(ts); - var yT12 = stan::math::ode_rk45_tol(ayt(), y0, t0d, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT12 = stan::math::ode_rk45_tol(stan::test::ayt(), y0, t0d, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT12.grad(); check_yT(yT12); @@ -538,16 +486,16 @@ TEST(StanMathOde_ode_rk45_tol, arg_combos_test) { check_ts(ts); check_a(a); - var yT13 = stan::math::ode_rk45_tol(ayt(), y0, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT13 = stan::math::ode_rk45_tol(stan::test::ayt(), y0, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT13.grad(); check_yT(yT13); check_y0(y0); check_t0(t0); - var yT14 = stan::math::ode_rk45_tol(ayt(), y0, t0, tsd, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT14 = stan::math::ode_rk45_tol(stan::test::ayt(), y0, t0, tsd, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT14.grad(); check_yT(yT14); @@ -555,8 +503,8 @@ TEST(StanMathOde_ode_rk45_tol, arg_combos_test) { check_t0(t0); check_a(a); - var yT15 = stan::math::ode_rk45_tol(ayt(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, ad)[0](0); + var yT15 = stan::math::ode_rk45_tol(stan::test::ayt(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, ad)[0](0); stan::math::set_zero_all_adjoints(); yT15.grad(); check_yT(yT15); @@ -564,8 +512,8 @@ TEST(StanMathOde_ode_rk45_tol, arg_combos_test) { check_t0(t0); check_ts(ts); - var yT16 = stan::math::ode_rk45_tol(ayt(), y0, t0, ts, 1e-10, 1e-10, 1e6, - nullptr, a)[0](0); + var yT16 = stan::math::ode_rk45_tol(stan::test::ayt(), y0, t0, ts, 1e-10, + 1e-10, 1e6, nullptr, a)[0](0); stan::math::set_zero_all_adjoints(); yT16.grad(); check_yT(yT16); @@ -576,6 +524,8 @@ TEST(StanMathOde_ode_rk45_tol, arg_combos_test) { } TEST(StanMathOde_ode_rk45_tol, too_much_work) { + using stan::math::var; + Eigen::Matrix y0 = Eigen::VectorXd::Zero(1).template cast(); var t0 = 0; @@ -583,8 +533,8 @@ TEST(StanMathOde_ode_rk45_tol, too_much_work) { var a = 1.0; - EXPECT_THROW_MSG(stan::math::ode_rk45_tol(CosArg1(), y0, t0, ts, 1e-6, 1e-6, - 100, nullptr, a), + EXPECT_THROW_MSG(stan::math::ode_rk45_tol(stan::test::CosArg1(), y0, t0, ts, + 1e-6, 1e-6, 100, nullptr, a), std::domain_error, "ode_rk45_tol: Failed to integrate to next output time"); }