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.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
8#include <cstddef>
9#include <vector>
10
11namespace stan {
12namespace math {
13template <typename Tuple, require_tuple_t<Tuple>* = nullptr>
14inline auto value_of(Tuple&& tup);
15template <typename T, require_std_vector_t<T>* = nullptr,
16 require_not_st_arithmetic<T>* = nullptr>
17inline auto value_of(const T& x);
27template <typename T, require_st_arithmetic<T>* = nullptr>
28inline decltype(auto) value_of(T&& x) {
29 if constexpr (std::is_rvalue_reference_v<T&&>) {
30 return std::decay_t<T>(std::forward<T>(x));
31 } else {
32 return std::forward<T>(x);
33 }
34}
35
36template <typename T, require_complex_t<T>* = nullptr,
37 require_t<std::is_arithmetic<
38 typename std::decay_t<T>::value_type>>* = nullptr>
39inline auto value_of(T&& x) {
40 return std::forward<T>(x);
41}
42
43template <
44 typename T, require_complex_t<T>* = nullptr,
46inline auto value_of(T&& x) {
48 return std::complex<inner_t>{value_of(x.real()), value_of(x.imag())};
49}
50
59template <typename T, require_std_vector_t<T>*, require_not_st_arithmetic<T>*>
60inline auto value_of(const T& x) {
61 std::vector<plain_type_t<decltype(value_of(std::declval<value_type_t<T>>()))>>
62 out;
63 out.reserve(x.size());
64 for (auto&& x_elem : x) {
65 out.emplace_back(value_of(x_elem));
66 }
67 return out;
68}
69
80template <typename EigMat, require_eigen_dense_base_t<EigMat>* = nullptr,
81 require_not_st_arithmetic<EigMat>* = nullptr>
82inline auto value_of(EigMat&& M) {
83 return make_holder(
84 [](auto&& a) {
85 return a.unaryExpr([](const auto& scal) { return value_of(scal); });
86 },
87 std::forward<EigMat>(M));
88}
89
90template <typename EigMat, require_eigen_sparse_base_t<EigMat>* = nullptr,
91 require_not_st_arithmetic<EigMat>* = nullptr>
92inline auto value_of(EigMat&& M) {
93 auto&& M_ref = to_ref(M);
94 using scalar_t = decltype(value_of(std::declval<value_type_t<EigMat>>()));
95 promote_scalar_t<scalar_t, plain_type_t<EigMat>> ret(M_ref.rows(),
96 M_ref.cols());
97 ret.reserve(M_ref.nonZeros());
98 for (int k = 0; k < M_ref.outerSize(); ++k) {
99 for (typename std::decay_t<EigMat>::InnerIterator it(M_ref, k); it; ++it) {
100 ret.insert(it.row(), it.col()) = value_of(it.valueRef());
101 }
102 }
103 ret.makeCompressed();
104 return ret;
105}
106
107/*
108 * For Sparse Eigen matrices and expressions of non-arithmetic types, return an
109 *expression that represents the Eigen::Matrix resulting from applying value_of
110 *elementwise
111 *
112 * @tparam EigMat type of the matrix
113 *
114 * @param[in] M Matrix to be converted
115 * @return Matrix of values
116 */
117template <typename EigMat, require_eigen_sparse_base_t<EigMat>* = nullptr,
118 require_st_arithmetic<EigMat>* = nullptr>
119inline auto value_of(EigMat&& M) {
120 return std::forward<EigMat>(M);
121}
122
128template <typename Tuple, require_tuple_t<Tuple>*>
129inline auto value_of(Tuple&& tup) {
130 return stan::math::apply(
131 [](auto&&... args) {
132 return make_holder_tuple(
133 value_of(std::forward<decltype(args)>(args))...);
134 },
135 std::forward<Tuple>(tup));
136}
137
138} // namespace math
139} // namespace stan
140
141#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.
constexpr auto make_holder_tuple(Types &&... args)
Holds ownership of rvalues and forwards lvalues into a tuple.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
auto make_holder(F &&func, Args &&... args)
Constructs an expression from given arguments using given functor.
Definition holder.hpp:368
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
typename plain_type< std::decay_t< T > >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...