Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

adds testing suite for nested binary var matrix functions #2502

Merged
merged 15 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix docs and breakup beta tests for windows
  • Loading branch information
SteveBronder committed Jun 10, 2021
commit 86a783a640dc000d2c94a7a1b681e522caaaa2d2
2 changes: 1 addition & 1 deletion stan/math/rev/fun/bessel_first_kind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ inline var bessel_first_kind(int v, const var& a) {
}

/**
* Overload with `var_value<Matrix>` for `int` and `std::vector<int>`
* Overload with `var_value<Matrix>` for `int`, `std::vector<int>`, and `std::vector<std::vector<int>>`
*/
template <typename T1, typename T2, require_st_integral<T1>* = nullptr,
require_eigen_t<T2>* = nullptr>
Expand Down
3 changes: 3 additions & 0 deletions stan/math/rev/fun/bessel_second_kind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ inline var bessel_second_kind(int v, const var& a) {
});
}

/**
* Overload with `var_value<Matrix>` for `int`, `std::vector<int>`, and `std::vector<std::vector<int>>`
*/
template <typename T1, typename T2, require_st_integral<T1>* = nullptr,
require_eigen_t<T2>* = nullptr>
inline auto bessel_second_kind(const T1& v, const var_value<T2>& a) {
Expand Down
5 changes: 4 additions & 1 deletion stan/math/rev/fun/binary_log_loss.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ inline var binary_log_loss(int y, const var& y_hat) {
}
}

/**
* Overload with `int` and `var_value<Matrix>`
*/
template <typename Mat, require_eigen_t<Mat>* = nullptr>
inline auto binary_log_loss(int y, const var_value<Mat>& y_hat) {
if (y == 0) {
Expand All @@ -71,7 +74,7 @@ inline auto binary_log_loss(int y, const var_value<Mat>& y_hat) {
}

/**
* Overload for std::vector<int> and `var_value<Vector>`
* Overload with `var_value<Matrix>` for `std::vector<int>` and `std::vector<std::vector<int>>`
*/
template <typename StdVec, typename Mat, require_eigen_t<Mat>* = nullptr,
require_st_integral<StdVec>* = nullptr>
Expand Down
56 changes: 25 additions & 31 deletions stan/math/rev/functor/apply_scalar_binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ namespace stan {
namespace math {

/**
* Specialisation for use with two Eigen inputs. Eigen's binaryExpr framework
* is used for more efficient indexing of both row- and column-major inputs
* without separate loops.
* Specialisation for use with combinations of
* `Eigen::Matrix` and `var_value<Eigen::Matrix>` inputs.
* Eigen's binaryExpr framework is used for more efficient indexing of both row- and column-major inputs without separate loops.
*
* @tparam T1 Type of first argument to which functor is applied.
* @tparam T2 Type of second argument to which functor is applied.
* @tparam F Type of functor to apply.
* @param x First Eigen input to which operation is applied.
* @param y Second Eigen input to which operation is applied.
* @param f functor to apply to Eigen input.
* @return Eigen object with result of applying functor to inputs.
* @param x First Matrix input to which operation is applied.
* @param y Second Matrix input to which operation is applied.
* @param f functor to apply to Matrix inputs.
* @return `var_value<Matrix>` with result of applying functor to inputs.
*/
template <typename T1, typename T2, typename F,
require_any_var_matrix_t<T1, T2>* = nullptr,
Expand All @@ -33,16 +33,16 @@ inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) {
}

/**
* Specialisation for use with one Eigen vector (row or column) and
* Specialisation for use with one `var_value<Eigen vector>` (row or column) and
* a one-dimensional std::vector of integer types
*
* @tparam T1 Type of first argument to which functor is applied.
* @tparam T2 Type of second argument to which functor is applied.
* @tparam F Type of functor to apply.
* @param x Eigen input to which operation is applied.
* @param x Matrix input to which operation is applied.
* @param y Integer std::vector input to which operation is applied.
* @param f functor to apply to inputs.
* @return Eigen object with result of applying functor to inputs.
* @return var_value<Eigen> object with result of applying functor to inputs.
*/
template <typename T1, typename T2, typename F,
require_var_matrix_t<T1>* = nullptr,
Expand All @@ -54,7 +54,7 @@ inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) {

/**
* Specialisation for use with a one-dimensional std::vector of integer types
* and one Eigen vector (row or column).
* and one `var_value<Eigen vector>` (row or column).
*
* @tparam T1 Type of first argument to which functor is applied.
* @tparam T2 Type of second argument to which functor is applied.
Expand All @@ -73,13 +73,13 @@ inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) {
}

/**
* Specialisation for use with one Eigen matrix and
* Specialisation for use with one `var_value<Matrix>` and
* a two-dimensional std::vector of integer types
*
* @tparam T1 Type of first argument to which functor is applied.
* @tparam T2 Type of second argument to which functor is applied.
* @tparam F Type of functor to apply.
* @param x Eigen matrix input to which operation is applied.
* @param x var with Eigen matrix inner type to which operation is applied.
* @param y Nested integer std::vector input to which operation is applied.
* @param f functor to apply to inputs.
* @return Eigen object with result of applying functor to inputs.
Expand All @@ -94,13 +94,13 @@ inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) {

/**
* Specialisation for use with a two-dimensional std::vector of integer types
* and one Eigen matrix.
* and one `var_value<Matrix>`.
*
* @tparam T1 Type of first argument to which functor is applied.
* @tparam T2 Type of second argument to which functor is applied.
* @tparam F Type of functor to apply.
* @param x Nested integer std::vector input to which operation is applied.
* @param y Eigen matrix input to which operation is applied.
* @param y var value with inner Eigen matrix input to which operation is applied.
* @param f functor to apply to inputs.
* @return Eigen object with result of applying functor to inputs.
*/
Expand All @@ -113,19 +113,16 @@ inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) {
}

/**
* Specialisation for use when the first input is an Eigen type and the second
* is a scalar. Eigen's unaryExpr framework is used for more efficient indexing
* of both row- and column-major inputs. The unaryExpr framework also allows
* for the scalar to be captured and applied to each element in the Eigen
* object.
* Specialisation for use when the first input is an `var_value<Eigen> type and the second
* is a scalar.
*
* @tparam T1 Type of Eigen object to which functor is applied.
* @tparam T1 Type of `var_value<Matrix>` object to which functor is applied.
* @tparam T2 Type of scalar to which functor is applied.
* @tparam F Type of functor to apply.
* @param x Eigen input to which operation is applied.
* @param x Matrix input to which operation is applied.
* @param y Scalar input to which operation is applied.
* @param f functor to apply to Eigen and scalar inputs.
* @return Eigen object with result of applying functor to inputs.
* @param f functor to apply to var matrix and scalar inputs.
* @return `var_value<Matrix> object with result of applying functor to inputs.
*
* Note: The return expresssion needs to be evaluated, otherwise the captured
* function and scalar fall out of scope.
Expand All @@ -139,18 +136,15 @@ inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) {

/**
* Specialisation for use when the first input is an scalar and the second is
* an Eigen type. Eigen's unaryExpr framework is used for more efficient
* indexing of both row- and column-major inputs. The unaryExpr framework also
* allows for the scalar to be captured and applied to each element in the
* Eigen object.
* an `var_value<Eigen>`.
*
* @tparam T1 Type of scalar to which functor is applied.
* @tparam T2 Type of Eigen object to which functor is applied.
* @tparam T2 var value with inner Eigen type to which functor is applied.
* @tparam F Type of functor to apply.
* @param x Scalar input to which operation is applied.
* @param y Eigen input to which operation is applied.
* @param y var matrix input to which operation is applied.
* @param f functor to apply to Eigen and scalar inputs.
* @return Eigen object with result of applying functor to inputs.
* @return var value with inner Eigen type with result of applying functor to inputs.
*
* Note: The return expresssion needs to be evaluated, otherwise the captured
* function and scalar fall out of scope.
Expand Down
15 changes: 15 additions & 0 deletions test/unit/math/mix/fun/beta2_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <test/unit/math/test_ad.hpp>
andrjohns marked this conversation as resolved.
Show resolved Hide resolved


TEST(mathMixScalFun, beta_varmat_vectorized) {
auto f = [](const auto& x1, const auto& x2) {
using stan::math::beta;
return beta(x1, x2);
};

Eigen::MatrixXd in1(2, 2);
in1 << 0.5, 3.4, 5.2, 0.5;
Eigen::MatrixXd in2(2, 2);
in2 << 3.3, 0.9, 6.7, 3.3;
stan::test::expect_ad_vectorized_matvar(f, in1, in2);
}
13 changes: 0 additions & 13 deletions test/unit/math/mix/fun/beta_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,3 @@ TEST(mathMixScalFun, beta_varmat) {
stan::test::expect_ad_matvar(f, in1(0), in2);
stan::test::expect_ad_matvar(f, in1, in2(0));
}

TEST(mathMixScalFun, beta_varmat_vectorized) {
auto f = [](const auto& x1, const auto& x2) {
using stan::math::beta;
return beta(x1, x2);
};

Eigen::MatrixXd in1(2, 2);
in1 << 0.5, 3.4, 5.2, 0.5;
Eigen::MatrixXd in2(2, 2);
in2 << 3.3, 0.9, 6.7, 3.3;
stan::test::expect_ad_vectorized_matvar(f, in1, in2);
}