@@ -70,9 +70,10 @@ cholesky_factor_constrain(const T& x, int M, int N) {
7070 * determinant
7171 * @return Cholesky factor
7272 */
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 >
7475inline 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) {
7677 check_size_match (" cholesky_factor_constrain" , " x.size()" , x.size (),
7778 " ((N * (N + 1)) / 2 + (M - N) * N)" ,
7879 ((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) {
8889/* *
8990 * Return the Cholesky factor of the specified size read from the specified
9091 * 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.
9694 *
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.
102120 * @param x Vector of unconstrained values
103121 * @param M number of rows
104122 * @param N number of columns
105123 * @param[in,out] lp log density accumulator
106124 * @return Cholesky factor
107125 */
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+ });
116132}
117133
118134/* *
@@ -126,21 +142,25 @@ inline auto cholesky_factor_constrain(const T& x, int M, int N,
126142 *
127143 * @tparam Jacobian if `true`, increment log density accumulator with log
128144 * 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.
132150 * @param x Vector of unconstrained values
133151 * @param M number of rows
134152 * @param N number of columns
135153 * @param[in,out] lp log density accumulator
136154 * @return Cholesky factor
137155 */
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+ }
144164}
145165
146166} // namespace math
0 commit comments