@@ -23,7 +23,7 @@ namespace math {
2323 * The log of the multivariate normal density for the given y, mu, and
2424 * a Cholesky factor L of the variance matrix.
2525 * Sigma = LL', a square, semi-positive definite matrix.
26- *
26+ *
2727 * This version of the function is vectorized on y and mu.
2828 *
2929 * Analytic expressions taken from
@@ -122,9 +122,9 @@ return_type_t<T_y, T_loc, T_covar> multi_normal_cholesky_lpdf(
122122 logp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
123123 }
124124
125- if (include_summand<propto, T_y, T_loc, T_covar_elem>::value) {
125+ if (include_summand<propto, T_y, T_loc, T_covar_elem>::value) {
126126 Eigen::Matrix<T_partials_return, Eigen::Dynamic, Eigen::Dynamic>
127- y_val_minus_mu_val (size_y, size_vec);
127+ y_val_minus_mu_val (size_y, size_vec);
128128
129129 for (size_t i = 0 ; i < size_vec; i++) {
130130 decltype (auto ) y_val = as_value_column_vector_or_scalar (y_vec[i]);
@@ -134,12 +134,13 @@ return_type_t<T_y, T_loc, T_covar> multi_normal_cholesky_lpdf(
134134
135135 matrix_partials_t half, scaled_diff;
136136
137- // If the covariance is not autodiff, we can avoid computing a matrix inverse
138- if (is_constant<T_covar_elem>::value) {
137+ // If the covariance is not autodiff, we can avoid computing a matrix
138+ // inverse
139+ if (is_constant<T_covar_elem>::value) {
139140 matrix_partials_t L_val = value_of (L_ref);
140141
141142 half = mdivide_left_tri<Eigen::Lower>(L_val, y_val_minus_mu_val)
142- .transpose ();
143+ .transpose ();
143144
144145 scaled_diff = mdivide_right_tri<Eigen::Lower>(half, L_val).transpose ();
145146
@@ -148,20 +149,24 @@ return_type_t<T_y, T_loc, T_covar> multi_normal_cholesky_lpdf(
148149 }
149150 } else {
150151 matrix_partials_t inv_L_val
151- = mdivide_left_tri<Eigen::Lower>(value_of (L_ref));
152+ = mdivide_left_tri<Eigen::Lower>(value_of (L_ref));
152153
153- half = (inv_L_val.template triangularView <Eigen::Lower>() * y_val_minus_mu_val).transpose ();
154+ half = (inv_L_val.template triangularView <Eigen::Lower>()
155+ * y_val_minus_mu_val)
156+ .transpose ();
154157
155- scaled_diff = (half * inv_L_val.template triangularView <Eigen::Lower>()).transpose ();
158+ scaled_diff = (half * inv_L_val.template triangularView <Eigen::Lower>())
159+ .transpose ();
156160
157161 logp += sum (log (inv_L_val.diagonal ())) * size_vec;
158162 ops_partials.edge3_ .partials_ -= size_vec * inv_L_val.transpose ();
159163
160164 for (size_t i = 0 ; i < size_vec; i++) {
161- ops_partials.edge3_ .partials_vec_ [i] += scaled_diff.col (i) * half.row (i);
165+ ops_partials.edge3_ .partials_vec_ [i]
166+ += scaled_diff.col (i) * half.row (i);
162167 }
163168 }
164-
169+
165170 logp -= 0.5 * sum (columns_dot_self (half));
166171
167172 for (size_t i = 0 ; i < size_vec; i++) {
@@ -251,30 +256,32 @@ return_type_t<T_y, T_loc, T_covar> multi_normal_cholesky_lpdf(
251256 row_vector_partials_t half;
252257 vector_partials_t scaled_diff;
253258
254- // If the covariance is not autodiff, we can avoid computing a matrix inverse
255- if (is_constant<T_covar_elem>::value) {
259+ // If the covariance is not autodiff, we can avoid computing a matrix
260+ // inverse
261+ if (is_constant<T_covar_elem>::value) {
256262 matrix_partials_t L_val = value_of (L_ref);
257-
263+
258264 half = mdivide_left_tri<Eigen::Lower>(L_val, y_val - mu_val).transpose ();
259265
260- scaled_diff
261- = mdivide_right_tri<Eigen::Lower>(half, L_val).transpose ();
266+ scaled_diff = mdivide_right_tri<Eigen::Lower>(half, L_val).transpose ();
262267
263268 if (include_summand<propto>::value) {
264269 logp -= sum (log (L_val.diagonal ()));
265270 }
266271 } else {
267272 matrix_partials_t inv_L_val
268- = mdivide_left_tri<Eigen::Lower>(value_of (L_ref));
273+ = mdivide_left_tri<Eigen::Lower>(value_of (L_ref));
269274
270275 half = (inv_L_val.template triangularView <Eigen::Lower>()
271- * (y_val - mu_val).template cast <T_partials_return>())
272- .transpose ();
276+ * (y_val - mu_val).template cast <T_partials_return>())
277+ .transpose ();
273278
274- scaled_diff = (half * inv_L_val.template triangularView <Eigen::Lower>()).transpose ();
279+ scaled_diff = (half * inv_L_val.template triangularView <Eigen::Lower>())
280+ .transpose ();
275281
276282 logp += sum (log (inv_L_val.diagonal ()));
277- ops_partials.edge3_ .partials_ += scaled_diff * half - inv_L_val.transpose ();
283+ ops_partials.edge3_ .partials_
284+ += scaled_diff * half - inv_L_val.transpose ();
278285 }
279286
280287 logp -= 0.5 * sum (dot_self (half));
0 commit comments