Automatic Differentiation
 
Loading...
Searching...
No Matches
conditional_copy_and_promote.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUNCTOR_CONDITIONAL_COPY_AND_PROMOTE_HPP
2#define STAN_MATH_PRIM_FUNCTOR_CONDITIONAL_COPY_AND_PROMOTE_HPP
3
14#include <cstdint>
15#include <utility>
16#include <vector>
17
18namespace stan::math::internal {
19
24enum class COPY_TYPE : uint8_t { SHALLOW = 0, DEEP = 1 };
25
38template <template <typename...> class Filter, typename PromotedType = double,
39 COPY_TYPE CopyType = COPY_TYPE::DEEP, typename... Args>
40inline auto conditional_copy_and_promote(Args&&... args) {
41 return map_if<Filter>(
42 [](auto&& arg) {
43 if constexpr (is_tuple_v<decltype(arg)>) {
44 return stan::math::apply(
45 [](auto&&... inner_args) {
46 return make_holder_tuple(
47 conditional_copy_and_promote<Filter, PromotedType,
48 CopyType>(
49 std::forward<decltype(inner_args)>(inner_args))...);
50 },
51 std::forward<decltype(arg)>(arg));
52 } else if constexpr (is_std_vector_v<decltype(arg)>) {
53 std::vector<decltype(conditional_copy_and_promote<
54 Filter, PromotedType, CopyType>(arg[0]))>
55 ret;
56 for (std::size_t i = 0; i < arg.size(); ++i) {
57 ret.push_back(
58 conditional_copy_and_promote<Filter, PromotedType, CopyType>(
59 arg[i]));
60 }
61 return ret;
62 } else {
63 if constexpr (CopyType == COPY_TYPE::DEEP) {
64 return stan::math::eval(promote_scalar<PromotedType>(
65 value_of_rec(std::forward<decltype(arg)>(arg))));
66 } else if (CopyType == COPY_TYPE::SHALLOW) {
67 if constexpr (std::is_same_v<PromotedType,
68 scalar_type_t<decltype(arg)>>) {
69 return std::forward<decltype(arg)>(arg);
70 } else {
71 return stan::math::eval(promote_scalar<PromotedType>(
72 std::forward<decltype(arg)>(arg)));
73 }
74 }
75 }
76 },
77 std::forward<Args>(args)...);
78}
79
88template <typename PromotedType, typename... Args>
89inline auto deep_copy_vargs(Args&&... args) {
92 std::forward<Args>(args)...);
93}
94
106template <typename PromotedType, typename... Args>
107inline auto shallow_copy_vargs(Args&&... args) {
110 std::forward<Args>(args)...);
111}
112
113} // namespace stan::math::internal
114
115#endif
auto deep_copy_vargs(Args &&... args)
Conditional deep copy types with a var scalar type to PromotedType.
auto shallow_copy_vargs(Args &&... args)
Conditional shallow copy types with a var scalar type to PromotedType.
COPY_TYPE
Decide if object should be deep or shallow copied when using conditional_copy_and_promote .
auto conditional_copy_and_promote(Args &&... args)
Conditional copy and promote a type's scalar type to a PromotedType.
A comparator that works for any container type that has the brackets operator.
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > arg(const std::complex< fvar< T > > &z)
Return the phase angle of the complex argument.
Definition arg.hpp:19
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
constexpr auto make_holder_tuple(Types &&... args)
Holds ownership of rvalues and forwards lvalues into a tuple.
constexpr bool is_tuple_v
Definition is_tuple.hpp:24
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
typename scalar_type< T >::type scalar_type_t
constexpr bool is_std_vector_v