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 var<mat> specializations for lower/upper bounds #2285

Merged
merged 72 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
2875bf8
adds tests for lb ub and lub constraints that take in a var<mat> type
SteveBronder Jan 1, 2021
b52cc3f
Merge remote-tracking branch 'origin/develop' into feature/varmat-bou…
SteveBronder Jan 2, 2021
453c159
fix docs
SteveBronder Jan 2, 2021
d58bf12
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 2, 2021
8a9ca3f
Adds constants to infinity checks
SteveBronder Jan 2, 2021
ee76e0c
update headers
SteveBronder Jan 2, 2021
a4bc716
fix headers
SteveBronder Jan 2, 2021
b8f12ca
fix pf for identity_free
SteveBronder Jan 2, 2021
2b141ff
Finished varmat support for `simplex_constrain` (Issue #2101)
bbbales2 Jan 2, 2021
314ba66
Merge commit 'be49360ca7eb65addfa7001c04699fd6f1a85330' into HEAD
yashikno Jan 2, 2021
c0e835f
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 2, 2021
a7e8e37
fix headers
SteveBronder Jan 2, 2021
3728892
Merge remote-tracking branch 'origin/develop' into feature/varmat-bou…
bbbales2 Jan 5, 2021
d17f751
Updating lb and up some more, work in progress (Issue #2101)
bbbales2 Jan 5, 2021
cd90e7f
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 5, 2021
ee9808a
Work in progress constraints (Issue #2101)
bbbales2 Jan 5, 2021
baa1933
Updating lower/upper constrains to not have special behavior for infi…
bbbales2 Jan 6, 2021
d1da586
Merge branch 'feature/varmat-bound-constrains' of github.com:stan-dev…
bbbales2 Jan 6, 2021
b455cbe
Merge commit '0e9f2c0652d05dc5b9fdf22de986751b754eceeb' into HEAD
yashikno Jan 6, 2021
768364a
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 6, 2021
512605f
Updated log1p, check_less test errors, and log1p tests (Issue #2101)
bbbales2 Jan 6, 2021
3c26026
Merge branch 'feature/varmat-bound-constrains' of github.com:stan-dev…
bbbales2 Jan 6, 2021
c4573c3
Added constrain error checks (Issue #2101)
bbbales2 Jan 6, 2021
ed5f0cb
Added check_finite tests to constrains (Issue #2101)
bbbales2 Jan 6, 2021
3ab81b6
Merge commit 'a280e0d5bc5556c5a3fa5f100e1b8931d61f8f5c' into HEAD
yashikno Jan 6, 2021
71ea9f8
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 6, 2021
67d5106
Added a few vector prim tests (Issue #2101)
bbbales2 Jan 6, 2021
27e8374
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 6, 2021
5ea355d
Added back check_less stuff (Issue #2101)
bbbales2 Jan 6, 2021
7909673
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 6, 2021
202caeb
Work in progress to fix bug (Issue #2101)
bbbales2 Jan 7, 2021
647284b
Adding array specialization of `as_array_or_scalar` (Issue #2101)
bbbales2 Jan 7, 2021
d37a4ee
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 7, 2021
41cc349
Merge remote-tracking branch 'origin/develop' into feature/varmat-bou…
bbbales2 Jan 7, 2021
bcaceb7
Merge branch 'feature/varmat-bound-constrains' of github.com:stan-dev…
bbbales2 Jan 7, 2021
4db37fc
Added missing header (Issue #2101)
bbbales2 Jan 7, 2021
4d9d20f
Simplified `elt_multply`, removed extra comments in constrain, remove…
bbbales2 Jan 8, 2021
83bb692
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 8, 2021
cbfce12
Made lub constrain function symmetric (Issue #2101)
bbbales2 Jan 8, 2021
9b5cc30
Merge branch 'feature/varmat-bound-constrains' of github.com:stan-dev…
bbbales2 Jan 8, 2021
41e8045
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 8, 2021
78f74a1
Cleanup (Issue #2101)
bbbales2 Jan 8, 2021
156c1a1
Merge commit '8c7733a6bd13835a2cec19283b511f5ddc3ee174' into HEAD
yashikno Jan 8, 2021
da3c8a3
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 8, 2021
b98581a
Merge remote-tracking branch 'origin/develop' into feature/varmat-bou…
bbbales2 Jan 11, 2021
22429e6
Added holders to constrain functions (Issue #2101)
bbbales2 Jan 11, 2021
312c676
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jan 11, 2021
32ae9b3
Merge remote-tracking branch 'origin/develop' into feature/varmat-bou…
bbbales2 Feb 1, 2021
aa5d354
Added elt_multiply tests (Issue #2101)
bbbales2 Feb 1, 2021
e6ac4ae
Merge remote-tracking branch 'origin/develop' into feature/varmat-bou…
bbbales2 Feb 1, 2021
68c13c5
Added tests and docs for operator unary negative
bbbales2 Feb 1, 2021
0a1f283
Added test for check less (Issue #2101)
bbbales2 Feb 1, 2021
ce0f9c7
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 1, 2021
d3b3224
add util header to unary negative test for rev core
SteveBronder Feb 1, 2021
d1dd916
remove std::moves from constrains
SteveBronder Feb 1, 2021
62e527d
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 1, 2021
d5cb70c
update move semantics for transforms
SteveBronder Feb 1, 2021
1e10afa
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 1, 2021
90f01eb
fix unit vector constrain
SteveBronder Feb 1, 2021
23daef2
use const ref instead of const for lb constrain
SteveBronder Feb 1, 2021
961e082
Merge remote-tracking branch 'origin/develop' into feature/varmat-bou…
SteveBronder Feb 3, 2021
23a24b7
remove move from lub_constrain diff obj
SteveBronder Feb 3, 2021
1204614
fix lb and lub constrains
SteveBronder Feb 3, 2021
a4f7c73
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 3, 2021
2c862e9
remove holder from lub
SteveBronder Feb 4, 2021
6fcd1e1
update lub for checks
SteveBronder Feb 4, 2021
324686d
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 4, 2021
4c80d2e
fix names in lub constrain
SteveBronder Feb 4, 2021
e67352e
use evals in lub
SteveBronder Feb 4, 2021
e9814cd
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 4, 2021
c5ea613
Merge remote-tracking branch 'origin/develop' into feature/varmat-bou…
bbbales2 Feb 4, 2021
ae633f1
Non-expression for now (Issue #2101)
bbbales2 Feb 4, 2021
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
132 changes: 102 additions & 30 deletions stan/math/prim/err/check_less.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,145 @@
#include <stan/math/prim/fun/get.hpp>
#include <stan/math/prim/fun/scalar_seq_view.hpp>
#include <stan/math/prim/fun/size.hpp>
#include <stan/math/prim/fun/to_ref.hpp>
#include <stan/math/prim/fun/as_array_or_scalar.hpp>
#include <string>

namespace stan {
namespace math {

namespace internal {
template <typename T_y, typename T_high, bool is_vec>
struct less {
static void check(const char* function, const char* name, const T_y& y,
const T_high& high) {
scalar_seq_view<T_high> high_vec(high);
for (size_t n = 0; n < stan::math::size(high); n++) {
if (!(y < high_vec[n])) {
/**
* Check if <code>y</code> is strictly less than <code>high</code>.
* This function is vectorized and will check each element of
* <code>y</code> against each element of <code>high</code>.
* @tparam T_y Type of y
* @tparam T_high Type of upper bound
* @param function Function name (for error messages)
* @param name Variable name (for error messages)
* @param y Variable to check
* @param high Upper bound
* @throw <code>domain_error</code> if y is not less than low
* or if any element of y or high is NaN.
*/
template <typename T_y, typename T_high,
require_all_stan_scalar_t<T_y, T_high>* = nullptr>
inline void check_less(const char* function, const char* name, const T_y& y,
const T_high& high) {
if (!(y < high)) {
[&]() STAN_COLD_PATH {
std::stringstream msg;
msg << ", but must be less than ";
msg << high;
std::string msg_str(msg.str());
throw_domain_error(function, name, y, "is ", msg_str.c_str());
}();
}
}

/**
* Check if <code>y</code> is strictly less than <code>high</code>.
* This function is vectorized and will check each element of
* <code>y</code> against each element of <code>high</code>.
* @tparam T_y A container type
* @tparam T_high A container type
* @param function Function name (for error messages)
* @param name Variable name (for error messages)
* @param y Variable to check
* @param high Upper bound
* @throw <code>domain_error</code> if y is not less than low
* or if any element of y or high is NaN.
*/
template <typename T_y, typename T_high,
require_all_container_t<T_y, T_high>* = nullptr>
inline void check_less(const char* function, const char* name, const T_y& y,
const T_high& high) {
const auto& high_ref = as_array_or_scalar(high);
const auto& y_ref = as_array_or_scalar(y);
Eigen::Index n = 0;
for (Eigen::Index j = 0; j < y_ref.cols(); ++j) {
for (Eigen::Index i = 0; i < y_ref.rows(); ++i) {
if (!(y_ref.coeff(i, j) < high_ref.coeff(i, j))) {
[&]() STAN_COLD_PATH {
std::stringstream msg;
msg << ", but must be less than ";
msg << high_vec[n];
msg << high_ref.coeff(i, j);
std::string msg_str(msg.str());
throw_domain_error(function, name, y, "is ", msg_str.c_str());
throw_domain_error_vec(function, name, y_ref.coeff(i, j), i + j,
"is ", msg_str.c_str());
}();
}
}
}
};
}

template <typename T_y, typename T_high>
struct less<T_y, T_high, true> {
static void check(const char* function, const char* name, const T_y& y,
const T_high& high) {
scalar_seq_view<T_high> high_vec(high);
const auto& y_ref = to_ref(y);
for (size_t n = 0; n < stan::math::size(y_ref); n++) {
if (!(stan::get(y_ref, n) < high_vec[n])) {
/**
* Check if <code>y</code> is strictly less than <code>high</code>.
* This function is vectorized and will check each element of
* <code>y</code> against each element of <code>high</code>.
* @tparam T_y A container type
* @tparam T_high A scalar type
* @param function Function name (for error messages)
* @param name Variable name (for error messages)
* @param y Variable to check
* @param high Upper bound
* @throw <code>domain_error</code> if y is not less than low
* or if any element of y or high is NaN.
*/
template <typename T_y, typename T_high, require_container_t<T_y>* = nullptr,
require_stan_scalar_t<T_high>* = nullptr>
inline void check_less(const char* function, const char* name, const T_y& y,
const T_high& high) {
const auto& y_ref = as_array_or_scalar(y);
Eigen::Index n = 0;
for (Eigen::Index j = 0; j < y_ref.cols(); ++j) {
for (Eigen::Index i = 0; i < y_ref.rows(); ++i) {
if (!(y_ref.coeff(i, j) < high)) {
[&]() STAN_COLD_PATH {
std::stringstream msg;
msg << ", but must be less than ";
msg << high_vec[n];
msg << high;
std::string msg_str(msg.str());
throw_domain_error_vec(function, name, y_ref, n, "is ",
msg_str.c_str());
throw_domain_error_vec(function, name, y_ref.coeff(i, j), i + j,
"is ", msg_str.c_str());
}();
}
}
}
};
} // namespace internal
}

/**
* Check if <code>y</code> is strictly less than <code>high</code>.
* This function is vectorized and will check each element of
* <code>y</code> against each element of <code>high</code>.
* @tparam T_y Type of y
* @tparam T_high Type of upper bound
* @tparam T_y A container type
* @tparam T_high A scalar type
* @param function Function name (for error messages)
* @param name Variable name (for error messages)
* @param y Variable to check
* @param high Upper bound
* @throw <code>domain_error</code> if y is not less than low
* or if any element of y or high is NaN.
*/
template <typename T_y, typename T_high>
template <typename T_y, typename T_high, require_container_t<T_high>* = nullptr,
require_stan_scalar_t<T_y>* = nullptr>
inline void check_less(const char* function, const char* name, const T_y& y,
const T_high& high) {
internal::less<T_y, T_high, is_vector_like<T_y>::value>::check(function, name,
y, high);
const auto& high_ref = as_array_or_scalar(high);
Eigen::Index n = 0;
for (Eigen::Index j = 0; j < high_ref.cols(); ++j) {
for (Eigen::Index i = 0; i < high_ref.rows(); ++i) {
if (!(y < high_ref.coeff(i, j))) {
[&]() STAN_COLD_PATH {
std::stringstream msg;
msg << ", but must be less than ";
msg << high_ref(i, j);
std::string msg_str(msg.str());
throw_domain_error(function, name, y, "is ", msg_str.c_str());
}();
}
}
}
}

} // namespace math
} // namespace stan
#endif
15 changes: 14 additions & 1 deletion stan/math/prim/fun/as_array_or_scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,27 @@ inline T as_array_or_scalar(T&& v) {
return std::forward<T>(v);
}

/**
* Returns specified input value.
*
* @tparam T Type of element.
* @param v Specified value.
* @return Same value.
*/
template <typename T, require_eigen_array_t<T>* = nullptr>
inline T as_array_or_scalar(T&& v) {
return std::forward<T>(v);
}

/**
* Converts a matrix type to an array.
*
* @tparam T Type of \c Eigen \c Matrix or expression
* @param v Specified \c Eigen \c Matrix or expression.
* @return Matrix converted to an array.
*/
template <typename T, require_eigen_t<T>* = nullptr>
template <typename T, typename = require_eigen_t<T>,
require_not_eigen_array_t<T>* = nullptr>
inline auto as_array_or_scalar(T&& v) {
return make_holder([](auto& x) { return x.array(); }, std::forward<T>(v));
}
Expand Down
16 changes: 8 additions & 8 deletions stan/math/prim/fun/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ inline double pi() { return boost::math::constants::pi<double>(); }
/**
* Smallest positive value.
*/
const double EPSILON = std::numeric_limits<double>::epsilon();
constexpr double EPSILON = std::numeric_limits<double>::epsilon();

/**
* Positive infinity.
*/
const double INFTY = std::numeric_limits<double>::infinity();
constexpr double INFTY = std::numeric_limits<double>::infinity();

/**
* Negative infinity.
*/
const double NEGATIVE_INFTY = -INFTY;
constexpr double NEGATIVE_INFTY = -INFTY;

/**
* (Quiet) not-a-number value.
*/
const double NOT_A_NUMBER = std::numeric_limits<double>::quiet_NaN();
constexpr double NOT_A_NUMBER = std::numeric_limits<double>::quiet_NaN();

/**
* Twice the value of \f$ \pi \f$,
Expand Down Expand Up @@ -182,29 +182,29 @@ const double POISSON_MAX_RATE = std::pow(2.0, 30);
*
* @return Positive infinity.
*/
inline double positive_infinity() { return INFTY; }
static constexpr inline double positive_infinity() { return INFTY; }

/**
* Return negative infinity.
*
* @return Negative infinity.
*/
inline double negative_infinity() { return NEGATIVE_INFTY; }
static constexpr inline double negative_infinity() { return NEGATIVE_INFTY; }

/**
* Return (quiet) not-a-number.
*
* @return Quiet not-a-number.
*/
inline double not_a_number() { return NOT_A_NUMBER; }
static constexpr inline double not_a_number() { return NOT_A_NUMBER; }

/**
* Returns the difference between 1.0 and the next value
* representable.
*
* @return Minimum positive number.
*/
inline double machine_precision() { return EPSILON; }
static constexpr inline double machine_precision() { return EPSILON; }

/**
* Returns the natural logarithm of ten.
Expand Down
21 changes: 20 additions & 1 deletion stan/math/prim/fun/elt_multiply.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef STAN_MATH_PRIM_FUN_ELT_MULTIPLY_HPP
#define STAN_MATH_PRIM_FUN_ELT_MULTIPLY_HPP

#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/err.hpp>
#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/fun/multiply.hpp>

namespace stan {
namespace math {
Expand Down Expand Up @@ -44,6 +45,24 @@ auto elt_multiply(const Scalar1& a, const Scalar2& b) {
return a * b;
}

/**
* Return specified matrix multiplied by specified scalar.
*
* One argument is a matrix and one is a scalar.
*
* @tparam T1 type of the first argument
* @tparam T2 type of the second argument
*
* @param A first argument
* @param B second argument
* @return product of matrix and scalar
*/
template <typename T1, typename T2, require_any_matrix_t<T1, T2>* = nullptr,
require_any_stan_scalar_t<T1, T2>* = nullptr>
inline auto elt_multiply(const T1& A, const T2& B) {
return multiply(A, B);
}

} // namespace math
} // namespace stan

Expand Down
16 changes: 8 additions & 8 deletions stan/math/prim/fun/identity_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ namespace math {
* <p>This method is effectively a no-op and is mainly useful as a
* placeholder in auto-generated code.
*
* @tparam T Type of scalar.
* @param[in] x free scalar
* @tparam T Any type.
* @param[in] x object
* @return transformed input
*/
template <typename T>
inline T identity_constrain(const T& x) {
return x;
inline auto identity_constrain(T&& x) {
return std::forward<T>(x);
}

/**
Expand All @@ -30,15 +30,15 @@ inline T identity_constrain(const T& x) {
* <p>This method is effectively a no-op and mainly useful as a
* placeholder in auto-generated code.
*
* @tparam T type of scalar
* @tparam T Any type
* @tparam S type of log probability
* @param[in] x scalar
* @param[in] x object
* @param[in] lp log density reference
* @return transformed input
*/
template <typename T, typename S>
inline T identity_constrain(const T& x, S& lp) {
return x;
inline auto identity_constrain(T&& x, S& lp) {
return std::forward<T>(x);
}

} // namespace math
Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/fun/identity_free.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace math {
* @return value
*/
template <typename T>
inline T identity_free(const T& y) {
return y;
inline auto identity_free(T&& y) {
return std::forward<T>(y);
}

} // namespace math
Expand Down
Loading