Automatic Differentiation
 
Loading...
Searching...
No Matches
value_of.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_VALUE_OF_HPP
2#define STAN_MATH_PRIM_FUN_VALUE_OF_HPP
3
6#include <cstddef>
7#include <vector>
8
9namespace stan {
10namespace math {
11
20template <typename T, require_st_arithmetic<T>* = nullptr>
21inline T value_of(T&& x) {
22 return std::forward<T>(x);
23}
24
25template <typename T, require_complex_t<T>* = nullptr,
26 require_t<std::is_arithmetic<
27 typename std::decay_t<T>::value_type>>* = nullptr>
28inline auto value_of(T&& x) {
29 return std::forward<T>(x);
30}
31
32template <
33 typename T, require_complex_t<T>* = nullptr,
35inline auto value_of(T&& x) {
37 return std::complex<inner_t>{value_of(x.real()), value_of(x.imag())};
38}
39
48template <typename T, require_std_vector_t<T>* = nullptr,
49 require_not_st_arithmetic<T>* = nullptr>
50inline auto value_of(const T& x) {
51 std::vector<plain_type_t<decltype(value_of(std::declval<value_type_t<T>>()))>>
52 out;
53 out.reserve(x.size());
54 for (auto&& x_elem : x) {
55 out.emplace_back(value_of(x_elem));
56 }
57 return out;
58}
59
70template <typename EigMat, require_eigen_dense_base_t<EigMat>* = nullptr,
71 require_not_st_arithmetic<EigMat>* = nullptr>
72inline auto value_of(EigMat&& M) {
73 return make_holder(
74 [](auto& a) {
75 return a.unaryExpr([](const auto& scal) { return value_of(scal); });
76 },
77 std::forward<EigMat>(M));
78}
79
80template <typename EigMat, require_eigen_sparse_base_t<EigMat>* = nullptr,
81 require_not_st_arithmetic<EigMat>* = nullptr>
82inline auto value_of(EigMat&& M) {
83 auto&& M_ref = to_ref(M);
84 using scalar_t = decltype(value_of(std::declval<value_type_t<EigMat>>()));
85 promote_scalar_t<scalar_t, plain_type_t<EigMat>> ret(M_ref.rows(),
86 M_ref.cols());
87 ret.reserve(M_ref.nonZeros());
88 for (int k = 0; k < M_ref.outerSize(); ++k) {
89 for (typename std::decay_t<EigMat>::InnerIterator it(M_ref, k); it; ++it) {
90 ret.insert(it.row(), it.col()) = value_of(it.valueRef());
91 }
92 }
93 ret.makeCompressed();
94 return ret;
95}
96template <typename EigMat, require_eigen_sparse_base_t<EigMat>* = nullptr,
97 require_st_arithmetic<EigMat>* = nullptr>
98inline auto value_of(EigMat&& M) {
99 return std::forward<EigMat>(M);
100}
101
102} // namespace math
103} // namespace stan
104
105#endif
require_not_t< std::is_arithmetic< std::decay_t< T > > > require_not_arithmetic_t
Require type does not satisfy std::is_arithmetic.
require_t< is_complex< std::decay_t< T > > > require_complex_t
Require type satisfies is_complex.
typename partials_type< T >::type partials_type_t
Helper alias for accessing the partial type.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
auto make_holder(const F &func, Args &&... args)
Constructs an expression from given arguments using given functor.
Definition holder.hpp:352
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...