-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2853 from andrjohns/vectorised-select
Add vectorised select(), any(), and all() functions
- Loading branch information
Showing
8 changed files
with
656 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#ifndef STAN_MATH_PRIM_FUN_ALL_HPP | ||
#define STAN_MATH_PRIM_FUN_ALL_HPP | ||
|
||
#include <stan/math/prim/meta.hpp> | ||
#include <stan/math/prim/functor/for_each.hpp> | ||
#include <algorithm> | ||
|
||
namespace stan { | ||
namespace math { | ||
|
||
/** | ||
* Return true if all values in the input are true. | ||
* | ||
* Overload for a single integral input | ||
* | ||
* @tparam T Any type convertible to `bool` | ||
* @param x integral input | ||
* @return The input unchanged | ||
*/ | ||
template <typename T, require_t<std::is_convertible<T, bool>>* = nullptr> | ||
constexpr inline bool all(T x) { | ||
return x; | ||
} | ||
|
||
/** | ||
* Return true if all values in the input are true. | ||
* | ||
* Overload for Eigen types | ||
* | ||
* @tparam ContainerT A type derived from `Eigen::EigenBase` that has an | ||
* `integral` scalar type | ||
* @param x Eigen object of boolean inputs | ||
* @return Boolean indicating whether all elements are true | ||
*/ | ||
template <typename ContainerT, | ||
require_eigen_st<std::is_integral, ContainerT>* = nullptr> | ||
inline bool all(const ContainerT& x) { | ||
return x.all(); | ||
} | ||
|
||
// Forward-declaration for correct resolution of all(std::vector<std::tuple>) | ||
template <typename... Types> | ||
inline bool all(const std::tuple<Types...>& x); | ||
|
||
/** | ||
* Return true if all values in the input are true. | ||
* | ||
* Overload for a std::vector/nested inputs. The Eigen::Map/apply_vector_unary | ||
* approach cannot be used as std::vector<bool> types do not have a .data() | ||
* member and are not always stored contiguously. | ||
* | ||
* @tparam InnerT Type within std::vector | ||
* @param x Nested container of boolean inputs | ||
* @return Boolean indicating whether all elements are true | ||
*/ | ||
template <typename InnerT> | ||
inline bool all(const std::vector<InnerT>& x) { | ||
return std::all_of(x.begin(), x.end(), [](const auto& i) { return all(i); }); | ||
} | ||
|
||
/** | ||
* Return true if all values in the input are true. | ||
* | ||
* Overload for a tuple input. | ||
* | ||
* @tparam Types of items within tuple | ||
* @param x Tuple of boolean scalar-type elements | ||
* @return Boolean indicating whether all elements are true | ||
*/ | ||
template <typename... Types> | ||
inline bool all(const std::tuple<Types...>& x) { | ||
bool all_true = true; | ||
math::for_each( | ||
[&all_true](const auto& i) { | ||
all_true = all_true && all(i); | ||
return; | ||
}, | ||
x); | ||
return all_true; | ||
} | ||
|
||
} // namespace math | ||
} // namespace stan | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#ifndef STAN_MATH_PRIM_FUN_ANY_HPP | ||
#define STAN_MATH_PRIM_FUN_ANY_HPP | ||
|
||
#include <stan/math/prim/meta.hpp> | ||
#include <stan/math/prim/functor/for_each.hpp> | ||
#include <algorithm> | ||
|
||
namespace stan { | ||
namespace math { | ||
|
||
/** | ||
* Return true if any values in the input are true. | ||
* | ||
* Overload for a single boolean input | ||
* | ||
* @tparam T Any type convertible to `bool` | ||
* @param x boolean input | ||
* @return The input unchanged | ||
*/ | ||
template <typename T, require_t<std::is_convertible<T, bool>>* = nullptr> | ||
constexpr inline bool any(T x) { | ||
return x; | ||
} | ||
|
||
/** | ||
* Return true if any values in the input are true. | ||
* | ||
* Overload for Eigen types | ||
* | ||
* @tparam ContainerT A type derived from `Eigen::EigenBase` that has an | ||
* `integral` scalar type | ||
* @param x Eigen object of boolean inputs | ||
* @return Boolean indicating whether any elements are true | ||
*/ | ||
template <typename ContainerT, | ||
require_eigen_st<std::is_integral, ContainerT>* = nullptr> | ||
inline bool any(const ContainerT& x) { | ||
return x.any(); | ||
} | ||
|
||
// Forward-declaration for correct resolution of any(std::vector<std::tuple>) | ||
template <typename... Types> | ||
inline bool any(const std::tuple<Types...>& x); | ||
|
||
/** | ||
* Return true if any values in the input are true. | ||
* | ||
* Overload for a std::vector/nested inputs. The Eigen::Map/apply_vector_unary | ||
* approach cannot be used as std::vector<bool> types do not have a .data() | ||
* member and are not always stored contiguously. | ||
* | ||
* @tparam InnerT Type within std::vector | ||
* @param x Nested container of boolean inputs | ||
* @return Boolean indicating whether any elements are true | ||
*/ | ||
template <typename InnerT> | ||
inline bool any(const std::vector<InnerT>& x) { | ||
return std::any_of(x.begin(), x.end(), [](const auto& i) { return any(i); }); | ||
} | ||
|
||
/** | ||
* Return true if any values in the input are true. | ||
* | ||
* Overload for a tuple input. | ||
* | ||
* @tparam Types of items within tuple | ||
* @param x Tuple of boolean scalar-type elements | ||
* @return Boolean indicating whether any elements are true | ||
*/ | ||
template <typename... Types> | ||
inline bool any(const std::tuple<Types...>& x) { | ||
bool any_true = false; | ||
math::for_each( | ||
[&any_true](const auto& i) { | ||
any_true = any_true || any(i); | ||
return; | ||
}, | ||
x); | ||
return any_true; | ||
} | ||
|
||
} // namespace math | ||
} // namespace stan | ||
|
||
#endif |
Oops, something went wrong.