Stan Math Library  4.9.0 Automatic Differentiation
Searching...
No Matches
apply_vector_unary.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUNCTOR_APPLY_VECTOR_UNARY_HPP
2#define STAN_MATH_PRIM_FUNCTOR_APPLY_VECTOR_UNARY_HPP
3
11#include <vector>
12
13namespace stan {
14namespace math {
15// Forward declaration to allow specializations
16template <typename T, typename Enable = void>
18
31template <typename T>
43 template <typename F, typename T2 = T,
45 static inline auto apply(const T& x, const F& f) {
46 return make_holder([](const auto& a) { return a.matrix().derived(); },
47 f(x));
48 }
49
50 template <typename F, typename T2 = T,
52 static inline auto apply(const T& x, const F& f) {
53 return make_holder([](const auto& a) { return a.array().derived(); }, f(x));
54 }
55
68 template <typename F, typename T2 = T,
70 static inline auto apply_no_holder(const T& x, const F& f) {
71 return f(x).matrix().derived();
72 }
73
74 template <typename F, typename T2 = T,
76 static inline auto apply_no_holder(const T& x, const F& f) {
77 return f(x).array().derived();
78 }
79
91 template <typename F>
92 static inline auto reduce(const T& x, const F& f) {
93 return f(x);
94 }
95};
96
106template <typename T>
109 using T_map = typename Eigen::Map<const Eigen::Matrix<T_vt, -1, 1>>;
110
121 template <typename F>
122 static inline auto apply(const T& x, const F& f) {
123 using T_return = value_type_t<decltype(f(as_column_vector_or_scalar(x)))>;
124 std::vector<T_return> result(x.size());
125 Eigen::Map<Eigen::Matrix<T_return, -1, 1>>(result.data(), result.size())
126 = f(as_column_vector_or_scalar(x)).matrix();
127 return result;
128 }
129
141 template <typename F>
142 static inline auto apply_no_holder(const T& x, const F& f) {
143 return apply(x, f);
144 }
145
156 template <typename F>
157 static inline auto reduce(const T& x, const F& f) {
159 }
160};
161
172template <typename T>
176
188 template <typename F>
189 static inline auto apply(const T& x, const F& f) {
190 using T_return
191 = plain_type_t<decltype(apply_vector_unary<T_vt>::apply(x[0], f))>;
192 std::vector<T_return> result(x.size());
193 std::transform(x.begin(), x.end(), result.begin(), [&f](auto&& xx) {
194 return apply_vector_unary<T_vt>::apply_no_holder(xx, f);
195 });
196 return result;
197 }
198
210 template <typename F>
211 static inline auto apply_no_holder(const T& x, const F& f) {
212 return apply(x, f);
213 }
214
225 template <typename F>
226 static inline auto reduce(const T& x, const F& f) {
227 size_t x_size = x.size();
228 using T_return = decltype(apply_vector_unary<T_vt>::reduce(x[0], f));
229 std::vector<T_return> result(x_size);
230 for (size_t i = 0; i < x_size; ++i)
231 result[i] = apply_vector_unary<T_vt>::reduce(x[i], f);
232 return result;
233 }
234};
235
236} // namespace math
237} // namespace stan
238#endif
require_t< is_eigen< std::decay_t< T > > > require_eigen_t
Require type satisfies is_eigen.
Definition is_eigen.hpp:55
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
require_t< container_type_check_base< is_std_vector, value_type_t, TypeCheck, Check... > > require_std_vector_vt
Require type satisfies is_std_vector.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
auto make_holder(const F &func, Args &&... args)
Constructs an expression from given arguments using given functor.
Definition holder.hpp:352
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:52
typename plain_type< T >::type plain_type_t
bool_constant< math::disjunction< is_container< Container >, is_var_matrix< Container > >::value > is_container_or_var_matrix
Deduces whether type is eigen matrix, standard vector, or var<Matrix>.
std::enable_if_t< Check::value > require_t
If condition is true, template is enabled.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Checks if decayed type is a var, fvar, or arithmetic.
static auto reduce(const T &x, const F &f)
Member function for applying a functor to a vector and subsequently returning a scalar.
static auto apply(const T &x, const F &f)
Member function for applying a functor to a vector and subsequently returning a vector.
static auto apply_no_holder(const T &x, const F &f)
Member function for applying a functor to a vector and subsequently returning a vector.
static auto reduce(const T &x, const F &f)
Member function for applying a functor to each container in an std::vector and subsequently returning...
static auto apply(const T &x, const F &f)
Member function for applying a functor to each container in an std::vector and subsequently returning...
static auto apply_no_holder(const T &x, const F &f)
Member function for applying a functor to each container in an std::vector and subsequently returning...
static auto apply(const T &x, const F &f)
Member function for applying a functor to a vector and subsequently returning a vector.
static auto apply_no_holder(const T &x, const F &f)
Member function for applying a functor to each container in an std::vector and subsequently returning...
static auto reduce(const T &x, const F &f)
Member function for applying a functor to a vector and subsequently returning a scalar.