Automatic Differentiation
 
Loading...
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(T2&& x, F&& f) {
46 if constexpr (is_eigen_array<decltype(f(x))>::value) {
47 return make_holder(
48 [](auto&& xx) { return std::forward<decltype(xx)>(xx).matrix(); },
49 make_holder(std::forward<F>(f), std::forward<T2>(x)));
50 } else {
51 return make_holder(std::forward<F>(f), std::forward<T2>(x));
52 }
53 }
54
55 template <typename F, typename T2 = T,
57 static inline auto apply(T2&& x, F&& f) {
58 if constexpr (is_eigen_array<decltype(f(x))>::value) {
59 return make_holder(std::forward<F>(f), std::forward<T2>(x));
60 } else {
61 return make_holder(
62 [](auto&& xx) { return std::forward<decltype(xx)>(xx).array(); },
63 make_holder(std::forward<F>(f), std::forward<T2>(x)));
64 }
65 }
66
79 template <typename F, typename T2 = T,
81 static inline auto apply_no_holder(T2& x, F&& f) {
82 return std::forward<F>(f)(std::forward<T2>(x));
83 }
84
85 template <typename F, typename T2 = T,
87 static inline auto apply_no_holder(T2&& x, F&& f) {
88 return std::forward<F>(f)(std::forward<T2>(x));
89 }
90
102 template <typename T2, typename F>
103 static inline auto reduce(T2&& x, F&& f) {
104 return make_holder(std::forward<F>(f), std::forward<T2>(x));
105 }
106};
107
117template <typename T>
120 using T_map = typename Eigen::Map<const Eigen::Matrix<T_vt, -1, 1>>;
121
132 template <typename T2, typename F>
133 static inline auto apply(T2&& x, F&& f) {
134 using T_return = value_type_t<decltype(
135 f(as_column_vector_or_scalar(std::forward<T2>(x))))>;
136 std::vector<T_return> result(x.size());
137 Eigen::Map<Eigen::Matrix<T_return, -1, 1>>(result.data(), result.size())
138 = std::forward<F>(f)(as_column_vector_or_scalar(std::forward<T2>(x)))
139 .matrix();
140 return result;
141 }
142
154 template <typename T2, typename F>
155 static inline auto apply_no_holder(T2&& x, F&& f) {
156 return apply(std::forward<T2>(x), std::forward<F>(f));
157 }
158
169 template <typename T2, typename F>
170 static inline auto reduce(T2&& x, F&& f) {
172 as_column_vector_or_scalar(std::forward<T2>(x)), std::forward<F>(f));
173 }
174};
175
186template <typename T>
190
202 template <typename F>
203 static inline auto apply(const T& x, const F& f) {
204 using T_return
205 = plain_type_t<decltype(apply_vector_unary<T_vt>::apply(x[0], f))>;
206 std::vector<T_return> result(x.size());
207 std::transform(x.begin(), x.end(), result.begin(), [&f](auto&& xx) {
208 return apply_vector_unary<T_vt>::apply(xx, f);
209 });
210 return result;
211 }
212
224 template <typename F>
225 static inline auto apply_no_holder(const T& x, const F& f) {
226 return apply(x, f);
227 }
228
239 template <typename F>
240 static inline auto reduce(const T& x, const F& f) {
241 size_t x_size = x.size();
242 using T_return = decltype(apply_vector_unary<T_vt>::reduce(x[0], f));
243 std::vector<T_return> result(x_size);
244 for (size_t i = 0; i < x_size; ++i)
245 result[i] = apply_vector_unary<T_vt>::reduce(x[i], f);
246 return result;
247 }
248};
249
250} // namespace math
251} // namespace stan
252#endif
require_t< is_eigen< std::decay_t< T > > > require_eigen_t
Require type satisfies is_eigen.
Definition is_eigen.hpp:113
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(F &&func, Args &&... args)
Calls given function with given arguments.
Definition holder.hpp:437
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
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>.
typename plain_type< std::decay_t< T > >::type plain_type_t
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 ...
Check if a type is derived from Eigen::ArrayBase
Definition is_eigen.hpp:264
Checks if decayed type is a var, fvar, or arithmetic.
static auto apply(T2 &&x, F &&f)
Member function for applying a functor to a vector and subsequently returning a vector.
static auto reduce(T2 &&x, F &&f)
Member function for applying a functor to a vector and subsequently returning a scalar.
static auto apply_no_holder(T2 &x, 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 reduce(T2 &&x, F &&f)
Member function for applying a functor to a vector and subsequently returning a scalar.
static auto apply(T2 &&x, F &&f)
Member function for applying a functor to a vector and subsequently returning a vector.
static auto apply_no_holder(T2 &&x, F &&f)
Member function for applying a functor to each container in an std::vector and subsequently returning...