Automatic Differentiation
 
Loading...
Searching...
No Matches
conditional_copy_and_promote.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_MIX_FUNCTOR_CONDITIONAL_COPY_AND_PROMOTE_HPP
2#define STAN_MATH_MIX_FUNCTOR_CONDITIONAL_COPY_AND_PROMOTE_HPP
3
7
8namespace stan::math::internal {
9
14enum class COPY_TYPE : uint8_t { SHALLOW = 0, DEEP = 1 };
15
28template <template <typename...> class Filter,
29 typename PromotedType = stan::math::var,
30 COPY_TYPE CopyType = COPY_TYPE::DEEP, typename... Args>
31inline auto conditional_copy_and_promote(Args&&... args) {
32 return map_if<Filter>(
33 [](auto&& arg) {
34 if constexpr (is_tuple_v<decltype(arg)>) {
35 return stan::math::apply(
36 [](auto&&... inner_args) {
37 return make_holder_tuple(
38 conditional_copy_and_promote<Filter, PromotedType,
39 CopyType>(
40 std::forward<decltype(inner_args)>(inner_args))...);
41 },
42 std::forward<decltype(arg)>(arg));
43 } else if constexpr (is_std_vector_v<decltype(arg)>) {
44 std::vector<decltype(conditional_copy_and_promote<
45 Filter, PromotedType, CopyType>(arg[0]))>
46 ret;
47 for (std::size_t i = 0; i < arg.size(); ++i) {
48 ret.push_back(
49 conditional_copy_and_promote<Filter, PromotedType, CopyType>(
50 arg[i]));
51 }
52 return ret;
53 } else {
54 if constexpr (CopyType == COPY_TYPE::DEEP) {
55 return stan::math::eval(promote_scalar<PromotedType>(
56 value_of_rec(std::forward<decltype(arg)>(arg))));
57 } else if (CopyType == COPY_TYPE::SHALLOW) {
58 if constexpr (std::is_same_v<PromotedType,
59 scalar_type_t<decltype(arg)>>) {
60 return std::forward<decltype(arg)>(arg);
61 } else {
62 return stan::math::eval(promote_scalar<PromotedType>(
63 std::forward<decltype(arg)>(arg)));
64 }
65 }
66 }
67 },
68 std::forward<Args>(args)...);
69}
70
79template <typename PromotedType, typename... Args>
80inline auto deep_copy_vargs(Args&&... args) {
83 std::forward<Args>(args)...);
84}
85
97template <typename PromotedType, typename... Args>
98inline auto shallow_copy_vargs(Args&&... args) {
101 std::forward<Args>(args)...);
102}
103
104} // namespace stan::math::internal
105
106#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.
var_value< double > var
Definition var.hpp:1187
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