Automatic Differentiation
 
Loading...
Searching...
No Matches
select.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_SELECT_HPP
2#define STAN_MATH_PRIM_FUN_SELECT_HPP
3
7
8namespace stan {
9namespace math {
10
23template <typename T_true, typename T_false,
24 typename ReturnT = return_type_t<T_true, T_false>,
25 require_all_stan_scalar_t<T_true, T_false>* = nullptr>
26inline ReturnT select(const bool c, const T_true y_true,
27 const T_false y_false) {
28 return c ? ReturnT(y_true) : ReturnT(y_false);
29}
30
47template <
48 typename T_true, typename T_false,
49 typename T_return = return_type_t<T_true, T_false>,
50 typename T_true_plain = promote_scalar_t<T_return, plain_type_t<T_true>>,
51 typename T_false_plain = promote_scalar_t<T_return, plain_type_t<T_false>>,
54inline T_true_plain select(const bool c, T_true&& y_true, T_false&& y_false) {
55 check_matching_dims("select", "left hand side", y_true, "right hand side",
56 y_false);
57 return c ? T_true_plain(std::forward<T_true>(y_true))
58 : T_true_plain(std::forward<T_false>(y_false));
59}
60
78template <typename T_true, typename T_false,
79 typename ReturnT = promote_scalar_t<return_type_t<T_true, T_false>,
83inline ReturnT select(const bool c, const T_true& y_true,
84 const T_false& y_false) {
85 if (c) {
86 return y_true;
87 } else {
89 y_true, y_false,
90 [](const auto& y_true_inner, const auto& y_false_inner) {
91 return y_false_inner;
92 });
93 }
94}
95
113template <typename T_true, typename T_false,
114 typename ReturnT = promote_scalar_t<return_type_t<T_true, T_false>,
118inline ReturnT select(const bool c, const T_true y_true,
119 const T_false y_false) {
120 if (c) {
121 return apply_scalar_binary(
122 y_true, y_false,
123 [](const auto& y_true_inner, const auto& y_false_inner) {
124 return y_true_inner;
125 });
126 } else {
127 return y_false;
128 }
129}
130
146template <typename T_bool, typename T_true, typename T_false,
149inline auto select(const T_bool c, const T_true y_true, const T_false y_false) {
150 using ret_t = return_type_t<T_true, T_false>;
151 return c
152 .unaryExpr([y_true, y_false](bool cond) {
153 return cond ? ret_t(y_true) : ret_t(y_false);
154 })
155 .eval();
156}
157
170template <typename T_bool, typename T_true, typename T_false,
173inline auto select(const T_bool c, const T_true y_true, const T_false y_false) {
174 check_consistent_sizes("select", "boolean", c, "left hand side", y_true,
175 "right hand side", y_false);
176 using ret_t = return_type_t<T_true, T_false>;
177 return c.select(y_true, y_false).template cast<ret_t>().eval();
178}
179
180} // namespace math
181} // namespace stan
182
183#endif
require_all_t< is_container< std::decay_t< Types > >... > require_all_container_t
Require all of the types satisfy is_container.
require_t< is_container< std::decay_t< T > > > require_container_t
Require type satisfies is_container.
require_any_t< is_eigen_array< std::decay_t< Types > >... > require_any_eigen_array_t
Require any of the types satisfy is_eigen_array.
Definition is_eigen.hpp:283
require_t< container_type_check_base< is_eigen_array, value_type_t, TypeCheck, Check... > > require_eigen_array_vt
Require type satisfies is_eigen_array.
Definition is_eigen.hpp:308
require_t< is_eigen_array< std::decay_t< T > > > require_eigen_array_t
Require type satisfies is_eigen_array.
Definition is_eigen.hpp:271
select_< as_operation_cl_t< T_condition >, as_operation_cl_t< T_then >, as_operation_cl_t< T_else > > select(T_condition &&condition, T_then &&then, T_else &&els)
Selection operation on kernel generator expressions.
Definition select.hpp:148
require_all_t< std::is_same< std::decay_t< T >, std::decay_t< Types > >... > require_all_same_t
Require T and all of the Types satisfy std::is_same.
require_all_t< is_stan_scalar< std::decay_t< Types > >... > require_all_stan_scalar_t
Require all of the types satisfy is_stan_scalar.
require_t< is_stan_scalar< std::decay_t< T > > > require_stan_scalar_t
Require type satisfies is_stan_scalar.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
void check_matching_dims(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the two containers have the same dimensions.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
auto apply_scalar_binary(const T1 &x, const T2 &y, const F &f)
Base template function for vectorization of binary scalar functions defined by applying a functor to ...
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...