@@ -70,9 +70,10 @@ cholesky_factor_constrain(const T& x, int M, int N) {
70
70
* determinant
71
71
* @return Cholesky factor
72
72
*/
73
- template <typename T, require_eigen_vector_t <T>* = nullptr >
73
+ template <typename T, typename Lp, require_eigen_vector_t <T>* = nullptr ,
74
+ require_convertible_t <return_type_t <T>, Lp>* = nullptr >
74
75
inline Eigen::Matrix<value_type_t <T>, Eigen::Dynamic, Eigen::Dynamic>
75
- cholesky_factor_constrain (const T& x, int M, int N, return_type_t <T> & lp) {
76
+ cholesky_factor_constrain (const T& x, int M, int N, Lp & lp) {
76
77
check_size_match (" cholesky_factor_constrain" , " x.size()" , x.size (),
77
78
" ((N * (N + 1)) / 2 + (M - N) * N)" ,
78
79
((N * (N + 1 )) / 2 + (M - N) * N));
@@ -88,31 +89,46 @@ cholesky_factor_constrain(const T& x, int M, int N, return_type_t<T>& lp) {
88
89
/* *
89
90
* Return the Cholesky factor of the specified size read from the specified
90
91
* vector. A total of (N choose 2) + N + N * (M - N) free parameters are
91
- * required to read an M by N Cholesky factor. If the `Jacobian` parameter is
92
- * `true`, the log density accumulator is incremented with the log absolute
93
- * Jacobian determinant of the transform. All of the transforms are specified
94
- * with their Jacobians in the *Stan Reference Manual* chapter Constraint
95
- * Transforms.
92
+ * required to read an M by N Cholesky factor.
93
+ * This overload handles looping over the elements of a standard vector.
96
94
*
97
- * @tparam Jacobian if `true`, increment log density accumulator with log
98
- * absolute Jacobian determinant of constraining transform
99
- * @tparam T A type inheriting from `Eigen::DenseBase` or a `var_value` with
100
- * inner type inheriting from `Eigen::DenseBase` with compile time dynamic rows
101
- * and 1 column
95
+ * @tparam T A standard vector with inner type inheriting from
96
+ * `Eigen::DenseBase` or a `var_value` with inner type inheriting from
97
+ * `Eigen::DenseBase` with compile time dynamic rows and 1 column
98
+ * @param x Vector of unconstrained values
99
+ * @param M number of rows
100
+ * @param N number of columns
101
+ * @return Cholesky factor
102
+ */
103
+ template <typename T, require_std_vector_t <T>* = nullptr >
104
+ inline auto cholesky_factor_constrain (const T& x, int M, int N) {
105
+ return apply_vector_unary<T>::apply (
106
+ x, [M, N](auto && v) { return cholesky_factor_constrain (v, M, N); });
107
+ }
108
+
109
+ /* *
110
+ * Return the Cholesky factor of the specified size read from the specified
111
+ * vector. A total of (N choose 2) + N + N * (M - N) free parameters are
112
+ * required to read an M by N Cholesky factor.
113
+ * This overload handles looping over the elements of a standard vector.
114
+ *
115
+ * @tparam T A standard vector with inner type inheriting from
116
+ * `Eigen::DenseBase` or a `var_value` with inner type inheriting from
117
+ * `Eigen::DenseBase` with compile time dynamic rows and 1 column
118
+ * @tparam Lp Scalar type for the lp argument. The scalar type of T should be
119
+ * convertable to this.
102
120
* @param x Vector of unconstrained values
103
121
* @param M number of rows
104
122
* @param N number of columns
105
123
* @param[in,out] lp log density accumulator
106
124
* @return Cholesky factor
107
125
*/
108
- template <bool Jacobian, typename T, require_not_std_vector_t <T>* = nullptr >
109
- inline auto cholesky_factor_constrain (const T& x, int M, int N,
110
- return_type_t <T>& lp) {
111
- if (Jacobian) {
112
- return cholesky_factor_constrain (x, M, N, lp);
113
- } else {
114
- return cholesky_factor_constrain (x, M, N);
115
- }
126
+ template <typename T, typename Lp, require_std_vector_t <T>* = nullptr ,
127
+ require_convertible_t <return_type_t <T>, Lp>* = nullptr >
128
+ inline auto cholesky_factor_constrain (const T& x, int M, int N, Lp& lp) {
129
+ return apply_vector_unary<T>::apply (x, [&lp, M, N](auto && v) {
130
+ return cholesky_factor_constrain (v, M, N, lp);
131
+ });
116
132
}
117
133
118
134
/* *
@@ -126,21 +142,25 @@ inline auto cholesky_factor_constrain(const T& x, int M, int N,
126
142
*
127
143
* @tparam Jacobian if `true`, increment log density accumulator with log
128
144
* absolute Jacobian determinant of constraining transform
129
- * @tparam T A standard vector with inner type inheriting from
130
- * `Eigen::DenseBase` or a `var_value` with inner type inheriting from
131
- * `Eigen::DenseBase` with compile time dynamic rows and 1 column
145
+ * @tparam T A type inheriting from `Eigen::DenseBase` or a `var_value` with
146
+ * inner type inheriting from `Eigen::DenseBase` with compile time dynamic rows
147
+ * and 1 column
148
+ * @tparam Lp A scalar type for the lp argument. The scalar type of T should be
149
+ * convertable to this.
132
150
* @param x Vector of unconstrained values
133
151
* @param M number of rows
134
152
* @param N number of columns
135
153
* @param[in,out] lp log density accumulator
136
154
* @return Cholesky factor
137
155
*/
138
- template <bool Jacobian, typename T, require_std_vector_t <T>* = nullptr >
139
- inline auto cholesky_factor_constrain (const T& x, int M, int N,
140
- return_type_t <T>& lp) {
141
- return apply_vector_unary<T>::apply (x, [&lp, M, N](auto && v) {
142
- return cholesky_factor_constrain<Jacobian>(v, M, N, lp);
143
- });
156
+ template <bool Jacobian, typename T, typename Lp,
157
+ require_convertible_t <return_type_t <T>, Lp>* = nullptr >
158
+ inline auto cholesky_factor_constrain (const T& x, int M, int N, Lp& lp) {
159
+ if constexpr (Jacobian) {
160
+ return cholesky_factor_constrain (x, M, N, lp);
161
+ } else {
162
+ return cholesky_factor_constrain (x, M, N);
163
+ }
144
164
}
145
165
146
166
} // namespace math
0 commit comments