Automatic Differentiation
 
Loading...
Searching...
No Matches
deep_copy_vars.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CORE_DEEP_COPY_VARS_HPP
2#define STAN_MATH_REV_CORE_DEEP_COPY_VARS_HPP
3
9
10#include <utility>
11#include <vector>
12
13namespace stan {
14namespace math {
15
24template <typename Arith, typename = require_arithmetic_t<scalar_type_t<Arith>>>
25inline Arith deep_copy_vars(Arith&& arg) {
26 return std::forward<Arith>(arg);
27}
28
35inline auto deep_copy_vars(const var& arg) {
36 return var(new vari(arg.val(), false));
37}
38
46template <typename VarVec, require_std_vector_vt<is_var, VarVec>* = nullptr>
47inline auto deep_copy_vars(VarVec&& arg) {
48 std::vector<var> copy_vec(arg.size());
49 for (size_t i = 0; i < arg.size(); ++i) {
50 copy_vec[i] = new vari(arg[i].val(), false);
51 }
52 return copy_vec;
53}
54
62template <typename VecContainer,
65inline auto deep_copy_vars(VecContainer&& arg) {
66 std::vector<value_type_t<VecContainer>> copy_vec(arg.size());
67 for (size_t i = 0; i < arg.size(); ++i) {
68 copy_vec[i] = deep_copy_vars(arg[i]);
69 }
70 return copy_vec;
71}
72
80template <typename EigT, require_eigen_vt<is_var, EigT>* = nullptr>
81inline auto deep_copy_vars(EigT&& arg) {
82 return arg.unaryExpr([](auto&& x) { return var(new vari(x.val(), false)); })
83 .eval();
84}
85
93template <typename Tuple, require_tuple_t<Tuple>* = nullptr>
94inline auto deep_copy_vars(Tuple&& arg) {
95 return stan::math::apply(
96 [](auto&&... tuple_args) {
97 return make_holder_tuple(
98 deep_copy_vars(std::forward<decltype(tuple_args)>(tuple_args))...);
99 },
100 std::forward<Tuple>(arg));
101}
102
103} // namespace math
104} // namespace stan
105
106#endif
require_t< container_type_check_base< is_std_vector, value_type_t, TypeCheck, Check... > > require_std_vector_vt
Require type satisfies is_std_vector.
require_t< container_type_check_base< is_std_vector, scalar_type_t, TypeCheck, Check... > > require_std_vector_st
Require type satisfies is_std_vector.
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.
vari_value< double > vari
Definition vari.hpp:197
Arith deep_copy_vars(Arith &&arg)
Forward arguments that do not contain vars.
var_value< double > var
Definition var.hpp:1187
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...