Loading [MathJax]/extensions/TeX/AMSsymbols.js
Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
map_if.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUNCTOR_MAP_IF_HPP
2#define STAN_MATH_PRIM_FUNCTOR_MAP_IF_HPP
3
7#include <functional>
8#include <tuple>
9#include <utility>
10
11namespace stan {
12namespace math {
13namespace internal {
14
15template <template <typename...> class Filter, typename F, typename Arg>
16inline constexpr decltype(auto) filter_fun(F&& f, Arg&& arg) {
17 if constexpr (Filter<Arg>::value) {
18 return f(std::forward<Arg>(arg));
19 } else {
20 return std::forward<Arg>(arg);
21 }
22}
23
24} // namespace internal
25
26/*
27 * Subset a tuple by a compile time filter on the types and return a
28 * tuple with an unary functor applied to each element where the filter was
29 * true:
30 * @note For types that fail the filter, a reference to the original type is
31 * returned so we avoid a copy.
32 *
33 * @tparam Filter a struct that accepts one template parameter and has a static
34 * constexpr bool member named value
35 * @tparam F Type of functor
36 * @tparam Args A parameter pack of arguments
37 * @param f functor callable
38 * @param args parameter pack of args
39 */
40template <template <typename...> class Filter, typename F, typename Tuple,
41 require_t<is_tuple<Tuple>>* = nullptr>
42inline constexpr auto map_if(F&& f, Tuple&& arg) {
43 return stan::math::apply(
44 [](auto&& f, auto&&... args) {
45 return make_holder_tuple(internal::filter_fun<Filter>(
46 std::forward<decltype(f)>(f),
47 std::forward<decltype(args)>(args))...);
48 },
49 std::forward<Tuple>(arg), std::forward<F>(f));
50}
51
52/*
53 * Subset a parameter pack by a compile time filter on the types and return a
54 * tuple with an unary functor applied to each element where the filter was
55 * true:
56 *
57 * @note For types that fail the filter, a reference to the original type is
58 * returned so we avoid a copy.
59 * @tparam Filter a struct that accepts one template parameter and has a static
60 * constexpr bool member named value
61 * @tparam F Type of functor
62 * @tparam Args A parameter pack of arguments
63 * @param f functor callable
64 * @param args parameter pack of args
65 */
66template <template <typename...> class Filter, typename F, typename Arg1,
67 typename... Args,
69inline constexpr auto map_if(F&& f, Arg1&& arg1, Args&&... args) {
70 return make_holder_tuple(
71 internal::filter_fun<Filter>(f, std::forward<Arg1>(arg1)),
72 internal::filter_fun<Filter>(f, std::forward<Args>(args))...);
73}
74
75template <template <typename...> class Filter, typename F, typename Arg,
77inline constexpr decltype(auto) map_if(F&& f, Arg&& arg) {
78 return internal::filter_fun<Filter>(std::forward<F>(f),
79 std::forward<Arg>(arg));
80}
81
82} // namespace math
83} // namespace stan
84
85#endif
constexpr decltype(auto) filter_fun(F &&f, Arg &&arg)
Definition map_if.hpp:16
fvar< T > arg(const std::complex< fvar< T > > &z)
Return the phase angle of the complex argument.
Definition arg.hpp:19
constexpr auto make_holder_tuple(Types &&... args)
Holds ownership of rvalues and forwards lvalues into a tuple.
constexpr auto map_if(F &&f, Tuple &&arg)
Definition map_if.hpp:42
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
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 ...