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
value_of_rec.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_VALUE_OF_REC_HPP
2#define STAN_MATH_PRIM_FUN_VALUE_OF_REC_HPP
3
8#include <complex>
9#include <cstddef>
10#include <vector>
11
12namespace stan {
13namespace math {
14
15template <typename Tuple, require_tuple_t<Tuple>* = nullptr>
16inline auto value_of_rec(Tuple&& tup);
17
18template <typename T, require_not_same_t<double, T>* = nullptr>
19inline std::vector<promote_scalar_t<double, T>> value_of_rec(
20 const std::vector<T>& x);
21
37template <typename T, require_stan_scalar_t<T> = nullptr>
38inline double constexpr value_of_rec(const T x) noexcept {
39 return static_cast<double>(x);
40}
41
53inline double constexpr value_of_rec(double x) noexcept { return x; }
54
62template <typename T>
63inline std::complex<double> value_of_rec(const std::complex<T>& x) {
64 return {value_of_rec(x.real()), value_of_rec(x.imag())};
65}
66
78template <typename T, require_std_vector_t<T>* = nullptr,
79 require_vt_same<double, T>* = nullptr>
80inline T value_of_rec(T&& x) {
81 return std::forward<T>(x);
82}
83
94template <typename T, require_not_st_same<T, double>* = nullptr,
95 require_eigen_t<T>* = nullptr>
96inline auto value_of_rec(T&& M) {
97 return make_holder(
98 [](auto&& m) {
99 return m.unaryExpr([](auto x) { return value_of_rec(x); });
100 },
101 std::forward<T>(M));
102}
103
116template <typename T, require_st_same<T, double>* = nullptr,
117 require_eigen_t<T>* = nullptr>
118inline decltype(auto) value_of_rec(T&& x) {
119 if constexpr (is_plain_type<T>::value || is_holder_v<T>) {
120 if constexpr (std::is_rvalue_reference_v<T&&>) {
121 return std::decay_t<T>(std::forward<T>(x));
122 } else {
123 return x;
124 }
125 } else {
126 return make_holder([](auto&& m) { return m; }, std::forward<T>(x));
127 }
128}
129
140template <typename T, require_not_same_t<double, T>*>
141inline std::vector<promote_scalar_t<double, T>> value_of_rec(
142 const std::vector<T>& x) {
143 size_t x_size = x.size();
144 std::vector<promote_scalar_t<double, plain_type_t<T>>> result(x_size);
145 for (size_t i = 0; i < x_size; i++) {
146 result[i] = value_of_rec(x[i]);
147 }
148 return result;
149}
150
156template <typename Tuple, require_tuple_t<Tuple>*>
157inline auto value_of_rec(Tuple&& tup) {
158 return stan::math::apply(
159 [](auto&&... args) {
160 return make_holder_tuple(
161 value_of_rec(std::forward<decltype(args)>(args))...);
162 },
163 std::forward<Tuple>(tup));
164}
165
166} // namespace math
167} // namespace stan
168
169#endif
std::is_same< std::decay_t< S >, plain_type_t< std::decay_t< S > > > is_plain_type
Checks whether the template type T is an assignable type.
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
constexpr auto make_holder_tuple(Types &&... args)
Holds ownership of rvalues and forwards lvalues into a tuple.
auto make_holder(F &&func, Args &&... args)
Constructs an expression from given arguments using given functor.
Definition holder.hpp:368
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 ...