@@ -89,34 +89,28 @@ cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) {
8989/* *
9090 * Return the Cholesky factor of the specified size read from the specified
9191 * vector. A total of (N choose 2) + N + N * (M - N) free parameters are
92- * required to read an M by N Cholesky factor. If the `Jacobian` parameter is
93- * `true`, the log density accumulator is incremented with the log absolute
94- * Jacobian determinant of the transform. All of the transforms are specified
95- * with their Jacobians in the *Stan Reference Manual* chapter Constraint
96- * Transforms.
92+ * required to read an M by N Cholesky factor.
93+ * This overload handles looping over the elements of a standard vector.
9794 *
98- * @tparam Jacobian if `true`, increment log density accumulator with log
99- * absolute Jacobian determinant of constraining transform
100- * @tparam T A type inheriting from `Eigen::DenseBase` or a `var_value` with
101- * inner type inheriting from `Eigen::DenseBase` with compile time dynamic rows
102- * and 1 column
103- * @tparam Lp A scalar type for the lp argument. The scalar type of T should be
104- * convertable to this.
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+ * @tparam Lp A pack that is either empty, or exactly one scalar type for the lp
99+ * argument. The scalar type of T should be convertable to this.
105100 * @param x Vector of unconstrained values
106101 * @param M number of rows
107102 * @param N number of columns
108- * @param[in,out] lp log density accumulator
103+ * @param[in,out] lp log density accumulator or empty
109104 * @return Cholesky factor
110105 */
111- template <bool Jacobian, typename T, typename Lp,
112- require_not_std_vector_t <T>* = nullptr ,
113- require_convertible_t <return_type_t <T>, Lp>* = nullptr >
114- inline auto cholesky_factor_constrain (const T& x, int M, int N, Lp& lp) {
115- if (Jacobian) {
116- return cholesky_factor_constrain (x, M, N, lp);
117- } else {
118- return cholesky_factor_constrain (x, M, N);
119- }
106+ template <typename T, typename ... Lp, require_std_vector_t <T>* = nullptr >
107+ inline auto cholesky_factor_constrain (const T& x, int M, int N, Lp&... lp) {
108+ static_assert (sizeof ...(lp) == 0 || sizeof ...(lp) == 1 ,
109+ " cholesky_factor_constrain should be called with either "
110+ " three or four arguments" );
111+ return apply_vector_unary<T>::apply (x, [&lp..., M, N](auto && v) {
112+ return cholesky_factor_constrain (v, M, N, lp...);
113+ });
120114}
121115
122116/* *
@@ -130,9 +124,9 @@ inline auto cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) {
130124 *
131125 * @tparam Jacobian if `true`, increment log density accumulator with log
132126 * absolute Jacobian determinant of constraining transform
133- * @tparam T A standard vector with inner type inheriting from
134- * `Eigen::DenseBase` or a `var_value` with inner type inheriting from
135- * `Eigen::DenseBase` with compile time dynamic rows and 1 column
127+ * @tparam T A type inheriting from `Eigen::DenseBase` or a `var_value` with
128+ * inner type inheriting from `Eigen::DenseBase` with compile time dynamic rows
129+ * and 1 column
136130 * @tparam Lp A scalar type for the lp argument. The scalar type of T should be
137131 * convertable to this.
138132 * @param x Vector of unconstrained values
@@ -142,12 +136,13 @@ inline auto cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) {
142136 * @return Cholesky factor
143137 */
144138template <bool Jacobian, typename T, typename Lp,
145- require_std_vector_t <T>* = nullptr ,
146139 require_convertible_t <return_type_t <T>, Lp>* = nullptr >
147140inline auto cholesky_factor_constrain (const T& x, int M, int N, Lp& lp) {
148- return apply_vector_unary<T>::apply (x, [&lp, M, N](auto && v) {
149- return cholesky_factor_constrain<Jacobian>(v, M, N, lp);
150- });
141+ if constexpr (Jacobian) {
142+ return cholesky_factor_constrain (x, M, N, lp);
143+ } else {
144+ return cholesky_factor_constrain (x, M, N);
145+ }
151146}
152147
153148} // namespace math
0 commit comments