From 3c0b5ea75d6dff49b12b0b184cc3e29181a832e4 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 5 Oct 2021 16:54:08 -0400 Subject: [PATCH 01/16] Adds specializations for handling sparse matrix unary operations and whether they should return values for zeroed out values --- stan/math/prim/fun/acos.hpp | 18 +++++- stan/math/prim/functor/apply_scalar_unary.hpp | 63 +++++++++++++++---- stan/math/prim/meta/is_container.hpp | 2 +- stan/math/prim/meta/is_eigen_matrix_base.hpp | 18 ++++++ stan/math/rev/functor/apply_scalar_unary.hpp | 8 +-- test/unit/math/mix/fun/acos_test.cpp | 1 + test/unit/math/mix/fun/atan_test.cpp | 1 + test/unit/math/test_ad.hpp | 19 ++++++ 8 files changed, 111 insertions(+), 19 deletions(-) diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index d529ad04d48..8c5b35c5bdc 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -47,7 +47,7 @@ template * = nullptr> inline auto acos(const Container& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::value>::apply(x); } /** @@ -64,7 +64,21 @@ inline auto acos(const Container& x) { return apply_vector_unary::apply( x, [](const auto& v) { return v.array().acos(); }); } - +/* +template * = nullptr> +inline auto acos(const SparseMat& x) { + using val_t = value_type_t; + auto zeroed_val = stan::math::acos(val_t(0.0)); + using eig_mat = Eigen::Matrix; + eig_mat ret = eig_mat::Constant(x.rows(), x.cols(), zeroed_val); + for (Eigen::Index k = 0; k < x.outerSize(); ++k) { + for (typename SparseMat::InnerIterator it(x, k); it; ++it) { + ret.coeffRef(it.row(), it.col()) = acos(it.value()); + } + } + return ret; +} +*/ namespace internal { /** * Return the arc cosine of the complex argument. diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index 1dbc8b6f27d..55289e325b9 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -37,7 +37,7 @@ namespace math { * @tparam F Type of function to apply. * @tparam T Type of argument to which function is applied. */ -template +template struct apply_scalar_unary; /** @@ -48,8 +48,8 @@ struct apply_scalar_unary; * @tparam F Type of function to apply. * @tparam T Type of argument to which function is applied. */ -template -struct apply_scalar_unary> { +template +struct apply_scalar_unary> { /** * Type of underlying scalar for the matrix type T. */ @@ -63,11 +63,50 @@ struct apply_scalar_unary> { * @return Componentwise application of the function specified * by F to the specified matrix. */ - static inline auto apply(const T& x) { + template * = nullptr> + static inline auto apply(const TT& x) { return x.unaryExpr( [](scalar_t x) { return apply_scalar_unary::apply(x); }); } + /** + * Return the result of applying the function defined by the + * template parameter F to the specified matrix argument. + * + * @param x Matrix to which operation is applied. + * @return Componentwise application of the function specified + * by F to the specified matrix. + */ + template >* = nullptr, + require_eigen_sparse_matrix_base_t* = nullptr> + static inline auto apply(const TT& x) { + using val_t = value_type_t; + auto zeroed_val = apply_scalar_unary::apply(val_t(0.0)); + using eig_mat = Eigen::Matrix; + eig_mat ret = eig_mat::Constant(x.rows(), x.cols(), zeroed_val); + for (Eigen::Index k = 0; k < x.outerSize(); ++k) { + for (typename TT::InnerIterator it(x, k); it; ++it) { + ret.coeffRef(it.row(), it.col()) = apply_scalar_unary::apply(it.value()); + } + } + return ret; + } + + template >* = nullptr, + require_eigen_sparse_matrix_base_t* = nullptr> + static inline auto apply(const TT& x) { + auto ret = x.eval(); + for (Eigen::Index k = 0; k < x.outerSize(); ++k) { + for (typename TT::InnerIterator it(x, k), ret_it(ret, k); it; ++it, ++ret_it) { + ret_it.valueRef() = apply_scalar_unary::apply(it.value()); + } + } + return ret; + } + + /** * Return type for applying the function elementwise to a matrix * expression template of type T. @@ -82,8 +121,8 @@ struct apply_scalar_unary> { * * @tparam F Type of function defining static apply function. */ -template -struct apply_scalar_unary> { +template +struct apply_scalar_unary> { /** * The return type, double. */ @@ -107,8 +146,8 @@ struct apply_scalar_unary> { * * @tparam F Type of function defining static apply function. */ -template -struct apply_scalar_unary> { +template +struct apply_scalar_unary> { /** * The return type, double. */ @@ -134,8 +173,8 @@ struct apply_scalar_unary> { * * @tparam F Type of function defining static apply function. */ -template -struct apply_scalar_unary> { +template +struct apply_scalar_unary> { /** * The return type, double. */ @@ -162,8 +201,8 @@ struct apply_scalar_unary> { * @tparam F Class defining a static apply function. * @tparam T Type of element contained in standard vector. */ -template -struct apply_scalar_unary> { +template +struct apply_scalar_unary, ApplyZero, void> { /** * Return type, which is calculated recursively as a standard * vector of the return type of the contained type T. diff --git a/stan/math/prim/meta/is_container.hpp b/stan/math/prim/meta/is_container.hpp index b24f0f3d558..43a2764526c 100644 --- a/stan/math/prim/meta/is_container.hpp +++ b/stan/math/prim/meta/is_container.hpp @@ -20,7 +20,7 @@ namespace stan { */ template using is_container = bool_constant< - math::disjunction, is_std_vector>::value>; + math::disjunction, is_std_vector>::value>; STAN_ADD_REQUIRE_UNARY(container, is_container, general_types); STAN_ADD_REQUIRE_CONTAINER(container, is_container, general_types); diff --git a/stan/math/prim/meta/is_eigen_matrix_base.hpp b/stan/math/prim/meta/is_eigen_matrix_base.hpp index 9e4e6e9465e..bf42b42e187 100644 --- a/stan/math/prim/meta/is_eigen_matrix_base.hpp +++ b/stan/math/prim/meta/is_eigen_matrix_base.hpp @@ -27,6 +27,24 @@ STAN_ADD_REQUIRE_UNARY(eigen_matrix_base, is_eigen_matrix_base, STAN_ADD_REQUIRE_CONTAINER(eigen_matrix_base, is_eigen_matrix_base, require_eigens_types); +/** +* Checks whether type T is derived from Eigen::MatrixBase. +* If true this will have a static member function named value with a type +* of true, else value is false. +* @tparam T Type to check if it is derived from `MatrixBase` +* @tparam Enable used for SFINAE deduction. +* @ingroup type_trait +*/ +template +struct is_eigen_sparse_matrix_base +: bool_constant::value> { +}; + +STAN_ADD_REQUIRE_UNARY(eigen_sparse_matrix_base, is_eigen_sparse_matrix_base, + require_eigens_types); +STAN_ADD_REQUIRE_CONTAINER(eigen_sparse_matrix_base, is_eigen_sparse_matrix_base, + require_eigens_types); + } // namespace stan #endif diff --git a/stan/math/rev/functor/apply_scalar_unary.hpp b/stan/math/rev/functor/apply_scalar_unary.hpp index d68bd344335..ce1411e4802 100644 --- a/stan/math/rev/functor/apply_scalar_unary.hpp +++ b/stan/math/rev/functor/apply_scalar_unary.hpp @@ -15,8 +15,8 @@ namespace math { * * @tparam F Type of function to apply. */ -template -struct apply_scalar_unary { +template +struct apply_scalar_unary { /** * Function return type, which is var. */ @@ -31,8 +31,8 @@ struct apply_scalar_unary { static inline return_t apply(const var& x) { return F::fun(x); } }; -template -struct apply_scalar_unary> { +template +struct apply_scalar_unary> { /** * Function return type, which is a `var_value` with plain value type. */ diff --git a/test/unit/math/mix/fun/acos_test.cpp b/test/unit/math/mix/fun/acos_test.cpp index 40219636ed7..a8263f44506 100644 --- a/test/unit/math/mix/fun/acos_test.cpp +++ b/test/unit/math/mix/fun/acos_test.cpp @@ -40,4 +40,5 @@ TEST(mathMixMatFun, acos_varmat) { A(i) = all_args[i]; } expect_ad_vector_matvar(f, A); + stan::test::expect_ad(stan::test::make_sparse_mat_func(f), A); } diff --git a/test/unit/math/mix/fun/atan_test.cpp b/test/unit/math/mix/fun/atan_test.cpp index 01ff681fa15..d62df358e3e 100644 --- a/test/unit/math/mix/fun/atan_test.cpp +++ b/test/unit/math/mix/fun/atan_test.cpp @@ -34,4 +34,5 @@ TEST(mathMixMatFun, atan_varmat) { A(i) = all_args[i]; } expect_ad_vector_matvar(f, A); + stan::test::expect_ad(stan::test::make_sparse_mat_func(f), A); } diff --git a/test/unit/math/test_ad.hpp b/test/unit/math/test_ad.hpp index 538b7c450a4..369d1bfcd9e 100644 --- a/test/unit/math/test_ad.hpp +++ b/test/unit/math/test_ad.hpp @@ -2130,6 +2130,25 @@ std::vector square_test_matrices(int low, int high) { return xs; } +template +auto gen_sparse_diag_mat(T1&& x) { + using triplet_t = Eigen::Triplet>; + std::vector tripletList; + tripletList.reserve(x.size()); + for (int i = 0; i < x.size(); i++) { + tripletList.emplace_back(i, i, x(i)); + } + Eigen::SparseMatrix> x_sparse(x.size(), x.size()); + x_sparse.setFromTriplets(tripletList.begin(), tripletList.end()); + x_sparse.makeCompressed(); + return x_sparse; +} + +template +auto make_sparse_mat_func(F&& f) { + return [&f](auto&& x) { return f(stan::test::gen_sparse_diag_mat(x));}; +} + } // namespace test } // namespace stan #endif From c647ad73d1b931d322e6041800006785c71db5da Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 5 Oct 2021 18:02:47 -0400 Subject: [PATCH 02/16] have checks for the correct return type for sparse matrix unary --- stan/math/prim/fun/acos.hpp | 18 +------- stan/math/prim/functor/apply_scalar_unary.hpp | 46 ++++++++++++------- test/unit/math/mix/fun/acos_test.cpp | 2 +- test/unit/math/mix/fun/atan_test.cpp | 2 +- test/unit/math/test_ad.hpp | 19 +++++++- 5 files changed, 51 insertions(+), 36 deletions(-) diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index 8c5b35c5bdc..93b77a217ee 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -47,7 +47,7 @@ template * = nullptr> inline auto acos(const Container& x) { - return apply_scalar_unary::value>::apply(x); + return apply_scalar_unary::apply(x); } /** @@ -64,21 +64,7 @@ inline auto acos(const Container& x) { return apply_vector_unary::apply( x, [](const auto& v) { return v.array().acos(); }); } -/* -template * = nullptr> -inline auto acos(const SparseMat& x) { - using val_t = value_type_t; - auto zeroed_val = stan::math::acos(val_t(0.0)); - using eig_mat = Eigen::Matrix; - eig_mat ret = eig_mat::Constant(x.rows(), x.cols(), zeroed_val); - for (Eigen::Index k = 0; k < x.outerSize(); ++k) { - for (typename SparseMat::InnerIterator it(x, k); it; ++it) { - ret.coeffRef(it.row(), it.col()) = acos(it.value()); - } - } - return ret; -} -*/ + namespace internal { /** * Return the arc cosine of the complex argument. diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index 55289e325b9..d7f8a96b9f5 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -36,6 +36,9 @@ namespace math { * * @tparam F Type of function to apply. * @tparam T Type of argument to which function is applied. + * @tparam ApplyZero If true, the function applied is assumed to return zero for + * inputs of zero and so sparse matrices will return sparse matrices. A value of + * false will return a dense matrix for sparse matrices. */ template struct apply_scalar_unary; @@ -59,47 +62,58 @@ struct apply_scalar_unary> { * Return the result of applying the function defined by the * template parameter F to the specified matrix argument. * + * @tparam DenseMat A type derived from `Eigen::DenseBase`. * @param x Matrix to which operation is applied. * @return Componentwise application of the function specified * by F to the specified matrix. */ - template * = nullptr> - static inline auto apply(const TT& x) { + template * = nullptr> + static inline auto apply(const DenseMat& x) { return x.unaryExpr( [](scalar_t x) { return apply_scalar_unary::apply(x); }); } /** - * Return the result of applying the function defined by the - * template parameter F to the specified matrix argument. + * Special case for `ApplyZero` set to true, returning a dense matrix. Return the result of applying the function defined by the template parameter F to the specified matrix argument. * + * @param SparseMat A type derived from `Eigen::SparseMatrixBase` + * @tparam NonZeroZero Shortcut trick for using class template for deduction, should not be set manually. * @param x Matrix to which operation is applied. * @return Componentwise application of the function specified * by F to the specified matrix. */ - template >* = nullptr, - require_eigen_sparse_matrix_base_t* = nullptr> - static inline auto apply(const TT& x) { - using val_t = value_type_t; + template >* = nullptr, + require_eigen_sparse_matrix_base_t* = nullptr> + static inline auto apply(const SparseMat& x) { + using val_t = value_type_t; auto zeroed_val = apply_scalar_unary::apply(val_t(0.0)); - using eig_mat = Eigen::Matrix; - eig_mat ret = eig_mat::Constant(x.rows(), x.cols(), zeroed_val); + using dense_mat = Eigen::Matrix; + dense_mat ret = dense_mat::Constant(x.rows(), x.cols(), zeroed_val); for (Eigen::Index k = 0; k < x.outerSize(); ++k) { - for (typename TT::InnerIterator it(x, k); it; ++it) { + for (typename SparseMat::InnerIterator it(x, k); it; ++it) { ret.coeffRef(it.row(), it.col()) = apply_scalar_unary::apply(it.value()); } } return ret; } - template >* = nullptr, - require_eigen_sparse_matrix_base_t* = nullptr> - static inline auto apply(const TT& x) { + require_eigen_sparse_matrix_base_t* = nullptr> + static inline auto apply(const SparseMat& x) { auto ret = x.eval(); for (Eigen::Index k = 0; k < x.outerSize(); ++k) { - for (typename TT::InnerIterator it(x, k), ret_it(ret, k); it; ++it, ++ret_it) { + for (typename SparseMat::InnerIterator it(x, k), ret_it(ret, k); it; ++it, ++ret_it) { ret_it.valueRef() = apply_scalar_unary::apply(it.value()); } } diff --git a/test/unit/math/mix/fun/acos_test.cpp b/test/unit/math/mix/fun/acos_test.cpp index a8263f44506..9262d456a7e 100644 --- a/test/unit/math/mix/fun/acos_test.cpp +++ b/test/unit/math/mix/fun/acos_test.cpp @@ -40,5 +40,5 @@ TEST(mathMixMatFun, acos_varmat) { A(i) = all_args[i]; } expect_ad_vector_matvar(f, A); - stan::test::expect_ad(stan::test::make_sparse_mat_func(f), A); + stan::test::expect_ad(stan::test::make_sparse_mat_func(f), A); } diff --git a/test/unit/math/mix/fun/atan_test.cpp b/test/unit/math/mix/fun/atan_test.cpp index d62df358e3e..4c3f7e90c40 100644 --- a/test/unit/math/mix/fun/atan_test.cpp +++ b/test/unit/math/mix/fun/atan_test.cpp @@ -34,5 +34,5 @@ TEST(mathMixMatFun, atan_varmat) { A(i) = all_args[i]; } expect_ad_vector_matvar(f, A); - stan::test::expect_ad(stan::test::make_sparse_mat_func(f), A); + stan::test::expect_ad(stan::test::make_sparse_mat_func(f), A); } diff --git a/test/unit/math/test_ad.hpp b/test/unit/math/test_ad.hpp index 369d1bfcd9e..b002d6f3035 100644 --- a/test/unit/math/test_ad.hpp +++ b/test/unit/math/test_ad.hpp @@ -2144,9 +2144,24 @@ auto gen_sparse_diag_mat(T1&& x) { return x_sparse; } -template +/** + * Takes a unary lambda f taking a vector and makes a diagonal sparse matrix. + * @tparam ExpectDenseReturn if true, the return is checked for whether it's type + * is derived from `Eigen::DenseBase`, otherwise the return type is checked + * for whether it's type is derived from `Eigen::SparseMatrixBase`. + * @tparam A lambda + * @param An unary lambda + */ +template auto make_sparse_mat_func(F&& f) { - return [&f](auto&& x) { return f(stan::test::gen_sparse_diag_mat(x));}; + return [&f](auto&& x) { + auto ret = f(stan::test::gen_sparse_diag_mat(x)); + constexpr bool ret_bool = std::conditional_t, + stan::is_eigen_sparse_matrix_base>::value; + EXPECT_TRUE(ret_bool); + return ret; + }; } } // namespace test From 7589e5916991cff5809b2ad709a7f68b132c38b5 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 5 Oct 2021 18:09:39 -0400 Subject: [PATCH 03/16] update tests --- stan/math/prim/functor/apply_scalar_unary.hpp | 4 ++-- stan/math/prim/meta/is_eigen_matrix_base.hpp | 17 ----------------- test/unit/math/test_ad.hpp | 2 +- 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index d7f8a96b9f5..c05b9b77ffe 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -84,7 +84,7 @@ struct apply_scalar_unary> { */ template >* = nullptr, - require_eigen_sparse_matrix_base_t* = nullptr> + require_eigen_sparse_base_t* = nullptr> static inline auto apply(const SparseMat& x) { using val_t = value_type_t; auto zeroed_val = apply_scalar_unary::apply(val_t(0.0)); @@ -109,7 +109,7 @@ struct apply_scalar_unary> { */ template >* = nullptr, - require_eigen_sparse_matrix_base_t* = nullptr> + require_eigen_sparse_base_t* = nullptr> static inline auto apply(const SparseMat& x) { auto ret = x.eval(); for (Eigen::Index k = 0; k < x.outerSize(); ++k) { diff --git a/stan/math/prim/meta/is_eigen_matrix_base.hpp b/stan/math/prim/meta/is_eigen_matrix_base.hpp index bf42b42e187..0befcace346 100644 --- a/stan/math/prim/meta/is_eigen_matrix_base.hpp +++ b/stan/math/prim/meta/is_eigen_matrix_base.hpp @@ -27,23 +27,6 @@ STAN_ADD_REQUIRE_UNARY(eigen_matrix_base, is_eigen_matrix_base, STAN_ADD_REQUIRE_CONTAINER(eigen_matrix_base, is_eigen_matrix_base, require_eigens_types); -/** -* Checks whether type T is derived from Eigen::MatrixBase. -* If true this will have a static member function named value with a type -* of true, else value is false. -* @tparam T Type to check if it is derived from `MatrixBase` -* @tparam Enable used for SFINAE deduction. -* @ingroup type_trait -*/ -template -struct is_eigen_sparse_matrix_base -: bool_constant::value> { -}; - -STAN_ADD_REQUIRE_UNARY(eigen_sparse_matrix_base, is_eigen_sparse_matrix_base, - require_eigens_types); -STAN_ADD_REQUIRE_CONTAINER(eigen_sparse_matrix_base, is_eigen_sparse_matrix_base, - require_eigens_types); } // namespace stan diff --git a/test/unit/math/test_ad.hpp b/test/unit/math/test_ad.hpp index b002d6f3035..f239a8b5e46 100644 --- a/test/unit/math/test_ad.hpp +++ b/test/unit/math/test_ad.hpp @@ -2158,7 +2158,7 @@ auto make_sparse_mat_func(F&& f) { auto ret = f(stan::test::gen_sparse_diag_mat(x)); constexpr bool ret_bool = std::conditional_t, - stan::is_eigen_sparse_matrix_base>::value; + stan::is_eigen_sparse_base>::value; EXPECT_TRUE(ret_bool); return ret; }; From d1416646f2353475e1b5f8cc2a493edfb291bca6 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 5 Oct 2021 22:13:12 +0000 Subject: [PATCH 04/16] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/prim/functor/apply_scalar_unary.hpp | 36 +++++++++++-------- stan/math/prim/meta/is_container.hpp | 5 +-- stan/math/prim/meta/is_eigen_matrix_base.hpp | 1 - test/unit/math/test_ad.hpp | 13 +++---- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index c05b9b77ffe..c35cb519bde 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -37,10 +37,11 @@ namespace math { * @tparam F Type of function to apply. * @tparam T Type of argument to which function is applied. * @tparam ApplyZero If true, the function applied is assumed to return zero for - * inputs of zero and so sparse matrices will return sparse matrices. A value of - * false will return a dense matrix for sparse matrices. + * inputs of zero and so sparse matrices will return sparse matrices. A value + * of false will return a dense matrix for sparse matrices. */ -template +template struct apply_scalar_unary; /** @@ -74,17 +75,20 @@ struct apply_scalar_unary> { } /** - * Special case for `ApplyZero` set to true, returning a dense matrix. Return the result of applying the function defined by the template parameter F to the specified matrix argument. + * Special case for `ApplyZero` set to true, returning a dense matrix. Return + * the result of applying the function defined by the template parameter F to + * the specified matrix argument. * * @param SparseMat A type derived from `Eigen::SparseMatrixBase` - * @tparam NonZeroZero Shortcut trick for using class template for deduction, should not be set manually. + * @tparam NonZeroZero Shortcut trick for using class template for deduction, + * should not be set manually. * @param x Matrix to which operation is applied. * @return Componentwise application of the function specified * by F to the specified matrix. */ template >* = nullptr, - require_eigen_sparse_base_t* = nullptr> + require_t>* = nullptr, + require_eigen_sparse_base_t* = nullptr> static inline auto apply(const SparseMat& x) { using val_t = value_type_t; auto zeroed_val = apply_scalar_unary::apply(val_t(0.0)); @@ -92,35 +96,39 @@ struct apply_scalar_unary> { dense_mat ret = dense_mat::Constant(x.rows(), x.cols(), zeroed_val); for (Eigen::Index k = 0; k < x.outerSize(); ++k) { for (typename SparseMat::InnerIterator it(x, k); it; ++it) { - ret.coeffRef(it.row(), it.col()) = apply_scalar_unary::apply(it.value()); + ret.coeffRef(it.row(), it.col()) + = apply_scalar_unary::apply(it.value()); } } return ret; } /** - * Special case for `ApplyZero` set to false, returning a sparse matrix. Return the result of applying the function defined by the template parameter F to the specified matrix argument. + * Special case for `ApplyZero` set to false, returning a sparse matrix. + * Return the result of applying the function defined by the template + * parameter F to the specified matrix argument. * * @tparam SparseMat A type derived from `Eigen::SparseMatrixBase` - * @tparam NonZeroZero Shortcut trick for using class template for deduction, should not be set manually. + * @tparam NonZeroZero Shortcut trick for using class template for deduction, + * should not be set manually. * @param x Matrix to which operation is applied. * @return Componentwise application of the function specified * by F to the specified matrix. */ template >* = nullptr, - require_eigen_sparse_base_t* = nullptr> + require_t>* = nullptr, + require_eigen_sparse_base_t* = nullptr> static inline auto apply(const SparseMat& x) { auto ret = x.eval(); for (Eigen::Index k = 0; k < x.outerSize(); ++k) { - for (typename SparseMat::InnerIterator it(x, k), ret_it(ret, k); it; ++it, ++ret_it) { + for (typename SparseMat::InnerIterator it(x, k), ret_it(ret, k); it; + ++it, ++ret_it) { ret_it.valueRef() = apply_scalar_unary::apply(it.value()); } } return ret; } - /** * Return type for applying the function elementwise to a matrix * expression template of type T. diff --git a/stan/math/prim/meta/is_container.hpp b/stan/math/prim/meta/is_container.hpp index 43a2764526c..ea8808e38f4 100644 --- a/stan/math/prim/meta/is_container.hpp +++ b/stan/math/prim/meta/is_container.hpp @@ -19,8 +19,9 @@ namespace stan { * @tparam Container type to check */ template -using is_container = bool_constant< - math::disjunction, is_std_vector>::value>; +using is_container + = bool_constant, + is_std_vector>::value>; STAN_ADD_REQUIRE_UNARY(container, is_container, general_types); STAN_ADD_REQUIRE_CONTAINER(container, is_container, general_types); diff --git a/stan/math/prim/meta/is_eigen_matrix_base.hpp b/stan/math/prim/meta/is_eigen_matrix_base.hpp index 0befcace346..9e4e6e9465e 100644 --- a/stan/math/prim/meta/is_eigen_matrix_base.hpp +++ b/stan/math/prim/meta/is_eigen_matrix_base.hpp @@ -27,7 +27,6 @@ STAN_ADD_REQUIRE_UNARY(eigen_matrix_base, is_eigen_matrix_base, STAN_ADD_REQUIRE_CONTAINER(eigen_matrix_base, is_eigen_matrix_base, require_eigens_types); - } // namespace stan #endif diff --git a/test/unit/math/test_ad.hpp b/test/unit/math/test_ad.hpp index f239a8b5e46..2b35a8768f0 100644 --- a/test/unit/math/test_ad.hpp +++ b/test/unit/math/test_ad.hpp @@ -2132,7 +2132,7 @@ std::vector square_test_matrices(int low, int high) { template auto gen_sparse_diag_mat(T1&& x) { - using triplet_t = Eigen::Triplet>; + using triplet_t = Eigen::Triplet>; std::vector tripletList; tripletList.reserve(x.size()); for (int i = 0; i < x.size(); i++) { @@ -2146,8 +2146,8 @@ auto gen_sparse_diag_mat(T1&& x) { /** * Takes a unary lambda f taking a vector and makes a diagonal sparse matrix. - * @tparam ExpectDenseReturn if true, the return is checked for whether it's type - * is derived from `Eigen::DenseBase`, otherwise the return type is checked + * @tparam ExpectDenseReturn if true, the return is checked for whether it's + * type is derived from `Eigen::DenseBase`, otherwise the return type is checked * for whether it's type is derived from `Eigen::SparseMatrixBase`. * @tparam A lambda * @param An unary lambda @@ -2156,9 +2156,10 @@ template auto make_sparse_mat_func(F&& f) { return [&f](auto&& x) { auto ret = f(stan::test::gen_sparse_diag_mat(x)); - constexpr bool ret_bool = std::conditional_t, - stan::is_eigen_sparse_base>::value; + constexpr bool ret_bool + = std::conditional_t, + stan::is_eigen_sparse_base>::value; EXPECT_TRUE(ret_bool); return ret; }; From 27d2a7bba6cfecc2f5210471e875ce9e69af2592 Mon Sep 17 00:00:00 2001 From: stevebronder Date: Thu, 7 Oct 2021 15:03:12 -0400 Subject: [PATCH 05/16] All sparse unary functions return sparse matrices --- stan/math/prim/functor/apply_scalar_unary.hpp | 15 +++++++++++---- test/unit/math/test_ad.hpp | 8 +------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index c35cb519bde..bd9b4f5ff3e 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -92,14 +92,21 @@ struct apply_scalar_unary> { static inline auto apply(const SparseMat& x) { using val_t = value_type_t; auto zeroed_val = apply_scalar_unary::apply(val_t(0.0)); - using dense_mat = Eigen::Matrix; - dense_mat ret = dense_mat::Constant(x.rows(), x.cols(), zeroed_val); + const auto x_size = x.size(); + std::vector> triplet_list(x_size, Eigen::Triplet(0, 0, zeroed_val)); + for(Eigen::Index i = 0; i < x.rows(); ++i) { + for(Eigen::Index j = 0; j < x.cols(); ++j) { + // Column major order + triplet_list[i * x.cols() + j] = Eigen::Triplet(i, j, zeroed_val); + } + } for (Eigen::Index k = 0; k < x.outerSize(); ++k) { for (typename SparseMat::InnerIterator it(x, k); it; ++it) { - ret.coeffRef(it.row(), it.col()) - = apply_scalar_unary::apply(it.value()); + triplet_list[it.row() * x.cols() + it.col()] = Eigen::Triplet(it.row(), it.col(), apply_scalar_unary::apply(it.value())); } } + SparseMat ret(x.rows(), x.cols()); + ret.setFromTriplets(triplet_list.begin(), triplet_list.end()); return ret; } diff --git a/test/unit/math/test_ad.hpp b/test/unit/math/test_ad.hpp index 2b35a8768f0..8fefd3340ab 100644 --- a/test/unit/math/test_ad.hpp +++ b/test/unit/math/test_ad.hpp @@ -2155,13 +2155,7 @@ auto gen_sparse_diag_mat(T1&& x) { template auto make_sparse_mat_func(F&& f) { return [&f](auto&& x) { - auto ret = f(stan::test::gen_sparse_diag_mat(x)); - constexpr bool ret_bool - = std::conditional_t, - stan::is_eigen_sparse_base>::value; - EXPECT_TRUE(ret_bool); - return ret; + return f(stan::test::gen_sparse_diag_mat(x)); }; } From a7971f228afd0da9368f1bc06e8c5f7ef3ffba05 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 7 Oct 2021 19:03:52 +0000 Subject: [PATCH 06/16] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/prim/functor/apply_scalar_unary.hpp | 14 +++++++++----- test/unit/math/test_ad.hpp | 4 +--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index bd9b4f5ff3e..4b252a7ad2d 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -93,16 +93,20 @@ struct apply_scalar_unary> { using val_t = value_type_t; auto zeroed_val = apply_scalar_unary::apply(val_t(0.0)); const auto x_size = x.size(); - std::vector> triplet_list(x_size, Eigen::Triplet(0, 0, zeroed_val)); - for(Eigen::Index i = 0; i < x.rows(); ++i) { - for(Eigen::Index j = 0; j < x.cols(); ++j) { + std::vector> triplet_list( + x_size, Eigen::Triplet(0, 0, zeroed_val)); + for (Eigen::Index i = 0; i < x.rows(); ++i) { + for (Eigen::Index j = 0; j < x.cols(); ++j) { // Column major order - triplet_list[i * x.cols() + j] = Eigen::Triplet(i, j, zeroed_val); + triplet_list[i * x.cols() + j] + = Eigen::Triplet(i, j, zeroed_val); } } for (Eigen::Index k = 0; k < x.outerSize(); ++k) { for (typename SparseMat::InnerIterator it(x, k); it; ++it) { - triplet_list[it.row() * x.cols() + it.col()] = Eigen::Triplet(it.row(), it.col(), apply_scalar_unary::apply(it.value())); + triplet_list[it.row() * x.cols() + it.col()] = Eigen::Triplet( + it.row(), it.col(), + apply_scalar_unary::apply(it.value())); } } SparseMat ret(x.rows(), x.cols()); diff --git a/test/unit/math/test_ad.hpp b/test/unit/math/test_ad.hpp index 8fefd3340ab..cb3b3d59319 100644 --- a/test/unit/math/test_ad.hpp +++ b/test/unit/math/test_ad.hpp @@ -2154,9 +2154,7 @@ auto gen_sparse_diag_mat(T1&& x) { */ template auto make_sparse_mat_func(F&& f) { - return [&f](auto&& x) { - return f(stan::test::gen_sparse_diag_mat(x)); - }; + return [&f](auto&& x) { return f(stan::test::gen_sparse_diag_mat(x)); }; } } // namespace test From 83eb6fdd0f35429ea2bbd81f47aba8d23688af9f Mon Sep 17 00:00:00 2001 From: stevebronder Date: Thu, 7 Oct 2021 15:33:27 -0400 Subject: [PATCH 07/16] Small cleanup --- stan/math/prim/functor/apply_scalar_unary.hpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index 4b252a7ad2d..e8c0fdc9e37 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -91,25 +91,23 @@ struct apply_scalar_unary> { require_eigen_sparse_base_t* = nullptr> static inline auto apply(const SparseMat& x) { using val_t = value_type_t; + using triplet_t = Eigen::Triplet; auto zeroed_val = apply_scalar_unary::apply(val_t(0.0)); const auto x_size = x.size(); - std::vector> triplet_list( - x_size, Eigen::Triplet(0, 0, zeroed_val)); + std::vector triplet_list(x_size, triplet_t(0, 0, zeroed_val)); for (Eigen::Index i = 0; i < x.rows(); ++i) { for (Eigen::Index j = 0; j < x.cols(); ++j) { // Column major order - triplet_list[i * x.cols() + j] - = Eigen::Triplet(i, j, zeroed_val); + triplet_list[i * x.cols() + j] = triplet_t(i, j, zeroed_val); } } for (Eigen::Index k = 0; k < x.outerSize(); ++k) { for (typename SparseMat::InnerIterator it(x, k); it; ++it) { - triplet_list[it.row() * x.cols() + it.col()] = Eigen::Triplet( - it.row(), it.col(), - apply_scalar_unary::apply(it.value())); + triplet_list[it.row() * x.cols() + it.col()] = triplet_t( + it.row(), it.col(), apply_scalar_unary::apply(it.value())); } } - SparseMat ret(x.rows(), x.cols()); + plain_type_t ret(x.rows(), x.cols()); ret.setFromTriplets(triplet_list.begin(), triplet_list.end()); return ret; } From e53238ece095324f49468baf055f84b3e72bef3f Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 7 Oct 2021 19:34:08 +0000 Subject: [PATCH 08/16] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/prim/functor/apply_scalar_unary.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index e8c0fdc9e37..73594c82295 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -103,8 +103,9 @@ struct apply_scalar_unary> { } for (Eigen::Index k = 0; k < x.outerSize(); ++k) { for (typename SparseMat::InnerIterator it(x, k); it; ++it) { - triplet_list[it.row() * x.cols() + it.col()] = triplet_t( - it.row(), it.col(), apply_scalar_unary::apply(it.value())); + triplet_list[it.row() * x.cols() + it.col()] + = triplet_t(it.row(), it.col(), + apply_scalar_unary::apply(it.value())); } } plain_type_t ret(x.rows(), x.cols()); From 01f5ea7aab0fdb21144cd9f19a2356e88f45750c Mon Sep 17 00:00:00 2001 From: stevebronder Date: Thu, 7 Oct 2021 15:58:55 -0400 Subject: [PATCH 09/16] fix headers --- stan/math/prim/functor/apply_scalar_unary.hpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index 73594c82295..39ca4e7c47d 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -2,12 +2,7 @@ #define STAN_MATH_PRIM_FUNCTOR_APPLY_SCALAR_UNARY_HPP #include -#include -#include -#include -#include -#include -#include +#include #include #include From 6ca05afea80d89df72be238bdaa12c438cdb7c21 Mon Sep 17 00:00:00 2001 From: stevebronder Date: Thu, 7 Oct 2021 16:19:33 -0400 Subject: [PATCH 10/16] add cos --- stan/math/prim/fun/cos.hpp | 2 +- stan/math/prim/functor/apply_scalar_unary.hpp | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index 442c633c7b3..cea527271c3 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -42,7 +42,7 @@ template * = nullptr> inline auto cos(const Container& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } /** diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index 39ca4e7c47d..4439e41ca1d 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -70,9 +70,7 @@ struct apply_scalar_unary> { } /** - * Special case for `ApplyZero` set to true, returning a dense matrix. Return - * the result of applying the function defined by the template parameter F to - * the specified matrix argument. + * Special case for `ApplyZero` set to true, returning a full sparse matrix. Return the result of applying the function defined by the template parameter F to the specified matrix argument. * * @param SparseMat A type derived from `Eigen::SparseMatrixBase` * @tparam NonZeroZero Shortcut trick for using class template for deduction, From b6144b23ee8f7b5ae8c76a11f26a1fde321e6f12 Mon Sep 17 00:00:00 2001 From: stevebronder Date: Thu, 7 Oct 2021 16:40:53 -0400 Subject: [PATCH 11/16] apply zero or nonzeroing to unary funcs --- stan/math/prim/fun/digamma.hpp | 2 +- stan/math/prim/fun/erfc.hpp | 2 +- stan/math/prim/fun/exp.hpp | 2 +- stan/math/prim/fun/exp2.hpp | 2 +- stan/math/prim/fun/inv.hpp | 2 +- stan/math/prim/fun/inv_Phi.hpp | 2 +- stan/math/prim/fun/inv_cloglog.hpp | 2 +- stan/math/prim/fun/inv_logit.hpp | 2 +- stan/math/prim/fun/inv_sqrt.hpp | 2 +- stan/math/prim/fun/lgamma.hpp | 2 +- stan/math/prim/fun/log.hpp | 2 +- stan/math/prim/fun/log10.hpp | 2 +- stan/math/prim/fun/log1m.hpp | 2 +- stan/math/prim/fun/log1m_exp.hpp | 2 +- stan/math/prim/fun/log1m_inv_logit.hpp | 2 +- stan/math/prim/fun/log1p.hpp | 2 +- stan/math/prim/fun/log1p_exp.hpp | 2 +- stan/math/prim/fun/log2.hpp | 2 +- stan/math/prim/fun/log_inv_logit.hpp | 2 +- stan/math/prim/fun/logit.hpp | 2 +- stan/math/prim/fun/tgamma.hpp | 2 +- 21 files changed, 21 insertions(+), 21 deletions(-) diff --git a/stan/math/prim/fun/digamma.hpp b/stan/math/prim/fun/digamma.hpp index e4f8199202c..5186a5d0f35 100644 --- a/stan/math/prim/fun/digamma.hpp +++ b/stan/math/prim/fun/digamma.hpp @@ -75,7 +75,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto digamma(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/erfc.hpp b/stan/math/prim/fun/erfc.hpp index b5196cb273d..029c1808e3c 100644 --- a/stan/math/prim/fun/erfc.hpp +++ b/stan/math/prim/fun/erfc.hpp @@ -37,7 +37,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto erfc(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/exp.hpp b/stan/math/prim/fun/exp.hpp index 384d8d3c7d4..6307b7bbe0c 100644 --- a/stan/math/prim/fun/exp.hpp +++ b/stan/math/prim/fun/exp.hpp @@ -45,7 +45,7 @@ template < require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto exp(const Container& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } /** diff --git a/stan/math/prim/fun/exp2.hpp b/stan/math/prim/fun/exp2.hpp index 23b2e803131..06e8ca13c1a 100644 --- a/stan/math/prim/fun/exp2.hpp +++ b/stan/math/prim/fun/exp2.hpp @@ -40,7 +40,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto exp2(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index 92ec18be095..ab958e658b3 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -35,7 +35,7 @@ template < typename T, require_not_container_st* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto inv(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } /** diff --git a/stan/math/prim/fun/inv_Phi.hpp b/stan/math/prim/fun/inv_Phi.hpp index 7730a1833f4..f300e7a70d4 100644 --- a/stan/math/prim/fun/inv_Phi.hpp +++ b/stan/math/prim/fun/inv_Phi.hpp @@ -179,7 +179,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto inv_Phi(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index daaec48c278..a2edfe48e49 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -77,7 +77,7 @@ template * = nullptr> inline auto inv_cloglog(const Container& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } /** diff --git a/stan/math/prim/fun/inv_logit.hpp b/stan/math/prim/fun/inv_logit.hpp index 7c8355f9ffe..a931df57833 100644 --- a/stan/math/prim/fun/inv_logit.hpp +++ b/stan/math/prim/fun/inv_logit.hpp @@ -85,7 +85,7 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto inv_logit(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } // TODO(Tadej): Eigen is introducing their implementation logistic() of this diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index d2ec2b01885..a77218f32e7 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -46,7 +46,7 @@ template * = nullptr> inline auto inv_sqrt(const Container& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } /** diff --git a/stan/math/prim/fun/lgamma.hpp b/stan/math/prim/fun/lgamma.hpp index 93896185525..8b2f0872617 100644 --- a/stan/math/prim/fun/lgamma.hpp +++ b/stan/math/prim/fun/lgamma.hpp @@ -117,7 +117,7 @@ struct lgamma_fun { template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto lgamma(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/log.hpp b/stan/math/prim/fun/log.hpp index ba256709b95..c80138bbbfb 100644 --- a/stan/math/prim/fun/log.hpp +++ b/stan/math/prim/fun/log.hpp @@ -48,7 +48,7 @@ template < require_not_var_matrix_t* = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log(const Container& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } /** diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index a5702086bfd..222e3f4c1d2 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -39,7 +39,7 @@ template < require_not_container_st* = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log10(const Container& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } /** diff --git a/stan/math/prim/fun/log1m.hpp b/stan/math/prim/fun/log1m.hpp index 2b81dbd1edb..9dc1e019c48 100644 --- a/stan/math/prim/fun/log1m.hpp +++ b/stan/math/prim/fun/log1m.hpp @@ -71,7 +71,7 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log1m(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/log1m_exp.hpp b/stan/math/prim/fun/log1m_exp.hpp index 5243088649d..a8ba63aa997 100644 --- a/stan/math/prim/fun/log1m_exp.hpp +++ b/stan/math/prim/fun/log1m_exp.hpp @@ -81,7 +81,7 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log1m_exp(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/log1m_inv_logit.hpp b/stan/math/prim/fun/log1m_inv_logit.hpp index 4755da4f21e..693a78690f4 100644 --- a/stan/math/prim/fun/log1m_inv_logit.hpp +++ b/stan/math/prim/fun/log1m_inv_logit.hpp @@ -84,7 +84,7 @@ template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline typename apply_scalar_unary::return_t log1m_inv_logit(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/log1p.hpp b/stan/math/prim/fun/log1p.hpp index 36f1c4b05e4..81d659639eb 100644 --- a/stan/math/prim/fun/log1p.hpp +++ b/stan/math/prim/fun/log1p.hpp @@ -80,7 +80,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log1p(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/log1p_exp.hpp b/stan/math/prim/fun/log1p_exp.hpp index 821e08889e3..cd91d4cffed 100644 --- a/stan/math/prim/fun/log1p_exp.hpp +++ b/stan/math/prim/fun/log1p_exp.hpp @@ -76,7 +76,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log1p_exp(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/log2.hpp b/stan/math/prim/fun/log2.hpp index ef0dd4dd844..eaf04724056 100644 --- a/stan/math/prim/fun/log2.hpp +++ b/stan/math/prim/fun/log2.hpp @@ -47,7 +47,7 @@ struct log2_fun { template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log2(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/log_inv_logit.hpp b/stan/math/prim/fun/log_inv_logit.hpp index 3436f87d951..be329f01732 100644 --- a/stan/math/prim/fun/log_inv_logit.hpp +++ b/stan/math/prim/fun/log_inv_logit.hpp @@ -81,7 +81,7 @@ struct log_inv_logit_fun { template * = nullptr> inline auto log_inv_logit(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math diff --git a/stan/math/prim/fun/logit.hpp b/stan/math/prim/fun/logit.hpp index affdae68b73..3535f5ebcfc 100644 --- a/stan/math/prim/fun/logit.hpp +++ b/stan/math/prim/fun/logit.hpp @@ -89,7 +89,7 @@ template < require_not_var_matrix_t* = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto logit(const Container& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } /** diff --git a/stan/math/prim/fun/tgamma.hpp b/stan/math/prim/fun/tgamma.hpp index 89ae4439289..ef514f90d17 100644 --- a/stan/math/prim/fun/tgamma.hpp +++ b/stan/math/prim/fun/tgamma.hpp @@ -51,7 +51,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto tgamma(const T& x) { - return apply_scalar_unary::apply(x); + return apply_scalar_unary::apply(x); } } // namespace math From 03d446d5259ca0d1dc88e3111d8b12c19fd4c565 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 7 Oct 2021 20:46:47 +0000 Subject: [PATCH 12/16] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- stan/math/prim/functor/apply_scalar_unary.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index 4439e41ca1d..f651965d19d 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -70,7 +70,9 @@ struct apply_scalar_unary> { } /** - * Special case for `ApplyZero` set to true, returning a full sparse matrix. Return the result of applying the function defined by the template parameter F to the specified matrix argument. + * Special case for `ApplyZero` set to true, returning a full sparse matrix. + * Return the result of applying the function defined by the template + * parameter F to the specified matrix argument. * * @param SparseMat A type derived from `Eigen::SparseMatrixBase` * @tparam NonZeroZero Shortcut trick for using class template for deduction, From af21d26f272ce0f7ab11cae72a157727b57c0227 Mon Sep 17 00:00:00 2001 From: stevebronder Date: Fri, 8 Oct 2021 12:16:38 -0400 Subject: [PATCH 13/16] fix headers --- stan/math/prim/meta/is_container.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stan/math/prim/meta/is_container.hpp b/stan/math/prim/meta/is_container.hpp index ea8808e38f4..311f0e8bb91 100644 --- a/stan/math/prim/meta/is_container.hpp +++ b/stan/math/prim/meta/is_container.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include @@ -15,7 +15,7 @@ namespace stan { /** - * Deduces whether type is eigen matrix or standard vector. + * Deduces whether type is a dense eigen type or standard vector. * @tparam Container type to check */ template From 30190e378dfb2ce07062a1b907d5828311f93532 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 31 Mar 2022 13:16:54 -0400 Subject: [PATCH 14/16] fixup log1m_inv_logit return type so it just uses auto --- stan/math/prim/fun/log1m_inv_logit.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stan/math/prim/fun/log1m_inv_logit.hpp b/stan/math/prim/fun/log1m_inv_logit.hpp index 693a78690f4..8a99a5fd1e1 100644 --- a/stan/math/prim/fun/log1m_inv_logit.hpp +++ b/stan/math/prim/fun/log1m_inv_logit.hpp @@ -82,7 +82,7 @@ struct log1m_inv_logit_fun { */ template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline typename apply_scalar_unary::return_t +inline auto log1m_inv_logit(const T& x) { return apply_scalar_unary::apply(x); } From d7adc5e70fdf8703b2cf360b3477d8f12823925b Mon Sep 17 00:00:00 2001 From: Stan BuildBot Date: Thu, 31 Mar 2022 13:18:04 -0400 Subject: [PATCH 15/16] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- stan/math/prim/fun/log1m_inv_logit.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stan/math/prim/fun/log1m_inv_logit.hpp b/stan/math/prim/fun/log1m_inv_logit.hpp index 8a99a5fd1e1..c41bc301ae4 100644 --- a/stan/math/prim/fun/log1m_inv_logit.hpp +++ b/stan/math/prim/fun/log1m_inv_logit.hpp @@ -82,8 +82,7 @@ struct log1m_inv_logit_fun { */ template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline auto -log1m_inv_logit(const T& x) { +inline auto log1m_inv_logit(const T& x) { return apply_scalar_unary::apply(x); } From 729456de02d6c4cb90bcee57756a71606060cf51 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 17 May 2022 14:46:39 -0400 Subject: [PATCH 16/16] move opencl headers below fwd in mix.hpp --- stan/math/mix.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stan/math/mix.hpp b/stan/math/mix.hpp index 38b6a5e9c0e..f28846b29dd 100644 --- a/stan/math/mix.hpp +++ b/stan/math/mix.hpp @@ -5,15 +5,15 @@ #include #include -#ifdef STAN_OPENCL -#include -#endif - #include #include #include #include +#ifdef STAN_OPENCL +#include +#endif + #include #include #include