@@ -10,102 +10,48 @@ namespace stan {
1010namespace math {
1111
1212/* *
13- * Return the value of the specified scalar argument
14- * converted to a double value.
13+ * Inputs that are arithmetic types or containers of airthmetric types
14+ * are returned from value_of unchanged
1515 *
16- * <p>See the <code>primitive_value</code> function to
17- * extract values without casting to <code>double</code>.
18- *
19- * <p>This function is meant to cover the primitive types. For
20- * types requiring pass-by-reference, this template function
21- * should be specialized.
22- *
23- * @tparam T type of scalar.
24- * @param x scalar to convert to double
25- * @return value of scalar cast to double
26- */
27- template <typename T, require_arithmetic_t <T>* = nullptr >
28- inline double value_of (const T x) {
29- return static_cast <double >(x);
30- }
31-
32- /* *
33- * Return the specified argument.
34- *
35- * <p>See <code>value_of(T)</code> for a polymorphic
36- * implementation using static casts.
37- *
38- * <p>This inline pass-through no-op should be compiled away.
39- *
40- * @param x value
41- * @return input value
42- */
43- template <>
44- inline double value_of<double >(double x) {
45- return x;
16+ * @tparam T Input type
17+ * @param[in] x Input argument
18+ * @return Forwarded input argument
19+ **/
20+ template <typename T, require_st_arithmetic<T>* = nullptr >
21+ inline decltype (auto ) value_of(T&& x) {
22+ return std::forward<T>(x);
4623}
4724
4825/* *
49- * Return the specified argument.
50- *
51- * <p>See <code>value_of(T)</code> for a polymorphic
52- * implementation using static casts.
26+ * For std::vectors of non-arithmetic types, return a std::vector composed
27+ * of value_of applied to each element.
5328 *
54- * <p>This inline pass-through no-op should be compiled away.
55- *
56- * @param x value
57- * @return input value
58- */
59- inline int value_of (int x) { return x; }
60-
61- /* *
62- * Convert a std::vector of type T to a std::vector of
63- * child_type<T>::type.
64- *
65- * @tparam T Scalar type in std::vector
66- * @param[in] x std::vector to be converted
29+ * @tparam T Input element type
30+ * @param[in] x Input std::vector
6731 * @return std::vector of values
6832 **/
69- template <typename T, require_not_double_or_int_t <T>* = nullptr >
70- inline std::vector<typename child_type<T>::type> value_of (
71- const std::vector<T>& x) {
72- size_t x_size = x.size ();
73- std::vector<typename child_type<T>::type> result (x_size);
74- for (size_t i = 0 ; i < x_size; i++) {
75- result[i] = value_of (x[i]);
33+ template <typename T, require_not_st_arithmetic<T>* = nullptr >
34+ inline auto value_of (const std::vector<T>& x) {
35+ std::vector<plain_type_t <decltype (value_of (std::declval<T>()))>> out;
36+ out.reserve (x.size ());
37+ for (auto && x_elem : x) {
38+ out.emplace_back (value_of (x_elem));
7639 }
77- return result ;
40+ return out ;
7841}
7942
8043/* *
81- * Return the specified argument.
82- *
83- * <p>See <code>value_of(T)</code> for a polymorphic
84- * implementation using static casts.
85- *
86- * <p>This inline pass-through no-op should be compiled away.
87- *
88- * @param x Specified std::vector.
89- * @return Specified std::vector.
90- */
91- template <typename Vec, require_std_vector_vt<is_double_or_int, Vec>* = nullptr >
92- inline Vec value_of (Vec&& x) {
93- return std::forward<Vec>(x);
94- }
95-
96- /* *
97- * Convert a matrix of type T to a matrix of doubles.
98- *
99- * T must implement value_of. See
100- * test/math/fwd/fun/value_of.cpp for fvar and var usage.
44+ * For Eigen matrices and expressions of non-arithmetic types, return an
45+ *expression that represents the Eigen::Matrix resulting from applying value_of
46+ *elementwise
10147 *
10248 * @tparam EigMat type of the matrix
10349 *
10450 * @param[in] M Matrix to be converted
10551 * @return Matrix of values
10652 **/
10753template <typename EigMat, require_eigen_t <EigMat>* = nullptr ,
108- require_not_vt_double_or_int <EigMat>* = nullptr >
54+ require_not_st_arithmetic <EigMat>* = nullptr >
10955inline auto value_of (EigMat&& M) {
11056 return make_holder (
11157 [](auto & a) {
@@ -114,25 +60,6 @@ inline auto value_of(EigMat&& M) {
11460 std::forward<EigMat>(M));
11561}
11662
117- /* *
118- * Return the specified argument.
119- *
120- * <p>See <code>value_of(T)</code> for a polymorphic
121- * implementation using static casts.
122- *
123- * <p>This inline pass-through no-op should be compiled away.
124- *
125- * @tparam EigMat type of the matrix
126- *
127- * @param x Specified matrix.
128- * @return Specified matrix.
129- */
130- template <typename EigMat,
131- require_eigen_vt<is_double_or_int, EigMat>* = nullptr >
132- inline EigMat value_of (EigMat&& x) {
133- return std::forward<EigMat>(x);
134- }
135-
13663} // namespace math
13764} // namespace stan
13865
0 commit comments