From 5cff178f5b1c8bbba1c77dcd1824f2320b0440ae Mon Sep 17 00:00:00 2001 From: stevo15025 Date: Thu, 5 Sep 2019 10:42:18 +0300 Subject: [PATCH 01/12] Add braces to everything --- make/clang-tidy | 8 +++ stan/math/fwd/arr/fun/log_sum_exp.hpp | 3 +- stan/math/fwd/arr/fun/sum.hpp | 3 +- stan/math/fwd/arr/fun/to_fvar.hpp | 6 +- stan/math/fwd/core/fvar.hpp | 9 ++- stan/math/fwd/mat/fun/crossprod.hpp | 3 +- stan/math/fwd/mat/fun/divide.hpp | 9 ++- stan/math/fwd/mat/fun/dot_product.hpp | 36 ++++++---- stan/math/fwd/mat/fun/log_softmax.hpp | 8 ++- stan/math/fwd/mat/fun/mdivide_left_ldlt.hpp | 3 +- stan/math/fwd/mat/fun/multiply.hpp | 9 ++- .../fun/multiply_lower_tri_self_transpose.hpp | 6 +- stan/math/fwd/mat/fun/qr_Q.hpp | 6 +- stan/math/fwd/mat/fun/qr_R.hpp | 6 +- stan/math/fwd/mat/fun/softmax.hpp | 3 +- stan/math/fwd/mat/fun/sum.hpp | 3 +- stan/math/fwd/mat/fun/tcrossprod.hpp | 3 +- stan/math/fwd/mat/fun/to_fvar.hpp | 3 +- .../fwd/mat/fun/unit_vector_constrain.hpp | 9 ++- stan/math/fwd/mat/functor/gradient.hpp | 6 +- stan/math/fwd/mat/functor/hessian.hpp | 9 ++- stan/math/fwd/scal/fun/abs.hpp | 9 +-- stan/math/fwd/scal/fun/binary_log_loss.hpp | 5 +- stan/math/fwd/scal/fun/fabs.hpp | 9 +-- stan/math/fwd/scal/fun/fdim.hpp | 15 ++-- stan/math/fwd/scal/fun/fmax.hpp | 15 ++-- stan/math/fwd/scal/fun/fmin.hpp | 15 ++-- stan/math/fwd/scal/fun/fmod.hpp | 5 +- stan/math/fwd/scal/fun/gamma_p.hpp | 15 ++-- stan/math/fwd/scal/fun/grad_inc_beta.hpp | 3 +- stan/math/fwd/scal/fun/lmgamma.hpp | 3 +- stan/math/fwd/scal/fun/log.hpp | 5 +- stan/math/fwd/scal/fun/log10.hpp | 5 +- stan/math/fwd/scal/fun/log1m_exp.hpp | 3 +- stan/math/fwd/scal/fun/log2.hpp | 5 +- stan/math/fwd/scal/fun/log_diff_exp.hpp | 9 ++- stan/math/fwd/scal/fun/log_sum_exp.hpp | 6 +- stan/math/fwd/scal/fun/logit.hpp | 5 +- stan/math/fwd/scal/fun/pow.hpp | 18 +++-- stan/math/memory/stack_alloc.hpp | 42 +++++++---- stan/math/mix/mat/functor/grad_hessian.hpp | 3 +- .../mat/functor/grad_tr_mat_times_hessian.hpp | 12 ++-- .../mix/mat/functor/gradient_dot_vector.hpp | 3 +- stan/math/mix/mat/functor/hessian.hpp | 9 ++- .../mix/mat/functor/hessian_times_vector.hpp | 6 +- .../mix/mat/functor/partial_derivative.hpp | 3 +- stan/math/opencl/cholesky_decompose.hpp | 3 +- stan/math/opencl/diagonal_multiply.hpp | 6 +- stan/math/opencl/err/check_diagonal_zeros.hpp | 3 +- stan/math/opencl/err/check_nan.hpp | 3 +- stan/math/opencl/err/check_symmetric.hpp | 3 +- stan/math/opencl/err/check_vector.hpp | 9 ++- stan/math/opencl/matrix_cl.hpp | 9 ++- stan/math/opencl/multiply.hpp | 3 +- stan/math/opencl/multiply_transpose.hpp | 3 +- stan/math/opencl/opencl_context.hpp | 3 +- stan/math/opencl/transpose.hpp | 3 +- stan/math/opencl/zeros.hpp | 3 +- stan/math/prim/arr/err/check_nonzero_size.hpp | 3 +- stan/math/prim/arr/err/is_ordered.hpp | 3 +- stan/math/prim/arr/fun/dot.hpp | 3 +- stan/math/prim/arr/fun/dot_self.hpp | 3 +- stan/math/prim/arr/fun/fill.hpp | 3 +- stan/math/prim/arr/fun/inverse_softmax.hpp | 3 +- stan/math/prim/arr/fun/log_sum_exp.hpp | 12 ++-- stan/math/prim/arr/fun/promote_elements.hpp | 3 +- stan/math/prim/arr/fun/promote_scalar.hpp | 3 +- stan/math/prim/arr/fun/scaled_add.hpp | 3 +- stan/math/prim/arr/fun/sub.hpp | 3 +- stan/math/prim/arr/fun/value_of.hpp | 3 +- stan/math/prim/arr/fun/value_of_rec.hpp | 3 +- .../prim/arr/functor/coupled_ode_observer.hpp | 6 +- .../prim/arr/functor/coupled_ode_system.hpp | 3 +- stan/math/prim/arr/functor/integrate_1d.hpp | 3 +- .../prim/arr/functor/integrate_ode_rk45.hpp | 9 ++- .../prim/mat/err/check_cholesky_factor.hpp | 3 +- .../mat/err/check_cholesky_factor_corr.hpp | 3 +- stan/math/prim/mat/err/check_column_index.hpp | 3 +- .../mat/err/check_consistent_size_mvt.hpp | 8 ++- stan/math/prim/mat/err/check_finite.hpp | 3 +- stan/math/prim/mat/err/check_pos_definite.hpp | 12 ++-- .../prim/mat/err/check_pos_semidefinite.hpp | 9 ++- .../prim/mat/err/check_positive_ordered.hpp | 3 +- stan/math/prim/mat/err/check_range.hpp | 9 ++- stan/math/prim/mat/err/check_row_index.hpp | 3 +- .../prim/mat/err/check_std_vector_index.hpp | 3 +- stan/math/prim/mat/err/check_symmetric.hpp | 3 +- stan/math/prim/mat/err/check_vector.hpp | 9 ++- .../prim/mat/err/is_cholesky_factor_corr.hpp | 6 +- stan/math/prim/mat/err/is_corr_matrix.hpp | 12 ++-- stan/math/prim/mat/err/is_pos_definite.hpp | 12 ++-- stan/math/prim/mat/err/is_symmetric.hpp | 9 ++- stan/math/prim/mat/fun/LDLT_factor.hpp | 12 ++-- stan/math/prim/mat/fun/MatrixExponential.h | 4 +- stan/math/prim/mat/fun/accumulator.hpp | 6 +- stan/math/prim/mat/fun/append_array.hpp | 6 +- stan/math/prim/mat/fun/append_col.hpp | 18 +++-- stan/math/prim/mat/fun/append_row.hpp | 12 ++-- stan/math/prim/mat/fun/assign.hpp | 17 +++-- stan/math/prim/mat/fun/autocorrelation.hpp | 27 +++++--- stan/math/prim/mat/fun/chol2inv.hpp | 3 +- .../prim/mat/fun/cholesky_corr_constrain.hpp | 12 ++-- .../mat/fun/cholesky_factor_constrain.hpp | 12 ++-- .../prim/mat/fun/cholesky_factor_free.hpp | 9 ++- .../prim/mat/fun/corr_matrix_constrain.hpp | 6 +- stan/math/prim/mat/fun/corr_matrix_free.hpp | 3 +- .../prim/mat/fun/cov_matrix_constrain.hpp | 15 ++-- .../prim/mat/fun/cov_matrix_constrain_lkj.hpp | 12 ++-- stan/math/prim/mat/fun/cov_matrix_free.hpp | 6 +- .../math/prim/mat/fun/cov_matrix_free_lkj.hpp | 9 ++- stan/math/prim/mat/fun/csr_extract_u.hpp | 3 +- stan/math/prim/mat/fun/csr_extract_v.hpp | 3 +- stan/math/prim/mat/fun/csr_extract_w.hpp | 3 +- .../prim/mat/fun/csr_matrix_times_vector.hpp | 3 +- .../math/prim/mat/fun/csr_to_dense_matrix.hpp | 3 +- stan/math/prim/mat/fun/cumulative_sum.hpp | 6 +- stan/math/prim/mat/fun/dims.hpp | 3 +- stan/math/prim/mat/fun/dot_product.hpp | 3 +- stan/math/prim/mat/fun/factor_cov_matrix.hpp | 6 +- stan/math/prim/mat/fun/gp_dot_prod_cov.hpp | 12 ++-- stan/math/prim/mat/fun/gp_exp_quad_cov.hpp | 69 ++++++++++++------- stan/math/prim/mat/fun/gp_exponential_cov.hpp | 45 ++++++++---- stan/math/prim/mat/fun/gp_matern32_cov.hpp | 51 +++++++++----- stan/math/prim/mat/fun/gp_matern52_cov.hpp | 30 +++++--- stan/math/prim/mat/fun/gp_periodic_cov.hpp | 15 ++-- stan/math/prim/mat/fun/head.hpp | 12 ++-- stan/math/prim/mat/fun/initialize.hpp | 6 +- stan/math/prim/mat/fun/inverse_spd.hpp | 12 ++-- stan/math/prim/mat/fun/log_mix.hpp | 54 ++++++++++----- stan/math/prim/mat/fun/log_sum_exp.hpp | 3 +- stan/math/prim/mat/fun/make_nu.hpp | 6 +- .../mat/fun/matrix_exp_action_handler.hpp | 3 +- stan/math/prim/mat/fun/matrix_exp_pade.hpp | 3 +- stan/math/prim/mat/fun/max.hpp | 6 +- stan/math/prim/mat/fun/mdivide_right_tri.hpp | 3 +- stan/math/prim/mat/fun/min.hpp | 6 +- .../fun/multiply_lower_tri_self_transpose.hpp | 6 +- stan/math/prim/mat/fun/num_elements.hpp | 3 +- stan/math/prim/mat/fun/ordered_constrain.hpp | 9 ++- stan/math/prim/mat/fun/ordered_free.hpp | 6 +- .../mat/fun/positive_ordered_constrain.hpp | 9 ++- .../prim/mat/fun/positive_ordered_free.hpp | 6 +- stan/math/prim/mat/fun/prod.hpp | 6 +- stan/math/prim/mat/fun/promote_elements.hpp | 3 +- stan/math/prim/mat/fun/promote_scalar.hpp | 9 ++- stan/math/prim/mat/fun/qr_Q.hpp | 6 +- stan/math/prim/mat/fun/qr_R.hpp | 9 ++- stan/math/prim/mat/fun/qr_thin_Q.hpp | 6 +- stan/math/prim/mat/fun/qr_thin_R.hpp | 6 +- stan/math/prim/mat/fun/rank.hpp | 6 +- stan/math/prim/mat/fun/read_corr_L.hpp | 3 +- stan/math/prim/mat/fun/resize.hpp | 6 +- stan/math/prim/mat/fun/sd.hpp | 6 +- stan/math/prim/mat/fun/segment.hpp | 3 +- stan/math/prim/mat/fun/sort_indices.hpp | 8 ++- stan/math/prim/mat/fun/stan_print.hpp | 15 ++-- stan/math/prim/mat/fun/sub_col.hpp | 3 +- stan/math/prim/mat/fun/sub_row.hpp | 3 +- stan/math/prim/mat/fun/tail.hpp | 12 ++-- stan/math/prim/mat/fun/tcrossprod.hpp | 6 +- stan/math/prim/mat/fun/to_array_1d.hpp | 12 ++-- stan/math/prim/mat/fun/to_array_2d.hpp | 6 +- stan/math/prim/mat/fun/to_matrix.hpp | 39 +++++++---- stan/math/prim/mat/fun/to_row_vector.hpp | 3 +- stan/math/prim/mat/fun/to_vector.hpp | 3 +- stan/math/prim/mat/fun/value_of.hpp | 6 +- stan/math/prim/mat/fun/value_of_rec.hpp | 6 +- stan/math/prim/mat/fun/variance.hpp | 6 +- .../prim/mat/fun/welford_covar_estimator.hpp | 3 +- .../prim/mat/fun/welford_var_estimator.hpp | 3 +- stan/math/prim/mat/functor/map_rect.hpp | 3 +- .../prim/mat/functor/map_rect_combine.hpp | 9 ++- .../mat/prob/bernoulli_logit_glm_lpmf.hpp | 14 ++-- .../prim/mat/prob/bernoulli_logit_glm_rng.hpp | 3 +- .../mat/prob/categorical_logit_glm_lpmf.hpp | 6 +- .../prim/mat/prob/categorical_logit_lpmf.hpp | 15 ++-- .../prim/mat/prob/categorical_logit_rng.hpp | 3 +- stan/math/prim/mat/prob/categorical_lpmf.hpp | 18 +++-- stan/math/prim/mat/prob/categorical_rng.hpp | 3 +- stan/math/prim/mat/prob/dirichlet_lpmf.hpp | 12 ++-- stan/math/prim/mat/prob/dirichlet_rng.hpp | 3 +- .../prim/mat/prob/gaussian_dlm_obs_lpdf.hpp | 6 +- .../prim/mat/prob/gaussian_dlm_obs_rng.hpp | 6 +- stan/math/prim/mat/prob/inv_wishart_lpdf.hpp | 6 +- .../prim/mat/prob/lkj_corr_cholesky_lpdf.hpp | 9 ++- stan/math/prim/mat/prob/lkj_corr_lpdf.hpp | 23 ++++--- stan/math/prim/mat/prob/lkj_cov_lpdf.hpp | 6 +- .../prim/mat/prob/matrix_normal_prec_lpdf.hpp | 3 +- .../prim/mat/prob/multi_gp_cholesky_lpdf.hpp | 3 +- stan/math/prim/mat/prob/multi_gp_lpdf.hpp | 3 +- .../mat/prob/multi_normal_cholesky_lpdf.hpp | 18 +++-- .../mat/prob/multi_normal_cholesky_rng.hpp | 3 +- stan/math/prim/mat/prob/multi_normal_lpdf.hpp | 15 ++-- .../prim/mat/prob/multi_normal_prec_lpdf.hpp | 15 ++-- .../prim/mat/prob/multi_normal_prec_rng.hpp | 3 +- stan/math/prim/mat/prob/multi_normal_rng.hpp | 3 +- .../prim/mat/prob/multi_student_t_lpdf.hpp | 15 ++-- .../prim/mat/prob/multi_student_t_rng.hpp | 3 +- stan/math/prim/mat/prob/multinomial_lpmf.hpp | 9 ++- stan/math/prim/mat/prob/multinomial_rng.hpp | 3 +- .../mat/prob/neg_binomial_2_log_glm_lpmf.hpp | 26 ++++--- .../math/prim/mat/prob/normal_id_glm_lpdf.hpp | 28 +++++--- .../prim/mat/prob/ordered_logistic_lpmf.hpp | 21 ++++-- .../prim/mat/prob/ordered_logistic_rng.hpp | 3 +- .../math/prim/mat/prob/ordered_probit_rng.hpp | 3 +- .../prim/mat/prob/poisson_log_glm_lpmf.hpp | 14 ++-- stan/math/prim/mat/prob/wishart_lpdf.hpp | 12 ++-- stan/math/prim/mat/prob/wishart_rng.hpp | 3 +- .../prim/mat/vectorize/apply_scalar_unary.hpp | 3 +- .../prim/scal/err/check_2F1_converges.hpp | 3 +- .../prim/scal/err/check_3F2_converges.hpp | 9 ++- .../prim/scal/err/check_consistent_size.hpp | 3 +- stan/math/prim/scal/err/check_finite.hpp | 6 +- stan/math/prim/scal/err/check_nonnegative.hpp | 6 +- stan/math/prim/scal/err/check_not_nan.hpp | 6 +- stan/math/prim/scal/err/check_positive.hpp | 6 +- stan/math/prim/scal/err/check_size_match.hpp | 6 +- stan/math/prim/scal/err/is_less_or_equal.hpp | 3 +- stan/math/prim/scal/err/is_not_nan.hpp | 3 +- stan/math/prim/scal/err/is_positive.hpp | 3 +- stan/math/prim/scal/err/is_scal_finite.hpp | 3 +- stan/math/prim/scal/fun/F32.hpp | 6 +- stan/math/prim/scal/fun/Phi.hpp | 9 +-- stan/math/prim/scal/fun/choose.hpp | 3 +- stan/math/prim/scal/fun/divide.hpp | 3 +- stan/math/prim/scal/fun/fdim.hpp | 3 +- stan/math/prim/scal/fun/fmax.hpp | 6 +- stan/math/prim/scal/fun/fmin.hpp | 6 +- stan/math/prim/scal/fun/gamma_p.hpp | 6 +- stan/math/prim/scal/fun/grad_2F1.hpp | 12 ++-- stan/math/prim/scal/fun/grad_F32.hpp | 15 ++-- stan/math/prim/scal/fun/grad_inc_beta.hpp | 3 +- .../math/prim/scal/fun/grad_reg_inc_gamma.hpp | 12 ++-- .../scal/fun/grad_reg_lower_inc_gamma.hpp | 18 +++-- stan/math/prim/scal/fun/inc_beta_dda.hpp | 21 ++++-- stan/math/prim/scal/fun/inc_beta_ddb.hpp | 12 ++-- stan/math/prim/scal/fun/inv_Phi.hpp | 6 +- stan/math/prim/scal/fun/inv_logit.hpp | 3 +- stan/math/prim/scal/fun/lb_constrain.hpp | 6 +- stan/math/prim/scal/fun/lb_free.hpp | 3 +- stan/math/prim/scal/fun/lmgamma.hpp | 3 +- .../math/prim/scal/fun/locscale_constrain.hpp | 6 +- stan/math/prim/scal/fun/locscale_free.hpp | 3 +- stan/math/prim/scal/fun/log1m.hpp | 3 +- stan/math/prim/scal/fun/log1m_exp.hpp | 7 +- stan/math/prim/scal/fun/log1m_inv_logit.hpp | 3 +- stan/math/prim/scal/fun/log1p_exp.hpp | 3 +- stan/math/prim/scal/fun/log_diff_exp.hpp | 3 +- .../prim/scal/fun/log_falling_factorial.hpp | 3 +- stan/math/prim/scal/fun/log_inv_logit.hpp | 3 +- .../fun/log_modified_bessel_first_kind.hpp | 9 ++- .../prim/scal/fun/log_rising_factorial.hpp | 3 +- stan/math/prim/scal/fun/log_sum_exp.hpp | 9 ++- stan/math/prim/scal/fun/lub_constrain.hpp | 24 ++++--- stan/math/prim/scal/fun/lub_free.hpp | 6 +- stan/math/prim/scal/fun/modulus.hpp | 3 +- stan/math/prim/scal/fun/multiply_log.hpp | 3 +- .../scal/fun/offset_multiplier_constrain.hpp | 6 +- .../prim/scal/fun/offset_multiplier_free.hpp | 3 +- stan/math/prim/scal/fun/tgamma.hpp | 3 +- stan/math/prim/scal/fun/trigamma.hpp | 3 +- stan/math/prim/scal/fun/ub_constrain.hpp | 6 +- stan/math/prim/scal/fun/ub_free.hpp | 3 +- stan/math/prim/scal/prob/bernoulli_cdf.hpp | 15 ++-- stan/math/prim/scal/prob/bernoulli_lccdf.hpp | 9 ++- stan/math/prim/scal/prob/bernoulli_lcdf.hpp | 12 ++-- .../prim/scal/prob/bernoulli_logit_lpmf.hpp | 20 +++--- stan/math/prim/scal/prob/bernoulli_lpmf.hpp | 22 +++--- .../math/prim/scal/prob/beta_binomial_cdf.hpp | 12 ++-- .../prim/scal/prob/beta_binomial_lccdf.hpp | 6 +- .../prim/scal/prob/beta_binomial_lcdf.hpp | 6 +- .../prim/scal/prob/beta_binomial_lpmf.hpp | 3 +- stan/math/prim/scal/prob/beta_cdf.hpp | 27 +++++--- stan/math/prim/scal/prob/beta_lccdf.hpp | 12 ++-- stan/math/prim/scal/prob/beta_lcdf.hpp | 12 ++-- stan/math/prim/scal/prob/beta_lpdf.hpp | 57 ++++++++++----- .../prim/scal/prob/beta_proportion_lccdf.hpp | 12 ++-- .../prim/scal/prob/beta_proportion_lcdf.hpp | 12 ++-- .../prim/scal/prob/beta_proportion_lpdf.hpp | 27 +++++--- stan/math/prim/scal/prob/binomial_cdf.hpp | 12 ++-- stan/math/prim/scal/prob/binomial_lccdf.hpp | 9 ++- stan/math/prim/scal/prob/binomial_lcdf.hpp | 9 ++- .../prim/scal/prob/binomial_logit_lpmf.hpp | 21 ++++-- stan/math/prim/scal/prob/binomial_lpmf.hpp | 18 +++-- stan/math/prim/scal/prob/cauchy_cdf.hpp | 24 ++++--- stan/math/prim/scal/prob/cauchy_lccdf.hpp | 12 ++-- stan/math/prim/scal/prob/cauchy_lcdf.hpp | 12 ++-- stan/math/prim/scal/prob/cauchy_lpdf.hpp | 24 ++++--- stan/math/prim/scal/prob/chi_square_cdf.hpp | 21 ++++-- stan/math/prim/scal/prob/chi_square_lccdf.hpp | 15 ++-- stan/math/prim/scal/prob/chi_square_lcdf.hpp | 15 ++-- stan/math/prim/scal/prob/chi_square_lpdf.hpp | 39 +++++++---- .../prim/scal/prob/double_exponential_cdf.hpp | 26 ++++--- .../scal/prob/double_exponential_lccdf.hpp | 21 ++++-- .../scal/prob/double_exponential_lcdf.hpp | 21 ++++-- .../scal/prob/double_exponential_lpdf.hpp | 30 +++++--- .../prim/scal/prob/exp_mod_normal_cdf.hpp | 30 +++++--- .../prim/scal/prob/exp_mod_normal_lccdf.hpp | 20 ++++-- .../prim/scal/prob/exp_mod_normal_lcdf.hpp | 20 ++++-- .../prim/scal/prob/exp_mod_normal_lpdf.hpp | 27 +++++--- .../prim/scal/prob/exp_mod_normal_rng.hpp | 3 +- stan/math/prim/scal/prob/exponential_cdf.hpp | 9 ++- .../math/prim/scal/prob/exponential_lccdf.hpp | 9 ++- stan/math/prim/scal/prob/exponential_lcdf.hpp | 9 ++- stan/math/prim/scal/prob/exponential_lpdf.hpp | 21 ++++-- stan/math/prim/scal/prob/frechet_cdf.hpp | 21 ++++-- stan/math/prim/scal/prob/frechet_lccdf.hpp | 12 ++-- stan/math/prim/scal/prob/frechet_lcdf.hpp | 12 ++-- stan/math/prim/scal/prob/frechet_lpdf.hpp | 51 +++++++++----- stan/math/prim/scal/prob/gamma_cdf.hpp | 27 +++++--- stan/math/prim/scal/prob/gamma_lccdf.hpp | 18 +++-- stan/math/prim/scal/prob/gamma_lcdf.hpp | 18 +++-- stan/math/prim/scal/prob/gamma_lpdf.hpp | 42 +++++++---- stan/math/prim/scal/prob/gumbel_cdf.hpp | 21 ++++-- stan/math/prim/scal/prob/gumbel_lccdf.hpp | 12 ++-- stan/math/prim/scal/prob/gumbel_lcdf.hpp | 12 ++-- stan/math/prim/scal/prob/gumbel_lpdf.hpp | 24 ++++--- stan/math/prim/scal/prob/gumbel_rng.hpp | 3 +- .../prim/scal/prob/hypergeometric_lpmf.hpp | 9 ++- .../prim/scal/prob/hypergeometric_rng.hpp | 5 +- .../prim/scal/prob/inv_chi_square_cdf.hpp | 21 ++++-- .../prim/scal/prob/inv_chi_square_lccdf.hpp | 15 ++-- .../prim/scal/prob/inv_chi_square_lcdf.hpp | 15 ++-- .../prim/scal/prob/inv_chi_square_lpdf.hpp | 36 ++++++---- stan/math/prim/scal/prob/inv_gamma_cdf.hpp | 27 +++++--- stan/math/prim/scal/prob/inv_gamma_lccdf.hpp | 18 +++-- stan/math/prim/scal/prob/inv_gamma_lcdf.hpp | 18 +++-- stan/math/prim/scal/prob/inv_gamma_lpdf.hpp | 48 ++++++++----- stan/math/prim/scal/prob/logistic_cdf.hpp | 24 ++++--- stan/math/prim/scal/prob/logistic_lccdf.hpp | 15 ++-- stan/math/prim/scal/prob/logistic_lcdf.hpp | 15 ++-- stan/math/prim/scal/prob/logistic_lpdf.hpp | 39 +++++++---- stan/math/prim/scal/prob/logistic_rng.hpp | 3 +- stan/math/prim/scal/prob/lognormal_cdf.hpp | 24 ++++--- stan/math/prim/scal/prob/lognormal_lccdf.hpp | 15 ++-- stan/math/prim/scal/prob/lognormal_lcdf.hpp | 15 ++-- stan/math/prim/scal/prob/lognormal_lpdf.hpp | 51 +++++++++----- .../prim/scal/prob/neg_binomial_2_cdf.hpp | 18 +++-- .../prim/scal/prob/neg_binomial_2_lccdf.hpp | 6 +- .../prim/scal/prob/neg_binomial_2_lcdf.hpp | 14 ++-- .../scal/prob/neg_binomial_2_log_lpmf.hpp | 42 +++++++---- .../prim/scal/prob/neg_binomial_2_lpmf.hpp | 42 +++++++---- stan/math/prim/scal/prob/neg_binomial_cdf.hpp | 18 +++-- .../prim/scal/prob/neg_binomial_lccdf.hpp | 12 ++-- .../math/prim/scal/prob/neg_binomial_lcdf.hpp | 12 ++-- .../math/prim/scal/prob/neg_binomial_lpmf.hpp | 54 ++++++++++----- stan/math/prim/scal/prob/normal_cdf.hpp | 30 +++++--- stan/math/prim/scal/prob/normal_lccdf.hpp | 21 +++--- stan/math/prim/scal/prob/normal_lcdf.hpp | 21 +++--- stan/math/prim/scal/prob/normal_lpdf.hpp | 27 +++++--- .../prim/scal/prob/normal_sufficient_lpdf.hpp | 24 ++++--- stan/math/prim/scal/prob/pareto_cdf.hpp | 24 ++++--- stan/math/prim/scal/prob/pareto_lccdf.hpp | 15 ++-- stan/math/prim/scal/prob/pareto_lcdf.hpp | 15 ++-- stan/math/prim/scal/prob/pareto_lpdf.hpp | 39 +++++++---- .../math/prim/scal/prob/pareto_type_2_cdf.hpp | 33 ++++++--- .../prim/scal/prob/pareto_type_2_lccdf.hpp | 21 ++++-- .../prim/scal/prob/pareto_type_2_lcdf.hpp | 18 +++-- .../prim/scal/prob/pareto_type_2_lpdf.hpp | 39 +++++++---- .../math/prim/scal/prob/pareto_type_2_rng.hpp | 3 +- stan/math/prim/scal/prob/poisson_cdf.hpp | 15 ++-- stan/math/prim/scal/prob/poisson_lccdf.hpp | 12 ++-- stan/math/prim/scal/prob/poisson_lcdf.hpp | 12 ++-- stan/math/prim/scal/prob/poisson_log_lpmf.hpp | 33 ++++++--- stan/math/prim/scal/prob/poisson_lpmf.hpp | 27 +++++--- stan/math/prim/scal/prob/rayleigh_cdf.hpp | 12 ++-- stan/math/prim/scal/prob/rayleigh_lccdf.hpp | 12 ++-- stan/math/prim/scal/prob/rayleigh_lcdf.hpp | 12 ++-- stan/math/prim/scal/prob/rayleigh_lpdf.hpp | 21 ++++-- .../scal/prob/scaled_inv_chi_square_cdf.hpp | 24 ++++--- .../scal/prob/scaled_inv_chi_square_lccdf.hpp | 15 ++-- .../scal/prob/scaled_inv_chi_square_lcdf.hpp | 15 ++-- .../scal/prob/scaled_inv_chi_square_lpdf.hpp | 54 ++++++++++----- stan/math/prim/scal/prob/skew_normal_cdf.hpp | 27 +++++--- .../math/prim/scal/prob/skew_normal_lccdf.hpp | 15 ++-- stan/math/prim/scal/prob/skew_normal_lcdf.hpp | 15 ++-- stan/math/prim/scal/prob/skew_normal_lpdf.hpp | 33 ++++++--- stan/math/prim/scal/prob/skew_normal_rng.hpp | 3 +- stan/math/prim/scal/prob/std_normal_lpdf.hpp | 12 ++-- stan/math/prim/scal/prob/student_t_cdf.hpp | 36 ++++++---- stan/math/prim/scal/prob/student_t_lccdf.hpp | 24 ++++--- stan/math/prim/scal/prob/student_t_lcdf.hpp | 24 ++++--- stan/math/prim/scal/prob/student_t_lpdf.hpp | 39 +++++++---- stan/math/prim/scal/prob/uniform_cdf.hpp | 24 ++++--- stan/math/prim/scal/prob/uniform_lccdf.hpp | 18 +++-- stan/math/prim/scal/prob/uniform_lcdf.hpp | 18 +++-- stan/math/prim/scal/prob/uniform_lpdf.hpp | 30 +++++--- stan/math/prim/scal/prob/von_mises_lpdf.hpp | 33 ++++++--- stan/math/prim/scal/prob/von_mises_rng.hpp | 3 +- stan/math/prim/scal/prob/weibull_cdf.hpp | 21 ++++-- stan/math/prim/scal/prob/weibull_lccdf.hpp | 12 ++-- stan/math/prim/scal/prob/weibull_lcdf.hpp | 12 ++-- stan/math/prim/scal/prob/weibull_lpdf.hpp | 54 ++++++++++----- stan/math/prim/scal/prob/wiener_lpdf.hpp | 15 ++-- stan/math/rev/arr/fun/log_sum_exp.hpp | 12 ++-- stan/math/rev/arr/fun/sum.hpp | 9 ++- stan/math/rev/arr/fun/to_var.hpp | 3 +- .../rev/arr/functor/coupled_ode_system.hpp | 39 +++++++---- stan/math/rev/arr/functor/integrate_1d.hpp | 6 +- stan/math/rev/core/gevv_vvv_vari.hpp | 9 ++- stan/math/rev/core/matrix_vari.hpp | 3 +- stan/math/rev/core/operator_addition.hpp | 11 +-- stan/math/rev/core/operator_divide_equal.hpp | 3 +- stan/math/rev/core/operator_division.hpp | 8 ++- stan/math/rev/core/operator_minus_equal.hpp | 3 +- .../math/rev/core/operator_multiplication.hpp | 11 +-- .../math/rev/core/operator_multiply_equal.hpp | 3 +- stan/math/rev/core/operator_plus_equal.hpp | 3 +- stan/math/rev/core/operator_subtraction.hpp | 13 ++-- .../rev/core/operator_unary_decrement.hpp | 5 +- .../rev/core/operator_unary_increment.hpp | 5 +- .../math/rev/core/operator_unary_negative.hpp | 5 +- stan/math/rev/core/operator_unary_plus.hpp | 3 +- stan/math/rev/core/precomputed_gradients.hpp | 6 +- stan/math/rev/core/print_stack.hpp | 3 +- stan/math/rev/core/recover_memory.hpp | 3 +- stan/math/rev/core/recover_memory_nested.hpp | 3 +- stan/math/rev/core/set_zero_all_adjoints.hpp | 6 +- .../rev/core/set_zero_all_adjoints_nested.hpp | 6 +- stan/math/rev/core/stored_gradient_vari.hpp | 3 +- stan/math/rev/core/var.hpp | 6 +- stan/math/rev/core/vari.hpp | 5 +- stan/math/rev/core/vector_vari.hpp | 3 +- stan/math/rev/mat/fun/cholesky_decompose.hpp | 12 ++-- stan/math/rev/mat/fun/cov_exp_quad.hpp | 6 +- stan/math/rev/mat/fun/dot_product.hpp | 3 +- stan/math/rev/mat/fun/gp_exp_quad_cov.hpp | 18 +++-- stan/math/rev/mat/fun/gp_periodic_cov.hpp | 12 ++-- stan/math/rev/mat/fun/initialize_variable.hpp | 3 +- stan/math/rev/mat/fun/log_softmax.hpp | 8 ++- stan/math/rev/mat/fun/log_sum_exp.hpp | 3 +- stan/math/rev/mat/fun/mdivide_left_tri.hpp | 48 ++++++++----- .../fun/multiply_lower_tri_self_transpose.hpp | 11 +-- stan/math/rev/mat/fun/ordered_constrain.hpp | 3 +- .../mat/fun/positive_ordered_constrain.hpp | 3 +- stan/math/rev/mat/fun/sd.hpp | 6 +- stan/math/rev/mat/fun/softmax.hpp | 3 +- stan/math/rev/mat/fun/sum.hpp | 3 +- stan/math/rev/mat/fun/tcrossprod.hpp | 12 ++-- stan/math/rev/mat/fun/trace_gen_quad_form.hpp | 15 ++-- .../rev/mat/fun/trace_inv_quad_form_ldlt.hpp | 23 ++++--- stan/math/rev/mat/fun/variance.hpp | 6 +- stan/math/rev/mat/functor/adj_jac_apply.hpp | 9 ++- stan/math/rev/mat/functor/algebra_solver.hpp | 36 ++++++---- stan/math/rev/mat/functor/algebra_system.hpp | 5 +- .../rev/mat/functor/cvodes_integrator.hpp | 15 ++-- stan/math/rev/mat/functor/cvodes_ode_data.hpp | 9 ++- stan/math/rev/mat/functor/idas_integrator.hpp | 18 +++-- stan/math/rev/mat/functor/idas_system.hpp | 12 ++-- .../rev/mat/functor/map_rect_concurrent.hpp | 9 ++- stan/math/rev/mat/functor/map_rect_reduce.hpp | 12 ++-- .../rev/mat/meta/operands_and_partials.hpp | 6 +- stan/math/rev/scal/fun/binary_log_loss.hpp | 5 +- stan/math/rev/scal/fun/ceil.hpp | 3 +- stan/math/rev/scal/fun/fabs.hpp | 9 +-- stan/math/rev/scal/fun/fdim.hpp | 10 +-- stan/math/rev/scal/fun/floor.hpp | 3 +- stan/math/rev/scal/fun/fma.hpp | 10 +-- stan/math/rev/scal/fun/fmax.hpp | 18 +++-- stan/math/rev/scal/fun/fmin.hpp | 18 +++-- stan/math/rev/scal/fun/fmod.hpp | 5 +- stan/math/rev/scal/fun/gamma_p.hpp | 9 ++- stan/math/rev/scal/fun/grad_inc_beta.hpp | 3 +- stan/math/rev/scal/fun/if_else.hpp | 10 +-- stan/math/rev/scal/fun/lmgamma.hpp | 3 +- stan/math/rev/scal/fun/log_diff_exp.hpp | 10 +-- .../rev/scal/fun/log_falling_factorial.hpp | 10 +-- stan/math/rev/scal/fun/log_mix.hpp | 9 ++- stan/math/rev/scal/fun/log_sum_exp.hpp | 10 +-- stan/math/rev/scal/fun/multiply_log.hpp | 18 +++-- stan/math/rev/scal/fun/pow.hpp | 27 +++++--- stan/math/rev/scal/fun/round.hpp | 3 +- stan/math/rev/scal/fun/trunc.hpp | 3 +- test/unit/math/mix/mat/eigen_plugins_test.cpp | 24 ++++--- 474 files changed, 3669 insertions(+), 1876 deletions(-) diff --git a/make/clang-tidy b/make/clang-tidy index 512c37fd69e..e06e26caa53 100644 --- a/make/clang-tidy +++ b/make/clang-tidy @@ -21,3 +21,11 @@ clang-tidy-fix: -checks='$(tidy_checks)' -format-style='file' -header-filter='./stan/*' \ -fix -export-fixes="./.clang-fixes.log" \ -- $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) $(INC_GTEST) $(CXXFLAGS_GTEST) $(CPPFLAGS) + +.PHONY: clang-tidy-cleanup +clang-tidy-cleanup: + find $(folder) -path './test/unit/*' -wholename '*$(files).cpp' -type f | xargs -I {} clang-tidy "{}" \ + -checks='readability-braces-around-statements' \ + -format-style='file' -header-filter='./stan/*' \ + -fix -export-fixes="./.clang-fixes.log" \ + -- $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) $(INC_GTEST) $(CXXFLAGS_GTEST) $(CPPFLAGS) diff --git a/stan/math/fwd/arr/fun/log_sum_exp.hpp b/stan/math/fwd/arr/fun/log_sum_exp.hpp index 69982fb27f1..fa6e3a11fa0 100644 --- a/stan/math/fwd/arr/fun/log_sum_exp.hpp +++ b/stan/math/fwd/arr/fun/log_sum_exp.hpp @@ -12,8 +12,9 @@ template fvar log_sum_exp(const std::vector >& v) { using std::exp; std::vector vals(v.size()); - for (size_t i = 0; i < v.size(); ++i) + for (size_t i = 0; i < v.size(); ++i) { vals[i] = v[i].val_; + } T deriv(0.0); T denominator(0.0); for (size_t i = 0; i < v.size(); ++i) { diff --git a/stan/math/fwd/arr/fun/sum.hpp b/stan/math/fwd/arr/fun/sum.hpp index 4514a37ba82..eedbf61f772 100644 --- a/stan/math/fwd/arr/fun/sum.hpp +++ b/stan/math/fwd/arr/fun/sum.hpp @@ -18,8 +18,9 @@ namespace math { */ template inline fvar sum(const std::vector >& m) { - if (m.size() == 0) + if (m.size() == 0) { return 0.0; + } std::vector vals(m.size()); std::vector tans(m.size()); for (size_t i = 0; i < m.size(); ++i) { diff --git a/stan/math/fwd/arr/fun/to_fvar.hpp b/stan/math/fwd/arr/fun/to_fvar.hpp index 75d6179725c..fcfa6d88bcc 100644 --- a/stan/math/fwd/arr/fun/to_fvar.hpp +++ b/stan/math/fwd/arr/fun/to_fvar.hpp @@ -11,8 +11,9 @@ namespace math { template inline std::vector> to_fvar(const std::vector& v) { std::vector> x(v.size()); - for (size_t i = 0; i < v.size(); ++i) + for (size_t i = 0; i < v.size(); ++i) { x[i] = T(v[i]); + } return x; } @@ -20,8 +21,9 @@ template inline std::vector> to_fvar(const std::vector& v, const std::vector& d) { std::vector> x(v.size()); - for (size_t i = 0; i < v.size(); ++i) + for (size_t i = 0; i < v.size(); ++i) { x[i] = fvar(v[i], d[i]); + } return x; } diff --git a/stan/math/fwd/core/fvar.hpp b/stan/math/fwd/core/fvar.hpp index 78ea4607d4e..e2bd242e4ab 100644 --- a/stan/math/fwd/core/fvar.hpp +++ b/stan/math/fwd/core/fvar.hpp @@ -85,8 +85,9 @@ struct fvar { * @param[in] v value */ fvar(const T& v) : val_(v), d_(0.0) { // NOLINT(runtime/explicit) - if (unlikely(is_nan(v))) + if (unlikely(is_nan(v))) { d_ = v; + } } /** @@ -103,8 +104,9 @@ struct fvar { fvar(const V& v, typename std::enable_if::value>::type* dummy = 0) : val_(v), d_(0.0) { - if (unlikely(is_nan(v))) + if (unlikely(is_nan(v))) { d_ = v; + } } /** @@ -120,8 +122,9 @@ struct fvar { */ template fvar(const V& v, const D& d) : val_(v), d_(d) { - if (unlikely(is_nan(v))) + if (unlikely(is_nan(v))) { d_ = v; + } } /** diff --git a/stan/math/fwd/mat/fun/crossprod.hpp b/stan/math/fwd/mat/fun/crossprod.hpp index 39e20f65ebf..5d62efb7eaf 100644 --- a/stan/math/fwd/mat/fun/crossprod.hpp +++ b/stan/math/fwd/mat/fun/crossprod.hpp @@ -11,8 +11,9 @@ namespace math { template inline Eigen::Matrix, C, C> crossprod( const Eigen::Matrix, R, C>& m) { - if (m.rows() == 0) + if (m.rows() == 0) { return Eigen::Matrix, C, C>(0, 0); + } return multiply(transpose(m), m); } diff --git a/stan/math/fwd/mat/fun/divide.hpp b/stan/math/fwd/mat/fun/divide.hpp index 721941697ff..c53d820535a 100644 --- a/stan/math/fwd/mat/fun/divide.hpp +++ b/stan/math/fwd/mat/fun/divide.hpp @@ -12,8 +12,9 @@ inline Eigen::Matrix, R, C> divide( const Eigen::Matrix, R, C>& v, const fvar& c) { Eigen::Matrix, R, C> res(v.rows(), v.cols()); for (int i = 0; i < v.rows(); i++) { - for (int j = 0; j < v.cols(); j++) + for (int j = 0; j < v.cols(); j++) { res(i, j) = v(i, j) / c; + } } return res; } @@ -23,8 +24,9 @@ inline Eigen::Matrix, R, C> divide( const Eigen::Matrix, R, C>& v, double c) { Eigen::Matrix, R, C> res(v.rows(), v.cols()); for (int i = 0; i < v.rows(); i++) { - for (int j = 0; j < v.cols(); j++) + for (int j = 0; j < v.cols(); j++) { res(i, j) = v(i, j) / c; + } } return res; } @@ -34,8 +36,9 @@ inline Eigen::Matrix, R, C> divide(const Eigen::Matrix& v, const fvar& c) { Eigen::Matrix, R, C> res(v.rows(), v.cols()); for (int i = 0; i < v.rows(); i++) { - for (int j = 0; j < v.cols(); j++) + for (int j = 0; j < v.cols(); j++) { res(i, j) = v(i, j) / c; + } } return res; } diff --git a/stan/math/fwd/mat/fun/dot_product.hpp b/stan/math/fwd/mat/fun/dot_product.hpp index 44777f259c2..153fc18d1f2 100644 --- a/stan/math/fwd/mat/fun/dot_product.hpp +++ b/stan/math/fwd/mat/fun/dot_product.hpp @@ -18,8 +18,9 @@ inline fvar dot_product(const Eigen::Matrix, R1, C1>& v1, check_matching_sizes("dot_product", "v1", v1, "v2", v2); fvar ret(0, 0); - for (size_type i = 0; i < v1.size(); i++) + for (size_type i = 0; i < v1.size(); i++) { ret += v1(i) * v2(i); + } return ret; } @@ -31,8 +32,9 @@ inline fvar dot_product(const Eigen::Matrix, R1, C1>& v1, check_matching_sizes("dot_product", "v1", v1, "v2", v2); fvar ret(0, 0); - for (size_type i = 0; i < v1.size(); i++) + for (size_type i = 0; i < v1.size(); i++) { ret += v1(i) * v2(i); + } return ret; } @@ -44,8 +46,9 @@ inline fvar dot_product(const Eigen::Matrix& v1, check_matching_sizes("dot_product", "v1", v1, "v2", v2); fvar ret(0, 0); - for (size_type i = 0; i < v1.size(); i++) + for (size_type i = 0; i < v1.size(); i++) { ret += v1(i) * v2(i); + } return ret; } @@ -57,8 +60,9 @@ inline fvar dot_product(const Eigen::Matrix, R1, C1>& v1, check_vector("dot_product", "v2", v2); fvar ret(0, 0); - for (size_type i = 0; i < length; i++) + for (size_type i = 0; i < length; i++) { ret += v1(i) * v2(i); + } return ret; } @@ -70,8 +74,9 @@ inline fvar dot_product(const Eigen::Matrix, R1, C1>& v1, check_vector("dot_product", "v2", v2); fvar ret(0, 0); - for (size_type i = 0; i < length; i++) + for (size_type i = 0; i < length; i++) { ret += v1(i) * v2(i); + } return ret; } @@ -83,8 +88,9 @@ inline fvar dot_product(const Eigen::Matrix& v1, check_vector("dot_product", "v2", v2); fvar ret(0, 0); - for (size_type i = 0; i < length; i++) + for (size_type i = 0; i < length; i++) { ret += v1(i) * v2(i); + } return ret; } @@ -93,8 +99,9 @@ inline fvar dot_product(const std::vector >& v1, const std::vector >& v2) { check_matching_sizes("dot_product", "v1", v1, "v2", v2); fvar ret(0, 0); - for (size_t i = 0; i < v1.size(); i++) + for (size_t i = 0; i < v1.size(); i++) { ret += v1.at(i) * v2.at(i); + } return ret; } @@ -103,8 +110,9 @@ inline fvar dot_product(const std::vector& v1, const std::vector >& v2) { check_matching_sizes("dot_product", "v1", v1, "v2", v2); fvar ret(0, 0); - for (size_t i = 0; i < v1.size(); i++) + for (size_t i = 0; i < v1.size(); i++) { ret += v1.at(i) * v2.at(i); + } return ret; } @@ -113,8 +121,9 @@ inline fvar dot_product(const std::vector >& v1, const std::vector& v2) { check_matching_sizes("dot_product", "v1", v1, "v2", v2); fvar ret(0, 0); - for (size_t i = 0; i < v1.size(); i++) + for (size_t i = 0; i < v1.size(); i++) { ret += v1.at(i) * v2.at(i); + } return ret; } @@ -122,8 +131,9 @@ template inline fvar dot_product(const std::vector >& v1, const std::vector >& v2, size_type& length) { fvar ret(0, 0); - for (size_type i = 0; i < length; i++) + for (size_type i = 0; i < length; i++) { ret += v1.at(i) * v2.at(i); + } return ret; } @@ -131,8 +141,9 @@ template inline fvar dot_product(const std::vector& v1, const std::vector >& v2, size_type& length) { fvar ret(0, 0); - for (size_type i = 0; i < length; i++) + for (size_type i = 0; i < length; i++) { ret += v1.at(i) * v2.at(i); + } return ret; } @@ -140,8 +151,9 @@ template inline fvar dot_product(const std::vector >& v1, const std::vector& v2, size_type& length) { fvar ret(0, 0); - for (size_type i = 0; i < length; i++) + for (size_type i = 0; i < length; i++) { ret += v1.at(i) * v2.at(i); + } return ret; } diff --git a/stan/math/fwd/mat/fun/log_softmax.hpp b/stan/math/fwd/mat/fun/log_softmax.hpp index 398b676899e..711fa220b4e 100644 --- a/stan/math/fwd/mat/fun/log_softmax.hpp +++ b/stan/math/fwd/mat/fun/log_softmax.hpp @@ -17,8 +17,9 @@ inline Eigen::Matrix, Eigen::Dynamic, 1> log_softmax( using Eigen::Matrix; Matrix alpha_t(alpha.size()); - for (int k = 0; k < alpha.size(); ++k) + for (int k = 0; k < alpha.size(); ++k) { alpha_t(k) = alpha(k).val_; + } Matrix softmax_alpha_t = softmax(alpha_t); Matrix log_softmax_alpha_t = log_softmax(alpha_t); @@ -33,11 +34,12 @@ inline Eigen::Matrix, Eigen::Dynamic, 1> log_softmax( T negative_alpha_m_d_times_softmax_alpha_t_m = -alpha(m).d_ * softmax_alpha_t(m); for (int k = 0; k < alpha.size(); ++k) { - if (m == k) + if (m == k) { log_softmax_alpha(k).d_ += alpha(m).d_ + negative_alpha_m_d_times_softmax_alpha_t_m; - else + } else { log_softmax_alpha(k).d_ += negative_alpha_m_d_times_softmax_alpha_t_m; + } } } diff --git a/stan/math/fwd/mat/fun/mdivide_left_ldlt.hpp b/stan/math/fwd/mat/fun/mdivide_left_ldlt.hpp index b6c67f7097e..7b5b6cb1a83 100644 --- a/stan/math/fwd/mat/fun/mdivide_left_ldlt.hpp +++ b/stan/math/fwd/mat/fun/mdivide_left_ldlt.hpp @@ -26,11 +26,12 @@ inline Eigen::Matrix, R1, C2> mdivide_left_ldlt( Eigen::Matrix b_val(b.rows(), b.cols()); Eigen::Matrix b_der(b.rows(), b.cols()); - for (int i = 0; i < b.rows(); i++) + for (int i = 0; i < b.rows(); i++) { for (int j = 0; j < b.cols(); j++) { b_val(i, j) = b(i, j).val_; b_der(i, j) = b(i, j).d_; } + } return to_fvar(mdivide_left_ldlt(A, b_val), mdivide_left_ldlt(A, b_der)); } diff --git a/stan/math/fwd/mat/fun/multiply.hpp b/stan/math/fwd/mat/fun/multiply.hpp index b82ba59384b..7e3243fc183 100644 --- a/stan/math/fwd/mat/fun/multiply.hpp +++ b/stan/math/fwd/mat/fun/multiply.hpp @@ -15,8 +15,9 @@ inline Eigen::Matrix, R1, C1> multiply( const Eigen::Matrix, R1, C1>& m, const fvar& c) { Eigen::Matrix, R1, C1> res(m.rows(), m.cols()); for (int i = 0; i < m.rows(); i++) { - for (int j = 0; j < m.cols(); j++) + for (int j = 0; j < m.cols(); j++) { res(i, j) = c * m(i, j); + } } return res; } @@ -26,8 +27,9 @@ inline Eigen::Matrix, R2, C2> multiply( const Eigen::Matrix, R2, C2>& m, double c) { Eigen::Matrix, R2, C2> res(m.rows(), m.cols()); for (int i = 0; i < m.rows(); i++) { - for (int j = 0; j < m.cols(); j++) + for (int j = 0; j < m.cols(); j++) { res(i, j) = c * m(i, j); + } } return res; } @@ -37,8 +39,9 @@ inline Eigen::Matrix, R1, C1> multiply( const Eigen::Matrix& m, const fvar& c) { Eigen::Matrix, R1, C1> res(m.rows(), m.cols()); for (int i = 0; i < m.rows(); i++) { - for (int j = 0; j < m.cols(); j++) + for (int j = 0; j < m.cols(); j++) { res(i, j) = c * m(i, j); + } } return res; } diff --git a/stan/math/fwd/mat/fun/multiply_lower_tri_self_transpose.hpp b/stan/math/fwd/mat/fun/multiply_lower_tri_self_transpose.hpp index 6388a4831f4..8b2a00e64ec 100644 --- a/stan/math/fwd/mat/fun/multiply_lower_tri_self_transpose.hpp +++ b/stan/math/fwd/mat/fun/multiply_lower_tri_self_transpose.hpp @@ -12,14 +12,16 @@ namespace math { template inline Eigen::Matrix, R, R> multiply_lower_tri_self_transpose( const Eigen::Matrix, R, C>& m) { - if (m.rows() == 0) + if (m.rows() == 0) { return Eigen::Matrix, R, R>(0, 0); + } Eigen::Matrix, R, C> L(m.rows(), m.cols()); L.setZero(); for (size_type i = 0; i < m.rows(); i++) { - for (size_type j = 0; (j < i + 1) && (j < m.cols()); j++) + for (size_type j = 0; (j < i + 1) && (j < m.cols()); j++) { L(i, j) = m(i, j); + } } return multiply(L, transpose(L)); } diff --git a/stan/math/fwd/mat/fun/qr_Q.hpp b/stan/math/fwd/mat/fun/qr_Q.hpp index 25f8c896ca6..d3b1fdf8375 100644 --- a/stan/math/fwd/mat/fun/qr_Q.hpp +++ b/stan/math/fwd/mat/fun/qr_Q.hpp @@ -18,9 +18,11 @@ Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> qr_Q( Eigen::HouseholderQR qr(m.rows(), m.cols()); qr.compute(m); matrix_fwd_t Q = qr.householderQ(); - for (int i = 0; i < m.cols(); i++) - if (qr.matrixQR()(i, i) < 0.0) + for (int i = 0; i < m.cols(); i++) { + if (qr.matrixQR()(i, i) < 0.0) { Q.col(i) *= -1.0; + } + } return Q; } diff --git a/stan/math/fwd/mat/fun/qr_R.hpp b/stan/math/fwd/mat/fun/qr_R.hpp index 873891aef20..c408624cda7 100644 --- a/stan/math/fwd/mat/fun/qr_R.hpp +++ b/stan/math/fwd/mat/fun/qr_R.hpp @@ -19,10 +19,12 @@ Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> qr_R( qr.compute(m); matrix_fwd_t R = qr.matrixQR().topLeftCorner(m.rows(), m.cols()); for (int i = 0; i < R.rows(); i++) { - for (int j = 0; j < i; j++) + for (int j = 0; j < i; j++) { R(i, j) = 0.0; - if (i < R.cols() && R(i, i) < 0.0) + } + if (i < R.cols() && R(i, i) < 0.0) { R.row(i) *= -1.0; + } } return R; } diff --git a/stan/math/fwd/mat/fun/softmax.hpp b/stan/math/fwd/mat/fun/softmax.hpp index 5db7ab0b44f..79daa4c5fda 100644 --- a/stan/math/fwd/mat/fun/softmax.hpp +++ b/stan/math/fwd/mat/fun/softmax.hpp @@ -15,8 +15,9 @@ inline Eigen::Matrix, Eigen::Dynamic, 1> softmax( using Eigen::Matrix; Matrix alpha_t(alpha.size()); - for (int k = 0; k < alpha.size(); ++k) + for (int k = 0; k < alpha.size(); ++k) { alpha_t(k) = alpha(k).val_; + } Matrix softmax_alpha_t = softmax(alpha_t); diff --git a/stan/math/fwd/mat/fun/sum.hpp b/stan/math/fwd/mat/fun/sum.hpp index ccb21a7ee77..9025972664a 100644 --- a/stan/math/fwd/mat/fun/sum.hpp +++ b/stan/math/fwd/mat/fun/sum.hpp @@ -19,8 +19,9 @@ namespace math { */ template inline fvar sum(const Eigen::Matrix, R, C>& m) { - if (m.size() == 0) + if (m.size() == 0) { return 0.0; + } Eigen::Matrix vals(m.size()); Eigen::Matrix tans(m.size()); for (int i = 0; i < m.size(); ++i) { diff --git a/stan/math/fwd/mat/fun/tcrossprod.hpp b/stan/math/fwd/mat/fun/tcrossprod.hpp index 8a347f5cbbe..7e079cadc47 100644 --- a/stan/math/fwd/mat/fun/tcrossprod.hpp +++ b/stan/math/fwd/mat/fun/tcrossprod.hpp @@ -11,8 +11,9 @@ namespace math { template inline Eigen::Matrix, R, R> tcrossprod( const Eigen::Matrix, R, C>& m) { - if (m.rows() == 0) + if (m.rows() == 0) { return Eigen::Matrix, R, R>(0, 0); + } return multiply(m, transpose(m)); } diff --git a/stan/math/fwd/mat/fun/to_fvar.hpp b/stan/math/fwd/mat/fun/to_fvar.hpp index afa0de5a6f6..de25628bb75 100644 --- a/stan/math/fwd/mat/fun/to_fvar.hpp +++ b/stan/math/fwd/mat/fun/to_fvar.hpp @@ -37,8 +37,9 @@ template inline Eigen::Matrix, R, C> to_fvar( const Eigen::Matrix& m) { Eigen::Matrix, R, C> m_fd(m.rows(), m.cols()); - for (int i = 0; i < m.size(); ++i) + for (int i = 0; i < m.size(); ++i) { m_fd(i) = m(i); + } return m_fd; } diff --git a/stan/math/fwd/mat/fun/unit_vector_constrain.hpp b/stan/math/fwd/mat/fun/unit_vector_constrain.hpp index 9c5a6a53824..e8ee586b6e8 100644 --- a/stan/math/fwd/mat/fun/unit_vector_constrain.hpp +++ b/stan/math/fwd/mat/fun/unit_vector_constrain.hpp @@ -22,13 +22,15 @@ inline Eigen::Matrix, R, C> unit_vector_constrain( using std::sqrt; Matrix y_t(y.size()); - for (int k = 0; k < y.size(); ++k) + for (int k = 0; k < y.size(); ++k) { y_t.coeffRef(k) = y.coeff(k).val_; + } Matrix unit_vector_y_t = unit_vector_constrain(y_t); Matrix, R, C> unit_vector_y(y.size()); - for (int k = 0; k < y.size(); ++k) + for (int k = 0; k < y.size(); ++k) { unit_vector_y.coeffRef(k).val_ = unit_vector_y_t.coeff(k); + } T squared_norm = dot_self(y_t); T norm = sqrt(squared_norm); @@ -38,8 +40,9 @@ inline Eigen::Matrix, R, C> unit_vector_constrain( for (int m = 0; m < y.size(); ++m) { J.coeffRef(m, m) += inv_norm; - for (int k = 0; k < y.size(); ++k) + for (int k = 0; k < y.size(); ++k) { unit_vector_y.coeffRef(k).d_ = J.coeff(k, m); + } } return unit_vector_y; } diff --git a/stan/math/fwd/mat/functor/gradient.hpp b/stan/math/fwd/mat/functor/gradient.hpp index c1f84155d57..608254c2436 100644 --- a/stan/math/fwd/mat/functor/gradient.hpp +++ b/stan/math/fwd/mat/functor/gradient.hpp @@ -41,11 +41,13 @@ void gradient(const F& f, const Eigen::Matrix& x, T& fx, Eigen::Matrix, Eigen::Dynamic, 1> x_fvar(x.size()); grad_fx.resize(x.size()); for (int i = 0; i < x.size(); ++i) { - for (int k = 0; k < x.size(); ++k) + for (int k = 0; k < x.size(); ++k) { x_fvar(k) = fvar(x(k), k == i); + } fvar fx_fvar = f(x_fvar); - if (i == 0) + if (i == 0) { fx = fx_fvar.val_; + } grad_fx(i) = fx_fvar.d_; } } diff --git a/stan/math/fwd/mat/functor/hessian.hpp b/stan/math/fwd/mat/functor/hessian.hpp index cea6db21c3b..c7b3a5d3907 100644 --- a/stan/math/fwd/mat/functor/hessian.hpp +++ b/stan/math/fwd/mat/functor/hessian.hpp @@ -51,13 +51,16 @@ void hessian(const F& f, const Eigen::Matrix& x, T& fx, Eigen::Matrix >, Eigen::Dynamic, 1> x_fvar(x.size()); for (int i = 0; i < x.size(); ++i) { for (int j = i; j < x.size(); ++j) { - for (int k = 0; k < x.size(); ++k) + for (int k = 0; k < x.size(); ++k) { x_fvar(k) = fvar >(fvar(x(k), j == k), fvar(i == k, 0)); + } fvar > fx_fvar = f(x_fvar); - if (j == 0) + if (j == 0) { fx = fx_fvar.val_.val_; - if (i == j) + } + if (i == j) { grad(i) = fx_fvar.d_.val_; + } H(i, j) = fx_fvar.d_.d_; H(j, i) = H(i, j); } diff --git a/stan/math/fwd/scal/fun/abs.hpp b/stan/math/fwd/scal/fun/abs.hpp index b8ad3bd5690..72dadf9553b 100644 --- a/stan/math/fwd/scal/fun/abs.hpp +++ b/stan/math/fwd/scal/fun/abs.hpp @@ -12,14 +12,15 @@ namespace math { template inline fvar abs(const fvar& x) { - if (x.val_ > 0.0) + if (x.val_ > 0.0) { return x; - else if (x.val_ < 0.0) + } else if (x.val_ < 0.0) { return fvar(-x.val_, -x.d_); - else if (x.val_ == 0.0) + } else if (x.val_ == 0.0) { return fvar(0, 0); - else + } else { return fvar(abs(x.val_), NOT_A_NUMBER); + } } } // namespace math diff --git a/stan/math/fwd/scal/fun/binary_log_loss.hpp b/stan/math/fwd/scal/fun/binary_log_loss.hpp index ef6015e24ff..9c1924602b3 100644 --- a/stan/math/fwd/scal/fun/binary_log_loss.hpp +++ b/stan/math/fwd/scal/fun/binary_log_loss.hpp @@ -10,11 +10,12 @@ namespace math { template inline fvar binary_log_loss(int y, const fvar& y_hat) { - if (y) + if (y) { return fvar(binary_log_loss(y, y_hat.val_), -y_hat.d_ / y_hat.val_); - else + } else { return fvar(binary_log_loss(y, y_hat.val_), y_hat.d_ / (1.0 - y_hat.val_)); + } } } // namespace math } // namespace stan diff --git a/stan/math/fwd/scal/fun/fabs.hpp b/stan/math/fwd/scal/fun/fabs.hpp index e5b2a63030a..09347509872 100644 --- a/stan/math/fwd/scal/fun/fabs.hpp +++ b/stan/math/fwd/scal/fun/fabs.hpp @@ -15,14 +15,15 @@ template inline fvar fabs(const fvar& x) { using std::fabs; - if (unlikely(is_nan(value_of(x.val_)))) + if (unlikely(is_nan(value_of(x.val_)))) { return fvar(fabs(x.val_), NOT_A_NUMBER); - else if (x.val_ > 0.0) + } else if (x.val_ > 0.0) { return x; - else if (x.val_ < 0.0) + } else if (x.val_ < 0.0) { return fvar(-x.val_, -x.d_); - else + } else { return fvar(0, 0); + } } } // namespace math diff --git a/stan/math/fwd/scal/fun/fdim.hpp b/stan/math/fwd/scal/fun/fdim.hpp index ab59f91aa7c..6a26aef9c9e 100644 --- a/stan/math/fwd/scal/fun/fdim.hpp +++ b/stan/math/fwd/scal/fun/fdim.hpp @@ -19,10 +19,11 @@ namespace math { */ template inline fvar fdim(const fvar& x, const fvar& y) { - if (x.val_ < y.val_) + if (x.val_ < y.val_) { return fvar(fdim(x.val_, y.val_), 0); - else + } else { return fvar(fdim(x.val_, y.val_), x.d_ - y.d_); + } } /** @@ -36,10 +37,11 @@ inline fvar fdim(const fvar& x, const fvar& y) { */ template inline fvar fdim(const fvar& x, double y) { - if (x.val_ < y) + if (x.val_ < y) { return fvar(fdim(x.val_, y), 0); - else + } else { return fvar(fdim(x.val_, y), x.d_); + } } /** @@ -53,10 +55,11 @@ inline fvar fdim(const fvar& x, double y) { */ template inline fvar fdim(double x, const fvar& y) { - if (x < y.val_) + if (x < y.val_) { return fvar(fdim(x, y.val_), 0); - else + } else { return fvar(fdim(x, y.val_), -y.d_); + } } } // namespace math diff --git a/stan/math/fwd/scal/fun/fmax.hpp b/stan/math/fwd/scal/fun/fmax.hpp index ec0164ce8a4..4b7941dd8d4 100644 --- a/stan/math/fwd/scal/fun/fmax.hpp +++ b/stan/math/fwd/scal/fun/fmax.hpp @@ -21,10 +21,11 @@ namespace math { template inline fvar fmax(const fvar& x1, const fvar& x2) { if (unlikely(is_nan(x1.val_))) { - if (is_nan(x2.val_)) + if (is_nan(x2.val_)) { return fvar(fmax(x1.val_, x2.val_), NOT_A_NUMBER); - else + } else { return fvar(x2.val_, x2.d_); + } } else if (unlikely(is_nan(x2.val_))) { return fvar(x1.val_, x1.d_); } else if (x1.val_ > x2.val_) { @@ -47,10 +48,11 @@ inline fvar fmax(const fvar& x1, const fvar& x2) { template inline fvar fmax(double x1, const fvar& x2) { if (unlikely(is_nan(x1))) { - if (is_nan(x2.val_)) + if (is_nan(x2.val_)) { return fvar(fmax(x1, x2.val_), NOT_A_NUMBER); - else + } else { return fvar(x2.val_, x2.d_); + } } else if (unlikely(is_nan(x2.val_))) { return fvar(x1, 0.0); } else if (x1 > x2.val_) { @@ -73,10 +75,11 @@ inline fvar fmax(double x1, const fvar& x2) { template inline fvar fmax(const fvar& x1, double x2) { if (unlikely(is_nan(x1.val_))) { - if (is_nan(x2)) + if (is_nan(x2)) { return fvar(fmax(x1.val_, x2), NOT_A_NUMBER); - else + } else { return fvar(x2, 0.0); + } } else if (unlikely(is_nan(x2))) { return fvar(x1.val_, x1.d_); } else if (x1.val_ > x2) { diff --git a/stan/math/fwd/scal/fun/fmin.hpp b/stan/math/fwd/scal/fun/fmin.hpp index ec115a1980e..4f2efd90cda 100644 --- a/stan/math/fwd/scal/fun/fmin.hpp +++ b/stan/math/fwd/scal/fun/fmin.hpp @@ -13,10 +13,11 @@ namespace math { template inline fvar fmin(const fvar& x1, const fvar& x2) { if (unlikely(is_nan(x1.val_))) { - if (is_nan(x2.val_)) + if (is_nan(x2.val_)) { return fvar(fmin(x1.val_, x2.val_), NOT_A_NUMBER); - else + } else { return fvar(x2.val_, x2.d_); + } } else if (unlikely(is_nan(x2.val_))) { return fvar(x1.val_, x1.d_); } else if (x1.val_ < x2.val_) { @@ -31,10 +32,11 @@ inline fvar fmin(const fvar& x1, const fvar& x2) { template inline fvar fmin(double x1, const fvar& x2) { if (unlikely(is_nan(x1))) { - if (is_nan(x2.val_)) + if (is_nan(x2.val_)) { return fvar(fmin(x1, x2.val_), NOT_A_NUMBER); - else + } else { return fvar(x2.val_, x2.d_); + } } else if (unlikely(is_nan(x2.val_))) { return fvar(x1, 0.0); } else if (x1 < x2.val_) { @@ -49,10 +51,11 @@ inline fvar fmin(double x1, const fvar& x2) { template inline fvar fmin(const fvar& x1, double x2) { if (unlikely(is_nan(x1.val_))) { - if (is_nan(x2)) + if (is_nan(x2)) { return fvar(fmin(x1.val_, x2), NOT_A_NUMBER); - else + } else { return fvar(x2, 0.0); + } } else if (unlikely(is_nan(x2))) { return fvar(x1.val_, x1.d_); } else if (x1.val_ < x2) { diff --git a/stan/math/fwd/scal/fun/fmod.hpp b/stan/math/fwd/scal/fun/fmod.hpp index 7aab65e9829..5ffb731b717 100644 --- a/stan/math/fwd/scal/fun/fmod.hpp +++ b/stan/math/fwd/scal/fun/fmod.hpp @@ -21,10 +21,11 @@ inline fvar fmod(const fvar& x1, const fvar& x2) { template inline fvar fmod(const fvar& x1, double x2) { using std::fmod; - if (unlikely(is_any_nan(value_of(x1.val_), x2))) + if (unlikely(is_any_nan(value_of(x1.val_), x2))) { return fvar(fmod(x1.val_, x2), NOT_A_NUMBER); - else + } else { return fvar(fmod(x1.val_, x2), x1.d_ / x2); + } } template diff --git a/stan/math/fwd/scal/fun/gamma_p.hpp b/stan/math/fwd/scal/fun/gamma_p.hpp index 28013017bf1..2ba6a67c99d 100644 --- a/stan/math/fwd/scal/fun/gamma_p.hpp +++ b/stan/math/fwd/scal/fun/gamma_p.hpp @@ -17,10 +17,12 @@ inline fvar gamma_p(const fvar &x1, const fvar &x2) { using std::log; T u = gamma_p(x1.val_, x2.val_); - if (is_inf(x1.val_)) + if (is_inf(x1.val_)) { return fvar(u, std::numeric_limits::quiet_NaN()); - if (is_inf(x2.val_)) + } + if (is_inf(x2.val_)) { return fvar(u, std::numeric_limits::quiet_NaN()); + } T der1 = grad_reg_lower_inc_gamma(x1.val_, x2.val_, 1.0e-10); T der2 = exp(-x2.val_ + (x1.val_ - 1.0) * log(x2.val_) - lgamma(x1.val_)); @@ -31,10 +33,12 @@ inline fvar gamma_p(const fvar &x1, const fvar &x2) { template inline fvar gamma_p(const fvar &x1, double x2) { T u = gamma_p(x1.val_, x2); - if (is_inf(x1.val_)) + if (is_inf(x1.val_)) { return fvar(u, std::numeric_limits::quiet_NaN()); - if (is_inf(x2)) + } + if (is_inf(x2)) { return fvar(u, std::numeric_limits::quiet_NaN()); + } T der1 = grad_reg_lower_inc_gamma(x1.val_, x2, 1.0e-10); @@ -47,8 +51,9 @@ inline fvar gamma_p(double x1, const fvar &x2) { using std::log; T u = gamma_p(x1, x2.val_); - if (is_inf(x1)) + if (is_inf(x1)) { return fvar(u, std::numeric_limits::quiet_NaN()); + } T der2 = exp(-x2.val_ + (x1 - 1.0) * log(x2.val_) - lgamma(x1)); diff --git a/stan/math/fwd/scal/fun/grad_inc_beta.hpp b/stan/math/fwd/scal/fun/grad_inc_beta.hpp index 3ec57a57481..8452aab0bcb 100644 --- a/stan/math/fwd/scal/fun/grad_inc_beta.hpp +++ b/stan/math/fwd/scal/fun/grad_inc_beta.hpp @@ -41,8 +41,9 @@ void grad_inc_beta(fvar& g1, fvar& g2, fvar a, fvar b, fvar z) { fvar dF1 = 0; fvar dF2 = 0; - if (value_of(value_of(C))) + if (value_of(value_of(C))) { grad_2F1(dF1, dF2, a + b, fvar(1.0), a + 1, z); + } g1 = (c1 - 1.0 / a) * c3 + C * (dF1 + dF2); g2 = c2 * c3 + C * dF1; diff --git a/stan/math/fwd/scal/fun/lmgamma.hpp b/stan/math/fwd/scal/fun/lmgamma.hpp index 81076734cde..efa846055ad 100644 --- a/stan/math/fwd/scal/fun/lmgamma.hpp +++ b/stan/math/fwd/scal/fun/lmgamma.hpp @@ -14,8 +14,9 @@ inline fvar::type> lmgamma( int x1, const fvar& x2) { using std::log; T deriv = 0; - for (int count = 1; count < x1 + 1; count++) + for (int count = 1; count < x1 + 1; count++) { deriv += x2.d_ * digamma(x2.val_ + (1.0 - count) / 2.0); + } return fvar::type>(lmgamma(x1, x2.val_), deriv); } diff --git a/stan/math/fwd/scal/fun/log.hpp b/stan/math/fwd/scal/fun/log.hpp index a36c94d1f9d..2ee0983f8cd 100644 --- a/stan/math/fwd/scal/fun/log.hpp +++ b/stan/math/fwd/scal/fun/log.hpp @@ -11,10 +11,11 @@ namespace math { template inline fvar log(const fvar& x) { using std::log; - if (x.val_ < 0.0) + if (x.val_ < 0.0) { return fvar(NOT_A_NUMBER, NOT_A_NUMBER); - else + } else { return fvar(log(x.val_), x.d_ / x.val_); + } } } // namespace math } // namespace stan diff --git a/stan/math/fwd/scal/fun/log10.hpp b/stan/math/fwd/scal/fun/log10.hpp index 58e4bb9e702..ec8cab8e0cb 100644 --- a/stan/math/fwd/scal/fun/log10.hpp +++ b/stan/math/fwd/scal/fun/log10.hpp @@ -12,10 +12,11 @@ template inline fvar log10(const fvar& x) { using std::log; using std::log10; - if (x.val_ < 0.0) + if (x.val_ < 0.0) { return fvar(NOT_A_NUMBER, NOT_A_NUMBER); - else + } else { return fvar(log10(x.val_), x.d_ / (x.val_ * LOG_10)); + } } } // namespace math diff --git a/stan/math/fwd/scal/fun/log1m_exp.hpp b/stan/math/fwd/scal/fun/log1m_exp.hpp index 1231286dc99..6d4fb7e7b81 100644 --- a/stan/math/fwd/scal/fun/log1m_exp.hpp +++ b/stan/math/fwd/scal/fun/log1m_exp.hpp @@ -21,8 +21,9 @@ namespace math { */ template inline fvar log1m_exp(const fvar& x) { - if (x.val_ >= 0) + if (x.val_ >= 0) { return fvar(NOT_A_NUMBER); + } return fvar(log1m_exp(x.val_), x.d_ / -expm1(-x.val_)); } diff --git a/stan/math/fwd/scal/fun/log2.hpp b/stan/math/fwd/scal/fun/log2.hpp index ac1c8a82bd7..9294b13a19f 100644 --- a/stan/math/fwd/scal/fun/log2.hpp +++ b/stan/math/fwd/scal/fun/log2.hpp @@ -18,10 +18,11 @@ namespace math { */ template inline fvar log2(const fvar& x) { - if (x.val_ < 0.0) + if (x.val_ < 0.0) { return fvar(NOT_A_NUMBER, NOT_A_NUMBER); - else + } else { return fvar(log2(x.val_), x.d_ / (x.val_ * LOG_2)); + } } } // namespace math diff --git a/stan/math/fwd/scal/fun/log_diff_exp.hpp b/stan/math/fwd/scal/fun/log_diff_exp.hpp index 4a428db9ce8..9afb604126c 100644 --- a/stan/math/fwd/scal/fun/log_diff_exp.hpp +++ b/stan/math/fwd/scal/fun/log_diff_exp.hpp @@ -13,8 +13,9 @@ namespace math { template inline fvar log_diff_exp(const fvar& x1, const fvar& x2) { if (x1.val_ <= x2.val_) { - if (x1.val_ < INFTY && x1.val_ == x2.val_) + if (x1.val_ < INFTY && x1.val_ == x2.val_) { return fvar(NEGATIVE_INFTY, NOT_A_NUMBER); + } return fvar(NOT_A_NUMBER, NOT_A_NUMBER); } return fvar( @@ -25,8 +26,9 @@ inline fvar log_diff_exp(const fvar& x1, const fvar& x2) { template inline fvar log_diff_exp(const T1& x1, const fvar& x2) { if (x1 <= x2.val_) { - if (x1 < INFTY && x1 == x2.val_) + if (x1 < INFTY && x1 == x2.val_) { return fvar(NEGATIVE_INFTY, x2.d_ * NEGATIVE_INFTY); + } return fvar(NOT_A_NUMBER, NOT_A_NUMBER); } return fvar(log_diff_exp(x1, x2.val_), -x2.d_ / expm1(x1 - x2.val_)); @@ -36,8 +38,9 @@ template inline fvar log_diff_exp(const fvar& x1, const T2& x2) { if (x1.val_ <= x2) { if (x1.val_ < INFTY && x1.val_ == x2) { - if (x2 == NEGATIVE_INFTY) + if (x2 == NEGATIVE_INFTY) { return fvar(NEGATIVE_INFTY, x1.d_); + } return fvar(NEGATIVE_INFTY, x1.d_ * INFTY); } return fvar(NOT_A_NUMBER, NOT_A_NUMBER); diff --git a/stan/math/fwd/scal/fun/log_sum_exp.hpp b/stan/math/fwd/scal/fun/log_sum_exp.hpp index f1e0ab40d73..e9dea3b7789 100644 --- a/stan/math/fwd/scal/fun/log_sum_exp.hpp +++ b/stan/math/fwd/scal/fun/log_sum_exp.hpp @@ -20,16 +20,18 @@ inline fvar log_sum_exp(const fvar& x1, const fvar& x2) { template inline fvar log_sum_exp(double x1, const fvar& x2) { using std::exp; - if (x1 == NEGATIVE_INFTY) + if (x1 == NEGATIVE_INFTY) { return fvar(x2.val_, x2.d_); + } return fvar(log_sum_exp(x1, x2.val_), x2.d_ / (exp(x1 - x2.val_) + 1)); } template inline fvar log_sum_exp(const fvar& x1, double x2) { using std::exp; - if (x2 == NEGATIVE_INFTY) + if (x2 == NEGATIVE_INFTY) { return fvar(x1.val_, x1.d_); + } return fvar(log_sum_exp(x1.val_, x2), x1.d_ / (1 + exp(x2 - x1.val_))); } diff --git a/stan/math/fwd/scal/fun/logit.hpp b/stan/math/fwd/scal/fun/logit.hpp index 6a1553daf87..08a1fb8ed5f 100644 --- a/stan/math/fwd/scal/fun/logit.hpp +++ b/stan/math/fwd/scal/fun/logit.hpp @@ -12,10 +12,11 @@ namespace math { template inline fvar logit(const fvar& x) { - if (x.val_ > 1 || x.val_ < 0) + if (x.val_ > 1 || x.val_ < 0) { return fvar(NOT_A_NUMBER, NOT_A_NUMBER); - else + } else { return fvar(logit(x.val_), x.d_ / (x.val_ - square(x.val_))); + } } } // namespace math } // namespace stan diff --git a/stan/math/fwd/scal/fun/pow.hpp b/stan/math/fwd/scal/fun/pow.hpp index 8290a3a3c06..b413f8f404a 100644 --- a/stan/math/fwd/scal/fun/pow.hpp +++ b/stan/math/fwd/scal/fun/pow.hpp @@ -34,18 +34,24 @@ inline fvar pow(const fvar& x1, double x2) { using std::pow; using std::sqrt; - if (x2 == -2) + if (x2 == -2) { return inv_square(x1); - if (x2 == -1) + } + if (x2 == -1) { return inv(x1); - if (x2 == -0.5) + } + if (x2 == -0.5) { return inv_sqrt(x1); - if (x2 == 0.5) + } + if (x2 == 0.5) { return sqrt(x1); - if (x2 == 1.0) + } + if (x2 == 1.0) { return x1; - if (x2 == 2.0) + } + if (x2 == 2.0) { return square(x1); + } return fvar(pow(x1.val_, x2), x1.d_ * x2 * pow(x1.val_, x2 - 1)); } diff --git a/stan/math/memory/stack_alloc.hpp b/stan/math/memory/stack_alloc.hpp index d832ff3915a..97b1f721145 100644 --- a/stan/math/memory/stack_alloc.hpp +++ b/stan/math/memory/stack_alloc.hpp @@ -37,8 +37,9 @@ const size_t DEFAULT_INITIAL_NBYTES = 1 << 16; // 64KB // big fun to inline, but only called twice inline char* eight_byte_aligned_malloc(size_t size) { char* ptr = static_cast(malloc(size)); - if (!ptr) + if (!ptr) { return ptr; // malloc failed to alloc + } if (!is_aligned(ptr, 8U)) { std::stringstream s; s << "invalid alignment to 8 bytes, ptr=" @@ -94,17 +95,20 @@ class stack_alloc { char* result; ++cur_block_; // Find the next block (if any) containing at least len bytes. - while ((cur_block_ < blocks_.size()) && (sizes_[cur_block_] < len)) + while ((cur_block_ < blocks_.size()) && (sizes_[cur_block_] < len)) { ++cur_block_; + } // Allocate a new block if necessary. if (unlikely(cur_block_ >= blocks_.size())) { // New block should be max(2*size of last block, len) bytes. size_t newsize = sizes_.back() * 2; - if (newsize < len) + if (newsize < len) { newsize = len; + } blocks_.push_back(internal::eight_byte_aligned_malloc(newsize)); - if (!blocks_.back()) + if (!blocks_.back()) { throw std::bad_alloc(); + } sizes_.push_back(newsize); } result = blocks_[cur_block_]; @@ -130,8 +134,9 @@ class stack_alloc { cur_block_(0), cur_block_end_(blocks_[0] + initial_nbytes), next_loc_(blocks_[0]) { - if (!blocks_[0]) + if (!blocks_[0]) { throw std::bad_alloc(); // no msg allowed in bad_alloc ctor + } } /** @@ -142,9 +147,11 @@ class stack_alloc { */ ~stack_alloc() { // free ALL blocks - for (auto& block : blocks_) - if (block) + for (auto& block : blocks_) { + if (block) { free(block); + } + } } /** @@ -164,8 +171,9 @@ class stack_alloc { char* result = next_loc_; next_loc_ += len; // Occasionally, we have to switch blocks. - if (unlikely(next_loc_ >= cur_block_end_)) + if (unlikely(next_loc_ >= cur_block_end_)) { result = move_to_next_block(len); + } return reinterpret_cast(result); } @@ -208,8 +216,9 @@ class stack_alloc { * recover memory back to the last start_nested call. */ inline void recover_nested() { - if (unlikely(nested_cur_blocks_.empty())) + if (unlikely(nested_cur_blocks_.empty())) { recover_all(); + } cur_block_ = nested_cur_blocks_.back(); nested_cur_blocks_.pop_back(); @@ -228,9 +237,11 @@ class stack_alloc { */ inline void free_all() { // frees all BUT the first (index 0) block - for (size_t i = 1; i < blocks_.size(); ++i) - if (blocks_[i]) + for (size_t i = 1; i < blocks_.size(); ++i) { + if (blocks_[i]) { free(blocks_[i]); + } + } sizes_.resize(1); blocks_.resize(1); recover_all(); @@ -263,11 +274,14 @@ class stack_alloc { * false otherwise. */ inline bool in_stack(const void* ptr) const { - for (size_t i = 0; i < cur_block_; ++i) - if (ptr >= blocks_[i] && ptr < blocks_[i] + sizes_[i]) + for (size_t i = 0; i < cur_block_; ++i) { + if (ptr >= blocks_[i] && ptr < blocks_[i] + sizes_[i]) { return true; - if (ptr >= blocks_[cur_block_] && ptr < next_loc_) + } + } + if (ptr >= blocks_[cur_block_] && ptr < next_loc_) { return true; + } return false; } }; diff --git a/stan/math/mix/mat/functor/grad_hessian.hpp b/stan/math/mix/mat/functor/grad_hessian.hpp index 813b50fa419..328fb5eecf1 100644 --- a/stan/math/mix/mat/functor/grad_hessian.hpp +++ b/stan/math/mix/mat/functor/grad_hessian.hpp @@ -55,9 +55,10 @@ void grad_hessian( for (int j = i; j < d; ++j) { start_nested(); Matrix >, Dynamic, 1> x_ffvar(d); - for (int k = 0; k < d; ++k) + for (int k = 0; k < d; ++k) { x_ffvar(k) = fvar >(fvar(x(k), i == k), fvar(j == k, 0)); + } fvar > fx_ffvar = f(x_ffvar); H(i, j) = fx_ffvar.d_.d_.val(); H(j, i) = H(i, j); diff --git a/stan/math/mix/mat/functor/grad_tr_mat_times_hessian.hpp b/stan/math/mix/mat/functor/grad_tr_mat_times_hessian.hpp index 109a69e9370..79fe4b4a497 100644 --- a/stan/math/mix/mat/functor/grad_tr_mat_times_hessian.hpp +++ b/stan/math/mix/mat/functor/grad_tr_mat_times_hessian.hpp @@ -23,18 +23,21 @@ void grad_tr_mat_times_hessian( grad_tr_MH.resize(x.size()); Matrix x_var(x.size()); - for (int i = 0; i < x.size(); ++i) + for (int i = 0; i < x.size(); ++i) { x_var(i) = x(i); + } Matrix, Dynamic, 1> x_fvar(x.size()); var sum(0.0); Matrix M_n(x.size()); for (int n = 0; n < x.size(); ++n) { - for (int k = 0; k < x.size(); ++k) + for (int k = 0; k < x.size(); ++k) { M_n(k) = M(n, k); - for (int k = 0; k < x.size(); ++k) + } + for (int k = 0; k < x.size(); ++k) { x_fvar(k) = fvar(x_var(k), k == n); + } fvar fx; fvar grad_fx_dot_v; gradient_dot_vector, double>(f, x_fvar, M_n, fx, grad_fx_dot_v); @@ -42,8 +45,9 @@ void grad_tr_mat_times_hessian( } grad(sum.vi_); - for (int i = 0; i < x.size(); ++i) + for (int i = 0; i < x.size(); ++i) { grad_tr_MH(i) = x_var(i).adj(); + } } catch (const std::exception& e) { recover_memory_nested(); throw; diff --git a/stan/math/mix/mat/functor/gradient_dot_vector.hpp b/stan/math/mix/mat/functor/gradient_dot_vector.hpp index d6db5e14de1..f25d257f15b 100644 --- a/stan/math/mix/mat/functor/gradient_dot_vector.hpp +++ b/stan/math/mix/mat/functor/gradient_dot_vector.hpp @@ -16,8 +16,9 @@ void gradient_dot_vector(const F& f, T1& grad_fx_dot_v) { using Eigen::Matrix; Matrix, Eigen::Dynamic, 1> x_fvar(x.size()); - for (int i = 0; i < x.size(); ++i) + for (int i = 0; i < x.size(); ++i) { x_fvar(i) = fvar(x(i), v(i)); + } fvar fx_fvar = f(x_fvar); fx = fx_fvar.val_; grad_fx_dot_v = fx_fvar.d_; diff --git a/stan/math/mix/mat/functor/hessian.hpp b/stan/math/mix/mat/functor/hessian.hpp index e8fbe4d79c7..951899ad276 100644 --- a/stan/math/mix/mat/functor/hessian.hpp +++ b/stan/math/mix/mat/functor/hessian.hpp @@ -54,15 +54,18 @@ void hessian(const F& f, const Eigen::Matrix& x, for (int i = 0; i < x.size(); ++i) { start_nested(); Eigen::Matrix, Eigen::Dynamic, 1> x_fvar(x.size()); - for (int j = 0; j < x.size(); ++j) + for (int j = 0; j < x.size(); ++j) { x_fvar(j) = fvar(x(j), i == j); + } fvar fx_fvar = f(x_fvar); grad(i) = fx_fvar.d_.val(); - if (i == 0) + if (i == 0) { fx = fx_fvar.val_.val(); + } stan::math::grad(fx_fvar.d_.vi_); - for (int j = 0; j < x.size(); ++j) + for (int j = 0; j < x.size(); ++j) { H(i, j) = x_fvar(j).val_.adj(); + } recover_memory_nested(); } } catch (const std::exception& e) { diff --git a/stan/math/mix/mat/functor/hessian_times_vector.hpp b/stan/math/mix/mat/functor/hessian_times_vector.hpp index abe915292f7..a6d3546103a 100644 --- a/stan/math/mix/mat/functor/hessian_times_vector.hpp +++ b/stan/math/mix/mat/functor/hessian_times_vector.hpp @@ -20,16 +20,18 @@ void hessian_times_vector(const F& f, start_nested(); try { Matrix x_var(x.size()); - for (int i = 0; i < x_var.size(); ++i) + for (int i = 0; i < x_var.size(); ++i) { x_var(i) = x(i); + } var fx_var; var grad_fx_var_dot_v; gradient_dot_vector(f, x_var, v, fx_var, grad_fx_var_dot_v); fx = fx_var.val(); grad(grad_fx_var_dot_v.vi_); Hv.resize(x.size()); - for (int i = 0; i < x.size(); ++i) + for (int i = 0; i < x.size(); ++i) { Hv(i) = x_var(i).adj(); + } } catch (const std::exception& e) { recover_memory_nested(); throw; diff --git a/stan/math/mix/mat/functor/partial_derivative.hpp b/stan/math/mix/mat/functor/partial_derivative.hpp index 7033b6ae94e..09c7044d181 100644 --- a/stan/math/mix/mat/functor/partial_derivative.hpp +++ b/stan/math/mix/mat/functor/partial_derivative.hpp @@ -26,8 +26,9 @@ void partial_derivative(const F& f, const Eigen::Matrix& x, int n, T& fx, T& dfx_dxn) { Eigen::Matrix, Eigen::Dynamic, 1> x_fvar(x.size()); - for (int i = 0; i < x.size(); ++i) + for (int i = 0; i < x.size(); ++i) { x_fvar(i) = fvar(x(i), i == n); + } fvar fx_fvar = f(x_fvar); fx = fx_fvar.val_; dfx_dxn = fx_fvar.d_; diff --git a/stan/math/opencl/cholesky_decompose.hpp b/stan/math/opencl/cholesky_decompose.hpp index add056c137e..87275cddd1a 100644 --- a/stan/math/opencl/cholesky_decompose.hpp +++ b/stan/math/opencl/cholesky_decompose.hpp @@ -43,8 +43,9 @@ namespace math { */ template > inline void cholesky_decompose(matrix_cl& A) { - if (A.rows() == 0) + if (A.rows() == 0) { return; + } // Repeats the blocked cholesky decomposition until the size of the remaining // submatrix is smaller or equal to the minimum blocks size // or a heuristic of 100. diff --git a/stan/math/opencl/diagonal_multiply.hpp b/stan/math/opencl/diagonal_multiply.hpp index 4831eec1532..f7e232b3adf 100644 --- a/stan/math/opencl/diagonal_multiply.hpp +++ b/stan/math/opencl/diagonal_multiply.hpp @@ -21,12 +21,14 @@ template > inline matrix_cl> diagonal_multiply( const matrix_cl& A, const T2 scalar) { matrix_cl> B(A); - if (B.size() == 0) + if (B.size() == 0) { return B; + } // For rectangular matrices int min_dim = B.rows(); - if (B.cols() < min_dim) + if (B.cols() < min_dim) { min_dim = B.cols(); + } try { opencl_kernels::scalar_mul_diagonal(cl::NDRange(min_dim), B, scalar, B.rows(), min_dim); diff --git a/stan/math/opencl/err/check_diagonal_zeros.hpp b/stan/math/opencl/err/check_diagonal_zeros.hpp index 986f1a3b55c..722782a7772 100644 --- a/stan/math/opencl/err/check_diagonal_zeros.hpp +++ b/stan/math/opencl/err/check_diagonal_zeros.hpp @@ -24,8 +24,9 @@ namespace math { template > inline void check_diagonal_zeros(const char* function, const char* name, const matrix_cl& y) { - if (y.size() == 0) + if (y.size() == 0) { return; + } cl::CommandQueue cmd_queue = opencl_context.queue(); cl::Context ctx = opencl_context.context(); try { diff --git a/stan/math/opencl/err/check_nan.hpp b/stan/math/opencl/err/check_nan.hpp index 0e7ae5ede60..e1d2fbe9917 100644 --- a/stan/math/opencl/err/check_nan.hpp +++ b/stan/math/opencl/err/check_nan.hpp @@ -25,8 +25,9 @@ namespace math { template > inline void check_nan(const char* function, const char* name, const matrix_cl& y) { - if (y.size() == 0) + if (y.size() == 0) { return; + } try { int nan_flag = 0; matrix_cl nan_chk(1, 1); diff --git a/stan/math/opencl/err/check_symmetric.hpp b/stan/math/opencl/err/check_symmetric.hpp index eddf9a7e0ab..c4ed0297946 100644 --- a/stan/math/opencl/err/check_symmetric.hpp +++ b/stan/math/opencl/err/check_symmetric.hpp @@ -25,8 +25,9 @@ namespace math { template > inline void check_symmetric(const char* function, const char* name, const matrix_cl& y) { - if (y.size() == 0) + if (y.size() == 0) { return; + } check_square(function, name, y); try { int symmetric_flag = 1; diff --git a/stan/math/opencl/err/check_vector.hpp b/stan/math/opencl/err/check_vector.hpp index cf6b777a2f4..a1ae69836b5 100644 --- a/stan/math/opencl/err/check_vector.hpp +++ b/stan/math/opencl/err/check_vector.hpp @@ -25,12 +25,15 @@ namespace math { template inline void check_vector(const char* function, const char* name, const matrix_cl& x) { - if (x.rows() == 1) + if (x.rows() == 1) { return; - if (x.cols() == 1) + } + if (x.cols() == 1) { return; - if (x.rows() == 1 || x.cols() == 1) + } + if (x.rows() == 1 || x.cols() == 1) { return; + } std::ostringstream msg; msg << ") has " << x.rows() << " rows and " << x.cols() diff --git a/stan/math/opencl/matrix_cl.hpp b/stan/math/opencl/matrix_cl.hpp index 993b96a6147..879720d2de4 100644 --- a/stan/math/opencl/matrix_cl.hpp +++ b/stan/math/opencl/matrix_cl.hpp @@ -195,8 +195,9 @@ class matrix_cl> { matrix_cl(const matrix_cl& A) : rows_(A.rows()), cols_(A.cols()), view_(A.view()) { - if (A.size() == 0) + if (A.size() == 0) { return; + } this->wait_for_read_write_events(); cl::Context& ctx = opencl_context.context(); cl::CommandQueue queue = opencl_context.queue(); @@ -237,8 +238,9 @@ class matrix_cl> { explicit matrix_cl(const std::vector>& A) try : rows_(A.empty() ? 0 : A[0].size()), cols_(A.size()) { - if (this->size() == 0) + if (this->size() == 0) { return; + } cl::Context& ctx = opencl_context.context(); cl::CommandQueue& queue = opencl_context.queue(); // creates the OpenCL buffer to copy the Eigen @@ -432,8 +434,9 @@ class matrix_cl> { a.rows(), "destination.rows()", rows()); check_size_match("assignment of (OpenCL) matrices", "source.cols()", a.cols(), "destination.cols()", cols()); - if (a.size() == 0) + if (a.size() == 0) { return *this; + } view_ = a.view(); this->wait_for_read_write_events(); cl::CommandQueue queue = opencl_context.queue(); diff --git a/stan/math/opencl/multiply.hpp b/stan/math/opencl/multiply.hpp index 15b44e5d743..fb085f1c8bc 100644 --- a/stan/math/opencl/multiply.hpp +++ b/stan/math/opencl/multiply.hpp @@ -115,8 +115,9 @@ template > inline matrix_cl> multiply(const matrix_cl& A, const T2 scalar) { matrix_cl> temp(A.rows(), A.cols(), A.view()); - if (A.size() == 0) + if (A.size() == 0) { return temp; + } try { opencl_kernels::scalar_mul(cl::NDRange(A.rows(), A.cols()), temp, A, scalar, A.rows(), A.cols(), A.view()); diff --git a/stan/math/opencl/multiply_transpose.hpp b/stan/math/opencl/multiply_transpose.hpp index 5abe4fbdb11..8025043c5a8 100644 --- a/stan/math/opencl/multiply_transpose.hpp +++ b/stan/math/opencl/multiply_transpose.hpp @@ -29,8 +29,9 @@ inline matrix_cl multiply_transpose(const matrix_cl& A) { ? matrix_cl_view::Diagonal : matrix_cl_view::Entire); - if (A.size() == 0) + if (A.size() == 0) { return temp; + } // padding the matrices so the dimensions are divisible with local // improves performance becasuse we can omit if statements in the // multiply kernel diff --git a/stan/math/opencl/opencl_context.hpp b/stan/math/opencl/opencl_context.hpp index 07831a100f0..76c5113f215 100644 --- a/stan/math/opencl/opencl_context.hpp +++ b/stan/math/opencl/opencl_context.hpp @@ -56,8 +56,9 @@ inline cl::size_t to_size_t(const size_t (&values)[N]) { template <> inline cl::size_t<3> to_size_t(const size_t (&values)[3]) { cl::size_t<3> s; - for (size_t i = 0; i < 3; i++) + for (size_t i = 0; i < 3; i++) { s[i] = values[i]; + } return s; } } // namespace opencl diff --git a/stan/math/opencl/transpose.hpp b/stan/math/opencl/transpose.hpp index da4737d8e23..0716f33a38e 100644 --- a/stan/math/opencl/transpose.hpp +++ b/stan/math/opencl/transpose.hpp @@ -21,8 +21,9 @@ namespace math { template > inline matrix_cl transpose(const matrix_cl& src) { matrix_cl dst(src.cols(), src.rows(), transpose(src.view())); - if (dst.size() == 0) + if (dst.size() == 0) { return dst; + } try { opencl_kernels::transpose(cl::NDRange(src.rows(), src.cols()), dst, src, src.rows(), src.cols()); diff --git a/stan/math/opencl/zeros.hpp b/stan/math/opencl/zeros.hpp index 9104b95e3ad..72a21b46cc8 100644 --- a/stan/math/opencl/zeros.hpp +++ b/stan/math/opencl/zeros.hpp @@ -27,8 +27,9 @@ namespace math { template template inline void matrix_cl>::zeros() try { - if (size() == 0) + if (size() == 0) { return; + } this->view_ = both(this->view_, invert(matrix_view)); cl::CommandQueue cmdQueue = opencl_context.queue(); opencl_kernels::zeros(cl::NDRange(this->rows(), this->cols()), *this, diff --git a/stan/math/prim/arr/err/check_nonzero_size.hpp b/stan/math/prim/arr/err/check_nonzero_size.hpp index c94d1da3eb7..a6957711759 100644 --- a/stan/math/prim/arr/err/check_nonzero_size.hpp +++ b/stan/math/prim/arr/err/check_nonzero_size.hpp @@ -21,8 +21,9 @@ namespace math { template inline void check_nonzero_size(const char* function, const char* name, const T_y& y) { - if (y.size() > 0) + if (y.size() > 0) { return; + } invalid_argument(function, name, 0, "has size ", ", but must have a non-zero size"); } diff --git a/stan/math/prim/arr/err/is_ordered.hpp b/stan/math/prim/arr/err/is_ordered.hpp index 9920b0c8420..f6f053c87e4 100644 --- a/stan/math/prim/arr/err/is_ordered.hpp +++ b/stan/math/prim/arr/err/is_ordered.hpp @@ -18,8 +18,9 @@ namespace math { template inline bool is_ordered(const std::vector& y) { for (size_t n = 1; n < y.size(); ++n) { - if (!(y[n] > y[n - 1])) + if (!(y[n] > y[n - 1])) { return false; + } } return true; } diff --git a/stan/math/prim/arr/fun/dot.hpp b/stan/math/prim/arr/fun/dot.hpp index 2a7c9997cb5..196452680b6 100644 --- a/stan/math/prim/arr/fun/dot.hpp +++ b/stan/math/prim/arr/fun/dot.hpp @@ -10,8 +10,9 @@ namespace math { inline double dot(const std::vector& x, const std::vector& y) { double sum = 0.0; - for (size_t i = 0; i < x.size(); ++i) + for (size_t i = 0; i < x.size(); ++i) { sum += x[i] * y[i]; + } return sum; } diff --git a/stan/math/prim/arr/fun/dot_self.hpp b/stan/math/prim/arr/fun/dot_self.hpp index 2ae969c1dd6..9b47bbaca5d 100644 --- a/stan/math/prim/arr/fun/dot_self.hpp +++ b/stan/math/prim/arr/fun/dot_self.hpp @@ -10,8 +10,9 @@ namespace math { inline double dot_self(const std::vector& x) { double sum = 0.0; - for (double i : x) + for (double i : x) { sum += i * i; + } return sum; } diff --git a/stan/math/prim/arr/fun/fill.hpp b/stan/math/prim/arr/fun/fill.hpp index a3dfb010226..210132be7c8 100644 --- a/stan/math/prim/arr/fun/fill.hpp +++ b/stan/math/prim/arr/fun/fill.hpp @@ -21,8 +21,9 @@ namespace math { */ template void fill(std::vector& x, const S& y) { - for (typename std::vector::size_type i = 0; i < x.size(); ++i) + for (typename std::vector::size_type i = 0; i < x.size(); ++i) { fill(x[i], y); + } } } // namespace math diff --git a/stan/math/prim/arr/fun/inverse_softmax.hpp b/stan/math/prim/arr/fun/inverse_softmax.hpp index 18f3e98124a..9a844fd279c 100644 --- a/stan/math/prim/arr/fun/inverse_softmax.hpp +++ b/stan/math/prim/arr/fun/inverse_softmax.hpp @@ -35,8 +35,9 @@ template void inverse_softmax(const Vector& simplex, Vector& y) { using std::log; check_matching_sizes("inverse_softmax", "simplex", simplex, "y", y); - for (size_t i = 0; i < simplex.size(); ++i) + for (size_t i = 0; i < simplex.size(); ++i) { y[i] = log(simplex[i]); + } } } // namespace math diff --git a/stan/math/prim/arr/fun/log_sum_exp.hpp b/stan/math/prim/arr/fun/log_sum_exp.hpp index 45729af2f09..1b7f7f2ddbe 100644 --- a/stan/math/prim/arr/fun/log_sum_exp.hpp +++ b/stan/math/prim/arr/fun/log_sum_exp.hpp @@ -28,14 +28,18 @@ inline double log_sum_exp(const std::vector& x) { using std::log; using std::numeric_limits; double max = -numeric_limits::infinity(); - for (double xx : x) - if (xx > max) + for (double xx : x) { + if (xx > max) { max = xx; + } + } double sum = 0.0; - for (size_t ii = 0; ii < x.size(); ii++) - if (x[ii] != -numeric_limits::infinity()) + for (size_t ii = 0; ii < x.size(); ii++) { + if (x[ii] != -numeric_limits::infinity()) { sum += exp(x[ii] - max); + } + } return max + log(sum); } diff --git a/stan/math/prim/arr/fun/promote_elements.hpp b/stan/math/prim/arr/fun/promote_elements.hpp index 523516fc659..bec98a40165 100644 --- a/stan/math/prim/arr/fun/promote_elements.hpp +++ b/stan/math/prim/arr/fun/promote_elements.hpp @@ -29,8 +29,9 @@ struct promote_elements, std::vector > { inline static std::vector promote(const std::vector& u) { std::vector t; t.reserve(u.size()); - for (size_t i = 0; i < u.size(); ++i) + for (size_t i = 0; i < u.size(); ++i) { t.push_back(promote_elements::promote(u[i])); + } return t; } }; diff --git a/stan/math/prim/arr/fun/promote_scalar.hpp b/stan/math/prim/arr/fun/promote_scalar.hpp index 899a18f8504..96573bb2fcf 100644 --- a/stan/math/prim/arr/fun/promote_scalar.hpp +++ b/stan/math/prim/arr/fun/promote_scalar.hpp @@ -33,8 +33,9 @@ struct promote_scalar_struct > { typedef std::vector::type> return_t; typedef typename index_type::type idx_t; return_t y(x.size()); - for (idx_t i = 0; i < x.size(); ++i) + for (idx_t i = 0; i < x.size(); ++i) { y[i] = promote_scalar_struct::apply(x[i]); + } return y; } }; diff --git a/stan/math/prim/arr/fun/scaled_add.hpp b/stan/math/prim/arr/fun/scaled_add.hpp index 6429b33387d..63646447e76 100644 --- a/stan/math/prim/arr/fun/scaled_add.hpp +++ b/stan/math/prim/arr/fun/scaled_add.hpp @@ -10,8 +10,9 @@ namespace math { inline void scaled_add(std::vector& x, const std::vector& y, double lambda) { - for (size_t i = 0; i < x.size(); ++i) + for (size_t i = 0; i < x.size(); ++i) { x[i] += lambda * y[i]; + } } } // namespace math diff --git a/stan/math/prim/arr/fun/sub.hpp b/stan/math/prim/arr/fun/sub.hpp index 275e099a593..2b075e782d4 100644 --- a/stan/math/prim/arr/fun/sub.hpp +++ b/stan/math/prim/arr/fun/sub.hpp @@ -11,8 +11,9 @@ namespace math { inline void sub(std::vector& x, std::vector& y, std::vector& result) { result.resize(x.size()); - for (size_t i = 0; i < x.size(); ++i) + for (size_t i = 0; i < x.size(); ++i) { result[i] = x[i] - y[i]; + } } } // namespace math diff --git a/stan/math/prim/arr/fun/value_of.hpp b/stan/math/prim/arr/fun/value_of.hpp index 5607f2184c0..a3fafa86dcb 100644 --- a/stan/math/prim/arr/fun/value_of.hpp +++ b/stan/math/prim/arr/fun/value_of.hpp @@ -22,8 +22,9 @@ inline std::vector::type> value_of( const std::vector& x) { size_t size = x.size(); std::vector::type> result(size); - for (size_t i = 0; i < size; i++) + for (size_t i = 0; i < size; i++) { result[i] = value_of(x[i]); + } return result; } diff --git a/stan/math/prim/arr/fun/value_of_rec.hpp b/stan/math/prim/arr/fun/value_of_rec.hpp index 4cb06501742..eb04293df90 100644 --- a/stan/math/prim/arr/fun/value_of_rec.hpp +++ b/stan/math/prim/arr/fun/value_of_rec.hpp @@ -23,8 +23,9 @@ template inline std::vector value_of_rec(const std::vector& x) { size_t size = x.size(); std::vector result(size); - for (size_t i = 0; i < size; i++) + for (size_t i = 0; i < size; i++) { result[i] = value_of_rec(x[i]); + } return result; } diff --git a/stan/math/prim/arr/functor/coupled_ode_observer.hpp b/stan/math/prim/arr/functor/coupled_ode_observer.hpp index b11a0501c60..2fe608e5f87 100644 --- a/stan/math/prim/arr/functor/coupled_ode_observer.hpp +++ b/stan/math/prim/arr/functor/coupled_ode_observer.hpp @@ -122,14 +122,16 @@ struct coupled_ode_observer { for (size_t j = 0; j < N_; j++) { // iterate over parameters for each equation if (!is_constant_all::value) { - for (std::size_t k = 0; k < N_; k++) + for (std::size_t k = 0; k < N_; k++) { ops_partials.edge1_.partials_[k] = coupled_state[N_ + N_ * k + j]; + } } if (!is_constant_all::value) { - for (std::size_t k = 0; k < M_; k++) + for (std::size_t k = 0; k < M_; k++) { ops_partials.edge2_.partials_[k] = coupled_state[N_ + index_offset_theta_ + N_ * k + j]; + } } if (!is_constant_all::value) { diff --git a/stan/math/prim/arr/functor/coupled_ode_system.hpp b/stan/math/prim/arr/functor/coupled_ode_system.hpp index 60c24f03999..752d92b10c4 100644 --- a/stan/math/prim/arr/functor/coupled_ode_system.hpp +++ b/stan/math/prim/arr/functor/coupled_ode_system.hpp @@ -114,8 +114,9 @@ class coupled_ode_system { */ std::vector initial_state() const { std::vector state(size_, 0.0); - for (size_t n = 0; n < N_; n++) + for (size_t n = 0; n < N_; n++) { state[n] = y0_dbl_[n]; + } return state; } }; diff --git a/stan/math/prim/arr/functor/integrate_1d.hpp b/stan/math/prim/arr/functor/integrate_1d.hpp index 3ec6cb96430..ed6f3788c4f 100644 --- a/stan/math/prim/arr/functor/integrate_1d.hpp +++ b/stan/math/prim/arr/functor/integrate_1d.hpp @@ -202,8 +202,9 @@ inline double integrate_1d( check_less_or_equal(function, "lower limit", a, b); if (a == b) { - if (std::isinf(a)) + if (std::isinf(a)) { domain_error(function, "Integration endpoints are both", a, "", ""); + } return 0.0; } else { return integrate( diff --git a/stan/math/prim/arr/functor/integrate_ode_rk45.hpp b/stan/math/prim/arr/functor/integrate_ode_rk45.hpp index bf65d6becae..6fa74daa79d 100644 --- a/stan/math/prim/arr/functor/integrate_ode_rk45.hpp +++ b/stan/math/prim/arr/functor/integrate_ode_rk45.hpp @@ -96,15 +96,18 @@ std::vector>> integrate_ode_rk45( check_ordered("integrate_ode_rk45", "times", ts_dbl); check_less("integrate_ode_rk45", "initial time", t0_dbl, ts_dbl[0]); - if (relative_tolerance <= 0) + if (relative_tolerance <= 0) { invalid_argument("integrate_ode_rk45", "relative_tolerance,", relative_tolerance, "", ", must be greater than 0"); - if (absolute_tolerance <= 0) + } + if (absolute_tolerance <= 0) { invalid_argument("integrate_ode_rk45", "absolute_tolerance,", absolute_tolerance, "", ", must be greater than 0"); - if (max_num_steps <= 0) + } + if (max_num_steps <= 0) { invalid_argument("integrate_ode_rk45", "max_num_steps,", max_num_steps, "", ", must be greater than 0"); + } // creates basic or coupled system by template specializations coupled_ode_system coupled_system(f, y0, theta, x, x_int, msgs); diff --git a/stan/math/prim/mat/err/check_cholesky_factor.hpp b/stan/math/prim/mat/err/check_cholesky_factor.hpp index 122487798f5..f8c6b21e3ce 100644 --- a/stan/math/prim/mat/err/check_cholesky_factor.hpp +++ b/stan/math/prim/mat/err/check_cholesky_factor.hpp @@ -32,9 +32,10 @@ inline void check_cholesky_factor( y.rows()); check_positive(function, "columns of Cholesky factor", y.cols()); check_lower_triangular(function, name, y); - for (int i = 0; i < y.cols(); ++i) + for (int i = 0; i < y.cols(); ++i) { // FIXME: should report row check_positive(function, name, y(i, i)); + } } } // namespace math diff --git a/stan/math/prim/mat/err/check_cholesky_factor_corr.hpp b/stan/math/prim/mat/err/check_cholesky_factor_corr.hpp index 86425864ddb..2c7334039f4 100644 --- a/stan/math/prim/mat/err/check_cholesky_factor_corr.hpp +++ b/stan/math/prim/mat/err/check_cholesky_factor_corr.hpp @@ -33,8 +33,9 @@ void check_cholesky_factor_corr( const Eigen::Matrix& y) { check_square(function, name, y); check_lower_triangular(function, name, y); - for (int i = 0; i < y.rows(); ++i) + for (int i = 0; i < y.rows(); ++i) { check_positive(function, name, y(i, i)); + } for (int i = 0; i < y.rows(); ++i) { Eigen::Matrix y_i = y.row(i).transpose(); check_unit_vector(function, name, y_i); diff --git a/stan/math/prim/mat/err/check_column_index.hpp b/stan/math/prim/mat/err/check_column_index.hpp index 1e0bc9934d3..545617c4473 100644 --- a/stan/math/prim/mat/err/check_column_index.hpp +++ b/stan/math/prim/mat/err/check_column_index.hpp @@ -30,8 +30,9 @@ template inline void check_column_index(const char* function, const char* name, const Eigen::Matrix& y, size_t i) { if (i >= stan::error_index::value - && i < static_cast(y.cols()) + stan::error_index::value) + && i < static_cast(y.cols()) + stan::error_index::value) { return; + } std::stringstream msg; msg << " for columns of " << name; diff --git a/stan/math/prim/mat/err/check_consistent_size_mvt.hpp b/stan/math/prim/mat/err/check_consistent_size_mvt.hpp index 45414d1e318..e9ced647479 100644 --- a/stan/math/prim/mat/err/check_consistent_size_mvt.hpp +++ b/stan/math/prim/mat/err/check_consistent_size_mvt.hpp @@ -28,17 +28,19 @@ inline void check_consistent_size_mvt(const char* function, const char* name, if (length(x) == 0) { size_x = 0; - if (expected_size == 0) + if (expected_size == 0) { return; + } } else { size_t size_x = stan::length_mvt(x); bool x_contains_vectors = is_vector< typename std::remove_reference::type>::value; - if (!x_contains_vectors) + if (!x_contains_vectors) { return; - else if (expected_size == size_x) + } else if (expected_size == size_x) { return; + } } std::stringstream msg; diff --git a/stan/math/prim/mat/err/check_finite.hpp b/stan/math/prim/mat/err/check_finite.hpp index 8ed76bbfe18..3dee01bdd11 100644 --- a/stan/math/prim/mat/err/check_finite.hpp +++ b/stan/math/prim/mat/err/check_finite.hpp @@ -29,9 +29,10 @@ struct finite, true> { const Eigen::Matrix& y) { if (!value_of(y).allFinite()) { for (int n = 0; n < y.size(); ++n) { - if (!(boost::math::isfinite)(y(n))) + if (!(boost::math::isfinite)(y(n))) { domain_error_vec(function, name, y, n, "is ", ", but must be finite!"); + } } } } diff --git a/stan/math/prim/mat/err/check_pos_definite.hpp b/stan/math/prim/mat/err/check_pos_definite.hpp index 08411a4094b..1687bd056e2 100644 --- a/stan/math/prim/mat/err/check_pos_definite.hpp +++ b/stan/math/prim/mat/err/check_pos_definite.hpp @@ -29,13 +29,15 @@ inline void check_pos_definite(const char* function, const char* name, const Eigen::Matrix& y) { check_symmetric(function, name, y); check_positive(function, name, "rows", y.rows()); - if (y.rows() == 1 && !(y(0, 0) > CONSTRAINT_TOLERANCE)) + if (y.rows() == 1 && !(y(0, 0) > CONSTRAINT_TOLERANCE)) { domain_error(function, name, "is not positive definite.", ""); + } Eigen::LDLT cholesky = value_of_rec(y).ldlt(); if (cholesky.info() != Eigen::Success || !cholesky.isPositive() - || (cholesky.vectorD().array() <= 0.0).any()) + || (cholesky.vectorD().array() <= 0.0).any()) { domain_error(function, name, "is not positive definite.", ""); + } check_not_nan(function, name, y); } @@ -53,8 +55,9 @@ template inline void check_pos_definite(const char* function, const char* name, const Eigen::LDLT& cholesky) { if (cholesky.info() != Eigen::Success || !cholesky.isPositive() - || !(cholesky.vectorD().array() > 0.0).all()) + || !(cholesky.vectorD().array() > 0.0).all()) { domain_error(function, "LDLT decomposition of", " failed", name); + } } /** @@ -72,8 +75,9 @@ template inline void check_pos_definite(const char* function, const char* name, const Eigen::LLT& cholesky) { if (cholesky.info() != Eigen::Success - || !(cholesky.matrixLLT().diagonal().array() > 0.0).all()) + || !(cholesky.matrixLLT().diagonal().array() > 0.0).all()) { domain_error(function, "Matrix", " is not positive definite", name); + } } } // namespace math diff --git a/stan/math/prim/mat/err/check_pos_semidefinite.hpp b/stan/math/prim/mat/err/check_pos_semidefinite.hpp index fe79123b904..261d98cb1f7 100644 --- a/stan/math/prim/mat/err/check_pos_semidefinite.hpp +++ b/stan/math/prim/mat/err/check_pos_semidefinite.hpp @@ -32,16 +32,18 @@ inline void check_pos_semidefinite( check_symmetric(function, name, y); check_positive(function, name, "rows", y.rows()); - if (y.rows() == 1 && !(y(0, 0) >= 0.0)) + if (y.rows() == 1 && !(y(0, 0) >= 0.0)) { domain_error(function, name, "is not positive semi-definite.", ""); + } using Eigen::Dynamic; using Eigen::LDLT; using Eigen::Matrix; LDLT > cholesky = value_of_rec(y).ldlt(); if (cholesky.info() != Eigen::Success - || (cholesky.vectorD().array() < 0.0).any()) + || (cholesky.vectorD().array() < 0.0).any()) { domain_error(function, name, "is not positive semi-definite.", ""); + } check_not_nan(function, name, y); } @@ -59,8 +61,9 @@ template inline void check_pos_semidefinite(const char* function, const char* name, const Eigen::LDLT& cholesky) { if (cholesky.info() != Eigen::Success - || (cholesky.vectorD().array() < 0.0).any()) + || (cholesky.vectorD().array() < 0.0).any()) { domain_error(function, name, "is not positive semi-definite.", ""); + } } } // namespace math diff --git a/stan/math/prim/mat/err/check_positive_ordered.hpp b/stan/math/prim/mat/err/check_positive_ordered.hpp index bac2b82f710..dfbba3a3484 100644 --- a/stan/math/prim/mat/err/check_positive_ordered.hpp +++ b/stan/math/prim/mat/err/check_positive_ordered.hpp @@ -27,8 +27,9 @@ void check_positive_ordered(const char* function, const char* name, using Eigen::Dynamic; using Eigen::Matrix; - if (y.size() == 0) + if (y.size() == 0) { return; + } if (y[0] < 0) { std::ostringstream msg; diff --git a/stan/math/prim/mat/err/check_range.hpp b/stan/math/prim/mat/err/check_range.hpp index ffd2497404d..eace472160a 100644 --- a/stan/math/prim/mat/err/check_range.hpp +++ b/stan/math/prim/mat/err/check_range.hpp @@ -24,8 +24,9 @@ namespace math { inline void check_range(const char* function, const char* name, int max, int index, int nested_level, const char* error_msg) { if ((index >= stan::error_index::value) - && (index < max + stan::error_index::value)) + && (index < max + stan::error_index::value)) { return; + } std::stringstream msg; msg << "; index position = " << nested_level; @@ -48,8 +49,9 @@ inline void check_range(const char* function, const char* name, int max, inline void check_range(const char* function, const char* name, int max, int index, const char* error_msg) { if ((index >= stan::error_index::value) - && (index < max + stan::error_index::value)) + && (index < max + stan::error_index::value)) { return; + } out_of_range(function, max, index, error_msg); } @@ -67,8 +69,9 @@ inline void check_range(const char* function, const char* name, int max, inline void check_range(const char* function, const char* name, int max, int index) { if ((index >= stan::error_index::value) - && (index < max + stan::error_index::value)) + && (index < max + stan::error_index::value)) { return; + } out_of_range(function, max, index); } diff --git a/stan/math/prim/mat/err/check_row_index.hpp b/stan/math/prim/mat/err/check_row_index.hpp index d6dd8aa8afa..b4d803dbb41 100644 --- a/stan/math/prim/mat/err/check_row_index.hpp +++ b/stan/math/prim/mat/err/check_row_index.hpp @@ -27,8 +27,9 @@ template inline void check_row_index(const char* function, const char* name, const Eigen::Matrix& y, size_t i) { if (i >= stan::error_index::value - && i < static_cast(y.rows()) + stan::error_index::value) + && i < static_cast(y.rows()) + stan::error_index::value) { return; + } std::stringstream msg; msg << " for rows of " << name; diff --git a/stan/math/prim/mat/err/check_std_vector_index.hpp b/stan/math/prim/mat/err/check_std_vector_index.hpp index 3358891b8f6..86de406710b 100644 --- a/stan/math/prim/mat/err/check_std_vector_index.hpp +++ b/stan/math/prim/mat/err/check_std_vector_index.hpp @@ -26,8 +26,9 @@ template inline void check_std_vector_index(const char* function, const char* name, const std::vector& y, int i) { if (i >= static_cast(stan::error_index::value) - && i < static_cast(y.size() + stan::error_index::value)) + && i < static_cast(y.size() + stan::error_index::value)) { return; + } std::stringstream msg; msg << " for " << name; diff --git a/stan/math/prim/mat/err/check_symmetric.hpp b/stan/math/prim/mat/err/check_symmetric.hpp index 87ee12cc984..60f5a34c3c4 100644 --- a/stan/math/prim/mat/err/check_symmetric.hpp +++ b/stan/math/prim/mat/err/check_symmetric.hpp @@ -35,8 +35,9 @@ inline void check_symmetric( Eigen::Matrix >::type size_type; size_type k = y.rows(); - if (k == 1) + if (k == 1) { return; + } for (size_type m = 0; m < k; ++m) { for (size_type n = m + 1; n < k; ++n) { if (!(fabs(value_of(y(m, n)) - value_of(y(n, m))) diff --git a/stan/math/prim/mat/err/check_vector.hpp b/stan/math/prim/mat/err/check_vector.hpp index 3b6be93a6f5..8bf88d65ebb 100644 --- a/stan/math/prim/mat/err/check_vector.hpp +++ b/stan/math/prim/mat/err/check_vector.hpp @@ -27,12 +27,15 @@ namespace math { template inline void check_vector(const char* function, const char* name, const Eigen::Matrix& x) { - if (R == 1) + if (R == 1) { return; - if (C == 1) + } + if (C == 1) { return; - if (x.rows() == 1 || x.cols() == 1) + } + if (x.rows() == 1 || x.cols() == 1) { return; + } std::ostringstream msg; msg << ") has " << x.rows() << " rows and " << x.cols() diff --git a/stan/math/prim/mat/err/is_cholesky_factor_corr.hpp b/stan/math/prim/mat/err/is_cholesky_factor_corr.hpp index 9996e3ed6fe..86f7545e5e9 100644 --- a/stan/math/prim/mat/err/is_cholesky_factor_corr.hpp +++ b/stan/math/prim/mat/err/is_cholesky_factor_corr.hpp @@ -25,12 +25,14 @@ namespace math { template inline bool is_cholesky_factor_corr( const Eigen::Matrix& y) { - if (!is_cholesky_factor(y)) + if (!is_cholesky_factor(y)) { return false; + } for (int i = 0; i < y.rows(); ++i) { Eigen::Matrix y_i = y.row(i).transpose(); - if (!is_unit_vector(y_i)) + if (!is_unit_vector(y_i)) { return false; + } } return true; } diff --git a/stan/math/prim/mat/err/is_corr_matrix.hpp b/stan/math/prim/mat/err/is_corr_matrix.hpp index 962cc25b81c..a05869f7fed 100644 --- a/stan/math/prim/mat/err/is_corr_matrix.hpp +++ b/stan/math/prim/mat/err/is_corr_matrix.hpp @@ -31,16 +31,20 @@ inline bool is_corr_matrix( typedef typename index_type< Eigen::Matrix >::type size_t; - if (!is_size_match(y.rows(), y.cols())) + if (!is_size_match(y.rows(), y.cols())) { return false; - if (!is_positive(y.rows())) + } + if (!is_positive(y.rows())) { return false; - if (!is_pos_definite(y)) + } + if (!is_pos_definite(y)) { return false; + } if (is_symmetric(y)) { for (size_t k = 0; k < y.rows(); ++k) { - if (!(fabs(y(k, k) - 1.0) <= CONSTRAINT_TOLERANCE)) + if (!(fabs(y(k, k) - 1.0) <= CONSTRAINT_TOLERANCE)) { return false; + } } } return true; diff --git a/stan/math/prim/mat/err/is_pos_definite.hpp b/stan/math/prim/mat/err/is_pos_definite.hpp index 1a79d32dd62..febc7c14178 100644 --- a/stan/math/prim/mat/err/is_pos_definite.hpp +++ b/stan/math/prim/mat/err/is_pos_definite.hpp @@ -25,16 +25,20 @@ namespace math { */ template inline bool is_pos_definite(const Eigen::Matrix& y) { - if (!is_symmetric(y)) + if (!is_symmetric(y)) { return false; - if (!is_positive(y.rows())) + } + if (!is_positive(y.rows())) { return false; - if (y.rows() == 1 && !(y(0, 0) > CONSTRAINT_TOLERANCE)) + } + if (y.rows() == 1 && !(y(0, 0) > CONSTRAINT_TOLERANCE)) { return false; + } Eigen::LDLT cholesky = value_of_rec(y).ldlt(); if (cholesky.info() != Eigen::Success || !cholesky.isPositive() - || (cholesky.vectorD().array() <= 0.0).any()) + || (cholesky.vectorD().array() <= 0.0).any()) { return false; + } return is_not_nan(y); } diff --git a/stan/math/prim/mat/err/is_symmetric.hpp b/stan/math/prim/mat/err/is_symmetric.hpp index fa7e395ceb3..bde27c7847a 100644 --- a/stan/math/prim/mat/err/is_symmetric.hpp +++ b/stan/math/prim/mat/err/is_symmetric.hpp @@ -20,20 +20,23 @@ namespace math { template inline bool is_symmetric( const Eigen::Matrix& y) { - if (!is_square(y)) + if (!is_square(y)) { return false; + } typedef typename index_type< Eigen::Matrix>::type size_type; size_type k = y.rows(); - if (k == 1) + if (k == 1) { return true; + } for (size_type m = 0; m < k; ++m) { for (size_type n = m + 1; n < k; ++n) { if (!(fabs(value_of(y(m, n)) - value_of(y(n, m))) - <= CONSTRAINT_TOLERANCE)) + <= CONSTRAINT_TOLERANCE)) { return false; + } } } return true; diff --git a/stan/math/prim/mat/fun/LDLT_factor.hpp b/stan/math/prim/mat/fun/LDLT_factor.hpp index 455513dbc72..0cd9400775e 100644 --- a/stan/math/prim/mat/fun/LDLT_factor.hpp +++ b/stan/math/prim/mat/fun/LDLT_factor.hpp @@ -81,14 +81,18 @@ class LDLT_factor { } inline bool success() const { - if (ldltP_->info() != Eigen::Success) + if (ldltP_->info() != Eigen::Success) { return false; - if (!(ldltP_->isPositive())) + } + if (!(ldltP_->isPositive())) { return false; + } vector_t ldltP_diag(ldltP_->vectorD()); - for (int i = 0; i < ldltP_diag.size(); ++i) - if (ldltP_diag(i) <= 0 || is_nan(ldltP_diag(i))) + for (int i = 0; i < ldltP_diag.size(); ++i) { + if (ldltP_diag(i) <= 0 || is_nan(ldltP_diag(i))) { return false; + } + } return true; } diff --git a/stan/math/prim/mat/fun/MatrixExponential.h b/stan/math/prim/mat/fun/MatrixExponential.h index 766077f0919..927b38108fa 100644 --- a/stan/math/prim/mat/fun/MatrixExponential.h +++ b/stan/math/prim/mat/fun/MatrixExponential.h @@ -197,7 +197,9 @@ namespace Eigen { } else { const double maxnorm = 5.371920351148152; frexp(value_of_rec(l1norm) / value_of_rec(maxnorm), &squarings); - if (squarings < 0) squarings = 0; + if (squarings < 0) { + squarings = 0; + } MatrixType A = arg.unaryExpr(MatrixExponentialScalingOp(squarings)); matrix_exp_pade13(A, U, V); } diff --git a/stan/math/prim/mat/fun/accumulator.hpp b/stan/math/prim/mat/fun/accumulator.hpp index 6171fba55e8..5629812bf02 100644 --- a/stan/math/prim/mat/fun/accumulator.hpp +++ b/stan/math/prim/mat/fun/accumulator.hpp @@ -80,8 +80,9 @@ class accumulator { */ template void add(const Eigen::Matrix& m) { - for (int i = 0; i < m.size(); ++i) + for (int i = 0; i < m.size(); ++i) { this->add(m(i)); + } } /** @@ -95,8 +96,9 @@ class accumulator { */ template void add(const std::vector& xs) { - for (size_t i = 0; i < xs.size(); ++i) + for (size_t i = 0; i < xs.size(); ++i) { this->add(xs[i]); + } } /** diff --git a/stan/math/prim/mat/fun/append_array.hpp b/stan/math/prim/mat/fun/append_array.hpp index eff75999824..c0f610eddb5 100644 --- a/stan/math/prim/mat/fun/append_array.hpp +++ b/stan/math/prim/mat/fun/append_array.hpp @@ -37,10 +37,12 @@ append_array(const std::vector& x, const std::vector& y) { zdims[0] += y.size(); } resize(z, zdims); - for (size_t i = 0; i < x.size(); ++i) + for (size_t i = 0; i < x.size(); ++i) { assign(z[i], x[i]); - for (size_t i = 0; i < y.size(); ++i) + } + for (size_t i = 0; i < y.size(); ++i) { assign(z[i + x.size()], y[i]); + } return z; } diff --git a/stan/math/prim/mat/fun/append_col.hpp b/stan/math/prim/mat/fun/append_col.hpp index 46eb4cd1f47..88e15f3e6f8 100644 --- a/stan/math/prim/mat/fun/append_col.hpp +++ b/stan/math/prim/mat/fun/append_col.hpp @@ -46,13 +46,17 @@ append_col(const Eigen::Matrix& A, check_size_match("append_col", "rows of A", Arows, "rows of B", Brows); Matrix, Dynamic, Dynamic> result(Arows, Acols + Bcols); - for (int j = 0; j < Acols; j++) - for (int i = 0; i < Arows; i++) + for (int j = 0; j < Acols; j++) { + for (int i = 0; i < Arows; i++) { result(i, j) = A(i, j); + } + } - for (int j = Acols, k = 0; k < Bcols; j++, k++) - for (int i = 0; i < Arows; i++) + for (int j = Acols, k = 0; k < Bcols; j++, k++) { + for (int i = 0; i < Arows; i++) { result(i, j) = B(i, k); + } + } return result; } @@ -82,10 +86,12 @@ inline Eigen::Matrix, 1, Eigen::Dynamic> append_col( int Asize = A.size(); int Bsize = B.size(); Matrix, 1, Dynamic> result(Asize + Bsize); - for (int i = 0; i < Asize; i++) + for (int i = 0; i < Asize; i++) { result(i) = A(i); - for (int i = 0, j = Asize; i < Bsize; i++, j++) + } + for (int i = 0, j = Asize; i < Bsize; i++, j++) { result(j) = B(i); + } return result; } diff --git a/stan/math/prim/mat/fun/append_row.hpp b/stan/math/prim/mat/fun/append_row.hpp index 63adcbfaa7f..a9e0f6ee7ce 100644 --- a/stan/math/prim/mat/fun/append_row.hpp +++ b/stan/math/prim/mat/fun/append_row.hpp @@ -45,10 +45,12 @@ append_row(const Eigen::Matrix& A, Matrix, Dynamic, Dynamic> result(Arows + Brows, Acols); for (int j = 0; j < Acols; j++) { - for (int i = 0; i < Arows; i++) + for (int i = 0; i < Arows; i++) { result(i, j) = A(i, j); - for (int i = Arows, k = 0; k < Brows; i++, k++) + } + for (int i = Arows, k = 0; k < Brows; i++, k++) { result(i, j) = B(k, j); + } } return result; } @@ -77,10 +79,12 @@ inline Eigen::Matrix, Eigen::Dynamic, 1> append_row( int Asize = A.size(); int Bsize = B.size(); Matrix, 1, Dynamic> result(Asize + Bsize); - for (int i = 0; i < Asize; i++) + for (int i = 0; i < Asize; i++) { result(i) = A(i); - for (int i = 0, j = Asize; i < Bsize; i++, j++) + } + for (int i = 0, j = Asize; i < Bsize; i++, j++) { result(j) = B(i); + } return result; } diff --git a/stan/math/prim/mat/fun/assign.hpp b/stan/math/prim/mat/fun/assign.hpp index 637f04663dd..d8b2f017cbb 100644 --- a/stan/math/prim/mat/fun/assign.hpp +++ b/stan/math/prim/mat/fun/assign.hpp @@ -23,10 +23,11 @@ namespace math { */ template inline void print_mat_size(std::ostream& o) { - if (N == Eigen::Dynamic) + if (N == Eigen::Dynamic) { o << "dynamically sized"; - else + } else { o << N; + } } /** @@ -107,8 +108,9 @@ template inline void assign(Eigen::Matrix& x, const Eigen::Matrix& y) { check_matching_dims("assign", "left-hand-side", x, "right-hand-side", y); - for (int i = 0; i < x.size(); ++i) + for (int i = 0; i < x.size(); ++i) { assign(x(i), y(i)); + } } /** @@ -140,9 +142,11 @@ inline void assign(Eigen::Block x, const Eigen::Matrix& y) { "right-hand side rows", y.rows()); check_size_match("assign", "left-hand side cols", x.cols(), "right-hand side cols", y.cols()); - for (int n = 0; n < y.cols(); ++n) - for (int m = 0; m < y.rows(); ++m) + for (int n = 0; n < y.cols(); ++n) { + for (int m = 0; m < y.rows(); ++m) { assign(x(m, n), y(m, n)); + } + } } /** @@ -167,8 +171,9 @@ inline void assign(Eigen::Block x, const Eigen::Matrix& y) { template inline void assign(std::vector& x, const std::vector& y) { check_matching_sizes("assign", "left-hand side", x, "right-hand side", y); - for (size_t i = 0; i < x.size(); ++i) + for (size_t i = 0; i < x.size(); ++i) { assign(x[i], y[i]); + } } } // namespace math diff --git a/stan/math/prim/mat/fun/autocorrelation.hpp b/stan/math/prim/mat/fun/autocorrelation.hpp index 9e8951e8e3c..dfa1c603f0f 100644 --- a/stan/math/prim/mat/fun/autocorrelation.hpp +++ b/stan/math/prim/mat/fun/autocorrelation.hpp @@ -15,18 +15,23 @@ namespace internal { * a minimum number of zeros are padded. */ inline size_t fft_next_good_size(size_t N) { - if (N <= 2) + if (N <= 2) { return 2; + } while (true) { size_t m = N; - while ((m % 2) == 0) + while ((m % 2) == 0) { m /= 2; - while ((m % 3) == 0) + } + while ((m % 3) == 0) { m /= 3; - while ((m % 5) == 0) + } + while ((m % 5) == 0) { m /= 5; - if (m <= 1) + } + if (m <= 1) { return N; + } N++; } } @@ -68,12 +73,14 @@ void autocorrelation(const std::vector& y, std::vector& ac, vector centered_signal(y); centered_signal.insert(centered_signal.end(), Mt2 - N, 0.0); T mean = stan::math::mean(y); - for (size_t i = 0; i < N; i++) + for (size_t i = 0; i < N; i++) { centered_signal[i] -= mean; + } fft.fwd(freqvec, centered_signal); - for (size_t i = 0; i < Mt2; ++i) + for (size_t i = 0; i < Mt2; ++i) { freqvec[i] = complex(norm(freqvec[i]), 0.0); + } fft.inv(ac, freqvec); ac.resize(N); @@ -82,8 +89,9 @@ void autocorrelation(const std::vector& y, std::vector& ac, ac[i] /= (N - i); } T var = ac[0]; - for (size_t i = 0; i < N; ++i) + for (size_t i = 0; i < N; ++i) { ac[i] /= var; + } } /** @@ -128,8 +136,9 @@ void autocorrelation(const Eigen::MatrixBase& y, fft.inv(ac_tmp, freqvec); fft.ClearFlag(fft.HalfSpectrum); - for (size_t i = 0; i < N; ++i) + for (size_t i = 0; i < N; ++i) { ac_tmp(i) /= (N - i); + } ac = ac_tmp.head(N).array() / ac_tmp(0); } diff --git a/stan/math/prim/mat/fun/chol2inv.hpp b/stan/math/prim/mat/fun/chol2inv.hpp index f217396580a..ee6b35cd743 100644 --- a/stan/math/prim/mat/fun/chol2inv.hpp +++ b/stan/math/prim/mat/fun/chol2inv.hpp @@ -27,8 +27,9 @@ Eigen::Matrix chol2inv( check_lower_triangular("chol2inv", "L", L); int K = L.rows(); typedef Eigen::Matrix matrix_t; - if (K == 0) + if (K == 0) { return L; + } if (K == 1) { matrix_t X(1, 1); X.coeffRef(0) = inv_square(L.coeff(0)); diff --git a/stan/math/prim/mat/fun/cholesky_corr_constrain.hpp b/stan/math/prim/mat/fun/cholesky_corr_constrain.hpp index 6f0fb6fdd2b..b1f2d2a8ff8 100644 --- a/stan/math/prim/mat/fun/cholesky_corr_constrain.hpp +++ b/stan/math/prim/mat/fun/cholesky_corr_constrain.hpp @@ -21,11 +21,13 @@ Eigen::Matrix cholesky_corr_constrain( check_size_match("cholesky_corr_constrain", "y.size()", y.size(), "k_choose_2", k_choose_2); Matrix z(k_choose_2); - for (int i = 0; i < k_choose_2; ++i) + for (int i = 0; i < k_choose_2; ++i) { z(i) = corr_constrain(y(i)); + } Matrix x(K, K); - if (K == 0) + if (K == 0) { return x; + } x.setZero(); x(0, 0) = 1; int k = 0; @@ -52,11 +54,13 @@ Eigen::Matrix cholesky_corr_constrain( check_size_match("cholesky_corr_constrain", "y.size()", y.size(), "k_choose_2", k_choose_2); Matrix z(k_choose_2); - for (int i = 0; i < k_choose_2; ++i) + for (int i = 0; i < k_choose_2; ++i) { z(i) = corr_constrain(y(i), lp); + } Matrix x(K, K); - if (K == 0) + if (K == 0) { return x; + } x.setZero(); x(0, 0) = 1; int k = 0; diff --git a/stan/math/prim/mat/fun/cholesky_factor_constrain.hpp b/stan/math/prim/mat/fun/cholesky_factor_constrain.hpp index 37f4f5aa314..27f6c5732ed 100644 --- a/stan/math/prim/mat/fun/cholesky_factor_constrain.hpp +++ b/stan/math/prim/mat/fun/cholesky_factor_constrain.hpp @@ -38,16 +38,20 @@ Eigen::Matrix cholesky_factor_constrain( int pos = 0; for (int m = 0; m < N; ++m) { - for (int n = 0; n < m; ++n) + for (int n = 0; n < m; ++n) { y(m, n) = x(pos++); + } y(m, m) = exp(x(pos++)); - for (int n = m + 1; n < N; ++n) + for (int n = m + 1; n < N; ++n) { y(m, n) = zero; + } } - for (int m = N; m < M; ++m) - for (int n = 0; n < N; ++n) + for (int m = N; m < M; ++m) { + for (int n = 0; n < N; ++n) { y(m, n) = x(pos++); + } + } return y; } diff --git a/stan/math/prim/mat/fun/cholesky_factor_free.hpp b/stan/math/prim/mat/fun/cholesky_factor_free.hpp index e959a5c3bf2..5b0fd68be9a 100644 --- a/stan/math/prim/mat/fun/cholesky_factor_free.hpp +++ b/stan/math/prim/mat/fun/cholesky_factor_free.hpp @@ -29,14 +29,17 @@ Eigen::Matrix cholesky_factor_free( int pos = 0; for (int m = 0; m < N; ++m) { - for (int n = 0; n < m; ++n) + for (int n = 0; n < m; ++n) { x(pos++) = y(m, n); + } x(pos++) = log(y(m, m)); } - for (int m = N; m < M; ++m) - for (int n = 0; n < N; ++n) + for (int m = N; m < M; ++m) { + for (int n = 0; n < N; ++n) { x(pos++) = y(m, n); + } + } return x; } diff --git a/stan/math/prim/mat/fun/corr_matrix_constrain.hpp b/stan/math/prim/mat/fun/corr_matrix_constrain.hpp index 73f9cfd8698..baec681b8a4 100644 --- a/stan/math/prim/mat/fun/corr_matrix_constrain.hpp +++ b/stan/math/prim/mat/fun/corr_matrix_constrain.hpp @@ -47,8 +47,9 @@ Eigen::Matrix corr_matrix_constrain( check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", k_choose_2); Eigen::Array cpcs(k_choose_2); - for (size_type i = 0; i < k_choose_2; ++i) + for (size_type i = 0; i < k_choose_2; ++i) { cpcs[i] = corr_constrain(x[i]); + } return read_corr_matrix(cpcs, k); } @@ -85,8 +86,9 @@ Eigen::Matrix corr_matrix_constrain( check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", k_choose_2); Array cpcs(k_choose_2); - for (size_type i = 0; i < k_choose_2; ++i) + for (size_type i = 0; i < k_choose_2; ++i) { cpcs[i] = corr_constrain(x[i], lp); + } return read_corr_matrix(cpcs, k, lp); } diff --git a/stan/math/prim/mat/fun/corr_matrix_free.hpp b/stan/math/prim/mat/fun/corr_matrix_free.hpp index 92cb8178603..6793be2efa7 100644 --- a/stan/math/prim/mat/fun/corr_matrix_free.hpp +++ b/stan/math/prim/mat/fun/corr_matrix_free.hpp @@ -49,8 +49,9 @@ Eigen::Matrix corr_matrix_free( Array x(k_choose_2); Array sds(k); bool successful = factor_cov_matrix(y, x, sds); - if (!successful) + if (!successful) { domain_error("corr_matrix_free", "factor_cov_matrix failed on y", y, ""); + } for (size_type i = 0; i < k; ++i) { check_bounded("corr_matrix_free", "log(sd)", sds[i], -CONSTRAINT_TOLERANCE, CONSTRAINT_TOLERANCE); diff --git a/stan/math/prim/mat/fun/cov_matrix_constrain.hpp b/stan/math/prim/mat/fun/cov_matrix_constrain.hpp index 71be3e59fd3..2f7237d560d 100644 --- a/stan/math/prim/mat/fun/cov_matrix_constrain.hpp +++ b/stan/math/prim/mat/fun/cov_matrix_constrain.hpp @@ -38,11 +38,13 @@ Eigen::Matrix cov_matrix_constrain( "K + (K choose 2)", (K * (K + 1)) / 2); int i = 0; for (index_t m = 0; m < K; ++m) { - for (int n = 0; n < m; ++n) + for (int n = 0; n < m; ++n) { L(m, n) = x(i++); + } L(m, m) = exp(x(i++)); - for (index_t n = m + 1; n < K; ++n) + for (index_t n = m + 1; n < K; ++n) { L(m, n) = 0.0; + } } return multiply_lower_tri_self_transpose(L); } @@ -75,16 +77,19 @@ Eigen::Matrix cov_matrix_constrain( Matrix L(K, K); int i = 0; for (index_t m = 0; m < K; ++m) { - for (index_t n = 0; n < m; ++n) + for (index_t n = 0; n < m; ++n) { L(m, n) = x(i++); + } L(m, m) = exp(x(i++)); - for (index_t n = m + 1; n < K; ++n) + for (index_t n = m + 1; n < K; ++n) { L(m, n) = 0.0; + } } // Jacobian for complete transform, including exp() above lp += (K * LOG_2); // needless constant; want propto - for (index_t k = 0; k < K; ++k) + for (index_t k = 0; k < K; ++k) { lp += (K - k + 1) * log(L(k, k)); // only +1 because index from 0 + } return multiply_lower_tri_self_transpose(L); } diff --git a/stan/math/prim/mat/fun/cov_matrix_constrain_lkj.hpp b/stan/math/prim/mat/fun/cov_matrix_constrain_lkj.hpp index 7032e3834bb..eb5101e389c 100644 --- a/stan/math/prim/mat/fun/cov_matrix_constrain_lkj.hpp +++ b/stan/math/prim/mat/fun/cov_matrix_constrain_lkj.hpp @@ -34,11 +34,13 @@ Eigen::Matrix cov_matrix_constrain_lkj( size_t k_choose_2 = (k * (k - 1)) / 2; Eigen::Array cpcs(k_choose_2); int pos = 0; - for (size_t i = 0; i < k_choose_2; ++i) + for (size_t i = 0; i < k_choose_2; ++i) { cpcs[i] = corr_constrain(x[pos++]); + } Eigen::Array sds(k); - for (size_t i = 0; i < k; ++i) + for (size_t i = 0; i < k; ++i) { sds[i] = positive_constrain(x[pos++]); + } return read_cov_matrix(cpcs, sds); } @@ -72,11 +74,13 @@ Eigen::Matrix cov_matrix_constrain_lkj( size_t k_choose_2 = (k * (k - 1)) / 2; Eigen::Array cpcs(k_choose_2); int pos = 0; - for (size_t i = 0; i < k_choose_2; ++i) + for (size_t i = 0; i < k_choose_2; ++i) { cpcs[i] = corr_constrain(x[pos++], lp); + } Eigen::Array sds(k); - for (size_t i = 0; i < k; ++i) + for (size_t i = 0; i < k; ++i) { sds[i] = positive_constrain(x[pos++], lp); + } return read_cov_matrix(cpcs, sds, lp); } diff --git a/stan/math/prim/mat/fun/cov_matrix_free.hpp b/stan/math/prim/mat/fun/cov_matrix_free.hpp index 5ff3ed646ea..efb5206c4c3 100644 --- a/stan/math/prim/mat/fun/cov_matrix_free.hpp +++ b/stan/math/prim/mat/fun/cov_matrix_free.hpp @@ -41,8 +41,9 @@ Eigen::Matrix cov_matrix_free( using std::log; int K = y.rows(); - for (int k = 0; k < K; ++k) + for (int k = 0; k < K; ++k) { check_positive("cov_matrix_free", "y", y(k, k)); + } Eigen::Matrix x((K * (K + 1)) / 2); // FIXME: see Eigen LDLT for rank-revealing version -- use that // even if less efficient? @@ -51,8 +52,9 @@ Eigen::Matrix cov_matrix_free( Eigen::Matrix L = llt.matrixL(); int i = 0; for (int m = 0; m < K; ++m) { - for (int n = 0; n < m; ++n) + for (int n = 0; n < m; ++n) { x(i++) = L(m, n); + } x(i++) = log(L(m, m)); } return x; diff --git a/stan/math/prim/mat/fun/cov_matrix_free_lkj.hpp b/stan/math/prim/mat/fun/cov_matrix_free_lkj.hpp index 725af0df6ff..8a926d81edd 100644 --- a/stan/math/prim/mat/fun/cov_matrix_free_lkj.hpp +++ b/stan/math/prim/mat/fun/cov_matrix_free_lkj.hpp @@ -43,15 +43,18 @@ Eigen::Matrix cov_matrix_free_lkj( Array cpcs(k_choose_2); Array sds(k); bool successful = factor_cov_matrix(y, cpcs, sds); - if (!successful) + if (!successful) { domain_error("cov_matrix_free_lkj", "factor_cov_matrix failed on y", "", ""); + } Matrix x(k_choose_2 + k); size_type pos = 0; - for (size_type i = 0; i < k_choose_2; ++i) + for (size_type i = 0; i < k_choose_2; ++i) { x[pos++] = cpcs[i]; - for (size_type i = 0; i < k; ++i) + } + for (size_type i = 0; i < k; ++i) { x[pos++] = sds[i]; + } return x; } diff --git a/stan/math/prim/mat/fun/csr_extract_u.hpp b/stan/math/prim/mat/fun/csr_extract_u.hpp index a22cce3c765..517c1b6a30f 100644 --- a/stan/math/prim/mat/fun/csr_extract_u.hpp +++ b/stan/math/prim/mat/fun/csr_extract_u.hpp @@ -24,8 +24,9 @@ template const std::vector csr_extract_u( const Eigen::SparseMatrix& A) { std::vector u(A.outerSize() + 1); // last entry is garbage. - for (int nze = 0; nze <= A.outerSize(); ++nze) + for (int nze = 0; nze <= A.outerSize(); ++nze) { u[nze] = *(A.outerIndexPtr() + nze) + stan::error_index::value; + } return u; } diff --git a/stan/math/prim/mat/fun/csr_extract_v.hpp b/stan/math/prim/mat/fun/csr_extract_v.hpp index 7b0834760a3..66208dcb75e 100644 --- a/stan/math/prim/mat/fun/csr_extract_v.hpp +++ b/stan/math/prim/mat/fun/csr_extract_v.hpp @@ -25,8 +25,9 @@ template const std::vector csr_extract_v( const Eigen::SparseMatrix& A) { std::vector v(A.nonZeros()); - for (int nze = 0; nze < A.nonZeros(); ++nze) + for (int nze = 0; nze < A.nonZeros(); ++nze) { v[nze] = *(A.innerIndexPtr() + nze) + stan::error_index::value; + } return v; } diff --git a/stan/math/prim/mat/fun/csr_extract_w.hpp b/stan/math/prim/mat/fun/csr_extract_w.hpp index 5d8d67d4d46..07d270dd542 100644 --- a/stan/math/prim/mat/fun/csr_extract_w.hpp +++ b/stan/math/prim/mat/fun/csr_extract_w.hpp @@ -21,8 +21,9 @@ const Eigen::Matrix csr_extract_w( const Eigen::SparseMatrix& A) { Eigen::Matrix w(A.nonZeros()); w.setZero(); - for (int nze = 0; nze < A.nonZeros(); ++nze) + for (int nze = 0; nze < A.nonZeros(); ++nze) { w[nze] = *(A.valuePtr() + nze); + } return w; } diff --git a/stan/math/prim/mat/fun/csr_matrix_times_vector.hpp b/stan/math/prim/mat/fun/csr_matrix_times_vector.hpp index adb6572527f..fff7befa4f8 100644 --- a/stan/math/prim/mat/fun/csr_matrix_times_vector.hpp +++ b/stan/math/prim/mat/fun/csr_matrix_times_vector.hpp @@ -87,8 +87,9 @@ csr_matrix_times_vector(int m, int n, check_size_match("csr_matrix_times_vector", "w", w.size(), "v", v.size()); check_size_match("csr_matrix_times_vector", "u/z", u[m - 1] + csr_u_to_z(u, m - 1) - 1, "v", v.size()); - for (int i : v) + for (int i : v) { check_range("csr_matrix_times_vector", "v[]", n, i); + } Eigen::Matrix result(m); result.setZero(); diff --git a/stan/math/prim/mat/fun/csr_to_dense_matrix.hpp b/stan/math/prim/mat/fun/csr_to_dense_matrix.hpp index ecd3ee618ba..eca795ab404 100644 --- a/stan/math/prim/mat/fun/csr_to_dense_matrix.hpp +++ b/stan/math/prim/mat/fun/csr_to_dense_matrix.hpp @@ -45,8 +45,9 @@ inline Eigen::Matrix csr_to_dense_matrix( check_size_match("csr_to_dense_matrix", "w", w.size(), "v", v.size()); check_size_match("csr_to_dense_matrix", "u/z", u[m - 1] + csr_u_to_z(u, m - 1) - 1, "v", v.size()); - for (int i : v) + for (int i : v) { check_range("csr_to_dense_matrix", "v[]", n, i); + } Matrix result(m, n); result.setZero(); diff --git a/stan/math/prim/mat/fun/cumulative_sum.hpp b/stan/math/prim/mat/fun/cumulative_sum.hpp index 94d11558438..c7e281d037c 100644 --- a/stan/math/prim/mat/fun/cumulative_sum.hpp +++ b/stan/math/prim/mat/fun/cumulative_sum.hpp @@ -23,8 +23,9 @@ namespace math { template inline std::vector cumulative_sum(const std::vector& x) { std::vector result(x.size()); - if (x.size() == 0) + if (x.size() == 0) { return result; + } std::partial_sum(x.begin(), x.end(), result.begin(), std::plus()); return result; } @@ -46,8 +47,9 @@ inline std::vector cumulative_sum(const std::vector& x) { template inline Eigen::Matrix cumulative_sum(const Eigen::Matrix& m) { Eigen::Matrix result(m.rows(), m.cols()); - if (m.size() == 0) + if (m.size() == 0) { return result; + } std::partial_sum(m.data(), m.data() + m.size(), result.data(), std::plus()); return result; diff --git a/stan/math/prim/mat/fun/dims.hpp b/stan/math/prim/mat/fun/dims.hpp index 174f965269d..283f4b81ad3 100644 --- a/stan/math/prim/mat/fun/dims.hpp +++ b/stan/math/prim/mat/fun/dims.hpp @@ -19,8 +19,9 @@ inline void dims(const Eigen::Matrix& x, std::vector& result) { template inline void dims(const std::vector& x, std::vector& result) { result.push_back(x.size()); - if (x.size() > 0) + if (x.size() > 0) { dims(x[0], result); + } } template diff --git a/stan/math/prim/mat/fun/dot_product.hpp b/stan/math/prim/mat/fun/dot_product.hpp index 34f62068834..4beecd59371 100644 --- a/stan/math/prim/mat/fun/dot_product.hpp +++ b/stan/math/prim/mat/fun/dot_product.hpp @@ -33,8 +33,9 @@ inline double dot_product(const Eigen::Matrix &v1, */ inline double dot_product(const double *v1, const double *v2, size_t length) { double result = 0; - for (size_t i = 0; i < length; i++) + for (size_t i = 0; i < length; i++) { result += v1[i] * v2[i]; + } return result; } /** diff --git a/stan/math/prim/mat/fun/factor_cov_matrix.hpp b/stan/math/prim/mat/fun/factor_cov_matrix.hpp index 5d5c1f65802..0326c44968c 100644 --- a/stan/math/prim/mat/fun/factor_cov_matrix.hpp +++ b/stan/math/prim/mat/fun/factor_cov_matrix.hpp @@ -28,8 +28,9 @@ bool factor_cov_matrix( size_t K = sds.rows(); sds = Sigma.diagonal().array(); - if ((sds <= 0.0).any()) + if ((sds <= 0.0).any()) { return false; + } sds = sds.sqrt(); Eigen::DiagonalMatrix D(K); @@ -41,8 +42,9 @@ bool factor_cov_matrix( R.diagonal().setOnes(); Eigen::LDLT > ldlt; ldlt = R.ldlt(); - if (!ldlt.isPositive()) + if (!ldlt.isPositive()) { return false; + } Eigen::Matrix U = ldlt.matrixU(); factor_U(U, CPCs); return true; diff --git a/stan/math/prim/mat/fun/gp_dot_prod_cov.hpp b/stan/math/prim/mat/fun/gp_dot_prod_cov.hpp index fe80d993c2c..fe10a2cc45b 100644 --- a/stan/math/prim/mat/fun/gp_dot_prod_cov.hpp +++ b/stan/math/prim/mat/fun/gp_dot_prod_cov.hpp @@ -51,8 +51,9 @@ gp_dot_prod_cov(const std::vector> &x, Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } T_sigma sigma_sq = square(sigma); @@ -102,8 +103,9 @@ gp_dot_prod_cov(const std::vector &x, const T_sigma &sigma) { Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } T_sigma sigma_sq = square(sigma); @@ -164,8 +166,9 @@ gp_dot_prod_cov(const std::vector> &x1, Eigen::Dynamic> cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } T_sigma sigma_sq = square(sigma); @@ -219,8 +222,9 @@ gp_dot_prod_cov(const std::vector &x1, const std::vector &x2, Eigen::Dynamic> cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } T_sigma sigma_sq = square(sigma); diff --git a/stan/math/prim/mat/fun/gp_exp_quad_cov.hpp b/stan/math/prim/mat/fun/gp_exp_quad_cov.hpp index 2220d7a7c4b..10e57959d45 100644 --- a/stan/math/prim/mat/fun/gp_exp_quad_cov.hpp +++ b/stan/math/prim/mat/fun/gp_exp_quad_cov.hpp @@ -187,11 +187,13 @@ gp_exp_quad_cov(const std::vector &x, const T_sigma &sigma, Eigen::Dynamic> cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } - for (size_t n = 0; n < x_size; ++n) + for (size_t n = 0; n < x_size; ++n) { check_not_nan("gp_exp_quad_cov", "x", x[n]); + } cov = internal::gp_exp_quad_cov(x, square(sigma), -0.5 / square(length_scale)); @@ -224,8 +226,9 @@ gp_exp_quad_cov(const std::vector> &x, Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } check_size_match("gp_exp_quad_cov", "x dimension", x[0].size(), "number of length scales", length_scale.size()); @@ -268,13 +271,16 @@ gp_exp_quad_cov(const std::vector &x1, const std::vector &x2, Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } - for (size_t i = 0; i < x1_size; ++i) + for (size_t i = 0; i < x1_size; ++i) { check_not_nan(function_name, "x1", x1[i]); - for (size_t i = 0; i < x2_size; ++i) + } + for (size_t i = 0; i < x2_size; ++i) { check_not_nan(function_name, "x2", x2[i]); + } cov = internal::gp_exp_quad_cov(x1, x2, square(sigma), -0.5 / square(length_scale)); @@ -314,14 +320,17 @@ gp_exp_quad_cov(const std::vector> &x1, Eigen::Dynamic> cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } const char *function_name = "gp_exp_quad_cov"; - for (size_t i = 0; i < x1_size; ++i) + for (size_t i = 0; i < x1_size; ++i) { check_not_nan(function_name, "x1", x1[i]); - for (size_t i = 0; i < x2_size; ++i) + } + for (size_t i = 0; i < x2_size; ++i) { check_not_nan(function_name, "x2", x2[i]); + } check_positive_finite(function_name, "magnitude", sigma); check_positive_finite(function_name, "length scale", length_scale); check_size_match(function_name, "x dimension", x1[0].size(), @@ -355,12 +364,14 @@ inline Eigen::MatrixXd gp_exp_quad_cov(const std::vector &x, const auto x_size = x.size(); Eigen::MatrixXd cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } const auto total_size = x_size + cov.size(); if (total_size < opencl_context.tuning_opts().gp_exp_quad_cov_simple) { - for (size_t n = 0; n < x_size; ++n) + for (size_t n = 0; n < x_size; ++n) { check_not_nan("gp_exp_quad_cov", "x", x[n]); + } cov = internal::gp_exp_quad_cov(x, square(sigma), -0.5 / square(length_scale)); @@ -397,14 +408,16 @@ inline Eigen::MatrixXd gp_exp_quad_cov(const std::vector &x, const size_t x_size = x.size(); Eigen::MatrixXd cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } const size_t inner_x1_size = x[0].size(); const auto total_size = x_size * inner_x1_size + cov.size(); if (total_size < opencl_context.tuning_opts().gp_exp_quad_cov_complex) { - for (size_t i = 0; i < x_size; ++i) + for (size_t i = 0; i < x_size; ++i) { check_not_nan("gp_exp_quad_cov", "x", x[i]); + } cov = internal::gp_exp_quad_cov(x, square(sigma), -0.5 / square(length_scale)); return cov; @@ -439,8 +452,9 @@ inline Eigen::MatrixXd gp_exp_quad_cov( const size_t x_size = x.size(); Eigen::MatrixXd cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } const size_t inner_x1_size = x[0].size(); check_size_match(function_name, "x dimension", inner_x1_size, @@ -484,14 +498,17 @@ inline typename Eigen::MatrixXd gp_exp_quad_cov(const std::vector &x1, check_positive_finite(function_name, "length scale", length_scale); Eigen::MatrixXd cov(x1.size(), x2.size()); - if (x1.size() == 0 || x1.size() == 0) + if (x1.size() == 0 || x1.size() == 0) { return cov; + } const auto total_size = x1.size() + x2.size() + cov.size(); if (total_size < opencl_context.tuning_opts().gp_exp_quad_cov_simple) { - for (size_t i = 0; i < x1.size(); ++i) + for (size_t i = 0; i < x1.size(); ++i) { check_not_nan(function_name, "x1", x1[i]); - for (size_t i = 0; i < x2.size(); ++i) + } + for (size_t i = 0; i < x2.size(); ++i) { check_not_nan(function_name, "x2", x2[i]); + } cov = internal::gp_exp_quad_cov(x1, x2, square(sigma), -0.5 / square(length_scale)); @@ -534,18 +551,21 @@ inline typename Eigen::MatrixXd gp_exp_quad_cov( check_positive_finite(function_name, "length scale", length_scale); Eigen::MatrixXd cov(x1.size(), x2.size()); - if (x1.size() == 0 || x1.size() == 0) + if (x1.size() == 0 || x1.size() == 0) { return cov; + } const int x1_inner_size = x1[0].size(); const int x2_inner_size = x1[0].size(); const auto total_size = x1_size * x1_inner_size + x2_size * x2_inner_size + cov.size(); if (total_size < opencl_context.tuning_opts().gp_exp_quad_cov_complex) { - for (size_t i = 0; i < x1.size(); ++i) + for (size_t i = 0; i < x1.size(); ++i) { check_not_nan(function_name, "x1", x1[i]); - for (size_t i = 0; i < x2.size(); ++i) + } + for (size_t i = 0; i < x2.size(); ++i) { check_not_nan(function_name, "x2", x2[i]); + } cov = internal::gp_exp_quad_cov(x1, x2, square(sigma), -0.5 / square(length_scale)); @@ -585,8 +605,9 @@ inline typename Eigen::MatrixXd gp_exp_quad_cov( size_t l_size = length_scale.size(); Eigen::MatrixXd cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } const int x1_inner_size = x1[0].size(); const int x2_inner_size = x1[0].size(); @@ -600,10 +621,12 @@ inline typename Eigen::MatrixXd gp_exp_quad_cov( const auto total_size = x1_size * x1_inner_size + x2_size * x2_inner_size + l_size + cov.size(); if (total_size < opencl_context.tuning_opts().gp_exp_quad_cov_complex) { - for (size_t i = 0; i < x1_size; ++i) + for (size_t i = 0; i < x1_size; ++i) { check_not_nan(function_name, "x1", x1[i]); - for (size_t i = 0; i < x2_size; ++i) + } + for (size_t i = 0; i < x2_size; ++i) { check_not_nan(function_name, "x1", x2[i]); + } cov = internal::gp_exp_quad_cov(divide_columns(x1, length_scale), divide_columns(x2, length_scale), square(sigma)); diff --git a/stan/math/prim/mat/fun/gp_exponential_cov.hpp b/stan/math/prim/mat/fun/gp_exponential_cov.hpp index 15db9444ff9..a3ca2efe711 100644 --- a/stan/math/prim/mat/fun/gp_exponential_cov.hpp +++ b/stan/math/prim/mat/fun/gp_exponential_cov.hpp @@ -42,17 +42,20 @@ gp_exponential_cov(const std::vector &x, const T_s &sigma, size_t x_size = size_of(x); Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } const char *function = "gp_exponential_cov"; size_t x_obs_size = size_of(x[0]); - for (size_t i = 0; i < x_size; ++i) + for (size_t i = 0; i < x_size; ++i) { check_size_match(function, "x row", x_obs_size, "x's other row", size_of(x[i])); + } - for (size_t i = 0; i < x_size; ++i) + for (size_t i = 0; i < x_size; ++i) { check_not_nan(function, "x", x[i]); + } check_positive_finite(function, "magnitude", sigma); check_positive_finite(function, "length scale", length_scale); @@ -97,12 +100,14 @@ gp_exponential_cov(const std::vector> &x, size_t x_size = size_of(x); Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } const char *function = "gp_exponential_cov"; - for (size_t n = 0; n < x_size; ++n) + for (size_t n = 0; n < x_size; ++n) { check_not_nan(function, "x", x[n]); + } check_positive_finite(function, "magnitude", sigma); check_positive_finite(function, "length scale", length_scale); @@ -159,22 +164,27 @@ gp_exponential_cov(const std::vector &x1, const std::vector &x2, Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } const char *function = "gp_exponential_cov"; size_t x1_obs_size = size_of(x1[0]); - for (size_t i = 0; i < x1_size; ++i) + for (size_t i = 0; i < x1_size; ++i) { check_size_match(function, "x1's row", x1_obs_size, "x1's other row", size_of(x1[i])); - for (size_t i = 0; i < x2_size; ++i) + } + for (size_t i = 0; i < x2_size; ++i) { check_size_match(function, "x1's row", x1_obs_size, "x2's other row", size_of(x2[i])); + } - for (size_t n = 0; n < x1_size; ++n) + for (size_t n = 0; n < x1_size; ++n) { check_not_nan(function, "x1", x1[n]); - for (size_t n = 0; n < x2_size; ++n) + } + for (size_t n = 0; n < x2_size; ++n) { check_not_nan(function, "x2", x2[n]); + } check_positive_finite(function, "magnitude", sigma); check_positive_finite(function, "length scale", length_scale); @@ -225,24 +235,29 @@ gp_exponential_cov(const std::vector> &x1, Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } const char *function = "gp_exponential_cov"; - for (size_t n = 0; n < x1_size; ++n) + for (size_t n = 0; n < x1_size; ++n) { check_not_nan(function, "x1", x1[n]); - for (size_t n = 0; n < x2_size; ++n) + } + for (size_t n = 0; n < x2_size; ++n) { check_not_nan(function, "x2", x2[n]); + } check_positive_finite(function, "magnitude", sigma); check_positive_finite(function, "length scale", length_scale); - for (size_t i = 0; i < x1_size; ++i) + for (size_t i = 0; i < x1_size; ++i) { check_size_match(function, "x1's row", size_of(x1[i]), "number of length scales", l_size); - for (size_t i = 0; i < x2_size; ++i) + } + for (size_t i = 0; i < x2_size; ++i) { check_size_match(function, "x2's row", size_of(x2[i]), "number of length scales", l_size); + } T_s sigma_sq = square(sigma); T_l temp; diff --git a/stan/math/prim/mat/fun/gp_matern32_cov.hpp b/stan/math/prim/mat/fun/gp_matern32_cov.hpp index 1324e8c5560..8e6d3c2398a 100644 --- a/stan/math/prim/mat/fun/gp_matern32_cov.hpp +++ b/stan/math/prim/mat/fun/gp_matern32_cov.hpp @@ -48,17 +48,20 @@ gp_matern32_cov(const std::vector &x, const T_s &sigma, Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } const char *function = "gp_matern32_cov"; size_t x_obs_size = size_of(x[0]); - for (size_t i = 0; i < x_size; ++i) + for (size_t i = 0; i < x_size; ++i) { check_size_match(function, "x row", x_obs_size, "x's other row", size_of(x[i])); + } - for (size_t n = 0; n < x_size; ++n) + for (size_t n = 0; n < x_size; ++n) { check_not_nan(function, "x", x[n]); + } check_positive_finite(function, "magnitude", sigma); check_positive_finite(function, "length scale", length_scale); @@ -108,18 +111,21 @@ gp_matern32_cov(const std::vector> &x, Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } const char *function = "gp_matern32_cov"; size_t l_size = length_scale.size(); - for (size_t n = 0; n < x_size; ++n) + for (size_t n = 0; n < x_size; ++n) { check_not_nan(function, "x", x[n]); + } check_positive_finite(function, "magnitude", sigma); check_positive_finite(function, "length scale", length_scale); - for (size_t n = 0; n < x_size; ++n) + for (size_t n = 0; n < x_size; ++n) { check_not_nan(function, "length scale", length_scale[n]); + } check_size_match(function, "x dimension", size_of(x[0]), "number of length scales", l_size); @@ -178,22 +184,27 @@ gp_matern32_cov(const std::vector &x1, const std::vector &x2, Eigen::Dynamic> cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } const char *function = "gp_matern32_cov"; size_t x1_obs_size = size_of(x1[0]); - for (size_t i = 0; i < x1_size; ++i) + for (size_t i = 0; i < x1_size; ++i) { check_size_match(function, "x1's row", x1_obs_size, "x1's other row", size_of(x1[i])); - for (size_t i = 0; i < x2_size; ++i) + } + for (size_t i = 0; i < x2_size; ++i) { check_size_match(function, "x1's row", x1_obs_size, "x2's other row", size_of(x2[i])); + } - for (size_t n = 0; n < x1_size; ++n) + for (size_t n = 0; n < x1_size; ++n) { check_not_nan(function, "x1", x1[n]); - for (size_t n = 0; n < x2_size; ++n) + } + for (size_t n = 0; n < x2_size; ++n) { check_not_nan(function, "x2", x2[n]); + } check_positive_finite(function, "magnitude", sigma); check_positive_finite(function, "length scale", length_scale); @@ -250,28 +261,34 @@ gp_matern32_cov(const std::vector> &x1, Eigen::Dynamic> cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } const char *function = "gp_matern_32_cov"; - for (size_t n = 0; n < x1_size; ++n) + for (size_t n = 0; n < x1_size; ++n) { check_not_nan(function, "x1", x1[n]); - for (size_t n = 0; n < x2_size; ++n) + } + for (size_t n = 0; n < x2_size; ++n) { check_not_nan(function, "x2", x2[n]); + } check_positive_finite(function, "magnitude", sigma); check_positive_finite(function, "length scale", length_scale); size_t l_size = length_scale.size(); - for (size_t n = 0; n < l_size; ++n) + for (size_t n = 0; n < l_size; ++n) { check_not_nan(function, "length scale", length_scale[n]); + } - for (size_t i = 0; i < x1_size; ++i) + for (size_t i = 0; i < x1_size; ++i) { check_size_match(function, "x1's row", size_of(x1[i]), "number of length scales", l_size); - for (size_t i = 0; i < x2_size; ++i) + } + for (size_t i = 0; i < x2_size; ++i) { check_size_match(function, "x2's row", size_of(x2[i]), "number of length scales", l_size); + } T_s sigma_sq = square(sigma); double root_3 = sqrt(3.0); diff --git a/stan/math/prim/mat/fun/gp_matern52_cov.hpp b/stan/math/prim/mat/fun/gp_matern52_cov.hpp index 4cbbf2d5203..5f1fa5d310c 100644 --- a/stan/math/prim/mat/fun/gp_matern52_cov.hpp +++ b/stan/math/prim/mat/fun/gp_matern52_cov.hpp @@ -49,11 +49,13 @@ gp_matern52_cov(const std::vector &x, const T_s &sigma, Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } - for (size_t n = 0; n < x_size; ++n) + for (size_t n = 0; n < x_size; ++n) { check_not_nan("gp_matern52_cov", "x", x[n]); + } check_positive_finite("gp_matern52_cov", "magnitude", sigma); check_positive_finite("gp_matern52_cov", "length scale", length_scale); @@ -110,12 +112,14 @@ gp_matern52_cov(const std::vector> &x, size_t x_size = x.size(); Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } size_t l_size = length_scale.size(); - for (size_t n = 0; n < x_size; ++n) + for (size_t n = 0; n < x_size; ++n) { check_not_nan("gp_matern52_cov", "x", x[n]); + } check_positive_finite("gp_matern52_cov", "magnitude", sigma); check_positive_finite("gp_matern52_cov", "length scale", length_scale); @@ -179,13 +183,16 @@ gp_matern52_cov(const std::vector &x1, const std::vector &x2, Eigen::Dynamic> cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } - for (size_t n = 0; n < x1_size; ++n) + for (size_t n = 0; n < x1_size; ++n) { check_not_nan("gp_matern52_cov", "x1", x1[n]); - for (size_t n = 0; n < x2_size; ++n) + } + for (size_t n = 0; n < x2_size; ++n) { check_not_nan("gp_matern52_cov", "x1", x2[n]); + } check_positive_finite("gp_matern52_cov", "magnitude", sigma); check_positive_finite("gp_matern52_cov", "length scale", length_scale); @@ -247,15 +254,18 @@ gp_matern52_cov(const std::vector> &x1, Eigen::Dynamic> cov(x1_size, x2_size); - if (x1_size == 0 || x2_size == 0) + if (x1_size == 0 || x2_size == 0) { return cov; + } size_t l_size = length_scale.size(); - for (size_t n = 0; n < x1_size; ++n) + for (size_t n = 0; n < x1_size; ++n) { check_not_nan("gp_matern52_cov", "x1", x1[n]); - for (size_t n = 0; n < x2_size; ++n) + } + for (size_t n = 0; n < x2_size; ++n) { check_not_nan("gp_matern52_cov", "x1", x2[n]); + } check_positive_finite("gp_matern52_cov", "magnitude", sigma); check_positive_finite("gp_matern52_cov", "length scale", length_scale); diff --git a/stan/math/prim/mat/fun/gp_periodic_cov.hpp b/stan/math/prim/mat/fun/gp_periodic_cov.hpp index 7693e0b74b6..f9e2a4f2d7e 100644 --- a/stan/math/prim/mat/fun/gp_periodic_cov.hpp +++ b/stan/math/prim/mat/fun/gp_periodic_cov.hpp @@ -55,16 +55,18 @@ gp_periodic_cov(const std::vector &x, const T_sigma &sigma, const T_l &l, check_positive(fun, "signal standard deviation", sigma); check_positive(fun, "length-scale", l); check_positive(fun, "period", p); - for (size_t n = 0; n < x.size(); ++n) + for (size_t n = 0; n < x.size(); ++n) { check_not_nan(fun, "element of x", x[n]); + } Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x.size(), x.size()); size_t x_size = x.size(); - if (x_size == 0) + if (x_size == 0) { return cov; + } T_sigma sigma_sq = square(sigma); T_l neg_two_inv_l_sq = -2.0 * inv_square(l); @@ -124,16 +126,19 @@ gp_periodic_cov(const std::vector &x1, const std::vector &x2, check_positive(fun, "signal standard deviation", sigma); check_positive(fun, "length-scale", l); check_positive(fun, "period", p); - for (size_t n = 0; n < x1.size(); ++n) + for (size_t n = 0; n < x1.size(); ++n) { check_not_nan(fun, "element of x1", x1[n]); - for (size_t n = 0; n < x2.size(); ++n) + } + for (size_t n = 0; n < x2.size(); ++n) { check_not_nan(fun, "element of x2", x2[n]); + } Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov(x1.size(), x2.size()); - if (x1.size() == 0 || x2.size() == 0) + if (x1.size() == 0 || x2.size() == 0) { return cov; + } T_sigma sigma_sq = square(sigma); T_l neg_two_inv_l_sq = -2.0 * inv_square(l); diff --git a/stan/math/prim/mat/fun/head.hpp b/stan/math/prim/mat/fun/head.hpp index bc3562e018d..8138793a467 100644 --- a/stan/math/prim/mat/fun/head.hpp +++ b/stan/math/prim/mat/fun/head.hpp @@ -23,8 +23,9 @@ namespace math { template inline Eigen::Matrix head( const Eigen::Matrix& v, size_t n) { - if (n != 0) + if (n != 0) { check_row_index("head", "n", v, n); + } return v.head(n); } @@ -41,8 +42,9 @@ inline Eigen::Matrix head( template inline Eigen::Matrix head( const Eigen::Matrix& rv, size_t n) { - if (n != 0) + if (n != 0) { check_column_index("head", "n", rv, n); + } return rv.head(n); } @@ -58,12 +60,14 @@ inline Eigen::Matrix head( */ template std::vector head(const std::vector& sv, size_t n) { - if (n != 0) + if (n != 0) { check_std_vector_index("head", "n", sv, n); + } std::vector s; - for (size_t i = 0; i < n; ++i) + for (size_t i = 0; i < n; ++i) { s.push_back(sv[i]); + } return s; } diff --git a/stan/math/prim/mat/fun/initialize.hpp b/stan/math/prim/mat/fun/initialize.hpp index fe545469108..998f17450b5 100644 --- a/stan/math/prim/mat/fun/initialize.hpp +++ b/stan/math/prim/mat/fun/initialize.hpp @@ -22,13 +22,15 @@ inline void initialize(T& x, V v) { } template inline void initialize(Eigen::Matrix& x, const V& v) { - for (int i = 0; i < x.size(); ++i) + for (int i = 0; i < x.size(); ++i) { initialize(x(i), v); + } } template inline void initialize(std::vector& x, const V& v) { - for (size_t i = 0; i < x.size(); ++i) + for (size_t i = 0; i < x.size(); ++i) { initialize(x[i], v); + } } } // namespace math diff --git a/stan/math/prim/mat/fun/inverse_spd.hpp b/stan/math/prim/mat/fun/inverse_spd.hpp index d5c982a3463..8647d1db1e2 100644 --- a/stan/math/prim/mat/fun/inverse_spd.hpp +++ b/stan/math/prim/mat/fun/inverse_spd.hpp @@ -24,14 +24,18 @@ inline Eigen::Matrix inverse_spd( check_symmetric("inverse_spd", "m", m); Matrix mmt = T(0.5) * (m + m.transpose()); LDLT > ldlt(mmt); - if (ldlt.info() != Eigen::Success) + if (ldlt.info() != Eigen::Success) { domain_error("invese_spd", "LDLT factor failed", "", ""); - if (!ldlt.isPositive()) + } + if (!ldlt.isPositive()) { domain_error("invese_spd", "matrix not positive definite", "", ""); + } Matrix diag_ldlt = ldlt.vectorD(); - for (int i = 0; i < diag_ldlt.size(); ++i) - if (diag_ldlt(i) <= 0) + for (int i = 0; i < diag_ldlt.size(); ++i) { + if (diag_ldlt(i) <= 0) { domain_error("invese_spd", "matrix not positive definite", "", ""); + } + } return ldlt.solve(Eigen::Matrix::Identity( m.rows(), m.cols())); } diff --git a/stan/math/prim/mat/fun/log_mix.hpp b/stan/math/prim/mat/fun/log_mix.hpp index e9a48d65c47..e53f0e22b80 100644 --- a/stan/math/prim/mat/fun/log_mix.hpp +++ b/stan/math/prim/mat/fun/log_mix.hpp @@ -56,13 +56,15 @@ return_type_t log_mix(const T_theta& theta, scalar_seq_view theta_vec(theta); T_partials_vec theta_dbl(N); - for (int n = 0; n < N; ++n) + for (int n = 0; n < N; ++n) { theta_dbl[n] = value_of(theta_vec[n]); + } scalar_seq_view lam_vec(lambda); T_partials_vec lam_dbl(N); - for (int n = 0; n < N; ++n) + for (int n = 0; n < N; ++n) { lam_dbl[n] = value_of(lam_vec[n]); + } T_partials_return logp = log_sum_exp((log(theta_dbl) + lam_dbl).eval()); @@ -73,13 +75,15 @@ return_type_t log_mix(const T_theta& theta, operands_and_partials ops_partials(theta, lambda); if (!is_constant_all::value) { - for (int n = 0; n < N; ++n) + for (int n = 0; n < N; ++n) { ops_partials.edge1_.partials_[n] = theta_deriv[n]; + } } if (!is_constant_all::value) { - for (int n = 0; n < N; ++n) + for (int n = 0; n < N; ++n) { ops_partials.edge2_.partials_[n] = lam_deriv[n]; + } } return ops_partials.build(logp); @@ -141,20 +145,24 @@ return_type_t > > log_mix( scalar_seq_view theta_vec(theta); T_partials_vec theta_dbl(M); - for (int m = 0; m < M; ++m) + for (int m = 0; m < M; ++m) { theta_dbl[m] = value_of(theta_vec[m]); + } T_partials_mat lam_dbl(M, N); vector_seq_view lam_vec(lambda); - for (int n = 0; n < N; ++n) - for (int m = 0; m < M; ++m) + for (int n = 0; n < N; ++n) { + for (int m = 0; m < M; ++m) { lam_dbl(m, n) = value_of(lam_vec[n][m]); + } + } T_partials_mat logp_tmp = log(theta_dbl).replicate(1, N) + lam_dbl; T_partials_vec logp(N); - for (int n = 0; n < N; ++n) + for (int n = 0; n < N; ++n) { logp[n] = log_sum_exp(logp_tmp.col(n).eval()); + } operands_and_partials ops_partials(theta, lambda); @@ -163,14 +171,16 @@ return_type_t > > log_mix( = (lam_dbl - logp.transpose().replicate(M, 1)) .unaryExpr([](T_partials_return x) { return exp(x); }); if (!is_constant_all::value) { - for (int m = 0; m < M; ++m) + for (int m = 0; m < M; ++m) { ops_partials.edge1_.partials_[m] = derivs.row(m).sum(); + } } if (!is_constant_all::value) { - for (int n = 0; n < N; ++n) + for (int n = 0; n < N; ++n) { ops_partials.edge2_.partials_vec_[n] = derivs.col(n).cwiseProduct(theta_dbl); + } } } return ops_partials.build(logp.sum()); @@ -229,38 +239,46 @@ return_type_t > > log_mix( scalar_seq_view theta_vec(theta); T_partials_vec theta_dbl(M); - for (int m = 0; m < M; ++m) + for (int m = 0; m < M; ++m) { theta_dbl[m] = value_of(theta_vec[m]); + } T_partials_mat lam_dbl(M, N); - for (int n = 0; n < N; ++n) - for (int m = 0; m < M; ++m) + for (int n = 0; n < N; ++n) { + for (int m = 0; m < M; ++m) { lam_dbl(m, n) = value_of(lambda[n][m]); + } + } T_partials_mat logp_tmp = log(theta_dbl).replicate(1, N) + lam_dbl; T_partials_vec logp(N); - for (int n = 0; n < N; ++n) + for (int n = 0; n < N; ++n) { logp[n] = log_sum_exp(logp_tmp.col(n).eval()); + } T_partials_mat derivs = (lam_dbl - logp.transpose().replicate(M, 1)) .unaryExpr([](T_partials_return x) { return exp(x); }); T_partials_mat lam_deriv(M, N); - for (int n = 0; n < N; ++n) + for (int n = 0; n < N; ++n) { lam_deriv.col(n) = derivs.col(n).cwiseProduct(theta_dbl); + } operands_and_partials ops_partials(theta, lambda); if (!is_constant_all::value) { - for (int m = 0; m < M; ++m) + for (int m = 0; m < M; ++m) { ops_partials.edge1_.partials_[m] = derivs.row(m).sum(); + } } if (!is_constant_all::value) { - for (int n = 0; n < N; ++n) - for (int m = 0; m < M; ++m) + for (int n = 0; n < N; ++n) { + for (int m = 0; m < M; ++m) { ops_partials.edge2_.partials_vec_[n][m] = lam_deriv(m, n); + } + } } return ops_partials.build(logp.sum()); } diff --git a/stan/math/prim/mat/fun/log_sum_exp.hpp b/stan/math/prim/mat/fun/log_sum_exp.hpp index 988d99321b6..f8fd1c05fdc 100644 --- a/stan/math/prim/mat/fun/log_sum_exp.hpp +++ b/stan/math/prim/mat/fun/log_sum_exp.hpp @@ -25,8 +25,9 @@ namespace math { template double log_sum_exp(const Eigen::Matrix& x) { const double max = x.maxCoeff(); - if (!std::isfinite(max)) + if (!std::isfinite(max)) { return max; + } return max + std::log((x.array() - max).exp().sum()); } diff --git a/stan/math/prim/mat/fun/make_nu.hpp b/stan/math/prim/mat/fun/make_nu.hpp index 7ccfd5ca8c8..8c597cb6c22 100644 --- a/stan/math/prim/mat/fun/make_nu.hpp +++ b/stan/math/prim/mat/fun/make_nu.hpp @@ -26,14 +26,16 @@ Eigen::Array make_nu(const T& eta, size_t K) { Array nu(K * (K - 1) / 2); T alpha = eta + 0.5 * (K - 2.0); // from Lewandowski et. al. T alpha2 = 2.0 * alpha; - for (size_type j = 0; j < (K - 1); ++j) + for (size_type j = 0; j < (K - 1); ++j) { nu(j) = alpha2; + } size_t counter = K - 1; for (size_type i = 1; i < (K - 1); ++i) { alpha -= 0.5; alpha2 = 2.0 * alpha; - for (size_type j = i + 1; j < K; ++j, ++counter) + for (size_type j = i + 1; j < K; ++j, ++counter) { nu(counter) = alpha2; + } } return nu; } diff --git a/stan/math/prim/mat/fun/matrix_exp_action_handler.hpp b/stan/math/prim/mat/fun/matrix_exp_action_handler.hpp index c435e5750d0..c3dcb0a0acc 100644 --- a/stan/math/prim/mat/fun/matrix_exp_action_handler.hpp +++ b/stan/math/prim/mat/fun/matrix_exp_action_handler.hpp @@ -79,8 +79,9 @@ class matrix_exp_action_handler { } F *= eta; B = F; - if (conv) + if (conv) { break; + } } res.col(col) = F; } // loop b columns diff --git a/stan/math/prim/mat/fun/matrix_exp_pade.hpp b/stan/math/prim/mat/fun/matrix_exp_pade.hpp index 001ddd6938d..c9202ccce6d 100644 --- a/stan/math/prim/mat/fun/matrix_exp_pade.hpp +++ b/stan/math/prim/mat/fun/matrix_exp_pade.hpp @@ -26,8 +26,9 @@ MatrixType matrix_exp_pade(const MatrixType& arg) { MatrixType numer = U + V; MatrixType denom = -U + V; MatrixType pade_approximation = denom.partialPivLu().solve(numer); - for (int i = 0; i < squarings; ++i) + for (int i = 0; i < squarings; ++i) { pade_approximation *= pade_approximation; // undo scaling by + } // repeated squaring return pade_approximation; } diff --git a/stan/math/prim/mat/fun/max.hpp b/stan/math/prim/mat/fun/max.hpp index 7fe6eb865b8..edc8bcf3289 100644 --- a/stan/math/prim/mat/fun/max.hpp +++ b/stan/math/prim/mat/fun/max.hpp @@ -33,8 +33,9 @@ inline int max(const std::vector& x) { */ template inline T max(const std::vector& x) { - if (x.size() == 0) + if (x.size() == 0) { return -std::numeric_limits::infinity(); + } Eigen::Map> m(&x[0], x.size()); return m.maxCoeff(); } @@ -47,8 +48,9 @@ inline T max(const std::vector& x) { */ template inline T max(const Eigen::Matrix& m) { - if (m.size() == 0) + if (m.size() == 0) { return -std::numeric_limits::infinity(); + } return m.maxCoeff(); } diff --git a/stan/math/prim/mat/fun/mdivide_right_tri.hpp b/stan/math/prim/mat/fun/mdivide_right_tri.hpp index 5ca12e19c6d..749395bbeac 100644 --- a/stan/math/prim/mat/fun/mdivide_right_tri.hpp +++ b/stan/math/prim/mat/fun/mdivide_right_tri.hpp @@ -28,10 +28,11 @@ inline Eigen::Matrix, R1, C2> mdivide_right_tri( const Eigen::Matrix &b, const Eigen::Matrix &A) { check_square("mdivide_right_tri", "A", A); check_multiplicable("mdivide_right_tri", "b", b, "A", A); - if (TriView != Eigen::Lower && TriView != Eigen::Upper) + if (TriView != Eigen::Lower && TriView != Eigen::Upper) { domain_error("mdivide_left_tri", "triangular view must be Eigen::Lower or Eigen::Upper", "", ""); + } return promote_common, Eigen::Matrix >( A) .template triangularView() diff --git a/stan/math/prim/mat/fun/min.hpp b/stan/math/prim/mat/fun/min.hpp index 1fd86cce4b6..8ef449c7241 100644 --- a/stan/math/prim/mat/fun/min.hpp +++ b/stan/math/prim/mat/fun/min.hpp @@ -32,8 +32,9 @@ inline int min(const std::vector& x) { */ template inline T min(const std::vector& x) { - if (x.size() == 0) + if (x.size() == 0) { return std::numeric_limits::infinity(); + } Eigen::Map> m(&x[0], x.size()); return m.minCoeff(); } @@ -46,8 +47,9 @@ inline T min(const std::vector& x) { */ template inline T min(const Eigen::Matrix& m) { - if (m.size() == 0) + if (m.size() == 0) { return std::numeric_limits::infinity(); + } return m.minCoeff(); } diff --git a/stan/math/prim/mat/fun/multiply_lower_tri_self_transpose.hpp b/stan/math/prim/mat/fun/multiply_lower_tri_self_transpose.hpp index f174b795939..c1f0c9d6705 100644 --- a/stan/math/prim/mat/fun/multiply_lower_tri_self_transpose.hpp +++ b/stan/math/prim/mat/fun/multiply_lower_tri_self_transpose.hpp @@ -17,8 +17,9 @@ namespace math { */ inline matrix_d multiply_lower_tri_self_transpose(const matrix_d& L) { int K = L.rows(); - if (K == 0) + if (K == 0) { return L; + } if (K == 1) { matrix_d result(1, 1); result(0) = square(L(0)); // first elt, so don't need double idx @@ -30,8 +31,9 @@ inline matrix_d multiply_lower_tri_self_transpose(const matrix_d& L) { for (int m = 0; m < K; ++m) { int k = (J < m + 1) ? J : m + 1; LLt(m, m) = Lt.col(m).head(k).squaredNorm(); - for (int n = (m + 1); n < K; ++n) + for (int n = (m + 1); n < K; ++n) { LLt(n, m) = LLt(m, n) = Lt.col(m).head(k).dot(Lt.col(n).head(k)); + } } return LLt; } diff --git a/stan/math/prim/mat/fun/num_elements.hpp b/stan/math/prim/mat/fun/num_elements.hpp index fe19fec7070..6667567c253 100644 --- a/stan/math/prim/mat/fun/num_elements.hpp +++ b/stan/math/prim/mat/fun/num_elements.hpp @@ -39,8 +39,9 @@ inline int num_elements(const Eigen::Matrix& m) { */ template inline int num_elements(const std::vector& v) { - if (v.size() == 0) + if (v.size() == 0) { return 0; + } return v.size() * num_elements(v[0]); } diff --git a/stan/math/prim/mat/fun/ordered_constrain.hpp b/stan/math/prim/mat/fun/ordered_constrain.hpp index 0e3d4799f47..a7691c49b2b 100644 --- a/stan/math/prim/mat/fun/ordered_constrain.hpp +++ b/stan/math/prim/mat/fun/ordered_constrain.hpp @@ -28,11 +28,13 @@ Eigen::Matrix ordered_constrain( size_type k = x.size(); Matrix y(k); - if (k == 0) + if (k == 0) { return y; + } y[0] = x[0]; - for (size_type i = 1; i < k; ++i) + for (size_type i = 1; i < k; ++i) { y[i] = y[i - 1] + exp(x[i]); + } return y; } @@ -56,8 +58,9 @@ inline Eigen::Matrix ordered_constrain( typedef typename index_type >::type size_type; - for (size_type i = 1; i < x.size(); ++i) + for (size_type i = 1; i < x.size(); ++i) { lp += x(i); + } return ordered_constrain(x); } diff --git a/stan/math/prim/mat/fun/ordered_free.hpp b/stan/math/prim/mat/fun/ordered_free.hpp index 55148ee60c7..c4ec2159aa4 100644 --- a/stan/math/prim/mat/fun/ordered_free.hpp +++ b/stan/math/prim/mat/fun/ordered_free.hpp @@ -32,11 +32,13 @@ Eigen::Matrix ordered_free( size_type k = y.size(); Matrix x(k); - if (k == 0) + if (k == 0) { return x; + } x[0] = y[0]; - for (size_type i = 1; i < k; ++i) + for (size_type i = 1; i < k; ++i) { x[i] = log(y[i] - y[i - 1]); + } return x; } } // namespace math diff --git a/stan/math/prim/mat/fun/positive_ordered_constrain.hpp b/stan/math/prim/mat/fun/positive_ordered_constrain.hpp index a39d4d0c246..e20a0902cb2 100644 --- a/stan/math/prim/mat/fun/positive_ordered_constrain.hpp +++ b/stan/math/prim/mat/fun/positive_ordered_constrain.hpp @@ -27,11 +27,13 @@ Eigen::Matrix positive_ordered_constrain( size_type k = x.size(); Matrix y(k); - if (k == 0) + if (k == 0) { return y; + } y[0] = exp(x[0]); - for (size_type i = 1; i < k; ++i) + for (size_type i = 1; i < k; ++i) { y[i] = y[i - 1] + exp(x[i]); + } return y; } @@ -54,8 +56,9 @@ inline Eigen::Matrix positive_ordered_constrain( using Eigen::Matrix; typedef typename index_type >::type size_type; - for (size_type i = 0; i < x.size(); ++i) + for (size_type i = 0; i < x.size(); ++i) { lp += x(i); + } return positive_ordered_constrain(x); } diff --git a/stan/math/prim/mat/fun/positive_ordered_free.hpp b/stan/math/prim/mat/fun/positive_ordered_free.hpp index 3c777682ffb..f92ae0e2d04 100644 --- a/stan/math/prim/mat/fun/positive_ordered_free.hpp +++ b/stan/math/prim/mat/fun/positive_ordered_free.hpp @@ -33,11 +33,13 @@ Eigen::Matrix positive_ordered_free( "Positive ordered variable", y); size_type k = y.size(); Matrix x(k); - if (k == 0) + if (k == 0) { return x; + } x[0] = log(y[0]); - for (size_type i = 1; i < k; ++i) + for (size_type i = 1; i < k; ++i) { x[i] = log(y[i] - y[i - 1]); + } return x; } } // namespace math diff --git a/stan/math/prim/mat/fun/prod.hpp b/stan/math/prim/mat/fun/prod.hpp index f6963689211..49bba5a4015 100644 --- a/stan/math/prim/mat/fun/prod.hpp +++ b/stan/math/prim/mat/fun/prod.hpp @@ -15,8 +15,9 @@ namespace math { */ template inline T prod(const std::vector& v) { - if (v.size() == 0) + if (v.size() == 0) { return 1; + } Eigen::Map> m(&v[0], v.size()); return m.prod(); } @@ -29,8 +30,9 @@ inline T prod(const std::vector& v) { */ template inline T prod(const Eigen::Matrix& v) { - if (v.size() == 0) + if (v.size() == 0) { return 1.0; + } return v.prod(); } diff --git a/stan/math/prim/mat/fun/promote_elements.hpp b/stan/math/prim/mat/fun/promote_elements.hpp index 0efc831b41a..c598bf5440e 100644 --- a/stan/math/prim/mat/fun/promote_elements.hpp +++ b/stan/math/prim/mat/fun/promote_elements.hpp @@ -27,8 +27,9 @@ struct promote_elements, Eigen::Matrix > { inline static Eigen::Matrix promote( const Eigen::Matrix& u) { Eigen::Matrix t(u.rows(), u.cols()); - for (int i = 0; i < u.size(); ++i) + for (int i = 0; i < u.size(); ++i) { t(i) = promote_elements::promote(u(i)); + } return t; } }; diff --git a/stan/math/prim/mat/fun/promote_scalar.hpp b/stan/math/prim/mat/fun/promote_scalar.hpp index fc37d40b11a..d38173f0e3d 100644 --- a/stan/math/prim/mat/fun/promote_scalar.hpp +++ b/stan/math/prim/mat/fun/promote_scalar.hpp @@ -30,8 +30,9 @@ struct promote_scalar_struct > { const Eigen::Matrix& x) { Eigen::Matrix::type, -1, -1> y(x.rows(), x.cols()); - for (int i = 0; i < x.size(); ++i) + for (int i = 0; i < x.size(); ++i) { y(i) = promote_scalar_struct::apply(x(i)); + } return y; } }; @@ -58,8 +59,9 @@ struct promote_scalar_struct > { const Eigen::Matrix& x) { Eigen::Matrix::type, 1, -1> y(x.rows(), x.cols()); - for (int i = 0; i < x.size(); ++i) + for (int i = 0; i < x.size(); ++i) { y(i) = promote_scalar_struct::apply(x(i)); + } return y; } }; @@ -86,8 +88,9 @@ struct promote_scalar_struct > { const Eigen::Matrix& x) { Eigen::Matrix::type, -1, 1> y(x.rows(), x.cols()); - for (int i = 0; i < x.size(); ++i) + for (int i = 0; i < x.size(); ++i) { y(i) = promote_scalar_struct::apply(x(i)); + } return y; } }; diff --git a/stan/math/prim/mat/fun/qr_Q.hpp b/stan/math/prim/mat/fun/qr_Q.hpp index 6530d4b8930..a1bc3d522d5 100644 --- a/stan/math/prim/mat/fun/qr_Q.hpp +++ b/stan/math/prim/mat/fun/qr_Q.hpp @@ -24,9 +24,11 @@ Eigen::Matrix qr_Q( qr.compute(m); matrix_t Q = qr.householderQ(); const int min_size = std::min(m.rows(), m.cols()); - for (int i = 0; i < min_size; i++) - if (qr.matrixQR().coeff(i, i) < 0) + for (int i = 0; i < min_size; i++) { + if (qr.matrixQR().coeff(i, i) < 0) { Q.col(i) *= -1.0; + } + } return Q; } diff --git a/stan/math/prim/mat/fun/qr_R.hpp b/stan/math/prim/mat/fun/qr_R.hpp index 184c4739ab8..92643739794 100644 --- a/stan/math/prim/mat/fun/qr_R.hpp +++ b/stan/math/prim/mat/fun/qr_R.hpp @@ -23,14 +23,17 @@ Eigen::Matrix qr_R( Eigen::HouseholderQR qr(m.rows(), m.cols()); qr.compute(m); matrix_t R = qr.matrixQR(); - if (m.rows() > m.cols()) + if (m.rows() > m.cols()) { R.bottomRows(m.rows() - m.cols()).setZero(); + } const int min_size = std::min(m.rows(), m.cols()); for (int i = 0; i < min_size; i++) { - for (int j = 0; j < i; j++) + for (int j = 0; j < i; j++) { R.coeffRef(i, j) = 0.0; - if (R(i, i) < 0) + } + if (R(i, i) < 0) { R.row(i) *= -1.0; + } } return R; } diff --git a/stan/math/prim/mat/fun/qr_thin_Q.hpp b/stan/math/prim/mat/fun/qr_thin_Q.hpp index 29ebda9f2f2..211912cb2a0 100644 --- a/stan/math/prim/mat/fun/qr_thin_Q.hpp +++ b/stan/math/prim/mat/fun/qr_thin_Q.hpp @@ -24,9 +24,11 @@ Eigen::Matrix qr_thin_Q( qr.compute(m); const int min_size = std::min(m.rows(), m.cols()); matrix_t Q = qr.householderQ() * matrix_t::Identity(m.rows(), min_size); - for (int i = 0; i < min_size; i++) - if (qr.matrixQR().coeff(i, i) < 0) + for (int i = 0; i < min_size; i++) { + if (qr.matrixQR().coeff(i, i) < 0) { Q.col(i) *= -1.0; + } + } return Q; } diff --git a/stan/math/prim/mat/fun/qr_thin_R.hpp b/stan/math/prim/mat/fun/qr_thin_R.hpp index 9fabc1bae40..5516806c507 100644 --- a/stan/math/prim/mat/fun/qr_thin_R.hpp +++ b/stan/math/prim/mat/fun/qr_thin_R.hpp @@ -25,10 +25,12 @@ Eigen::Matrix qr_thin_R( const int min_size = std::min(m.rows(), m.cols()); matrix_t R = qr.matrixQR().topLeftCorner(min_size, m.cols()); for (int i = 0; i < min_size; i++) { - for (int j = 0; j < i; j++) + for (int j = 0; j < i; j++) { R.coeffRef(i, j) = 0.0; - if (R(i, i) < 0) + } + if (R(i, i) < 0) { R.row(i) *= -1.0; + } } return R; } diff --git a/stan/math/prim/mat/fun/rank.hpp b/stan/math/prim/mat/fun/rank.hpp index fd830959942..7db5d289aee 100644 --- a/stan/math/prim/mat/fun/rank.hpp +++ b/stan/math/prim/mat/fun/rank.hpp @@ -21,9 +21,11 @@ inline int rank(const C& v, int s) { check_range("rank", "v", v.size(), s); --s; // adjust for indexing by one int count = 0; - for (typename index_type::type i = 0; i < v.size(); ++i) - if (v[i] < v[s]) + for (typename index_type::type i = 0; i < v.size(); ++i) { + if (v[i] < v[s]) { ++count; + } + } return count; } diff --git a/stan/math/prim/mat/fun/read_corr_L.hpp b/stan/math/prim/mat/fun/read_corr_L.hpp index ac5f0bc64f4..7223aef952f 100644 --- a/stan/math/prim/mat/fun/read_corr_L.hpp +++ b/stan/math/prim/mat/fun/read_corr_L.hpp @@ -92,11 +92,12 @@ Eigen::Matrix read_corr_L( // no need to abs() because this Jacobian determinant // is strictly positive (and triangular) // see inverse of Jacobian in equation 11 of LKJ paper - for (size_t k = 1; k <= (K - 2); k++) + for (size_t k = 1; k <= (K - 2); k++) { for (size_t i = k + 1; i <= K; i++) { values(pos) = (K - k - 1) * log1m(square(CPCs(pos))); pos++; } + } log_prob += 0.5 * sum(values); return read_corr_L(CPCs, K); diff --git a/stan/math/prim/mat/fun/resize.hpp b/stan/math/prim/mat/fun/resize.hpp index 7ca03765760..94321108ef0 100644 --- a/stan/math/prim/mat/fun/resize.hpp +++ b/stan/math/prim/mat/fun/resize.hpp @@ -21,10 +21,12 @@ template void resize(std::vector& x, const std::vector& dims, int pos) { x.resize(dims[pos]); ++pos; - if (pos >= static_cast(dims.size())) + if (pos >= static_cast(dims.size())) { return; // skips lowest loop to scalar - for (size_t i = 0; i < x.size(); ++i) + } + for (size_t i = 0; i < x.size(); ++i) { resize(x[i], dims, pos); + } } } // namespace internal diff --git a/stan/math/prim/mat/fun/sd.hpp b/stan/math/prim/mat/fun/sd.hpp index a69147ad684..2208e715b21 100644 --- a/stan/math/prim/mat/fun/sd.hpp +++ b/stan/math/prim/mat/fun/sd.hpp @@ -19,8 +19,9 @@ namespace math { template inline return_type_t sd(const std::vector& v) { check_nonzero_size("sd", "v", v); - if (v.size() == 1) + if (v.size() == 1) { return 0.0; + } return sqrt(variance(v)); } @@ -33,8 +34,9 @@ inline return_type_t sd(const std::vector& v) { template inline return_type_t sd(const Eigen::Matrix& m) { check_nonzero_size("sd", "m", m); - if (m.size() == 1) + if (m.size() == 1) { return 0.0; + } return sqrt(variance(m)); } diff --git a/stan/math/prim/mat/fun/segment.hpp b/stan/math/prim/mat/fun/segment.hpp index 97ad55d522f..8e870eec0b2 100644 --- a/stan/math/prim/mat/fun/segment.hpp +++ b/stan/math/prim/mat/fun/segment.hpp @@ -50,8 +50,9 @@ std::vector segment(const std::vector& sv, size_t i, size_t n) { static_cast(sv.size())); } std::vector s; - for (size_t j = 0; j < n; ++j) + for (size_t j = 0; j < n; ++j) { s.push_back(sv[i + j - 1]); + } return s; } diff --git a/stan/math/prim/mat/fun/sort_indices.hpp b/stan/math/prim/mat/fun/sort_indices.hpp index a9772078120..f212bbc04ba 100644 --- a/stan/math/prim/mat/fun/sort_indices.hpp +++ b/stan/math/prim/mat/fun/sort_indices.hpp @@ -39,10 +39,11 @@ class index_comparator { * @param j Index of second value for comparison */ bool operator()(int i, int j) const { - if (ascending) + if (ascending) { return xs_[i - 1] < xs_[j - 1]; - else + } else { return xs_[i - 1] > xs_[j - 1]; + } } }; @@ -64,8 +65,9 @@ std::vector sort_indices(const C& xs) { idx_t size = xs.size(); std::vector idxs; idxs.resize(size); - for (idx_t i = 0; i < size; ++i) + for (idx_t i = 0; i < size; ++i) { idxs[i] = i + 1; + } internal::index_comparator comparator(xs); std::sort(idxs.begin(), idxs.end(), comparator); return idxs; diff --git a/stan/math/prim/mat/fun/stan_print.hpp b/stan/math/prim/mat/fun/stan_print.hpp index 667d53bb2a3..eaedec00076 100644 --- a/stan/math/prim/mat/fun/stan_print.hpp +++ b/stan/math/prim/mat/fun/stan_print.hpp @@ -17,8 +17,9 @@ template void stan_print(std::ostream* o, const std::vector& x) { *o << '['; for (size_t i = 0; i < x.size(); ++i) { - if (i > 0) + if (i > 0) { *o << ','; + } stan_print(o, x[i]); } *o << ']'; @@ -28,8 +29,9 @@ template void stan_print(std::ostream* o, const Eigen::Matrix& x) { *o << '['; for (int i = 0; i < x.size(); ++i) { - if (i > 0) + if (i > 0) { *o << ','; + } stan_print(o, x(i)); } *o << ']'; @@ -39,8 +41,9 @@ template void stan_print(std::ostream* o, const Eigen::Matrix& x) { *o << '['; for (int i = 0; i < x.size(); ++i) { - if (i > 0) + if (i > 0) { *o << ','; + } stan_print(o, x(i)); } *o << ']'; @@ -51,12 +54,14 @@ void stan_print(std::ostream* o, const Eigen::Matrix& x) { *o << '['; for (int i = 0; i < x.rows(); ++i) { - if (i > 0) + if (i > 0) { *o << ','; + } *o << '['; for (int j = 0; j < x.row(i).size(); ++j) { - if (j > 0) + if (j > 0) { *o << ','; + } stan_print(o, x.row(i)(j)); } *o << ']'; diff --git a/stan/math/prim/mat/fun/sub_col.hpp b/stan/math/prim/mat/fun/sub_col.hpp index 37d56c47d8f..88d950fe152 100644 --- a/stan/math/prim/mat/fun/sub_col.hpp +++ b/stan/math/prim/mat/fun/sub_col.hpp @@ -22,8 +22,9 @@ inline Eigen::Matrix sub_col( const Eigen::Matrix& m, size_t i, size_t j, size_t nrows) { check_row_index("sub_col", "i", m, i); - if (nrows > 0) + if (nrows > 0) { check_row_index("sub_col", "i+nrows-1", m, i + nrows - 1); + } check_column_index("sub_col", "j", m, j); return m.block(i - 1, j - 1, nrows, 1); } diff --git a/stan/math/prim/mat/fun/sub_row.hpp b/stan/math/prim/mat/fun/sub_row.hpp index 60b510d8334..72dc22433c0 100644 --- a/stan/math/prim/mat/fun/sub_row.hpp +++ b/stan/math/prim/mat/fun/sub_row.hpp @@ -23,8 +23,9 @@ inline Eigen::Matrix sub_row( size_t j, size_t ncols) { check_row_index("sub_row", "i", m, i); check_column_index("sub_row", "j", m, j); - if (ncols > 0) + if (ncols > 0) { check_column_index("sub_col", "j+ncols-1", m, j + ncols - 1); + } return m.block(i - 1, j - 1, 1, ncols); } diff --git a/stan/math/prim/mat/fun/tail.hpp b/stan/math/prim/mat/fun/tail.hpp index 9bf4306ee79..b481ad94f88 100644 --- a/stan/math/prim/mat/fun/tail.hpp +++ b/stan/math/prim/mat/fun/tail.hpp @@ -24,8 +24,9 @@ namespace math { template inline Eigen::Matrix tail( const Eigen::Matrix& v, size_t n) { - if (n != 0) + if (n != 0) { check_row_index("tail", "n", v, n); + } return v.tail(n); } @@ -42,8 +43,9 @@ inline Eigen::Matrix tail( template inline Eigen::Matrix tail( const Eigen::Matrix& rv, size_t n) { - if (n != 0) + if (n != 0) { check_column_index("tail", "n", rv, n); + } return rv.tail(n); } @@ -60,11 +62,13 @@ inline Eigen::Matrix tail( template std::vector tail(const std::vector& sv, size_t n) { typedef typename index_type >::type idx_t; - if (n != 0) + if (n != 0) { check_std_vector_index("tail", "n", sv, n); + } std::vector s; - for (idx_t i = sv.size() - n; i < sv.size(); ++i) + for (idx_t i = sv.size() - n; i < sv.size(); ++i) { s.push_back(sv[i]); + } return s; } diff --git a/stan/math/prim/mat/fun/tcrossprod.hpp b/stan/math/prim/mat/fun/tcrossprod.hpp index 4fcf10b2221..63e6c9ca5f7 100644 --- a/stan/math/prim/mat/fun/tcrossprod.hpp +++ b/stan/math/prim/mat/fun/tcrossprod.hpp @@ -14,10 +14,12 @@ namespace math { * @return M times its transpose. */ inline matrix_d tcrossprod(const matrix_d& M) { - if (M.rows() == 0) + if (M.rows() == 0) { return matrix_d(0, 0); - if (M.rows() == 1) + } + if (M.rows() == 1) { return M * M.transpose(); + } matrix_d result(M.rows(), M.rows()); return result.setZero().selfadjointView().rankUpdate(M); } diff --git a/stan/math/prim/mat/fun/to_array_1d.hpp b/stan/math/prim/mat/fun/to_array_1d.hpp index a3c95d7e198..40344639653 100644 --- a/stan/math/prim/mat/fun/to_array_1d.hpp +++ b/stan/math/prim/mat/fun/to_array_1d.hpp @@ -16,8 +16,9 @@ inline std::vector to_array_1d(const Eigen::Matrix& matrix) { const T* datap = matrix.data(); int size = matrix.size(); std::vector result(size); - for (int i = 0; i < size; i++) + for (int i = 0; i < size; i++) { result[i] = datap[i]; + } return result; } @@ -33,12 +34,15 @@ inline std::vector::type> to_array_1d( const std::vector >& x) { size_t size1 = x.size(); size_t size2 = 0; - if (size1 != 0) + if (size1 != 0) { size2 = x[0].size(); + } std::vector y(size1 * size2); - for (size_t i = 0, ij = 0; i < size1; i++) - for (size_t j = 0; j < size2; j++, ij++) + for (size_t i = 0, ij = 0; i < size1; i++) { + for (size_t j = 0; j < size2; j++, ij++) { y[ij] = x[i][j]; + } + } return to_array_1d(y); } diff --git a/stan/math/prim/mat/fun/to_array_2d.hpp b/stan/math/prim/mat/fun/to_array_2d.hpp index 7a7433e796c..7751111ae5a 100644 --- a/stan/math/prim/mat/fun/to_array_2d.hpp +++ b/stan/math/prim/mat/fun/to_array_2d.hpp @@ -16,9 +16,11 @@ inline std::vector > to_array_2d( int C = matrix.cols(); int R = matrix.rows(); vector > result(R, vector(C)); - for (int i = 0, ij = 0; i < C; i++) - for (int j = 0; j < R; j++, ij++) + for (int i = 0, ij = 0; i < C; i++) { + for (int j = 0; j < R; j++, ij++) { result[j][i] = datap[ij]; + } + } return result; } diff --git a/stan/math/prim/mat/fun/to_matrix.hpp b/stan/math/prim/mat/fun/to_matrix.hpp index 6e8959dd606..35356745a71 100644 --- a/stan/math/prim/mat/fun/to_matrix.hpp +++ b/stan/math/prim/mat/fun/to_matrix.hpp @@ -39,13 +39,16 @@ template inline Eigen::Matrix to_matrix( const std::vector >& x) { int rows = x.size(); - if (rows == 0) + if (rows == 0) { return Eigen::Matrix(0, 0); + } int cols = x[0].size(); Eigen::Matrix result(rows, cols); - for (int i = 0, ij = 0; i < cols; i++) - for (int j = 0; j < rows; j++, ij++) + for (int i = 0, ij = 0; i < cols; i++) { + for (int j = 0; j < rows; j++, ij++) { result(ij) = x[j][i]; + } + } return result; } @@ -62,15 +65,18 @@ inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> to_matrix(const std::vector >& x) { using boost::math::tools::promote_args; size_t rows = x.size(); - if (rows == 0) + if (rows == 0) { return Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic>(0, 0); + } size_t cols = x[0].size(); Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> result(rows, cols); - for (size_t i = 0, ij = 0; i < cols; i++) - for (size_t j = 0; j < rows; j++, ij++) + for (size_t i = 0, ij = 0; i < cols; i++) { + for (size_t j = 0; j < rows; j++, ij++) { result(ij) = x[j][i]; + } + } return result; } @@ -134,8 +140,9 @@ inline Eigen::Matrix to_matrix( int size = x.size(); check_size_match(function, "rows * columns", m * n, "vector size", size); Eigen::Matrix result(m, n); - for (int i = 0; i < size; i++) + for (int i = 0; i < size; i++) { result(i) = x[i]; + } return result; } @@ -158,14 +165,17 @@ inline Eigen::Matrix to_matrix( template inline Eigen::Matrix to_matrix( const Eigen::Matrix& x, int m, int n, bool col_major) { - if (col_major) + if (col_major) { return to_matrix(x, m, n); + } check_size_match("to_matrix", "rows * columns", m * n, "matrix size", x.size()); Eigen::Matrix result(m, n); - for (int i = 0, ij = 0; i < m; i++) - for (int j = 0; j < n; j++, ij++) + for (int i = 0, ij = 0; i < m; i++) { + for (int j = 0; j < n; j++, ij++) { result(i, j) = x(ij); + } + } return result; } @@ -188,15 +198,18 @@ inline Eigen::Matrix to_matrix( template inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> to_matrix(const std::vector& x, int m, int n, bool col_major) { - if (col_major) + if (col_major) { return to_matrix(x, m, n); + } check_size_match("to_matrix", "rows * columns", m * n, "matrix size", x.size()); Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> result(m, n); - for (int i = 0, ij = 0; i < m; i++) - for (int j = 0; j < n; j++, ij++) + for (int i = 0, ij = 0; i < m; i++) { + for (int j = 0; j < n; j++, ij++) { result(i, j) = x[ij]; + } + } return result; } diff --git a/stan/math/prim/mat/fun/to_row_vector.hpp b/stan/math/prim/mat/fun/to_row_vector.hpp index d873f4d468b..4064d3c027b 100644 --- a/stan/math/prim/mat/fun/to_row_vector.hpp +++ b/stan/math/prim/mat/fun/to_row_vector.hpp @@ -30,8 +30,9 @@ inline Eigen::Matrix to_row_vector( const std::vector& vec) { int C = vec.size(); Eigen::Matrix result(C); - for (int i = 0; i < C; i++) + for (int i = 0; i < C; i++) { result(i) = vec[i]; + } return result; } diff --git a/stan/math/prim/mat/fun/to_vector.hpp b/stan/math/prim/mat/fun/to_vector.hpp index b952ae2a0fa..dce573893f7 100644 --- a/stan/math/prim/mat/fun/to_vector.hpp +++ b/stan/math/prim/mat/fun/to_vector.hpp @@ -30,8 +30,9 @@ inline Eigen::Matrix to_vector( const std::vector& vec) { int R = vec.size(); Eigen::Matrix result(R); - for (int i = 0; i < R; i++) + for (int i = 0; i < R; i++) { result(i) = vec[i]; + } return result; } diff --git a/stan/math/prim/mat/fun/value_of.hpp b/stan/math/prim/mat/fun/value_of.hpp index ff3c3f49166..42f510e8816 100644 --- a/stan/math/prim/mat/fun/value_of.hpp +++ b/stan/math/prim/mat/fun/value_of.hpp @@ -24,9 +24,11 @@ template inline Eigen::Matrix::type, R, C> value_of( const Eigen::Matrix& M) { Eigen::Matrix::type, R, C> Md(M.rows(), M.cols()); - for (int j = 0; j < M.cols(); j++) - for (int i = 0; i < M.rows(); i++) + for (int j = 0; j < M.cols(); j++) { + for (int i = 0; i < M.rows(); i++) { Md(i, j) = value_of(M(i, j)); + } + } return Md; } diff --git a/stan/math/prim/mat/fun/value_of_rec.hpp b/stan/math/prim/mat/fun/value_of_rec.hpp index e2e0c3e2c74..049aff1e7f4 100644 --- a/stan/math/prim/mat/fun/value_of_rec.hpp +++ b/stan/math/prim/mat/fun/value_of_rec.hpp @@ -23,9 +23,11 @@ template inline Eigen::Matrix value_of_rec( const Eigen::Matrix& M) { Eigen::Matrix Md(M.rows(), M.cols()); - for (int j = 0; j < M.cols(); j++) - for (int i = 0; i < M.rows(); i++) + for (int j = 0; j < M.cols(); j++) { + for (int i = 0; i < M.rows(); i++) { Md(i, j) = value_of_rec(M(i, j)); + } + } return Md; } diff --git a/stan/math/prim/mat/fun/variance.hpp b/stan/math/prim/mat/fun/variance.hpp index a6c82cf2996..28ca0bf9723 100644 --- a/stan/math/prim/mat/fun/variance.hpp +++ b/stan/math/prim/mat/fun/variance.hpp @@ -21,8 +21,9 @@ namespace math { template inline return_type_t variance(const std::vector& v) { check_nonzero_size("variance", "v", v); - if (v.size() == 1) + if (v.size() == 1) { return 0.0; + } T v_mean(mean(v)); T sum_sq_diff(0); for (size_t i = 0; i < v.size(); ++i) { @@ -42,8 +43,9 @@ template inline return_type_t variance(const Eigen::Matrix& m) { check_nonzero_size("variance", "m", m); - if (m.size() == 1) + if (m.size() == 1) { return 0.0; + } return_type_t mn(mean(m)); return_type_t sum_sq_diff(0); for (int i = 0; i < m.size(); ++i) { diff --git a/stan/math/prim/mat/fun/welford_covar_estimator.hpp b/stan/math/prim/mat/fun/welford_covar_estimator.hpp index ef7891caea7..b679dfde898 100644 --- a/stan/math/prim/mat/fun/welford_covar_estimator.hpp +++ b/stan/math/prim/mat/fun/welford_covar_estimator.hpp @@ -33,8 +33,9 @@ class welford_covar_estimator { void sample_mean(Eigen::VectorXd& mean) { mean = m_; } void sample_covariance(Eigen::MatrixXd& covar) { - if (num_samples_ > 1) + if (num_samples_ > 1) { covar = m2_ / (num_samples_ - 1.0); + } } protected: diff --git a/stan/math/prim/mat/fun/welford_var_estimator.hpp b/stan/math/prim/mat/fun/welford_var_estimator.hpp index 4b668fe609c..c3cd7784176 100644 --- a/stan/math/prim/mat/fun/welford_var_estimator.hpp +++ b/stan/math/prim/mat/fun/welford_var_estimator.hpp @@ -33,8 +33,9 @@ class welford_var_estimator { void sample_mean(Eigen::VectorXd& mean) { mean = m_; } void sample_variance(Eigen::VectorXd& var) { - if (num_samples_ > 1) + if (num_samples_ > 1) { var = m2_ / (num_samples_ - 1.0); + } } protected: diff --git a/stan/math/prim/mat/functor/map_rect.hpp b/stan/math/prim/mat/functor/map_rect.hpp index bd5102ea000..34dbbb765ce 100644 --- a/stan/math/prim/mat/functor/map_rect.hpp +++ b/stan/math/prim/mat/functor/map_rect.hpp @@ -164,8 +164,9 @@ map_rect(const Eigen::Matrix& shared_params, size_x_i); } - if (job_params_dims[0] == 0) + if (job_params_dims[0] == 0) { return return_t(); + } #ifdef STAN_MPI return internal::map_rect_mpi( diff --git a/stan/math/prim/mat/functor/map_rect_combine.hpp b/stan/math/prim/mat/functor/map_rect_combine.hpp index 8ecd3b04968..2db158d6547 100644 --- a/stan/math/prim/mat/functor/map_rect_combine.hpp +++ b/stan/math/prim/mat/functor/map_rect_combine.hpp @@ -59,8 +59,9 @@ class map_rect_combine { num_shared_operands_(shared_params.rows()), num_job_operands_(dims(job_params)[1]) { ops_partials_.reserve(job_params.size()); - for (const auto& job_param : job_params) + for (const auto& job_param : job_params) { ops_partials_.emplace_back(shared_params, job_param); + } } result_t operator()(const matrix_d& world_result, @@ -74,13 +75,15 @@ class map_rect_combine { for (std::size_t i = 0, ij = 0; i != num_jobs; ++i) { for (int j = 0; j != world_f_out[i]; ++j, ++ij) { - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials_[i].edge1_.partials_ = world_result.block(1, ij, num_shared_operands_, 1); + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials_[i].edge2_.partials_ = world_result.block(offset_job_params, ij, num_job_operands_, 1); + } out(ij) = ops_partials_[i].build(world_result(0, ij)); } diff --git a/stan/math/prim/mat/prob/bernoulli_logit_glm_lpmf.hpp b/stan/math/prim/mat/prob/bernoulli_logit_glm_lpmf.hpp index af456f8dc71..bbf0ac5b965 100644 --- a/stan/math/prim/mat/prob/bernoulli_logit_glm_lpmf.hpp +++ b/stan/math/prim/mat/prob/bernoulli_logit_glm_lpmf.hpp @@ -63,15 +63,18 @@ return_type_t bernoulli_logit_glm_lpmf( check_bounded(function, "Vector of dependent variables", y, 0, 1); check_consistent_size(function, "Vector of dependent variables", y, N); check_consistent_size(function, "Weight vector", beta, M); - if (is_vector::value) + if (is_vector::value) { check_consistent_sizes(function, "Vector of intercepts", alpha, "Vector of dependent variables", y); + } - if (size_zero(y, x, beta)) + if (size_zero(y, x, beta)) { return 0; + } - if (!include_summand::value) + if (!include_summand::value) { return 0; + } T_partials_return logp(0); const auto &x_val = value_of_rec(x); @@ -122,10 +125,11 @@ return_type_t bernoulli_logit_glm_lpmf( = (beta_val_vec * theta_derivative.transpose()).transpose(); } if (!is_constant_all::value) { - if (is_vector::value) + if (is_vector::value) { ops_partials.edge2_.partials_ = theta_derivative; - else + } else { ops_partials.edge2_.partials_[0] = sum(theta_derivative); + } } } return ops_partials.build(logp); diff --git a/stan/math/prim/mat/prob/bernoulli_logit_glm_rng.hpp b/stan/math/prim/mat/prob/bernoulli_logit_glm_rng.hpp index ce981ea9a44..f04255d7145 100644 --- a/stan/math/prim/mat/prob/bernoulli_logit_glm_rng.hpp +++ b/stan/math/prim/mat/prob/bernoulli_logit_glm_rng.hpp @@ -57,8 +57,9 @@ inline typename VectorBuilder::type bernoulli_logit_glm_rng( scalar_seq_view beta_vec(beta); Eigen::VectorXd beta_vector(M); - for (int i = 0; i < M; ++i) + for (int i = 0; i < M; ++i) { beta_vector[i] = beta_vec[i]; + } Eigen::VectorXd x_beta = x * beta_vector; diff --git a/stan/math/prim/mat/prob/categorical_logit_glm_lpmf.hpp b/stan/math/prim/mat/prob/categorical_logit_glm_lpmf.hpp index 6fce70d2b44..f6c3a6d4719 100644 --- a/stan/math/prim/mat/prob/categorical_logit_glm_lpmf.hpp +++ b/stan/math/prim/mat/prob/categorical_logit_glm_lpmf.hpp @@ -64,12 +64,14 @@ categorical_logit_glm_lpmf( check_bounded(function, "categorical outcome out of support", y, 1, N_classes); - if (size_zero(y, x, beta)) + if (size_zero(y, x, beta)) { return 0; + } if (!include_summand::value) + T_beta_scalar>::value) { return 0; + } const auto& x_val = value_of_rec(x); const auto& beta_val = value_of_rec(beta); diff --git a/stan/math/prim/mat/prob/categorical_logit_lpmf.hpp b/stan/math/prim/mat/prob/categorical_logit_lpmf.hpp index 6c44b5ce9c4..eb7c907a1db 100644 --- a/stan/math/prim/mat/prob/categorical_logit_lpmf.hpp +++ b/stan/math/prim/mat/prob/categorical_logit_lpmf.hpp @@ -24,8 +24,9 @@ return_type_t categorical_logit_lpmf( beta.size()); check_finite(function, "log odds parameter", beta); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } // FIXME: wasteful vs. creating term (n-1) if not vectorized return beta(n - 1) - log_sum_exp(beta); // == log_softmax(beta)(n-1); @@ -43,23 +44,27 @@ return_type_t categorical_logit_lpmf( const Eigen::Matrix& beta) { static const char* function = "categorical_logit_lpmf"; - for (const auto& x : ns) + for (const auto& x : ns) { check_bounded(function, "categorical outcome out of support", x, 1, beta.size()); + } check_finite(function, "log odds parameter", beta); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } - if (ns.empty()) + if (ns.empty()) { return 0.0; + } Eigen::Matrix log_softmax_beta = log_softmax(beta); // FIXME: replace with more efficient sum() Eigen::Matrix, Eigen::Dynamic, 1> results(ns.size()); - for (size_t i = 0; i < ns.size(); ++i) + for (size_t i = 0; i < ns.size(); ++i) { results[i] = log_softmax_beta(ns[i] - 1); + } return sum(results); } diff --git a/stan/math/prim/mat/prob/categorical_logit_rng.hpp b/stan/math/prim/mat/prob/categorical_logit_rng.hpp index 3d4f11f96e7..bdad79c396b 100644 --- a/stan/math/prim/mat/prob/categorical_logit_rng.hpp +++ b/stan/math/prim/mat/prob/categorical_logit_rng.hpp @@ -40,8 +40,9 @@ inline int categorical_logit_rng(const Eigen::VectorXd& beta, RNG& rng) { double c = uniform01_rng(); int b = 0; - while (c > index(b)) + while (c > index(b)) { b++; + } return b + 1; } } // namespace math diff --git a/stan/math/prim/mat/prob/categorical_lpmf.hpp b/stan/math/prim/mat/prob/categorical_lpmf.hpp index 01c157017b3..71a52b7e392 100644 --- a/stan/math/prim/mat/prob/categorical_lpmf.hpp +++ b/stan/math/prim/mat/prob/categorical_lpmf.hpp @@ -25,8 +25,9 @@ return_type_t categorical_lpmf( check_bounded(function, "Number of categories", n, lb, theta.size()); check_simplex(function, "Probabilities parameter", theta); - if (include_summand::value) + if (include_summand::value) { return log(theta(n - 1)); + } return 0.0; } @@ -48,26 +49,31 @@ return_type_t categorical_lpmf( int lb = 1; - for (size_t i = 0; i < ns.size(); ++i) + for (size_t i = 0; i < ns.size(); ++i) { check_bounded(function, "element of outcome array", ns[i], lb, theta.size()); + } check_simplex(function, "Probabilities parameter", theta); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } - if (ns.size() == 0) + if (ns.size() == 0) { return 0.0; + } Eigen::Matrix log_theta(theta.size()); - for (int i = 0; i < theta.size(); ++i) + for (int i = 0; i < theta.size(); ++i) { log_theta(i) = log(theta(i)); + } Eigen::Matrix, Eigen::Dynamic, 1> log_theta_ns( ns.size()); - for (size_t i = 0; i < ns.size(); ++i) + for (size_t i = 0; i < ns.size(); ++i) { log_theta_ns(i) = log_theta(ns[i] - 1); + } return sum(log_theta_ns); } diff --git a/stan/math/prim/mat/prob/categorical_rng.hpp b/stan/math/prim/mat/prob/categorical_rng.hpp index 425d885bf3f..c1cc9236d66 100644 --- a/stan/math/prim/mat/prob/categorical_rng.hpp +++ b/stan/math/prim/mat/prob/categorical_rng.hpp @@ -29,8 +29,9 @@ inline int categorical_rng( double c = uniform01_rng(); int b = 0; - while (c > index(b, 0)) + while (c > index(b, 0)) { b++; + } return b + 1; } } // namespace math diff --git a/stan/math/prim/mat/prob/dirichlet_lpmf.hpp b/stan/math/prim/mat/prob/dirichlet_lpmf.hpp index 1eb627bcaaf..3866b9d2cf1 100644 --- a/stan/math/prim/mat/prob/dirichlet_lpmf.hpp +++ b/stan/math/prim/mat/prob/dirichlet_lpmf.hpp @@ -66,11 +66,13 @@ return_type_t dirichlet_lpmf(const T_prob& theta, T_partials_return lp(0.0); - if (include_summand::value) + if (include_summand::value) { lp += lgamma(alpha_dbl.sum()) - lgamma(alpha_dbl).sum(); + } - if (include_summand::value) + if (include_summand::value) { lp += (theta_dbl.array().log() * (alpha_dbl.array() - 1.0)).sum(); + } T_partials_vec theta_deriv = (alpha_dbl.array() - 1.0) / theta_dbl.array(); @@ -79,11 +81,13 @@ return_type_t dirichlet_lpmf(const T_prob& theta, + theta_dbl.array().log(); operands_and_partials ops_partials(theta, alpha); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_ = theta_deriv; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_ = alpha_deriv; + } return ops_partials.build(lp); } diff --git a/stan/math/prim/mat/prob/dirichlet_rng.hpp b/stan/math/prim/mat/prob/dirichlet_rng.hpp index c228bda6f07..10462ba20ae 100644 --- a/stan/math/prim/mat/prob/dirichlet_rng.hpp +++ b/stan/math/prim/mat/prob/dirichlet_rng.hpp @@ -56,8 +56,9 @@ inline Eigen::VectorXd dirichlet_rng( } double log_sum_y = log_sum_exp(log_y); VectorXd theta(alpha.size()); - for (int i = 0; i < alpha.size(); ++i) + for (int i = 0; i < alpha.size(); ++i) { theta(i) = exp(log_y(i) - log_sum_y); + } return theta; } diff --git a/stan/math/prim/mat/prob/gaussian_dlm_obs_lpdf.hpp b/stan/math/prim/mat/prob/gaussian_dlm_obs_lpdf.hpp index 724ed9c3792..0295e30d29a 100644 --- a/stan/math/prim/mat/prob/gaussian_dlm_obs_lpdf.hpp +++ b/stan/math/prim/mat/prob/gaussian_dlm_obs_lpdf.hpp @@ -102,8 +102,9 @@ gaussian_dlm_obs_lpdf( check_pos_definite(function, "C0", C0); check_finite(function, "C0", C0); - if (size_zero(y)) + if (size_zero(y)) { return 0; + } T_lp lp(0); if (include_summand::value) { @@ -257,8 +258,9 @@ gaussian_dlm_obs_lpdf( check_finite(function, "C0", C0); check_not_nan(function, "C0", C0); - if (y.cols() == 0 || y.rows() == 0) + if (y.cols() == 0 || y.rows() == 0) { return 0; + } T_lp lp(0); if (include_summand::value) { diff --git a/stan/math/prim/mat/prob/gaussian_dlm_obs_rng.hpp b/stan/math/prim/mat/prob/gaussian_dlm_obs_rng.hpp index a7723032633..ef507f55348 100644 --- a/stan/math/prim/mat/prob/gaussian_dlm_obs_rng.hpp +++ b/stan/math/prim/mat/prob/gaussian_dlm_obs_rng.hpp @@ -44,8 +44,9 @@ inline Eigen::VectorXd multi_normal_semidefinite_rng( Eigen::VectorXd stddev = S_ldlt.vectorD().array().sqrt().matrix(); size_t M = S_ldlt.vectorD().size(); Eigen::VectorXd z(M); - for (int i = 0; i < M; i++) + for (int i = 0; i < M; i++) { z(i) = stddev(i) * std_normal_rng(); + } Eigen::VectorXd Y = mu + (S_ldlt.transpositionsP().transpose() * (S_ldlt.matrixL() * z)); @@ -81,8 +82,9 @@ inline Eigen::VectorXd multi_normal_definite_rng( size_t M = S_llt.matrixL().rows(); Eigen::VectorXd z(M); - for (int i = 0; i < M; i++) + for (int i = 0; i < M; i++) { z(i) = std_normal_rng(); + } Eigen::VectorXd Y = mu + S_llt.matrixL() * z; diff --git a/stan/math/prim/mat/prob/inv_wishart_lpdf.hpp b/stan/math/prim/mat/prob/inv_wishart_lpdf.hpp index ef95fc72d7e..a48fc3882de 100644 --- a/stan/math/prim/mat/prob/inv_wishart_lpdf.hpp +++ b/stan/math/prim/mat/prob/inv_wishart_lpdf.hpp @@ -68,8 +68,9 @@ return_type_t inv_wishart_lpdf( LDLT_factor ldlt_S(S); check_ldlt_factor(function, "LDLT_Factor of scale parameter", ldlt_S); - if (include_summand::value) + if (include_summand::value) { lp -= lmgamma(k, 0.5 * nu); + } if (include_summand::value) { lp += 0.5 * nu * log_determinant_ldlt(ldlt_S); } @@ -93,8 +94,9 @@ return_type_t inv_wishart_lpdf( S.template selfadjointView()))); lp -= 0.5 * trace(Winv_S); } - if (include_summand::value) + if (include_summand::value) { lp += nu * k * NEG_LOG_TWO_OVER_TWO; + } return lp; } diff --git a/stan/math/prim/mat/prob/lkj_corr_cholesky_lpdf.hpp b/stan/math/prim/mat/prob/lkj_corr_cholesky_lpdf.hpp index 129c3ec315b..ab24fdfa1e8 100644 --- a/stan/math/prim/mat/prob/lkj_corr_cholesky_lpdf.hpp +++ b/stan/math/prim/mat/prob/lkj_corr_cholesky_lpdf.hpp @@ -26,18 +26,21 @@ return_type_t lkj_corr_cholesky_lpdf( check_lower_triangular(function, "Random variable", L); const unsigned int K = L.rows(); - if (K == 0) + if (K == 0) { return 0.0; + } - if (include_summand::value) + if (include_summand::value) { lp += do_lkj_constant(eta, K); + } if (include_summand::value) { const int Km1 = K - 1; Eigen::Matrix log_diagonals = L.diagonal().tail(Km1).array().log(); Eigen::Matrix values(Km1); - for (int k = 0; k < Km1; k++) + for (int k = 0; k < Km1; k++) { values(k) = (Km1 - k - 1) * log_diagonals(k); + } if ((eta == 1.0) && stan::is_constant_all >::value) { lp += sum(values); diff --git a/stan/math/prim/mat/prob/lkj_corr_lpdf.hpp b/stan/math/prim/mat/prob/lkj_corr_lpdf.hpp index 236a23ffe2b..63e1055f103 100644 --- a/stan/math/prim/mat/prob/lkj_corr_lpdf.hpp +++ b/stan/math/prim/mat/prob/lkj_corr_lpdf.hpp @@ -20,20 +20,23 @@ return_type_t do_lkj_constant(const T_shape& eta, if (eta == 1.0) { // C++ integer division is appropriate in this block Eigen::VectorXd denominator(Km1 / 2); - for (int k = 1; k <= denominator.rows(); k++) + for (int k = 1; k <= denominator.rows(); k++) { denominator(k - 1) = lgamma(2.0 * k); + } constant = -denominator.sum(); - if ((K % 2) == 1) + if ((K % 2) == 1) { constant -= 0.25 * (K * K - 1) * LOG_PI - 0.25 * (Km1 * Km1) * LOG_2 - Km1 * lgamma(0.5 * (K + 1)); - else + } else { constant -= 0.25 * K * (K - 2) * LOG_PI + 0.25 * (3 * K * K - 4 * K) * LOG_2 + K * lgamma(0.5 * K) - Km1 * lgamma(static_cast(K)); + } } else { constant = Km1 * lgamma(eta + 0.5 * Km1); - for (int k = 1; k <= Km1; k++) + for (int k = 1; k <= Km1; k++) { constant -= 0.5 * k * LOG_PI + lgamma(eta + 0.5 * (Km1 - k)); + } } return constant; } @@ -53,18 +56,22 @@ return_type_t lkj_corr_lpdf( check_corr_matrix(function, "Correlation matrix", y); const unsigned int K = y.rows(); - if (K == 0) + if (K == 0) { return 0.0; + } - if (include_summand::value) + if (include_summand::value) { lp += do_lkj_constant(eta, K); + } if ((eta == 1.0) - && stan::is_constant_all >::value) + && stan::is_constant_all >::value) { return lp; + } - if (!include_summand::value) + if (!include_summand::value) { return lp; + } Eigen::Matrix values = y.ldlt().vectorD().array().log().matrix(); diff --git a/stan/math/prim/mat/prob/lkj_cov_lpdf.hpp b/stan/math/prim/mat/prob/lkj_cov_lpdf.hpp index 579a886f931..f6f33c58a4a 100644 --- a/stan/math/prim/mat/prob/lkj_cov_lpdf.hpp +++ b/stan/math/prim/mat/prob/lkj_cov_lpdf.hpp @@ -34,9 +34,11 @@ return_type_t lkj_cov_lpdf( check_positive(function, "Shape parameter", eta); check_finite(function, "Location parameter", mu); check_finite(function, "Scale parameter", sigma); - for (int m = 0; m < y.rows(); ++m) - for (int n = 0; n < y.cols(); ++n) + for (int m = 0; m < y.rows(); ++m) { + for (int n = 0; n < y.cols(); ++n) { check_finite(function, "Covariance matrix", y(m, n)); + } + } const unsigned int K = y.rows(); const Eigen::Array sds = y.diagonal().array().sqrt(); diff --git a/stan/math/prim/mat/prob/matrix_normal_prec_lpdf.hpp b/stan/math/prim/mat/prob/matrix_normal_prec_lpdf.hpp index 55c4c16cdb8..58fb2c9760e 100644 --- a/stan/math/prim/mat/prob/matrix_normal_prec_lpdf.hpp +++ b/stan/math/prim/mat/prob/matrix_normal_prec_lpdf.hpp @@ -65,8 +65,9 @@ return_type_t matrix_normal_prec_lpdf( check_finite(function, "Location parameter", Mu); check_finite(function, "Random variable", y); - if (include_summand::value) + if (include_summand::value) { lp += NEG_LOG_SQRT_TWO_PI * y.cols() * y.rows(); + } if (include_summand::value) { lp += log_determinant_ldlt(ldlt_Sigma) * (0.5 * y.rows()); diff --git a/stan/math/prim/mat/prob/multi_gp_cholesky_lpdf.hpp b/stan/math/prim/mat/prob/multi_gp_cholesky_lpdf.hpp index 78b95bcca7c..265db5df237 100644 --- a/stan/math/prim/mat/prob/multi_gp_cholesky_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_gp_cholesky_lpdf.hpp @@ -50,8 +50,9 @@ return_type_t multi_gp_cholesky_lpdf( check_positive(function, "Kernel scales", w); check_finite(function, "Random variable", y); - if (y.rows() == 0) + if (y.rows() == 0) { return 0; + } T_lp lp(0); if (include_summand::value) { diff --git a/stan/math/prim/mat/prob/multi_gp_lpdf.hpp b/stan/math/prim/mat/prob/multi_gp_lpdf.hpp index cf66444e7b6..65712fad205 100644 --- a/stan/math/prim/mat/prob/multi_gp_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_gp_lpdf.hpp @@ -57,8 +57,9 @@ return_type_t multi_gp_lpdf( check_positive_finite(function, "Kernel scales", w); check_finite(function, "Random variable", y); - if (y.rows() == 0) + if (y.rows() == 0) { return lp; + } if (include_summand::value) { lp += NEG_LOG_SQRT_TWO_PI * y.rows() * y.cols(); diff --git a/stan/math/prim/mat/prob/multi_normal_cholesky_lpdf.hpp b/stan/math/prim/mat/prob/multi_normal_cholesky_lpdf.hpp index adaf52f0042..273179b5578 100644 --- a/stan/math/prim/mat/prob/multi_normal_cholesky_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_normal_cholesky_lpdf.hpp @@ -53,8 +53,9 @@ return_type_t multi_normal_cholesky_lpdf( check_consistent_sizes_mvt(function, "y", y, "mu", mu); size_t number_of_y = length_mvt(y); size_t number_of_mu = length_mvt(mu); - if (number_of_y == 0 || number_of_mu == 0) + if (number_of_y == 0 || number_of_mu == 0) { return 0; + } vector_seq_view y_vec(y); vector_seq_view mu_vec(mu); const size_t size_vec = max_size_mvt(y, mu); @@ -102,14 +103,16 @@ return_type_t multi_normal_cholesky_lpdf( check_not_nan(function, "Random variable", y_vec[i]); } - if (unlikely(size_y == 0)) + if (unlikely(size_y == 0)) { return T_return(0); + } T_partials_return logp(0); operands_and_partials ops_partials(y, mu, L); - if (include_summand::value) + if (include_summand::value) { logp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec; + } const matrix_partials_t inv_L_dbl = mdivide_left_tri(value_of(L)); @@ -117,8 +120,9 @@ return_type_t multi_normal_cholesky_lpdf( if (include_summand::value) { for (size_t i = 0; i < size_vec; i++) { vector_partials_t y_minus_mu_dbl(size_y); - for (int j = 0; j < size_y; j++) + for (int j = 0; j < size_y; j++) { y_minus_mu_dbl(j) = value_of(y_vec[i](j)) - value_of(mu_vec[i](j)); + } const row_vector_partials_t half = (inv_L_dbl.template triangularView() * y_minus_mu_dbl) @@ -130,12 +134,14 @@ return_type_t multi_normal_cholesky_lpdf( logp -= 0.5 * dot_self(half); if (!is_constant_all::value) { - for (int j = 0; j < size_y; j++) + for (int j = 0; j < size_y; j++) { ops_partials.edge1_.partials_vec_[i](j) -= scaled_diff(j); + } } if (!is_constant_all::value) { - for (int j = 0; j < size_y; j++) + for (int j = 0; j < size_y; j++) { ops_partials.edge2_.partials_vec_[i](j) += scaled_diff(j); + } } if (!is_constant_all::value) { ops_partials.edge3_.partials_ += scaled_diff * half; diff --git a/stan/math/prim/mat/prob/multi_normal_cholesky_rng.hpp b/stan/math/prim/mat/prob/multi_normal_cholesky_rng.hpp index 0cbf56f7702..25253cfff07 100644 --- a/stan/math/prim/mat/prob/multi_normal_cholesky_rng.hpp +++ b/stan/math/prim/mat/prob/multi_normal_cholesky_rng.hpp @@ -63,8 +63,9 @@ multi_normal_cholesky_rng( for (size_t n = 0; n < N; ++n) { Eigen::VectorXd z(L.cols()); - for (int i = 0; i < L.cols(); i++) + for (int i = 0; i < L.cols(); i++) { z(i) = std_normal_rng(); + } output[n] = Eigen::VectorXd(mu_vec[n]) + L * z; } diff --git a/stan/math/prim/mat/prob/multi_normal_lpdf.hpp b/stan/math/prim/mat/prob/multi_normal_lpdf.hpp index c04a5ed417c..16a6e374e0d 100644 --- a/stan/math/prim/mat/prob/multi_normal_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_normal_lpdf.hpp @@ -35,8 +35,9 @@ return_type_t multi_normal_lpdf(const T_y& y, size_t number_of_y = length_mvt(y); size_t number_of_mu = length_mvt(mu); - if (number_of_y == 0 || number_of_mu == 0) + if (number_of_y == 0 || number_of_mu == 0) { return 0.0; + } check_consistent_sizes_mvt(function, "y", y, "mu", mu); lp_type lp(0.0); @@ -91,21 +92,25 @@ return_type_t multi_normal_lpdf(const T_y& y, check_not_nan(function, "Random variable", y_vec[i]); } - if (size_y == 0) + if (size_y == 0) { return lp; + } - if (include_summand::value) + if (include_summand::value) { lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec; + } - if (include_summand::value) + if (include_summand::value) { lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec; + } if (include_summand::value) { lp_type sum_lp_vec(0.0); for (size_t i = 0; i < size_vec; i++) { Eigen::Matrix, Dynamic, 1> y_minus_mu(size_y); - for (int j = 0; j < size_y; j++) + for (int j = 0; j < size_y; j++) { y_minus_mu(j) = y_vec[i](j) - mu_vec[i](j); + } sum_lp_vec += trace_inv_quad_form_ldlt(ldlt_Sigma, y_minus_mu); } lp -= 0.5 * sum_lp_vec; diff --git a/stan/math/prim/mat/prob/multi_normal_prec_lpdf.hpp b/stan/math/prim/mat/prob/multi_normal_prec_lpdf.hpp index 7b83c88134a..e7436da969f 100644 --- a/stan/math/prim/mat/prob/multi_normal_prec_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_normal_prec_lpdf.hpp @@ -35,8 +35,9 @@ return_type_t multi_normal_prec_lpdf( size_t number_of_y = length_mvt(y); size_t number_of_mu = length_mvt(mu); - if (number_of_y == 0 || number_of_mu == 0) + if (number_of_y == 0 || number_of_mu == 0) { return 0; + } check_consistent_sizes_mvt(function, "y", y, "mu", mu); lp_type lp(0); @@ -91,22 +92,26 @@ return_type_t multi_normal_prec_lpdf( check_not_nan(function, "Random variable", y_vec[i]); } - if (size_y == 0) + if (size_y == 0) { return lp; + } - if (include_summand::value) + if (include_summand::value) { lp += 0.5 * log_determinant_ldlt(ldlt_Sigma) * size_vec; + } - if (include_summand::value) + if (include_summand::value) { lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec; + } if (include_summand::value) { lp_type sum_lp_vec(0.0); for (size_t i = 0; i < size_vec; i++) { Eigen::Matrix, Eigen::Dynamic, 1> y_minus_mu( size_y); - for (int j = 0; j < size_y; j++) + for (int j = 0; j < size_y; j++) { y_minus_mu(j) = y_vec[i](j) - mu_vec[i](j); + } sum_lp_vec += trace_quad_form(Sigma, y_minus_mu); } lp -= 0.5 * sum_lp_vec; diff --git a/stan/math/prim/mat/prob/multi_normal_prec_rng.hpp b/stan/math/prim/mat/prob/multi_normal_prec_rng.hpp index 8d04a4843e1..235eb3a6c15 100644 --- a/stan/math/prim/mat/prob/multi_normal_prec_rng.hpp +++ b/stan/math/prim/mat/prob/multi_normal_prec_rng.hpp @@ -76,8 +76,9 @@ multi_normal_prec_rng(const T_loc &mu, const Eigen::MatrixXd &S, RNG &rng) { for (size_t n = 0; n < N; ++n) { Eigen::VectorXd z(S.cols()); - for (int i = 0; i < S.cols(); i++) + for (int i = 0; i < S.cols(); i++) { z(i) = std_normal_rng(); + } output[n] = Eigen::VectorXd(mu_vec[n]) + llt_of_S.matrixU().solve(z); } diff --git a/stan/math/prim/mat/prob/multi_normal_rng.hpp b/stan/math/prim/mat/prob/multi_normal_rng.hpp index a07dccc34b7..f3ea118a924 100644 --- a/stan/math/prim/mat/prob/multi_normal_rng.hpp +++ b/stan/math/prim/mat/prob/multi_normal_rng.hpp @@ -72,8 +72,9 @@ multi_normal_rng(const T_loc& mu, for (size_t n = 0; n < N; ++n) { Eigen::VectorXd z(S.cols()); - for (int i = 0; i < S.cols(); i++) + for (int i = 0; i < S.cols(); i++) { z(i) = std_normal_rng(); + } output[n] = Eigen::VectorXd(mu_vec[n]) + llt_of_S.matrixL() * z; } diff --git a/stan/math/prim/mat/prob/multi_student_t_lpdf.hpp b/stan/math/prim/mat/prob/multi_student_t_lpdf.hpp index cb378315e9d..51869ed33ba 100644 --- a/stan/math/prim/mat/prob/multi_student_t_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_student_t_lpdf.hpp @@ -38,16 +38,18 @@ return_type_t multi_student_t_lpdf( check_not_nan(function, "Degrees of freedom parameter", nu); check_positive(function, "Degrees of freedom parameter", nu); - if (is_inf(nu)) + if (is_inf(nu)) { return multi_normal_log(y, mu, Sigma); + } using Eigen::Matrix; using std::vector; size_t number_of_y = length_mvt(y); size_t number_of_mu = length_mvt(mu); - if (number_of_y == 0 || number_of_mu == 0) + if (number_of_y == 0 || number_of_mu == 0) { return 0; + } check_consistent_sizes_mvt(function, "y", y, "mu", mu); vector_seq_view y_vec(y); @@ -102,8 +104,9 @@ return_type_t multi_student_t_lpdf( LDLT_factor ldlt_Sigma(Sigma); check_ldlt_factor(function, "LDLT_Factor of scale parameter", ldlt_Sigma); - if (size_y == 0) + if (size_y == 0) { return 0; + } lp_type lp(0); @@ -113,8 +116,9 @@ return_type_t multi_student_t_lpdf( lp -= (0.5 * size_y) * log(nu) * size_vec; } - if (include_summand::value) + if (include_summand::value) { lp -= (0.5 * size_y) * LOG_PI * size_vec; + } using Eigen::Array; @@ -127,8 +131,9 @@ return_type_t multi_student_t_lpdf( for (size_t i = 0; i < size_vec; i++) { Eigen::Matrix, Eigen::Dynamic, 1> y_minus_mu( size_y); - for (int j = 0; j < size_y; j++) + for (int j = 0; j < size_y; j++) { y_minus_mu(j) = y_vec[i](j) - mu_vec[i](j); + } sum_lp_vec += log1p(trace_inv_quad_form_ldlt(ldlt_Sigma, y_minus_mu) / nu); } diff --git a/stan/math/prim/mat/prob/multi_student_t_rng.hpp b/stan/math/prim/mat/prob/multi_student_t_rng.hpp index b2c00d60a0b..93b95bf1c90 100644 --- a/stan/math/prim/mat/prob/multi_student_t_rng.hpp +++ b/stan/math/prim/mat/prob/multi_student_t_rng.hpp @@ -81,8 +81,9 @@ multi_student_t_rng( double w = 1.0 / gamma_rng(); for (size_t n = 0; n < N; ++n) { Eigen::VectorXd z(S.cols()); - for (int i = 0; i < S.cols(); i++) + for (int i = 0; i < S.cols(); i++) { z(i) = std::sqrt(w) * std_normal_rng(); + } output[n] = Eigen::VectorXd(mu_vec[n]) + llt_of_S.matrixL() * z; } diff --git a/stan/math/prim/mat/prob/multinomial_lpmf.hpp b/stan/math/prim/mat/prob/multinomial_lpmf.hpp index ed0a353cbb0..5345c68d573 100644 --- a/stan/math/prim/mat/prob/multinomial_lpmf.hpp +++ b/stan/math/prim/mat/prob/multinomial_lpmf.hpp @@ -29,15 +29,18 @@ return_type_t multinomial_lpmf( if (include_summand::value) { double sum = 1.0; - for (int n : ns) + for (int n : ns) { sum += n; + } lp += lgamma(sum); - for (int n : ns) + for (int n : ns) { lp -= lgamma(n + 1.0); + } } if (include_summand::value) { - for (unsigned int i = 0; i < ns.size(); ++i) + for (unsigned int i = 0; i < ns.size(); ++i) { lp += multiply_log(ns[i], theta[i]); + } } return lp; } diff --git a/stan/math/prim/mat/prob/multinomial_rng.hpp b/stan/math/prim/mat/prob/multinomial_rng.hpp index ea59f0184da..ba293e575ed 100644 --- a/stan/math/prim/mat/prob/multinomial_rng.hpp +++ b/stan/math/prim/mat/prob/multinomial_rng.hpp @@ -23,8 +23,9 @@ inline std::vector multinomial_rng( int n_left = N; for (int k = 0; n_left > 0 && k < theta.size(); ++k) { double p = theta[k] / mass_left; - if (p > 1.0) + if (p > 1.0) { p = 1.0; + } result[k] = binomial_rng(n_left, p, rng); n_left -= result[k]; mass_left -= theta[k]; diff --git a/stan/math/prim/mat/prob/neg_binomial_2_log_glm_lpmf.hpp b/stan/math/prim/mat/prob/neg_binomial_2_log_glm_lpmf.hpp index 4850e74f774..fb00f4ad80d 100644 --- a/stan/math/prim/mat/prob/neg_binomial_2_log_glm_lpmf.hpp +++ b/stan/math/prim/mat/prob/neg_binomial_2_log_glm_lpmf.hpp @@ -85,18 +85,22 @@ return_type_t neg_binomial_2_log_glm_lpmf( check_positive_finite(function, "Precision parameter", phi); check_consistent_size(function, "Vector of dependent variables", y, N); check_consistent_size(function, "Weight vector", beta, M); - if (is_vector::value) + if (is_vector::value) { check_consistent_sizes(function, "Vector of precision parameters", phi, "Vector of dependent variables", y); - if (is_vector::value) + } + if (is_vector::value) { check_consistent_sizes(function, "Vector of intercepts", alpha, "Vector of dependent variables", y); + } - if (size_zero(y, x, beta, phi)) + if (size_zero(y, x, beta, phi)) { return 0; + } - if (!include_summand::value) + if (!include_summand::value) { return 0; + } T_partials_return logp(0); const auto& x_val = value_of_rec(x); @@ -131,18 +135,21 @@ return_type_t neg_binomial_2_log_glm_lpmf( if (include_summand::value) { if (is_vector::value) { scalar_seq_view phi_vec(phi_val); - for (size_t n = 0; n < N; ++n) + for (size_t n = 0; n < N; ++n) { logp += multiply_log(phi_vec[n], phi_vec[n]) - lgamma(phi_vec[n]); + } } else { logp += N * (multiply_log(as_scalar(phi_val), as_scalar(phi_val)) - lgamma(as_scalar(phi_val))); } } - if (include_summand::value) + if (include_summand::value) { logp -= sum(y_plus_phi * logsumexp_theta_logphi); - if (include_summand::value) + } + if (include_summand::value) { logp += sum(y_arr * theta); + } if (include_summand::value) { logp += sum(lgamma(y_plus_phi)); } @@ -163,10 +170,11 @@ return_type_t neg_binomial_2_log_glm_lpmf( = (beta_val_vec * theta_derivative.transpose()).transpose(); } if (!is_constant_all::value) { - if (is_vector::value) + if (is_vector::value) { ops_partials.edge2_.partials_ = theta_derivative; - else + } else { ops_partials.edge2_.partials_[0] = sum(theta_derivative); + } } } if (!is_constant_all::value) { diff --git a/stan/math/prim/mat/prob/normal_id_glm_lpdf.hpp b/stan/math/prim/mat/prob/normal_id_glm_lpdf.hpp index 98673565e9a..b6f38b17f55 100644 --- a/stan/math/prim/mat/prob/normal_id_glm_lpdf.hpp +++ b/stan/math/prim/mat/prob/normal_id_glm_lpdf.hpp @@ -68,17 +68,21 @@ return_type_t normal_id_glm_lpdf( check_positive_finite(function, "Scale vector", sigma); check_consistent_size(function, "Vector of dependent variables", y, N); check_consistent_size(function, "Weight vector", beta, M); - if (is_vector::value) + if (is_vector::value) { check_consistent_sizes(function, "Vector of scale parameters", sigma, "Vector of dependent variables", y); - if (is_vector::value) + } + if (is_vector::value) { check_consistent_sizes(function, "Vector of intercepts", alpha, "Vector of dependent variables", y); - if (size_zero(y, x, beta, sigma)) + } + if (size_zero(y, x, beta, sigma)) { return 0; + } - if (!include_summand::value) + if (!include_summand::value) { return 0; + } const auto &x_val = value_of_rec(x); const auto &beta_val = value_of_rec(beta); @@ -119,10 +123,11 @@ return_type_t normal_id_glm_lpdf( ops_partials.edge4_.partials_ = mu_derivative.transpose() * x_val; } if (!is_constant_all::value) { - if (is_vector::value) + if (is_vector::value) { ops_partials.edge3_.partials_ = mu_derivative; - else + } else { ops_partials.edge3_.partials_[0] = sum(mu_derivative); + } } if (!is_constant_all::value) { if (is_vector::value) { @@ -155,16 +160,19 @@ return_type_t normal_id_glm_lpdf( // Compute log probability. T_partials_return logp(0.0); - if (include_summand::value) + if (include_summand::value) { logp += NEG_LOG_SQRT_TWO_PI * N; + } if (include_summand::value) { - if (is_vector::value) + if (is_vector::value) { logp -= sum(log(sigma_val_vec)); - else + } else { logp -= N * log(as_scalar(sigma_val)); + } } - if (include_summand::value) + if (include_summand::value) { logp -= 0.5 * y_minus_mu_over_sigma_squared_sum; + } return ops_partials.build(logp); } diff --git a/stan/math/prim/mat/prob/ordered_logistic_lpmf.hpp b/stan/math/prim/mat/prob/ordered_logistic_lpmf.hpp index e47ba39bbb7..10ee376f787 100644 --- a/stan/math/prim/mat/prob/ordered_logistic_lpmf.hpp +++ b/stan/math/prim/mat/prob/ordered_logistic_lpmf.hpp @@ -86,9 +86,10 @@ return_type_t ordered_logistic_lpmf(const T_y& y, int C_l = length_mvt(c); check_consistent_sizes(function, "Integers", y, "Locations", lambda); - if (C_l > 1) + if (C_l > 1) { check_size_match(function, "Length of location variables ", N, "Number of cutpoint vectors ", C_l); + } int size_c_old = c_vec[0].size(); for (int i = 1; i < C_l; i++) { @@ -117,29 +118,34 @@ return_type_t ordered_logistic_lpmf(const T_y& y, T_partials_vec c_dbl = value_of(c_vec[0]).template cast(); for (int n = 0; n < N; ++n) { - if (C_l > 1) + if (C_l > 1) { c_dbl = value_of(c_vec[n]).template cast(); + } T_partials_return lam_dbl = value_of(lam_vec[n]); if (y_vec[n] == 1) { logp -= log1p_exp(lam_dbl - c_dbl[0]); T_partials_return d = inv_logit(lam_dbl - c_dbl[0]); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= d; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_vec_[n](0) += d; + } } else if (y_vec[n] == K) { logp -= log1p_exp(c_dbl[K - 2] - lam_dbl); T_partials_return d = inv_logit(c_dbl[K - 2] - lam_dbl); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] = d; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_vec_[n](K - 2) -= d; + } } else { T_partials_return d1 @@ -151,8 +157,9 @@ return_type_t ordered_logistic_lpmf(const T_y& y, logp += log_inv_logit_diff(lam_dbl - c_dbl[y_vec[n] - 2], lam_dbl - c_dbl[y_vec[n] - 1]); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= d1 + d2; + } if (!is_constant_all::value) { ops_partials.edge2_.partials_vec_[n](y_vec[n] - 2) += d1; diff --git a/stan/math/prim/mat/prob/ordered_logistic_rng.hpp b/stan/math/prim/mat/prob/ordered_logistic_rng.hpp index da843f92ea2..5889f071c94 100644 --- a/stan/math/prim/mat/prob/ordered_logistic_rng.hpp +++ b/stan/math/prim/mat/prob/ordered_logistic_rng.hpp @@ -28,8 +28,9 @@ inline int ordered_logistic_rng( Eigen::VectorXd cut(c.rows() + 1); cut(0) = 1 - inv_logit(eta - c(0)); - for (int j = 1; j < c.rows(); j++) + for (int j = 1; j < c.rows(); j++) { cut(j) = inv_logit(eta - c(j - 1)) - inv_logit(eta - c(j)); + } cut(c.rows()) = inv_logit(eta - c(c.rows() - 1)); return categorical_rng(cut, rng); diff --git a/stan/math/prim/mat/prob/ordered_probit_rng.hpp b/stan/math/prim/mat/prob/ordered_probit_rng.hpp index 3d2a5ec9811..72d3d7fdce4 100644 --- a/stan/math/prim/mat/prob/ordered_probit_rng.hpp +++ b/stan/math/prim/mat/prob/ordered_probit_rng.hpp @@ -22,8 +22,9 @@ inline int ordered_probit_rng(double eta, const Eigen::VectorXd& c, RNG& rng) { Eigen::VectorXd cut(c.rows() + 1); cut(0) = 1 - Phi(eta - c(0)); - for (int j = 1; j < c.rows(); j++) + for (int j = 1; j < c.rows(); j++) { cut(j) = Phi(eta - c(j - 1)) - Phi(eta - c(j)); + } cut(c.rows()) = Phi(eta - c(c.rows() - 1)); return categorical_rng(cut, rng); diff --git a/stan/math/prim/mat/prob/poisson_log_glm_lpmf.hpp b/stan/math/prim/mat/prob/poisson_log_glm_lpmf.hpp index 6822e44ee6f..ffce3b853ca 100644 --- a/stan/math/prim/mat/prob/poisson_log_glm_lpmf.hpp +++ b/stan/math/prim/mat/prob/poisson_log_glm_lpmf.hpp @@ -65,14 +65,17 @@ return_type_t poisson_log_glm_lpmf(const T_y& y, check_nonnegative(function, "Vector of dependent variables", y); check_consistent_size(function, "Vector of dependent variables", y, N); check_consistent_size(function, "Weight vector", beta, M); - if (is_vector::value) + if (is_vector::value) { check_consistent_sizes(function, "Vector of intercepts", alpha, "Vector of dependent variables", y); - if (size_zero(y, x, beta)) + } + if (size_zero(y, x, beta)) { return 0; + } - if (!include_summand::value) + if (!include_summand::value) { return 0; + } T_partials_return logp(0); @@ -118,10 +121,11 @@ return_type_t poisson_log_glm_lpmf(const T_y& y, = (beta_val_vec * theta_derivative.transpose()).transpose(); } if (!is_constant_all::value) { - if (is_vector::value) + if (is_vector::value) { ops_partials.edge2_.partials_ = theta_derivative; - else + } else { ops_partials.edge2_.partials_[0] = theta_derivative_sum; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/mat/prob/wishart_lpdf.hpp b/stan/math/prim/mat/prob/wishart_lpdf.hpp index 88f7db0d628..bcba60d9409 100644 --- a/stan/math/prim/mat/prob/wishart_lpdf.hpp +++ b/stan/math/prim/mat/prob/wishart_lpdf.hpp @@ -71,14 +71,17 @@ return_type_t wishart_lpdf( LDLT_factor ldlt_S(S); check_ldlt_factor(function, "LDLT_Factor of scale parameter", ldlt_S); - if (include_summand::value) + if (include_summand::value) { lp += nu * k * NEG_LOG_TWO_OVER_TWO; + } - if (include_summand::value) + if (include_summand::value) { lp -= lmgamma(k, 0.5 * nu); + } - if (include_summand::value) + if (include_summand::value) { lp -= 0.5 * nu * log_determinant_ldlt(ldlt_S); + } if (include_summand::value) { Matrix, Dynamic, Dynamic> Sinv_W( @@ -87,8 +90,9 @@ return_type_t wishart_lpdf( lp -= 0.5 * trace(Sinv_W); } - if (include_summand::value && nu != (k + 1)) + if (include_summand::value && nu != (k + 1)) { lp += 0.5 * (nu - k - 1.0) * log_determinant_ldlt(ldlt_W); + } return lp; } diff --git a/stan/math/prim/mat/prob/wishart_rng.hpp b/stan/math/prim/mat/prob/wishart_rng.hpp index fa800c1052a..b1a57de3dd5 100644 --- a/stan/math/prim/mat/prob/wishart_rng.hpp +++ b/stan/math/prim/mat/prob/wishart_rng.hpp @@ -24,8 +24,9 @@ inline Eigen::MatrixXd wishart_rng(double nu, const Eigen::MatrixXd& S, MatrixXd B = MatrixXd::Zero(k, k); for (int j = 0; j < k; ++j) { - for (int i = 0; i < j; ++i) + for (int i = 0; i < j; ++i) { B(i, j) = normal_rng(0, 1, rng); + } B(j, j) = std::sqrt(chi_square_rng(nu - j, rng)); } return crossprod(B * S.llt().matrixU()); diff --git a/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp b/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp index 09296073d92..864a45cddeb 100644 --- a/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp +++ b/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp @@ -140,8 +140,9 @@ struct apply_scalar_unary > { */ static inline return_t apply(const std::vector& x) { return_t fx(x.size()); - for (size_t i = 0; i < x.size(); ++i) + for (size_t i = 0; i < x.size(); ++i) { fx[i] = apply_scalar_unary::apply(x[i]); + } return fx; } }; diff --git a/stan/math/prim/scal/err/check_2F1_converges.hpp b/stan/math/prim/scal/err/check_2F1_converges.hpp index f42bc04ab33..3db50a2d0f6 100644 --- a/stan/math/prim/scal/err/check_2F1_converges.hpp +++ b/stan/math/prim/scal/err/check_2F1_converges.hpp @@ -56,8 +56,9 @@ inline void check_2F1_converges(const char* function, const T_a1& a1, bool is_undefined = is_nonpositive_integer(b1) && fabs(b1) <= num_terms; if (!is_undefined - && (is_polynomial || fabs(z) < 1 || (fabs(z) == 1 && b1 > a1 + a2))) + && (is_polynomial || fabs(z) < 1 || (fabs(z) == 1 && b1 > a1 + a2))) { return; + } std::stringstream msg; msg << "called from function '" << function << "', " diff --git a/stan/math/prim/scal/err/check_3F2_converges.hpp b/stan/math/prim/scal/err/check_3F2_converges.hpp index a876ab66e0d..135739508ea 100644 --- a/stan/math/prim/scal/err/check_3F2_converges.hpp +++ b/stan/math/prim/scal/err/check_3F2_converges.hpp @@ -68,12 +68,15 @@ inline void check_3F2_converges(const char* function, const T_a1& a1, bool is_undefined = (is_nonpositive_integer(b1) && fabs(b1) <= num_terms) || (is_nonpositive_integer(b2) && fabs(b2) <= num_terms); - if (is_polynomial && !is_undefined) + if (is_polynomial && !is_undefined) { return; - if (fabs(z) < 1.0 && !is_undefined) + } + if (fabs(z) < 1.0 && !is_undefined) { return; - if (fabs(z) == 1.0 && !is_undefined && b1 + b2 > a1 + a2 + a3) + } + if (fabs(z) == 1.0 && !is_undefined && b1 + b2 > a1 + a2 + a3) { return; + } std::stringstream msg; msg << "called from function '" << function << "', " diff --git a/stan/math/prim/scal/err/check_consistent_size.hpp b/stan/math/prim/scal/err/check_consistent_size.hpp index a7e65329c82..7318a7ef7c3 100644 --- a/stan/math/prim/scal/err/check_consistent_size.hpp +++ b/stan/math/prim/scal/err/check_consistent_size.hpp @@ -23,8 +23,9 @@ template inline void check_consistent_size(const char* function, const char* name, const T& x, size_t expected_size) { if (!is_vector::value - || (is_vector::value && expected_size == stan::size_of(x))) + || (is_vector::value && expected_size == stan::size_of(x))) { return; + } std::stringstream msg; msg << ", expecting dimension = " << expected_size diff --git a/stan/math/prim/scal/err/check_finite.hpp b/stan/math/prim/scal/err/check_finite.hpp index af3c59302f1..4dbbbbeaf8d 100644 --- a/stan/math/prim/scal/err/check_finite.hpp +++ b/stan/math/prim/scal/err/check_finite.hpp @@ -14,8 +14,9 @@ namespace internal { template struct finite { static void check(const char* function, const char* name, const T_y& y) { - if (!(boost::math::isfinite(value_of_rec(y)))) + if (!(boost::math::isfinite(value_of_rec(y)))) { domain_error(function, name, y, "is ", ", but must be finite!"); + } } }; @@ -23,8 +24,9 @@ template struct finite { static void check(const char* function, const char* name, const T_y& y) { for (size_t n = 0; n < stan::length(y); n++) { - if (!(boost::math::isfinite(value_of_rec(stan::get(y, n))))) + if (!(boost::math::isfinite(value_of_rec(stan::get(y, n))))) { domain_error_vec(function, name, y, n, "is ", ", but must be finite!"); + } } } }; diff --git a/stan/math/prim/scal/err/check_nonnegative.hpp b/stan/math/prim/scal/err/check_nonnegative.hpp index 8e41702cf3e..5ea18713ca5 100644 --- a/stan/math/prim/scal/err/check_nonnegative.hpp +++ b/stan/math/prim/scal/err/check_nonnegative.hpp @@ -15,8 +15,9 @@ struct nonnegative { static void check(const char* function, const char* name, const T_y& y) { // have to use not is_unsigned. is_signed will be false // floating point types that have no unsigned versions. - if (!std::is_unsigned::value && !(y >= 0)) + if (!std::is_unsigned::value && !(y >= 0)) { domain_error(function, name, y, "is ", ", but must be >= 0!"); + } } }; @@ -25,8 +26,9 @@ struct nonnegative { static void check(const char* function, const char* name, const T_y& y) { for (size_t n = 0; n < stan::length(y); n++) { if (!std::is_unsigned::type>::value - && !(stan::get(y, n) >= 0)) + && !(stan::get(y, n) >= 0)) { domain_error_vec(function, name, y, n, "is ", ", but must be >= 0!"); + } } } }; diff --git a/stan/math/prim/scal/err/check_not_nan.hpp b/stan/math/prim/scal/err/check_not_nan.hpp index 9534147b8c6..9d8dbd285fe 100644 --- a/stan/math/prim/scal/err/check_not_nan.hpp +++ b/stan/math/prim/scal/err/check_not_nan.hpp @@ -14,8 +14,9 @@ namespace internal { template struct not_nan { static void check(const char* function, const char* name, const T_y& y) { - if (is_nan(value_of_rec(y))) + if (is_nan(value_of_rec(y))) { domain_error(function, name, y, "is ", ", but must not be nan!"); + } } }; @@ -23,8 +24,9 @@ template struct not_nan { static void check(const char* function, const char* name, const T_y& y) { for (size_t n = 0; n < stan::length(y); n++) { - if (is_nan(value_of_rec(stan::get(y, n)))) + if (is_nan(value_of_rec(stan::get(y, n)))) { domain_error_vec(function, name, y, n, "is ", ", but must not be nan!"); + } } } }; diff --git a/stan/math/prim/scal/err/check_positive.hpp b/stan/math/prim/scal/err/check_positive.hpp index b2aedebfb1a..cac27a2c456 100644 --- a/stan/math/prim/scal/err/check_positive.hpp +++ b/stan/math/prim/scal/err/check_positive.hpp @@ -18,8 +18,9 @@ struct positive { static void check(const char* function, const char* name, const T_y& y) { // have to use not is_unsigned. is_signed will be false // floating point types that have no unsigned versions. - if (!std::is_unsigned::value && !(y > 0)) + if (!std::is_unsigned::value && !(y > 0)) { domain_error(function, name, y, "is ", ", but must be > 0!"); + } } }; @@ -29,8 +30,9 @@ struct positive { using stan::length; for (size_t n = 0; n < length(y); n++) { if (!std::is_unsigned::type>::value - && !(stan::get(y, n) > 0)) + && !(stan::get(y, n) > 0)) { domain_error_vec(function, name, y, n, "is ", ", but must be > 0!"); + } } } }; diff --git a/stan/math/prim/scal/err/check_size_match.hpp b/stan/math/prim/scal/err/check_size_match.hpp index d48a8042019..5586c068eea 100644 --- a/stan/math/prim/scal/err/check_size_match.hpp +++ b/stan/math/prim/scal/err/check_size_match.hpp @@ -23,8 +23,9 @@ namespace math { template inline void check_size_match(const char* function, const char* name_i, T_size1 i, const char* name_j, T_size2 j) { - if (i == static_cast(j)) + if (i == static_cast(j)) { return; + } std::ostringstream msg; msg << ") and " << name_j << " (" << j << ") must match in size"; @@ -49,8 +50,9 @@ template inline void check_size_match(const char* function, const char* expr_i, const char* name_i, T_size1 i, const char* expr_j, const char* name_j, T_size2 j) { - if (i == static_cast(j)) + if (i == static_cast(j)) { return; + } std::ostringstream updated_name; updated_name << expr_i << name_i; std::string updated_name_str(updated_name.str()); diff --git a/stan/math/prim/scal/err/is_less_or_equal.hpp b/stan/math/prim/scal/err/is_less_or_equal.hpp index 59146667e14..76da280739c 100644 --- a/stan/math/prim/scal/err/is_less_or_equal.hpp +++ b/stan/math/prim/scal/err/is_less_or_equal.hpp @@ -22,8 +22,9 @@ template inline bool is_less_or_equal(const T_y& y, const T_high& high) { scalar_seq_view high_vec(high); for (size_t n = 0; n < stan::length(high); n++) { - if (!(stan::get(y, n) <= high_vec[n])) + if (!(stan::get(y, n) <= high_vec[n])) { return false; + } } return true; } diff --git a/stan/math/prim/scal/err/is_not_nan.hpp b/stan/math/prim/scal/err/is_not_nan.hpp index 3dd8484615f..38999b04dc1 100644 --- a/stan/math/prim/scal/err/is_not_nan.hpp +++ b/stan/math/prim/scal/err/is_not_nan.hpp @@ -20,8 +20,9 @@ namespace math { template inline bool is_not_nan(const T_y& y) { for (size_t n = 0; n < stan::length(y); ++n) { - if (is_nan(value_of_rec(stan::get(y, n)))) + if (is_nan(value_of_rec(stan::get(y, n)))) { return false; + } } return true; } diff --git a/stan/math/prim/scal/err/is_positive.hpp b/stan/math/prim/scal/err/is_positive.hpp index d765cbaeb5a..73e930c7418 100644 --- a/stan/math/prim/scal/err/is_positive.hpp +++ b/stan/math/prim/scal/err/is_positive.hpp @@ -17,8 +17,9 @@ namespace math { template inline bool is_positive(const T_y& y) { for (size_t n = 0; n < stan::length(y); ++n) { - if (!(stan::get(y, n) > 0)) + if (!(stan::get(y, n) > 0)) { return false; + } } return true; } diff --git a/stan/math/prim/scal/err/is_scal_finite.hpp b/stan/math/prim/scal/err/is_scal_finite.hpp index fdf1ad5f385..167e1db3f10 100644 --- a/stan/math/prim/scal/err/is_scal_finite.hpp +++ b/stan/math/prim/scal/err/is_scal_finite.hpp @@ -19,8 +19,9 @@ namespace math { template inline bool is_scal_finite(const T_y& y) { for (size_t n = 0; n < stan::length(y); ++n) { - if (!(boost::math::isfinite(value_of_rec(stan::get(y, n))))) + if (!(boost::math::isfinite(value_of_rec(stan::get(y, n))))) { return false; + } } return true; } diff --git a/stan/math/prim/scal/fun/F32.hpp b/stan/math/prim/scal/fun/F32.hpp index fd303431200..097581b7269 100644 --- a/stan/math/prim/scal/fun/F32.hpp +++ b/stan/math/prim/scal/fun/F32.hpp @@ -63,16 +63,18 @@ T F32(const T& a1, const T& a2, const T& a3, const T& b1, const T& b2, for (int k = 0; k <= max_steps; ++k) { T p = (a1 + k) * (a2 + k) * (a3 + k) / ((b1 + k) * (b2 + k) * (k + 1)); - if (p == 0.0) + if (p == 0.0) { return t_acc; + } log_t += log(fabs(p)) + log_z; t_sign = p >= 0.0 ? t_sign : -t_sign; T t_new = t_sign > 0.0 ? exp(log_t) : -exp(log_t); t_acc += t_new; - if (fabs(t_new) <= precision) + if (fabs(t_new) <= precision) { return t_acc; + } if (is_inf(t_acc)) { domain_error("F32", "sum (output)", t_acc, "overflow ", diff --git a/stan/math/prim/scal/fun/Phi.hpp b/stan/math/prim/scal/fun/Phi.hpp index 8e037fa3a19..dbbbaa3f2ee 100644 --- a/stan/math/prim/scal/fun/Phi.hpp +++ b/stan/math/prim/scal/fun/Phi.hpp @@ -29,14 +29,15 @@ namespace math { */ inline double Phi(double x) { check_not_nan("Phi", "x", x); - if (x < -37.5) + if (x < -37.5) { return 0; - else if (x < -5.0) + } else if (x < -5.0) { return 0.5 * erfc(-INV_SQRT_2 * x); - else if (x > 8.25) + } else if (x > 8.25) { return 1; - else + } else { return 0.5 * (1.0 + erf(INV_SQRT_2 * x)); + } } } // namespace math diff --git a/stan/math/prim/scal/fun/choose.hpp b/stan/math/prim/scal/fun/choose.hpp index 3f89d2f0e6e..54cc02bcdb2 100644 --- a/stan/math/prim/scal/fun/choose.hpp +++ b/stan/math/prim/scal/fun/choose.hpp @@ -29,8 +29,9 @@ namespace math { inline int choose(int n, int k) { check_nonnegative("choose", "n", n); check_nonnegative("choose", "k", k); - if (k > n) + if (k > n) { return 0; + } const double choices = boost::math::binomial_coefficient(n, k); check_less_or_equal("choose", "n choose k", choices, std::numeric_limits::max()); diff --git a/stan/math/prim/scal/fun/divide.hpp b/stan/math/prim/scal/fun/divide.hpp index c4a6d69dd7f..cbdf8145f68 100644 --- a/stan/math/prim/scal/fun/divide.hpp +++ b/stan/math/prim/scal/fun/divide.hpp @@ -22,8 +22,9 @@ inline return_type_t divide(const T1& x, const T2& y) { } inline int divide(int x, int y) { - if (unlikely(y == 0)) + if (unlikely(y == 0)) { domain_error("divide", "denominator is", y, ""); + } return x / y; } diff --git a/stan/math/prim/scal/fun/fdim.hpp b/stan/math/prim/scal/fun/fdim.hpp index 86dc1200edb..f0776feb1ca 100644 --- a/stan/math/prim/scal/fun/fdim.hpp +++ b/stan/math/prim/scal/fun/fdim.hpp @@ -25,8 +25,9 @@ template inline return_type_t fdim(T1 x, T2 y) { typedef return_type_t return_t; using std::numeric_limits; - if (is_any_nan(x, y)) + if (is_any_nan(x, y)) { return NOT_A_NUMBER; + } return (x <= y) ? 0 : x - y; } diff --git a/stan/math/prim/scal/fun/fmax.hpp b/stan/math/prim/scal/fun/fmax.hpp index 48504fdcca0..bf68cbf76b5 100644 --- a/stan/math/prim/scal/fun/fmax.hpp +++ b/stan/math/prim/scal/fun/fmax.hpp @@ -18,10 +18,12 @@ namespace math { */ template inline return_type_t fmax(const T1& x, const T2& y) { - if (is_nan(x)) + if (is_nan(x)) { return y; - if (is_nan(y)) + } + if (is_nan(y)) { return x; + } return x > y ? x : y; } diff --git a/stan/math/prim/scal/fun/fmin.hpp b/stan/math/prim/scal/fun/fmin.hpp index e1ba1945e3e..e6cf1cd9fcc 100644 --- a/stan/math/prim/scal/fun/fmin.hpp +++ b/stan/math/prim/scal/fun/fmin.hpp @@ -18,10 +18,12 @@ namespace math { */ template inline return_type_t fmin(const T1& x, const T2& y) { - if (is_nan(x)) + if (is_nan(x)) { return y; - if (is_nan(y)) + } + if (is_nan(y)) { return x; + } return y > x ? x : y; } diff --git a/stan/math/prim/scal/fun/gamma_p.hpp b/stan/math/prim/scal/fun/gamma_p.hpp index 510f5dbb7e8..0a8eb48e2d3 100644 --- a/stan/math/prim/scal/fun/gamma_p.hpp +++ b/stan/math/prim/scal/fun/gamma_p.hpp @@ -65,10 +65,12 @@ namespace math { * if z is at a pole of the function */ inline double gamma_p(double z, double a) { - if (is_nan(z)) + if (is_nan(z)) { return not_a_number(); - if (is_nan(a)) + } + if (is_nan(a)) { return not_a_number(); + } check_positive("gamma_p", "first argument (z)", z); check_nonnegative("gamma_p", "second argument (a)", a); return boost::math::gamma_p(z, a, boost_policy_t()); diff --git a/stan/math/prim/scal/fun/grad_2F1.hpp b/stan/math/prim/scal/fun/grad_2F1.hpp index e3719fb5665..e2976c11829 100644 --- a/stan/math/prim/scal/fun/grad_2F1.hpp +++ b/stan/math/prim/scal/fun/grad_2F1.hpp @@ -45,8 +45,9 @@ void grad_2F1(T& g_a1, T& g_b1, const T& a1, const T& a2, const T& b1, g_b1 = 0.0; T log_g_old[2]; - for (auto& i : log_g_old) + for (auto& i : log_g_old) { i = NEGATIVE_INFTY; + } T log_t_old = 0.0; T log_t_new = 0.0; @@ -56,13 +57,15 @@ void grad_2F1(T& g_a1, T& g_b1, const T& a1, const T& a2, const T& b1, double log_t_new_sign = 1.0; double log_t_old_sign = 1.0; double log_g_old_sign[2]; - for (double& x : log_g_old_sign) + for (double& x : log_g_old_sign) { x = 1.0; + } for (int k = 0; k <= max_steps; ++k) { T p = (a1 + k) * (a2 + k) / ((b1 + k) * (1 + k)); - if (p == 0) + if (p == 0) { return; + } log_t_new += log(fabs(p)) + log_z; log_t_new_sign = p >= 0.0 ? log_t_new_sign : -log_t_new_sign; @@ -80,8 +83,9 @@ void grad_2F1(T& g_a1, T& g_b1, const T& a1, const T& a2, const T& b1, g_a1 += log_g_old_sign[0] > 0 ? exp(log_g_old[0]) : -exp(log_g_old[0]); g_b1 += log_g_old_sign[1] > 0 ? exp(log_g_old[1]) : -exp(log_g_old[1]); - if (log_t_new <= log(precision)) + if (log_t_new <= log(precision)) { return; // implicit abs + } log_t_old = log_t_new; log_t_old_sign = log_t_new_sign; diff --git a/stan/math/prim/scal/fun/grad_F32.hpp b/stan/math/prim/scal/fun/grad_F32.hpp index b73a03a786f..3b777e85dfc 100644 --- a/stan/math/prim/scal/fun/grad_F32.hpp +++ b/stan/math/prim/scal/fun/grad_F32.hpp @@ -44,12 +44,14 @@ void grad_F32(T* g, const T& a1, const T& a2, const T& a3, const T& b1, using std::fabs; using std::log; - for (int i = 0; i < 6; ++i) + for (int i = 0; i < 6; ++i) { g[i] = 0.0; + } T log_g_old[6]; - for (auto& x : log_g_old) + for (auto& x : log_g_old) { x = NEGATIVE_INFTY; + } T log_t_old = 0.0; T log_t_new = 0.0; @@ -59,13 +61,15 @@ void grad_F32(T* g, const T& a1, const T& a2, const T& a3, const T& b1, double log_t_new_sign = 1.0; double log_t_old_sign = 1.0; double log_g_old_sign[6]; - for (int i = 0; i < 6; ++i) + for (int i = 0; i < 6; ++i) { log_g_old_sign[i] = 1.0; + } for (int k = 0; k <= max_steps; ++k) { T p = (a1 + k) * (a2 + k) * (a3 + k) / ((b1 + k) * (b2 + k) * (1 + k)); - if (p == 0) + if (p == 0) { return; + } log_t_new += log(fabs(p)) + log_z; log_t_new_sign = p >= 0.0 ? log_t_new_sign : -log_t_new_sign; @@ -110,8 +114,9 @@ void grad_F32(T* g, const T& a1, const T& a2, const T& a3, const T& b1, g[i] += log_g_old_sign[i] * exp(log_g_old[i]); } - if (log_t_new <= log(precision)) + if (log_t_new <= log(precision)) { return; // implicit abs + } log_t_old = log_t_new; log_t_old_sign = log_t_new_sign; diff --git a/stan/math/prim/scal/fun/grad_inc_beta.hpp b/stan/math/prim/scal/fun/grad_inc_beta.hpp index 366549895e0..f02901eeb44 100644 --- a/stan/math/prim/scal/fun/grad_inc_beta.hpp +++ b/stan/math/prim/scal/fun/grad_inc_beta.hpp @@ -27,8 +27,9 @@ inline void grad_inc_beta(double& g1, double& g2, double a, double b, double C = exp(a * c1 + b * c2) / a; double dF1 = 0; double dF2 = 0; - if (C) + if (C) { grad_2F1(dF1, dF2, a + b, 1.0, a + 1, z); + } g1 = fma((c1 - inv(a)), c3, C * (dF1 + dF2)); g2 = fma(c2, c3, C * dF1); } diff --git a/stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp b/stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp index 31339e447dd..ae22af892d4 100644 --- a/stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp +++ b/stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp @@ -54,8 +54,9 @@ return_type_t grad_reg_inc_gamma(T1 a, T2 z, T1 g, T1 dig, using std::log; typedef return_type_t TP; - if (is_any_nan(a, z, g, dig)) + if (is_any_nan(a, z, g, dig)) { return std::numeric_limits::quiet_NaN(); + } T2 l = log(z); if (z >= a && z >= 8) { @@ -77,8 +78,9 @@ return_type_t grad_reg_inc_gamma(T1 a, T2 z, T1 g, T1 dig, fac *= a_minus_one_minus_k; delta = dfac / zpow; - if (is_inf(delta)) + if (is_inf(delta)) { domain_error("grad_reg_inc_gamma", "is not converging", "", ""); + } } return gamma_q(a, z) * (l - dig) + exp(-z + (a - 1) * l) * S / g; @@ -94,10 +96,12 @@ return_type_t grad_reg_inc_gamma(T1 a, T2 z, T1 g, T1 dig, log_s += log_z - log(k); s_sign = -s_sign; log_delta = log_s - multiply_log(2, k + a); - if (is_inf(log_delta)) + if (is_inf(log_delta)) { domain_error("grad_reg_inc_gamma", "is not converging", "", ""); - if (log_delta <= log(precision)) + } + if (log_delta <= log(precision)) { return gamma_p(a, z) * (dig - l) + exp(a * l) * S / g; + } } domain_error("grad_reg_inc_gamma", "k (internal counter)", max_steps, "exceeded ", diff --git a/stan/math/prim/scal/fun/grad_reg_lower_inc_gamma.hpp b/stan/math/prim/scal/fun/grad_reg_lower_inc_gamma.hpp index 38796b4267f..d12af3a94c9 100644 --- a/stan/math/prim/scal/fun/grad_reg_lower_inc_gamma.hpp +++ b/stan/math/prim/scal/fun/grad_reg_lower_inc_gamma.hpp @@ -111,13 +111,15 @@ return_type_t grad_reg_lower_inc_gamma(const T1& a, const T2& z, using std::pow; typedef return_type_t TP; - if (is_any_nan(a, z)) + if (is_any_nan(a, z)) { return std::numeric_limits::quiet_NaN(); + } check_positive_finite("grad_reg_lower_inc_gamma", "a", a); - if (z == 0.0) + if (z == 0.0) { return 0.0; + } check_positive_finite("grad_reg_lower_inc_gamma", "z", z); if ((a < 0.8 && z > 15.0) || (a < 12.0 && z > 30.0) @@ -140,13 +142,15 @@ return_type_t grad_reg_lower_inc_gamma(const T1& a, const T2& z, while (true) { term = exp(a_plus_n * log_z - lgamma_a_plus_n_plus_1); sum_a += term; - if (term <= precision) + if (term <= precision) { break; - if (n >= max_steps) + } + if (n >= max_steps) { domain_error("grad_reg_lower_inc_gamma", "n (internal counter)", max_steps, "exceeded ", " iterations, gamma_p(a,z) gradient (a) " "did not converge."); + } ++n; lgamma_a_plus_n_plus_1 += log1p(a_plus_n); ++a_plus_n; @@ -160,13 +164,15 @@ return_type_t grad_reg_lower_inc_gamma(const T1& a, const T2& z, term = exp(a_plus_n * log_z - lgamma_a_plus_n_plus_1) * digamma(a_plus_n + 1); sum_b += term; - if (term <= precision) + if (term <= precision) { return emz * (log_z * sum_a - sum_b); - if (n >= max_steps) + } + if (n >= max_steps) { domain_error("grad_reg_lower_inc_gamma", "n (internal counter)", max_steps, "exceeded ", " iterations, gamma_p(a,z) gradient (a) " "did not converge."); + } ++n; lgamma_a_plus_n_plus_1 += log1p(a_plus_n); ++a_plus_n; diff --git a/stan/math/prim/scal/fun/inc_beta_dda.hpp b/stan/math/prim/scal/fun/inc_beta_dda.hpp index a2b084fd4a6..332b8d450c2 100644 --- a/stan/math/prim/scal/fun/inc_beta_dda.hpp +++ b/stan/math/prim/scal/fun/inc_beta_dda.hpp @@ -37,19 +37,25 @@ T inc_beta_dda(T a, T b, T z, T digamma_a, T digamma_ab) { using std::log; using std::pow; - if (b > a) + if (b > a) { if ((0.1 < z && z <= 0.75 && b > 500) || (0.01 < z && z <= 0.1 && b > 2500) - || (0.001 < z && z <= 0.01 && b > 1e5)) + || (0.001 < z && z <= 0.01 && b > 1e5)) { return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab); + } + } - if (z > 0.75 && a < 500) + if (z > 0.75 && a < 500) { return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab); - if (z > 0.9 && a < 2500) + } + if (z > 0.9 && a < 2500) { return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab); - if (z > 0.99 && a < 1e5) + } + if (z > 0.99 && a < 1e5) { return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab); - if (z > 0.999) + } + if (z > 0.999) { return -inc_beta_ddb(b, a, 1 - z, digamma_a, digamma_ab); + } double threshold = 1e-10; @@ -80,9 +86,10 @@ T inc_beta_dda(T a, T b, T z, T digamma_a, T digamma_ab) { ++k; summand *= z / k; - if (k > 1e5) + if (k > 1e5) { domain_error("inc_beta_dda", "did not converge within 10000 iterations", "", ""); + } } return inc_beta(a, b, z) * (log(z) + sum_numer / sum_denom); } diff --git a/stan/math/prim/scal/fun/inc_beta_ddb.hpp b/stan/math/prim/scal/fun/inc_beta_ddb.hpp index 0000a9954b1..ed375878111 100644 --- a/stan/math/prim/scal/fun/inc_beta_ddb.hpp +++ b/stan/math/prim/scal/fun/inc_beta_ddb.hpp @@ -40,14 +40,17 @@ T inc_beta_ddb(T a, T b, T z, T digamma_b, T digamma_ab) { using std::log; using std::pow; - if (b > a) + if (b > a) { if ((0.1 < z && z <= 0.75 && b > 500) || (0.01 < z && z <= 0.1 && b > 2500) - || (0.001 < z && z <= 0.01 && b > 1e5)) + || (0.001 < z && z <= 0.01 && b > 1e5)) { return -inc_beta_dda(b, a, 1 - z, digamma_b, digamma_ab); + } + } if ((z > 0.75 && a < 500) || (z > 0.9 && a < 2500) || (z > 0.99 && a < 1e5) - || (z > 0.999)) + || (z > 0.999)) { return -inc_beta_dda(b, a, 1 - z, digamma_b, digamma_ab); + } double threshold = 1e-10; @@ -74,9 +77,10 @@ T inc_beta_ddb(T a, T b, T z, T digamma_b, T digamma_ab) { ++k; summand *= z / k; - if (k > 1e5) + if (k > 1e5) { domain_error("inc_beta_ddb", "did not converge within 100000 iterations", "", ""); + } } return inc_beta(a, b, z) * (log(1 - z) - digamma_b + sum_numer / sum_denom); diff --git a/stan/math/prim/scal/fun/inv_Phi.hpp b/stan/math/prim/scal/fun/inv_Phi.hpp index ae6b35989ad..77715da818e 100644 --- a/stan/math/prim/scal/fun/inv_Phi.hpp +++ b/stan/math/prim/scal/fun/inv_Phi.hpp @@ -28,10 +28,12 @@ namespace math { inline double inv_Phi(double p) { check_bounded("inv_Phi", "Probability variable", p, 0, 1); - if (p < 8e-311) + if (p < 8e-311) { return NEGATIVE_INFTY; - if (p == 1) + } + if (p == 1) { return INFTY; + } static const double a[6] = {-3.969683028665376e+01, 2.209460984245205e+02, -2.759285104469687e+02, diff --git a/stan/math/prim/scal/fun/inv_logit.hpp b/stan/math/prim/scal/fun/inv_logit.hpp index cd33b71feba..6e4589bfcf1 100644 --- a/stan/math/prim/scal/fun/inv_logit.hpp +++ b/stan/math/prim/scal/fun/inv_logit.hpp @@ -50,8 +50,9 @@ inline double inv_logit(double a) { using std::exp; if (a < 0) { double exp_a = exp(a); - if (a < LOG_EPSILON) + if (a < LOG_EPSILON) { return exp_a; + } return exp_a / (1 + exp_a); } return inv(1 + exp(-a)); diff --git a/stan/math/prim/scal/fun/lb_constrain.hpp b/stan/math/prim/scal/fun/lb_constrain.hpp index 550b41ec150..aaea971283b 100644 --- a/stan/math/prim/scal/fun/lb_constrain.hpp +++ b/stan/math/prim/scal/fun/lb_constrain.hpp @@ -32,8 +32,9 @@ namespace math { template inline return_type_t lb_constrain(const T& x, const L& lb) { using std::exp; - if (lb == NEGATIVE_INFTY) + if (lb == NEGATIVE_INFTY) { return identity_constrain(x); + } return exp(x) + lb; } @@ -56,8 +57,9 @@ inline return_type_t lb_constrain(const T& x, const L& lb) { template inline return_type_t lb_constrain(const T& x, const L& lb, T& lp) { using std::exp; - if (lb == NEGATIVE_INFTY) + if (lb == NEGATIVE_INFTY) { return identity_constrain(x, lp); + } lp += x; return exp(x) + lb; } diff --git a/stan/math/prim/scal/fun/lb_free.hpp b/stan/math/prim/scal/fun/lb_free.hpp index 51b0f753343..ac498ac1e49 100644 --- a/stan/math/prim/scal/fun/lb_free.hpp +++ b/stan/math/prim/scal/fun/lb_free.hpp @@ -29,8 +29,9 @@ namespace math { template inline return_type_t lb_free(const T& y, const L& lb) { using std::log; - if (lb == NEGATIVE_INFTY) + if (lb == NEGATIVE_INFTY) { return identity_free(y); + } check_greater_or_equal("lb_free", "Lower bounded variable", y, lb); return log(y - lb); } diff --git a/stan/math/prim/scal/fun/lmgamma.hpp b/stan/math/prim/scal/fun/lmgamma.hpp index 8644f8ff3df..af492f60727 100644 --- a/stan/math/prim/scal/fun/lmgamma.hpp +++ b/stan/math/prim/scal/fun/lmgamma.hpp @@ -55,8 +55,9 @@ template inline return_type_t lmgamma(int k, T x) { return_type_t result = k * (k - 1) * LOG_PI_OVER_FOUR; - for (int j = 1; j <= k; ++j) + for (int j = 1; j <= k; ++j) { result += lgamma(x + (1.0 - j) / 2.0); + } return result; } diff --git a/stan/math/prim/scal/fun/locscale_constrain.hpp b/stan/math/prim/scal/fun/locscale_constrain.hpp index 622e2c2ec12..2edf7070de8 100644 --- a/stan/math/prim/scal/fun/locscale_constrain.hpp +++ b/stan/math/prim/scal/fun/locscale_constrain.hpp @@ -40,8 +40,9 @@ inline return_type_t locscale_constrain(const T& x, const M& mu, const S& sigma) { check_finite("locscale_constrain", "location", mu); if (sigma == 1) { - if (mu == 0) + if (mu == 0) { return identity_constrain(x); + } return mu + x; } check_positive_finite("locscale_constrain", "scale", sigma); @@ -80,8 +81,9 @@ inline return_type_t locscale_constrain(const T& x, const M& mu, using std::log; check_finite("locscale_constrain", "location", mu); if (sigma == 1) { - if (mu == 0) + if (mu == 0) { return identity_constrain(x); + } return mu + x; } check_positive_finite("locscale_constrain", "scale", sigma); diff --git a/stan/math/prim/scal/fun/locscale_free.hpp b/stan/math/prim/scal/fun/locscale_free.hpp index 79cd80bdd9b..685c132cee7 100644 --- a/stan/math/prim/scal/fun/locscale_free.hpp +++ b/stan/math/prim/scal/fun/locscale_free.hpp @@ -43,8 +43,9 @@ inline return_type_t locscale_free(const T& y, const L& mu, const S& sigma) { check_finite("locscale_free", "location", mu); if (sigma == 1) { - if (mu == 0) + if (mu == 0) { return identity_free(y); + } return y - mu; } check_positive_finite("locscale_free", "scale", sigma); diff --git a/stan/math/prim/scal/fun/log1m.hpp b/stan/math/prim/scal/fun/log1m.hpp index 8bed9b08718..c909100fb9b 100644 --- a/stan/math/prim/scal/fun/log1m.hpp +++ b/stan/math/prim/scal/fun/log1m.hpp @@ -40,8 +40,9 @@ namespace math { * @throw std::overflow_error If the computation overflows. */ inline double log1m(double x) { - if (!is_nan(x)) + if (!is_nan(x)) { check_less_or_equal("log1m", "x", x, 1); + } return stan::math::log1p(-x); } diff --git a/stan/math/prim/scal/fun/log1m_exp.hpp b/stan/math/prim/scal/fun/log1m_exp.hpp index 181441a91b9..c82bcb13a14 100644 --- a/stan/math/prim/scal/fun/log1m_exp.hpp +++ b/stan/math/prim/scal/fun/log1m_exp.hpp @@ -46,12 +46,13 @@ namespace math { inline double log1m_exp(double a) { using std::exp; using std::log; - if (a >= 0) + if (a >= 0) { return NOT_A_NUMBER; - else if (a > -0.693147) + } else if (a > -0.693147) { return log(-expm1(a)); // 0.693147 ~= log(2) - else + } else { return log1m(exp(a)); + } } } // namespace math diff --git a/stan/math/prim/scal/fun/log1m_inv_logit.hpp b/stan/math/prim/scal/fun/log1m_inv_logit.hpp index fa0dd7b8947..214080264e1 100644 --- a/stan/math/prim/scal/fun/log1m_inv_logit.hpp +++ b/stan/math/prim/scal/fun/log1m_inv_logit.hpp @@ -34,8 +34,9 @@ namespace math { */ inline double log1m_inv_logit(double u) { using std::exp; - if (u > 0.0) + if (u > 0.0) { return -u - log1p(exp(-u)); // prevent underflow + } return -log1p(exp(u)); } diff --git a/stan/math/prim/scal/fun/log1p_exp.hpp b/stan/math/prim/scal/fun/log1p_exp.hpp index 64b435287c6..eff51abc5d2 100644 --- a/stan/math/prim/scal/fun/log1p_exp.hpp +++ b/stan/math/prim/scal/fun/log1p_exp.hpp @@ -43,8 +43,9 @@ namespace math { inline double log1p_exp(double a) { using std::exp; // like log_sum_exp below with b=0.0; prevents underflow - if (a > 0.0) + if (a > 0.0) { return a + log1p(exp(-a)); + } return log1p(exp(a)); } diff --git a/stan/math/prim/scal/fun/log_diff_exp.hpp b/stan/math/prim/scal/fun/log_diff_exp.hpp index 3e7b35dc824..9467b58837d 100644 --- a/stan/math/prim/scal/fun/log_diff_exp.hpp +++ b/stan/math/prim/scal/fun/log_diff_exp.hpp @@ -46,8 +46,9 @@ namespace math { */ template inline return_type_t log_diff_exp(const T1 x, const T2 y) { - if (x <= y) + if (x <= y) { return (x < INFTY && x == y) ? NEGATIVE_INFTY : NOT_A_NUMBER; + } return x + log1m_exp(y - x); } diff --git a/stan/math/prim/scal/fun/log_falling_factorial.hpp b/stan/math/prim/scal/fun/log_falling_factorial.hpp index 8c42e8b297a..19c23a5368d 100644 --- a/stan/math/prim/scal/fun/log_falling_factorial.hpp +++ b/stan/math/prim/scal/fun/log_falling_factorial.hpp @@ -53,8 +53,9 @@ namespace math { */ template inline return_type_t log_falling_factorial(const T1 x, const T2 n) { - if (is_any_nan(x, n)) + if (is_any_nan(x, n)) { return NOT_A_NUMBER; + } static const char* function = "log_falling_factorial"; check_positive(function, "first argument", x); return lgamma(x + 1) - lgamma(x - n + 1); diff --git a/stan/math/prim/scal/fun/log_inv_logit.hpp b/stan/math/prim/scal/fun/log_inv_logit.hpp index 8fc2104eb26..237a30f87d7 100644 --- a/stan/math/prim/scal/fun/log_inv_logit.hpp +++ b/stan/math/prim/scal/fun/log_inv_logit.hpp @@ -31,8 +31,9 @@ namespace math { */ inline double log_inv_logit(double u) { using std::exp; - if (u < 0.0) + if (u < 0.0) { return u - log1p(exp(u)); // prevent underflow + } return -log1p(exp(-u)); } diff --git a/stan/math/prim/scal/fun/log_modified_bessel_first_kind.hpp b/stan/math/prim/scal/fun/log_modified_bessel_first_kind.hpp index ae30627f978..079c3cbc825 100644 --- a/stan/math/prim/scal/fun/log_modified_bessel_first_kind.hpp +++ b/stan/math/prim/scal/fun/log_modified_bessel_first_kind.hpp @@ -53,14 +53,17 @@ inline return_type_t log_modified_bessel_first_kind( typedef return_type_t T; if (z == 0) { - if (v == 0) + if (v == 0) { return 0.0; - if (v > 0) + } + if (v > 0) { return NEGATIVE_INFTY; + } return INFTY; } - if (is_inf(z)) + if (is_inf(z)) { return z; + } if (v == 0) { // WARNING: will not autodiff for v = 0 correctly // modified from Boost's bessel_i0_imp in the double precision case, diff --git a/stan/math/prim/scal/fun/log_rising_factorial.hpp b/stan/math/prim/scal/fun/log_rising_factorial.hpp index f3cc2ef0435..56735749b08 100644 --- a/stan/math/prim/scal/fun/log_rising_factorial.hpp +++ b/stan/math/prim/scal/fun/log_rising_factorial.hpp @@ -51,8 +51,9 @@ namespace math { */ template inline return_type_t log_rising_factorial(const T1& x, const T2& n) { - if (is_any_nan(x, n)) + if (is_any_nan(x, n)) { return std::numeric_limits::quiet_NaN(); + } static const char* function = "log_rising_factorial"; check_positive(function, "first argument", x); return lgamma(x + n) - lgamma(x); diff --git a/stan/math/prim/scal/fun/log_sum_exp.hpp b/stan/math/prim/scal/fun/log_sum_exp.hpp index 29b8335f8df..63e9af29979 100644 --- a/stan/math/prim/scal/fun/log_sum_exp.hpp +++ b/stan/math/prim/scal/fun/log_sum_exp.hpp @@ -46,12 +46,15 @@ namespace math { */ template inline return_type_t log_sum_exp(const T2& a, const T1& b) { - if (a == NEGATIVE_INFTY) + if (a == NEGATIVE_INFTY) { return b; - if (a == INFTY && b == INFTY) + } + if (a == INFTY && b == INFTY) { return INFTY; - if (a > b) + } + if (a > b) { return a + log1p_exp(b - a); + } return b + log1p_exp(a - b); } diff --git a/stan/math/prim/scal/fun/lub_constrain.hpp b/stan/math/prim/scal/fun/lub_constrain.hpp index b2ef90da846..8494662bf8b 100644 --- a/stan/math/prim/scal/fun/lub_constrain.hpp +++ b/stan/math/prim/scal/fun/lub_constrain.hpp @@ -45,22 +45,26 @@ inline return_type_t lub_constrain(const T& x, const L& lb, const U& ub) { using std::exp; check_less("lub_constrain", "lb", lb, ub); - if (lb == NEGATIVE_INFTY) + if (lb == NEGATIVE_INFTY) { return ub_constrain(x, ub); - if (ub == INFTY) + } + if (ub == INFTY) { return lb_constrain(x, lb); + } T inv_logit_x; if (x > 0) { inv_logit_x = inv_logit(x); // Prevent x from reaching one unless it really really should. - if ((x < INFTY) && (inv_logit_x == 1)) + if ((x < INFTY) && (inv_logit_x == 1)) { inv_logit_x = 1 - 1e-15; + } } else { inv_logit_x = inv_logit(x); // Prevent x from reaching zero unless it really really should. - if ((x > NEGATIVE_INFTY) && (inv_logit_x == 0)) + if ((x > NEGATIVE_INFTY) && (inv_logit_x == 0)) { inv_logit_x = 1e-15; + } } return fma((ub - lb), inv_logit_x, lb); } @@ -112,25 +116,29 @@ inline return_type_t lub_constrain(const T& x, const L& lb, using std::exp; using std::log; check_less("lub_constrain", "lb", lb, ub); - if (lb == NEGATIVE_INFTY) + if (lb == NEGATIVE_INFTY) { return ub_constrain(x, ub, lp); - if (ub == INFTY) + } + if (ub == INFTY) { return lb_constrain(x, lb, lp); + } T inv_logit_x; if (x > 0) { T exp_minus_x = exp(-x); inv_logit_x = inv_logit(x); lp += log(ub - lb) - x - 2 * log1p(exp_minus_x); // Prevent x from reaching one unless it really really should. - if ((x < INFTY) && (inv_logit_x == 1)) + if ((x < INFTY) && (inv_logit_x == 1)) { inv_logit_x = 1 - 1e-15; + } } else { T exp_x = exp(x); inv_logit_x = inv_logit(x); lp += log(ub - lb) + x - 2 * log1p(exp_x); // Prevent x from reaching zero unless it really really should. - if ((x > NEGATIVE_INFTY) && (inv_logit_x == 0)) + if ((x > NEGATIVE_INFTY) && (inv_logit_x == 0)) { inv_logit_x = 1e-15; + } } return fma((ub - lb), inv_logit_x, lb); } diff --git a/stan/math/prim/scal/fun/lub_free.hpp b/stan/math/prim/scal/fun/lub_free.hpp index f23b91460e2..b18e13b6f0b 100644 --- a/stan/math/prim/scal/fun/lub_free.hpp +++ b/stan/math/prim/scal/fun/lub_free.hpp @@ -46,10 +46,12 @@ namespace math { template inline return_type_t lub_free(const T& y, const L& lb, const U& ub) { check_bounded("lub_free", "Bounded variable", y, lb, ub); - if (lb == NEGATIVE_INFTY) + if (lb == NEGATIVE_INFTY) { return ub_free(y, ub); - if (ub == INFTY) + } + if (ub == INFTY) { return lb_free(y, lb); + } return logit((y - lb) / (ub - lb)); } diff --git a/stan/math/prim/scal/fun/modulus.hpp b/stan/math/prim/scal/fun/modulus.hpp index 16c54ac7515..04d2a9febf4 100644 --- a/stan/math/prim/scal/fun/modulus.hpp +++ b/stan/math/prim/scal/fun/modulus.hpp @@ -10,8 +10,9 @@ namespace stan { namespace math { inline int modulus(int x, int y) { - if (unlikely(y == 0)) + if (unlikely(y == 0)) { domain_error("modulus", "divisor is", 0, ""); + } return x % y; } diff --git a/stan/math/prim/scal/fun/multiply_log.hpp b/stan/math/prim/scal/fun/multiply_log.hpp index 7c8896775e3..664c2e5b2a8 100644 --- a/stan/math/prim/scal/fun/multiply_log.hpp +++ b/stan/math/prim/scal/fun/multiply_log.hpp @@ -50,8 +50,9 @@ namespace math { template inline return_type_t multiply_log(const T_a a, const T_b b) { using std::log; - if (b == 0.0 && a == 0.0) + if (b == 0.0 && a == 0.0) { return 0.0; + } return a * log(b); } diff --git a/stan/math/prim/scal/fun/offset_multiplier_constrain.hpp b/stan/math/prim/scal/fun/offset_multiplier_constrain.hpp index 4e4bac79f5a..024782d258a 100644 --- a/stan/math/prim/scal/fun/offset_multiplier_constrain.hpp +++ b/stan/math/prim/scal/fun/offset_multiplier_constrain.hpp @@ -43,8 +43,9 @@ inline return_type_t offset_multiplier_constrain(const T& x, const S& sigma) { check_finite("offset_multiplier_constrain", "offset", mu); if (sigma == 1) { - if (mu == 0) + if (mu == 0) { return identity_constrain(x); + } return mu + x; } check_positive_finite("offset_multiplier_constrain", "multiplier", sigma); @@ -85,8 +86,9 @@ inline return_type_t offset_multiplier_constrain(const T& x, using std::log; check_finite("offset_multiplier_constrain", "offset", mu); if (sigma == 1) { - if (mu == 0) + if (mu == 0) { return identity_constrain(x); + } return mu + x; } check_positive_finite("offset_multiplier_constrain", "multiplier", sigma); diff --git a/stan/math/prim/scal/fun/offset_multiplier_free.hpp b/stan/math/prim/scal/fun/offset_multiplier_free.hpp index 1167ad3473b..6e92b645e63 100644 --- a/stan/math/prim/scal/fun/offset_multiplier_free.hpp +++ b/stan/math/prim/scal/fun/offset_multiplier_free.hpp @@ -43,8 +43,9 @@ inline return_type_t offset_multiplier_free(const T& y, const L& mu, const S& sigma) { check_finite("offset_multiplier_free", "offset", mu); if (sigma == 1) { - if (mu == 0) + if (mu == 0) { return identity_free(y); + } return y - mu; } check_positive_finite("offset_multiplier_free", "multiplier", sigma); diff --git a/stan/math/prim/scal/fun/tgamma.hpp b/stan/math/prim/scal/fun/tgamma.hpp index 6830834432f..776e635f3b0 100644 --- a/stan/math/prim/scal/fun/tgamma.hpp +++ b/stan/math/prim/scal/fun/tgamma.hpp @@ -16,8 +16,9 @@ namespace math { * @return The gamma function applied to argument. */ inline double tgamma(double x) { - if (x == 0.0 || is_nonpositive_integer(x)) + if (x == 0.0 || is_nonpositive_integer(x)) { domain_error("tgamma", "x", x, "x == 0 or negative integer"); + } return std::tgamma(x); } diff --git a/stan/math/prim/scal/fun/trigamma.hpp b/stan/math/prim/scal/fun/trigamma.hpp index e6202262d2a..ea4adaf9cd9 100644 --- a/stan/math/prim/scal/fun/trigamma.hpp +++ b/stan/math/prim/scal/fun/trigamma.hpp @@ -61,8 +61,9 @@ inline T trigamma_impl(const T& x) { } // small value approximation if x <= small. - if (x <= small) + if (x <= small) { return inv_square(x); + } // use recurrence relation until x >= large // see http://mathworld.wolfram.com/PolygammaFunction.html diff --git a/stan/math/prim/scal/fun/ub_constrain.hpp b/stan/math/prim/scal/fun/ub_constrain.hpp index 1b054428d7a..badf291b8ab 100644 --- a/stan/math/prim/scal/fun/ub_constrain.hpp +++ b/stan/math/prim/scal/fun/ub_constrain.hpp @@ -32,8 +32,9 @@ namespace math { template inline return_type_t ub_constrain(const T& x, const U& ub) { using std::exp; - if (ub == INFTY) + if (ub == INFTY) { return identity_constrain(x); + } return ub - exp(x); } @@ -63,8 +64,9 @@ inline return_type_t ub_constrain(const T& x, const U& ub) { template inline return_type_t ub_constrain(const T& x, const U& ub, T& lp) { using std::exp; - if (ub == INFTY) + if (ub == INFTY) { return identity_constrain(x, lp); + } lp += x; return ub - exp(x); } diff --git a/stan/math/prim/scal/fun/ub_free.hpp b/stan/math/prim/scal/fun/ub_free.hpp index 43fb5d5eca2..09630aa4fbb 100644 --- a/stan/math/prim/scal/fun/ub_free.hpp +++ b/stan/math/prim/scal/fun/ub_free.hpp @@ -36,8 +36,9 @@ namespace math { template inline return_type_t ub_free(const T& y, const U& ub) { using std::log; - if (ub == INFTY) + if (ub == INFTY) { return identity_free(y); + } check_less_or_equal("ub_free", "Upper bounded variable", y, ub); return log(ub - y); } diff --git a/stan/math/prim/scal/prob/bernoulli_cdf.hpp b/stan/math/prim/scal/prob/bernoulli_cdf.hpp index 808488b21b9..c3f5703364c 100644 --- a/stan/math/prim/scal/prob/bernoulli_cdf.hpp +++ b/stan/math/prim/scal/prob/bernoulli_cdf.hpp @@ -29,8 +29,9 @@ return_type_t bernoulli_cdf(const T_n& n, const T_prob& theta) { static const char* function = "bernoulli_cdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, theta)) + if (size_zero(n, theta)) { return 1.0; + } T_partials_return P(1.0); @@ -48,27 +49,31 @@ return_type_t bernoulli_cdf(const T_n& n, const T_prob& theta) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(0.0); + } } for (size_t i = 0; i < size; i++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(n_vec[i]) >= 1) + if (value_of(n_vec[i]) >= 1) { continue; + } const T_partials_return Pi = 1 - value_of(theta_vec[i]); P *= Pi; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += -1 / Pi; + } } if (!is_constant_all::value) { - for (size_t i = 0; i < stan::length(theta); ++i) + for (size_t i = 0; i < stan::length(theta); ++i) { ops_partials.edge1_.partials_[i] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/bernoulli_lccdf.hpp b/stan/math/prim/scal/prob/bernoulli_lccdf.hpp index 1a42bfdb01c..8846d383010 100644 --- a/stan/math/prim/scal/prob/bernoulli_lccdf.hpp +++ b/stan/math/prim/scal/prob/bernoulli_lccdf.hpp @@ -30,8 +30,9 @@ return_type_t bernoulli_lccdf(const T_n& n, const T_prob& theta) { static const char* function = "bernoulli_lccdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, theta)) + if (size_zero(n, theta)) { return 0.0; + } T_partials_return P(0.0); @@ -50,8 +51,9 @@ return_type_t bernoulli_lccdf(const T_n& n, const T_prob& theta) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(0.0); + } } for (size_t i = 0; i < size; i++) { @@ -64,8 +66,9 @@ return_type_t bernoulli_lccdf(const T_n& n, const T_prob& theta) { P += log(Pi); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += 1 / Pi; + } } } diff --git a/stan/math/prim/scal/prob/bernoulli_lcdf.hpp b/stan/math/prim/scal/prob/bernoulli_lcdf.hpp index 125e9fc1627..060f214897d 100644 --- a/stan/math/prim/scal/prob/bernoulli_lcdf.hpp +++ b/stan/math/prim/scal/prob/bernoulli_lcdf.hpp @@ -30,8 +30,9 @@ return_type_t bernoulli_lcdf(const T_n& n, const T_prob& theta) { static const char* function = "bernoulli_lcdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, theta)) + if (size_zero(n, theta)) { return 0.0; + } T_partials_return P(0.0); @@ -50,22 +51,25 @@ return_type_t bernoulli_lcdf(const T_n& n, const T_prob& theta) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(negative_infinity()); + } } for (size_t i = 0; i < size; i++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(n_vec[i]) >= 1) + if (value_of(n_vec[i]) >= 1) { continue; + } const T_partials_return Pi = 1 - value_of(theta_vec[i]); P += log(Pi); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] -= 1 / Pi; + } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/bernoulli_logit_lpmf.hpp b/stan/math/prim/scal/prob/bernoulli_logit_lpmf.hpp index 4c01d755994..460fd1e0191 100644 --- a/stan/math/prim/scal/prob/bernoulli_logit_lpmf.hpp +++ b/stan/math/prim/scal/prob/bernoulli_logit_lpmf.hpp @@ -32,8 +32,9 @@ return_type_t bernoulli_logit_lpmf(const T_n& n, const T_prob& theta) { using std::exp; - if (size_zero(n, theta)) + if (size_zero(n, theta)) { return 0.0; + } T_partials_return logp(0.0); @@ -42,8 +43,9 @@ return_type_t bernoulli_logit_lpmf(const T_n& n, const T_prob& theta) { check_consistent_sizes(function, "Random variable", n, "Probability parameter", theta); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view n_vec(n); scalar_seq_view theta_vec(theta); @@ -59,21 +61,23 @@ return_type_t bernoulli_logit_lpmf(const T_n& n, const T_prob& theta) { // Handle extreme values gracefully using Taylor approximations. static const double cutoff = 20.0; - if (ntheta > cutoff) + if (ntheta > cutoff) { logp -= exp_m_ntheta; - else if (ntheta < -cutoff) + } else if (ntheta < -cutoff) { logp += ntheta; - else + } else { logp -= log1p(exp_m_ntheta); + } if (!is_constant_all::value) { - if (ntheta > cutoff) + if (ntheta > cutoff) { ops_partials.edge1_.partials_[n] -= exp_m_ntheta; - else if (ntheta < -cutoff) + } else if (ntheta < -cutoff) { ops_partials.edge1_.partials_[n] += sign; - else + } else { ops_partials.edge1_.partials_[n] += sign * exp_m_ntheta / (exp_m_ntheta + 1); + } } } return ops_partials.build(logp); diff --git a/stan/math/prim/scal/prob/bernoulli_lpmf.hpp b/stan/math/prim/scal/prob/bernoulli_lpmf.hpp index 5531c120db1..3a8e4fa97fa 100644 --- a/stan/math/prim/scal/prob/bernoulli_lpmf.hpp +++ b/stan/math/prim/scal/prob/bernoulli_lpmf.hpp @@ -32,8 +32,9 @@ return_type_t bernoulli_lpmf(const T_n& n, const T_prob& theta) { using std::log; - if (size_zero(n, theta)) + if (size_zero(n, theta)) { return 0.0; + } T_partials_return logp(0.0); @@ -43,8 +44,9 @@ return_type_t bernoulli_lpmf(const T_n& n, const T_prob& theta) { check_consistent_sizes(function, "Random variable", n, "Probability parameter", theta); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view n_vec(n); scalar_seq_view theta_vec(theta); @@ -60,12 +62,14 @@ return_type_t bernoulli_lpmf(const T_n& n, const T_prob& theta) { // avoid nans when sum == N or sum == 0 if (sum == N) { logp += N * log(theta_dbl); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[0] += N / theta_dbl; + } } else if (sum == 0) { logp += N * log1m(theta_dbl); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[0] += N / (theta_dbl - 1); + } } else { const T_partials_return log_theta = log(theta_dbl); const T_partials_return log1m_theta = log1m(theta_dbl); @@ -83,16 +87,18 @@ return_type_t bernoulli_lpmf(const T_n& n, const T_prob& theta) { const int n_int = value_of(n_vec[n]); const T_partials_return theta_dbl = value_of(theta_vec[n]); - if (n_int == 1) + if (n_int == 1) { logp += log(theta_dbl); - else + } else { logp += log1m(theta_dbl); + } if (!is_constant_all::value) { - if (n_int == 1) + if (n_int == 1) { ops_partials.edge1_.partials_[n] += 1.0 / theta_dbl; - else + } else { ops_partials.edge1_.partials_[n] += 1.0 / (theta_dbl - 1); + } } } } diff --git a/stan/math/prim/scal/prob/beta_binomial_cdf.hpp b/stan/math/prim/scal/prob/beta_binomial_cdf.hpp index 22929c11c52..59b94a46406 100644 --- a/stan/math/prim/scal/prob/beta_binomial_cdf.hpp +++ b/stan/math/prim/scal/prob/beta_binomial_cdf.hpp @@ -42,8 +42,9 @@ return_type_t beta_binomial_cdf(const T_n& n, const T_N& N, static const char* function = "beta_binomial_cdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, N, alpha, beta)) + if (size_zero(n, N, alpha, beta)) { return 1.0; + } T_partials_return P(1.0); @@ -69,8 +70,9 @@ return_type_t beta_binomial_cdf(const T_n& n, const T_N& N, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) <= 0) + if (value_of(n_vec[i]) <= 0) { return ops_partials.build(0.0); + } } for (size_t i = 0; i < size; i++) { @@ -129,12 +131,14 @@ return_type_t beta_binomial_cdf(const T_n& n, const T_N& N, } if (!is_constant_all::value) { - for (size_t i = 0; i < stan::length(alpha); ++i) + for (size_t i = 0; i < stan::length(alpha); ++i) { ops_partials.edge1_.partials_[i] *= P; + } } if (!is_constant_all::value) { - for (size_t i = 0; i < stan::length(beta); ++i) + for (size_t i = 0; i < stan::length(beta); ++i) { ops_partials.edge2_.partials_[i] *= P; + } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/beta_binomial_lccdf.hpp b/stan/math/prim/scal/prob/beta_binomial_lccdf.hpp index 5ea410faf5b..0c9e2d93197 100644 --- a/stan/math/prim/scal/prob/beta_binomial_lccdf.hpp +++ b/stan/math/prim/scal/prob/beta_binomial_lccdf.hpp @@ -42,8 +42,9 @@ return_type_t beta_binomial_lccdf(const T_n& n, const T_N& N, static const char* function = "beta_binomial_lccdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, N, alpha, beta)) + if (size_zero(n, N, alpha, beta)) { return 0.0; + } T_partials_return P(0.0); @@ -70,8 +71,9 @@ return_type_t beta_binomial_lccdf(const T_n& n, const T_N& N, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as neg infinity for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) <= 0) + if (value_of(n_vec[i]) <= 0) { return ops_partials.build(0.0); + } } for (size_t i = 0; i < size; i++) { diff --git a/stan/math/prim/scal/prob/beta_binomial_lcdf.hpp b/stan/math/prim/scal/prob/beta_binomial_lcdf.hpp index 90b09e147b8..be3c288e20e 100644 --- a/stan/math/prim/scal/prob/beta_binomial_lcdf.hpp +++ b/stan/math/prim/scal/prob/beta_binomial_lcdf.hpp @@ -42,8 +42,9 @@ return_type_t beta_binomial_lcdf(const T_n& n, const T_N& N, static const char* function = "beta_binomial_lcdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, N, alpha, beta)) + if (size_zero(n, N, alpha, beta)) { return 0.0; + } T_partials_return P(0.0); @@ -69,8 +70,9 @@ return_type_t beta_binomial_lcdf(const T_n& n, const T_N& N, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as neg infinity for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) <= 0) + if (value_of(n_vec[i]) <= 0) { return ops_partials.build(negative_infinity()); + } } for (size_t i = 0; i < size; i++) { diff --git a/stan/math/prim/scal/prob/beta_binomial_lpmf.hpp b/stan/math/prim/scal/prob/beta_binomial_lpmf.hpp index 886d541ea25..5f858ba7a54 100644 --- a/stan/math/prim/scal/prob/beta_binomial_lpmf.hpp +++ b/stan/math/prim/scal/prob/beta_binomial_lpmf.hpp @@ -69,8 +69,9 @@ return_type_t beta_binomial_lpmf(const T_n& n, const T_N& N, const auto size = max_size(n, N, alpha, beta); for (size_t i = 0; i < size; i++) { - if (n_vec[i] < 0 || n_vec[i] > N_vec[i]) + if (n_vec[i] < 0 || n_vec[i] > N_vec[i]) { return ops_partials.build(LOG_ZERO); + } } if (include_summand::value) { diff --git a/stan/math/prim/scal/prob/beta_cdf.hpp b/stan/math/prim/scal/prob/beta_cdf.hpp index 475ba5e238f..99a9ed4f147 100644 --- a/stan/math/prim/scal/prob/beta_cdf.hpp +++ b/stan/math/prim/scal/prob/beta_cdf.hpp @@ -37,8 +37,9 @@ return_type_t beta_cdf( typedef partials_return_type_t T_partials_return; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 1.0; + } static const char* function = "beta_cdf"; @@ -64,8 +65,9 @@ return_type_t beta_cdf( // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) <= 0) + if (value_of(y_vec[i]) <= 0) { return ops_partials.build(0.0); + } } VectorBuilder::value, @@ -94,8 +96,9 @@ return_type_t beta_cdf( for (size_t n = 0; n < N; n++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(y_vec[n]) >= 1.0) + if (value_of(y_vec[n]) >= 1.0) { continue; + } const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return alpha_dbl = value_of(alpha_vec[n]); @@ -105,33 +108,39 @@ return_type_t beta_cdf( P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += inc_beta_ddz(alpha_dbl, beta_dbl, y_dbl) / Pn; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += inc_beta_dda(alpha_dbl, beta_dbl, y_dbl, digamma_alpha_vec[n], digamma_sum_vec[n]) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += inc_beta_ddb(alpha_dbl, beta_dbl, y_dbl, digamma_beta_vec[n], digamma_sum_vec[n]) / Pn; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(alpha); ++n) + for (size_t n = 0; n < stan::length(alpha); ++n) { ops_partials.edge2_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(beta); ++n) + for (size_t n = 0; n < stan::length(beta); ++n) { ops_partials.edge3_.partials_[n] *= P; + } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/beta_lccdf.hpp b/stan/math/prim/scal/prob/beta_lccdf.hpp index a0dabb6de04..463f2910db7 100644 --- a/stan/math/prim/scal/prob/beta_lccdf.hpp +++ b/stan/math/prim/scal/prob/beta_lccdf.hpp @@ -42,8 +42,9 @@ return_type_t beta_lccdf( typedef partials_return_type_t T_partials_return; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0.0; + } static const char* function = "beta_lccdf"; @@ -102,10 +103,11 @@ return_type_t beta_lccdf( ccdf_log += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= pow(1 - y_dbl, beta_dbl - 1) * pow(y_dbl, alpha_dbl - 1) / betafunc_dbl / Pn; + } T_partials_return g1 = 0; T_partials_return g2 = 0; @@ -115,10 +117,12 @@ return_type_t beta_lccdf( digamma_alpha_vec[n], digamma_beta_vec[n], digamma_sum_vec[n], betafunc_dbl); } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= g1 / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= g2 / Pn; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/beta_lcdf.hpp b/stan/math/prim/scal/prob/beta_lcdf.hpp index 1c1528fefbe..d4336196b2e 100644 --- a/stan/math/prim/scal/prob/beta_lcdf.hpp +++ b/stan/math/prim/scal/prob/beta_lcdf.hpp @@ -42,8 +42,9 @@ return_type_t beta_lcdf( typedef partials_return_type_t T_partials_return; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0.0; + } static const char* function = "beta_lcdf"; @@ -103,10 +104,11 @@ return_type_t beta_lcdf( cdf_log += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += pow(1 - y_dbl, beta_dbl - 1) * pow(y_dbl, alpha_dbl - 1) / betafunc_dbl / Pn; + } T_partials_return g1 = 0; T_partials_return g2 = 0; @@ -116,10 +118,12 @@ return_type_t beta_lcdf( digamma_alpha_vec[n], digamma_beta_vec[n], digamma_sum_vec[n], betafunc_dbl); } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += g1 / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += g2 / Pn; + } } return ops_partials.build(cdf_log); diff --git a/stan/math/prim/scal/prob/beta_lpdf.hpp b/stan/math/prim/scal/prob/beta_lpdf.hpp index 77f648d13f6..36564c9f5ce 100644 --- a/stan/math/prim/scal/prob/beta_lpdf.hpp +++ b/stan/math/prim/scal/prob/beta_lpdf.hpp @@ -55,10 +55,12 @@ return_type_t beta_lpdf( check_nonnegative(function, "Random variable", y); check_less_or_equal(function, "Random variable", y, 1); - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0; - if (!include_summand::value) + } + if (!include_summand::value) { return 0; + } T_partials_return logp(0); scalar_seq_view y_vec(y); @@ -68,8 +70,9 @@ return_type_t beta_lpdf( for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); - if (y_dbl < 0 || y_dbl > 1) + if (y_dbl < 0 || y_dbl > 1) { return LOG_ZERO; + } } operands_and_partials ops_partials(y, alpha, @@ -83,10 +86,12 @@ return_type_t beta_lpdf( log1m_y(length(y)); for (size_t n = 0; n < length(y); n++) { - if (include_summand::value) + if (include_summand::value) { log_y[n] = log(value_of(y_vec[n])); - if (include_summand::value) + } + if (include_summand::value) { log1m_y[n] = log1m(value_of(y_vec[n])); + } } VectorBuilder::value, T_partials_return, @@ -96,10 +101,12 @@ return_type_t beta_lpdf( T_scale_succ> digamma_alpha(length(alpha)); for (size_t n = 0; n < length(alpha); n++) { - if (include_summand::value) + if (include_summand::value) { lgamma_alpha[n] = lgamma(value_of(alpha_vec[n])); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { digamma_alpha[n] = digamma(value_of(alpha_vec[n])); + } } VectorBuilder::value, T_partials_return, @@ -110,10 +117,12 @@ return_type_t beta_lpdf( digamma_beta(length(beta)); for (size_t n = 0; n < length(beta); n++) { - if (include_summand::value) + if (include_summand::value) { lgamma_beta[n] = lgamma(value_of(beta_vec[n])); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { digamma_beta[n] = digamma(value_of(beta_vec[n])); + } } VectorBuilder::value, @@ -127,10 +136,12 @@ return_type_t beta_lpdf( for (size_t n = 0; n < max_size(alpha, beta); n++) { const T_partials_return alpha_beta = value_of(alpha_vec[n]) + value_of(beta_vec[n]); - if (include_summand::value) + if (include_summand::value) { lgamma_alpha_beta[n] = lgamma(alpha_beta); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { digamma_alpha_beta[n] = digamma(alpha_beta); + } } for (size_t n = 0; n < N; n++) { @@ -138,26 +149,34 @@ return_type_t beta_lpdf( const T_partials_return alpha_dbl = value_of(alpha_vec[n]); const T_partials_return beta_dbl = value_of(beta_vec[n]); - if (include_summand::value) + if (include_summand::value) { logp += lgamma_alpha_beta[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= lgamma_alpha[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= lgamma_beta[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += (alpha_dbl - 1.0) * log_y[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += (beta_dbl - 1.0) * log1m_y[n]; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += (alpha_dbl - 1) / y_dbl + (beta_dbl - 1) / (y_dbl - 1); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += log_y[n] + digamma_alpha_beta[n] - digamma_alpha[n]; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += log1m_y[n] + digamma_alpha_beta[n] - digamma_beta[n]; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/beta_proportion_lccdf.hpp b/stan/math/prim/scal/prob/beta_proportion_lccdf.hpp index 9dce1600a18..60278946d94 100644 --- a/stan/math/prim/scal/prob/beta_proportion_lccdf.hpp +++ b/stan/math/prim/scal/prob/beta_proportion_lccdf.hpp @@ -46,8 +46,9 @@ return_type_t beta_proportion_lccdf(const T_y& y, static const char* function = "beta_proportion_lccdf"; - if (size_zero(y, mu, kappa)) + if (size_zero(y, mu, kappa)) { return 0.0; + } T_partials_return ccdf_log(0.0); @@ -109,10 +110,11 @@ return_type_t beta_proportion_lccdf(const T_y& y, ccdf_log += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= pow(1 - y_dbl, kappa_mukappa_dbl - 1) * pow(y_dbl, mukappa_dbl - 1) / betafunc_dbl / Pn; + } T_partials_return g1 = 0; T_partials_return g2 = 0; @@ -122,11 +124,13 @@ return_type_t beta_proportion_lccdf(const T_y& y, digamma_mukappa[n], digamma_kappa_mukappa[n], digamma_kappa[n], betafunc_dbl); } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= kappa_dbl * (g1 - g2) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= (g1 * mu_dbl + g2 * (1 - mu_dbl)) / Pn; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/beta_proportion_lcdf.hpp b/stan/math/prim/scal/prob/beta_proportion_lcdf.hpp index 9c1d6643d8d..e7794236092 100644 --- a/stan/math/prim/scal/prob/beta_proportion_lcdf.hpp +++ b/stan/math/prim/scal/prob/beta_proportion_lcdf.hpp @@ -45,8 +45,9 @@ return_type_t beta_proportion_lcdf(const T_y& y, const T_prec& kappa) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, kappa)) + if (size_zero(y, mu, kappa)) { return 0.0; + } static const char* function = "beta_proportion_lcdf"; @@ -110,10 +111,11 @@ return_type_t beta_proportion_lcdf(const T_y& y, cdf_log += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += pow(1 - y_dbl, kappa_mukappa_dbl - 1) * pow(y_dbl, mukappa_dbl - 1) / betafunc_dbl / Pn; + } T_partials_return g1 = 0; T_partials_return g2 = 0; @@ -123,11 +125,13 @@ return_type_t beta_proportion_lcdf(const T_y& y, digamma_mukappa[n], digamma_kappa_mukappa[n], digamma_kappa[n], betafunc_dbl); } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += kappa_dbl * (g1 - g2) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += (g1 * mu_dbl + g2 * (1 - mu_dbl)) / Pn; + } } return ops_partials.build(cdf_log); diff --git a/stan/math/prim/scal/prob/beta_proportion_lpdf.hpp b/stan/math/prim/scal/prob/beta_proportion_lpdf.hpp index 41bd7326d55..093511d6c7a 100644 --- a/stan/math/prim/scal/prob/beta_proportion_lpdf.hpp +++ b/stan/math/prim/scal/prob/beta_proportion_lpdf.hpp @@ -55,10 +55,12 @@ return_type_t beta_proportion_lpdf(const T_y& y, check_less_or_equal(function, "Random variable", y, 1.0); check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Precision parameter", kappa); - if (size_zero(y, mu, kappa)) + if (size_zero(y, mu, kappa)) { return 0; - if (!include_summand::value) + } + if (!include_summand::value) { return 0; + } T_partials_return logp(0); scalar_seq_view y_vec(y); @@ -69,8 +71,9 @@ return_type_t beta_proportion_lpdf(const T_y& y, for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); - if (y_dbl < 0 || y_dbl > 1) + if (y_dbl < 0 || y_dbl > 1) { return LOG_ZERO; + } } operands_and_partials ops_partials(y, mu, kappa); @@ -126,11 +129,13 @@ return_type_t beta_proportion_lpdf(const T_y& y, digamma_kappa(length(kappa)); for (size_t n = 0; n < length(kappa); n++) { - if (include_summand::value) + if (include_summand::value) { lgamma_kappa[n] = lgamma(value_of(kappa_vec[n])); + } - if (!is_constant_all::value) + if (!is_constant_all::value) { digamma_kappa[n] = digamma(value_of(kappa_vec[n])); + } } for (size_t n = 0; n < N; n++) { @@ -138,10 +143,12 @@ return_type_t beta_proportion_lpdf(const T_y& y, const T_partials_return mu_dbl = value_of(mu_vec[n]); const T_partials_return kappa_dbl = value_of(kappa_vec[n]); - if (include_summand::value) + if (include_summand::value) { logp += lgamma_kappa[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= lgamma_mukappa[n] + lgamma_kappa_mukappa[n]; + } if (include_summand::value) { const T_partials_return mukappa_dbl = mu_dbl * kappa_dbl; logp += (mukappa_dbl - 1) * log_y[n] @@ -154,15 +161,17 @@ return_type_t beta_proportion_lpdf(const T_y& y, += (mukappa_dbl - 1) / y_dbl + (kappa_dbl - mukappa_dbl - 1) / (y_dbl - 1); } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += kappa_dbl * (digamma_kappa_mukappa[n] - digamma_mukappa[n] + log_y[n] - log1m_y[n]); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += digamma_kappa[n] + mu_dbl * (log_y[n] - digamma_mukappa[n]) + (1 - mu_dbl) * (log1m_y[n] - digamma_kappa_mukappa[n]); + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/binomial_cdf.hpp b/stan/math/prim/scal/prob/binomial_cdf.hpp index dd0c7647b68..ab3c435cd6b 100644 --- a/stan/math/prim/scal/prob/binomial_cdf.hpp +++ b/stan/math/prim/scal/prob/binomial_cdf.hpp @@ -37,8 +37,9 @@ return_type_t binomial_cdf(const T_n& n, const T_N& N, static const char* function = "binomial_cdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, N, theta)) + if (size_zero(n, N, theta)) { return 1.0; + } T_partials_return P(1.0); @@ -62,8 +63,9 @@ return_type_t binomial_cdf(const T_n& n, const T_N& N, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(0.0); + } } for (size_t i = 0; i < size; i++) { @@ -82,15 +84,17 @@ return_type_t binomial_cdf(const T_n& n, const T_N& N, P *= Pi; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] -= pow(theta_dbl, n_dbl) * pow(1 - theta_dbl, N_dbl - n_dbl - 1) / betafunc / Pi; + } } if (!is_constant_all::value) { - for (size_t i = 0; i < stan::length(theta); ++i) + for (size_t i = 0; i < stan::length(theta); ++i) { ops_partials.edge1_.partials_[i] *= P; + } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/binomial_lccdf.hpp b/stan/math/prim/scal/prob/binomial_lccdf.hpp index 80914b4a8eb..e48cd29c108 100644 --- a/stan/math/prim/scal/prob/binomial_lccdf.hpp +++ b/stan/math/prim/scal/prob/binomial_lccdf.hpp @@ -38,8 +38,9 @@ return_type_t binomial_lccdf(const T_n& n, const T_N& N, static const char* function = "binomial_lccdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, N, theta)) + if (size_zero(n, N, theta)) { return 0.0; + } T_partials_return P(0.0); @@ -65,8 +66,9 @@ return_type_t binomial_lccdf(const T_n& n, const T_N& N, // The gradients are technically ill-defined, // but treated as negative infinity for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(0.0); + } } for (size_t i = 0; i < size; i++) { @@ -84,10 +86,11 @@ return_type_t binomial_lccdf(const T_n& n, const T_N& N, P += log(Pi); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += pow(theta_dbl, n_dbl) * pow(1 - theta_dbl, N_dbl - n_dbl - 1) / betafunc / Pi; + } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/binomial_lcdf.hpp b/stan/math/prim/scal/prob/binomial_lcdf.hpp index 92d79fe0a79..b5bf6e0ab58 100644 --- a/stan/math/prim/scal/prob/binomial_lcdf.hpp +++ b/stan/math/prim/scal/prob/binomial_lcdf.hpp @@ -38,8 +38,9 @@ return_type_t binomial_lcdf(const T_n& n, const T_N& N, static const char* function = "binomial_lcdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, N, theta)) + if (size_zero(n, N, theta)) { return 0.0; + } T_partials_return P(0.0); @@ -65,8 +66,9 @@ return_type_t binomial_lcdf(const T_n& n, const T_N& N, // The gradients are technically ill-defined, // but treated as negative infinity for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(negative_infinity()); + } } for (size_t i = 0; i < size; i++) { @@ -84,10 +86,11 @@ return_type_t binomial_lcdf(const T_n& n, const T_N& N, P += log(Pi); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] -= pow(theta_dbl, n_dbl) * pow(1 - theta_dbl, N_dbl - n_dbl - 1) / betafunc / Pi; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/binomial_logit_lpmf.hpp b/stan/math/prim/scal/prob/binomial_logit_lpmf.hpp index 746ddeefe5d..97229c07fd8 100644 --- a/stan/math/prim/scal/prob/binomial_logit_lpmf.hpp +++ b/stan/math/prim/scal/prob/binomial_logit_lpmf.hpp @@ -39,8 +39,9 @@ return_type_t binomial_logit_lpmf(const T_n& n, const T_N& N, static const char* function = "binomial_logit_lpmf"; - if (size_zero(n, N, alpha)) + if (size_zero(n, N, alpha)) { return 0.0; + } T_partials_return logp = 0; check_bounded(function, "Successes variable", n, 0, N); @@ -50,8 +51,9 @@ return_type_t binomial_logit_lpmf(const T_n& n, const T_N& N, "Population size parameter", N, "Probability parameter", alpha); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view n_vec(n); scalar_seq_view N_vec(N); @@ -61,23 +63,27 @@ return_type_t binomial_logit_lpmf(const T_n& n, const T_N& N, operands_and_partials ops_partials(alpha); if (include_summand::value) { - for (size_t i = 0; i < size; ++i) + for (size_t i = 0; i < size; ++i) { logp += binomial_coefficient_log(N_vec[i], n_vec[i]); + } } VectorBuilder log_inv_logit_alpha( length(alpha)); - for (size_t i = 0; i < length(alpha); ++i) + for (size_t i = 0; i < length(alpha); ++i) { log_inv_logit_alpha[i] = log_inv_logit(value_of(alpha_vec[i])); + } VectorBuilder log_inv_logit_neg_alpha( length(alpha)); - for (size_t i = 0; i < length(alpha); ++i) + for (size_t i = 0; i < length(alpha); ++i) { log_inv_logit_neg_alpha[i] = log_inv_logit(-value_of(alpha_vec[i])); + } - for (size_t i = 0; i < size; ++i) + for (size_t i = 0; i < size; ++i) { logp += n_vec[i] * log_inv_logit_alpha[i] + (N_vec[i] - n_vec[i]) * log_inv_logit_neg_alpha[i]; + } if (length(alpha) == 1) { T_partials_return temp1 = 0; @@ -93,10 +99,11 @@ return_type_t binomial_logit_lpmf(const T_n& n, const T_N& N, } } else { if (!is_constant_all::value) { - for (size_t i = 0; i < size; ++i) + for (size_t i = 0; i < size; ++i) { ops_partials.edge1_.partials_[i] += n_vec[i] * inv_logit(-value_of(alpha_vec[i])) - (N_vec[i] - n_vec[i]) * inv_logit(value_of(alpha_vec[i])); + } } } diff --git a/stan/math/prim/scal/prob/binomial_lpmf.hpp b/stan/math/prim/scal/prob/binomial_lpmf.hpp index ba28ffda579..f5f1f578d77 100644 --- a/stan/math/prim/scal/prob/binomial_lpmf.hpp +++ b/stan/math/prim/scal/prob/binomial_lpmf.hpp @@ -39,8 +39,9 @@ return_type_t binomial_lpmf(const T_n& n, const T_N& N, static const char* function = "binomial_lpmf"; - if (size_zero(n, N, theta)) + if (size_zero(n, N, theta)) { return 0.0; + } T_partials_return logp = 0; check_bounded(function, "Successes variable", n, 0, N); @@ -51,8 +52,9 @@ return_type_t binomial_lpmf(const T_n& n, const T_N& N, "Population size parameter", N, "Probability parameter", theta); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view n_vec(n); scalar_seq_view N_vec(N); @@ -62,17 +64,20 @@ return_type_t binomial_lpmf(const T_n& n, const T_N& N, operands_and_partials ops_partials(theta); if (include_summand::value) { - for (size_t i = 0; i < size; ++i) + for (size_t i = 0; i < size; ++i) { logp += binomial_coefficient_log(N_vec[i], n_vec[i]); + } } VectorBuilder log1m_theta(length(theta)); - for (size_t i = 0; i < length(theta); ++i) + for (size_t i = 0; i < length(theta); ++i) { log1m_theta[i] = log1m(value_of(theta_vec[i])); + } - for (size_t i = 0; i < size; ++i) + for (size_t i = 0; i < size; ++i) { logp += multiply_log(n_vec[i], value_of(theta_vec[i])) + (N_vec[i] - n_vec[i]) * log1m_theta[i]; + } if (length(theta) == 1) { T_partials_return temp1 = 0; @@ -88,10 +93,11 @@ return_type_t binomial_lpmf(const T_n& n, const T_N& N, } } else { if (!is_constant_all::value) { - for (size_t i = 0; i < size; ++i) + for (size_t i = 0; i < size; ++i) { ops_partials.edge1_.partials_[i] += n_vec[i] / value_of(theta_vec[i]) - (N_vec[i] - n_vec[i]) / (1.0 - value_of(theta_vec[i])); + } } } diff --git a/stan/math/prim/scal/prob/cauchy_cdf.hpp b/stan/math/prim/scal/prob/cauchy_cdf.hpp index 6abbf192a09..250c4002316 100644 --- a/stan/math/prim/scal/prob/cauchy_cdf.hpp +++ b/stan/math/prim/scal/prob/cauchy_cdf.hpp @@ -34,8 +34,9 @@ return_type_t cauchy_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 1.0; + } static const char* function = "cauchy_cdf"; @@ -57,8 +58,9 @@ return_type_t cauchy_cdf(const T_y& y, const T_loc& mu, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) + if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) { return ops_partials.build(0.0); + } } using std::atan; @@ -80,28 +82,34 @@ return_type_t cauchy_cdf(const T_y& y, const T_loc& mu, P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += sigma_inv_dbl / (pi() * (1.0 + z * z) * Pn); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += -sigma_inv_dbl / (pi() * (1.0 + z * z) * Pn); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -z * sigma_inv_dbl / (pi() * (1.0 + z * z) * Pn); + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(mu); ++n) + for (size_t n = 0; n < stan::length(mu); ++n) { ops_partials.edge2_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(sigma); ++n) + for (size_t n = 0; n < stan::length(sigma); ++n) { ops_partials.edge3_.partials_[n] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/cauchy_lccdf.hpp b/stan/math/prim/scal/prob/cauchy_lccdf.hpp index c133cbb3952..930362b03e8 100644 --- a/stan/math/prim/scal/prob/cauchy_lccdf.hpp +++ b/stan/math/prim/scal/prob/cauchy_lccdf.hpp @@ -34,8 +34,9 @@ return_type_t cauchy_lccdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 0.0; + } static const char* function = "cauchy_lccdf"; @@ -69,12 +70,15 @@ return_type_t cauchy_lccdf(const T_y& y, const T_loc& mu, const T_partials_return rep_deriv = 1.0 / (Pn * pi() * (z * z * sigma_dbl + sigma_dbl)); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += rep_deriv * z; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/cauchy_lcdf.hpp b/stan/math/prim/scal/prob/cauchy_lcdf.hpp index 13657b76747..a5229fb54c0 100644 --- a/stan/math/prim/scal/prob/cauchy_lcdf.hpp +++ b/stan/math/prim/scal/prob/cauchy_lcdf.hpp @@ -34,8 +34,9 @@ return_type_t cauchy_lcdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 0.0; + } static const char* function = "cauchy_lcdf"; @@ -70,12 +71,15 @@ return_type_t cauchy_lcdf(const T_y& y, const T_loc& mu, const T_partials_return rep_deriv = 1.0 / (pi() * Pn * (z * z * sigma_dbl + sigma_dbl)); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * z; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/cauchy_lpdf.hpp b/stan/math/prim/scal/prob/cauchy_lpdf.hpp index 734cc83512c..cfae9c74b70 100644 --- a/stan/math/prim/scal/prob/cauchy_lpdf.hpp +++ b/stan/math/prim/scal/prob/cauchy_lpdf.hpp @@ -38,8 +38,9 @@ return_type_t cauchy_lpdf(const T_y& y, const T_loc& mu, static const char* function = "cauchy_lpdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 0.0; + } T_partials_return logp(0.0); @@ -49,8 +50,9 @@ return_type_t cauchy_lpdf(const T_y& y, const T_loc& mu, check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } using std::log; @@ -85,23 +87,29 @@ return_type_t cauchy_lpdf(const T_y& y, const T_loc& mu, const T_partials_return y_minus_mu_over_sigma_squared = y_minus_mu_over_sigma * y_minus_mu_over_sigma; - if (include_summand::value) + if (include_summand::value) { logp += NEG_LOG_PI; - if (include_summand::value) + } + if (include_summand::value) { logp -= log_sigma[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= log1p(y_minus_mu_over_sigma_squared); + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= 2 * y_minus_mu / (sigma_squared[n] + y_minus_mu_squared); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += 2 * y_minus_mu / (sigma_squared[n] + y_minus_mu_squared); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += (y_minus_mu_squared - sigma_squared[n]) * inv_sigma[n] / (sigma_squared[n] + y_minus_mu_squared); + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/chi_square_cdf.hpp b/stan/math/prim/scal/prob/chi_square_cdf.hpp index 4493504b5d1..12be2cc3d25 100644 --- a/stan/math/prim/scal/prob/chi_square_cdf.hpp +++ b/stan/math/prim/scal/prob/chi_square_cdf.hpp @@ -38,8 +38,9 @@ return_type_t chi_square_cdf(const T_y& y, const T_dof& nu) { T_partials_return cdf(1.0); - if (size_zero(y, nu)) + if (size_zero(y, nu)) { return cdf; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -56,8 +57,9 @@ return_type_t chi_square_cdf(const T_y& y, const T_dof& nu) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(0.0); + } } using std::exp; @@ -79,8 +81,9 @@ return_type_t chi_square_cdf(const T_y& y, const T_dof& nu) { for (size_t n = 0; n < N; n++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(y_vec[n]) == std::numeric_limits::infinity()) + if (value_of(y_vec[n]) == std::numeric_limits::infinity()) { continue; + } const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return alpha_dbl = value_of(nu_vec[n]) * 0.5; @@ -90,25 +93,29 @@ return_type_t chi_square_cdf(const T_y& y, const T_dof& nu) { cdf *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += beta_dbl * exp(-beta_dbl * y_dbl) * pow(beta_dbl * y_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= 0.5 * grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_dbl, gamma_vec[n], digamma_vec[n]) / Pn; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(nu); ++n) + for (size_t n = 0; n < stan::length(nu); ++n) { ops_partials.edge2_.partials_[n] *= cdf; + } } return ops_partials.build(cdf); } diff --git a/stan/math/prim/scal/prob/chi_square_lccdf.hpp b/stan/math/prim/scal/prob/chi_square_lccdf.hpp index 737fb7d8296..463dd23d029 100644 --- a/stan/math/prim/scal/prob/chi_square_lccdf.hpp +++ b/stan/math/prim/scal/prob/chi_square_lccdf.hpp @@ -39,8 +39,9 @@ return_type_t chi_square_lccdf(const T_y& y, const T_dof& nu) { T_partials_return ccdf_log(0.0); - if (size_zero(y, nu)) + if (size_zero(y, nu)) { return ccdf_log; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -57,8 +58,9 @@ return_type_t chi_square_lccdf(const T_y& y, const T_dof& nu) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(0.0); + } } using std::exp; @@ -81,8 +83,9 @@ return_type_t chi_square_lccdf(const T_y& y, const T_dof& nu) { for (size_t n = 0; n < N; n++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(y_vec[n]) == std::numeric_limits::infinity()) + if (value_of(y_vec[n]) == std::numeric_limits::infinity()) { return ops_partials.build(negative_infinity()); + } const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return alpha_dbl = value_of(nu_vec[n]) * 0.5; @@ -92,16 +95,18 @@ return_type_t chi_square_lccdf(const T_y& y, const T_dof& nu) { ccdf_log += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= beta_dbl * exp(-beta_dbl * y_dbl) * pow(beta_dbl * y_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += 0.5 * grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_dbl, gamma_vec[n], digamma_vec[n]) / Pn; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/chi_square_lcdf.hpp b/stan/math/prim/scal/prob/chi_square_lcdf.hpp index a1d0a0e5289..26a5281ce47 100644 --- a/stan/math/prim/scal/prob/chi_square_lcdf.hpp +++ b/stan/math/prim/scal/prob/chi_square_lcdf.hpp @@ -39,8 +39,9 @@ return_type_t chi_square_lcdf(const T_y& y, const T_dof& nu) { T_partials_return cdf_log(0.0); - if (size_zero(y, nu)) + if (size_zero(y, nu)) { return cdf_log; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -57,8 +58,9 @@ return_type_t chi_square_lcdf(const T_y& y, const T_dof& nu) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(negative_infinity()); + } } using std::exp; @@ -81,8 +83,9 @@ return_type_t chi_square_lcdf(const T_y& y, const T_dof& nu) { for (size_t n = 0; n < N; n++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(y_vec[n]) == std::numeric_limits::infinity()) + if (value_of(y_vec[n]) == std::numeric_limits::infinity()) { return ops_partials.build(0.0); + } const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return alpha_dbl = value_of(nu_vec[n]) * 0.5; @@ -92,16 +95,18 @@ return_type_t chi_square_lcdf(const T_y& y, const T_dof& nu) { cdf_log += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += beta_dbl * exp(-beta_dbl * y_dbl) * pow(beta_dbl * y_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= 0.5 * grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_dbl, gamma_vec[n], digamma_vec[n]) / Pn; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/chi_square_lpdf.hpp b/stan/math/prim/scal/prob/chi_square_lpdf.hpp index 5a5f1790913..3c8aeb2c7f3 100644 --- a/stan/math/prim/scal/prob/chi_square_lpdf.hpp +++ b/stan/math/prim/scal/prob/chi_square_lpdf.hpp @@ -45,8 +45,9 @@ return_type_t chi_square_lpdf(const T_y& y, const T_dof& nu) { check_positive_finite(function, "Degrees of freedom parameter", nu); check_consistent_sizes(function, "Random variable", y, "Degrees of freedom parameter", nu); - if (size_zero(y, nu)) + if (size_zero(y, nu)) { return 0; + } T_partials_return logp(0); @@ -54,27 +55,34 @@ return_type_t chi_square_lpdf(const T_y& y, const T_dof& nu) { scalar_seq_view nu_vec(nu); size_t N = max_size(y, nu); - for (size_t n = 0; n < length(y); n++) - if (value_of(y_vec[n]) < 0) + for (size_t n = 0; n < length(y); n++) { + if (value_of(y_vec[n]) < 0) { return LOG_ZERO; + } + } - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } using std::log; VectorBuilder::value, T_partials_return, T_y> log_y(length(y)); - for (size_t i = 0; i < length(y); i++) - if (include_summand::value) + for (size_t i = 0; i < length(y); i++) { + if (include_summand::value) { log_y[i] = log(value_of(y_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_y> inv_y(length(y)); - for (size_t i = 0; i < length(y); i++) - if (include_summand::value) + for (size_t i = 0; i < length(y); i++) { + if (include_summand::value) { inv_y[i] = 1.0 / value_of(y_vec[i]); + } + } VectorBuilder::value, T_partials_return, T_dof> lgamma_half_nu(length(nu)); @@ -83,10 +91,12 @@ return_type_t chi_square_lpdf(const T_y& y, const T_dof& nu) { for (size_t i = 0; i < length(nu); i++) { T_partials_return half_nu = 0.5 * value_of(nu_vec[i]); - if (include_summand::value) + if (include_summand::value) { lgamma_half_nu[i] = lgamma(half_nu); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { digamma_half_nu_over_two[i] = digamma(half_nu) * 0.5; + } } operands_and_partials ops_partials(y, nu); @@ -96,12 +106,15 @@ return_type_t chi_square_lpdf(const T_y& y, const T_dof& nu) { const T_partials_return half_y = 0.5 * y_dbl; const T_partials_return nu_dbl = value_of(nu_vec[n]); const T_partials_return half_nu = 0.5 * nu_dbl; - if (include_summand::value) + if (include_summand::value) { logp += nu_dbl * NEG_LOG_TWO_OVER_TWO - lgamma_half_nu[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += (half_nu - 1.0) * log_y[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= half_y; + } if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += (half_nu - 1.0) * inv_y[n] - 0.5; diff --git a/stan/math/prim/scal/prob/double_exponential_cdf.hpp b/stan/math/prim/scal/prob/double_exponential_cdf.hpp index 61577bfddd4..539437a4d17 100644 --- a/stan/math/prim/scal/prob/double_exponential_cdf.hpp +++ b/stan/math/prim/scal/prob/double_exponential_cdf.hpp @@ -32,8 +32,9 @@ return_type_t double_exponential_cdf( static const char* function = "double_exponential_cdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 1.0; + } using std::exp; @@ -57,10 +58,11 @@ return_type_t double_exponential_cdf( const T_partials_return scaled_diff = (y_dbl - mu_dbl) / (sigma_dbl); const T_partials_return exp_scaled_diff = exp(scaled_diff); - if (y_dbl < mu_dbl) + if (y_dbl < mu_dbl) { cdf *= exp_scaled_diff * 0.5; - else + } else { cdf *= 1.0 - 0.5 / exp_scaled_diff; + } } for (size_t n = 0; n < N; n++) { @@ -72,21 +74,27 @@ return_type_t double_exponential_cdf( const T_partials_return inv_sigma = 1.0 / sigma_dbl; if (y_dbl < mu_dbl) { - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += inv_sigma * cdf; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= inv_sigma * cdf; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= scaled_diff * inv_sigma * cdf; + } } else { const T_partials_return rep_deriv = cdf * inv_sigma / (2.0 * exp_scaled_diff - 1.0); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * scaled_diff; + } } } return ops_partials.build(cdf); diff --git a/stan/math/prim/scal/prob/double_exponential_lccdf.hpp b/stan/math/prim/scal/prob/double_exponential_lccdf.hpp index 9516dcf28f4..aa8f6d231dd 100644 --- a/stan/math/prim/scal/prob/double_exponential_lccdf.hpp +++ b/stan/math/prim/scal/prob/double_exponential_lccdf.hpp @@ -37,8 +37,9 @@ return_type_t double_exponential_lccdf( T_partials_return ccdf_log(0.0); - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return ccdf_log; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -67,21 +68,27 @@ return_type_t double_exponential_lccdf( ccdf_log += log1m(0.5 * exp(scaled_diff)); const T_partials_return rep_deriv = 1.0 / (2.0 * exp(-scaled_diff) - 1.0); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= rep_deriv * inv_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += rep_deriv * inv_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += rep_deriv * scaled_diff * inv_sigma; + } } else { ccdf_log += log_half - scaled_diff; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= inv_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += inv_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += scaled_diff * inv_sigma; + } } } return ops_partials.build(ccdf_log); diff --git a/stan/math/prim/scal/prob/double_exponential_lcdf.hpp b/stan/math/prim/scal/prob/double_exponential_lcdf.hpp index 00d2a7ff1ee..0b01b4a34cc 100644 --- a/stan/math/prim/scal/prob/double_exponential_lcdf.hpp +++ b/stan/math/prim/scal/prob/double_exponential_lcdf.hpp @@ -36,8 +36,9 @@ return_type_t double_exponential_lcdf( T_partials_return cdf_log(0.0); - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return cdf_log; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -65,22 +66,28 @@ return_type_t double_exponential_lcdf( if (y_dbl < mu_dbl) { cdf_log += log_half + scaled_diff; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += inv_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= inv_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= scaled_diff * inv_sigma; + } } else { cdf_log += log1m(0.5 * exp(-scaled_diff)); const T_partials_return rep_deriv = 1.0 / (2.0 * exp(scaled_diff) - 1.0); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv * inv_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv * inv_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * scaled_diff * inv_sigma; + } } } return ops_partials.build(cdf_log); diff --git a/stan/math/prim/scal/prob/double_exponential_lpdf.hpp b/stan/math/prim/scal/prob/double_exponential_lpdf.hpp index edec00d23bc..9e8188935dc 100644 --- a/stan/math/prim/scal/prob/double_exponential_lpdf.hpp +++ b/stan/math/prim/scal/prob/double_exponential_lpdf.hpp @@ -37,8 +37,9 @@ return_type_t double_exponential_lpdf( using std::fabs; using std::log; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 0.0; + } T_partials_return logp(0.0); check_finite(function, "Random variable", y); @@ -47,8 +48,9 @@ return_type_t double_exponential_lpdf( check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Shape parameter", sigma); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); @@ -66,12 +68,15 @@ return_type_t double_exponential_lpdf( log_sigma(length(sigma)); for (size_t i = 0; i < length(sigma); i++) { const T_partials_return sigma_dbl = value_of(sigma_vec[i]); - if (include_summand::value) + if (include_summand::value) { inv_sigma[i] = 1.0 / sigma_dbl; - if (include_summand::value) + } + if (include_summand::value) { log_sigma[i] = log(value_of(sigma_vec[i])); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { inv_sigma_squared[i] = inv_sigma[i] * inv_sigma[i]; + } } for (size_t n = 0; n < N; n++) { @@ -81,25 +86,30 @@ return_type_t double_exponential_lpdf( const T_partials_return y_m_mu = y_dbl - mu_dbl; const T_partials_return fabs_y_m_mu = fabs(y_m_mu); - if (include_summand::value) + if (include_summand::value) { logp += NEG_LOG_TWO; - if (include_summand::value) + } + if (include_summand::value) { logp -= log_sigma[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= fabs_y_m_mu * inv_sigma[n]; + } T_partials_return sign_y_m_mu_times_inv_sigma(0); - if (!is_constant_all::value) + if (!is_constant_all::value) { sign_y_m_mu_times_inv_sigma = sign(y_m_mu) * inv_sigma[n]; + } if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= sign_y_m_mu_times_inv_sigma; } if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += sign_y_m_mu_times_inv_sigma; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -inv_sigma[n] + fabs_y_m_mu * inv_sigma_squared[n]; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/exp_mod_normal_cdf.hpp b/stan/math/prim/scal/prob/exp_mod_normal_cdf.hpp index b1eb0b54e43..da0527ffe79 100644 --- a/stan/math/prim/scal/prob/exp_mod_normal_cdf.hpp +++ b/stan/math/prim/scal/prob/exp_mod_normal_cdf.hpp @@ -25,8 +25,9 @@ return_type_t exp_mod_normal_cdf( T_inv_scale>::type T_partials_return; T_partials_return cdf(1.0); - if (size_zero(y, mu, sigma, lambda)) + if (size_zero(y, mu, sigma, lambda)) { return cdf; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -51,8 +52,9 @@ return_type_t exp_mod_normal_cdf( const double sqrt_pi = std::sqrt(pi()); for (size_t n = 0; n < N; n++) { if (is_inf(y_vec[n])) { - if (y_vec[n] < 0.0) + if (y_vec[n] < 0.0) { return ops_partials.build(0.0); + } } const T_partials_return y_dbl = value_of(y_vec[n]); @@ -83,11 +85,13 @@ return_type_t exp_mod_normal_cdf( cdf *= cdf_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += (deriv_1 - deriv_2 + deriv_3) / cdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += (-deriv_1 + deriv_2 - deriv_3) / cdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += (-deriv_1 * v - deriv_3 * scaled_diff * SQRT_2 - deriv_2 * sigma_dbl * SQRT_2 @@ -95,7 +99,8 @@ return_type_t exp_mod_normal_cdf( * (-lambda_dbl + scaled_diff * SQRT_2 / sigma_dbl) - SQRT_2 * lambda_dbl)) / cdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += exp(0.5 * v_sq - u) * (SQRT_2 / sqrt_pi * 0.5 * sigma_dbl @@ -103,23 +108,28 @@ return_type_t exp_mod_normal_cdf( * (v / SQRT_2 - scaled_diff)) - (v * sigma_dbl + mu_dbl - y_dbl) * erf_calc) / cdf_; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(mu); ++n) + for (size_t n = 0; n < stan::length(mu); ++n) { ops_partials.edge2_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(sigma); ++n) + for (size_t n = 0; n < stan::length(sigma); ++n) { ops_partials.edge3_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(lambda); ++n) + for (size_t n = 0; n < stan::length(lambda); ++n) { ops_partials.edge4_.partials_[n] *= cdf; + } } return ops_partials.build(cdf); } diff --git a/stan/math/prim/scal/prob/exp_mod_normal_lccdf.hpp b/stan/math/prim/scal/prob/exp_mod_normal_lccdf.hpp index 25d0487672a..a886c76c58d 100644 --- a/stan/math/prim/scal/prob/exp_mod_normal_lccdf.hpp +++ b/stan/math/prim/scal/prob/exp_mod_normal_lccdf.hpp @@ -24,8 +24,9 @@ return_type_t exp_mod_normal_lccdf( T_partials_return; T_partials_return ccdf_log(0.0); - if (size_zero(y, mu, sigma, lambda)) + if (size_zero(y, mu, sigma, lambda)) { return ccdf_log; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -51,10 +52,11 @@ return_type_t exp_mod_normal_lccdf( const double sqrt_pi = std::sqrt(pi()); for (size_t n = 0; n < N; n++) { if (is_inf(y_vec[n])) { - if (y_vec[n] > 0.0) + if (y_vec[n] > 0.0) { return ops_partials.build(negative_infinity()); - else + } else { return ops_partials.build(0.0); + } } const T_partials_return y_dbl = value_of(y_vec[n]); @@ -87,12 +89,14 @@ return_type_t exp_mod_normal_lccdf( ccdf_log += log(ccdf_); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= (deriv_1 - deriv_2 + deriv_3) / ccdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= (-deriv_1 + deriv_2 - deriv_3) / ccdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= (-deriv_1 * v - deriv_3 * scaled_diff * SQRT_2 - deriv_2 * sigma_dbl * SQRT_2 @@ -100,7 +104,8 @@ return_type_t exp_mod_normal_lccdf( * (-lambda_dbl + scaled_diff * SQRT_2 / sigma_dbl) - SQRT_2 * lambda_dbl)) / ccdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] -= exp(0.5 * v_sq - u) * (SQRT_2 / sqrt_pi * 0.5 * sigma_dbl @@ -108,6 +113,7 @@ return_type_t exp_mod_normal_lccdf( * (v / SQRT_2 - scaled_diff)) - (v * sigma_dbl + mu_dbl - y_dbl) * erf_calc2) / ccdf_; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/exp_mod_normal_lcdf.hpp b/stan/math/prim/scal/prob/exp_mod_normal_lcdf.hpp index 00d47202bc1..8da6fdcbab5 100644 --- a/stan/math/prim/scal/prob/exp_mod_normal_lcdf.hpp +++ b/stan/math/prim/scal/prob/exp_mod_normal_lcdf.hpp @@ -25,8 +25,9 @@ return_type_t exp_mod_normal_lcdf( T_inv_scale>::type T_partials_return; T_partials_return cdf_log(0.0); - if (size_zero(y, mu, sigma, lambda)) + if (size_zero(y, mu, sigma, lambda)) { return cdf_log; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -52,10 +53,11 @@ return_type_t exp_mod_normal_lcdf( const double sqrt_pi = std::sqrt(pi()); for (size_t n = 0; n < N; n++) { if (is_inf(y_vec[n])) { - if (y_vec[n] < 0.0) + if (y_vec[n] < 0.0) { return ops_partials.build(negative_infinity()); - else + } else { return ops_partials.build(0.0); + } } const T_partials_return y_dbl = value_of(y_vec[n]); @@ -88,12 +90,14 @@ return_type_t exp_mod_normal_lcdf( cdf_log += log(cdf_); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += (deriv_1 - deriv_2 + deriv_3) / denom; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += (-deriv_1 + deriv_2 - deriv_3) / denom; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += (-deriv_1 * v - deriv_3 * scaled_diff * SQRT_2 - deriv_2 * sigma_dbl * SQRT_2 @@ -101,7 +105,8 @@ return_type_t exp_mod_normal_lcdf( * (-lambda_dbl + scaled_diff * SQRT_2 / sigma_dbl) - SQRT_2 * lambda_dbl)) / denom; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += exp(0.5 * v_sq - u) * (SQRT_2 / sqrt_pi * 0.5 * sigma_dbl @@ -109,6 +114,7 @@ return_type_t exp_mod_normal_lcdf( * (v / SQRT_2 - scaled_diff)) - (v * sigma_dbl + mu_dbl - y_dbl) * erf_calc2) / denom; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/exp_mod_normal_lpdf.hpp b/stan/math/prim/scal/prob/exp_mod_normal_lpdf.hpp index 06dfb3a4422..e4d5035044e 100644 --- a/stan/math/prim/scal/prob/exp_mod_normal_lpdf.hpp +++ b/stan/math/prim/scal/prob/exp_mod_normal_lpdf.hpp @@ -24,8 +24,9 @@ return_type_t exp_mod_normal_lpdf( typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, sigma, lambda)) + if (size_zero(y, mu, sigma, lambda)) { return 0.0; + } T_partials_return logp(0.0); @@ -37,8 +38,9 @@ return_type_t exp_mod_normal_lpdf( mu, "Scale parameter", sigma, "Inv_scale paramter", lambda); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } using std::exp; using std::log; @@ -61,15 +63,18 @@ return_type_t exp_mod_normal_lpdf( const T_partials_return pi_dbl = boost::math::constants::pi(); - if (include_summand::value) + if (include_summand::value) { logp -= log(2.0); - if (include_summand::value) + } + if (include_summand::value) { logp += log(lambda_dbl); - if (include_summand::value) + } + if (include_summand::value) { logp += lambda_dbl * (mu_dbl + 0.5 * lambda_dbl * sigma_dbl * sigma_dbl - y_dbl) + log(erfc((mu_dbl + lambda_dbl * sigma_dbl * sigma_dbl - y_dbl) / (sqrt(2.0) * sigma_dbl))); + } const T_partials_return deriv_logerfc = -2.0 / sqrt(pi_dbl) @@ -80,23 +85,27 @@ return_type_t exp_mod_normal_lpdf( / erfc((mu_dbl + lambda_dbl * sigma_dbl * sigma_dbl - y_dbl) / (sigma_dbl * std::sqrt(2.0))); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += -lambda_dbl + deriv_logerfc * -1.0 / (sigma_dbl * std::sqrt(2.0)); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += lambda_dbl + deriv_logerfc / (sigma_dbl * std::sqrt(2.0)); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += sigma_dbl * lambda_dbl * lambda_dbl + deriv_logerfc * (-mu_dbl / (sigma_dbl * sigma_dbl * std::sqrt(2.0)) + lambda_dbl / std::sqrt(2.0) + y_dbl / (sigma_dbl * sigma_dbl * std::sqrt(2.0))); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += 1 / lambda_dbl + lambda_dbl * sigma_dbl * sigma_dbl + mu_dbl - y_dbl + deriv_logerfc * sigma_dbl / std::sqrt(2.0); + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/exp_mod_normal_rng.hpp b/stan/math/prim/scal/prob/exp_mod_normal_rng.hpp index b14dde12be0..7360271ba7c 100644 --- a/stan/math/prim/scal/prob/exp_mod_normal_rng.hpp +++ b/stan/math/prim/scal/prob/exp_mod_normal_rng.hpp @@ -53,9 +53,10 @@ exp_mod_normal_rng(const T_loc& mu, const T_scale& sigma, size_t N = max_size(mu, sigma, lambda); VectorBuilder output(N); - for (size_t n = 0; n < N; ++n) + for (size_t n = 0; n < N; ++n) { output[n] = normal_rng(mu_vec[n], sigma_vec[n], rng) + exponential_rng(lambda_vec[n], rng); + } return output.data(); } diff --git a/stan/math/prim/scal/prob/exponential_cdf.hpp b/stan/math/prim/scal/prob/exponential_cdf.hpp index acf4000cb27..8fc18df1036 100644 --- a/stan/math/prim/scal/prob/exponential_cdf.hpp +++ b/stan/math/prim/scal/prob/exponential_cdf.hpp @@ -34,8 +34,9 @@ return_type_t exponential_cdf(const T_y& y, using std::exp; T_partials_return cdf(1.0); - if (size_zero(y, beta)) + if (size_zero(y, beta)) { return cdf; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -60,10 +61,12 @@ return_type_t exponential_cdf(const T_y& y, const T_partials_return one_m_exp = 1.0 - exp(-beta_dbl * y_dbl); T_partials_return rep_deriv = exp(-beta_dbl * y_dbl) / one_m_exp; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv * beta_dbl * cdf; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += rep_deriv * y_dbl * cdf; + } } return ops_partials.build(cdf); } diff --git a/stan/math/prim/scal/prob/exponential_lccdf.hpp b/stan/math/prim/scal/prob/exponential_lccdf.hpp index caeaee34df3..b8f6f8bc15d 100644 --- a/stan/math/prim/scal/prob/exponential_lccdf.hpp +++ b/stan/math/prim/scal/prob/exponential_lccdf.hpp @@ -19,8 +19,9 @@ return_type_t exponential_lccdf(const T_y& y, static const char* function = "exponential_lccdf"; T_partials_return ccdf_log(0.0); - if (size_zero(y, beta)) + if (size_zero(y, beta)) { return ccdf_log; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -36,10 +37,12 @@ return_type_t exponential_lccdf(const T_y& y, const T_partials_return y_dbl = value_of(y_vec[n]); ccdf_log += -beta_dbl * y_dbl; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= beta_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= y_dbl; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/exponential_lcdf.hpp b/stan/math/prim/scal/prob/exponential_lcdf.hpp index 9c5246f77f7..bb437c6feba 100644 --- a/stan/math/prim/scal/prob/exponential_lcdf.hpp +++ b/stan/math/prim/scal/prob/exponential_lcdf.hpp @@ -23,8 +23,9 @@ return_type_t exponential_lcdf(const T_y& y, using std::log; T_partials_return cdf_log(0.0); - if (size_zero(y, beta)) + if (size_zero(y, beta)) { return cdf_log; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -42,10 +43,12 @@ return_type_t exponential_lcdf(const T_y& y, cdf_log += log(one_m_exp); T_partials_return rep_deriv = -exp(-beta_dbl * y_dbl) / one_m_exp; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= rep_deriv * beta_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv * y_dbl; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/exponential_lpdf.hpp b/stan/math/prim/scal/prob/exponential_lpdf.hpp index 2f33aa9a61f..fc5a5f4734e 100644 --- a/stan/math/prim/scal/prob/exponential_lpdf.hpp +++ b/stan/math/prim/scal/prob/exponential_lpdf.hpp @@ -45,8 +45,9 @@ return_type_t exponential_lpdf(const T_y& y, static const char* function = "exponential_lpdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(y, beta)) + if (size_zero(y, beta)) { return 0.0; + } using std::log; @@ -63,24 +64,30 @@ return_type_t exponential_lpdf(const T_y& y, VectorBuilder::value, T_partials_return, T_inv_scale> log_beta(length(beta)); - for (size_t i = 0; i < length(beta); i++) - if (include_summand::value) + for (size_t i = 0; i < length(beta); i++) { + if (include_summand::value) { log_beta[i] = log(value_of(beta_vec[i])); + } + } operands_and_partials ops_partials(y, beta); for (size_t n = 0; n < N; n++) { const T_partials_return beta_dbl = value_of(beta_vec[n]); const T_partials_return y_dbl = value_of(y_vec[n]); - if (include_summand::value) + if (include_summand::value) { logp += log_beta[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= beta_dbl * y_dbl; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= beta_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += 1 / beta_dbl - y_dbl; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/frechet_cdf.hpp b/stan/math/prim/scal/prob/frechet_cdf.hpp index 4ee93001111..52379911abe 100644 --- a/stan/math/prim/scal/prob/frechet_cdf.hpp +++ b/stan/math/prim/scal/prob/frechet_cdf.hpp @@ -31,8 +31,9 @@ return_type_t frechet_cdf(const T_y& y, using std::exp; using std::log; - if (size_zero(y, alpha, sigma)) + if (size_zero(y, alpha, sigma)) { return 1.0; + } T_partials_return cdf(1.0); check_positive(function, "Random variable", y); @@ -54,25 +55,31 @@ return_type_t frechet_cdf(const T_y& y, cdf *= cdf_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += pow_ * alpha_dbl / y_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += pow_ * log(y_dbl / sigma_dbl); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= pow_ * alpha_dbl / sigma_dbl; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(alpha); ++n) + for (size_t n = 0; n < stan::length(alpha); ++n) { ops_partials.edge2_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(sigma); ++n) + for (size_t n = 0; n < stan::length(sigma); ++n) { ops_partials.edge3_.partials_[n] *= cdf; + } } return ops_partials.build(cdf); } diff --git a/stan/math/prim/scal/prob/frechet_lccdf.hpp b/stan/math/prim/scal/prob/frechet_lccdf.hpp index 52541c3f2bb..2c37d779863 100644 --- a/stan/math/prim/scal/prob/frechet_lccdf.hpp +++ b/stan/math/prim/scal/prob/frechet_lccdf.hpp @@ -29,8 +29,9 @@ return_type_t frechet_lccdf(const T_y& y, using boost::math::tools::promote_args; - if (size_zero(y, alpha, sigma)) + if (size_zero(y, alpha, sigma)) { return 0.0; + } T_partials_return ccdf_log(0.0); check_positive(function, "Random variable", y); @@ -56,12 +57,15 @@ return_type_t frechet_lccdf(const T_y& y, ccdf_log += log1m(exp_); const T_partials_return rep_deriv_ = pow_ / (1.0 / exp_ - 1); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= alpha_dbl / y_dbl * rep_deriv_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= log(y_dbl / sigma_dbl) * rep_deriv_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += alpha_dbl / sigma_dbl * rep_deriv_; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/frechet_lcdf.hpp b/stan/math/prim/scal/prob/frechet_lcdf.hpp index 9ff183def44..6842ff29671 100644 --- a/stan/math/prim/scal/prob/frechet_lcdf.hpp +++ b/stan/math/prim/scal/prob/frechet_lcdf.hpp @@ -30,8 +30,9 @@ return_type_t frechet_lcdf(const T_y& y, using boost::math::tools::promote_args; using std::log; - if (size_zero(y, alpha, sigma)) + if (size_zero(y, alpha, sigma)) { return 0.0; + } T_partials_return cdf_log(0.0); check_positive(function, "Random variable", y); @@ -52,12 +53,15 @@ return_type_t frechet_lcdf(const T_y& y, cdf_log -= pow_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += pow_ * alpha_dbl / y_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += pow_ * log(y_dbl / sigma_dbl); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= pow_ * alpha_dbl / sigma_dbl; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/frechet_lpdf.hpp b/stan/math/prim/scal/prob/frechet_lpdf.hpp index fcba9157a4d..3f27ca50b88 100644 --- a/stan/math/prim/scal/prob/frechet_lpdf.hpp +++ b/stan/math/prim/scal/prob/frechet_lpdf.hpp @@ -34,10 +34,12 @@ return_type_t frechet_lpdf(const T_y& y, check_consistent_sizes(function, "Random variable", y, "Shape parameter", alpha, "Scale parameter", sigma); - if (size_zero(y, alpha, sigma)) + if (size_zero(y, alpha, sigma)) { return 0; - if (!include_summand::value) + } + if (!include_summand::value) { return 0; + } T_partials_return logp(0); @@ -49,52 +51,65 @@ return_type_t frechet_lpdf(const T_y& y, VectorBuilder::value, T_partials_return, T_shape> log_alpha(length(alpha)); - for (size_t i = 0; i < length(alpha); i++) - if (include_summand::value) + for (size_t i = 0; i < length(alpha); i++) { + if (include_summand::value) { log_alpha[i] = log(value_of(alpha_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_y> log_y(length(y)); - for (size_t i = 0; i < length(y); i++) - if (include_summand::value) + for (size_t i = 0; i < length(y); i++) { + if (include_summand::value) { log_y[i] = log(value_of(y_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_scale> log_sigma(length(sigma)); - for (size_t i = 0; i < length(sigma); i++) - if (include_summand::value) + for (size_t i = 0; i < length(sigma); i++) { + if (include_summand::value) { log_sigma[i] = log(value_of(sigma_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_y> inv_y(length(y)); - for (size_t i = 0; i < length(y); i++) - if (include_summand::value) + for (size_t i = 0; i < length(y); i++) { + if (include_summand::value) { inv_y[i] = 1.0 / value_of(y_vec[i]); + } + } VectorBuilder::value, T_partials_return, T_y, T_shape, T_scale> sigma_div_y_pow_alpha(N); - for (size_t i = 0; i < N; i++) + for (size_t i = 0; i < N; i++) { if (include_summand::value) { const T_partials_return alpha_dbl = value_of(alpha_vec[i]); sigma_div_y_pow_alpha[i] = pow(inv_y[i] * value_of(sigma_vec[i]), alpha_dbl); } + } operands_and_partials ops_partials(y, alpha, sigma); for (size_t n = 0; n < N; n++) { const T_partials_return alpha_dbl = value_of(alpha_vec[n]); - if (include_summand::value) + if (include_summand::value) { logp += log_alpha[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= (alpha_dbl + 1.0) * log_y[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += alpha_dbl * log_sigma[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= sigma_div_y_pow_alpha[n]; + } if (!is_constant_all::value) { const T_partials_return inv_y_dbl = value_of(inv_y[n]); @@ -102,13 +117,15 @@ return_type_t frechet_lpdf(const T_y& y, += -(alpha_dbl + 1.0) * inv_y_dbl + alpha_dbl * sigma_div_y_pow_alpha[n] * inv_y_dbl; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += 1.0 / alpha_dbl + (1.0 - sigma_div_y_pow_alpha[n]) * (log_sigma[n] - log_y[n]); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += alpha_dbl / value_of(sigma_vec[n]) * (1 - sigma_div_y_pow_alpha[n]); + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/gamma_cdf.hpp b/stan/math/prim/scal/prob/gamma_cdf.hpp index db08cf8419e..d6365b8558e 100644 --- a/stan/math/prim/scal/prob/gamma_cdf.hpp +++ b/stan/math/prim/scal/prob/gamma_cdf.hpp @@ -42,8 +42,9 @@ template return_type_t gamma_cdf(const T_y& y, const T_shape& alpha, const T_inv_scale& beta) { - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 1.0; + } typedef partials_return_type_t T_partials_return; static const char* function = "gamma_cdf"; @@ -70,8 +71,9 @@ return_type_t gamma_cdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(0.0); + } } using std::exp; @@ -93,8 +95,9 @@ return_type_t gamma_cdf(const T_y& y, for (size_t n = 0; n < N; n++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(y_vec[n]) == std::numeric_limits::infinity()) + if (value_of(y_vec[n]) == std::numeric_limits::infinity()) { continue; + } const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return alpha_dbl = value_of(alpha_vec[n]); @@ -104,32 +107,38 @@ return_type_t gamma_cdf(const T_y& y, P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += beta_dbl * exp(-beta_dbl * y_dbl) * pow(beta_dbl * y_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_dbl, gamma_vec[n], digamma_vec[n]) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += y_dbl * exp(-beta_dbl * y_dbl) * pow(beta_dbl * y_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(alpha); ++n) + for (size_t n = 0; n < stan::length(alpha); ++n) { ops_partials.edge2_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(beta); ++n) + for (size_t n = 0; n < stan::length(beta); ++n) { ops_partials.edge3_.partials_[n] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/gamma_lccdf.hpp b/stan/math/prim/scal/prob/gamma_lccdf.hpp index 9b0cba4b978..90895a732b6 100644 --- a/stan/math/prim/scal/prob/gamma_lccdf.hpp +++ b/stan/math/prim/scal/prob/gamma_lccdf.hpp @@ -23,8 +23,9 @@ template return_type_t gamma_lccdf(const T_y& y, const T_shape& alpha, const T_inv_scale& beta) { - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0.0; + } typedef partials_return_type_t T_partials_return; @@ -49,8 +50,9 @@ return_type_t gamma_lccdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(0.0); + } } using std::exp; @@ -73,8 +75,9 @@ return_type_t gamma_lccdf(const T_y& y, for (size_t n = 0; n < N; n++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(y_vec[n]) == std::numeric_limits::infinity()) + if (value_of(y_vec[n]) == std::numeric_limits::infinity()) { return ops_partials.build(negative_infinity()); + } const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return alpha_dbl = value_of(alpha_vec[n]); @@ -84,19 +87,22 @@ return_type_t gamma_lccdf(const T_y& y, P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= beta_dbl * exp(-beta_dbl * y_dbl) * pow(beta_dbl * y_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_dbl, gamma_vec[n], digamma_vec[n]) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= y_dbl * exp(-beta_dbl * y_dbl) * pow(beta_dbl * y_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/gamma_lcdf.hpp b/stan/math/prim/scal/prob/gamma_lcdf.hpp index b72276073ff..972fa7cea7f 100644 --- a/stan/math/prim/scal/prob/gamma_lcdf.hpp +++ b/stan/math/prim/scal/prob/gamma_lcdf.hpp @@ -23,8 +23,9 @@ template return_type_t gamma_lcdf(const T_y& y, const T_shape& alpha, const T_inv_scale& beta) { - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0.0; + } typedef partials_return_type_t T_partials_return; static const char* function = "gamma_lcdf"; @@ -48,8 +49,9 @@ return_type_t gamma_lcdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(negative_infinity()); + } } using std::exp; @@ -72,8 +74,9 @@ return_type_t gamma_lcdf(const T_y& y, for (size_t n = 0; n < N; n++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(y_vec[n]) == std::numeric_limits::infinity()) + if (value_of(y_vec[n]) == std::numeric_limits::infinity()) { return ops_partials.build(0.0); + } const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return alpha_dbl = value_of(alpha_vec[n]); @@ -83,19 +86,22 @@ return_type_t gamma_lcdf(const T_y& y, P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += beta_dbl * exp(-beta_dbl * y_dbl) * pow(beta_dbl * y_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_dbl, gamma_vec[n], digamma_vec[n]) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += y_dbl * exp(-beta_dbl * y_dbl) * pow(beta_dbl * y_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/gamma_lpdf.hpp b/stan/math/prim/scal/prob/gamma_lpdf.hpp index 49309c67b5a..08e84fe6869 100644 --- a/stan/math/prim/scal/prob/gamma_lpdf.hpp +++ b/stan/math/prim/scal/prob/gamma_lpdf.hpp @@ -45,8 +45,9 @@ return_type_t gamma_lpdf(const T_y& y, static const char* function = "gamma_lpdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0.0; + } T_partials_return logp(0.0); @@ -56,8 +57,9 @@ return_type_t gamma_lpdf(const T_y& y, check_consistent_sizes(function, "Random variable", y, "Shape parameter", alpha, "Inverse scale parameter", beta); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view y_vec(y); scalar_seq_view alpha_vec(alpha); @@ -65,8 +67,9 @@ return_type_t gamma_lpdf(const T_y& y, for (size_t n = 0; n < length(y); n++) { const T_partials_return y_dbl = value_of(y_vec[n]); - if (y_dbl < 0) + if (y_dbl < 0) { return LOG_ZERO; + } } size_t N = max_size(y, alpha, beta); @@ -79,8 +82,9 @@ return_type_t gamma_lpdf(const T_y& y, log_y(length(y)); if (include_summand::value) { for (size_t n = 0; n < length(y); n++) { - if (value_of(y_vec[n]) > 0) + if (value_of(y_vec[n]) > 0) { log_y[n] = log(value_of(y_vec[n])); + } } } @@ -90,18 +94,21 @@ return_type_t gamma_lpdf(const T_y& y, VectorBuilder::value, T_partials_return, T_shape> digamma_alpha(length(alpha)); for (size_t n = 0; n < length(alpha); n++) { - if (include_summand::value) + if (include_summand::value) { lgamma_alpha[n] = lgamma(value_of(alpha_vec[n])); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { digamma_alpha[n] = digamma(value_of(alpha_vec[n])); + } } VectorBuilder::value, T_partials_return, T_inv_scale> log_beta(length(beta)); if (include_summand::value) { - for (size_t n = 0; n < length(beta); n++) + for (size_t n = 0; n < length(beta); n++) { log_beta[n] = log(value_of(beta_vec[n])); + } } for (size_t n = 0; n < N; n++) { @@ -109,22 +116,29 @@ return_type_t gamma_lpdf(const T_y& y, const T_partials_return alpha_dbl = value_of(alpha_vec[n]); const T_partials_return beta_dbl = value_of(beta_vec[n]); - if (include_summand::value) + if (include_summand::value) { logp -= lgamma_alpha[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += alpha_dbl * log_beta[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += (alpha_dbl - 1.0) * log_y[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= beta_dbl * y_dbl; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += (alpha_dbl - 1) / y_dbl - beta_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += -digamma_alpha[n] + log_beta[n] + log_y[n]; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += alpha_dbl / beta_dbl - y_dbl; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/gumbel_cdf.hpp b/stan/math/prim/scal/prob/gumbel_cdf.hpp index a0d720239c4..ca5cc7ee67d 100644 --- a/stan/math/prim/scal/prob/gumbel_cdf.hpp +++ b/stan/math/prim/scal/prob/gumbel_cdf.hpp @@ -37,8 +37,9 @@ return_type_t gumbel_cdf(const T_y& y, const T_loc& mu, using std::exp; T_partials_return cdf(1.0); - if (size_zero(y, mu, beta)) + if (size_zero(y, mu, beta)) { return cdf; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -64,25 +65,31 @@ return_type_t gumbel_cdf(const T_y& y, const T_loc& mu, const T_partials_return cdf_ = exp(-exp(-scaled_diff)); cdf *= cdf_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv / cdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv / cdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * scaled_diff / cdf_; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(mu); ++n) + for (size_t n = 0; n < stan::length(mu); ++n) { ops_partials.edge2_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(beta); ++n) + for (size_t n = 0; n < stan::length(beta); ++n) { ops_partials.edge3_.partials_[n] *= cdf; + } } return ops_partials.build(cdf); } diff --git a/stan/math/prim/scal/prob/gumbel_lccdf.hpp b/stan/math/prim/scal/prob/gumbel_lccdf.hpp index 3552a4e3e9e..336de8eb5e8 100644 --- a/stan/math/prim/scal/prob/gumbel_lccdf.hpp +++ b/stan/math/prim/scal/prob/gumbel_lccdf.hpp @@ -38,8 +38,9 @@ return_type_t gumbel_lccdf(const T_y& y, const T_loc& mu, using std::log; T_partials_return ccdf_log(0.0); - if (size_zero(y, mu, beta)) + if (size_zero(y, mu, beta)) { return ccdf_log; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -65,12 +66,15 @@ return_type_t gumbel_lccdf(const T_y& y, const T_loc& mu, const T_partials_return ccdf_log_ = 1.0 - exp(-exp(-scaled_diff)); ccdf_log += log(ccdf_log_); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= rep_deriv / ccdf_log_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += rep_deriv / ccdf_log_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += rep_deriv * scaled_diff / ccdf_log_; + } } return ops_partials.build(ccdf_log); diff --git a/stan/math/prim/scal/prob/gumbel_lcdf.hpp b/stan/math/prim/scal/prob/gumbel_lcdf.hpp index 8f6c2598e8a..997381052a7 100644 --- a/stan/math/prim/scal/prob/gumbel_lcdf.hpp +++ b/stan/math/prim/scal/prob/gumbel_lcdf.hpp @@ -37,8 +37,9 @@ return_type_t gumbel_lcdf(const T_y& y, const T_loc& mu, using std::exp; T_partials_return cdf_log(0.0); - if (size_zero(y, mu, beta)) + if (size_zero(y, mu, beta)) { return cdf_log; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -62,12 +63,15 @@ return_type_t gumbel_lcdf(const T_y& y, const T_loc& mu, const T_partials_return rep_deriv = exp(-scaled_diff) / beta_dbl; cdf_log -= exp(-scaled_diff); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * scaled_diff; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/gumbel_lpdf.hpp b/stan/math/prim/scal/prob/gumbel_lpdf.hpp index 54969928585..adc3201d206 100644 --- a/stan/math/prim/scal/prob/gumbel_lpdf.hpp +++ b/stan/math/prim/scal/prob/gumbel_lpdf.hpp @@ -37,8 +37,9 @@ return_type_t gumbel_lpdf(const T_y& y, const T_loc& mu, using std::exp; using std::log; - if (size_zero(y, mu, beta)) + if (size_zero(y, mu, beta)) { return 0.0; + } T_partials_return logp(0.0); @@ -48,8 +49,9 @@ return_type_t gumbel_lpdf(const T_y& y, const T_loc& mu, check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", beta); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } operands_and_partials ops_partials(y, mu, beta); @@ -64,8 +66,9 @@ return_type_t gumbel_lpdf(const T_y& y, const T_loc& mu, log_beta(length(beta)); for (size_t i = 0; i < length(beta); i++) { inv_beta[i] = 1.0 / value_of(beta_vec[i]); - if (include_summand::value) + if (include_summand::value) { log_beta[i] = log(value_of(beta_vec[i])); + } } for (size_t n = 0; n < N; n++) { @@ -75,20 +78,25 @@ return_type_t gumbel_lpdf(const T_y& y, const T_loc& mu, const T_partials_return y_minus_mu_over_beta = (y_dbl - mu_dbl) * inv_beta[n]; - if (include_summand::value) + if (include_summand::value) { logp -= log_beta[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += -y_minus_mu_over_beta - exp(-y_minus_mu_over_beta); + } T_partials_return scaled_diff = inv_beta[n] * exp(-y_minus_mu_over_beta); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= inv_beta[n] - scaled_diff; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += inv_beta[n] - scaled_diff; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -inv_beta[n] + y_minus_mu_over_beta * inv_beta[n] - scaled_diff * y_minus_mu_over_beta; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/gumbel_rng.hpp b/stan/math/prim/scal/prob/gumbel_rng.hpp index 0d9e2af56a7..c6e7bdd4eab 100644 --- a/stan/math/prim/scal/prob/gumbel_rng.hpp +++ b/stan/math/prim/scal/prob/gumbel_rng.hpp @@ -47,8 +47,9 @@ inline typename VectorBuilder::type gumbel_rng( VectorBuilder output(N); variate_generator > uniform01_rng(rng, uniform_01<>()); - for (size_t n = 0; n < N; ++n) + for (size_t n = 0; n < N; ++n) { output[n] = mu_vec[n] - beta_vec[n] * std::log(-std::log(uniform01_rng())); + } return output.data(); } diff --git a/stan/math/prim/scal/prob/hypergeometric_lpmf.hpp b/stan/math/prim/scal/prob/hypergeometric_lpmf.hpp index f5abbef2c88..c99b0ae54eb 100644 --- a/stan/math/prim/scal/prob/hypergeometric_lpmf.hpp +++ b/stan/math/prim/scal/prob/hypergeometric_lpmf.hpp @@ -19,8 +19,9 @@ double hypergeometric_lpmf(const T_n& n, const T_N& N, const T_a& a, const T_b& b) { static const char* function = "hypergeometric_lpmf"; - if (size_zero(n, N, a, b)) + if (size_zero(n, N, a, b)) { return 0.0; + } scalar_seq_view n_vec(n); scalar_seq_view N_vec(N); @@ -41,13 +42,15 @@ double hypergeometric_lpmf(const T_n& n, const T_N& N, const T_a& a, N, "Successes in population parameter", a, "Failures in population parameter", b); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } - for (size_t i = 0; i < size; i++) + for (size_t i = 0; i < size; i++) { logp += math::binomial_coefficient_log(a_vec[i], n_vec[i]) + math::binomial_coefficient_log(b_vec[i], N_vec[i] - n_vec[i]) - math::binomial_coefficient_log(a_vec[i] + b_vec[i], N_vec[i]); + } return logp; } diff --git a/stan/math/prim/scal/prob/hypergeometric_rng.hpp b/stan/math/prim/scal/prob/hypergeometric_rng.hpp index 1d59e854fc7..14ec333c74c 100644 --- a/stan/math/prim/scal/prob/hypergeometric_rng.hpp +++ b/stan/math/prim/scal/prob/hypergeometric_rng.hpp @@ -30,10 +30,11 @@ inline int hypergeometric_rng(int N, int a, int b, RNG& rng) { int max = a - 1; while (min < max) { int mid = (min + max) / 2; - if (cdf(dist, mid + 1) > u) + if (cdf(dist, mid + 1) > u) { max = mid; - else + } else { min = mid + 1; + } } return min + 1; } diff --git a/stan/math/prim/scal/prob/inv_chi_square_cdf.hpp b/stan/math/prim/scal/prob/inv_chi_square_cdf.hpp index 2f1290964e0..8f41075d93d 100644 --- a/stan/math/prim/scal/prob/inv_chi_square_cdf.hpp +++ b/stan/math/prim/scal/prob/inv_chi_square_cdf.hpp @@ -35,8 +35,9 @@ template return_type_t inv_chi_square_cdf(const T_y& y, const T_dof& nu) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, nu)) + if (size_zero(y, nu)) { return 1.0; + } static const char* function = "inv_chi_square_cdf"; @@ -56,9 +57,11 @@ return_type_t inv_chi_square_cdf(const T_y& y, const T_dof& nu) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero - for (size_t i = 0; i < stan::length(y); i++) - if (value_of(y_vec[i]) == 0) + for (size_t i = 0; i < stan::length(y); i++) { + if (value_of(y_vec[i]) == 0) { return ops_partials.build(0.0); + } + } using std::exp; using std::pow; @@ -91,26 +94,30 @@ return_type_t inv_chi_square_cdf(const T_y& y, const T_dof& nu) { P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += 0.5 * y_inv_dbl * y_inv_dbl * exp(-0.5 * y_inv_dbl) * pow(0.5 * y_inv_dbl, 0.5 * nu_dbl - 1) / tgamma(0.5 * nu_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += 0.5 * grad_reg_inc_gamma(0.5 * nu_dbl, 0.5 * y_inv_dbl, gamma_vec[n], digamma_vec[n]) / Pn; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(nu); ++n) + for (size_t n = 0; n < stan::length(nu); ++n) { ops_partials.edge2_.partials_[n] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/inv_chi_square_lccdf.hpp b/stan/math/prim/scal/prob/inv_chi_square_lccdf.hpp index f7239ee4043..3828f26ecee 100644 --- a/stan/math/prim/scal/prob/inv_chi_square_lccdf.hpp +++ b/stan/math/prim/scal/prob/inv_chi_square_lccdf.hpp @@ -36,8 +36,9 @@ template return_type_t inv_chi_square_lccdf(const T_y& y, const T_dof& nu) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, nu)) + if (size_zero(y, nu)) { return 0.0; + } static const char* function = "inv_chi_square_lccdf"; @@ -57,9 +58,11 @@ return_type_t inv_chi_square_lccdf(const T_y& y, const T_dof& nu) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero - for (size_t i = 0; i < stan::length(y); i++) - if (value_of(y_vec[i]) == 0) + for (size_t i = 0; i < stan::length(y); i++) { + if (value_of(y_vec[i]) == 0) { return ops_partials.build(0.0); + } + } using std::exp; using std::log; @@ -93,17 +96,19 @@ return_type_t inv_chi_square_lccdf(const T_y& y, const T_dof& nu) { P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= 0.5 * y_inv_dbl * y_inv_dbl * exp(-0.5 * y_inv_dbl) * pow(0.5 * y_inv_dbl, 0.5 * nu_dbl - 1) / tgamma(0.5 * nu_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= 0.5 * grad_reg_inc_gamma(0.5 * nu_dbl, 0.5 * y_inv_dbl, gamma_vec[n], digamma_vec[n]) / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/inv_chi_square_lcdf.hpp b/stan/math/prim/scal/prob/inv_chi_square_lcdf.hpp index 65419d70e36..c0baffa5799 100644 --- a/stan/math/prim/scal/prob/inv_chi_square_lcdf.hpp +++ b/stan/math/prim/scal/prob/inv_chi_square_lcdf.hpp @@ -36,8 +36,9 @@ template return_type_t inv_chi_square_lcdf(const T_y& y, const T_dof& nu) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, nu)) + if (size_zero(y, nu)) { return 0.0; + } static const char* function = "inv_chi_square_lcdf"; @@ -57,9 +58,11 @@ return_type_t inv_chi_square_lcdf(const T_y& y, const T_dof& nu) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero - for (size_t i = 0; i < stan::length(y); i++) - if (value_of(y_vec[i]) == 0) + for (size_t i = 0; i < stan::length(y); i++) { + if (value_of(y_vec[i]) == 0) { return ops_partials.build(negative_infinity()); + } + } using std::exp; using std::log; @@ -93,17 +96,19 @@ return_type_t inv_chi_square_lcdf(const T_y& y, const T_dof& nu) { P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += 0.5 * y_inv_dbl * y_inv_dbl * exp(-0.5 * y_inv_dbl) * pow(0.5 * y_inv_dbl, 0.5 * nu_dbl - 1) / tgamma(0.5 * nu_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += 0.5 * grad_reg_inc_gamma(0.5 * nu_dbl, 0.5 * y_inv_dbl, gamma_vec[n], digamma_vec[n]) / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/inv_chi_square_lpdf.hpp b/stan/math/prim/scal/prob/inv_chi_square_lpdf.hpp index 74c0145e9b5..f11b92903c8 100644 --- a/stan/math/prim/scal/prob/inv_chi_square_lpdf.hpp +++ b/stan/math/prim/scal/prob/inv_chi_square_lpdf.hpp @@ -44,8 +44,9 @@ return_type_t inv_chi_square_lpdf(const T_y& y, const T_dof& nu) { check_not_nan(function, "Random variable", y); check_consistent_sizes(function, "Random variable", y, "Degrees of freedom parameter", nu); - if (size_zero(y, nu)) + if (size_zero(y, nu)) { return 0; + } T_partials_return logp(0); @@ -53,24 +54,30 @@ return_type_t inv_chi_square_lpdf(const T_y& y, const T_dof& nu) { scalar_seq_view nu_vec(nu); size_t N = max_size(y, nu); - for (size_t n = 0; n < length(y); n++) - if (value_of(y_vec[n]) <= 0) + for (size_t n = 0; n < length(y); n++) { + if (value_of(y_vec[n]) <= 0) { return LOG_ZERO; + } + } using std::log; VectorBuilder::value, T_partials_return, T_y> log_y(length(y)); - for (size_t i = 0; i < length(y); i++) - if (include_summand::value) + for (size_t i = 0; i < length(y); i++) { + if (include_summand::value) { log_y[i] = log(value_of(y_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_y> inv_y(length(y)); - for (size_t i = 0; i < length(y); i++) - if (include_summand::value) + for (size_t i = 0; i < length(y); i++) { + if (include_summand::value) { inv_y[i] = 1.0 / value_of(y_vec[i]); + } + } VectorBuilder::value, T_partials_return, T_dof> lgamma_half_nu(length(nu)); @@ -78,10 +85,12 @@ return_type_t inv_chi_square_lpdf(const T_y& y, const T_dof& nu) { digamma_half_nu_over_two(length(nu)); for (size_t i = 0; i < length(nu); i++) { T_partials_return half_nu = 0.5 * value_of(nu_vec[i]); - if (include_summand::value) + if (include_summand::value) { lgamma_half_nu[i] = lgamma(half_nu); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { digamma_half_nu_over_two[i] = digamma(half_nu) * 0.5; + } } operands_and_partials ops_partials(y, nu); @@ -89,12 +98,15 @@ return_type_t inv_chi_square_lpdf(const T_y& y, const T_dof& nu) { const T_partials_return nu_dbl = value_of(nu_vec[n]); const T_partials_return half_nu = 0.5 * nu_dbl; - if (include_summand::value) + if (include_summand::value) { logp += nu_dbl * NEG_LOG_TWO_OVER_TWO - lgamma_half_nu[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= (half_nu + 1.0) * log_y[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= 0.5 * inv_y[n]; + } if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] diff --git a/stan/math/prim/scal/prob/inv_gamma_cdf.hpp b/stan/math/prim/scal/prob/inv_gamma_cdf.hpp index 998c5ec0720..3e56b5bf84f 100644 --- a/stan/math/prim/scal/prob/inv_gamma_cdf.hpp +++ b/stan/math/prim/scal/prob/inv_gamma_cdf.hpp @@ -40,8 +40,9 @@ return_type_t inv_gamma_cdf(const T_y& y, const T_scale& beta) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 1.0; + } static const char* function = "inv_gamma_cdf"; @@ -64,8 +65,9 @@ return_type_t inv_gamma_cdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(0.0); + } } using std::exp; @@ -87,8 +89,9 @@ return_type_t inv_gamma_cdf(const T_y& y, for (size_t n = 0; n < N; n++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(y_vec[n]) == std::numeric_limits::infinity()) + if (value_of(y_vec[n]) == std::numeric_limits::infinity()) { continue; + } const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return y_inv_dbl = 1.0 / y_dbl; @@ -99,34 +102,40 @@ return_type_t inv_gamma_cdf(const T_y& y, P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += beta_dbl * y_inv_dbl * y_inv_dbl * exp(-beta_dbl * y_inv_dbl) * pow(beta_dbl * y_inv_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_inv_dbl, gamma_vec[n], digamma_vec[n]) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -y_inv_dbl * exp(-beta_dbl * y_inv_dbl) * pow(beta_dbl * y_inv_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(alpha); ++n) + for (size_t n = 0; n < stan::length(alpha); ++n) { ops_partials.edge2_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(beta); ++n) + for (size_t n = 0; n < stan::length(beta); ++n) { ops_partials.edge3_.partials_[n] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/inv_gamma_lccdf.hpp b/stan/math/prim/scal/prob/inv_gamma_lccdf.hpp index 53b8f09d8d8..f6d736c210c 100644 --- a/stan/math/prim/scal/prob/inv_gamma_lccdf.hpp +++ b/stan/math/prim/scal/prob/inv_gamma_lccdf.hpp @@ -25,8 +25,9 @@ return_type_t inv_gamma_lccdf(const T_y& y, const T_scale& beta) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0.0; + } static const char* function = "inv_gamma_lccdf"; @@ -49,8 +50,9 @@ return_type_t inv_gamma_lccdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(0.0); + } } using std::exp; @@ -73,8 +75,9 @@ return_type_t inv_gamma_lccdf(const T_y& y, for (size_t n = 0; n < N; n++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(y_vec[n]) == std::numeric_limits::infinity()) + if (value_of(y_vec[n]) == std::numeric_limits::infinity()) { return ops_partials.build(negative_infinity()); + } const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return y_inv_dbl = 1.0 / y_dbl; @@ -85,21 +88,24 @@ return_type_t inv_gamma_lccdf(const T_y& y, P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= beta_dbl * y_inv_dbl * y_inv_dbl * exp(-beta_dbl * y_inv_dbl) * pow(beta_dbl * y_inv_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_inv_dbl, gamma_vec[n], digamma_vec[n]) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += y_inv_dbl * exp(-beta_dbl * y_inv_dbl) * pow(beta_dbl * y_inv_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/inv_gamma_lcdf.hpp b/stan/math/prim/scal/prob/inv_gamma_lcdf.hpp index d850c49e648..48b2c0c60e0 100644 --- a/stan/math/prim/scal/prob/inv_gamma_lcdf.hpp +++ b/stan/math/prim/scal/prob/inv_gamma_lcdf.hpp @@ -25,8 +25,9 @@ return_type_t inv_gamma_lcdf(const T_y& y, const T_scale& beta) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0.0; + } static const char* function = "inv_gamma_lcdf"; @@ -49,8 +50,9 @@ return_type_t inv_gamma_lcdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(negative_infinity()); + } } using std::exp; @@ -73,8 +75,9 @@ return_type_t inv_gamma_lcdf(const T_y& y, for (size_t n = 0; n < N; n++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(y_vec[n]) == std::numeric_limits::infinity()) + if (value_of(y_vec[n]) == std::numeric_limits::infinity()) { continue; + } const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return y_inv_dbl = 1.0 / y_dbl; @@ -85,21 +88,24 @@ return_type_t inv_gamma_lcdf(const T_y& y, P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += beta_dbl * y_inv_dbl * y_inv_dbl * exp(-beta_dbl * y_inv_dbl) * pow(beta_dbl * y_inv_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_inv_dbl, gamma_vec[n], digamma_vec[n]) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -y_inv_dbl * exp(-beta_dbl * y_inv_dbl) * pow(beta_dbl * y_inv_dbl, alpha_dbl - 1) / tgamma(alpha_dbl) / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/inv_gamma_lpdf.hpp b/stan/math/prim/scal/prob/inv_gamma_lpdf.hpp index 0638bfb5079..3832f3f4b8d 100644 --- a/stan/math/prim/scal/prob/inv_gamma_lpdf.hpp +++ b/stan/math/prim/scal/prob/inv_gamma_lpdf.hpp @@ -43,11 +43,13 @@ return_type_t inv_gamma_lpdf(const T_y& y, check_positive_finite(function, "Scale parameter", beta); check_consistent_sizes(function, "Random variable", y, "Shape parameter", alpha, "Scale parameter", beta); - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0; + } - if (!include_summand::value) + if (!include_summand::value) { return 0; + } T_partials_return logp(0); scalar_seq_view y_vec(y); @@ -56,8 +58,9 @@ return_type_t inv_gamma_lpdf(const T_y& y, for (size_t n = 0; n < length(y); n++) { const T_partials_return y_dbl = value_of(y_vec[n]); - if (y_dbl <= 0) + if (y_dbl <= 0) { return LOG_ZERO; + } } size_t N = max_size(y, alpha, beta); @@ -72,11 +75,14 @@ return_type_t inv_gamma_lpdf(const T_y& y, T_y> inv_y(length(y)); for (size_t n = 0; n < length(y); n++) { - if (include_summand::value) - if (value_of(y_vec[n]) > 0) + if (include_summand::value) { + if (value_of(y_vec[n]) > 0) { log_y[n] = log(value_of(y_vec[n])); - if (include_summand::value) + } + } + if (include_summand::value) { inv_y[n] = 1.0 / value_of(y_vec[n]); + } } VectorBuilder::value, T_partials_return, @@ -85,41 +91,51 @@ return_type_t inv_gamma_lpdf(const T_y& y, VectorBuilder::value, T_partials_return, T_shape> digamma_alpha(length(alpha)); for (size_t n = 0; n < length(alpha); n++) { - if (include_summand::value) + if (include_summand::value) { lgamma_alpha[n] = lgamma(value_of(alpha_vec[n])); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { digamma_alpha[n] = digamma(value_of(alpha_vec[n])); + } } VectorBuilder::value, T_partials_return, T_scale> log_beta(length(beta)); if (include_summand::value) { - for (size_t n = 0; n < length(beta); n++) + for (size_t n = 0; n < length(beta); n++) { log_beta[n] = log(value_of(beta_vec[n])); + } } for (size_t n = 0; n < N; n++) { const T_partials_return alpha_dbl = value_of(alpha_vec[n]); const T_partials_return beta_dbl = value_of(beta_vec[n]); - if (include_summand::value) + if (include_summand::value) { logp -= lgamma_alpha[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += alpha_dbl * log_beta[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= (alpha_dbl + 1.0) * log_y[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= beta_dbl * inv_y[n]; + } - if (!is_constant_all::type>::value) + if (!is_constant_all::type>::value) { ops_partials.edge1_.partials_[n] += -(alpha_dbl + 1) * inv_y[n] + beta_dbl * inv_y[n] * inv_y[n]; - if (!is_constant_all::type>::value) + } + if (!is_constant_all::type>::value) { ops_partials.edge2_.partials_[n] += -digamma_alpha[n] + log_beta[n] - log_y[n]; - if (!is_constant_all::type>::value) + } + if (!is_constant_all::type>::value) { ops_partials.edge3_.partials_[n] += alpha_dbl / beta_dbl - inv_y[n]; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/logistic_cdf.hpp b/stan/math/prim/scal/prob/logistic_cdf.hpp index 5b9df6abf9e..88d4c092934 100644 --- a/stan/math/prim/scal/prob/logistic_cdf.hpp +++ b/stan/math/prim/scal/prob/logistic_cdf.hpp @@ -21,8 +21,9 @@ return_type_t logistic_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 1.0; + } static const char* function = "logistic_cdf"; @@ -46,8 +47,9 @@ return_type_t logistic_cdf(const T_y& y, const T_loc& mu, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) + if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) { return ops_partials.build(0.0); + } } for (size_t n = 0; n < N; n++) { @@ -67,29 +69,35 @@ return_type_t logistic_cdf(const T_y& y, const T_loc& mu, P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += exp(logistic_log(y_dbl, mu_dbl, sigma_dbl)) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += -exp(logistic_log(y_dbl, mu_dbl, sigma_dbl)) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -(y_dbl - mu_dbl) * sigma_inv_vec * exp(logistic_log(y_dbl, mu_dbl, sigma_dbl)) / Pn; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(mu); ++n) + for (size_t n = 0; n < stan::length(mu); ++n) { ops_partials.edge2_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(sigma); ++n) + for (size_t n = 0; n < stan::length(sigma); ++n) { ops_partials.edge3_.partials_[n] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/logistic_lccdf.hpp b/stan/math/prim/scal/prob/logistic_lccdf.hpp index f6a5ed7d08a..431d18be25f 100644 --- a/stan/math/prim/scal/prob/logistic_lccdf.hpp +++ b/stan/math/prim/scal/prob/logistic_lccdf.hpp @@ -21,8 +21,9 @@ return_type_t logistic_lccdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 0.0; + } static const char* function = "logistic_lccdf"; @@ -47,8 +48,9 @@ return_type_t logistic_lccdf(const T_y& y, const T_loc& mu, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) + if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) { return ops_partials.build(0.0); + } } for (size_t n = 0; n < N; n++) { @@ -67,16 +69,19 @@ return_type_t logistic_lccdf(const T_y& y, const T_loc& mu, = 1.0 - 1.0 / (1.0 + exp(-(y_dbl - mu_dbl) * sigma_inv_vec)); P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= exp(logistic_log(y_dbl, mu_dbl, sigma_dbl)) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= -exp(logistic_log(y_dbl, mu_dbl, sigma_dbl)) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= -(y_dbl - mu_dbl) * sigma_inv_vec * exp(logistic_log(y_dbl, mu_dbl, sigma_dbl)) / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/logistic_lcdf.hpp b/stan/math/prim/scal/prob/logistic_lcdf.hpp index 57fb16978ab..7cd5e487a70 100644 --- a/stan/math/prim/scal/prob/logistic_lcdf.hpp +++ b/stan/math/prim/scal/prob/logistic_lcdf.hpp @@ -20,8 +20,9 @@ return_type_t logistic_lcdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 0.0; + } static const char* function = "logistic_lcdf"; @@ -46,8 +47,9 @@ return_type_t logistic_lcdf(const T_y& y, const T_loc& mu, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) + if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) { return ops_partials.build(-std::numeric_limits::infinity()); + } } for (size_t n = 0; n < N; n++) { @@ -66,16 +68,19 @@ return_type_t logistic_lcdf(const T_y& y, const T_loc& mu, = 1.0 / (1.0 + exp(-(y_dbl - mu_dbl) * sigma_inv_vec)); P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += exp(logistic_log(y_dbl, mu_dbl, sigma_dbl)) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += -exp(logistic_log(y_dbl, mu_dbl, sigma_dbl)) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -(y_dbl - mu_dbl) * sigma_inv_vec * exp(logistic_log(y_dbl, mu_dbl, sigma_dbl)) / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/logistic_lpdf.hpp b/stan/math/prim/scal/prob/logistic_lpdf.hpp index 07e6a033c43..3c8738e0c21 100644 --- a/stan/math/prim/scal/prob/logistic_lpdf.hpp +++ b/stan/math/prim/scal/prob/logistic_lpdf.hpp @@ -23,8 +23,9 @@ return_type_t logistic_lpdf(const T_y& y, const T_loc& mu, using std::exp; using std::log; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 0.0; + } T_partials_return logp(0.0); @@ -34,8 +35,9 @@ return_type_t logistic_lpdf(const T_y& y, const T_loc& mu, check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } operands_and_partials ops_partials(y, mu, sigma); @@ -50,8 +52,9 @@ return_type_t logistic_lpdf(const T_y& y, const T_loc& mu, log_sigma(length(sigma)); for (size_t i = 0; i < length(sigma); i++) { inv_sigma[i] = 1.0 / value_of(sigma_vec[i]); - if (include_summand::value) + if (include_summand::value) { log_sigma[i] = log(value_of(sigma_vec[i])); + } } VectorBuilder::value, T_partials_return, T_loc, @@ -60,10 +63,12 @@ return_type_t logistic_lpdf(const T_y& y, const T_loc& mu, VectorBuilder::value, T_partials_return, T_y, T_scale> exp_y_div_sigma(max_size(y, sigma)); if (!is_constant_all::value) { - for (size_t n = 0; n < max_size(mu, sigma); n++) + for (size_t n = 0; n < max_size(mu, sigma); n++) { exp_mu_div_sigma[n] = exp(value_of(mu_vec[n]) / value_of(sigma_vec[n])); - for (size_t n = 0; n < max_size(y, sigma); n++) + } + for (size_t n = 0; n < max_size(y, sigma); n++) { exp_y_div_sigma[n] = exp(value_of(y_vec[n]) / value_of(sigma_vec[n])); + } } for (size_t n = 0; n < N; n++) { @@ -73,34 +78,42 @@ return_type_t logistic_lpdf(const T_y& y, const T_loc& mu, const T_partials_return y_minus_mu = y_dbl - mu_dbl; const T_partials_return y_minus_mu_div_sigma = y_minus_mu * inv_sigma[n]; T_partials_return exp_m_y_minus_mu_div_sigma(0); - if (include_summand::value) + if (include_summand::value) { exp_m_y_minus_mu_div_sigma = exp(-y_minus_mu_div_sigma); + } T_partials_return inv_1p_exp_y_minus_mu_div_sigma(0); - if (!is_constant_all::value) + if (!is_constant_all::value) { inv_1p_exp_y_minus_mu_div_sigma = 1 / (1 + exp(y_minus_mu_div_sigma)); + } - if (include_summand::value) + if (include_summand::value) { logp -= y_minus_mu_div_sigma; - if (include_summand::value) + } + if (include_summand::value) { logp -= log_sigma[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= 2.0 * log1p(exp_m_y_minus_mu_div_sigma); + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += (2 * inv_1p_exp_y_minus_mu_div_sigma - 1) * inv_sigma[n]; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += (1 - 2 * exp_mu_div_sigma[n] / (exp_mu_div_sigma[n] + exp_y_div_sigma[n])) * inv_sigma[n]; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += ((1 - 2 * inv_1p_exp_y_minus_mu_div_sigma) * y_minus_mu * inv_sigma[n] - 1) * inv_sigma[n]; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/logistic_rng.hpp b/stan/math/prim/scal/prob/logistic_rng.hpp index 5d803da701a..8dec7df78ee 100644 --- a/stan/math/prim/scal/prob/logistic_rng.hpp +++ b/stan/math/prim/scal/prob/logistic_rng.hpp @@ -48,8 +48,9 @@ inline typename VectorBuilder::type logistic_rng( variate_generator > exp_rng( rng, exponential_distribution<>(1)); - for (size_t n = 0; n < N; ++n) + for (size_t n = 0; n < N; ++n) { output[n] = mu_vec[n] - sigma_vec[n] * std::log(exp_rng() / exp_rng()); + } return output.data(); } diff --git a/stan/math/prim/scal/prob/lognormal_cdf.hpp b/stan/math/prim/scal/prob/lognormal_cdf.hpp index 806beb5c46e..d4052c3534e 100644 --- a/stan/math/prim/scal/prob/lognormal_cdf.hpp +++ b/stan/math/prim/scal/prob/lognormal_cdf.hpp @@ -26,8 +26,9 @@ return_type_t lognormal_cdf(const T_y& y, const T_loc& mu, using std::exp; using std::log; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return cdf; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -44,8 +45,9 @@ return_type_t lognormal_cdf(const T_y& y, const T_loc& mu, const double sqrt_pi = std::sqrt(pi()); for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0.0) + if (value_of(y_vec[i]) == 0.0) { return ops_partials.build(0.0); + } } for (size_t n = 0; n < N; n++) { @@ -60,26 +62,32 @@ return_type_t lognormal_cdf(const T_y& y, const T_loc& mu, const T_partials_return cdf_ = 0.5 * erfc(-scaled_diff); cdf *= cdf_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv / cdf_ / y_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv / cdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * scaled_diff * SQRT_2 / cdf_; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(mu); ++n) + for (size_t n = 0; n < stan::length(mu); ++n) { ops_partials.edge2_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(sigma); ++n) + for (size_t n = 0; n < stan::length(sigma); ++n) { ops_partials.edge3_.partials_[n] *= cdf; + } } return ops_partials.build(cdf); } diff --git a/stan/math/prim/scal/prob/lognormal_lccdf.hpp b/stan/math/prim/scal/prob/lognormal_lccdf.hpp index 2a9650c1ed8..c660eedd948 100644 --- a/stan/math/prim/scal/prob/lognormal_lccdf.hpp +++ b/stan/math/prim/scal/prob/lognormal_lccdf.hpp @@ -26,8 +26,9 @@ return_type_t lognormal_lccdf(const T_y& y, using std::exp; using std::log; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return ccdf_log; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -44,8 +45,9 @@ return_type_t lognormal_lccdf(const T_y& y, const double sqrt_pi = std::sqrt(pi()); for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0.0) + if (value_of(y_vec[i]) == 0.0) { return ops_partials.build(0.0); + } } const double log_half = std::log(0.5); @@ -62,13 +64,16 @@ return_type_t lognormal_lccdf(const T_y& y, const T_partials_return erfc_calc = erfc(scaled_diff); ccdf_log += log_half + log(erfc_calc); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= rep_deriv / erfc_calc / y_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += rep_deriv / erfc_calc; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += rep_deriv * scaled_diff * SQRT_2 / erfc_calc; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/lognormal_lcdf.hpp b/stan/math/prim/scal/prob/lognormal_lcdf.hpp index 97cb4dc4a49..79f38aaea31 100644 --- a/stan/math/prim/scal/prob/lognormal_lcdf.hpp +++ b/stan/math/prim/scal/prob/lognormal_lcdf.hpp @@ -25,8 +25,9 @@ return_type_t lognormal_lcdf(const T_y& y, const T_loc& mu, using std::exp; using std::log; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return cdf_log; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -43,8 +44,9 @@ return_type_t lognormal_lcdf(const T_y& y, const T_loc& mu, const double sqrt_pi = std::sqrt(pi()); for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0.0) + if (value_of(y_vec[i]) == 0.0) { return ops_partials.build(negative_infinity()); + } } const double log_half = std::log(0.5); @@ -61,13 +63,16 @@ return_type_t lognormal_lcdf(const T_y& y, const T_loc& mu, const T_partials_return erfc_calc = erfc(-scaled_diff); cdf_log += log_half + log(erfc_calc); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv / erfc_calc / y_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv / erfc_calc; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * scaled_diff * SQRT_2 / erfc_calc; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/lognormal_lpdf.hpp b/stan/math/prim/scal/prob/lognormal_lpdf.hpp index 01ba8277ac6..d676c9f51cc 100644 --- a/stan/math/prim/scal/prob/lognormal_lpdf.hpp +++ b/stan/math/prim/scal/prob/lognormal_lpdf.hpp @@ -28,8 +28,9 @@ return_type_t lognormal_lpdf(const T_y& y, const T_loc& mu, check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 0; + } T_partials_return logp(0); @@ -38,9 +39,11 @@ return_type_t lognormal_lpdf(const T_y& y, const T_loc& mu, scalar_seq_view sigma_vec(sigma); size_t N = max_size(y, mu, sigma); - for (size_t n = 0; n < length(y); n++) - if (value_of(y_vec[n]) <= 0) + for (size_t n = 0; n < length(y); n++) { + if (value_of(y_vec[n]) <= 0) { return LOG_ZERO; + } + } operands_and_partials ops_partials(y, mu, sigma); @@ -50,8 +53,9 @@ return_type_t lognormal_lpdf(const T_y& y, const T_loc& mu, T_scale> log_sigma(length(sigma)); if (include_summand::value) { - for (size_t n = 0; n < length(sigma); n++) + for (size_t n = 0; n < length(sigma); n++) { log_sigma[n] = log(value_of(sigma_vec[n])); + } } VectorBuilder::value, @@ -61,58 +65,71 @@ return_type_t lognormal_lpdf(const T_y& y, const T_loc& mu, T_partials_return, T_scale> inv_sigma_sq(length(sigma)); if (include_summand::value) { - for (size_t n = 0; n < length(sigma); n++) + for (size_t n = 0; n < length(sigma); n++) { inv_sigma[n] = 1 / value_of(sigma_vec[n]); + } } if (include_summand::value) { - for (size_t n = 0; n < length(sigma); n++) + for (size_t n = 0; n < length(sigma); n++) { inv_sigma_sq[n] = inv_sigma[n] * inv_sigma[n]; + } } VectorBuilder::value, T_partials_return, T_y> log_y(length(y)); if (include_summand::value) { - for (size_t n = 0; n < length(y); n++) + for (size_t n = 0; n < length(y); n++) { log_y[n] = log(value_of(y_vec[n])); + } } VectorBuilder::value, T_partials_return, T_y> inv_y( length(y)); if (!is_constant_all::value) { - for (size_t n = 0; n < length(y); n++) + for (size_t n = 0; n < length(y); n++) { inv_y[n] = 1 / value_of(y_vec[n]); + } } - if (include_summand::value) + if (include_summand::value) { logp += N * NEG_LOG_SQRT_TWO_PI; + } for (size_t n = 0; n < N; n++) { const T_partials_return mu_dbl = value_of(mu_vec[n]); T_partials_return logy_m_mu(0); - if (include_summand::value) + if (include_summand::value) { logy_m_mu = log_y[n] - mu_dbl; + } T_partials_return logy_m_mu_sq = logy_m_mu * logy_m_mu; T_partials_return logy_m_mu_div_sigma(0); - if (!is_constant_all::value) + if (!is_constant_all::value) { logy_m_mu_div_sigma = logy_m_mu * inv_sigma_sq[n]; + } - if (include_summand::value) + if (include_summand::value) { logp -= log_sigma[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= log_y[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= 0.5 * logy_m_mu_sq * inv_sigma_sq[n]; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= (1 + logy_m_mu_div_sigma) * inv_y[n]; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += logy_m_mu_div_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += (logy_m_mu_div_sigma * logy_m_mu - 1) * inv_sigma[n]; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/neg_binomial_2_cdf.hpp b/stan/math/prim/scal/prob/neg_binomial_2_cdf.hpp index e9ad36de7fd..cbb132d90c8 100644 --- a/stan/math/prim/scal/prob/neg_binomial_2_cdf.hpp +++ b/stan/math/prim/scal/prob/neg_binomial_2_cdf.hpp @@ -24,8 +24,9 @@ return_type_t neg_binomial_2_cdf( T_partials_return; T_partials_return P(1.0); - if (size_zero(n, mu, phi)) + if (size_zero(n, mu, phi)) { return P; + } check_positive_finite(function, "Location parameter", mu); check_positive_finite(function, "Precision parameter", phi); @@ -43,8 +44,9 @@ return_type_t neg_binomial_2_cdf( // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(0.0); + } } VectorBuilder::value, T_partials_return, @@ -68,8 +70,9 @@ return_type_t neg_binomial_2_cdf( for (size_t i = 0; i < size; i++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(n_vec[i]) == std::numeric_limits::max()) + if (value_of(n_vec[i]) == std::numeric_limits::max()) { return ops_partials.build(1.0); + } const T_partials_return n_dbl = value_of(n_vec[i]); const T_partials_return mu_dbl = value_of(mu_vec[i]); @@ -83,9 +86,10 @@ return_type_t neg_binomial_2_cdf( P *= P_i; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += -inc_beta_ddz(phi_dbl, n_dbl + 1.0, p_dbl) * phi_dbl * d_dbl / P_i; + } if (!is_constant_all::value) { ops_partials.edge2_.partials_[i] @@ -97,13 +101,15 @@ return_type_t neg_binomial_2_cdf( } if (!is_constant_all::value) { - for (size_t i = 0; i < stan::length(mu); ++i) + for (size_t i = 0; i < stan::length(mu); ++i) { ops_partials.edge1_.partials_[i] *= P; + } } if (!is_constant_all::value) { - for (size_t i = 0; i < stan::length(phi); ++i) + for (size_t i = 0; i < stan::length(phi); ++i) { ops_partials.edge2_.partials_[i] *= P; + } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/neg_binomial_2_lccdf.hpp b/stan/math/prim/scal/prob/neg_binomial_2_lccdf.hpp index e18854c9a29..66fa846007b 100644 --- a/stan/math/prim/scal/prob/neg_binomial_2_lccdf.hpp +++ b/stan/math/prim/scal/prob/neg_binomial_2_lccdf.hpp @@ -16,8 +16,9 @@ namespace math { template return_type_t neg_binomial_2_lccdf( const T_n& n, const T_location& mu, const T_precision& phi) { - if (size_zero(n, mu, phi)) + if (size_zero(n, mu, phi)) { return 0.0; + } static const char* function = "neg_binomial_2_lccdf"; check_positive_finite(function, "Location parameter", mu); @@ -34,8 +35,9 @@ return_type_t neg_binomial_2_lccdf( VectorBuilder, T_location, T_precision> beta_vec(size_beta); - for (size_t i = 0; i < size_beta; ++i) + for (size_t i = 0; i < size_beta; ++i) { beta_vec[i] = phi_vec[i] / mu_vec[i]; + } return neg_binomial_ccdf_log(n, phi, beta_vec.data()); } diff --git a/stan/math/prim/scal/prob/neg_binomial_2_lcdf.hpp b/stan/math/prim/scal/prob/neg_binomial_2_lcdf.hpp index 156f8db4076..52fe24685c0 100644 --- a/stan/math/prim/scal/prob/neg_binomial_2_lcdf.hpp +++ b/stan/math/prim/scal/prob/neg_binomial_2_lcdf.hpp @@ -17,8 +17,9 @@ return_type_t neg_binomial_2_lcdf( const T_n& n, const T_location& mu, const T_precision& phi) { using std::log; - if (size_zero(n, mu, phi)) + if (size_zero(n, mu, phi)) { return 0.0; + } static const char* function = "neg_binomial_2_lcdf"; check_positive_finite(function, "Location parameter", mu); @@ -35,16 +36,19 @@ return_type_t neg_binomial_2_lcdf( VectorBuilder, T_location, T_precision> phi_mu(size_phi_mu); - for (size_t i = 0; i < size_phi_mu; i++) + for (size_t i = 0; i < size_phi_mu; i++) { phi_mu[i] = phi_vec[i] / (phi_vec[i] + mu_vec[i]); + } size_t size_n = length(n); VectorBuilder, T_n> np1(size_n); - for (size_t i = 0; i < size_n; i++) - if (n_vec[i] < 0) + for (size_t i = 0; i < size_n; i++) { + if (n_vec[i] < 0) { return log(0.0); - else + } else { np1[i] = n_vec[i] + 1.0; + } + } return beta_cdf_log(phi_mu.data(), phi, np1.data()); } diff --git a/stan/math/prim/scal/prob/neg_binomial_2_log_lpmf.hpp b/stan/math/prim/scal/prob/neg_binomial_2_log_lpmf.hpp index cd2221468c1..1629067d842 100644 --- a/stan/math/prim/scal/prob/neg_binomial_2_log_lpmf.hpp +++ b/stan/math/prim/scal/prob/neg_binomial_2_log_lpmf.hpp @@ -27,8 +27,9 @@ return_type_t neg_binomial_2_log_lpmf( static const char* function = "neg_binomial_2_log_lpmf"; - if (size_zero(n, eta, phi)) + if (size_zero(n, eta, phi)) { return 0.0; + } T_partials_return logp(0.0); check_nonnegative(function, "Failures variable", n); @@ -38,8 +39,9 @@ return_type_t neg_binomial_2_log_lpmf( "Log location parameter", eta, "Precision parameter", phi); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } using std::exp; using std::log; @@ -55,46 +57,58 @@ return_type_t neg_binomial_2_log_lpmf( size_t len_np = max_size(n, phi); VectorBuilder eta__(length(eta)); - for (size_t i = 0, size = length(eta); i < size; ++i) + for (size_t i = 0, size = length(eta); i < size; ++i) { eta__[i] = value_of(eta_vec[i]); + } VectorBuilder phi__(length(phi)); - for (size_t i = 0, size = length(phi); i < size; ++i) + for (size_t i = 0, size = length(phi); i < size; ++i) { phi__[i] = value_of(phi_vec[i]); + } VectorBuilder log_phi(length(phi)); - for (size_t i = 0, size = length(phi); i < size; ++i) + for (size_t i = 0, size = length(phi); i < size; ++i) { log_phi[i] = log(phi__[i]); + } VectorBuilder logsumexp_eta_logphi(len_ep); - for (size_t i = 0; i < len_ep; ++i) + for (size_t i = 0; i < len_ep; ++i) { logsumexp_eta_logphi[i] = log_sum_exp(eta__[i], log_phi[i]); + } VectorBuilder n_plus_phi(len_np); - for (size_t i = 0; i < len_np; ++i) + for (size_t i = 0; i < len_np; ++i) { n_plus_phi[i] = n_vec[i] + phi__[i]; + } for (size_t i = 0; i < size; i++) { - if (include_summand::value) + if (include_summand::value) { logp -= lgamma(n_vec[i] + 1.0); - if (include_summand::value) + } + if (include_summand::value) { logp += multiply_log(phi__[i], phi__[i]) - lgamma(phi__[i]); - if (include_summand::value) + } + if (include_summand::value) { logp -= (n_plus_phi[i]) * logsumexp_eta_logphi[i]; - if (include_summand::value) + } + if (include_summand::value) { logp += n_vec[i] * eta__[i]; - if (include_summand::value) + } + if (include_summand::value) { logp += lgamma(n_plus_phi[i]); + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += n_vec[i] - n_plus_phi[i] / (phi__[i] / exp(eta__[i]) + 1.0); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[i] += 1.0 - n_plus_phi[i] / (exp(eta__[i]) + phi__[i]) + log_phi[i] - logsumexp_eta_logphi[i] - digamma(phi__[i]) + digamma(n_plus_phi[i]); + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/neg_binomial_2_lpmf.hpp b/stan/math/prim/scal/prob/neg_binomial_2_lpmf.hpp index 4d23861d019..c44253f25da 100644 --- a/stan/math/prim/scal/prob/neg_binomial_2_lpmf.hpp +++ b/stan/math/prim/scal/prob/neg_binomial_2_lpmf.hpp @@ -25,8 +25,9 @@ return_type_t neg_binomial_2_lpmf( static const char* function = "neg_binomial_2_lpmf"; - if (size_zero(n, mu, phi)) + if (size_zero(n, mu, phi)) { return 0.0; + } T_partials_return logp(0.0); check_nonnegative(function, "Failures variable", n); @@ -35,8 +36,9 @@ return_type_t neg_binomial_2_lpmf( check_consistent_sizes(function, "Failures variable", n, "Location parameter", mu, "Precision parameter", phi); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } using std::log; @@ -51,50 +53,62 @@ return_type_t neg_binomial_2_lpmf( size_t len_np = max_size(n, phi); VectorBuilder mu__(length(mu)); - for (size_t i = 0, size = length(mu); i < size; ++i) + for (size_t i = 0, size = length(mu); i < size; ++i) { mu__[i] = value_of(mu_vec[i]); + } VectorBuilder phi__(length(phi)); - for (size_t i = 0, size = length(phi); i < size; ++i) + for (size_t i = 0, size = length(phi); i < size; ++i) { phi__[i] = value_of(phi_vec[i]); + } VectorBuilder log_phi(length(phi)); - for (size_t i = 0, size = length(phi); i < size; ++i) + for (size_t i = 0, size = length(phi); i < size; ++i) { log_phi[i] = log(phi__[i]); + } VectorBuilder log_mu_plus_phi(len_ep); - for (size_t i = 0; i < len_ep; ++i) + for (size_t i = 0; i < len_ep; ++i) { log_mu_plus_phi[i] = log(mu__[i] + phi__[i]); + } VectorBuilder n_plus_phi(len_np); - for (size_t i = 0; i < len_np; ++i) + for (size_t i = 0; i < len_np; ++i) { n_plus_phi[i] = n_vec[i] + phi__[i]; + } for (size_t i = 0; i < size; i++) { - if (include_summand::value) + if (include_summand::value) { logp -= lgamma(n_vec[i] + 1.0); - if (include_summand::value) + } + if (include_summand::value) { logp += multiply_log(phi__[i], phi__[i]) - lgamma(phi__[i]); - if (include_summand::value) + } + if (include_summand::value) { logp -= (n_plus_phi[i]) * log_mu_plus_phi[i]; - if (include_summand::value) + } + if (include_summand::value) { logp += multiply_log(n_vec[i], mu__[i]); - if (include_summand::value) + } + if (include_summand::value) { logp += lgamma(n_plus_phi[i]); + } // if phi is large we probably overflow, defer to Poisson: if (phi__[i] > 1e5) { logp = poisson_lpmf(n_vec[i], mu__[i]); } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += n_vec[i] / mu__[i] - (n_vec[i] + phi__[i]) / (mu__[i] + phi__[i]); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[i] += 1.0 - n_plus_phi[i] / (mu__[i] + phi__[i]) + log_phi[i] - log_mu_plus_phi[i] - digamma(phi__[i]) + digamma(n_plus_phi[i]); + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/neg_binomial_cdf.hpp b/stan/math/prim/scal/prob/neg_binomial_cdf.hpp index 881cafb3e68..cbfee35071d 100644 --- a/stan/math/prim/scal/prob/neg_binomial_cdf.hpp +++ b/stan/math/prim/scal/prob/neg_binomial_cdf.hpp @@ -23,8 +23,9 @@ return_type_t neg_binomial_cdf(const T_n& n, static const char* function = "neg_binomial_cdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, alpha, beta)) + if (size_zero(n, alpha, beta)) { return 1.0; + } T_partials_return P(1.0); @@ -43,8 +44,9 @@ return_type_t neg_binomial_cdf(const T_n& n, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(0.0); + } } VectorBuilder::value, T_partials_return, T_shape> @@ -66,8 +68,9 @@ return_type_t neg_binomial_cdf(const T_n& n, for (size_t i = 0; i < size; i++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(n_vec[i]) == std::numeric_limits::max()) + if (value_of(n_vec[i]) == std::numeric_limits::max()) { return ops_partials.build(1.0); + } const T_partials_return n_dbl = value_of(n_vec[i]); const T_partials_return alpha_dbl = value_of(alpha_vec[i]); @@ -87,19 +90,22 @@ return_type_t neg_binomial_cdf(const T_n& n, / P_i; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[i] += inc_beta_ddz(alpha_dbl, n_dbl + 1.0, p_dbl) * d_dbl / P_i; + } } if (!is_constant_all::value) { - for (size_t i = 0; i < stan::length(alpha); ++i) + for (size_t i = 0; i < stan::length(alpha); ++i) { ops_partials.edge1_.partials_[i] *= P; + } } if (!is_constant_all::value) { - for (size_t i = 0; i < stan::length(beta); ++i) + for (size_t i = 0; i < stan::length(beta); ++i) { ops_partials.edge2_.partials_[i] *= P; + } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/neg_binomial_lccdf.hpp b/stan/math/prim/scal/prob/neg_binomial_lccdf.hpp index 1c82db485f5..ea99f0cecf5 100644 --- a/stan/math/prim/scal/prob/neg_binomial_lccdf.hpp +++ b/stan/math/prim/scal/prob/neg_binomial_lccdf.hpp @@ -24,8 +24,9 @@ return_type_t neg_binomial_lccdf( static const char* function = "neg_binomial_lccdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, alpha, beta)) + if (size_zero(n, alpha, beta)) { return 0.0; + } T_partials_return P(0.0); @@ -48,8 +49,9 @@ return_type_t neg_binomial_lccdf( // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(0.0); + } } VectorBuilder::value, T_partials_return, T_shape> @@ -73,8 +75,9 @@ return_type_t neg_binomial_lccdf( for (size_t i = 0; i < size; i++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(n_vec[i]) == std::numeric_limits::max()) + if (value_of(n_vec[i]) == std::numeric_limits::max()) { return ops_partials.build(negative_infinity()); + } const T_partials_return n_dbl = value_of(n_vec[i]); const T_partials_return alpha_dbl = value_of(alpha_vec[i]); @@ -95,10 +98,11 @@ return_type_t neg_binomial_lccdf( beta_func); ops_partials.edge1_.partials_[i] -= g1 / Pi; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[i] -= d_dbl * pow(1 - p_dbl, n_dbl) * pow(p_dbl, alpha_dbl - 1) / beta_func / Pi; + } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/neg_binomial_lcdf.hpp b/stan/math/prim/scal/prob/neg_binomial_lcdf.hpp index 1b1b1d2efb4..2718c07107e 100644 --- a/stan/math/prim/scal/prob/neg_binomial_lcdf.hpp +++ b/stan/math/prim/scal/prob/neg_binomial_lcdf.hpp @@ -24,8 +24,9 @@ return_type_t neg_binomial_lcdf(const T_n& n, static const char* function = "neg_binomial_lcdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, alpha, beta)) + if (size_zero(n, alpha, beta)) { return 0.0; + } T_partials_return P(0.0); @@ -48,8 +49,9 @@ return_type_t neg_binomial_lcdf(const T_n& n, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(negative_infinity()); + } } VectorBuilder::value, T_partials_return, T_shape> @@ -73,8 +75,9 @@ return_type_t neg_binomial_lcdf(const T_n& n, for (size_t i = 0; i < size; i++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(n_vec[i]) == std::numeric_limits::max()) + if (value_of(n_vec[i]) == std::numeric_limits::max()) { return ops_partials.build(0.0); + } const T_partials_return n_dbl = value_of(n_vec[i]); const T_partials_return alpha_dbl = value_of(alpha_vec[i]); @@ -95,10 +98,11 @@ return_type_t neg_binomial_lcdf(const T_n& n, beta_func); ops_partials.edge1_.partials_[i] += g1 / Pi; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[i] += d_dbl * pow(1 - p_dbl, n_dbl) * pow(p_dbl, alpha_dbl - 1) / beta_func / Pi; + } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/neg_binomial_lpmf.hpp b/stan/math/prim/scal/prob/neg_binomial_lpmf.hpp index b1d927aab25..e7b9db3754a 100644 --- a/stan/math/prim/scal/prob/neg_binomial_lpmf.hpp +++ b/stan/math/prim/scal/prob/neg_binomial_lpmf.hpp @@ -26,8 +26,9 @@ return_type_t neg_binomial_lpmf(const T_n& n, static const char* function = "neg_binomial_lpmf"; - if (size_zero(n, alpha, beta)) + if (size_zero(n, alpha, beta)) { return 0.0; + } T_partials_return logp(0.0); check_nonnegative(function, "Failures variable", n); @@ -36,8 +37,9 @@ return_type_t neg_binomial_lpmf(const T_n& n, check_consistent_sizes(function, "Failures variable", n, "Shape parameter", alpha, "Inverse scale parameter", beta); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } using std::log; @@ -50,79 +52,95 @@ return_type_t neg_binomial_lpmf(const T_n& n, size_t len_ab = max_size(alpha, beta); VectorBuilder lambda(len_ab); - for (size_t i = 0; i < len_ab; ++i) + for (size_t i = 0; i < len_ab; ++i) { lambda[i] = value_of(alpha_vec[i]) / value_of(beta_vec[i]); + } VectorBuilder log1p_beta(length(beta)); - for (size_t i = 0; i < length(beta); ++i) + for (size_t i = 0; i < length(beta); ++i) { log1p_beta[i] = log1p(value_of(beta_vec[i])); + } VectorBuilder log_beta_m_log1p_beta( length(beta)); - for (size_t i = 0; i < length(beta); ++i) + for (size_t i = 0; i < length(beta); ++i) { log_beta_m_log1p_beta[i] = log(value_of(beta_vec[i])) - log1p_beta[i]; + } VectorBuilder alpha_times_log_beta_over_1p_beta(len_ab); - for (size_t i = 0; i < len_ab; ++i) + for (size_t i = 0; i < len_ab; ++i) { alpha_times_log_beta_over_1p_beta[i] = value_of(alpha_vec[i]) * log(value_of(beta_vec[i]) / (1.0 + value_of(beta_vec[i]))); + } VectorBuilder::value, T_partials_return, T_shape> digamma_alpha(length(alpha)); if (!is_constant_all::value) { - for (size_t i = 0; i < length(alpha); ++i) + for (size_t i = 0; i < length(alpha); ++i) { digamma_alpha[i] = digamma(value_of(alpha_vec[i])); + } } VectorBuilder::value, T_partials_return, T_inv_scale> log_beta(length(beta)); if (!is_constant_all::value) { - for (size_t i = 0; i < length(beta); ++i) + for (size_t i = 0; i < length(beta); ++i) { log_beta[i] = log(value_of(beta_vec[i])); + } } VectorBuilder::value, T_partials_return, T_shape, T_inv_scale> lambda_m_alpha_over_1p_beta(len_ab); if (!is_constant_all::value) { - for (size_t i = 0; i < len_ab; ++i) + for (size_t i = 0; i < len_ab; ++i) { lambda_m_alpha_over_1p_beta[i] = lambda[i] - (value_of(alpha_vec[i]) / (1.0 + value_of(beta_vec[i]))); + } } for (size_t i = 0; i < size; i++) { if (alpha_vec[i] > 1e10) { // reduces numerically to Poisson - if (include_summand::value) + if (include_summand::value) { logp -= lgamma(n_vec[i] + 1.0); - if (include_summand::value) + } + if (include_summand::value) { logp += multiply_log(n_vec[i], lambda[i]) - lambda[i]; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += n_vec[i] / value_of(alpha_vec[i]) - 1.0 / value_of(beta_vec[i]); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[i] += (lambda[i] - n_vec[i]) / value_of(beta_vec[i]); + } } else { // standard density definition - if (include_summand::value) - if (n_vec[i] != 0) + if (include_summand::value) { + if (n_vec[i] != 0) { logp += binomial_coefficient_log( n_vec[i] + value_of(alpha_vec[i]) - 1.0, n_vec[i]); - if (include_summand::value) + } + } + if (include_summand::value) { logp += alpha_times_log_beta_over_1p_beta[i] - n_vec[i] * log1p_beta[i]; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += digamma(value_of(alpha_vec[i]) + n_vec[i]) - digamma_alpha[i] + log_beta_m_log1p_beta[i]; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[i] += lambda_m_alpha_over_1p_beta[i] - n_vec[i] / (value_of(beta_vec[i]) + 1.0); + } } } return ops_partials.build(logp); diff --git a/stan/math/prim/scal/prob/normal_cdf.hpp b/stan/math/prim/scal/prob/normal_cdf.hpp index 33c216f6a2b..41e091fbdd2 100644 --- a/stan/math/prim/scal/prob/normal_cdf.hpp +++ b/stan/math/prim/scal/prob/normal_cdf.hpp @@ -38,8 +38,9 @@ return_type_t normal_cdf(const T_y& y, const T_loc& mu, T_partials_return cdf(1.0); - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return cdf; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -63,14 +64,15 @@ return_type_t normal_cdf(const T_y& y, const T_loc& mu, const T_partials_return scaled_diff = (y_dbl - mu_dbl) / (sigma_dbl * SQRT_2); T_partials_return cdf_; - if (scaled_diff < -37.5 * INV_SQRT_2) + if (scaled_diff < -37.5 * INV_SQRT_2) { cdf_ = 0.0; - else if (scaled_diff < -5.0 * INV_SQRT_2) + } else if (scaled_diff < -5.0 * INV_SQRT_2) { cdf_ = 0.5 * erfc(-scaled_diff); - else if (scaled_diff > 8.25 * INV_SQRT_2) + } else if (scaled_diff > 8.25 * INV_SQRT_2) { cdf_ = 1; - else + } else { cdf_ = 0.5 * (1.0 + erf(scaled_diff)); + } cdf *= cdf_; @@ -80,26 +82,32 @@ return_type_t normal_cdf(const T_y& y, const T_loc& mu, ? 0.0 : SQRT_TWO_OVER_PI * 0.5 * exp(-scaled_diff * scaled_diff) / cdf_ / sigma_dbl; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * scaled_diff * SQRT_2; + } } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(mu); ++n) + for (size_t n = 0; n < stan::length(mu); ++n) { ops_partials.edge2_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(sigma); ++n) + for (size_t n = 0; n < stan::length(sigma); ++n) { ops_partials.edge3_.partials_[n] *= cdf; + } } return ops_partials.build(cdf); } diff --git a/stan/math/prim/scal/prob/normal_lccdf.hpp b/stan/math/prim/scal/prob/normal_lccdf.hpp index c65b01b155e..f336af32386 100644 --- a/stan/math/prim/scal/prob/normal_lccdf.hpp +++ b/stan/math/prim/scal/prob/normal_lccdf.hpp @@ -25,8 +25,9 @@ return_type_t normal_lccdf(const T_y& y, const T_loc& mu, using std::log; T_partials_return ccdf_log(0.0); - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return ccdf_log; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -53,14 +54,15 @@ return_type_t normal_lccdf(const T_y& y, const T_loc& mu, = (y_dbl - mu_dbl) / (sigma_dbl * SQRT_2); T_partials_return one_m_erf; - if (scaled_diff < -37.5 * INV_SQRT_2) + if (scaled_diff < -37.5 * INV_SQRT_2) { one_m_erf = 2.0; - else if (scaled_diff < -5.0 * INV_SQRT_2) + } else if (scaled_diff < -5.0 * INV_SQRT_2) { one_m_erf = 2.0 - erfc(-scaled_diff); - else if (scaled_diff > 8.25 * INV_SQRT_2) + } else if (scaled_diff > 8.25 * INV_SQRT_2) { one_m_erf = 0.0; - else + } else { one_m_erf = 1.0 - erf(scaled_diff); + } ccdf_log += log_half + log(one_m_erf); @@ -70,13 +72,16 @@ return_type_t normal_lccdf(const T_y& y, const T_loc& mu, ? std::numeric_limits::infinity() : SQRT_TWO_OVER_PI * exp(-scaled_diff * scaled_diff) / one_m_erf / sigma_dbl; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= rep_deriv_div_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += rep_deriv_div_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += rep_deriv_div_sigma * scaled_diff * SQRT_2; + } } } return ops_partials.build(ccdf_log); diff --git a/stan/math/prim/scal/prob/normal_lcdf.hpp b/stan/math/prim/scal/prob/normal_lcdf.hpp index 357aaa098d6..3cd25f6b262 100644 --- a/stan/math/prim/scal/prob/normal_lcdf.hpp +++ b/stan/math/prim/scal/prob/normal_lcdf.hpp @@ -25,8 +25,9 @@ return_type_t normal_lcdf(const T_y& y, const T_loc& mu, using std::log; T_partials_return cdf_log(0.0); - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return cdf_log; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -52,14 +53,15 @@ return_type_t normal_lcdf(const T_y& y, const T_loc& mu, = (y_dbl - mu_dbl) / (sigma_dbl * SQRT_2); T_partials_return one_p_erf; - if (scaled_diff < -37.5 * INV_SQRT_2) + if (scaled_diff < -37.5 * INV_SQRT_2) { one_p_erf = 0.0; - else if (scaled_diff < -5.0 * INV_SQRT_2) + } else if (scaled_diff < -5.0 * INV_SQRT_2) { one_p_erf = erfc(-scaled_diff); - else if (scaled_diff > 8.25 * INV_SQRT_2) + } else if (scaled_diff > 8.25 * INV_SQRT_2) { one_p_erf = 2.0; - else + } else { one_p_erf = 1.0 + erf(scaled_diff); + } cdf_log += LOG_HALF + log(one_p_erf); @@ -69,13 +71,16 @@ return_type_t normal_lcdf(const T_y& y, const T_loc& mu, ? std::numeric_limits::infinity() : SQRT_TWO_OVER_PI * exp(-scaled_diff * scaled_diff) / sigma_dbl / one_p_erf; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv_div_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv_div_sigma; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv_div_sigma * scaled_diff * SQRT_2; + } } } return ops_partials.build(cdf_log); diff --git a/stan/math/prim/scal/prob/normal_lpdf.hpp b/stan/math/prim/scal/prob/normal_lpdf.hpp index 171d1191e88..9948159d96e 100644 --- a/stan/math/prim/scal/prob/normal_lpdf.hpp +++ b/stan/math/prim/scal/prob/normal_lpdf.hpp @@ -41,8 +41,9 @@ return_type_t normal_lpdf(const T_y& y, const T_loc& mu, using std::log; - if (size_zero(y, mu, sigma)) + if (size_zero(y, mu, sigma)) { return 0.0; + } T_partials_return logp(0.0); @@ -51,8 +52,9 @@ return_type_t normal_lpdf(const T_y& y, const T_loc& mu, check_positive(function, "Scale parameter", sigma); check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } operands_and_partials ops_partials(y, mu, sigma); @@ -67,8 +69,9 @@ return_type_t normal_lpdf(const T_y& y, const T_loc& mu, log_sigma(length(sigma)); for (size_t i = 0; i < length(sigma); i++) { inv_sigma[i] = 1.0 / value_of(sigma_vec[i]); - if (include_summand::value) + if (include_summand::value) { log_sigma[i] = log(value_of(sigma_vec[i])); + } } for (size_t n = 0; n < N; n++) { @@ -82,21 +85,27 @@ return_type_t normal_lpdf(const T_y& y, const T_loc& mu, static double NEGATIVE_HALF = -0.5; - if (include_summand::value) + if (include_summand::value) { logp += NEG_LOG_SQRT_TWO_PI; - if (include_summand::value) + } + if (include_summand::value) { logp -= log_sigma[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += NEGATIVE_HALF * y_minus_mu_over_sigma_squared; + } T_partials_return scaled_diff = inv_sigma[n] * y_minus_mu_over_sigma; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= scaled_diff; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += scaled_diff; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -inv_sigma[n] + inv_sigma[n] * y_minus_mu_over_sigma_squared; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/normal_sufficient_lpdf.hpp b/stan/math/prim/scal/prob/normal_sufficient_lpdf.hpp index 185ba5d57f4..2ebc54f3449 100644 --- a/stan/math/prim/scal/prob/normal_sufficient_lpdf.hpp +++ b/stan/math/prim/scal/prob/normal_sufficient_lpdf.hpp @@ -53,8 +53,9 @@ return_type_t normal_sufficient_lpdf( using std::log; // check if any vectors are zero length - if (size_zero(y_bar, s_squared, n_obs, mu, sigma)) + if (size_zero(y_bar, s_squared, n_obs, mu, sigma)) { return 0.0; + } // set up return value accumulator T_partials_return logp(0.0); @@ -74,8 +75,9 @@ return_type_t normal_sufficient_lpdf( s_squared, "Number of observations", n_obs, "Location parameter", mu, "Scale parameter", sigma); // check if no variables are involved and prop-to - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } // set up template expressions wrapping scalars into vector views operands_and_partials ops_partials(y_bar, s_squared, @@ -96,11 +98,13 @@ return_type_t normal_sufficient_lpdf( const T_partials_return sigma_dbl = value_of(sigma_vec[i]); const T_partials_return sigma_squared = pow(sigma_dbl, 2); - if (include_summand::value) + if (include_summand::value) { logp += NEG_LOG_SQRT_TWO_PI * n_obs_dbl; + } - if (include_summand::value) + if (include_summand::value) { logp -= n_obs_dbl * log(sigma_dbl); + } const T_partials_return cons_expr = (s_squared_dbl + n_obs_dbl * pow(y_bar_dbl - mu_dbl, 2)); @@ -111,16 +115,20 @@ return_type_t normal_sufficient_lpdf( if (!is_constant_all::value) { const T_partials_return common_derivative = n_obs_dbl * (mu_dbl - y_bar_dbl) / sigma_squared; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += common_derivative; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[i] -= common_derivative; + } } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[i] -= 0.5 / sigma_squared; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[i] += cons_expr / pow(sigma_dbl, 3) - n_obs_dbl / sigma_dbl; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/pareto_cdf.hpp b/stan/math/prim/scal/prob/pareto_cdf.hpp index c63852f3e18..82126432998 100644 --- a/stan/math/prim/scal/prob/pareto_cdf.hpp +++ b/stan/math/prim/scal/prob/pareto_cdf.hpp @@ -20,8 +20,9 @@ return_type_t pareto_cdf(const T_y& y, const T_shape& alpha) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, y_min, alpha)) + if (size_zero(y, y_min, alpha)) { return 1.0; + } static const char* function = "pareto_cdf"; @@ -47,8 +48,9 @@ return_type_t pareto_cdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) < value_of(y_min_vec[i])) + if (value_of(y_vec[i]) < value_of(y_min_vec[i])) { return ops_partials.build(0.0); + } } for (size_t n = 0; n < N; n++) { @@ -67,28 +69,34 @@ return_type_t pareto_cdf(const T_y& y, P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += alpha_dbl * y_min_inv_dbl * exp((alpha_dbl + 1) * log_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += -alpha_dbl * y_min_inv_dbl * exp(alpha_dbl * log_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -exp(alpha_dbl * log_dbl) * log_dbl / Pn; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y_min); ++n) + for (size_t n = 0; n < stan::length(y_min); ++n) { ops_partials.edge2_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(alpha); ++n) + for (size_t n = 0; n < stan::length(alpha); ++n) { ops_partials.edge3_.partials_[n] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/pareto_lccdf.hpp b/stan/math/prim/scal/prob/pareto_lccdf.hpp index b255b37633d..40f07891a9d 100644 --- a/stan/math/prim/scal/prob/pareto_lccdf.hpp +++ b/stan/math/prim/scal/prob/pareto_lccdf.hpp @@ -21,8 +21,9 @@ return_type_t pareto_lccdf(const T_y& y, const T_shape& alpha) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, y_min, alpha)) + if (size_zero(y, y_min, alpha)) { return 0.0; + } static const char* function = "pareto_lccdf"; @@ -48,8 +49,9 @@ return_type_t pareto_lccdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) < value_of(y_min_vec[i])) + if (value_of(y_vec[i]) < value_of(y_min_vec[i])) { return ops_partials.build(0.0); + } } for (size_t n = 0; n < N; n++) { @@ -66,13 +68,16 @@ return_type_t pareto_lccdf(const T_y& y, P += alpha_dbl * log_dbl; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= alpha_dbl * y_min_inv_dbl * exp(log_dbl); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += alpha_dbl * y_min_inv_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += log_dbl; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/pareto_lcdf.hpp b/stan/math/prim/scal/prob/pareto_lcdf.hpp index 549d4d5a493..8a55b50326f 100644 --- a/stan/math/prim/scal/prob/pareto_lcdf.hpp +++ b/stan/math/prim/scal/prob/pareto_lcdf.hpp @@ -21,8 +21,9 @@ return_type_t pareto_lcdf(const T_y& y, const T_shape& alpha) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, y_min, alpha)) + if (size_zero(y, y_min, alpha)) { return 0.0; + } static const char* function = "pareto_lcdf"; @@ -48,8 +49,9 @@ return_type_t pareto_lcdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) < value_of(y_min_vec[i])) + if (value_of(y_vec[i]) < value_of(y_min_vec[i])) { return ops_partials.build(negative_infinity()); + } } for (size_t n = 0; n < N; n++) { @@ -68,15 +70,18 @@ return_type_t pareto_lcdf(const T_y& y, P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += alpha_dbl * y_min_inv_dbl * exp((alpha_dbl + 1) * log_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= alpha_dbl * y_min_inv_dbl * exp(alpha_dbl * log_dbl) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= exp(alpha_dbl * log_dbl) * log_dbl / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/pareto_lpdf.hpp b/stan/math/prim/scal/prob/pareto_lpdf.hpp index 46813fa67b7..3a42eff3822 100644 --- a/stan/math/prim/scal/prob/pareto_lpdf.hpp +++ b/stan/math/prim/scal/prob/pareto_lpdf.hpp @@ -26,11 +26,13 @@ return_type_t pareto_lpdf(const T_y& y, check_positive_finite(function, "Shape parameter", alpha); check_consistent_sizes(function, "Random variable", y, "Scale parameter", y_min, "Shape parameter", alpha); - if (size_zero(y, y_min, alpha)) + if (size_zero(y, y_min, alpha)) { return 0; + } - if (!include_summand::value) + if (!include_summand::value) { return 0; + } T_partials_return logp(0); @@ -40,8 +42,9 @@ return_type_t pareto_lpdf(const T_y& y, size_t N = max_size(y, y_min, alpha); for (size_t n = 0; n < N; n++) { - if (y_vec[n] < y_min_vec[n]) + if (y_vec[n] < y_min_vec[n]) { return LOG_ZERO; + } } operands_and_partials ops_partials(y, y_min, alpha); @@ -50,49 +53,59 @@ return_type_t pareto_lpdf(const T_y& y, T_y> log_y(length(y)); if (include_summand::value) { - for (size_t n = 0; n < length(y); n++) + for (size_t n = 0; n < length(y); n++) { log_y[n] = log(value_of(y_vec[n])); + } } VectorBuilder::value, T_partials_return, T_y> inv_y(length(y)); if (!is_constant_all::value) { - for (size_t n = 0; n < length(y); n++) + for (size_t n = 0; n < length(y); n++) { inv_y[n] = 1 / value_of(y_vec[n]); + } } VectorBuilder::value, T_partials_return, T_scale> log_y_min(length(y_min)); if (include_summand::value) { - for (size_t n = 0; n < length(y_min); n++) + for (size_t n = 0; n < length(y_min); n++) { log_y_min[n] = log(value_of(y_min_vec[n])); + } } VectorBuilder::value, T_partials_return, T_shape> log_alpha(length(alpha)); if (include_summand::value) { - for (size_t n = 0; n < length(alpha); n++) + for (size_t n = 0; n < length(alpha); n++) { log_alpha[n] = log(value_of(alpha_vec[n])); + } } for (size_t n = 0; n < N; n++) { const T_partials_return alpha_dbl = value_of(alpha_vec[n]); - if (include_summand::value) + if (include_summand::value) { logp += log_alpha[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += alpha_dbl * log_y_min[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= alpha_dbl * log_y[n] + log_y[n]; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= alpha_dbl * inv_y[n] + inv_y[n]; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += alpha_dbl / value_of(y_min_vec[n]); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += 1 / alpha_dbl + log_y_min[n] - log_y[n]; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/pareto_type_2_cdf.hpp b/stan/math/prim/scal/prob/pareto_type_2_cdf.hpp index 0f1a465a562..d17f6b3816d 100644 --- a/stan/math/prim/scal/prob/pareto_type_2_cdf.hpp +++ b/stan/math/prim/scal/prob/pareto_type_2_cdf.hpp @@ -21,8 +21,9 @@ return_type_t pareto_type_2_cdf( typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, lambda, alpha)) + if (size_zero(y, mu, lambda, alpha)) { return 1.0; + } static const char* function = "pareto_type_2_cdf"; @@ -65,11 +66,13 @@ return_type_t pareto_type_2_cdf( = 1 + (value_of(y_vec[i]) - value_of(mu_vec[i])) / lambda_dbl; p1_pow_alpha[i] = pow(temp, -alpha_dbl); - if (!is_constant_all::value) + if (!is_constant_all::value) { grad_1_2[i] = p1_pow_alpha[i] / temp * alpha_dbl / lambda_dbl; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { grad_3[i] = log(temp) * p1_pow_alpha[i]; + } } for (size_t n = 0; n < N; n++) { @@ -81,32 +84,40 @@ return_type_t pareto_type_2_cdf( P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += grad_1_2[n] / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= grad_1_2[n] / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += (mu_dbl - y_dbl) * grad_1_2[n] / lambda_dbl / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += grad_3[n] / Pn; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(mu); ++n) + for (size_t n = 0; n < stan::length(mu); ++n) { ops_partials.edge2_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(lambda); ++n) + for (size_t n = 0; n < stan::length(lambda); ++n) { ops_partials.edge3_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(alpha); ++n) + for (size_t n = 0; n < stan::length(alpha); ++n) { ops_partials.edge4_.partials_[n] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/pareto_type_2_lccdf.hpp b/stan/math/prim/scal/prob/pareto_type_2_lccdf.hpp index c09fd506a1a..9f4f9224f7e 100644 --- a/stan/math/prim/scal/prob/pareto_type_2_lccdf.hpp +++ b/stan/math/prim/scal/prob/pareto_type_2_lccdf.hpp @@ -21,8 +21,9 @@ return_type_t pareto_type_2_lccdf( typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, lambda, alpha)) + if (size_zero(y, mu, lambda, alpha)) { return 0.0; + } static const char* function = "pareto_type_2_lccdf"; @@ -68,11 +69,13 @@ return_type_t pareto_type_2_lccdf( ccdf_log[i] = -alpha_dbl * log_temp; - if (!is_constant_all::value) + if (!is_constant_all::value) { a_over_lambda_plus_y[i] = alpha_dbl / (y_dbl - mu_dbl + lambda_dbl); + } - if (!is_constant_all::value) + if (!is_constant_all::value) { log_1p_y_over_lambda[i] = log_temp; + } } for (size_t n = 0; n < N; n++) { @@ -82,15 +85,19 @@ return_type_t pareto_type_2_lccdf( P += ccdf_log[n]; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= a_over_lambda_plus_y[n]; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += a_over_lambda_plus_y[n]; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += a_over_lambda_plus_y[n] * (y_dbl - mu_dbl) / lambda_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] -= log_1p_y_over_lambda[n]; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/pareto_type_2_lcdf.hpp b/stan/math/prim/scal/prob/pareto_type_2_lcdf.hpp index 2f173b5a829..086da300c7f 100644 --- a/stan/math/prim/scal/prob/pareto_type_2_lcdf.hpp +++ b/stan/math/prim/scal/prob/pareto_type_2_lcdf.hpp @@ -22,8 +22,9 @@ return_type_t pareto_type_2_lcdf( typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, lambda, alpha)) + if (size_zero(y, mu, lambda, alpha)) { return 0.0; + } static const char* function = "pareto_type_2_lcdf"; @@ -67,8 +68,9 @@ return_type_t pareto_type_2_lcdf( inv_p1_pow_alpha_minus_one[i] = 1.0 / (p1_pow_alpha - 1.0); - if (!is_constant_all::value) + if (!is_constant_all::value) { log_1p_y_over_lambda[i] = log(temp); + } } for (size_t n = 0; n < N; n++) { @@ -82,16 +84,20 @@ return_type_t pareto_type_2_lcdf( P += cdf_log[n]; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += grad_1_2; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= grad_1_2; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += (mu_dbl - y_dbl) * grad_1_2 / lambda_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += log_1p_y_over_lambda[n] * inv_p1_pow_alpha_minus_one[n]; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/pareto_type_2_lpdf.hpp b/stan/math/prim/scal/prob/pareto_type_2_lpdf.hpp index 4497d5e5f08..52effe0b56a 100644 --- a/stan/math/prim/scal/prob/pareto_type_2_lpdf.hpp +++ b/stan/math/prim/scal/prob/pareto_type_2_lpdf.hpp @@ -27,8 +27,9 @@ return_type_t pareto_type_2_lpdf( using std::log; - if (size_zero(y, mu, lambda, alpha)) + if (size_zero(y, mu, lambda, alpha)) { return 0.0; + } T_partials_return logp(0.0); @@ -39,8 +40,9 @@ return_type_t pareto_type_2_lpdf( check_consistent_sizes(function, "Random variable", y, "Scale parameter", lambda, "Shape parameter", alpha); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); @@ -55,32 +57,36 @@ return_type_t pareto_type_2_lpdf( T_partials_return, T_y, T_loc, T_scale> log1p_scaled_diff(N); if (include_summand::value) { - for (size_t n = 0; n < N; n++) + for (size_t n = 0; n < N; n++) { log1p_scaled_diff[n] = log1p((value_of(y_vec[n]) - value_of(mu_vec[n])) / value_of(lambda_vec[n])); + } } VectorBuilder::value, T_partials_return, T_scale> log_lambda(length(lambda)); if (include_summand::value) { - for (size_t n = 0; n < length(lambda); n++) + for (size_t n = 0; n < length(lambda); n++) { log_lambda[n] = log(value_of(lambda_vec[n])); + } } VectorBuilder::value, T_partials_return, T_shape> log_alpha(length(alpha)); if (include_summand::value) { - for (size_t n = 0; n < length(alpha); n++) + for (size_t n = 0; n < length(alpha); n++) { log_alpha[n] = log(value_of(alpha_vec[n])); + } } VectorBuilder::value, T_partials_return, T_shape> inv_alpha(length(alpha)); if (!is_constant_all::value) { - for (size_t n = 0; n < length(alpha); n++) + for (size_t n = 0; n < length(alpha); n++) { inv_alpha[n] = 1 / value_of(alpha_vec[n]); + } } for (size_t n = 0; n < N; n++) { @@ -93,22 +99,29 @@ return_type_t pareto_type_2_lpdf( const T_partials_return alpha_div_sum = alpha_dbl / sum_dbl; const T_partials_return deriv_1_2 = inv_sum + alpha_div_sum; - if (include_summand::value) + if (include_summand::value) { logp += log_alpha[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= log_lambda[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= (alpha_dbl + 1.0) * log1p_scaled_diff[n]; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= deriv_1_2; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += deriv_1_2; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= alpha_div_sum * (mu_dbl - y_dbl) / lambda_dbl + inv_sum; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += inv_alpha[n] - log1p_scaled_diff[n]; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/pareto_type_2_rng.hpp b/stan/math/prim/scal/prob/pareto_type_2_rng.hpp index 47c3ec9bec7..40f546674f3 100644 --- a/stan/math/prim/scal/prob/pareto_type_2_rng.hpp +++ b/stan/math/prim/scal/prob/pareto_type_2_rng.hpp @@ -56,10 +56,11 @@ pareto_type_2_rng(const T_loc& mu, const T_scale& lambda, const T_shape& alpha, variate_generator > uniform_rng( rng, uniform_real_distribution<>(0.0, 1.0)); - for (size_t n = 0; n < N; ++n) + for (size_t n = 0; n < N; ++n) { output[n] = (std::pow(1.0 - uniform_rng(), -1.0 / alpha_vec[n]) - 1.0) * lambda_vec[n] + mu_vec[n]; + } return output.data(); } diff --git a/stan/math/prim/scal/prob/poisson_cdf.hpp b/stan/math/prim/scal/prob/poisson_cdf.hpp index 1418e9e43d6..f1d27e25b02 100644 --- a/stan/math/prim/scal/prob/poisson_cdf.hpp +++ b/stan/math/prim/scal/prob/poisson_cdf.hpp @@ -21,8 +21,9 @@ return_type_t poisson_cdf(const T_n& n, const T_rate& lambda) { static const char* function = "poisson_cdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, lambda)) + if (size_zero(n, lambda)) { return 1.0; + } T_partials_return P(1.0); @@ -43,15 +44,17 @@ return_type_t poisson_cdf(const T_n& n, const T_rate& lambda) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(0.0); + } } for (size_t i = 0; i < size; i++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(n_vec[i]) == std::numeric_limits::max()) + if (value_of(n_vec[i]) == std::numeric_limits::max()) { continue; + } const T_partials_return n_dbl = value_of(n_vec[i]); const T_partials_return lambda_dbl = value_of(lambda_vec[i]); @@ -59,14 +62,16 @@ return_type_t poisson_cdf(const T_n& n, const T_rate& lambda) { P *= Pi; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] -= exp(-lambda_dbl) * pow(lambda_dbl, n_dbl) / tgamma(n_dbl + 1) / Pi; + } } if (!is_constant_all::value) { - for (size_t i = 0; i < stan::length(lambda); ++i) + for (size_t i = 0; i < stan::length(lambda); ++i) { ops_partials.edge1_.partials_[i] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/poisson_lccdf.hpp b/stan/math/prim/scal/prob/poisson_lccdf.hpp index 6dd527c5af2..0485c5d0bbb 100644 --- a/stan/math/prim/scal/prob/poisson_lccdf.hpp +++ b/stan/math/prim/scal/prob/poisson_lccdf.hpp @@ -20,8 +20,9 @@ return_type_t poisson_lccdf(const T_n& n, const T_rate& lambda) { static const char* function = "poisson_lccdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, lambda)) + if (size_zero(n, lambda)) { return 0.0; + } T_partials_return P(0.0); @@ -42,15 +43,17 @@ return_type_t poisson_lccdf(const T_n& n, const T_rate& lambda) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as neg infinity for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(0.0); + } } for (size_t i = 0; i < size; i++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(n_vec[i]) == std::numeric_limits::max()) + if (value_of(n_vec[i]) == std::numeric_limits::max()) { return ops_partials.build(negative_infinity()); + } const T_partials_return n_dbl = value_of(n_vec[i]); const T_partials_return lambda_dbl = value_of(lambda_vec[i]); @@ -58,9 +61,10 @@ return_type_t poisson_lccdf(const T_n& n, const T_rate& lambda) { P += log_Pi; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += exp( n_dbl * log(lambda_dbl) - lambda_dbl - lgamma(n_dbl + 1) - log_Pi); + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/poisson_lcdf.hpp b/stan/math/prim/scal/prob/poisson_lcdf.hpp index afa42e4fbd8..33b1b125f74 100644 --- a/stan/math/prim/scal/prob/poisson_lcdf.hpp +++ b/stan/math/prim/scal/prob/poisson_lcdf.hpp @@ -21,8 +21,9 @@ return_type_t poisson_lcdf(const T_n& n, const T_rate& lambda) { static const char* function = "poisson_lcdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(n, lambda)) + if (size_zero(n, lambda)) { return 0.0; + } T_partials_return P(0.0); @@ -43,15 +44,17 @@ return_type_t poisson_lcdf(const T_n& n, const T_rate& lambda) { // Explicit return for extreme values // The gradients are technically ill-defined, but treated as neg infinity for (size_t i = 0; i < stan::length(n); i++) { - if (value_of(n_vec[i]) < 0) + if (value_of(n_vec[i]) < 0) { return ops_partials.build(negative_infinity()); + } } for (size_t i = 0; i < size; i++) { // Explicit results for extreme values // The gradients are technically ill-defined, but treated as zero - if (value_of(n_vec[i]) == std::numeric_limits::max()) + if (value_of(n_vec[i]) == std::numeric_limits::max()) { continue; + } const T_partials_return n_dbl = value_of(n_vec[i]); const T_partials_return lambda_dbl = value_of(lambda_vec[i]); @@ -59,9 +62,10 @@ return_type_t poisson_lcdf(const T_n& n, const T_rate& lambda) { P += log_Pi; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += -exp( n_dbl * log(lambda_dbl) - lambda_dbl - lgamma(n_dbl + 1) - log_Pi); + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/poisson_log_lpmf.hpp b/stan/math/prim/scal/prob/poisson_log_lpmf.hpp index 060823261aa..fa7393c23a9 100644 --- a/stan/math/prim/scal/prob/poisson_log_lpmf.hpp +++ b/stan/math/prim/scal/prob/poisson_log_lpmf.hpp @@ -25,8 +25,9 @@ return_type_t poisson_log_lpmf(const T_n& n, using std::exp; - if (size_zero(n, alpha)) + if (size_zero(n, alpha)) { return 0.0; + } T_partials_return logp(0.0); @@ -35,21 +36,26 @@ return_type_t poisson_log_lpmf(const T_n& n, check_consistent_sizes(function, "Random variable", n, "Log rate parameter", alpha); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view n_vec(n); scalar_seq_view alpha_vec(alpha); size_t size = max_size(n, alpha); // FIXME: first loop size of alpha_vec, second loop if-ed for size==1 - for (size_t i = 0; i < size; i++) - if (std::numeric_limits::infinity() == alpha_vec[i]) + for (size_t i = 0; i < size; i++) { + if (std::numeric_limits::infinity() == alpha_vec[i]) { return LOG_ZERO; - for (size_t i = 0; i < size; i++) + } + } + for (size_t i = 0; i < size; i++) { if (-std::numeric_limits::infinity() == alpha_vec[i] - && n_vec[i] != 0) + && n_vec[i] != 0) { return LOG_ZERO; + } + } operands_and_partials ops_partials(alpha); @@ -57,21 +63,26 @@ return_type_t poisson_log_lpmf(const T_n& n, VectorBuilder::value, T_partials_return, T_log_rate> exp_alpha(length(alpha)); - for (size_t i = 0; i < length(alpha); i++) - if (include_summand::value) + for (size_t i = 0; i < length(alpha); i++) { + if (include_summand::value) { exp_alpha[i] = exp(value_of(alpha_vec[i])); + } + } for (size_t i = 0; i < size; i++) { if (!(alpha_vec[i] == -std::numeric_limits::infinity() && n_vec[i] == 0)) { - if (include_summand::value) + if (include_summand::value) { logp -= lgamma(n_vec[i] + 1.0); - if (include_summand::value) + } + if (include_summand::value) { logp += n_vec[i] * value_of(alpha_vec[i]) - exp_alpha[i]; + } } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += n_vec[i] - exp_alpha[i]; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/poisson_lpmf.hpp b/stan/math/prim/scal/prob/poisson_lpmf.hpp index 9d49722d4ae..c80f0bacda8 100644 --- a/stan/math/prim/scal/prob/poisson_lpmf.hpp +++ b/stan/math/prim/scal/prob/poisson_lpmf.hpp @@ -23,8 +23,9 @@ return_type_t poisson_lpmf(const T_n& n, const T_rate& lambda) { static const char* function = "poisson_lpmf"; - if (size_zero(n, lambda)) + if (size_zero(n, lambda)) { return 0.0; + } T_partials_return logp(0.0); @@ -34,34 +35,42 @@ return_type_t poisson_lpmf(const T_n& n, const T_rate& lambda) { check_consistent_sizes(function, "Random variable", n, "Rate parameter", lambda); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view n_vec(n); scalar_seq_view lambda_vec(lambda); size_t size = max_size(n, lambda); - for (size_t i = 0; i < size; i++) - if (is_inf(lambda_vec[i])) + for (size_t i = 0; i < size; i++) { + if (is_inf(lambda_vec[i])) { return LOG_ZERO; - for (size_t i = 0; i < size; i++) - if (lambda_vec[i] == 0 && n_vec[i] != 0) + } + } + for (size_t i = 0; i < size; i++) { + if (lambda_vec[i] == 0 && n_vec[i] != 0) { return LOG_ZERO; + } + } operands_and_partials ops_partials(lambda); for (size_t i = 0; i < size; i++) { if (!(lambda_vec[i] == 0 && n_vec[i] == 0)) { - if (include_summand::value) + if (include_summand::value) { logp -= lgamma(n_vec[i] + 1.0); - if (include_summand::value) + } + if (include_summand::value) { logp += multiply_log(n_vec[i], value_of(lambda_vec[i])) - value_of(lambda_vec[i]); + } } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[i] += n_vec[i] / value_of(lambda_vec[i]) - 1.0; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/rayleigh_cdf.hpp b/stan/math/prim/scal/prob/rayleigh_cdf.hpp index ed6a279f871..c53b057eb42 100644 --- a/stan/math/prim/scal/prob/rayleigh_cdf.hpp +++ b/stan/math/prim/scal/prob/rayleigh_cdf.hpp @@ -23,8 +23,9 @@ return_type_t rayleigh_cdf(const T_y& y, const T_scale& sigma) { T_partials_return cdf(1.0); - if (size_zero(y, sigma)) + if (size_zero(y, sigma)) { return cdf; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -50,8 +51,9 @@ return_type_t rayleigh_cdf(const T_y& y, const T_scale& sigma) { const T_partials_return inv_sigma_sqr = inv_sigma[n] * inv_sigma[n]; const T_partials_return exp_val = exp(-0.5 * y_sqr * inv_sigma_sqr); - if (include_summand::value) + if (include_summand::value) { cdf *= (1.0 - exp_val); + } } for (size_t n = 0; n < N; n++) { @@ -61,12 +63,14 @@ return_type_t rayleigh_cdf(const T_y& y, const T_scale& sigma) { const T_partials_return exp_val = exp(-0.5 * y_sqr * inv_sigma_sqr); const T_partials_return exp_div_1m_exp = exp_val / (1.0 - exp_val); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += y_dbl * inv_sigma_sqr * exp_div_1m_exp * cdf; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= y_sqr * inv_sigma_sqr * inv_sigma[n] * exp_div_1m_exp * cdf; + } } return ops_partials.build(cdf); } diff --git a/stan/math/prim/scal/prob/rayleigh_lccdf.hpp b/stan/math/prim/scal/prob/rayleigh_lccdf.hpp index 206dd041949..0323b03c99b 100644 --- a/stan/math/prim/scal/prob/rayleigh_lccdf.hpp +++ b/stan/math/prim/scal/prob/rayleigh_lccdf.hpp @@ -20,8 +20,9 @@ return_type_t rayleigh_lccdf(const T_y& y, const T_scale& sigma) { T_partials_return ccdf_log(0.0); - if (size_zero(y, sigma)) + if (size_zero(y, sigma)) { return ccdf_log; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -46,13 +47,16 @@ return_type_t rayleigh_lccdf(const T_y& y, const T_scale& sigma) { const T_partials_return y_sqr = y_dbl * y_dbl; const T_partials_return inv_sigma_sqr = inv_sigma[n] * inv_sigma[n]; - if (include_summand::value) + if (include_summand::value) { ccdf_log += -0.5 * y_sqr * inv_sigma_sqr; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= y_dbl * inv_sigma_sqr; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += y_sqr * inv_sigma_sqr * inv_sigma[n]; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/rayleigh_lcdf.hpp b/stan/math/prim/scal/prob/rayleigh_lcdf.hpp index c6d511929a7..db13944c1fe 100644 --- a/stan/math/prim/scal/prob/rayleigh_lcdf.hpp +++ b/stan/math/prim/scal/prob/rayleigh_lcdf.hpp @@ -23,8 +23,9 @@ return_type_t rayleigh_lcdf(const T_y& y, const T_scale& sigma) { T_partials_return cdf_log(0.0); - if (size_zero(y, sigma)) + if (size_zero(y, sigma)) { return cdf_log; + } check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -51,15 +52,18 @@ return_type_t rayleigh_lcdf(const T_y& y, const T_scale& sigma) { const T_partials_return exp_val = exp(-0.5 * y_sqr * inv_sigma_sqr); const T_partials_return exp_div_1m_exp = exp_val / (1.0 - exp_val); - if (include_summand::value) + if (include_summand::value) { cdf_log += log1m(exp_val); + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += y_dbl * inv_sigma_sqr * exp_div_1m_exp; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= y_sqr * inv_sigma_sqr * inv_sigma[n] * exp_div_1m_exp; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/rayleigh_lpdf.hpp b/stan/math/prim/scal/prob/rayleigh_lpdf.hpp index a69dc8d21a4..d0d6369c37a 100644 --- a/stan/math/prim/scal/prob/rayleigh_lpdf.hpp +++ b/stan/math/prim/scal/prob/rayleigh_lpdf.hpp @@ -20,8 +20,9 @@ return_type_t rayleigh_lpdf(const T_y& y, const T_scale& sigma) { using std::log; - if (size_zero(y, sigma)) + if (size_zero(y, sigma)) { return 0.0; + } T_partials_return logp(0.0); @@ -31,8 +32,9 @@ return_type_t rayleigh_lpdf(const T_y& y, const T_scale& sigma) { check_consistent_sizes(function, "Random variable", y, "Scale parameter", sigma); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } operands_and_partials ops_partials(y, sigma); @@ -46,8 +48,9 @@ return_type_t rayleigh_lpdf(const T_y& y, const T_scale& sigma) { log_sigma(length(sigma)); for (size_t i = 0; i < length(sigma); i++) { inv_sigma[i] = 1.0 / value_of(sigma_vec[i]); - if (include_summand::value) + if (include_summand::value) { log_sigma[i] = log(value_of(sigma_vec[i])); + } } for (size_t n = 0; n < N; n++) { @@ -55,18 +58,22 @@ return_type_t rayleigh_lpdf(const T_y& y, const T_scale& sigma) { const T_partials_return y_over_sigma = y_dbl * inv_sigma[n]; static double NEGATIVE_HALF = -0.5; - if (include_summand::value) + if (include_summand::value) { logp -= 2.0 * log_sigma[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += log(y_dbl); + } logp += NEGATIVE_HALF * y_over_sigma * y_over_sigma; T_partials_return scaled_diff = inv_sigma[n] * y_over_sigma; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += 1.0 / y_dbl - scaled_diff; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += y_over_sigma * scaled_diff - 2.0 * inv_sigma[n]; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/scaled_inv_chi_square_cdf.hpp b/stan/math/prim/scal/prob/scaled_inv_chi_square_cdf.hpp index df4849d1999..4259b6a7fba 100644 --- a/stan/math/prim/scal/prob/scaled_inv_chi_square_cdf.hpp +++ b/stan/math/prim/scal/prob/scaled_inv_chi_square_cdf.hpp @@ -37,8 +37,9 @@ return_type_t scaled_inv_chi_square_cdf(const T_y& y, const T_scale& s) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, nu, s)) + if (size_zero(y, nu, s)) { return 1.0; + } static const char* function = "scaled_inv_chi_square_cdf"; @@ -62,8 +63,9 @@ return_type_t scaled_inv_chi_square_cdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(0.0); + } } using std::exp; @@ -104,34 +106,40 @@ return_type_t scaled_inv_chi_square_cdf(const T_y& y, P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += half_nu_s2_overx_dbl * y_inv_dbl * gamma_p_deriv / Pn; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += (0.5 * grad_reg_inc_gamma(half_nu_dbl, half_nu_s2_overx_dbl, gamma_vec[n], digamma_vec[n]) - half_s2_overx_dbl * gamma_p_deriv) / Pn; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -2.0 * half_nu_dbl * s_dbl * y_inv_dbl * gamma_p_deriv / Pn; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(nu); ++n) + for (size_t n = 0; n < stan::length(nu); ++n) { ops_partials.edge2_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(s); ++n) + for (size_t n = 0; n < stan::length(s); ++n) { ops_partials.edge3_.partials_[n] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/scaled_inv_chi_square_lccdf.hpp b/stan/math/prim/scal/prob/scaled_inv_chi_square_lccdf.hpp index 571171926a2..e763b72578c 100644 --- a/stan/math/prim/scal/prob/scaled_inv_chi_square_lccdf.hpp +++ b/stan/math/prim/scal/prob/scaled_inv_chi_square_lccdf.hpp @@ -24,8 +24,9 @@ return_type_t scaled_inv_chi_square_lccdf( const T_y& y, const T_dof& nu, const T_scale& s) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, nu, s)) + if (size_zero(y, nu, s)) { return 0.0; + } static const char* function = "scaled_inv_chi_square_lccdf"; @@ -49,8 +50,9 @@ return_type_t scaled_inv_chi_square_lccdf( // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(0.0); + } } using std::exp; @@ -92,19 +94,22 @@ return_type_t scaled_inv_chi_square_lccdf( P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= half_nu_s2_overx_dbl * y_inv_dbl * gamma_p_deriv / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= (0.5 * grad_reg_inc_gamma(half_nu_dbl, half_nu_s2_overx_dbl, gamma_vec[n], digamma_vec[n]) - half_s2_overx_dbl * gamma_p_deriv) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += 2.0 * half_nu_dbl * s_dbl * y_inv_dbl * gamma_p_deriv / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/scaled_inv_chi_square_lcdf.hpp b/stan/math/prim/scal/prob/scaled_inv_chi_square_lcdf.hpp index d7567e5e73b..528ffb8b53d 100644 --- a/stan/math/prim/scal/prob/scaled_inv_chi_square_lcdf.hpp +++ b/stan/math/prim/scal/prob/scaled_inv_chi_square_lcdf.hpp @@ -24,8 +24,9 @@ return_type_t scaled_inv_chi_square_lcdf( const T_y& y, const T_dof& nu, const T_scale& s) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, nu, s)) + if (size_zero(y, nu, s)) { return 0.0; + } static const char* function = "scaled_inv_chi_square_lcdf"; @@ -49,8 +50,9 @@ return_type_t scaled_inv_chi_square_lcdf( // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == 0) + if (value_of(y_vec[i]) == 0) { return ops_partials.build(negative_infinity()); + } } using std::exp; @@ -92,19 +94,22 @@ return_type_t scaled_inv_chi_square_lcdf( P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += half_nu_s2_overx_dbl * y_inv_dbl * gamma_p_deriv / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += (0.5 * grad_reg_inc_gamma(half_nu_dbl, half_nu_s2_overx_dbl, gamma_vec[n], digamma_vec[n]) - half_s2_overx_dbl * gamma_p_deriv) / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -2.0 * half_nu_dbl * s_dbl * y_inv_dbl * gamma_p_deriv / Pn; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/scaled_inv_chi_square_lpdf.hpp b/stan/math/prim/scal/prob/scaled_inv_chi_square_lpdf.hpp index 84b9e111db0..1a0efb18950 100644 --- a/stan/math/prim/scal/prob/scaled_inv_chi_square_lpdf.hpp +++ b/stan/math/prim/scal/prob/scaled_inv_chi_square_lpdf.hpp @@ -48,10 +48,12 @@ return_type_t scaled_inv_chi_square_lpdf( check_consistent_sizes(function, "Random variable", y, "Degrees of freedom parameter", nu, "Scale parameter", s); - if (size_zero(y, nu, s)) + if (size_zero(y, nu, s)) { return 0; - if (!include_summand::value) + } + if (!include_summand::value) { return 0; + } T_partials_return logp(0); scalar_seq_view y_vec(y); scalar_seq_view nu_vec(nu); @@ -59,8 +61,9 @@ return_type_t scaled_inv_chi_square_lpdf( size_t N = max_size(y, nu, s); for (size_t n = 0; n < N; n++) { - if (value_of(y_vec[n]) <= 0) + if (value_of(y_vec[n]) <= 0) { return LOG_ZERO; + } } using std::log; @@ -68,30 +71,38 @@ return_type_t scaled_inv_chi_square_lpdf( VectorBuilder::value, T_partials_return, T_dof> half_nu(length(nu)); - for (size_t i = 0; i < length(nu); i++) - if (include_summand::value) + for (size_t i = 0; i < length(nu); i++) { + if (include_summand::value) { half_nu[i] = 0.5 * value_of(nu_vec[i]); + } + } VectorBuilder::value, T_partials_return, T_y> log_y(length(y)); - for (size_t i = 0; i < length(y); i++) - if (include_summand::value) + for (size_t i = 0; i < length(y); i++) { + if (include_summand::value) { log_y[i] = log(value_of(y_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_y> inv_y(length(y)); - for (size_t i = 0; i < length(y); i++) - if (include_summand::value) + for (size_t i = 0; i < length(y); i++) { + if (include_summand::value) { inv_y[i] = 1.0 / value_of(y_vec[i]); + } + } VectorBuilder::value, T_partials_return, T_scale> log_s(length(s)); - for (size_t i = 0; i < length(s); i++) - if (include_summand::value) + for (size_t i = 0; i < length(s); i++) { + if (include_summand::value) { log_s[i] = log(value_of(s_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_dof> log_half_nu(length(nu)); @@ -100,26 +111,33 @@ return_type_t scaled_inv_chi_square_lpdf( VectorBuilder::value, T_partials_return, T_dof> digamma_half_nu_over_two(length(nu)); for (size_t i = 0; i < length(nu); i++) { - if (include_summand::value) + if (include_summand::value) { lgamma_half_nu[i] = lgamma(half_nu[i]); - if (include_summand::value) + } + if (include_summand::value) { log_half_nu[i] = log(half_nu[i]); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { digamma_half_nu_over_two[i] = digamma(half_nu[i]) * 0.5; + } } operands_and_partials ops_partials(y, nu, s); for (size_t n = 0; n < N; n++) { const T_partials_return s_dbl = value_of(s_vec[n]); const T_partials_return nu_dbl = value_of(nu_vec[n]); - if (include_summand::value) + if (include_summand::value) { logp += half_nu[n] * log_half_nu[n] - lgamma_half_nu[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += nu_dbl * log_s[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= (half_nu[n] + 1.0) * log_y[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= half_nu[n] * s_dbl * s_dbl * inv_y[n]; + } if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] diff --git a/stan/math/prim/scal/prob/skew_normal_cdf.hpp b/stan/math/prim/scal/prob/skew_normal_cdf.hpp index 4a8e7dff7e6..d2a90b1271d 100644 --- a/stan/math/prim/scal/prob/skew_normal_cdf.hpp +++ b/stan/math/prim/scal/prob/skew_normal_cdf.hpp @@ -24,8 +24,9 @@ return_type_t skew_normal_cdf( T_partials_return cdf(1.0); - if (size_zero(y, mu, sigma, alpha)) + if (size_zero(y, mu, sigma, alpha)) { return cdf; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -71,33 +72,41 @@ return_type_t skew_normal_cdf( const T_partials_return rep_deriv = (-2.0 * deriv_owens + deriv_erfc) / cdf_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * diff; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += -2.0 * exp(-0.5 * diff_sq * (1.0 + alpha_dbl_sq)) / ((1 + alpha_dbl_sq) * 2.0 * pi()) / cdf_; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(mu); ++n) + for (size_t n = 0; n < stan::length(mu); ++n) { ops_partials.edge2_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(sigma); ++n) + for (size_t n = 0; n < stan::length(sigma); ++n) { ops_partials.edge3_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(alpha); ++n) + for (size_t n = 0; n < stan::length(alpha); ++n) { ops_partials.edge4_.partials_[n] *= cdf; + } } return ops_partials.build(cdf); } diff --git a/stan/math/prim/scal/prob/skew_normal_lccdf.hpp b/stan/math/prim/scal/prob/skew_normal_lccdf.hpp index 2d99db2a967..5e4e360fdd6 100644 --- a/stan/math/prim/scal/prob/skew_normal_lccdf.hpp +++ b/stan/math/prim/scal/prob/skew_normal_lccdf.hpp @@ -24,8 +24,9 @@ return_type_t skew_normal_lccdf( T_partials_return ccdf_log(0.0); - if (size_zero(y, mu, sigma, alpha)) + if (size_zero(y, mu, sigma, alpha)) { return ccdf_log; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -72,16 +73,20 @@ return_type_t skew_normal_lccdf( const T_partials_return rep_deriv = (-2.0 * deriv_owens + deriv_erfc) / ccdf_log_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += rep_deriv * diff; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] -= -2.0 * exp(-0.5 * diff_sq * (1.0 + alpha_dbl_sq)) / ((1 + alpha_dbl_sq) * 2.0 * pi()) / ccdf_log_; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/skew_normal_lcdf.hpp b/stan/math/prim/scal/prob/skew_normal_lcdf.hpp index 99ff3ff8b48..e6be0934164 100644 --- a/stan/math/prim/scal/prob/skew_normal_lcdf.hpp +++ b/stan/math/prim/scal/prob/skew_normal_lcdf.hpp @@ -24,8 +24,9 @@ return_type_t skew_normal_lcdf( T_partials_return cdf_log(0.0); - if (size_zero(y, mu, sigma, alpha)) + if (size_zero(y, mu, sigma, alpha)) { return cdf_log; + } check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -72,16 +73,20 @@ return_type_t skew_normal_lcdf( const T_partials_return rep_deriv = (-2.0 * deriv_owens + deriv_erfc) / cdf_log_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= rep_deriv; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * diff; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += -2.0 * exp(-0.5 * diff_sq * (1.0 + alpha_dbl_sq)) / ((1 + alpha_dbl_sq) * 2.0 * pi()) / cdf_log_; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/skew_normal_lpdf.hpp b/stan/math/prim/scal/prob/skew_normal_lpdf.hpp index 1b5237d3608..51ea25eb803 100644 --- a/stan/math/prim/scal/prob/skew_normal_lpdf.hpp +++ b/stan/math/prim/scal/prob/skew_normal_lpdf.hpp @@ -27,8 +27,9 @@ return_type_t skew_normal_lpdf( using std::exp; using std::log; - if (size_zero(y, mu, sigma, alpha)) + if (size_zero(y, mu, sigma, alpha)) { return 0.0; + } T_partials_return logp(0.0); @@ -39,8 +40,9 @@ return_type_t skew_normal_lpdf( check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma, "Shape paramter", alpha); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } operands_and_partials ops_partials(y, mu, sigma, alpha); @@ -57,8 +59,9 @@ return_type_t skew_normal_lpdf( log_sigma(length(sigma)); for (size_t i = 0; i < length(sigma); i++) { inv_sigma[i] = 1.0 / value_of(sigma_vec[i]); - if (include_summand::value) + if (include_summand::value) { log_sigma[i] = log(value_of(sigma_vec[i])); + } } for (size_t n = 0; n < N; n++) { @@ -71,37 +74,45 @@ return_type_t skew_normal_lpdf( = (y_dbl - mu_dbl) * inv_sigma[n]; const double pi_dbl = pi(); - if (include_summand::value) + if (include_summand::value) { logp -= 0.5 * log(2.0 * pi_dbl); - if (include_summand::value) + } + if (include_summand::value) { logp -= log(sigma_dbl); - if (include_summand::value) + } + if (include_summand::value) { logp -= y_minus_mu_over_sigma * y_minus_mu_over_sigma / 2.0; - if (include_summand::value) + } + if (include_summand::value) { logp += log(erfc(-alpha_dbl * y_minus_mu_over_sigma / std::sqrt(2.0))); + } T_partials_return deriv_logerf = 2.0 / std::sqrt(pi_dbl) * exp(-alpha_dbl * y_minus_mu_over_sigma / std::sqrt(2.0) * alpha_dbl * y_minus_mu_over_sigma / std::sqrt(2.0)) / (1 + erf(alpha_dbl * y_minus_mu_over_sigma / std::sqrt(2.0))); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += -y_minus_mu_over_sigma / sigma_dbl + deriv_logerf * alpha_dbl / (sigma_dbl * std::sqrt(2.0)); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += y_minus_mu_over_sigma / sigma_dbl + deriv_logerf * -alpha_dbl / (sigma_dbl * std::sqrt(2.0)); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -1.0 / sigma_dbl + y_minus_mu_over_sigma * y_minus_mu_over_sigma / sigma_dbl - deriv_logerf * y_minus_mu_over_sigma * alpha_dbl / (sigma_dbl * std::sqrt(2.0)); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += deriv_logerf * y_minus_mu_over_sigma / std::sqrt(2.0); + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/skew_normal_rng.hpp b/stan/math/prim/scal/prob/skew_normal_rng.hpp index 6754a3c41a1..a53867fffce 100644 --- a/stan/math/prim/scal/prob/skew_normal_rng.hpp +++ b/stan/math/prim/scal/prob/skew_normal_rng.hpp @@ -59,8 +59,9 @@ skew_normal_rng(const T_loc& mu, const T_scale& sigma, const T_shape& alpha, double r1 = norm_rng(); double r2 = norm_rng(); - if (r2 > alpha_vec[n] * r1) + if (r2 > alpha_vec[n] * r1) { r1 = -r1; + } output[n] = mu_vec[n] + sigma_vec[n] * r1; } diff --git a/stan/math/prim/scal/prob/std_normal_lpdf.hpp b/stan/math/prim/scal/prob/std_normal_lpdf.hpp index 2feedbe8b54..898fa1b680b 100644 --- a/stan/math/prim/scal/prob/std_normal_lpdf.hpp +++ b/stan/math/prim/scal/prob/std_normal_lpdf.hpp @@ -27,13 +27,15 @@ return_type_t std_normal_lpdf(const T_y& y) { static const char* function = "std_normal_lpdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(y)) + if (size_zero(y)) { return 0.0; + } check_not_nan(function, "Random variable", y); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } operands_and_partials ops_partials(y); scalar_seq_view y_vec(y); @@ -41,12 +43,14 @@ return_type_t std_normal_lpdf(const T_y& y) { for (size_t n = 0; n < length(y); n++) { const T_partials_return y_val = value_of(y_vec[n]); logp += y_val * y_val; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= y_val; + } } logp *= -0.5; - if (include_summand::value) + if (include_summand::value) { logp += NEG_LOG_SQRT_TWO_PI * length(y); + } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/student_t_cdf.hpp b/stan/math/prim/scal/prob/student_t_cdf.hpp index 07703c6b38d..5e2e114fb22 100644 --- a/stan/math/prim/scal/prob/student_t_cdf.hpp +++ b/stan/math/prim/scal/prob/student_t_cdf.hpp @@ -24,8 +24,9 @@ return_type_t student_t_cdf(const T_y& y, const T_scale& sigma) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, nu, mu, sigma)) + if (size_zero(y, nu, mu, sigma)) { return 1.0; + } static const char* function = "student_t_cdf"; @@ -48,8 +49,9 @@ return_type_t student_t_cdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) + if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) { return ops_partials.build(0.0); + } } using std::exp; @@ -101,9 +103,10 @@ return_type_t student_t_cdf(const T_y& y, P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += -zJacobian * d_ibeta * J * sigma_inv / Pn; + } if (!is_constant_all::value) { T_partials_return g1 = 0; T_partials_return g2 = 0; @@ -116,12 +119,14 @@ return_type_t student_t_cdf(const T_y& y, += zJacobian * (d_ibeta * (r / t) * (r / t) + 0.5 * g1) / Pn; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += zJacobian * d_ibeta * J * sigma_inv / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += zJacobian * d_ibeta * J * sigma_inv * t / Pn; + } } else { T_partials_return z @@ -136,9 +141,10 @@ return_type_t student_t_cdf(const T_y& y, P *= Pn; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += zJacobian * d_ibeta * J * sigma_inv / Pn; + } if (!is_constant_all::value) { T_partials_return g1 = 0; T_partials_return g2 = 0; @@ -150,30 +156,36 @@ return_type_t student_t_cdf(const T_y& y, ops_partials.edge2_.partials_[n] += zJacobian * (-d_ibeta * (r / t) * (r / t) + 0.5 * g2) / Pn; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -zJacobian * d_ibeta * J * sigma_inv / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += -zJacobian * d_ibeta * J * sigma_inv * t / Pn; + } } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(nu); ++n) + for (size_t n = 0; n < stan::length(nu); ++n) { ops_partials.edge2_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(mu); ++n) + for (size_t n = 0; n < stan::length(mu); ++n) { ops_partials.edge3_.partials_[n] *= P; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(sigma); ++n) + for (size_t n = 0; n < stan::length(sigma); ++n) { ops_partials.edge4_.partials_[n] *= P; + } } return ops_partials.build(P); } diff --git a/stan/math/prim/scal/prob/student_t_lccdf.hpp b/stan/math/prim/scal/prob/student_t_lccdf.hpp index 4a83e7e51e5..af3fcb1042c 100644 --- a/stan/math/prim/scal/prob/student_t_lccdf.hpp +++ b/stan/math/prim/scal/prob/student_t_lccdf.hpp @@ -23,8 +23,9 @@ return_type_t student_t_lccdf( const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& sigma) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, nu, mu, sigma)) + if (size_zero(y, nu, mu, sigma)) { return 0.0; + } static const char* function = "student_t_lccdf"; @@ -47,8 +48,9 @@ return_type_t student_t_lccdf( // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) + if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) { return ops_partials.build(0.0); + } } using std::exp; @@ -101,9 +103,10 @@ return_type_t student_t_lccdf( P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += zJacobian * d_ibeta * J * sigma_inv / Pn; + } if (!is_constant_all::value) { T_partials_return g1 = 0; @@ -117,12 +120,14 @@ return_type_t student_t_lccdf( -= zJacobian * (d_ibeta * (r / t) * (r / t) + 0.5 * g1) / Pn; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= zJacobian * d_ibeta * J * sigma_inv / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] -= zJacobian * d_ibeta * J * sigma_inv * t / Pn; + } } else { T_partials_return z @@ -136,9 +141,10 @@ return_type_t student_t_lccdf( P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= zJacobian * d_ibeta * J * sigma_inv / Pn; + } if (!is_constant_all::value) { T_partials_return g1 = 0; @@ -152,12 +158,14 @@ return_type_t student_t_lccdf( -= zJacobian * (-d_ibeta * (r / t) * (r / t) + 0.5 * g2) / Pn; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += zJacobian * d_ibeta * J * sigma_inv / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += zJacobian * d_ibeta * J * sigma_inv * t / Pn; + } } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/student_t_lcdf.hpp b/stan/math/prim/scal/prob/student_t_lcdf.hpp index a4e6354d80e..085ad207763 100644 --- a/stan/math/prim/scal/prob/student_t_lcdf.hpp +++ b/stan/math/prim/scal/prob/student_t_lcdf.hpp @@ -25,8 +25,9 @@ return_type_t student_t_lcdf(const T_y& y, const T_scale& sigma) { typedef partials_return_type_t T_partials_return; - if (size_zero(y, nu, mu, sigma)) + if (size_zero(y, nu, mu, sigma)) { return 0.0; + } static const char* function = "student_t_lcdf"; @@ -49,8 +50,9 @@ return_type_t student_t_lcdf(const T_y& y, // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::length(y); i++) { - if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) + if (value_of(y_vec[i]) == -std::numeric_limits::infinity()) { return ops_partials.build(negative_infinity()); + } } using std::exp; @@ -103,9 +105,10 @@ return_type_t student_t_lcdf(const T_y& y, P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += -zJacobian * d_ibeta * J * sigma_inv / Pn; + } if (!is_constant_all::value) { T_partials_return g1 = 0; @@ -119,12 +122,14 @@ return_type_t student_t_lcdf(const T_y& y, += zJacobian * (d_ibeta * (r / t) * (r / t) + 0.5 * g1) / Pn; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += zJacobian * d_ibeta * J * sigma_inv / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += zJacobian * d_ibeta * J * sigma_inv * t / Pn; + } } else { T_partials_return z @@ -138,9 +143,10 @@ return_type_t student_t_lcdf(const T_y& y, P += log(Pn); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += zJacobian * d_ibeta * J * sigma_inv / Pn; + } if (!is_constant_all::value) { T_partials_return g1 = 0; @@ -154,12 +160,14 @@ return_type_t student_t_lcdf(const T_y& y, += zJacobian * (-d_ibeta * (r / t) * (r / t) + 0.5 * g2) / Pn; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += -zJacobian * d_ibeta * J * sigma_inv / Pn; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge4_.partials_[n] += -zJacobian * d_ibeta * J * sigma_inv * t / Pn; + } } } return ops_partials.build(P); diff --git a/stan/math/prim/scal/prob/student_t_lpdf.hpp b/stan/math/prim/scal/prob/student_t_lpdf.hpp index bd7d8c09c75..d2775d09381 100644 --- a/stan/math/prim/scal/prob/student_t_lpdf.hpp +++ b/stan/math/prim/scal/prob/student_t_lpdf.hpp @@ -54,8 +54,9 @@ return_type_t student_t_lpdf(const T_y& y, static const char* function = "student_t_lpdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(y, nu, mu, sigma)) + if (size_zero(y, nu, mu, sigma)) { return 0.0; + } T_partials_return logp(0.0); @@ -67,8 +68,9 @@ return_type_t student_t_lpdf(const T_y& y, "Degrees of freedom parameter", nu, "Location parameter", mu, "Scale parameter", sigma); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view y_vec(y); scalar_seq_view nu_vec(nu); @@ -81,9 +83,11 @@ return_type_t student_t_lpdf(const T_y& y, VectorBuilder::value, T_partials_return, T_dof> half_nu(length(nu)); - for (size_t i = 0; i < length(nu); i++) - if (include_summand::value) + for (size_t i = 0; i < length(nu); i++) { + if (include_summand::value) { half_nu[i] = 0.5 * value_of(nu_vec[i]); + } + } VectorBuilder::value, T_partials_return, T_dof> lgamma_half_nu(length(nu)); @@ -109,16 +113,20 @@ return_type_t student_t_lpdf(const T_y& y, VectorBuilder::value, T_partials_return, T_dof> log_nu(length(nu)); - for (size_t i = 0; i < length(nu); i++) - if (include_summand::value) + for (size_t i = 0; i < length(nu); i++) { + if (include_summand::value) { log_nu[i] = log(value_of(nu_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_scale> log_sigma(length(sigma)); - for (size_t i = 0; i < length(sigma); i++) - if (include_summand::value) + for (size_t i = 0; i < length(sigma); i++) { + if (include_summand::value) { log_sigma[i] = log(value_of(sigma_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_y, T_dof, T_loc, T_scale> @@ -128,7 +136,7 @@ return_type_t student_t_lpdf(const T_y& y, T_partials_return, T_y, T_dof, T_loc, T_scale> log1p_exp(N); - for (size_t i = 0; i < N; i++) + for (size_t i = 0; i < N; i++) { if (include_summand::value) { const T_partials_return y_dbl = value_of(y_vec[i]); const T_partials_return mu_dbl = value_of(mu_vec[i]); @@ -138,6 +146,7 @@ return_type_t student_t_lpdf(const T_y& y, = square((y_dbl - mu_dbl) / sigma_dbl) / nu_dbl; log1p_exp[i] = log1p(square_y_minus_mu_over_sigma__over_nu[i]); } + } operands_and_partials ops_partials(y, nu, mu, sigma); @@ -146,14 +155,18 @@ return_type_t student_t_lpdf(const T_y& y, const T_partials_return mu_dbl = value_of(mu_vec[n]); const T_partials_return sigma_dbl = value_of(sigma_vec[n]); const T_partials_return nu_dbl = value_of(nu_vec[n]); - if (include_summand::value) + if (include_summand::value) { logp += NEG_LOG_SQRT_PI; - if (include_summand::value) + } + if (include_summand::value) { logp += lgamma_half_nu_plus_half[n] - lgamma_half_nu[n] - 0.5 * log_nu[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= log_sigma[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= (half_nu[n] + 0.5) * log1p_exp[n]; + } if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] diff --git a/stan/math/prim/scal/prob/uniform_cdf.hpp b/stan/math/prim/scal/prob/uniform_cdf.hpp index 6e5bf1a5df3..003cfc46e9e 100644 --- a/stan/math/prim/scal/prob/uniform_cdf.hpp +++ b/stan/math/prim/scal/prob/uniform_cdf.hpp @@ -18,8 +18,9 @@ return_type_t uniform_cdf(const T_y& y, const T_low& alpha, static const char* function = "uniform_cdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 1.0; + } T_partials_return cdf(1.0); check_not_nan(function, "Random variable", y); @@ -37,8 +38,9 @@ return_type_t uniform_cdf(const T_y& y, const T_low& alpha, for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); - if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n])) + if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n])) { return 0.0; + } } operands_and_partials ops_partials(y, alpha, beta); @@ -51,26 +53,32 @@ return_type_t uniform_cdf(const T_y& y, const T_low& alpha, cdf *= cdf_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += 1.0 / b_min_a / cdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += (y_dbl - beta_dbl) / b_min_a / b_min_a / cdf_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= 1.0 / b_min_a; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(alpha); ++n) + for (size_t n = 0; n < stan::length(alpha); ++n) { ops_partials.edge2_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(beta); ++n) + for (size_t n = 0; n < stan::length(beta); ++n) { ops_partials.edge3_.partials_[n] *= cdf; + } } return ops_partials.build(cdf); diff --git a/stan/math/prim/scal/prob/uniform_lccdf.hpp b/stan/math/prim/scal/prob/uniform_lccdf.hpp index 1fc382067c1..dcabdc38f17 100644 --- a/stan/math/prim/scal/prob/uniform_lccdf.hpp +++ b/stan/math/prim/scal/prob/uniform_lccdf.hpp @@ -23,8 +23,9 @@ return_type_t uniform_lccdf(const T_y& y, using std::log; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0.0; + } T_partials_return ccdf_log(0.0); check_not_nan(function, "Random variable", y); @@ -42,10 +43,12 @@ return_type_t uniform_lccdf(const T_y& y, for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); - if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n])) + if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n])) { return 0.0; - if (y_dbl == value_of(beta_vec[n])) + } + if (y_dbl == value_of(beta_vec[n])) { return LOG_ZERO; + } } operands_and_partials ops_partials(y, alpha, beta); @@ -58,14 +61,17 @@ return_type_t uniform_lccdf(const T_y& y, ccdf_log += log(ccdf_log_); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= 1.0 / b_min_a / ccdf_log_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= (y_dbl - beta_dbl) / b_min_a / b_min_a / ccdf_log_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += (y_dbl - alpha_dbl) / b_min_a / b_min_a / ccdf_log_; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/uniform_lcdf.hpp b/stan/math/prim/scal/prob/uniform_lcdf.hpp index 4fbffabff6d..756c5d4ac59 100644 --- a/stan/math/prim/scal/prob/uniform_lcdf.hpp +++ b/stan/math/prim/scal/prob/uniform_lcdf.hpp @@ -22,8 +22,9 @@ return_type_t uniform_lcdf(const T_y& y, const T_low& alpha, using std::log; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0.0; + } T_partials_return cdf_log(0.0); check_not_nan(function, "Random variable", y); @@ -43,10 +44,12 @@ return_type_t uniform_lcdf(const T_y& y, const T_low& alpha, for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); - if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n])) + if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n])) { return negative_infinity(); - if (y_dbl == value_of(beta_vec[n])) + } + if (y_dbl == value_of(beta_vec[n])) { return ops_partials.build(0.0); + } } for (size_t n = 0; n < N; n++) { @@ -58,13 +61,16 @@ return_type_t uniform_lcdf(const T_y& y, const T_low& alpha, cdf_log += log(cdf_log_); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += 1.0 / b_min_a / cdf_log_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += (y_dbl - beta_dbl) / b_min_a / b_min_a / cdf_log_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= 1.0 / b_min_a; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/uniform_lpdf.hpp b/stan/math/prim/scal/prob/uniform_lpdf.hpp index 84ef0ea48a1..c2b0032de55 100644 --- a/stan/math/prim/scal/prob/uniform_lpdf.hpp +++ b/stan/math/prim/scal/prob/uniform_lpdf.hpp @@ -44,8 +44,9 @@ return_type_t uniform_lpdf(const T_y& y, const T_low& alpha, using std::log; - if (size_zero(y, alpha, beta)) + if (size_zero(y, alpha, beta)) { return 0.0; + } T_partials_return logp(0.0); check_not_nan(function, "Random variable", y); @@ -56,8 +57,9 @@ return_type_t uniform_lpdf(const T_y& y, const T_low& alpha, "Lower bound parameter", alpha, "Upper bound parameter", beta); - if (!include_summand::value) + if (!include_summand::value) { return 0.0; + } scalar_seq_view y_vec(y); scalar_seq_view alpha_vec(alpha); @@ -66,35 +68,43 @@ return_type_t uniform_lpdf(const T_y& y, const T_low& alpha, for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); - if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n])) + if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n])) { return LOG_ZERO; + } } VectorBuilder::value, T_partials_return, T_low, T_high> inv_beta_minus_alpha(max_size(alpha, beta)); - for (size_t i = 0; i < max_size(alpha, beta); i++) - if (include_summand::value) + for (size_t i = 0; i < max_size(alpha, beta); i++) { + if (include_summand::value) { inv_beta_minus_alpha[i] = 1.0 / (value_of(beta_vec[i]) - value_of(alpha_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_low, T_high> log_beta_minus_alpha(max_size(alpha, beta)); - for (size_t i = 0; i < max_size(alpha, beta); i++) - if (include_summand::value) + for (size_t i = 0; i < max_size(alpha, beta); i++) { + if (include_summand::value) { log_beta_minus_alpha[i] = log(value_of(beta_vec[i]) - value_of(alpha_vec[i])); + } + } operands_and_partials ops_partials(y, alpha, beta); for (size_t n = 0; n < N; n++) { - if (include_summand::value) + if (include_summand::value) { logp -= log_beta_minus_alpha[n]; + } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += inv_beta_minus_alpha[n]; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= inv_beta_minus_alpha[n]; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/von_mises_lpdf.hpp b/stan/math/prim/scal/prob/von_mises_lpdf.hpp index 9ce577aab12..5c12bd8b20e 100644 --- a/stan/math/prim/scal/prob/von_mises_lpdf.hpp +++ b/stan/math/prim/scal/prob/von_mises_lpdf.hpp @@ -21,8 +21,9 @@ return_type_t von_mises_lpdf(T_y const& y, T_loc const& mu, static char const* const function = "von_mises_lpdf"; typedef partials_return_type_t T_partials_return; - if (size_zero(y, mu, kappa)) + if (size_zero(y, mu, kappa)) { return 0.0; + } using std::log; @@ -34,8 +35,9 @@ return_type_t von_mises_lpdf(T_y const& y, T_loc const& mu, check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", kappa); - if (!include_summand::value) + if (!include_summand::value) { return logp; + } const bool y_const = is_constant_all::value; const bool mu_const = is_constant_all::value; @@ -55,9 +57,10 @@ return_type_t von_mises_lpdf(T_y const& y, T_loc const& mu, log_bessel0(length(kappa)); for (size_t i = 0; i < length(kappa); i++) { kappa_dbl[i] = value_of(kappa_vec[i]); - if (include_summand::value) + if (include_summand::value) { log_bessel0[i] = log_modified_bessel_first_kind(0, value_of(kappa_vec[i])); + } } operands_and_partials ops_partials(y, mu, kappa); @@ -70,28 +73,36 @@ return_type_t von_mises_lpdf(T_y const& y, T_loc const& mu, const T_partials_return mu_dbl = value_of(mu_vec[n]); T_partials_return bessel0 = 0; - if (compute_bessel0) + if (compute_bessel0) { bessel0 = modified_bessel_first_kind(0, kappa_dbl[n]); + } T_partials_return bessel1 = 0; - if (compute_bessel1) + if (compute_bessel1) { bessel1 = modified_bessel_first_kind(-1, kappa_dbl[n]); + } const T_partials_return kappa_sin = kappa_dbl[n] * sin(mu_dbl - y_dbl); const T_partials_return kappa_cos = kappa_dbl[n] * cos(mu_dbl - y_dbl); - if (include_summand::value) + if (include_summand::value) { logp -= LOG_TWO_PI; - if (include_summand::value) + } + if (include_summand::value) { logp -= log_bessel0[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += kappa_cos; + } - if (!y_const) + if (!y_const) { ops_partials.edge1_.partials_[n] += kappa_sin; - if (!mu_const) + } + if (!mu_const) { ops_partials.edge2_.partials_[n] -= kappa_sin; - if (!kappa_const) + } + if (!kappa_const) { ops_partials.edge3_.partials_[n] += kappa_cos / kappa_dbl[n] - bessel1 / bessel0; + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/von_mises_rng.hpp b/stan/math/prim/scal/prob/von_mises_rng.hpp index ccbe1567017..53917f5e079 100644 --- a/stan/math/prim/scal/prob/von_mises_rng.hpp +++ b/stan/math/prim/scal/prob/von_mises_rng.hpp @@ -76,8 +76,9 @@ inline typename VectorBuilder::type von_mises_rng( double U2 = uniform_rng(); done = Y * (2 - Y) - U2 > 0; - if (!done) + if (!done) { done = std::log(Y / U2) + 1 - Y >= 0; + } } double U3 = uniform_rng() - 0.5; diff --git a/stan/math/prim/scal/prob/weibull_cdf.hpp b/stan/math/prim/scal/prob/weibull_cdf.hpp index 2b7533287a8..95bf6f70fc7 100644 --- a/stan/math/prim/scal/prob/weibull_cdf.hpp +++ b/stan/math/prim/scal/prob/weibull_cdf.hpp @@ -36,8 +36,9 @@ return_type_t weibull_cdf(const T_y& y, using std::exp; using std::log; - if (size_zero(y, alpha, sigma)) + if (size_zero(y, alpha, sigma)) { return 1.0; + } T_partials_return cdf(1.0); check_nonnegative(function, "Random variable", y); @@ -61,25 +62,31 @@ return_type_t weibull_cdf(const T_y& y, cdf *= cdf_; const T_partials_return rep_deriv = exp_ * pow_ / cdf_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv * alpha_dbl / y_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += rep_deriv * log(y_dbl / sigma_dbl); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * alpha_dbl / sigma_dbl; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(y); ++n) + for (size_t n = 0; n < stan::length(y); ++n) { ops_partials.edge1_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(alpha); ++n) + for (size_t n = 0; n < stan::length(alpha); ++n) { ops_partials.edge2_.partials_[n] *= cdf; + } } if (!is_constant_all::value) { - for (size_t n = 0; n < stan::length(sigma); ++n) + for (size_t n = 0; n < stan::length(sigma); ++n) { ops_partials.edge3_.partials_[n] *= cdf; + } } return ops_partials.build(cdf); } diff --git a/stan/math/prim/scal/prob/weibull_lccdf.hpp b/stan/math/prim/scal/prob/weibull_lccdf.hpp index 63a5120c36d..d088f44dedb 100644 --- a/stan/math/prim/scal/prob/weibull_lccdf.hpp +++ b/stan/math/prim/scal/prob/weibull_lccdf.hpp @@ -35,8 +35,9 @@ return_type_t weibull_lccdf(const T_y& y, using std::log; - if (size_zero(y, alpha, sigma)) + if (size_zero(y, alpha, sigma)) { return 0.0; + } T_partials_return ccdf_log(0.0); check_nonnegative(function, "Random variable", y); @@ -57,12 +58,15 @@ return_type_t weibull_lccdf(const T_y& y, ccdf_log -= pow_; - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] -= alpha_dbl / y_dbl * pow_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] -= log(y_dbl / sigma_dbl) * pow_; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += alpha_dbl / sigma_dbl * pow_; + } } return ops_partials.build(ccdf_log); } diff --git a/stan/math/prim/scal/prob/weibull_lcdf.hpp b/stan/math/prim/scal/prob/weibull_lcdf.hpp index 4aa32776d42..910a3d66f25 100644 --- a/stan/math/prim/scal/prob/weibull_lcdf.hpp +++ b/stan/math/prim/scal/prob/weibull_lcdf.hpp @@ -36,8 +36,9 @@ return_type_t weibull_lcdf(const T_y& y, using std::exp; using std::log; - if (size_zero(y, alpha, sigma)) + if (size_zero(y, alpha, sigma)) { return 0.0; + } T_partials_return cdf_log(0.0); check_nonnegative(function, "Random variable", y); @@ -61,12 +62,15 @@ return_type_t weibull_lcdf(const T_y& y, cdf_log += log(cdf_); const T_partials_return rep_deriv = pow_ / (1.0 / exp_ - 1.0); - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += rep_deriv * alpha_dbl / y_dbl; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += rep_deriv * log(y_dbl / sigma_dbl); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] -= rep_deriv * alpha_dbl / sigma_dbl; + } } return ops_partials.build(cdf_log); } diff --git a/stan/math/prim/scal/prob/weibull_lpdf.hpp b/stan/math/prim/scal/prob/weibull_lpdf.hpp index 4f868e33817..719490587c3 100644 --- a/stan/math/prim/scal/prob/weibull_lpdf.hpp +++ b/stan/math/prim/scal/prob/weibull_lpdf.hpp @@ -40,10 +40,12 @@ return_type_t weibull_lpdf(const T_y& y, check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Random variable", y, "Shape parameter", alpha, "Scale parameter", sigma); - if (size_zero(y, alpha, sigma)) + if (size_zero(y, alpha, sigma)) { return 0; - if (!include_summand::value) + } + if (!include_summand::value) { return 0; + } T_partials_return logp(0); scalar_seq_view y_vec(y); @@ -53,59 +55,73 @@ return_type_t weibull_lpdf(const T_y& y, for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); - if (y_dbl < 0) + if (y_dbl < 0) { return LOG_ZERO; + } } VectorBuilder::value, T_partials_return, T_shape> log_alpha(length(alpha)); - for (size_t i = 0; i < length(alpha); i++) - if (include_summand::value) + for (size_t i = 0; i < length(alpha); i++) { + if (include_summand::value) { log_alpha[i] = log(value_of(alpha_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_y> log_y(length(y)); - for (size_t i = 0; i < length(y); i++) - if (include_summand::value) + for (size_t i = 0; i < length(y); i++) { + if (include_summand::value) { log_y[i] = log(value_of(y_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_scale> log_sigma(length(sigma)); - for (size_t i = 0; i < length(sigma); i++) - if (include_summand::value) + for (size_t i = 0; i < length(sigma); i++) { + if (include_summand::value) { log_sigma[i] = log(value_of(sigma_vec[i])); + } + } VectorBuilder::value, T_partials_return, T_scale> inv_sigma(length(sigma)); - for (size_t i = 0; i < length(sigma); i++) - if (include_summand::value) + for (size_t i = 0; i < length(sigma); i++) { + if (include_summand::value) { inv_sigma[i] = 1.0 / value_of(sigma_vec[i]); + } + } VectorBuilder::value, T_partials_return, T_y, T_shape, T_scale> y_div_sigma_pow_alpha(N); - for (size_t i = 0; i < N; i++) + for (size_t i = 0; i < N; i++) { if (include_summand::value) { const T_partials_return y_dbl = value_of(y_vec[i]); const T_partials_return alpha_dbl = value_of(alpha_vec[i]); y_div_sigma_pow_alpha[i] = pow(y_dbl * inv_sigma[i], alpha_dbl); } + } operands_and_partials ops_partials(y, alpha, sigma); for (size_t n = 0; n < N; n++) { const T_partials_return alpha_dbl = value_of(alpha_vec[n]); - if (include_summand::value) + if (include_summand::value) { logp += log_alpha[n]; - if (include_summand::value) + } + if (include_summand::value) { logp += (alpha_dbl - 1.0) * log_y[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= alpha_dbl * log_sigma[n]; - if (include_summand::value) + } + if (include_summand::value) { logp -= y_div_sigma_pow_alpha[n]; + } if (!is_constant_all::value) { const T_partials_return inv_y = 1.0 / value_of(y_vec[n]); @@ -113,13 +129,15 @@ return_type_t weibull_lpdf(const T_y& y, += (alpha_dbl - 1.0) * inv_y - alpha_dbl * y_div_sigma_pow_alpha[n] * inv_y; } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge2_.partials_[n] += 1.0 / alpha_dbl + (1.0 - y_div_sigma_pow_alpha[n]) * (log_y[n] - log_sigma[n]); - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[n] += alpha_dbl * inv_sigma[n] * (y_div_sigma_pow_alpha[n] - 1.0); + } } return ops_partials.build(logp); } diff --git a/stan/math/prim/scal/prob/wiener_lpdf.hpp b/stan/math/prim/scal/prob/wiener_lpdf.hpp index d94fbcdcb75..8e0b9313c1e 100644 --- a/stan/math/prim/scal/prob/wiener_lpdf.hpp +++ b/stan/math/prim/scal/prob/wiener_lpdf.hpp @@ -86,8 +86,9 @@ return_type_t wiener_lpdf( static const double SQUARE_PI_OVER_TWO = square(pi()) * 0.5; static const double TWO_TIMES_LOG_SQRT_PI = 2.0 * LOG_SQRT_PI; - if (size_zero(y, alpha, beta, tau, delta)) + if (size_zero(y, alpha, beta, tau, delta)) { return 0.0; + } typedef return_type_t T_return_type; T_return_type lp(0.0); @@ -110,8 +111,9 @@ return_type_t wiener_lpdf( "Drift rate", delta); size_t N = std::max(max_size(y, alpha, beta), max_size(tau, delta)); - if (!N) + if (!N) { return 0.0; + } scalar_seq_view y_vec(y); scalar_seq_view alpha_vec(alpha); @@ -130,8 +132,9 @@ return_type_t wiener_lpdf( } } - if (!include_summand::value) + if (!include_summand::value) { return 0; + } for (size_t i = 0; i < N; i++) { typename scalar_type::type one_minus_beta = 1.0 - beta_vec[i]; @@ -170,15 +173,17 @@ return_type_t wiener_lpdf( K = ceil(ks); // round to smallest integer meeting error T_return_type tmp_expr1 = (K - 1.0) / 2.0; T_return_type tmp_expr2 = ceil(tmp_expr1); - for (k = -floor(tmp_expr1); k <= tmp_expr2; k++) + for (k = -floor(tmp_expr1); k <= tmp_expr2; k++) { tmp += (one_minus_beta + 2.0 * k) * exp(-(square(one_minus_beta + 2.0 * k)) * 0.5 / x); + } tmp = log(tmp) - LOG_TWO_OVER_TWO_PLUS_LOG_SQRT_PI - 1.5 * log_x; } else { // if large t is better... K = ceil(kl); // round to smallest integer meeting error - for (k = 1; k <= K; ++k) + for (k = 1; k <= K; ++k) { tmp += k * exp(-(square(k)) * (SQUARE_PI_OVER_TWO * x)) * sin(k * pi() * one_minus_beta); + } tmp = log(tmp) + TWO_TIMES_LOG_SQRT_PI; } diff --git a/stan/math/rev/arr/fun/log_sum_exp.hpp b/stan/math/rev/arr/fun/log_sum_exp.hpp index 8dddf627a1b..f08114e07df 100644 --- a/stan/math/rev/arr/fun/log_sum_exp.hpp +++ b/stan/math/rev/arr/fun/log_sum_exp.hpp @@ -16,13 +16,17 @@ inline double log_sum_exp_as_double(const std::vector& x) { using std::log; using std::numeric_limits; double max = -numeric_limits::infinity(); - for (size_t i = 0; i < x.size(); ++i) - if (x[i] > max) + for (size_t i = 0; i < x.size(); ++i) { + if (x[i] > max) { max = x[i].val(); + } + } double sum = 0.0; - for (size_t i = 0; i < x.size(); ++i) - if (x[i] != -numeric_limits::infinity()) + for (size_t i = 0; i < x.size(); ++i) { + if (x[i] != -numeric_limits::infinity()) { sum += exp(x[i].val() - max); + } + } return max + log(sum); } diff --git a/stan/math/rev/arr/fun/sum.hpp b/stan/math/rev/arr/fun/sum.hpp index 03634c9ad69..cb621ea7ad1 100644 --- a/stan/math/rev/arr/fun/sum.hpp +++ b/stan/math/rev/arr/fun/sum.hpp @@ -18,8 +18,9 @@ class sum_v_vari : public vari { inline static double sum_of_val(const std::vector& v) { double result = 0; - for (auto x : v) + for (auto x : v) { result += x.val(); + } return result; } @@ -32,8 +33,9 @@ class sum_v_vari : public vari { v_(reinterpret_cast(ChainableStack::instance_->memalloc_.alloc( v1.size() * sizeof(vari*)))), length_(v1.size()) { - for (size_t i = 0; i < length_; i++) + for (size_t i = 0; i < length_; i++) { v_[i] = v1[i].vi_; + } } virtual void chain() { @@ -50,8 +52,9 @@ class sum_v_vari : public vari { * @return Sum of vector entries. */ inline var sum(const std::vector& m) { - if (m.empty()) + if (m.empty()) { return 0.0; + } return var(new sum_v_vari(m)); } diff --git a/stan/math/rev/arr/fun/to_var.hpp b/stan/math/rev/arr/fun/to_var.hpp index cb7a0cecf75..61ae3c80612 100644 --- a/stan/math/rev/arr/fun/to_var.hpp +++ b/stan/math/rev/arr/fun/to_var.hpp @@ -18,8 +18,9 @@ namespace math { */ inline std::vector to_var(const std::vector& v) { std::vector var_vector(v.size()); - for (size_t n = 0; n < v.size(); n++) + for (size_t n = 0; n < v.size(); n++) { var_vector[n] = v[n]; + } return var_vector; } diff --git a/stan/math/rev/arr/functor/coupled_ode_system.hpp b/stan/math/rev/arr/functor/coupled_ode_system.hpp index e689b81db97..f255c6dc659 100644 --- a/stan/math/rev/arr/functor/coupled_ode_system.hpp +++ b/stan/math/rev/arr/functor/coupled_ode_system.hpp @@ -93,8 +93,9 @@ struct coupled_ode_system { M_(theta.size()), size_(N_ + N_ * M_), msgs_(msgs) { - for (const var& p : theta) + for (const var& p : theta) { theta_nochain_.emplace_back(var(new vari(p.val(), false))); + } } /** @@ -135,8 +136,9 @@ struct coupled_ode_system { // dy1_dt, dy2_dt, dy1_da, dy2_da, dy1_db, dy2_db double temp_deriv = theta_nochain_[j].adj(); const size_t offset = N_ + N_ * j; - for (size_t k = 0; k < N_; k++) + for (size_t k = 0; k < N_; k++) { temp_deriv += z[offset + k] * y_vars[k].adj(); + } dz_dt[offset + i] = temp_deriv; } @@ -146,8 +148,9 @@ struct coupled_ode_system { // reset to zero by the last call. This is done as a separate step here. // See efficiency note above on template specalization for more details // on this. - for (size_t j = 0; j < M_; ++j) + for (size_t j = 0; j < M_; ++j) { theta_nochain_[j].vi_->set_zero_adjoint(); + } } } catch (const std::exception& e) { recover_memory_nested(); @@ -180,8 +183,9 @@ struct coupled_ode_system { */ std::vector initial_state() const { std::vector state(size_, 0.0); - for (size_t n = 0; n < N_; n++) + for (size_t n = 0; n < N_; n++) { state[n] = y0_dbl_[n]; + } return state; } }; @@ -290,8 +294,9 @@ struct coupled_ode_system { // dy2_d{y0_b} double temp_deriv = 0; const size_t offset = N_ + N_ * j; - for (size_t k = 0; k < N_; k++) + for (size_t k = 0; k < N_; k++) { temp_deriv += z[offset + k] * y_vars[k].adj(); + } dz_dt[offset + i] = temp_deriv; } @@ -327,10 +332,12 @@ struct coupled_ode_system { */ std::vector initial_state() const { std::vector initial(size_, 0.0); - for (size_t i = 0; i < N_; i++) + for (size_t i = 0; i < N_; i++) { initial[i] = value_of(y0_[i]); - for (size_t i = 0; i < N_; i++) + } + for (size_t i = 0; i < N_; i++) { initial[N_ + i * N_ + i] = 1.0; + } return initial; } }; @@ -421,8 +428,9 @@ struct coupled_ode_system { M_(theta.size()), size_(N_ + N_ * (N_ + M_)), msgs_(msgs) { - for (const var& p : theta) + for (const var& p : theta) { theta_nochain_.emplace_back(var(new vari(p.val(), false))); + } } /** @@ -463,8 +471,9 @@ struct coupled_ode_system { // dy1_dt, dy2_dt, dy1_da, dy2_da, dy1_db, dy2_db double temp_deriv = 0; const size_t offset = N_ + N_ * j; - for (size_t k = 0; k < N_; k++) + for (size_t k = 0; k < N_; k++) { temp_deriv += z[offset + k] * y_vars[k].adj(); + } dz_dt[offset + i] = temp_deriv; } @@ -472,8 +481,9 @@ struct coupled_ode_system { for (size_t j = 0; j < M_; j++) { double temp_deriv = theta_nochain_[j].adj(); const size_t offset = N_ + N_ * N_ + N_ * j; - for (size_t k = 0; k < N_; k++) + for (size_t k = 0; k < N_; k++) { temp_deriv += z[offset + k] * y_vars[k].adj(); + } dz_dt[offset + i] = temp_deriv; } @@ -483,8 +493,9 @@ struct coupled_ode_system { // reset to zero by the last call. This is done as a separate step here. // See efficiency note above on template specalization for more details // on this. - for (size_t j = 0; j < M_; ++j) + for (size_t j = 0; j < M_; ++j) { theta_nochain_[j].vi_->set_zero_adjoint(); + } } } catch (const std::exception& e) { recover_memory_nested(); @@ -520,10 +531,12 @@ struct coupled_ode_system { */ std::vector initial_state() const { std::vector initial(size_, 0.0); - for (size_t i = 0; i < N_; i++) + for (size_t i = 0; i < N_; i++) { initial[i] = value_of(y0_[i]); - for (size_t i = 0; i < N_; i++) + } + for (size_t i = 0; i < N_; i++) { initial[N_ + i * N_ + i] = 1.0; + } return initial; } }; diff --git a/stan/math/rev/arr/functor/integrate_1d.hpp b/stan/math/rev/arr/functor/integrate_1d.hpp index e4f8628dc24..7e330a33928 100644 --- a/stan/math/rev/arr/functor/integrate_1d.hpp +++ b/stan/math/rev/arr/functor/integrate_1d.hpp @@ -37,8 +37,9 @@ inline double gradient_of_f(const F &f, const double &x, const double &xc, start_nested(); std::vector theta_var(theta_vals.size()); try { - for (size_t i = 0; i < theta_vals.size(); i++) + for (size_t i = 0; i < theta_vals.size(); i++) { theta_var[i] = theta_vals[i]; + } var fx = f(x, xc, theta_var, x_r, x_i, &msgs); fx.grad(); gradient = theta_var[n].adj(); @@ -125,9 +126,10 @@ inline return_type_t integrate_1d( check_less_or_equal(function, "lower limit", a, b); if (value_of(a) == value_of(b)) { - if (is_inf(a)) + if (is_inf(a)) { domain_error(function, "Integration endpoints are both", value_of(a), "", ""); + } return var(0.0); } else { double integral = integrate( diff --git a/stan/math/rev/core/gevv_vvv_vari.hpp b/stan/math/rev/core/gevv_vvv_vari.hpp index bbd8195dc89..0505788ad3d 100644 --- a/stan/math/rev/core/gevv_vvv_vari.hpp +++ b/stan/math/rev/core/gevv_vvv_vari.hpp @@ -19,8 +19,9 @@ class gevv_vvv_vari : public vari { const var* v2, int stride2, size_t length, double* dotprod) { double result = 0; - for (size_t i = 0; i < length; i++) + for (size_t i = 0; i < length; i++) { result += v1[i * stride1].vi_->val_ * v2[i * stride2].vi_->val_; + } *dotprod = result; return alpha->vi_->val_ * result; } @@ -35,10 +36,12 @@ class gevv_vvv_vari : public vari { v1_ = reinterpret_cast(ChainableStack::instance_->memalloc_.alloc( 2 * length_ * sizeof(vari*))); v2_ = v1_ + length_; - for (size_t i = 0; i < length_; i++) + for (size_t i = 0; i < length_; i++) { v1_[i] = v1[i * stride1].vi_; - for (size_t i = 0; i < length_; i++) + } + for (size_t i = 0; i < length_; i++) { v2_[i] = v2[i * stride2].vi_; + } } virtual ~gevv_vvv_vari() {} void chain() { diff --git a/stan/math/rev/core/matrix_vari.hpp b/stan/math/rev/core/matrix_vari.hpp index ef745f71f27..830a6406da2 100644 --- a/stan/math/rev/core/matrix_vari.hpp +++ b/stan/math/rev/core/matrix_vari.hpp @@ -19,8 +19,9 @@ class op_matrix_vari : public vari { op_matrix_vari(double f, const Eigen::Matrix& vs) : vari(f), size_(vs.size()) { vis_ = reinterpret_cast(operator new(sizeof(vari*) * vs.size())); - for (int i = 0; i < vs.size(); ++i) + for (int i = 0; i < vs.size(); ++i) { vis_[i] = vs(i).vi_; + } } vari* operator[](size_t n) const { return vis_[n]; } size_t size() { return size_; } diff --git a/stan/math/rev/core/operator_addition.hpp b/stan/math/rev/core/operator_addition.hpp index 29d6cbf230a..b2223790462 100644 --- a/stan/math/rev/core/operator_addition.hpp +++ b/stan/math/rev/core/operator_addition.hpp @@ -30,10 +30,11 @@ class add_vd_vari : public op_vd_vari { public: add_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ + b, avi, b) {} void chain() { - if (unlikely(is_any_nan(avi_->val_, bd_))) + if (unlikely(is_any_nan(avi_->val_, bd_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_; + } } }; } // namespace internal @@ -92,8 +93,9 @@ inline var operator+(const var& a, const var& b) { * @return Result of adding variable and scalar. */ inline var operator+(const var& a, double b) { - if (b == 0.0) + if (b == 0.0) { return a; + } return var(new internal::add_vd_vari(a.vi_, b)); } @@ -109,8 +111,9 @@ inline var operator+(const var& a, double b) { * @return Result of adding variable and scalar. */ inline var operator+(double a, const var& b) { - if (a == 0.0) + if (a == 0.0) { return b; + } return var(new internal::add_vd_vari(b.vi_, a)); // by symmetry } diff --git a/stan/math/rev/core/operator_divide_equal.hpp b/stan/math/rev/core/operator_divide_equal.hpp index f6b43f05d69..7b4fb947b8b 100644 --- a/stan/math/rev/core/operator_divide_equal.hpp +++ b/stan/math/rev/core/operator_divide_equal.hpp @@ -13,8 +13,9 @@ inline var& var::operator/=(const var& b) { } inline var& var::operator/=(double b) { - if (b == 1.0) + if (b == 1.0) { return *this; + } vi_ = new internal::divide_vd_vari(vi_, b); return *this; } diff --git a/stan/math/rev/core/operator_division.hpp b/stan/math/rev/core/operator_division.hpp index 51a87bb55c1..0ae035f066b 100644 --- a/stan/math/rev/core/operator_division.hpp +++ b/stan/math/rev/core/operator_division.hpp @@ -32,10 +32,11 @@ class divide_vd_vari : public op_vd_vari { public: divide_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ / b, avi, b) {} void chain() { - if (unlikely(is_any_nan(avi_->val_, bd_))) + if (unlikely(is_any_nan(avi_->val_, bd_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_ / bd_; + } } }; @@ -100,8 +101,9 @@ inline var operator/(const var& a, const var& b) { * @return Variable result of dividing the variable by the scalar. */ inline var operator/(const var& a, double b) { - if (b == 1.0) + if (b == 1.0) { return a; + } return var(new internal::divide_vd_vari(a.vi_, b)); } diff --git a/stan/math/rev/core/operator_minus_equal.hpp b/stan/math/rev/core/operator_minus_equal.hpp index 28c48e43b90..375f211b622 100644 --- a/stan/math/rev/core/operator_minus_equal.hpp +++ b/stan/math/rev/core/operator_minus_equal.hpp @@ -13,8 +13,9 @@ inline var& var::operator-=(const var& b) { } inline var& var::operator-=(double b) { - if (b == 0.0) + if (b == 0.0) { return *this; + } vi_ = new internal::subtract_vd_vari(vi_, b); return *this; } diff --git a/stan/math/rev/core/operator_multiplication.hpp b/stan/math/rev/core/operator_multiplication.hpp index 32072bfd984..cdc85a8aaa8 100644 --- a/stan/math/rev/core/operator_multiplication.hpp +++ b/stan/math/rev/core/operator_multiplication.hpp @@ -30,10 +30,11 @@ class multiply_vd_vari : public op_vd_vari { public: multiply_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ * b, avi, b) {} void chain() { - if (unlikely(is_any_nan(avi_->val_, bd_))) + if (unlikely(is_any_nan(avi_->val_, bd_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_ * bd_; + } } }; } // namespace internal @@ -91,8 +92,9 @@ inline var operator*(const var& a, const var& b) { * @return Variable result of multiplying operands. */ inline var operator*(const var& a, double b) { - if (b == 1.0) + if (b == 1.0) { return a; + } return var(new internal::multiply_vd_vari(a.vi_, b)); } @@ -108,8 +110,9 @@ inline var operator*(const var& a, double b) { * @return Variable result of multiplying the operands. */ inline var operator*(double a, const var& b) { - if (a == 1.0) + if (a == 1.0) { return b; + } return var(new internal::multiply_vd_vari(b.vi_, a)); // by symmetry } diff --git a/stan/math/rev/core/operator_multiply_equal.hpp b/stan/math/rev/core/operator_multiply_equal.hpp index c98cad46029..2679de84426 100644 --- a/stan/math/rev/core/operator_multiply_equal.hpp +++ b/stan/math/rev/core/operator_multiply_equal.hpp @@ -13,8 +13,9 @@ inline var& var::operator*=(const var& b) { } inline var& var::operator*=(double b) { - if (b == 1.0) + if (b == 1.0) { return *this; + } vi_ = new internal::multiply_vd_vari(vi_, b); return *this; } diff --git a/stan/math/rev/core/operator_plus_equal.hpp b/stan/math/rev/core/operator_plus_equal.hpp index b48622c8bfc..09175e82ff8 100644 --- a/stan/math/rev/core/operator_plus_equal.hpp +++ b/stan/math/rev/core/operator_plus_equal.hpp @@ -13,8 +13,9 @@ inline var& var::operator+=(const var& b) { } inline var& var::operator+=(double b) { - if (b == 0.0) + if (b == 0.0) { return *this; + } vi_ = new internal::add_vd_vari(vi_, b); return *this; } diff --git a/stan/math/rev/core/operator_subtraction.hpp b/stan/math/rev/core/operator_subtraction.hpp index db6af90c319..ab19550f502 100644 --- a/stan/math/rev/core/operator_subtraction.hpp +++ b/stan/math/rev/core/operator_subtraction.hpp @@ -31,10 +31,11 @@ class subtract_vd_vari : public op_vd_vari { public: subtract_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ - b, avi, b) {} void chain() { - if (unlikely(is_any_nan(avi_->val_, bd_))) + if (unlikely(is_any_nan(avi_->val_, bd_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_; + } } }; @@ -42,10 +43,11 @@ class subtract_dv_vari : public op_dv_vari { public: subtract_dv_vari(double a, vari* bvi) : op_dv_vari(a - bvi->val_, a, bvi) {} void chain() { - if (unlikely(is_any_nan(ad_, bvi_->val_))) + if (unlikely(is_any_nan(ad_, bvi_->val_))) { bvi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { bvi_->adj_ -= adj_; + } } }; } // namespace internal @@ -104,8 +106,9 @@ inline var operator-(const var& a, const var& b) { * @return Result of subtracting the scalar from the variable. */ inline var operator-(const var& a, double b) { - if (b == 0.0) + if (b == 0.0) { return a; + } return var(new internal::subtract_vd_vari(a.vi_, b)); } diff --git a/stan/math/rev/core/operator_unary_decrement.hpp b/stan/math/rev/core/operator_unary_decrement.hpp index bacd516c569..a5c3b4ec62a 100644 --- a/stan/math/rev/core/operator_unary_decrement.hpp +++ b/stan/math/rev/core/operator_unary_decrement.hpp @@ -14,10 +14,11 @@ class decrement_vari : public op_v_vari { public: explicit decrement_vari(vari* avi) : op_v_vari(avi->val_ - 1.0, avi) {} void chain() { - if (unlikely(is_nan(avi_->val_))) + if (unlikely(is_nan(avi_->val_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_; + } } }; } // namespace internal diff --git a/stan/math/rev/core/operator_unary_increment.hpp b/stan/math/rev/core/operator_unary_increment.hpp index 2dc9d0e6c45..d2c065dfe30 100644 --- a/stan/math/rev/core/operator_unary_increment.hpp +++ b/stan/math/rev/core/operator_unary_increment.hpp @@ -14,10 +14,11 @@ class increment_vari : public op_v_vari { public: explicit increment_vari(vari* avi) : op_v_vari(avi->val_ + 1.0, avi) {} void chain() { - if (unlikely(is_nan(avi_->val_))) + if (unlikely(is_nan(avi_->val_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_; + } } }; } // namespace internal diff --git a/stan/math/rev/core/operator_unary_negative.hpp b/stan/math/rev/core/operator_unary_negative.hpp index e892a9fb54f..ed88deff87e 100644 --- a/stan/math/rev/core/operator_unary_negative.hpp +++ b/stan/math/rev/core/operator_unary_negative.hpp @@ -14,10 +14,11 @@ class neg_vari : public op_v_vari { public: explicit neg_vari(vari* avi) : op_v_vari(-(avi->val_), avi) {} void chain() { - if (unlikely(is_nan(avi_->val_))) + if (unlikely(is_nan(avi_->val_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ -= adj_; + } } }; } // namespace internal diff --git a/stan/math/rev/core/operator_unary_plus.hpp b/stan/math/rev/core/operator_unary_plus.hpp index a86b3b73a3f..46b4cb2eef8 100644 --- a/stan/math/rev/core/operator_unary_plus.hpp +++ b/stan/math/rev/core/operator_unary_plus.hpp @@ -41,8 +41,9 @@ namespace math { * @return The input reference. */ inline var operator+(const var& a) { - if (unlikely(is_nan(a.vi_->val_))) + if (unlikely(is_nan(a.vi_->val_))) { return var(new precomp_v_vari(NOT_A_NUMBER, a.vi_, NOT_A_NUMBER)); + } return a; } diff --git a/stan/math/rev/core/precomputed_gradients.hpp b/stan/math/rev/core/precomputed_gradients.hpp index e2c528ae674..c51b8a5ae65 100644 --- a/stan/math/rev/core/precomputed_gradients.hpp +++ b/stan/math/rev/core/precomputed_gradients.hpp @@ -58,8 +58,9 @@ class precomputed_gradients_vari : public vari { vars.size())) { check_consistent_sizes("precomputed_gradients_vari", "vars", vars, "gradients", gradients); - for (size_t i = 0; i < vars.size(); ++i) + for (size_t i = 0; i < vars.size(); ++i) { varis_[i] = vars[i].vi_; + } std::copy(gradients.begin(), gradients.end(), gradients_); } @@ -68,8 +69,9 @@ class precomputed_gradients_vari : public vari { * prestored operands and gradient. */ void chain() { - for (size_t i = 0; i < size_; ++i) + for (size_t i = 0; i < size_; ++i) { varis_[i]->adj_ += adj_ * gradients_[i]; + } } }; diff --git a/stan/math/rev/core/print_stack.hpp b/stan/math/rev/core/print_stack.hpp index 7cfd8fc2b98..7043b43b0d0 100644 --- a/stan/math/rev/core/print_stack.hpp +++ b/stan/math/rev/core/print_stack.hpp @@ -21,12 +21,13 @@ inline void print_stack(std::ostream& o) { o << "STACK, size=" << ChainableStack::instance_->var_stack_.size() << std::endl; // TODO(carpenter): this shouldn't need to be cast any more - for (size_t i = 0; i < ChainableStack::instance_->var_stack_.size(); ++i) + for (size_t i = 0; i < ChainableStack::instance_->var_stack_.size(); ++i) { o << i << " " << ChainableStack::instance_->var_stack_[i] << " " << (static_cast(ChainableStack::instance_->var_stack_[i]))->val_ << " : " << (static_cast(ChainableStack::instance_->var_stack_[i]))->adj_ << std::endl; + } } } // namespace math diff --git a/stan/math/rev/core/recover_memory.hpp b/stan/math/rev/core/recover_memory.hpp index 3cad48a1957..c8ec7e92b66 100644 --- a/stan/math/rev/core/recover_memory.hpp +++ b/stan/math/rev/core/recover_memory.hpp @@ -16,10 +16,11 @@ namespace math { * false */ static inline void recover_memory() { - if (!empty_nested()) + if (!empty_nested()) { throw std::logic_error( "empty_nested() must be true" " before calling recover_memory()"); + } ChainableStack::instance_->var_stack_.clear(); ChainableStack::instance_->var_nochain_stack_.clear(); for (auto &x : ChainableStack::instance_->var_alloc_stack_) { diff --git a/stan/math/rev/core/recover_memory_nested.hpp b/stan/math/rev/core/recover_memory_nested.hpp index b51fdb5d29e..c002f452e3f 100644 --- a/stan/math/rev/core/recover_memory_nested.hpp +++ b/stan/math/rev/core/recover_memory_nested.hpp @@ -18,10 +18,11 @@ namespace math { * true */ static inline void recover_memory_nested() { - if (empty_nested()) + if (empty_nested()) { throw std::logic_error( "empty_nested() must be false" " before calling recover_memory_nested()"); + } ChainableStack::instance_->var_stack_.resize( ChainableStack::instance_->nested_var_stack_sizes_.back()); diff --git a/stan/math/rev/core/set_zero_all_adjoints.hpp b/stan/math/rev/core/set_zero_all_adjoints.hpp index a59556e1403..841ef6d5c60 100644 --- a/stan/math/rev/core/set_zero_all_adjoints.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints.hpp @@ -12,10 +12,12 @@ namespace math { * Reset all adjoint values in the stack to zero. */ static void set_zero_all_adjoints() { - for (auto &x : ChainableStack::instance_->var_stack_) + for (auto &x : ChainableStack::instance_->var_stack_) { x->set_zero_adjoint(); - for (auto &x : ChainableStack::instance_->var_nochain_stack_) + } + for (auto &x : ChainableStack::instance_->var_nochain_stack_) { x->set_zero_adjoint(); + } } } // namespace math diff --git a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp index 017b010300a..d23bfe19fd7 100644 --- a/stan/math/rev/core/set_zero_all_adjoints_nested.hpp +++ b/stan/math/rev/core/set_zero_all_adjoints_nested.hpp @@ -15,15 +15,17 @@ namespace math { * to zero. */ static void set_zero_all_adjoints_nested() { - if (empty_nested()) + if (empty_nested()) { throw std::logic_error( "empty_nested() must be false before calling" " set_zero_all_adjoints_nested()"); + } size_t start1 = ChainableStack::instance_->nested_var_stack_sizes_.back(); // avoid wrap with unsigned when start1 == 0 for (size_t i = (start1 == 0U) ? 0U : (start1 - 1); - i < ChainableStack::instance_->var_stack_.size(); ++i) + i < ChainableStack::instance_->var_stack_.size(); ++i) { ChainableStack::instance_->var_stack_[i]->set_zero_adjoint(); + } size_t start2 = ChainableStack::instance_->nested_var_nochain_stack_sizes_.back(); diff --git a/stan/math/rev/core/stored_gradient_vari.hpp b/stan/math/rev/core/stored_gradient_vari.hpp index 902aa58a48d..24b94e5c727 100644 --- a/stan/math/rev/core/stored_gradient_vari.hpp +++ b/stan/math/rev/core/stored_gradient_vari.hpp @@ -39,8 +39,9 @@ class stored_gradient_vari : public vari { * derivatives given for the daughter vari by the stored partials. */ void chain() { - for (size_t i = 0; i < size_; ++i) + for (size_t i = 0; i < size_; ++i) { dtrs_[i]->adj_ += adj_ * partials_[i]; + } } }; diff --git a/stan/math/rev/core/var.hpp b/stan/math/rev/core/var.hpp index 6b5a2cd0710..16780202340 100644 --- a/stan/math/rev/core/var.hpp +++ b/stan/math/rev/core/var.hpp @@ -318,8 +318,9 @@ class var { void grad(std::vector& x, std::vector& g) { stan::math::grad(vi_); g.resize(x.size()); - for (size_t i = 0; i < x.size(); ++i) + for (size_t i = 0; i < x.size(); ++i) { g[i] = x[i].vi_->adj_; + } } /** @@ -470,8 +471,9 @@ class var { * @return Reference to the specified output stream. */ friend std::ostream& operator<<(std::ostream& os, const var& v) { - if (v.vi_ == nullptr) + if (v.vi_ == nullptr) { return os << "uninitialized"; + } return os << v.val(); } }; diff --git a/stan/math/rev/core/vari.hpp b/stan/math/rev/core/vari.hpp index c765a5814e6..ababd029097 100644 --- a/stan/math/rev/core/vari.hpp +++ b/stan/math/rev/core/vari.hpp @@ -60,10 +60,11 @@ class vari { } vari(double x, bool stacked) : val_(x), adj_(0.0) { - if (stacked) + if (stacked) { ChainableStack::instance_->var_stack_.push_back(this); - else + } else { ChainableStack::instance_->var_nochain_stack_.push_back(this); + } } /** diff --git a/stan/math/rev/core/vector_vari.hpp b/stan/math/rev/core/vector_vari.hpp index 62379f61efd..b085d4624e7 100644 --- a/stan/math/rev/core/vector_vari.hpp +++ b/stan/math/rev/core/vector_vari.hpp @@ -17,8 +17,9 @@ class op_vector_vari : public vari { op_vector_vari(double f, const std::vector& vs) : vari(f), size_(vs.size()) { vis_ = reinterpret_cast(operator new(sizeof(vari*) * vs.size())); - for (size_t i = 0; i < vs.size(); ++i) + for (size_t i = 0; i < vs.size(); ++i) { vis_[i] = vs[i].vi_; + } } vari* operator[](size_t n) const { return vis_[n]; } size_t size() { return size_; } diff --git a/stan/math/rev/mat/fun/cholesky_decompose.hpp b/stan/math/rev/mat/fun/cholesky_decompose.hpp index c7ce6d06b41..584c90d2d8a 100644 --- a/stan/math/rev/mat/fun/cholesky_decompose.hpp +++ b/stan/math/rev/mat/fun/cholesky_decompose.hpp @@ -40,8 +40,9 @@ inline void set_lower_tri_coeff_ref(Eigen::Matrix& L, for (size_type i = j; i < L.cols(); ++i) { L.coeffRef(i, j).vi_ = vari_ref[pos++]; } - for (size_type k = 0; k < j; ++k) + for (size_type k = 0; k < j; ++k) { L.coeffRef(k, j).vi_ = dummy; + } } return; } @@ -156,9 +157,11 @@ class cholesky_block : public vari { D_adj.triangularView().setZero(); } pos = 0; - for (size_type j = 0; j < M_; ++j) - for (size_type i = j; i < M_; ++i) + for (size_type j = 0; j < M_; ++j) { + for (size_type i = j; i < M_; ++i) { vari_ref_A_[pos++]->adj_ += L_adj.coeffRef(i, j); + } + } } }; @@ -416,8 +419,9 @@ inline Eigen::Matrix cholesky_decompose( size_t pos = j + accum_i; L.coeffRef(i, j).vi_ = baseVari->vari_ref_L_[pos]; } - for (size_type k = 0; k < j; ++k) + for (size_type k = 0; k < j; ++k) { L.coeffRef(k, j).vi_ = dummy; + } accum += j; accum_i = accum; } diff --git a/stan/math/rev/mat/fun/cov_exp_quad.hpp b/stan/math/rev/mat/fun/cov_exp_quad.hpp index 42c7edd9fb3..e0a4d5f14a8 100644 --- a/stan/math/rev/mat/fun/cov_exp_quad.hpp +++ b/stan/math/rev/mat/fun/cov_exp_quad.hpp @@ -62,8 +62,9 @@ class cov_exp_quad_vari : public vari { ++pos; } } - for (size_t i = 0; i < size_; ++i) + for (size_t i = 0; i < size_; ++i) { cov_diag_[i] = new vari(sigma_sq_d_, false); + } } virtual void chain() { @@ -129,8 +130,9 @@ class cov_exp_quad_vari : public vari { ++pos; } } - for (size_t i = 0; i < size_; ++i) + for (size_t i = 0; i < size_; ++i) { cov_diag_[i] = new vari(sigma_sq_d_, false); + } } virtual void chain() { diff --git a/stan/math/rev/mat/fun/dot_product.hpp b/stan/math/rev/mat/fun/dot_product.hpp index 9a34fcf6a5e..b297042718d 100644 --- a/stan/math/rev/mat/fun/dot_product.hpp +++ b/stan/math/rev/mat/fun/dot_product.hpp @@ -104,8 +104,9 @@ class dot_product_vari : public vari { if (shared == nullptr) { mem_d = reinterpret_cast( ChainableStack::instance_->memalloc_.alloc(length_ * sizeof(double))); - for (size_t i = 0; i < length_; i++) + for (size_t i = 0; i < length_; i++) { mem_d[i] = ind[i]; + } } else { mem_d = shared; } diff --git a/stan/math/rev/mat/fun/gp_exp_quad_cov.hpp b/stan/math/rev/mat/fun/gp_exp_quad_cov.hpp index b7a73c57e7e..68fc32b6cce 100644 --- a/stan/math/rev/mat/fun/gp_exp_quad_cov.hpp +++ b/stan/math/rev/mat/fun/gp_exp_quad_cov.hpp @@ -88,8 +88,9 @@ class gp_exp_quad_cov_vari : public vari { ++pos; } } - for (size_t i = 0; i < size_; ++i) + for (size_t i = 0; i < size_; ++i) { cov_diag_[i] = new vari(sigma_sq_d_, false); + } } virtual void chain() { @@ -180,8 +181,9 @@ class gp_exp_quad_cov_vari : public vari { ++pos; } } - for (size_t i = 0; i < size_; ++i) + for (size_t i = 0; i < size_; ++i) { cov_diag_[i] = new vari(sigma_sq_d_, false); + } } virtual void chain() { @@ -214,12 +216,14 @@ inline Eigen::Matrix gp_exp_quad_cov(const std::vector &x, check_positive("gp_exp_quad_cov", "sigma", sigma); check_positive("gp_exp_quad_cov", "length_scale", length_scale); size_t x_size = x.size(); - for (size_t i = 0; i < x_size; ++i) + for (size_t i = 0; i < x_size; ++i) { check_not_nan("gp_exp_quad_cov", "x", x[i]); + } Eigen::Matrix cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } gp_exp_quad_cov_vari *baseVari = new gp_exp_quad_cov_vari(x, sigma, length_scale); @@ -256,12 +260,14 @@ inline Eigen::Matrix gp_exp_quad_cov(const std::vector &x, check_positive("gp_exp_quad_cov", "marginal variance", sigma); check_positive("gp_exp_quad_cov", "length-scale", length_scale); size_t x_size = x.size(); - for (size_t i = 0; i < x_size; ++i) + for (size_t i = 0; i < x_size; ++i) { check_not_nan("gp_exp_quad_cov", "x", x[i]); + } Eigen::Matrix cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } gp_exp_quad_cov_vari *baseVari = new gp_exp_quad_cov_vari(x, sigma, length_scale); diff --git a/stan/math/rev/mat/fun/gp_periodic_cov.hpp b/stan/math/rev/mat/fun/gp_periodic_cov.hpp index e32d406c30b..79f139e5a86 100644 --- a/stan/math/rev/mat/fun/gp_periodic_cov.hpp +++ b/stan/math/rev/mat/fun/gp_periodic_cov.hpp @@ -308,12 +308,14 @@ gp_periodic_cov(const std::vector &x, const var &sigma, const var &l, check_positive(fun, "length-scale", l); check_positive(fun, "period", p); size_t x_size = x.size(); - for (size_t i = 0; i < x_size; ++i) + for (size_t i = 0; i < x_size; ++i) { check_not_nan(fun, "element of x", x[i]); + } Eigen::Matrix cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } gp_periodic_cov_vari *baseVari = new gp_periodic_cov_vari(x, sigma, l, p); @@ -360,12 +362,14 @@ gp_periodic_cov(const std::vector &x, double sigma, const var &l, check_positive(fun, "length-scale", l); check_positive(fun, "period", p); size_t x_size = x.size(); - for (size_t i = 0; i < x_size; ++i) + for (size_t i = 0; i < x_size; ++i) { check_not_nan(fun, "element of x", x[i]); + } Eigen::Matrix cov(x_size, x_size); - if (x_size == 0) + if (x_size == 0) { return cov; + } gp_periodic_cov_vari *baseVari = new gp_periodic_cov_vari(x, sigma, l, p); diff --git a/stan/math/rev/mat/fun/initialize_variable.hpp b/stan/math/rev/mat/fun/initialize_variable.hpp index ba769ea6c11..f2bfb300fbe 100644 --- a/stan/math/rev/mat/fun/initialize_variable.hpp +++ b/stan/math/rev/mat/fun/initialize_variable.hpp @@ -32,8 +32,9 @@ inline void initialize_variable(Eigen::Matrix& matrix, */ template inline void initialize_variable(std::vector& variables, const var& value) { - for (size_t i = 0; i < variables.size(); ++i) + for (size_t i = 0; i < variables.size(); ++i) { initialize_variable(variables[i], value); + } } } // namespace math diff --git a/stan/math/rev/mat/fun/log_softmax.hpp b/stan/math/rev/mat/fun/log_softmax.hpp index a5cefa06838..ec990c1d673 100644 --- a/stan/math/rev/mat/fun/log_softmax.hpp +++ b/stan/math/rev/mat/fun/log_softmax.hpp @@ -33,10 +33,11 @@ class log_softmax_elt_vari : public vari { idx_(idx) {} void chain() { for (int m = 0; m < size_; ++m) { - if (m == idx_) + if (m == idx_) { alpha_[m]->adj_ += adj_ * (1 - softmax_alpha_[m]); - else + } else { alpha_[m]->adj_ -= adj_ * softmax_alpha_[m]; + } } } }; @@ -82,10 +83,11 @@ inline Eigen::Matrix log_softmax( Eigen::Map(softmax_alpha_d_array, a_size) = softmax_alpha_d; vector_v log_softmax_alpha(a_size); - for (int k = 0; k < a_size; ++k) + for (int k = 0; k < a_size; ++k) { log_softmax_alpha(k) = var(new internal::log_softmax_elt_vari( log_softmax_alpha_d[k], alpha_vi_array, softmax_alpha_d_array, a_size, k)); + } return log_softmax_alpha; } diff --git a/stan/math/rev/mat/fun/log_sum_exp.hpp b/stan/math/rev/mat/fun/log_sum_exp.hpp index 444c472cf32..eaf3be28583 100644 --- a/stan/math/rev/mat/fun/log_sum_exp.hpp +++ b/stan/math/rev/mat/fun/log_sum_exp.hpp @@ -20,8 +20,9 @@ namespace internal { template inline double log_sum_exp_as_double(const Eigen::Matrix& x) { const double max = x.val().maxCoeff(); - if (!std::isfinite(max)) + if (!std::isfinite(max)) { return max; + } return max + std::log((x.val().array() - max).exp().sum()); } diff --git a/stan/math/rev/mat/fun/mdivide_left_tri.hpp b/stan/math/rev/mat/fun/mdivide_left_tri.hpp index ccb3da7362e..6bedf33a0af 100644 --- a/stan/math/rev/mat/fun/mdivide_left_tri.hpp +++ b/stan/math/rev/mat/fun/mdivide_left_tri.hpp @@ -48,13 +48,17 @@ class mdivide_left_tri_vv_vari : public vari { size_t pos = 0; if (TriView == Eigen::Lower) { - for (size_type j = 0; j < M_; j++) - for (size_type i = j; i < M_; i++) + for (size_type j = 0; j < M_; j++) { + for (size_type i = j; i < M_; i++) { variRefA_[pos++] = A(i, j).vi_; + } + } } else if (TriView == Eigen::Upper) { - for (size_type j = 0; j < M_; j++) - for (size_type i = 0; i < j + 1; i++) + for (size_type j = 0; j < M_; j++) { + for (size_type i = 0; i < j + 1; i++) { variRefA_[pos++] = A(i, j).vi_; + } + } } Map c_map(C_, M_, N_); @@ -106,13 +110,17 @@ class mdivide_left_tri_vv_vari : public vari { #endif size_t pos = 0; if (TriView == Eigen::Lower) { - for (size_type j = 0; j < adjA.cols(); j++) - for (size_type i = j; i < adjA.rows(); i++) + for (size_type j = 0; j < adjA.cols(); j++) { + for (size_type i = j; i < adjA.rows(); i++) { variRefA_[pos++]->adj_ += adjA(i, j); + } + } } else if (TriView == Eigen::Upper) { - for (size_type j = 0; j < adjA.cols(); j++) - for (size_type i = 0; i < j + 1; i++) + for (size_type j = 0; j < adjA.cols(); j++) { + for (size_type i = 0; i < j + 1; i++) { variRefA_[pos++]->adj_ += adjA(i, j); + } + } } Map(variRefB_, M_, N_).adj() += adjB; } @@ -225,13 +233,17 @@ class mdivide_left_tri_vd_vari : public vari { size_t pos = 0; if (TriView == Eigen::Lower) { - for (size_type j = 0; j < M_; j++) - for (size_type i = j; i < M_; i++) + for (size_type j = 0; j < M_; j++) { + for (size_type i = j; i < M_; i++) { variRefA_[pos++] = A(i, j).vi_; + } + } } else if (TriView == Eigen::Upper) { - for (size_type j = 0; j < M_; j++) - for (size_type i = 0; i < j + 1; i++) + for (size_type j = 0; j < M_; j++) { + for (size_type i = 0; i < j + 1; i++) { variRefA_[pos++] = A(i, j).vi_; + } + } } Map Ad(A_, M_, M_); Map Cd(C_, M_, N_); @@ -281,13 +293,17 @@ class mdivide_left_tri_vd_vari : public vari { #endif size_t pos = 0; if (TriView == Eigen::Lower) { - for (size_type j = 0; j < adjA.cols(); j++) - for (size_type i = j; i < adjA.rows(); i++) + for (size_type j = 0; j < adjA.cols(); j++) { + for (size_type i = j; i < adjA.rows(); i++) { variRefA_[pos++]->adj_ += adjA(i, j); + } + } } else if (TriView == Eigen::Upper) { - for (size_type j = 0; j < adjA.cols(); j++) - for (size_type i = 0; i < j + 1; i++) + for (size_type j = 0; j < adjA.cols(); j++) { + for (size_type i = 0; i < j + 1; i++) { variRefA_[pos++]->adj_ += adjA(i, j); + } + } } } }; diff --git a/stan/math/rev/mat/fun/multiply_lower_tri_self_transpose.hpp b/stan/math/rev/mat/fun/multiply_lower_tri_self_transpose.hpp index 7f32d816025..fcdac326326 100644 --- a/stan/math/rev/mat/fun/multiply_lower_tri_self_transpose.hpp +++ b/stan/math/rev/mat/fun/multiply_lower_tri_self_transpose.hpp @@ -17,24 +17,27 @@ inline matrix_v multiply_lower_tri_self_transpose(const matrix_v& L) { int K = L.rows(); int J = L.cols(); matrix_v LLt(K, K); - if (K == 0) + if (K == 0) { return LLt; + } // if (K == 1) { // LLt(0, 0) = L(0, 0) * L(0, 0); // return LLt; // } int Knz; - if (K >= J) + if (K >= J) { Knz = (K - J) * J + (J * (J + 1)) / 2; - else // if (K < J) + } else { // if (K < J) Knz = (K * (K + 1)) / 2; + } vari** vs = reinterpret_cast( ChainableStack::instance_->memalloc_.alloc(Knz * sizeof(vari*))); int pos = 0; - for (int m = 0; m < K; ++m) + for (int m = 0; m < K; ++m) { for (int n = 0; n < ((J < (m + 1)) ? J : (m + 1)); ++n) { vs[pos++] = L(m, n).vi_; } + } for (int m = 0, mpos = 0; m < K; ++m, mpos += (J < m) ? J : m) { LLt(m, m) = var( new internal::dot_self_vari(vs + mpos, (J < (m + 1)) ? J : (m + 1))); diff --git a/stan/math/rev/mat/fun/ordered_constrain.hpp b/stan/math/rev/mat/fun/ordered_constrain.hpp index d33c25ee610..21a1e9aadeb 100644 --- a/stan/math/rev/mat/fun/ordered_constrain.hpp +++ b/stan/math/rev/mat/fun/ordered_constrain.hpp @@ -33,8 +33,9 @@ class ordered_constrain_op { N_ = x.size(); Eigen::Matrix y(N_); - if (N_ == 0) + if (N_ == 0) { return y; + } exp_x_ = ChainableStack::instance_->memalloc_.alloc_array(N_ - 1); diff --git a/stan/math/rev/mat/fun/positive_ordered_constrain.hpp b/stan/math/rev/mat/fun/positive_ordered_constrain.hpp index 809ad5d8932..6f841ef2312 100644 --- a/stan/math/rev/mat/fun/positive_ordered_constrain.hpp +++ b/stan/math/rev/mat/fun/positive_ordered_constrain.hpp @@ -33,8 +33,9 @@ class positive_ordered_constrain_op { N_ = x.size(); Eigen::Matrix y(N_); - if (N_ == 0) + if (N_ == 0) { return y; + } exp_x_ = ChainableStack::instance_->memalloc_.alloc_array(N_); diff --git a/stan/math/rev/mat/fun/sd.hpp b/stan/math/rev/mat/fun/sd.hpp index 83b21eabfbd..e363384602f 100644 --- a/stan/math/rev/mat/fun/sd.hpp +++ b/stan/math/rev/mat/fun/sd.hpp @@ -55,8 +55,9 @@ inline var calc_sd(size_t size, const var* dtrs) { */ inline var sd(const std::vector& v) { check_nonzero_size("sd", "v", v); - if (v.size() == 1) + if (v.size() == 1) { return 0; + } return internal::calc_sd(v.size(), &v[0]); } @@ -73,8 +74,9 @@ inline var sd(const std::vector& v) { template var sd(const Eigen::Matrix& m) { check_nonzero_size("sd", "m", m); - if (m.size() == 1) + if (m.size() == 1) { return 0; + } return internal::calc_sd(m.size(), &m(0)); } diff --git a/stan/math/rev/mat/fun/softmax.hpp b/stan/math/rev/mat/fun/softmax.hpp index 34f3308583c..a9278f1e767 100644 --- a/stan/math/rev/mat/fun/softmax.hpp +++ b/stan/math/rev/mat/fun/softmax.hpp @@ -33,8 +33,9 @@ class softmax_op { y_ = ChainableStack::instance_->memalloc_.alloc_array(N_); auto y = softmax(alpha); - for (int n = 0; n < N_; ++n) + for (int n = 0; n < N_; ++n) { y_[n] = y(n); + } return y; } diff --git a/stan/math/rev/mat/fun/sum.hpp b/stan/math/rev/mat/fun/sum.hpp index e4d785032bf..4fa740e9005 100644 --- a/stan/math/rev/mat/fun/sum.hpp +++ b/stan/math/rev/mat/fun/sum.hpp @@ -44,8 +44,9 @@ class sum_eigen_v_vari : public sum_v_vari { */ template inline var sum(const Eigen::Matrix& m) { - if (m.size() == 0) + if (m.size() == 0) { return 0.0; + } return var(new sum_eigen_v_vari(m)); } diff --git a/stan/math/rev/mat/fun/tcrossprod.hpp b/stan/math/rev/mat/fun/tcrossprod.hpp index 29d0f8f0785..320ab2b7168 100644 --- a/stan/math/rev/mat/fun/tcrossprod.hpp +++ b/stan/math/rev/mat/fun/tcrossprod.hpp @@ -19,8 +19,9 @@ namespace math { * @return M times its transpose. */ inline matrix_v tcrossprod(const matrix_v& M) { - if (M.rows() == 0) + if (M.rows() == 0) { return matrix_v(0, 0); + } // if (M.rows() == 1) // return M * M.transpose(); @@ -34,11 +35,14 @@ inline matrix_v tcrossprod(const matrix_v& M) { = reinterpret_cast(ChainableStack::instance_->memalloc_.alloc( (M.rows() * M.cols()) * sizeof(vari*))); int pos = 0; - for (int m = 0; m < M.rows(); ++m) - for (int n = 0; n < M.cols(); ++n) + for (int m = 0; m < M.rows(); ++m) { + for (int n = 0; n < M.cols(); ++n) { vs[pos++] = M(m, n).vi_; - for (int m = 0; m < M.rows(); ++m) + } + } + for (int m = 0; m < M.rows(); ++m) { MMt(m, m) = var(new internal::dot_self_vari(vs + m * M.cols(), M.cols())); + } for (int m = 0; m < M.rows(); ++m) { for (int n = 0; n < m; ++n) { MMt(m, n) = var(new internal::dot_product_vari( diff --git a/stan/math/rev/mat/fun/trace_gen_quad_form.hpp b/stan/math/rev/mat/fun/trace_gen_quad_form.hpp index 742c2202341..1526f220bf9 100644 --- a/stan/math/rev/mat/fun/trace_gen_quad_form.hpp +++ b/stan/math/rev/mat/fun/trace_gen_quad_form.hpp @@ -47,17 +47,22 @@ class trace_gen_quad_form_vari : public vari { Eigen::Matrix* varB) { Eigen::Matrix AtB; Eigen::Matrix BD; - if (varB || varA) + if (varB || varA) { BD.noalias() = B * D; - if (varB || varD) + } + if (varB || varD) { AtB.noalias() = A.transpose() * B; + } - if (varB) + if (varB) { (*varB).adj() += adj * (A * BD + AtB * D.transpose()); - if (varA) + } + if (varA) { (*varA).adj() += adj * (B * BD.transpose()); - if (varD) + } + if (varD) { (*varD).adj() += adj * (B.transpose() * AtB); + } } public: diff --git a/stan/math/rev/mat/fun/trace_inv_quad_form_ldlt.hpp b/stan/math/rev/mat/fun/trace_inv_quad_form_ldlt.hpp index 304292f412d..8e88861b36b 100644 --- a/stan/math/rev/mat/fun/trace_inv_quad_form_ldlt.hpp +++ b/stan/math/rev/mat/fun/trace_inv_quad_form_ldlt.hpp @@ -22,17 +22,19 @@ class trace_inv_quad_form_ldlt_impl : public chainable_alloc { matrix_d Bd = B.val(); variB_ = B.vi(); AinvB_ = ldlt_.solve(Bd); - if (haveD) + if (haveD) { C_.noalias() = Bd.transpose() * AinvB_; - else + } else { value_ = (Bd.transpose() * AinvB_).trace(); + } } inline void initializeB(const Eigen::Matrix &B, bool haveD) { AinvB_ = ldlt_.solve(B); - if (haveD) + if (haveD) { C_.noalias() = B.transpose() * AinvB_; - else + } else { value_ = (B.transpose() * AinvB_).trace(); + } } template @@ -88,12 +90,13 @@ class trace_inv_quad_form_ldlt_vari : public vari { trace_inv_quad_form_ldlt_impl *impl) { Eigen::Matrix aA; - if (impl->Dtype_ != 2) + if (impl->Dtype_ != 2) { aA.noalias() = -adj * (impl->AinvB_ * impl->D_.transpose() * impl->AinvB_.transpose()); - else + } else { aA.noalias() = -adj * (impl->AinvB_ * impl->AinvB_.transpose()); + } impl->ldlt_.alloc_->variA_.adj() += aA; } @@ -102,10 +105,11 @@ class trace_inv_quad_form_ldlt_vari : public vari { trace_inv_quad_form_ldlt_impl *impl) { matrix_d aB; - if (impl->Dtype_ != 2) + if (impl->Dtype_ != 2) { aB.noalias() = adj * impl->AinvB_ * (impl->D_ + impl->D_.transpose()); - else + } else { aB.noalias() = 2 * adj * impl->AinvB_; + } impl->variB_.adj() += aB; } @@ -124,8 +128,9 @@ class trace_inv_quad_form_ldlt_vari : public vari { chainB(adj_, impl_); - if (impl_->Dtype_ == 1) + if (impl_->Dtype_ == 1) { impl_->variD_.adj() += adj_ * impl_->C_; + } } trace_inv_quad_form_ldlt_impl *impl_; diff --git a/stan/math/rev/mat/fun/variance.hpp b/stan/math/rev/mat/fun/variance.hpp index 79956db9f2c..c99c2fef81e 100644 --- a/stan/math/rev/mat/fun/variance.hpp +++ b/stan/math/rev/mat/fun/variance.hpp @@ -41,8 +41,9 @@ inline var calc_variance(size_t size, const var* dtrs) { */ inline var variance(const std::vector& v) { check_nonzero_size("variance", "v", v); - if (v.size() == 1) + if (v.size() == 1) { return 0; + } return internal::calc_variance(v.size(), &v[0]); } @@ -59,8 +60,9 @@ inline var variance(const std::vector& v) { template var variance(const Eigen::Matrix& m) { check_nonzero_size("variance", "m", m); - if (m.size() == 1) + if (m.size() == 1) { return 0; + } return internal::calc_variance(m.size(), &m(0)); } diff --git a/stan/math/rev/mat/functor/adj_jac_apply.hpp b/stan/math/rev/mat/functor/adj_jac_apply.hpp index 679588ac790..b736130c1db 100644 --- a/stan/math/rev/mat/functor/adj_jac_apply.hpp +++ b/stan/math/rev/mat/functor/adj_jac_apply.hpp @@ -73,8 +73,9 @@ template void build_y_adj(vari** y_vi, const std::array& M, std::vector& y_adj) { y_adj.resize(M[0]); - for (size_t m = 0; m < y_adj.size(); ++m) + for (size_t m = 0; m < y_adj.size(); ++m) { y_adj[m] = y_vi[m]->adj_; + } } /** @@ -89,8 +90,9 @@ template void build_y_adj(vari** y_vi, const std::array& M, Eigen::Matrix& y_adj) { y_adj.resize(M[0], M[1]); - for (int m = 0; m < y_adj.size(); ++m) + for (int m = 0; m < y_adj.size(); ++m) { y_adj(m) = y_vi[m]->adj_; + } } /** @@ -455,8 +457,9 @@ struct adj_jac_vari : public vari { const Pargs&... args) { static constexpr int t = sizeof...(Targs) - sizeof...(Pargs) - 1; if (is_var_[t]) { - for (size_t n = 0; n < y_adj_jac.size(); ++n) + for (size_t n = 0; n < y_adj_jac.size(); ++n) { x_vis_[offsets_[t] + n]->adj_ += y_adj_jac[n]; + } } accumulate_adjoints(args...); diff --git a/stan/math/rev/mat/functor/algebra_solver.hpp b/stan/math/rev/mat/functor/algebra_solver.hpp index f5ffb6c772c..191523afaac 100644 --- a/stan/math/rev/mat/functor/algebra_solver.hpp +++ b/stan/math/rev/mat/functor/algebra_solver.hpp @@ -53,12 +53,14 @@ struct algebra_solver_vari : public vari { x_size_ * y_size_)) { using Eigen::Map; using Eigen::MatrixXd; - for (int i = 0; i < y.size(); ++i) + for (int i = 0; i < y.size(); ++i) { y_[i] = y(i).vi_; + } theta_[0] = this; - for (int i = 1; i < x.size(); ++i) + for (int i = 1; i < x.size(); ++i) { theta_[i] = new vari(theta_dbl(i), false); + } // Compute the Jacobian and store in array, using the // implicit function theorem, i.e. Jx_y = Jf_y / Jf_x @@ -70,9 +72,11 @@ struct algebra_solver_vari : public vari { } void chain() { - for (int j = 0; j < y_size_; j++) - for (int i = 0; i < x_size_; i++) + for (int j = 0; j < y_size_; j++) { + for (int i = 0; i < x_size_; i++) { y_[j]->adj_ += theta_[i]->adj_ * Jx_y_[j * x_size_ + i]; + } + } } }; @@ -128,26 +132,33 @@ Eigen::VectorXd algebra_solver( double relative_tolerance = 1e-10, double function_tolerance = 1e-6, long int max_num_steps = 1e+3) { // NOLINT(runtime/int) check_nonzero_size("algebra_solver", "initial guess", x); - for (int i = 0; i < x.size(); i++) + for (int i = 0; i < x.size(); i++) { check_finite("algebra_solver", "initial guess", x(i)); - for (int i = 0; i < y.size(); i++) + } + for (int i = 0; i < y.size(); i++) { check_finite("algebra_solver", "parameter vector", y(i)); - for (double i : dat) + } + for (double i : dat) { check_finite("algebra_solver", "continuous data", i); - for (int x : dat_int) + } + for (int x : dat_int) { check_finite("algebra_solver", "integer data", x); + } - if (relative_tolerance < 0) + if (relative_tolerance < 0) { invalid_argument("algebra_solver", "relative_tolerance,", relative_tolerance, "", ", must be greater than or equal to 0"); - if (function_tolerance < 0) + } + if (function_tolerance < 0) { invalid_argument("algebra_solver", "function_tolerance,", function_tolerance, "", ", must be greater than or equal to 0"); - if (max_num_steps <= 0) + } + if (max_num_steps <= 0) { invalid_argument("algebra_solver", "max_num_steps,", max_num_steps, "", ", must be greater than 0"); + } // Create functor for algebraic system typedef system_functor Fs; @@ -259,8 +270,9 @@ Eigen::Matrix algebra_solver( dat_int, theta_dbl, fx, msgs); Eigen::Matrix theta(x.size()); theta(0) = var(vi0->theta_[0]); - for (int i = 1; i < x.size(); ++i) + for (int i = 1; i < x.size(); ++i) { theta(i) = var(vi0->theta_[i]); + } return theta; } diff --git a/stan/math/rev/mat/functor/algebra_system.hpp b/stan/math/rev/mat/functor/algebra_system.hpp index 1e49828a878..e1f527f8fd9 100644 --- a/stan/math/rev/mat/functor/algebra_system.hpp +++ b/stan/math/rev/mat/functor/algebra_system.hpp @@ -55,10 +55,11 @@ struct system_functor { template inline Eigen::Matrix operator()( const Eigen::Matrix& iv) const { - if (x_is_iv) + if (x_is_iv) { return f_(iv, y_, dat_, dat_int_, msgs_); - else + } else { return f_(x_, iv, dat_, dat_int_, msgs_); + } } }; diff --git a/stan/math/rev/mat/functor/cvodes_integrator.hpp b/stan/math/rev/mat/functor/cvodes_integrator.hpp index e4d1e3e367c..b283c980860 100644 --- a/stan/math/rev/mat/functor/cvodes_integrator.hpp +++ b/stan/math/rev/mat/functor/cvodes_integrator.hpp @@ -97,15 +97,18 @@ class cvodes_integrator { check_nonzero_size(fun, "initial state", y0); check_ordered(fun, "times", ts_dbl); check_less(fun, "initial time", t0_dbl, ts_dbl[0]); - if (relative_tolerance <= 0) + if (relative_tolerance <= 0) { invalid_argument("integrate_ode_cvodes", "relative_tolerance,", relative_tolerance, "", ", must be greater than 0"); - if (absolute_tolerance <= 0) + } + if (absolute_tolerance <= 0) { invalid_argument("integrate_ode_cvodes", "absolute_tolerance,", absolute_tolerance, "", ", must be greater than 0"); - if (max_num_steps <= 0) + } + if (max_num_steps <= 0) { invalid_argument("integrate_ode_cvodes", "max_num_steps,", max_num_steps, "", ", must be greater than 0"); + } const size_t N = y0.size(); const size_t M = theta.size(); @@ -115,8 +118,9 @@ class cvodes_integrator { ode_data cvodes_data(f, y0, theta, x, x_int, msgs); void* cvodes_mem = CVodeCreate(Lmm); - if (cvodes_mem == nullptr) + if (cvodes_mem == nullptr) { throw std::runtime_error("CVodeCreate failed to allocate memory"); + } const size_t coupled_size = cvodes_data.coupled_ode_.size(); @@ -164,10 +168,11 @@ class cvodes_integrator { double t_init = t0_dbl; for (size_t n = 0; n < ts.size(); ++n) { double t_final = ts_dbl[n]; - if (t_final != t_init) + if (t_final != t_init) { cvodes_check_flag(CVode(cvodes_mem, t_final, cvodes_data.nv_state_, &t_init, CV_NORMAL), "CVode"); + } if (S > 0) { cvodes_check_flag( CVodeGetSens(cvodes_mem, &t_init, cvodes_data.nv_state_sens_), diff --git a/stan/math/rev/mat/functor/cvodes_ode_data.hpp b/stan/math/rev/mat/functor/cvodes_ode_data.hpp index 1340680d4f8..173dabb1c9b 100644 --- a/stan/math/rev/mat/functor/cvodes_ode_data.hpp +++ b/stan/math/rev/mat/functor/cvodes_ode_data.hpp @@ -102,8 +102,9 @@ class cvodes_ode_data { SUNLinSolFree(LS_); SUNMatDestroy(A_); N_VDestroy_Serial(nv_state_); - if (S_ > 0) + if (S_ > 0) { N_VDestroyVectorArray_Serial(nv_state_sens_, S_); + } } /** @@ -185,13 +186,15 @@ class cvodes_ode_data { std::vector z(coupled_state_.size()); std::vector&& dz_dt = std::vector(coupled_state_.size()); std::copy(y, y + N_, z.begin()); - for (std::size_t s = 0; s < S_; s++) + for (std::size_t s = 0; s < S_; s++) { std::copy(NV_DATA_S(yS[s]), NV_DATA_S(yS[s]) + N_, z.begin() + (s + 1) * N_); + } coupled_ode_(z, dz_dt, t); - for (std::size_t s = 0; s < S_; s++) + for (std::size_t s = 0; s < S_; s++) { std::move(dz_dt.begin() + (s + 1) * N_, dz_dt.begin() + (s + 2) * N_, NV_DATA_S(ySdot[s])); + } } }; diff --git a/stan/math/rev/mat/functor/idas_integrator.hpp b/stan/math/rev/mat/functor/idas_integrator.hpp index d45d85b9322..6038d13dac4 100644 --- a/stan/math/rev/mat/functor/idas_integrator.hpp +++ b/stan/math/rev/mat/functor/idas_integrator.hpp @@ -81,18 +81,22 @@ class idas_integrator { idas_integrator(const double rtol, const double atol, const int64_t max_num_steps = IDAS_MAX_STEPS) : rtol_(rtol), atol_(atol), max_num_steps_(max_num_steps) { - if (rtol_ <= 0) + if (rtol_ <= 0) { invalid_argument("idas_integrator", "relative tolerance,", rtol_, "", ", must be greater than 0"); - if (rtol_ > 1.0E-3) + } + if (rtol_ > 1.0E-3) { invalid_argument("idas_integrator", "relative tolerance,", rtol_, "", ", must be less than 1.0E-3"); - if (atol_ <= 0) + } + if (atol_ <= 0) { invalid_argument("idas_integrator", "absolute tolerance,", atol_, "", ", must be greater than 0"); - if (max_num_steps_ <= 0) + } + if (max_num_steps_ <= 0) { invalid_argument("idas_integrator", "max_num_steps,", max_num_steps_, "", ", must be greater than 0"); + } } /** @@ -177,12 +181,14 @@ void idas_integrator::init_sensitivity(Dae& dae) { auto n = dae.n(); if (Dae::is_var_yy0) { - for (size_t i = 0; i < n; ++i) + for (size_t i = 0; i < n; ++i) { NV_Ith_S(yys[i], i) = 1.0; + } } if (Dae::is_var_yp0) { - for (size_t i = 0; i < n; ++i) + for (size_t i = 0; i < n; ++i) { NV_Ith_S(yps[i + n], i) = 1.0; + } } CHECK_IDAS_CALL(IDASensInit(mem, dae.ns(), IDA_SIMULTANEOUS, dae.sensitivity_residual(), yys, yps)); diff --git a/stan/math/rev/mat/functor/idas_system.hpp b/stan/math/rev/mat/functor/idas_system.hpp index 6fcb2d62cb2..1d1f5a6f8aa 100644 --- a/stan/math/rev/mat/functor/idas_system.hpp +++ b/stan/math/rev/mat/functor/idas_system.hpp @@ -153,11 +153,13 @@ class idas_system { id_(N_VNew_Serial(N_)), mem_(IDACreate()), msgs_(msgs) { - if (nv_yy_ == NULL || nv_yp_ == NULL) + if (nv_yy_ == NULL || nv_yp_ == NULL) { throw std::runtime_error("N_VMake_Serial failed to allocate memory"); + } - if (mem_ == NULL) + if (mem_ == NULL) { throw std::runtime_error("IDACreate failed to allocate memory"); + } static const char* caller = "idas_system"; check_finite(caller, "initial state", yy0); @@ -173,8 +175,9 @@ class idas_system { check_greater_or_equal(caller, "derivative-algebra id", eq_id, 0); check_less_or_equal(caller, "derivative-algebra id", eq_id, 1); - for (size_t i = 0; i < N_; ++i) + for (size_t i = 0; i < N_; ++i) { NV_Ith_S(id_, i) = eq_id[i]; + } } /** @@ -320,8 +323,9 @@ class idas_system { std::vector yp_vec(yp_val, yp_val + N); auto res = dae->f_(t, yy_vec, yp_vec, dae->theta_, dae->x_r_, dae->x_i_, dae->msgs_); - for (size_t i = 0; i < N; ++i) + for (size_t i = 0; i < N; ++i) { NV_Ith_S(rr, i) = value_of(res[i]); + } return 0; }; diff --git a/stan/math/rev/mat/functor/map_rect_concurrent.hpp b/stan/math/rev/mat/functor/map_rect_concurrent.hpp index e530f474d0e..86a433f070d 100644 --- a/stan/math/rev/mat/functor/map_rect_concurrent.hpp +++ b/stan/math/rev/mat/functor/map_rect_concurrent.hpp @@ -40,9 +40,10 @@ map_rect_concurrent( const int end = start + size; std::vector chunk_f_out; chunk_f_out.reserve(size); - for (int i = start; i != end; i++) + for (int i = start; i != end; i++) { chunk_f_out.push_back(ReduceF()( shared_params_dbl, value_of(job_params[i]), x_r[i], x_i[i], msgs)); + } return chunk_f_out; }; @@ -73,17 +74,19 @@ map_rect_concurrent( int offset = 0; for (std::size_t i = 0; i < futures.size(); ++i) { const std::vector& chunk_result = futures[i].get(); - if (i == 0) + if (i == 0) { world_output.resize(chunk_result[0].rows(), num_jobs * chunk_result[0].cols()); + } for (const auto& job_result : chunk_result) { const int num_job_outputs = job_result.cols(); world_f_out.push_back(num_job_outputs); - if (world_output.cols() < offset + num_job_outputs) + if (world_output.cols() < offset + num_job_outputs) { world_output.conservativeResize(Eigen::NoChange, 2 * (offset + num_job_outputs)); + } world_output.block(0, offset, world_output.rows(), num_job_outputs) = job_result; diff --git a/stan/math/rev/mat/functor/map_rect_reduce.hpp b/stan/math/rev/mat/functor/map_rect_reduce.hpp index 7613f1e4b30..36b661a690a 100644 --- a/stan/math/rev/mat/functor/map_rect_reduce.hpp +++ b/stan/math/rev/mat/functor/map_rect_reduce.hpp @@ -41,11 +41,13 @@ struct map_rect_reduce { out(0, i) = fx_v(i).val(); set_zero_all_adjoints_nested(); fx_v(i).grad(); - for (size_type j = 0; j < num_shared_params; ++j) + for (size_type j = 0; j < num_shared_params; ++j) { out(1 + j, i) = shared_params_v(j).vi_->adj_; - for (size_type j = 0; j < num_job_specific_params; ++j) + } + for (size_type j = 0; j < num_job_specific_params; ++j) { out(1 + num_shared_params + j, i) = job_specific_params_v(j).vi_->adj_; + } } recover_memory_nested(); } catch (const std::exception& e) { @@ -80,8 +82,9 @@ struct map_rect_reduce { out(0, i) = fx_v(i).val(); set_zero_all_adjoints_nested(); fx_v(i).grad(); - for (size_type j = 0; j < num_job_specific_params; ++j) + for (size_type j = 0; j < num_job_specific_params; ++j) { out(1 + j, i) = job_specific_params_v(j).vi_->adj_; + } } recover_memory_nested(); } catch (const std::exception& e) { @@ -116,8 +119,9 @@ struct map_rect_reduce { out(0, i) = fx_v(i).val(); set_zero_all_adjoints_nested(); fx_v(i).grad(); - for (size_type j = 0; j < num_shared_params; ++j) + for (size_type j = 0; j < num_shared_params; ++j) { out(1 + j, i) = shared_params_v(j).vi_->adj_; + } } recover_memory_nested(); } catch (const std::exception& e) { diff --git a/stan/math/rev/mat/meta/operands_and_partials.hpp b/stan/math/rev/mat/meta/operands_and_partials.hpp index 7095668fda9..71cc06a3d20 100644 --- a/stan/math/rev/mat/meta/operands_and_partials.hpp +++ b/stan/math/rev/mat/meta/operands_and_partials.hpp @@ -109,8 +109,9 @@ class ops_partials_edge > > { } } int size() { - if (unlikely(this->operands_.size() == 0)) + if (unlikely(this->operands_.size() == 0)) { return 0; + } return this->operands_.size() * this->operands_[0].size(); } }; @@ -150,8 +151,9 @@ class ops_partials_edge > > { } } int size() { - if (unlikely(this->operands_.size() == 0)) + if (unlikely(this->operands_.size() == 0)) { return 0; + } return this->operands_.size() * this->operands_[0].size(); } }; diff --git a/stan/math/rev/scal/fun/binary_log_loss.hpp b/stan/math/rev/scal/fun/binary_log_loss.hpp index 78e1343e9f2..2669b1aa68c 100644 --- a/stan/math/rev/scal/fun/binary_log_loss.hpp +++ b/stan/math/rev/scal/fun/binary_log_loss.hpp @@ -59,10 +59,11 @@ class binary_log_loss_0_vari : public op_v_vari { * @return Log loss of response versus reference value. */ inline var binary_log_loss(int y, const var& y_hat) { - if (y == 0) + if (y == 0) { return var(new internal::binary_log_loss_0_vari(y_hat.vi_)); - else + } else { return var(new internal::binary_log_loss_1_vari(y_hat.vi_)); + } } } // namespace math diff --git a/stan/math/rev/scal/fun/ceil.hpp b/stan/math/rev/scal/fun/ceil.hpp index 04af32e9280..7996170d6e6 100644 --- a/stan/math/rev/scal/fun/ceil.hpp +++ b/stan/math/rev/scal/fun/ceil.hpp @@ -15,8 +15,9 @@ class ceil_vari : public op_v_vari { public: explicit ceil_vari(vari* avi) : op_v_vari(std::ceil(avi->val_), avi) {} void chain() { - if (unlikely(is_nan(avi_->val_))) + if (unlikely(is_nan(avi_->val_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); + } } }; } // namespace internal diff --git a/stan/math/rev/scal/fun/fabs.hpp b/stan/math/rev/scal/fun/fabs.hpp index 835b720cfc9..f038837283b 100644 --- a/stan/math/rev/scal/fun/fabs.hpp +++ b/stan/math/rev/scal/fun/fabs.hpp @@ -48,14 +48,15 @@ namespace math { * @return Absolute value of variable. */ inline var fabs(const var& a) { - if (a.val() > 0.0) + if (a.val() > 0.0) { return a; - else if (a.val() < 0.0) + } else if (a.val() < 0.0) { return var(new internal::neg_vari(a.vi_)); - else if (a.val() == 0) + } else if (a.val() == 0) { return var(new vari(0)); - else + } else { return var(new precomp_v_vari(NOT_A_NUMBER, a.vi_, NOT_A_NUMBER)); + } } } // namespace math diff --git a/stan/math/rev/scal/fun/fdim.hpp b/stan/math/rev/scal/fun/fdim.hpp index 71da95675b6..726ee0c98f0 100644 --- a/stan/math/rev/scal/fun/fdim.hpp +++ b/stan/math/rev/scal/fun/fdim.hpp @@ -29,10 +29,11 @@ class fdim_vd_vari : public op_vd_vari { public: fdim_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ - b, avi, b) {} void chain() { - if (unlikely(is_any_nan(avi_->val_, bd_))) + if (unlikely(is_any_nan(avi_->val_, bd_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_; + } } }; @@ -40,10 +41,11 @@ class fdim_dv_vari : public op_dv_vari { public: fdim_dv_vari(double a, vari* bvi) : op_dv_vari(a - bvi->val_, a, bvi) {} void chain() { - if (unlikely(is_any_nan(bvi_->val_, ad_))) + if (unlikely(is_any_nan(bvi_->val_, ad_))) { bvi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { bvi_->adj_ -= adj_; + } } }; } // namespace internal diff --git a/stan/math/rev/scal/fun/floor.hpp b/stan/math/rev/scal/fun/floor.hpp index 84e0f0e5211..7514d441114 100644 --- a/stan/math/rev/scal/fun/floor.hpp +++ b/stan/math/rev/scal/fun/floor.hpp @@ -15,8 +15,9 @@ class floor_vari : public op_v_vari { public: explicit floor_vari(vari* avi) : op_v_vari(std::floor(avi->val_), avi) {} void chain() { - if (unlikely(is_nan(avi_->val_))) + if (unlikely(is_nan(avi_->val_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); + } } }; } // namespace internal diff --git a/stan/math/rev/scal/fun/fma.hpp b/stan/math/rev/scal/fun/fma.hpp index a2435264e34..1880b35c6b3 100644 --- a/stan/math/rev/scal/fun/fma.hpp +++ b/stan/math/rev/scal/fun/fma.hpp @@ -63,10 +63,11 @@ class fma_vdd_vari : public op_vdd_vari { fma_vdd_vari(vari* avi, double b, double c) : op_vdd_vari(fma(avi->val_, b, c), avi, b, c) {} void chain() { - if (unlikely(is_any_nan(avi_->val_, bd_, cd_))) + if (unlikely(is_any_nan(avi_->val_, bd_, cd_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_ * bd_; + } } }; @@ -75,10 +76,11 @@ class fma_ddv_vari : public op_ddv_vari { fma_ddv_vari(double a, double b, vari* cvi) : op_ddv_vari(fma(a, b, cvi->val_), a, b, cvi) {} void chain() { - if (unlikely(is_any_nan(cvi_->val_, ad_, bd_))) + if (unlikely(is_any_nan(cvi_->val_, ad_, bd_))) { cvi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { cvi_->adj_ += adj_; + } } }; } // namespace internal diff --git a/stan/math/rev/scal/fun/fmax.hpp b/stan/math/rev/scal/fun/fmax.hpp index ff8549e0e98..b73ab3d00d8 100644 --- a/stan/math/rev/scal/fun/fmax.hpp +++ b/stan/math/rev/scal/fun/fmax.hpp @@ -60,13 +60,15 @@ namespace math { */ inline var fmax(const var& a, const var& b) { if (unlikely(is_nan(a))) { - if (unlikely(is_nan(b))) + if (unlikely(is_nan(b))) { return var(new precomp_vv_vari(NOT_A_NUMBER, a.vi_, b.vi_, NOT_A_NUMBER, NOT_A_NUMBER)); + } return b; } - if (unlikely(is_nan(b))) + if (unlikely(is_nan(b))) { return a; + } return a > b ? a : b; } @@ -86,12 +88,14 @@ inline var fmax(const var& a, const var& b) { */ inline var fmax(const var& a, double b) { if (unlikely(is_nan(a))) { - if (unlikely(is_nan(b))) + if (unlikely(is_nan(b))) { return var(new precomp_v_vari(NOT_A_NUMBER, a.vi_, NOT_A_NUMBER)); + } return var(b); } - if (unlikely(is_nan(b))) + if (unlikely(is_nan(b))) { return a; + } return a >= b ? a : var(b); } @@ -111,12 +115,14 @@ inline var fmax(const var& a, double b) { */ inline var fmax(double a, const var& b) { if (unlikely(is_nan(b))) { - if (unlikely(is_nan(a))) + if (unlikely(is_nan(a))) { return var(new precomp_v_vari(NOT_A_NUMBER, b.vi_, NOT_A_NUMBER)); + } return var(a); } - if (unlikely(is_nan(a))) + if (unlikely(is_nan(a))) { return b; + } return a > b ? var(a) : b; } diff --git a/stan/math/rev/scal/fun/fmin.hpp b/stan/math/rev/scal/fun/fmin.hpp index 73aa4079faf..abd9737b812 100644 --- a/stan/math/rev/scal/fun/fmin.hpp +++ b/stan/math/rev/scal/fun/fmin.hpp @@ -56,13 +56,15 @@ namespace math { */ inline var fmin(const var& a, const var& b) { if (unlikely(is_nan(a))) { - if (unlikely(is_nan(b))) + if (unlikely(is_nan(b))) { return var(new precomp_vv_vari(NOT_A_NUMBER, a.vi_, b.vi_, NOT_A_NUMBER, NOT_A_NUMBER)); + } return b; } - if (unlikely(is_nan(b))) + if (unlikely(is_nan(b))) { return a; + } return a < b ? a : b; } @@ -82,12 +84,14 @@ inline var fmin(const var& a, const var& b) { */ inline var fmin(const var& a, double b) { if (unlikely(is_nan(a))) { - if (unlikely(is_nan(b))) + if (unlikely(is_nan(b))) { return var(new precomp_v_vari(NOT_A_NUMBER, a.vi_, NOT_A_NUMBER)); + } return var(b); } - if (unlikely(is_nan(b))) + if (unlikely(is_nan(b))) { return a; + } return a <= b ? a : var(b); } @@ -107,12 +111,14 @@ inline var fmin(const var& a, double b) { */ inline var fmin(double a, const var& b) { if (unlikely(is_nan(b))) { - if (unlikely(is_nan(a))) + if (unlikely(is_nan(a))) { return var(new precomp_v_vari(NOT_A_NUMBER, b.vi_, NOT_A_NUMBER)); + } return var(a); } - if (unlikely(is_nan(a))) + if (unlikely(is_nan(a))) { return b; + } return b <= a ? b : var(a); } diff --git a/stan/math/rev/scal/fun/fmod.hpp b/stan/math/rev/scal/fun/fmod.hpp index ef9552c5a62..8bfd2729c7b 100644 --- a/stan/math/rev/scal/fun/fmod.hpp +++ b/stan/math/rev/scal/fun/fmod.hpp @@ -31,10 +31,11 @@ class fmod_vd_vari : public op_vd_vari { fmod_vd_vari(vari* avi, double b) : op_vd_vari(std::fmod(avi->val_, b), avi, b) {} void chain() { - if (unlikely(is_any_nan(avi_->val_, bd_))) + if (unlikely(is_any_nan(avi_->val_, bd_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_; + } } }; diff --git a/stan/math/rev/scal/fun/gamma_p.hpp b/stan/math/rev/scal/fun/gamma_p.hpp index d5951d1cb6c..e96738d0777 100644 --- a/stan/math/rev/scal/fun/gamma_p.hpp +++ b/stan/math/rev/scal/fun/gamma_p.hpp @@ -35,8 +35,9 @@ class gamma_p_vv_vari : public op_vv_vari { // return zero derivative as gamma_p is flat // to machine precision for b / a > 10 - if (std::fabs(bvi_->val_ / avi_->val_) > 10) + if (std::fabs(bvi_->val_ / avi_->val_) > 10) { return; + } avi_->adj_ += adj_ * grad_reg_lower_inc_gamma(avi_->val_, bvi_->val_); bvi_->adj_ @@ -62,8 +63,9 @@ class gamma_p_vd_vari : public op_vd_vari { // return zero derivative as gamma_p is flat // to machine precision for b / a > 10 - if (std::fabs(bd_ / avi_->val_) > 10) + if (std::fabs(bd_ / avi_->val_) > 10) { return; + } avi_->adj_ += adj_ * grad_reg_lower_inc_gamma(avi_->val_, bd_); } @@ -85,8 +87,9 @@ class gamma_p_dv_vari : public op_dv_vari { // return zero derivative as gamma_p is flat to // machine precision for b / a > 10 - if (std::fabs(bvi_->val_ / ad_) > 10) + if (std::fabs(bvi_->val_ / ad_) > 10) { return; + } bvi_->adj_ += adj_ * std::exp(-bvi_->val_ + (ad_ - 1.0) * std::log(bvi_->val_) diff --git a/stan/math/rev/scal/fun/grad_inc_beta.hpp b/stan/math/rev/scal/fun/grad_inc_beta.hpp index 79325e86369..593185116a2 100644 --- a/stan/math/rev/scal/fun/grad_inc_beta.hpp +++ b/stan/math/rev/scal/fun/grad_inc_beta.hpp @@ -41,8 +41,9 @@ inline void grad_inc_beta(var& g1, var& g2, const var& a, const var& b, var C = exp(a * c1 + b * c2) / a; var dF1 = 0; var dF2 = 0; - if (value_of(value_of(C))) + if (value_of(value_of(C))) { grad_2F1(dF1, dF2, a + b, var(1.0), a + 1, z); + } g1 = (c1 - 1.0 / a) * c3 + C * (dF1 + dF2); g2 = c2 * c3 + C * dF1; } diff --git a/stan/math/rev/scal/fun/if_else.hpp b/stan/math/rev/scal/fun/if_else.hpp index f8b20ead134..c0602154530 100644 --- a/stan/math/rev/scal/fun/if_else.hpp +++ b/stan/math/rev/scal/fun/if_else.hpp @@ -28,10 +28,11 @@ inline var if_else(bool c, const var& y_true, const var& y_false) { * @param y_false Variable to return if condition is false. */ inline var if_else(bool c, double y_true, const var& y_false) { - if (c) + if (c) { return var(y_true); - else + } else { return y_false; + } } /** * If the specified condition is true, return the first variable, @@ -43,10 +44,11 @@ inline var if_else(bool c, double y_true, const var& y_false) { * @param y_false Value to promote to variable and return if condition is false. */ inline var if_else(bool c, const var& y_true, double y_false) { - if (c) + if (c) { return y_true; - else + } else { return var(y_false); + } } } // namespace math diff --git a/stan/math/rev/scal/fun/lmgamma.hpp b/stan/math/rev/scal/fun/lmgamma.hpp index 9df4b218c41..398f1eba451 100644 --- a/stan/math/rev/scal/fun/lmgamma.hpp +++ b/stan/math/rev/scal/fun/lmgamma.hpp @@ -16,8 +16,9 @@ class lmgamma_dv_vari : public op_dv_vari { : op_dv_vari(lmgamma(a, bvi->val_), a, bvi) {} void chain() { double deriv = 0; - for (int i = 1; i < ad_ + 1; i++) + for (int i = 1; i < ad_ + 1; i++) { deriv += digamma(bvi_->val_ + (1.0 - i) / 2.0); + } bvi_->adj_ += adj_ * deriv; } }; diff --git a/stan/math/rev/scal/fun/log_diff_exp.hpp b/stan/math/rev/scal/fun/log_diff_exp.hpp index 0f8bbdb853b..d8980af1401 100644 --- a/stan/math/rev/scal/fun/log_diff_exp.hpp +++ b/stan/math/rev/scal/fun/log_diff_exp.hpp @@ -26,10 +26,11 @@ class log_diff_exp_vd_vari : public op_vd_vari { log_diff_exp_vd_vari(vari* avi, double b) : op_vd_vari(log_diff_exp(avi->val_, b), avi, b) {} void chain() { - if (val_ == NEGATIVE_INFTY) + if (val_ == NEGATIVE_INFTY) { avi_->adj_ += (bd_ == NEGATIVE_INFTY) ? adj_ : adj_ * INFTY; - else + } else { avi_->adj_ += adj_ * calculate_chain(avi_->val_, val_); + } } }; class log_diff_exp_dv_vari : public op_dv_vari { @@ -37,10 +38,11 @@ class log_diff_exp_dv_vari : public op_dv_vari { log_diff_exp_dv_vari(double a, vari* bvi) : op_dv_vari(log_diff_exp(a, bvi->val_), a, bvi) {} void chain() { - if (val_ == NEGATIVE_INFTY) + if (val_ == NEGATIVE_INFTY) { bvi_->adj_ -= adj_ * INFTY; - else + } else { bvi_->adj_ -= adj_ / expm1(ad_ - bvi_->val_); + } } }; } // namespace internal diff --git a/stan/math/rev/scal/fun/log_falling_factorial.hpp b/stan/math/rev/scal/fun/log_falling_factorial.hpp index f8e89698285..706421c4371 100644 --- a/stan/math/rev/scal/fun/log_falling_factorial.hpp +++ b/stan/math/rev/scal/fun/log_falling_factorial.hpp @@ -35,11 +35,12 @@ class log_falling_factorial_vd_vari : public op_vd_vari { log_falling_factorial_vd_vari(vari* avi, double b) : op_vd_vari(log_falling_factorial(avi->val_, b), avi, b) {} void chain() { - if (unlikely(is_any_nan(avi_->val_, bd_))) + if (unlikely(is_any_nan(avi_->val_, bd_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_ * (digamma(avi_->val_ + 1) - digamma(avi_->val_ - bd_ + 1)); + } } }; @@ -48,10 +49,11 @@ class log_falling_factorial_dv_vari : public op_dv_vari { log_falling_factorial_dv_vari(double a, vari* bvi) : op_dv_vari(log_falling_factorial(a, bvi->val_), a, bvi) {} void chain() { - if (unlikely(is_any_nan(ad_, bvi_->val_))) + if (unlikely(is_any_nan(ad_, bvi_->val_))) { bvi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { bvi_->adj_ += adj_ * digamma(ad_ - bvi_->val_ + 1); + } } }; } // namespace internal diff --git a/stan/math/rev/scal/fun/log_mix.hpp b/stan/math/rev/scal/fun/log_mix.hpp index 1edd3af4616..d4ab4c6c23d 100644 --- a/stan/math/rev/scal/fun/log_mix.hpp +++ b/stan/math/rev/scal/fun/log_mix.hpp @@ -105,16 +105,19 @@ inline return_type_t log_mix( one_m_t_prod_exp_lam2_m_lam1 = 1.0 - value_of(theta); } - if (!is_constant_all::value) + if (!is_constant_all::value) { ops_partials.edge1_.partials_[0] = one_m_exp_lam2_m_lam1 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge2_.partials_[0] = theta_double * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1; - if (!is_constant_all::value) + } + if (!is_constant_all::value) { ops_partials.edge3_.partials_[0] = one_m_t_prod_exp_lam2_m_lam1 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1; + } return ops_partials.build(log_mix_function_value); } diff --git a/stan/math/rev/scal/fun/log_sum_exp.hpp b/stan/math/rev/scal/fun/log_sum_exp.hpp index 955ae1cb869..e9ac1e17c8d 100644 --- a/stan/math/rev/scal/fun/log_sum_exp.hpp +++ b/stan/math/rev/scal/fun/log_sum_exp.hpp @@ -26,10 +26,11 @@ class log_sum_exp_vd_vari : public op_vd_vari { log_sum_exp_vd_vari(vari* avi, double b) : op_vd_vari(log_sum_exp(avi->val_, b), avi, b) {} void chain() { - if (val_ == NEGATIVE_INFTY) + if (val_ == NEGATIVE_INFTY) { avi_->adj_ += adj_; - else + } else { avi_->adj_ += adj_ * calculate_chain(avi_->val_, val_); + } } }; class log_sum_exp_dv_vari : public op_dv_vari { @@ -37,10 +38,11 @@ class log_sum_exp_dv_vari : public op_dv_vari { log_sum_exp_dv_vari(double a, vari* bvi) : op_dv_vari(log_sum_exp(a, bvi->val_), a, bvi) {} void chain() { - if (val_ == NEGATIVE_INFTY) + if (val_ == NEGATIVE_INFTY) { bvi_->adj_ += adj_; - else + } else { bvi_->adj_ += adj_ * calculate_chain(bvi_->val_, val_); + } } }; diff --git a/stan/math/rev/scal/fun/multiply_log.hpp b/stan/math/rev/scal/fun/multiply_log.hpp index 43a22193811..a0e069799c5 100644 --- a/stan/math/rev/scal/fun/multiply_log.hpp +++ b/stan/math/rev/scal/fun/multiply_log.hpp @@ -23,10 +23,11 @@ class multiply_log_vv_vari : public op_vv_vari { bvi_->adj_ = std::numeric_limits::quiet_NaN(); } else { avi_->adj_ += adj_ * log(bvi_->val_); - if (bvi_->val_ == 0.0 && avi_->val_ == 0) + if (bvi_->val_ == 0.0 && avi_->val_ == 0) { bvi_->adj_ += adj_ * std::numeric_limits::infinity(); - else + } else { bvi_->adj_ += adj_ * avi_->val_ / bvi_->val_; + } } } }; @@ -36,10 +37,11 @@ class multiply_log_vd_vari : public op_vd_vari { : op_vd_vari(multiply_log(avi->val_, b), avi, b) {} void chain() { using std::log; - if (unlikely(is_any_nan(avi_->val_, bd_))) + if (unlikely(is_any_nan(avi_->val_, bd_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); - else + } else { avi_->adj_ += adj_ * log(bd_); + } } }; class multiply_log_dv_vari : public op_dv_vari { @@ -47,10 +49,11 @@ class multiply_log_dv_vari : public op_dv_vari { multiply_log_dv_vari(double a, vari* bvi) : op_dv_vari(multiply_log(a, bvi->val_), a, bvi) {} void chain() { - if (bvi_->val_ == 0.0 && ad_ == 0.0) + if (bvi_->val_ == 0.0 && ad_ == 0.0) { bvi_->adj_ += adj_ * std::numeric_limits::infinity(); - else + } else { bvi_->adj_ += adj_ * ad_ / bvi_->val_; + } } }; } // namespace internal @@ -95,8 +98,9 @@ inline var multiply_log(const var& a, double b) { * @return Value of a*log(b) */ inline var multiply_log(double a, const var& b) { - if (a == 1.0) + if (a == 1.0) { return log(b); + } return var(new internal::multiply_log_dv_vari(a, b.vi_)); } diff --git a/stan/math/rev/scal/fun/pow.hpp b/stan/math/rev/scal/fun/pow.hpp index e64924398d7..d2a6ded16dc 100644 --- a/stan/math/rev/scal/fun/pow.hpp +++ b/stan/math/rev/scal/fun/pow.hpp @@ -25,8 +25,9 @@ class pow_vv_vari : public op_vv_vari { avi_->adj_ = std::numeric_limits::quiet_NaN(); bvi_->adj_ = std::numeric_limits::quiet_NaN(); } else { - if (avi_->val_ == 0.0) + if (avi_->val_ == 0.0) { return; // partials zero, avoids 0 & log(0) + } avi_->adj_ += adj_ * bvi_->val_ * val_ / avi_->val_; bvi_->adj_ += adj_ * std::log(avi_->val_) * val_; } @@ -41,8 +42,9 @@ class pow_vd_vari : public op_vd_vari { if (unlikely(is_any_nan(avi_->val_, bd_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); } else { - if (avi_->val_ == 0.0) + if (avi_->val_ == 0.0) { return; // partials zero, avoids 0 & log(0) + } avi_->adj_ += adj_ * bd_ * val_ / avi_->val_; } } @@ -56,8 +58,9 @@ class pow_dv_vari : public op_dv_vari { if (unlikely(is_any_nan(bvi_->val_, ad_))) { bvi_->adj_ = std::numeric_limits::quiet_NaN(); } else { - if (ad_ == 0.0) + if (ad_ == 0.0) { return; // partials zero, avoids 0 & log(0) + } bvi_->adj_ += adj_ * std::log(ad_) * val_; } } @@ -119,18 +122,24 @@ inline var pow(const var& base, const var& exponent) { * @return Base raised to the exponent. */ inline var pow(const var& base, double exponent) { - if (exponent == 0.5) + if (exponent == 0.5) { return sqrt(base); - if (exponent == 1.0) + } + if (exponent == 1.0) { return base; - if (exponent == 2.0) + } + if (exponent == 2.0) { return square(base); - if (exponent == -2.0) + } + if (exponent == -2.0) { return inv_square(base); - if (exponent == -1.0) + } + if (exponent == -1.0) { return inv(base); - if (exponent == -0.5) + } + if (exponent == -0.5) { return inv_sqrt(base); + } return var(new internal::pow_vd_vari(base.vi_, exponent)); } diff --git a/stan/math/rev/scal/fun/round.hpp b/stan/math/rev/scal/fun/round.hpp index 51c89503a99..f3179b81534 100644 --- a/stan/math/rev/scal/fun/round.hpp +++ b/stan/math/rev/scal/fun/round.hpp @@ -15,8 +15,9 @@ class round_vari : public op_v_vari { public: explicit round_vari(vari* avi) : op_v_vari(round(avi->val_), avi) {} void chain() { - if (unlikely(is_nan(avi_->val_))) + if (unlikely(is_nan(avi_->val_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); + } } }; } // namespace internal diff --git a/stan/math/rev/scal/fun/trunc.hpp b/stan/math/rev/scal/fun/trunc.hpp index ddb5b4fd5c6..468946705fa 100644 --- a/stan/math/rev/scal/fun/trunc.hpp +++ b/stan/math/rev/scal/fun/trunc.hpp @@ -15,8 +15,9 @@ class trunc_vari : public op_v_vari { public: explicit trunc_vari(vari* avi) : op_v_vari(trunc(avi->val_), avi) {} void chain() { - if (unlikely(is_nan(avi_->val_))) + if (unlikely(is_nan(avi_->val_))) { avi_->adj_ = std::numeric_limits::quiet_NaN(); + } } }; } // namespace internal diff --git a/test/unit/math/mix/mat/eigen_plugins_test.cpp b/test/unit/math/mix/mat/eigen_plugins_test.cpp index 1f72797aeb6..ea1a2035ca7 100644 --- a/test/unit/math/mix/mat/eigen_plugins_test.cpp +++ b/test/unit/math/mix/mat/eigen_plugins_test.cpp @@ -11,9 +11,11 @@ TEST(AgradMixMatrixAddons, matrix_fv) { matrix_fv mat_in = vals; - for (int i = 0; i < 100; i++) - for (int j = 0; j < 100; j++) + for (int i = 0; i < 100; i++) { + for (int j = 0; j < 100; j++) { mat_in(i, j).d_ = derivs(i, j); + } + } expect_matrix_eq(vals, mat_in.val().val()); expect_matrix_eq(vals.array().exp(), mat_in.val().val().array().exp()); @@ -37,9 +39,11 @@ TEST(AgradMixMatrixAddons, matrix_ffv) { matrix_ffv mat_in = vals; - for (int i = 0; i < 100; i++) - for (int j = 0; j < 100; j++) + for (int i = 0; i < 100; i++) { + for (int j = 0; j < 100; j++) { mat_in(i, j).d_ = derivs(i, j); + } + } expect_matrix_eq(vals, mat_in.val().val().val()); expect_matrix_eq(vals.array().exp(), mat_in.val().val().val().array().exp()); @@ -63,8 +67,9 @@ TEST(AgradMixMatrixAddons, vector_fv) { vector_fv vec_in = vals; - for (int i = 0; i < 100; i++) + for (int i = 0; i < 100; i++) { vec_in(i).d_ = derivs(i); + } expect_matrix_eq(vals, vec_in.val().val()); expect_matrix_eq(vals.array().exp(), vec_in.val().val().array().exp()); @@ -88,8 +93,9 @@ TEST(AgradMixMatrixAddons, vector_ffv) { vector_ffv vec_in = vals; - for (int i = 0; i < 100; i++) + for (int i = 0; i < 100; i++) { vec_in(i).d_ = derivs(i); + } expect_matrix_eq(vals, vec_in.val().val().val()); expect_matrix_eq(vals.array().exp(), vec_in.val().val().val().array().exp()); @@ -113,8 +119,9 @@ TEST(AgradMixMatrixAddons, row_vector_fv) { row_vector_fv row_vec_in = vals; - for (int i = 0; i < 100; i++) + for (int i = 0; i < 100; i++) { row_vec_in(i).d_ = derivs(i); + } expect_matrix_eq(vals, row_vec_in.val().val()); expect_matrix_eq(vals.array().exp(), row_vec_in.val().val().array().exp()); @@ -138,8 +145,9 @@ TEST(AgradMixMatrixAddons, row_vector_ffv) { row_vector_ffv row_vec_in = vals; - for (int i = 0; i < 100; i++) + for (int i = 0; i < 100; i++) { row_vec_in(i).d_ = derivs(i); + } expect_matrix_eq(vals, row_vec_in.val().val().val()); expect_matrix_eq(vals.array().exp(), From 9945e591c289f7a73621dcc9551ee0a07d5b37fa Mon Sep 17 00:00:00 2001 From: stevo15025 Date: Thu, 5 Sep 2019 10:42:50 +0300 Subject: [PATCH 02/12] Add braces to everything --- make/clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make/clang-tidy b/make/clang-tidy index e06e26caa53..221b0f7a988 100644 --- a/make/clang-tidy +++ b/make/clang-tidy @@ -25,7 +25,7 @@ clang-tidy-fix: .PHONY: clang-tidy-cleanup clang-tidy-cleanup: find $(folder) -path './test/unit/*' -wholename '*$(files).cpp' -type f | xargs -I {} clang-tidy "{}" \ - -checks='readability-braces-around-statements' \ + -checks='modernize-use-nullptr, modernize-use-using' \ -format-style='file' -header-filter='./stan/*' \ -fix -export-fixes="./.clang-fixes.log" \ -- $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) $(INC_GTEST) $(CXXFLAGS_GTEST) $(CPPFLAGS) From 80c296170a1462512722b55a8eb7381d566bd45a Mon Sep 17 00:00:00 2001 From: stevo15025 Date: Thu, 5 Sep 2019 10:54:52 +0300 Subject: [PATCH 03/12] replace typedef with using and add nullptr --- make/clang-tidy | 8 ----- stan/math/fwd/core/fvar.hpp | 3 +- stan/math/fwd/mat/fun/Eigen_NumTraits.hpp | 4 +-- stan/math/fwd/mat/fun/qr_Q.hpp | 2 +- stan/math/fwd/mat/fun/qr_R.hpp | 2 +- stan/math/fwd/mat/fun/typedefs.hpp | 10 +++--- .../fwd/mat/meta/operands_and_partials.hpp | 16 +++++----- .../fwd/mat/vectorize/apply_scalar_unary.hpp | 2 +- .../fwd/scal/meta/operands_and_partials.hpp | 4 +-- stan/math/fwd/scal/meta/partials_type.hpp | 2 +- stan/math/mix/mat/fun/typedefs.hpp | 10 +++--- stan/math/opencl/copy.hpp | 2 +- stan/math/opencl/matrix_cl.hpp | 8 ++--- stan/math/opencl/matrix_cl_view.hpp | 6 ++-- stan/math/opencl/opencl_context.hpp | 2 +- stan/math/prim/arr/fun/common_type.hpp | 2 +- stan/math/prim/arr/fun/promote_scalar.hpp | 4 +-- .../math/prim/arr/fun/promote_scalar_type.hpp | 2 +- .../prim/arr/functor/coupled_ode_observer.hpp | 2 +- .../prim/arr/meta/VectorBuilderHelper.hpp | 2 +- stan/math/prim/arr/meta/index_type.hpp | 2 +- stan/math/prim/arr/meta/is_vector.hpp | 4 +-- stan/math/prim/arr/meta/scalar_type.hpp | 8 ++--- stan/math/prim/arr/meta/value_type.hpp | 2 +- stan/math/prim/mat/fun/LDLT_factor.hpp | 10 +++--- stan/math/prim/mat/fun/MatrixExponential.h | 27 +++++++++------- stan/math/prim/mat/fun/append_col.hpp | 4 +-- stan/math/prim/mat/fun/append_row.hpp | 4 +-- stan/math/prim/mat/fun/chol2inv.hpp | 2 +- stan/math/prim/mat/fun/common_type.hpp | 2 +- .../prim/mat/fun/corr_matrix_constrain.hpp | 4 +-- stan/math/prim/mat/fun/corr_matrix_free.hpp | 2 +- .../prim/mat/fun/cov_matrix_constrain.hpp | 4 +-- .../math/prim/mat/fun/cov_matrix_free_lkj.hpp | 2 +- .../prim/mat/fun/csr_matrix_times_vector.hpp | 2 +- stan/math/prim/mat/fun/log_mix.hpp | 16 +++++----- stan/math/prim/mat/fun/make_nu.hpp | 2 +- stan/math/prim/mat/fun/ordered_constrain.hpp | 4 +-- stan/math/prim/mat/fun/ordered_free.hpp | 2 +- .../mat/fun/positive_ordered_constrain.hpp | 4 +-- .../prim/mat/fun/positive_ordered_free.hpp | 2 +- stan/math/prim/mat/fun/qr_Q.hpp | 2 +- stan/math/prim/mat/fun/qr_R.hpp | 2 +- stan/math/prim/mat/fun/qr_thin_Q.hpp | 2 +- stan/math/prim/mat/fun/qr_thin_R.hpp | 2 +- stan/math/prim/mat/fun/simplex_constrain.hpp | 4 +-- stan/math/prim/mat/fun/simplex_free.hpp | 2 +- stan/math/prim/mat/fun/sort_indices.hpp | 2 +- stan/math/prim/mat/fun/tail.hpp | 2 +- stan/math/prim/mat/fun/typedefs.hpp | 6 ++-- stan/math/prim/mat/functor/map_rect_mpi.hpp | 4 +-- .../math/prim/mat/meta/append_return_type.hpp | 8 ++--- stan/math/prim/mat/meta/is_vector.hpp | 6 ++-- .../prim/mat/meta/operands_and_partials.hpp | 4 +-- stan/math/prim/mat/meta/scalar_type.hpp | 10 +++--- stan/math/prim/mat/meta/seq_view.hpp | 12 +++---- stan/math/prim/mat/meta/value_type.hpp | 2 +- .../mat/prob/bernoulli_logit_glm_lpmf.hpp | 2 +- stan/math/prim/mat/prob/dirichlet_lpmf.hpp | 4 +-- .../prim/mat/prob/lkj_corr_cholesky_lpdf.hpp | 2 +- .../prim/mat/prob/multi_gp_cholesky_lpdf.hpp | 2 +- stan/math/prim/mat/prob/multi_gp_lpdf.hpp | 2 +- .../mat/prob/multi_normal_cholesky_lpdf.hpp | 8 ++--- stan/math/prim/mat/prob/multi_normal_lpdf.hpp | 4 +-- .../prim/mat/prob/multi_normal_prec_lpdf.hpp | 4 +-- .../prim/mat/prob/multi_student_t_lpdf.hpp | 4 +-- .../prim/mat/prob/ordered_logistic_lpmf.hpp | 4 +-- .../prim/mat/prob/poisson_log_glm_lpmf.hpp | 2 +- .../prim/mat/vectorize/apply_scalar_unary.hpp | 6 ++-- stan/math/prim/scal/fun/common_type.hpp | 2 +- stan/math/prim/scal/fun/fdim.hpp | 2 +- .../math/prim/scal/fun/grad_reg_inc_gamma.hpp | 2 +- .../scal/fun/grad_reg_lower_inc_gamma.hpp | 2 +- .../fun/log_modified_bessel_first_kind.hpp | 2 +- .../prim/scal/fun/promote_scalar_type.hpp | 2 +- stan/math/prim/scal/meta/StdVectorBuilder.hpp | 2 +- stan/math/prim/scal/meta/VectorBuilder.hpp | 2 +- .../prim/scal/meta/VectorBuilderHelper.hpp | 4 +-- stan/math/prim/scal/meta/child_type.hpp | 4 +-- stan/math/prim/scal/meta/index_type.hpp | 2 +- stan/math/prim/scal/meta/is_vector.hpp | 2 +- stan/math/prim/scal/meta/partials_type.hpp | 2 +- stan/math/prim/scal/meta/scalar_type.hpp | 4 +-- stan/math/prim/scal/meta/scalar_type_pre.hpp | 2 +- stan/math/prim/scal/meta/value_type.hpp | 2 +- stan/math/prim/scal/prob/bernoulli_cdf.hpp | 2 +- stan/math/prim/scal/prob/bernoulli_lccdf.hpp | 2 +- stan/math/prim/scal/prob/bernoulli_lcdf.hpp | 2 +- .../prim/scal/prob/bernoulli_logit_lpmf.hpp | 2 +- stan/math/prim/scal/prob/bernoulli_lpmf.hpp | 2 +- .../math/prim/scal/prob/beta_binomial_cdf.hpp | 2 +- .../prim/scal/prob/beta_binomial_lccdf.hpp | 2 +- .../prim/scal/prob/beta_binomial_lcdf.hpp | 2 +- .../prim/scal/prob/beta_binomial_lpmf.hpp | 2 +- .../prim/scal/prob/beta_proportion_lccdf.hpp | 2 +- .../prim/scal/prob/beta_proportion_lcdf.hpp | 2 +- .../prim/scal/prob/beta_proportion_lpdf.hpp | 2 +- stan/math/prim/scal/prob/binomial_cdf.hpp | 2 +- stan/math/prim/scal/prob/binomial_lccdf.hpp | 2 +- stan/math/prim/scal/prob/binomial_lcdf.hpp | 2 +- .../prim/scal/prob/binomial_logit_lpmf.hpp | 2 +- stan/math/prim/scal/prob/binomial_lpmf.hpp | 2 +- stan/math/prim/scal/prob/cauchy_cdf.hpp | 2 +- stan/math/prim/scal/prob/cauchy_lccdf.hpp | 2 +- stan/math/prim/scal/prob/cauchy_lcdf.hpp | 2 +- stan/math/prim/scal/prob/cauchy_lpdf.hpp | 2 +- stan/math/prim/scal/prob/chi_square_cdf.hpp | 2 +- stan/math/prim/scal/prob/chi_square_lccdf.hpp | 2 +- stan/math/prim/scal/prob/chi_square_lcdf.hpp | 2 +- stan/math/prim/scal/prob/chi_square_lpdf.hpp | 2 +- .../prim/scal/prob/double_exponential_cdf.hpp | 2 +- .../scal/prob/double_exponential_lccdf.hpp | 2 +- .../scal/prob/double_exponential_lcdf.hpp | 2 +- .../scal/prob/double_exponential_lpdf.hpp | 2 +- stan/math/prim/scal/prob/exponential_cdf.hpp | 2 +- .../math/prim/scal/prob/exponential_lccdf.hpp | 2 +- stan/math/prim/scal/prob/exponential_lcdf.hpp | 2 +- stan/math/prim/scal/prob/exponential_lpdf.hpp | 2 +- stan/math/prim/scal/prob/frechet_cdf.hpp | 2 +- stan/math/prim/scal/prob/frechet_lccdf.hpp | 2 +- stan/math/prim/scal/prob/frechet_lcdf.hpp | 2 +- stan/math/prim/scal/prob/frechet_lpdf.hpp | 2 +- stan/math/prim/scal/prob/gamma_cdf.hpp | 2 +- stan/math/prim/scal/prob/gamma_lccdf.hpp | 2 +- stan/math/prim/scal/prob/gamma_lcdf.hpp | 2 +- stan/math/prim/scal/prob/gamma_lpdf.hpp | 2 +- stan/math/prim/scal/prob/gumbel_cdf.hpp | 2 +- stan/math/prim/scal/prob/gumbel_lccdf.hpp | 2 +- stan/math/prim/scal/prob/gumbel_lcdf.hpp | 2 +- stan/math/prim/scal/prob/gumbel_lpdf.hpp | 2 +- .../prim/scal/prob/inv_chi_square_cdf.hpp | 2 +- .../prim/scal/prob/inv_chi_square_lccdf.hpp | 2 +- .../prim/scal/prob/inv_chi_square_lcdf.hpp | 2 +- .../prim/scal/prob/inv_chi_square_lpdf.hpp | 2 +- stan/math/prim/scal/prob/inv_gamma_cdf.hpp | 2 +- stan/math/prim/scal/prob/inv_gamma_lccdf.hpp | 2 +- stan/math/prim/scal/prob/inv_gamma_lcdf.hpp | 2 +- stan/math/prim/scal/prob/inv_gamma_lpdf.hpp | 2 +- stan/math/prim/scal/prob/logistic_cdf.hpp | 2 +- stan/math/prim/scal/prob/logistic_lccdf.hpp | 2 +- stan/math/prim/scal/prob/logistic_lcdf.hpp | 2 +- stan/math/prim/scal/prob/logistic_lpdf.hpp | 2 +- stan/math/prim/scal/prob/lognormal_cdf.hpp | 2 +- stan/math/prim/scal/prob/lognormal_lccdf.hpp | 2 +- stan/math/prim/scal/prob/lognormal_lcdf.hpp | 2 +- stan/math/prim/scal/prob/lognormal_lpdf.hpp | 2 +- stan/math/prim/scal/prob/neg_binomial_cdf.hpp | 2 +- .../prim/scal/prob/neg_binomial_lccdf.hpp | 2 +- .../math/prim/scal/prob/neg_binomial_lcdf.hpp | 2 +- .../math/prim/scal/prob/neg_binomial_lpmf.hpp | 2 +- stan/math/prim/scal/prob/normal_cdf.hpp | 2 +- stan/math/prim/scal/prob/normal_lccdf.hpp | 2 +- stan/math/prim/scal/prob/normal_lcdf.hpp | 2 +- stan/math/prim/scal/prob/normal_lpdf.hpp | 2 +- stan/math/prim/scal/prob/pareto_cdf.hpp | 2 +- stan/math/prim/scal/prob/pareto_lccdf.hpp | 2 +- stan/math/prim/scal/prob/pareto_lcdf.hpp | 2 +- stan/math/prim/scal/prob/pareto_lpdf.hpp | 2 +- stan/math/prim/scal/prob/poisson_cdf.hpp | 2 +- stan/math/prim/scal/prob/poisson_lccdf.hpp | 2 +- stan/math/prim/scal/prob/poisson_lcdf.hpp | 2 +- stan/math/prim/scal/prob/poisson_log_lpmf.hpp | 2 +- stan/math/prim/scal/prob/poisson_lpmf.hpp | 2 +- stan/math/prim/scal/prob/rayleigh_cdf.hpp | 2 +- stan/math/prim/scal/prob/rayleigh_lccdf.hpp | 2 +- stan/math/prim/scal/prob/rayleigh_lcdf.hpp | 2 +- stan/math/prim/scal/prob/rayleigh_lpdf.hpp | 2 +- .../scal/prob/scaled_inv_chi_square_cdf.hpp | 2 +- .../scal/prob/scaled_inv_chi_square_lccdf.hpp | 2 +- .../scal/prob/scaled_inv_chi_square_lcdf.hpp | 2 +- .../scal/prob/scaled_inv_chi_square_lpdf.hpp | 2 +- stan/math/prim/scal/prob/std_normal_lpdf.hpp | 2 +- stan/math/prim/scal/prob/student_t_cdf.hpp | 2 +- stan/math/prim/scal/prob/student_t_lccdf.hpp | 2 +- stan/math/prim/scal/prob/student_t_lcdf.hpp | 2 +- stan/math/prim/scal/prob/student_t_lpdf.hpp | 2 +- stan/math/prim/scal/prob/uniform_cdf.hpp | 2 +- stan/math/prim/scal/prob/uniform_lccdf.hpp | 2 +- stan/math/prim/scal/prob/uniform_lcdf.hpp | 2 +- stan/math/prim/scal/prob/uniform_lpdf.hpp | 2 +- stan/math/prim/scal/prob/von_mises_lpdf.hpp | 2 +- stan/math/prim/scal/prob/weibull_cdf.hpp | 2 +- stan/math/prim/scal/prob/weibull_lccdf.hpp | 2 +- stan/math/prim/scal/prob/weibull_lcdf.hpp | 2 +- stan/math/prim/scal/prob/weibull_lpdf.hpp | 2 +- stan/math/prim/scal/prob/wiener_lpdf.hpp | 2 +- stan/math/rev/core/chainablestack.hpp | 2 +- stan/math/rev/core/grad.hpp | 2 +- stan/math/rev/core/var.hpp | 4 +-- stan/math/rev/mat/fun/Eigen_NumTraits.hpp | 32 +++++++++---------- stan/math/rev/mat/fun/LDLT_factor.hpp | 4 +-- stan/math/rev/mat/fun/cholesky_decompose.hpp | 2 +- stan/math/rev/mat/fun/dot_product.hpp | 4 +-- stan/math/rev/mat/fun/softmax.hpp | 2 +- stan/math/rev/mat/fun/squared_distance.hpp | 4 +-- stan/math/rev/mat/fun/typedefs.hpp | 12 +++---- stan/math/rev/mat/functor/adj_jac_apply.hpp | 4 +-- stan/math/rev/mat/functor/algebra_solver.hpp | 12 +++---- .../rev/mat/functor/cvodes_integrator.hpp | 6 ++-- stan/math/rev/mat/functor/cvodes_ode_data.hpp | 6 ++-- .../rev/mat/functor/map_rect_concurrent.hpp | 4 +-- .../rev/mat/meta/operands_and_partials.hpp | 16 +++++----- .../rev/mat/vectorize/apply_scalar_unary.hpp | 2 +- stan/math/rev/scal/meta/partials_type.hpp | 2 +- 204 files changed, 337 insertions(+), 339 deletions(-) diff --git a/make/clang-tidy b/make/clang-tidy index 221b0f7a988..512c37fd69e 100644 --- a/make/clang-tidy +++ b/make/clang-tidy @@ -21,11 +21,3 @@ clang-tidy-fix: -checks='$(tidy_checks)' -format-style='file' -header-filter='./stan/*' \ -fix -export-fixes="./.clang-fixes.log" \ -- $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) $(INC_GTEST) $(CXXFLAGS_GTEST) $(CPPFLAGS) - -.PHONY: clang-tidy-cleanup -clang-tidy-cleanup: - find $(folder) -path './test/unit/*' -wholename '*$(files).cpp' -type f | xargs -I {} clang-tidy "{}" \ - -checks='modernize-use-nullptr, modernize-use-using' \ - -format-style='file' -header-filter='./stan/*' \ - -fix -export-fixes="./.clang-fixes.log" \ - -- $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) $(INC_GTEST) $(CXXFLAGS_GTEST) $(CPPFLAGS) diff --git a/stan/math/fwd/core/fvar.hpp b/stan/math/fwd/core/fvar.hpp index e2bd242e4ab..9e52d047427 100644 --- a/stan/math/fwd/core/fvar.hpp +++ b/stan/math/fwd/core/fvar.hpp @@ -102,7 +102,8 @@ struct fvar { */ template fvar(const V& v, - typename std::enable_if::value>::type* dummy = 0) + typename std::enable_if::value>::type* dummy + = nullptr) : val_(v), d_(0.0) { if (unlikely(is_nan(v))) { d_ = v; diff --git a/stan/math/fwd/mat/fun/Eigen_NumTraits.hpp b/stan/math/fwd/mat/fun/Eigen_NumTraits.hpp index 51cfe4f9ada..df7c2546ca8 100644 --- a/stan/math/fwd/mat/fun/Eigen_NumTraits.hpp +++ b/stan/math/fwd/mat/fun/Eigen_NumTraits.hpp @@ -58,7 +58,7 @@ struct NumTraits> : GenericNumTraits> { */ template struct ScalarBinaryOpTraits, double, BinaryOp> { - typedef stan::math::fvar ReturnType; + using ReturnType = stan::math::fvar; }; /** @@ -67,7 +67,7 @@ struct ScalarBinaryOpTraits, double, BinaryOp> { */ template struct ScalarBinaryOpTraits, BinaryOp> { - typedef stan::math::fvar ReturnType; + using ReturnType = stan::math::fvar; }; } // namespace Eigen diff --git a/stan/math/fwd/mat/fun/qr_Q.hpp b/stan/math/fwd/mat/fun/qr_Q.hpp index d3b1fdf8375..dd18a44a3cb 100644 --- a/stan/math/fwd/mat/fun/qr_Q.hpp +++ b/stan/math/fwd/mat/fun/qr_Q.hpp @@ -12,7 +12,7 @@ namespace math { template Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> qr_Q( const Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic>& m) { - typedef Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> matrix_fwd_t; + using matrix_fwd_t = Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic>; check_nonzero_size("qr_Q", "m", m); check_greater_or_equal("qr_Q", "m.rows()", m.rows(), m.cols()); Eigen::HouseholderQR qr(m.rows(), m.cols()); diff --git a/stan/math/fwd/mat/fun/qr_R.hpp b/stan/math/fwd/mat/fun/qr_R.hpp index c408624cda7..3fef24e31ab 100644 --- a/stan/math/fwd/mat/fun/qr_R.hpp +++ b/stan/math/fwd/mat/fun/qr_R.hpp @@ -12,7 +12,7 @@ namespace math { template Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> qr_R( const Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic>& m) { - typedef Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> matrix_fwd_t; + using matrix_fwd_t = Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic>; check_nonzero_size("qr_R", "m", m); check_greater_or_equal("qr_R", "m.rows()", m.rows(), m.cols()); Eigen::HouseholderQR qr(m.rows(), m.cols()); diff --git a/stan/math/fwd/mat/fun/typedefs.hpp b/stan/math/fwd/mat/fun/typedefs.hpp index f3ed71864c3..21a943f67a9 100644 --- a/stan/math/fwd/mat/fun/typedefs.hpp +++ b/stan/math/fwd/mat/fun/typedefs.hpp @@ -10,18 +10,18 @@ namespace math { typedef Eigen::Matrix::Index size_type; -typedef Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> matrix_fd; +using matrix_fd = Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic>; typedef Eigen::Matrix >, Eigen::Dynamic, Eigen::Dynamic> matrix_ffd; -typedef Eigen::Matrix, Eigen::Dynamic, 1> vector_fd; +using vector_fd = Eigen::Matrix, Eigen::Dynamic, 1>; -typedef Eigen::Matrix >, Eigen::Dynamic, 1> vector_ffd; +using vector_ffd = Eigen::Matrix >, Eigen::Dynamic, 1>; -typedef Eigen::Matrix, 1, Eigen::Dynamic> row_vector_fd; +using row_vector_fd = Eigen::Matrix, 1, Eigen::Dynamic>; -typedef Eigen::Matrix >, 1, Eigen::Dynamic> row_vector_ffd; +using row_vector_ffd = Eigen::Matrix >, 1, Eigen::Dynamic>; } // namespace math } // namespace stan diff --git a/stan/math/fwd/mat/meta/operands_and_partials.hpp b/stan/math/fwd/mat/meta/operands_and_partials.hpp index 4161f97463f..25aababb8e7 100644 --- a/stan/math/fwd/mat/meta/operands_and_partials.hpp +++ b/stan/math/fwd/mat/meta/operands_and_partials.hpp @@ -14,8 +14,8 @@ namespace internal { template class ops_partials_edge > > { public: - typedef std::vector > Op; - typedef Eigen::Matrix partials_t; + using Op = std::vector >; + using partials_t = Eigen::Matrix; partials_t partials_; // For univariate use-cases broadcast_array partials_vec_; // For multivariate explicit ops_partials_edge(const Op& ops) @@ -40,8 +40,8 @@ class ops_partials_edge > > { template class ops_partials_edge, R, C> > { public: - typedef Eigen::Matrix partials_t; - typedef Eigen::Matrix, R, C> Op; + using partials_t = Eigen::Matrix; + using Op = Eigen::Matrix, R, C>; partials_t partials_; // For univariate use-cases broadcast_array partials_vec_; // For multivariate explicit ops_partials_edge(const Op& ops) @@ -67,8 +67,8 @@ class ops_partials_edge, R, C> > { template class ops_partials_edge, R, C> > > { public: - typedef std::vector, R, C> > Op; - typedef Eigen::Matrix partial_t; + using Op = std::vector, R, C>>; + using partial_t = Eigen::Matrix; std::vector partials_vec_; explicit ops_partials_edge(const Op& ops) : partials_vec_(ops.size()), operands_(ops) { @@ -96,8 +96,8 @@ class ops_partials_edge, R, C> > > { template class ops_partials_edge > > > { public: - typedef std::vector > > Op; - typedef std::vector partial_t; + using Op = std::vector > >; + using partial_t = std::vector; std::vector partials_vec_; explicit ops_partials_edge(const Op& ops) : partials_vec_(length(ops)), operands_(ops) { diff --git a/stan/math/fwd/mat/vectorize/apply_scalar_unary.hpp b/stan/math/fwd/mat/vectorize/apply_scalar_unary.hpp index 3d96fe52758..6e39db6c73e 100644 --- a/stan/math/fwd/mat/vectorize/apply_scalar_unary.hpp +++ b/stan/math/fwd/mat/vectorize/apply_scalar_unary.hpp @@ -23,7 +23,7 @@ struct apply_scalar_unary > { * Function return type, which is same as the argument type for * the function, fvar<T>. */ - typedef fvar return_t; + using return_t = fvar; /** * Apply the function specified by F to the specified argument. diff --git a/stan/math/fwd/scal/meta/operands_and_partials.hpp b/stan/math/fwd/scal/meta/operands_and_partials.hpp index 807a7858330..ffb9a51ad95 100644 --- a/stan/math/fwd/scal/meta/operands_and_partials.hpp +++ b/stan/math/fwd/scal/meta/operands_and_partials.hpp @@ -11,7 +11,7 @@ namespace internal { template class ops_partials_edge > { public: - typedef fvar Op; + using Op = fvar; Dx partial_; broadcast_array partials_; explicit ops_partials_edge(const Op& op) @@ -71,7 +71,7 @@ class operands_and_partials > { internal::ops_partials_edge edge3_; internal::ops_partials_edge edge4_; internal::ops_partials_edge edge5_; - typedef fvar T_return_type; + using T_return_type = fvar; explicit operands_and_partials(const Op1& o1) : edge1_(o1) {} operands_and_partials(const Op1& o1, const Op2& o2) : edge1_(o1), edge2_(o2) {} diff --git a/stan/math/fwd/scal/meta/partials_type.hpp b/stan/math/fwd/scal/meta/partials_type.hpp index fa92c4d4e07..50e7d112ee4 100644 --- a/stan/math/fwd/scal/meta/partials_type.hpp +++ b/stan/math/fwd/scal/meta/partials_type.hpp @@ -8,7 +8,7 @@ namespace stan { template struct partials_type > { - typedef T type; + using type = T; }; } // namespace stan diff --git a/stan/math/mix/mat/fun/typedefs.hpp b/stan/math/mix/mat/fun/typedefs.hpp index 0a331df27c6..2ec20ca3687 100644 --- a/stan/math/mix/mat/fun/typedefs.hpp +++ b/stan/math/mix/mat/fun/typedefs.hpp @@ -9,18 +9,18 @@ namespace stan { namespace math { -typedef Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> matrix_fv; +using matrix_fv = Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic>; typedef Eigen::Matrix >, Eigen::Dynamic, Eigen::Dynamic> matrix_ffv; -typedef Eigen::Matrix, Eigen::Dynamic, 1> vector_fv; +using vector_fv = Eigen::Matrix, Eigen::Dynamic, 1>; -typedef Eigen::Matrix >, Eigen::Dynamic, 1> vector_ffv; +using vector_ffv = Eigen::Matrix >, Eigen::Dynamic, 1>; -typedef Eigen::Matrix, 1, Eigen::Dynamic> row_vector_fv; +using row_vector_fv = Eigen::Matrix, 1, Eigen::Dynamic>; -typedef Eigen::Matrix >, 1, Eigen::Dynamic> row_vector_ffv; +using row_vector_ffv = Eigen::Matrix >, 1, Eigen::Dynamic>; } // namespace math } // namespace stan diff --git a/stan/math/opencl/copy.hpp b/stan/math/opencl/copy.hpp index c6b3cdbdf36..c6203cac016 100644 --- a/stan/math/opencl/copy.hpp +++ b/stan/math/opencl/copy.hpp @@ -162,7 +162,7 @@ inline matrix_cl packed_copy(const std::vector& src, int rows) { cl::Event packed_event; const cl::CommandQueue queue = opencl_context.queue(); queue.enqueueWriteBuffer(packed.buffer(), CL_FALSE, 0, - sizeof(T) * packed_size, src.data(), NULL, + sizeof(T) * packed_size, src.data(), nullptr, &packed_event); packed.add_write_event(packed_event); stan::math::opencl_kernels::unpack(cl::NDRange(dst.rows(), dst.rows()), dst, diff --git a/stan/math/opencl/matrix_cl.hpp b/stan/math/opencl/matrix_cl.hpp index 879720d2de4..6da08142983 100644 --- a/stan/math/opencl/matrix_cl.hpp +++ b/stan/math/opencl/matrix_cl.hpp @@ -43,7 +43,7 @@ class matrix_cl> { mutable std::vector read_events_; // Tracks reads public: - typedef T type; + using type = T; // Forward declare the methods that work in place on the matrix template void zeros(); @@ -260,7 +260,7 @@ class matrix_cl> { cl::Event write_event; queue.enqueueWriteBuffer( buffer_cl_, CL_FALSE, sizeof(double) * offset_size, - sizeof(double) * rows_, A[i].data(), NULL, &write_event); + sizeof(double) * rows_, A[i].data(), nullptr, &write_event); this->add_write_event(write_event); } } catch (const cl::Error& e) { @@ -322,7 +322,7 @@ class matrix_cl> { buffer_cl_ = cl::Buffer(ctx, CL_MEM_READ_WRITE, sizeof(T) * A.size()); cl::Event transfer_event; queue.enqueueWriteBuffer(buffer_cl_, CL_FALSE, 0, sizeof(T) * A.size(), - A.data(), NULL, &transfer_event); + A.data(), nullptr, &transfer_event); this->add_write_event(transfer_event); } catch (const cl::Error& e) { check_opencl_error("matrix constructor", e); @@ -349,7 +349,7 @@ class matrix_cl> { buffer_cl_ = cl::Buffer(ctx, CL_MEM_READ_WRITE, sizeof(T) * A.size()); cl::Event transfer_event; queue.enqueueWriteBuffer(buffer_cl_, CL_FALSE, 0, sizeof(T) * A.size(), - A.data(), NULL, &transfer_event); + A.data(), nullptr, &transfer_event); this->add_write_event(transfer_event); } catch (const cl::Error& e) { check_opencl_error("matrix constructor", e); diff --git a/stan/math/opencl/matrix_cl_view.hpp b/stan/math/opencl/matrix_cl_view.hpp index 3930cbe816b..da16fcce7fa 100644 --- a/stan/math/opencl/matrix_cl_view.hpp +++ b/stan/math/opencl/matrix_cl_view.hpp @@ -18,7 +18,7 @@ enum class matrix_cl_view { Diagonal = 0, Lower = 1, Upper = 2, Entire = 3 }; */ inline const matrix_cl_view either(const matrix_cl_view left_view, const matrix_cl_view right_view) { - typedef typename std::underlying_type::type underlying; + using underlying = typename std::underlying_type::type; return static_cast(static_cast(left_view) | static_cast(right_view)); } @@ -31,7 +31,7 @@ inline const matrix_cl_view either(const matrix_cl_view left_view, */ inline const matrix_cl_view both(const matrix_cl_view left_view, const matrix_cl_view right_view) { - typedef typename std::underlying_type::type underlying; + using underlying = typename std::underlying_type::type; return static_cast(static_cast(left_view) & static_cast(right_view)); } @@ -69,7 +69,7 @@ inline const matrix_cl_view transpose(const matrix_cl_view view) { * @return inverted view */ inline const matrix_cl_view invert(const matrix_cl_view view) { - typedef typename std::underlying_type::type underlying; + using underlying = typename std::underlying_type::type; return static_cast( static_cast(matrix_cl_view::Entire) & ~static_cast(view)); diff --git a/stan/math/opencl/opencl_context.hpp b/stan/math/opencl/opencl_context.hpp index 76c5113f215..45586d5c0b7 100644 --- a/stan/math/opencl/opencl_context.hpp +++ b/stan/math/opencl/opencl_context.hpp @@ -174,7 +174,7 @@ class opencl_context_base { // the device // Holds Default parameter values for each Kernel. - typedef std::map map_base_opts; + using map_base_opts = std::map; map_base_opts base_opts_ = {{"LOWER", static_cast(matrix_cl_view::Lower)}, {"UPPER", static_cast(matrix_cl_view::Upper)}, diff --git a/stan/math/prim/arr/fun/common_type.hpp b/stan/math/prim/arr/fun/common_type.hpp index 350b5197237..9e7debe1c4f 100644 --- a/stan/math/prim/arr/fun/common_type.hpp +++ b/stan/math/prim/arr/fun/common_type.hpp @@ -18,7 +18,7 @@ namespace math { */ template struct common_type, std::vector > { - typedef std::vector::type> type; + using type = std::vector::type>; }; } // namespace math diff --git a/stan/math/prim/arr/fun/promote_scalar.hpp b/stan/math/prim/arr/fun/promote_scalar.hpp index 96573bb2fcf..6e7b4c9706f 100644 --- a/stan/math/prim/arr/fun/promote_scalar.hpp +++ b/stan/math/prim/arr/fun/promote_scalar.hpp @@ -30,8 +30,8 @@ struct promote_scalar_struct > { */ static std::vector::type> apply( const std::vector& x) { - typedef std::vector::type> return_t; - typedef typename index_type::type idx_t; + using return_t = std::vector::type>; + using idx_t = typename index_type::type; return_t y(x.size()); for (idx_t i = 0; i < x.size(); ++i) { y[i] = promote_scalar_struct::apply(x[i]); diff --git a/stan/math/prim/arr/fun/promote_scalar_type.hpp b/stan/math/prim/arr/fun/promote_scalar_type.hpp index 3c0cbad45db..6105adac4fc 100644 --- a/stan/math/prim/arr/fun/promote_scalar_type.hpp +++ b/stan/math/prim/arr/fun/promote_scalar_type.hpp @@ -21,7 +21,7 @@ struct promote_scalar_type > { /** * The promoted type. */ - typedef std::vector::type> type; + using type = std::vector::type>; }; } // namespace math diff --git a/stan/math/prim/arr/functor/coupled_ode_observer.hpp b/stan/math/prim/arr/functor/coupled_ode_observer.hpp index 2fe608e5f87..f2a113c8ead 100644 --- a/stan/math/prim/arr/functor/coupled_ode_observer.hpp +++ b/stan/math/prim/arr/functor/coupled_ode_observer.hpp @@ -28,7 +28,7 @@ namespace math { */ template struct coupled_ode_observer { - typedef return_type_t return_t; + using return_t = return_type_t; typedef operands_and_partials, std::vector, T_t0, T_ts> ops_partials_t; diff --git a/stan/math/prim/arr/meta/VectorBuilderHelper.hpp b/stan/math/prim/arr/meta/VectorBuilderHelper.hpp index bf95072fff2..16e1bc44014 100644 --- a/stan/math/prim/arr/meta/VectorBuilderHelper.hpp +++ b/stan/math/prim/arr/meta/VectorBuilderHelper.hpp @@ -18,7 +18,7 @@ class VectorBuilderHelper { public: explicit VectorBuilderHelper(size_t n) : x_(n) {} - typedef std::vector type; + using type = std::vector; T1& operator[](size_t i) { return x_[i]; } diff --git a/stan/math/prim/arr/meta/index_type.hpp b/stan/math/prim/arr/meta/index_type.hpp index 0dc9cd4bac1..5df00a63c8c 100644 --- a/stan/math/prim/arr/meta/index_type.hpp +++ b/stan/math/prim/arr/meta/index_type.hpp @@ -18,7 +18,7 @@ struct index_type > { /** * Typedef for index of standard vectors. */ - typedef typename std::vector::size_type type; + using type = typename std::vector::size_type; }; } // namespace math diff --git a/stan/math/prim/arr/meta/is_vector.hpp b/stan/math/prim/arr/meta/is_vector.hpp index 599eec52a88..cc8dbf09682 100644 --- a/stan/math/prim/arr/meta/is_vector.hpp +++ b/stan/math/prim/arr/meta/is_vector.hpp @@ -12,12 +12,12 @@ namespace stan { template struct is_vector { enum { value = is_vector::value }; - typedef T type; + using type = T; }; template struct is_vector > { enum { value = 1 }; - typedef T type; + using type = T; }; } // namespace stan #endif diff --git a/stan/math/prim/arr/meta/scalar_type.hpp b/stan/math/prim/arr/meta/scalar_type.hpp index 19457d85e1d..7b576f86108 100644 --- a/stan/math/prim/arr/meta/scalar_type.hpp +++ b/stan/math/prim/arr/meta/scalar_type.hpp @@ -7,22 +7,22 @@ namespace stan { template struct scalar_type > { - typedef typename scalar_type::type type; + using type = typename scalar_type::type; }; template struct scalar_type > { - typedef typename scalar_type::type type; + using type = typename scalar_type::type; }; template struct scalar_type&> { - typedef typename scalar_type::type type; + using type = typename scalar_type::type; }; template struct scalar_type&> { - typedef typename scalar_type::type type; + using type = typename scalar_type::type; }; } // namespace stan #endif diff --git a/stan/math/prim/arr/meta/value_type.hpp b/stan/math/prim/arr/meta/value_type.hpp index 118047f8877..8bf9f14f082 100644 --- a/stan/math/prim/arr/meta/value_type.hpp +++ b/stan/math/prim/arr/meta/value_type.hpp @@ -19,7 +19,7 @@ struct value_type > { * Type of value stored in a standard vector with type * T entries. */ - typedef T type; + using type = T; }; } // namespace math diff --git a/stan/math/prim/mat/fun/LDLT_factor.hpp b/stan/math/prim/mat/fun/LDLT_factor.hpp index 0cd9400775e..76400a38170 100644 --- a/stan/math/prim/mat/fun/LDLT_factor.hpp +++ b/stan/math/prim/mat/fun/LDLT_factor.hpp @@ -62,11 +62,11 @@ namespace math { template class LDLT_factor { public: - typedef Eigen::Matrix vector_t; - typedef Eigen::Matrix matrix_t; - typedef Eigen::LDLT ldlt_t; - typedef size_t size_type; - typedef double value_type; + using vector_t = Eigen::Matrix; + using matrix_t = Eigen::Matrix; + using ldlt_t = Eigen::LDLT; + using size_type = size_t; + using value_type = double; LDLT_factor() : N_(0), ldltP_(new ldlt_t()) {} diff --git a/stan/math/prim/mat/fun/MatrixExponential.h b/stan/math/prim/mat/fun/MatrixExponential.h index 927b38108fa..35f7aa12479 100644 --- a/stan/math/prim/mat/fun/MatrixExponential.h +++ b/stan/math/prim/mat/fun/MatrixExponential.h @@ -38,7 +38,7 @@ namespace Eigen { return ldexp(x, -m_squarings); } - typedef std::complex ComplexScalar; + using ComplexScalar = std::complex; /** \brief Scale a matrix coefficient. * @@ -63,8 +63,9 @@ namespace Eigen { template void matrix_exp_pade3(const MatrixType &A, MatrixType &U, MatrixType &V) { - using Eigen::internal::traits; - typedef typename Eigen::NumTraits::Scalar>::Real RealScalar; + using Eigen::internal::traits; + using RealScalar = + typename Eigen::NumTraits::Scalar>::Real; const RealScalar b[] = {120.L, 60.L, 12.L, 1.L}; const MatrixType A2 = A * A; const MatrixType tmp = b[3] * A2 + b[1] * MatrixType::Identity(A.rows(), A.cols()); @@ -79,8 +80,9 @@ namespace Eigen { */ template void matrix_exp_pade5(const MatrixType &A, MatrixType &U, MatrixType &V) - { - typedef typename Eigen::NumTraits::Scalar>::Real RealScalar; + { + using RealScalar = typename Eigen::NumTraits< + typename Eigen::internal::traits::Scalar>::Real; const RealScalar b[] = {30240.L, 15120.L, 3360.L, 420.L, 30.L, 1.L}; const MatrixType A2 = A * A; const MatrixType A4 = A2 * A2; @@ -97,8 +99,9 @@ namespace Eigen { template void matrix_exp_pade7(const MatrixType &A, MatrixType &U, MatrixType &V) { - using Eigen::internal::traits; - typedef typename Eigen::NumTraits::Scalar>::Real RealScalar; + using Eigen::internal::traits; + using RealScalar = + typename Eigen::NumTraits::Scalar>::Real; const RealScalar b[] = {17297280.L, 8648640.L, 1995840.L, 277200.L, 25200.L, 1512.L, 56.L, 1.L}; const MatrixType A2 = A * A; const MatrixType A4 = A2 * A2; @@ -117,8 +120,9 @@ namespace Eigen { template void matrix_exp_pade9(const MatrixType &A, MatrixType &U, MatrixType &V) { - using Eigen::internal::traits; - typedef typename Eigen::NumTraits::Scalar>::Real RealScalar; + using Eigen::internal::traits; + using RealScalar = + typename Eigen::NumTraits::Scalar>::Real; const RealScalar b[] = {17643225600.L, 8821612800.L, 2075673600.L, 302702400.L, 30270240.L, 2162160.L, 110880.L, 3960.L, 90.L, 1.L}; const MatrixType A2 = A * A; @@ -139,8 +143,9 @@ namespace Eigen { template void matrix_exp_pade13(const MatrixType &A, MatrixType &U, MatrixType &V) { - using Eigen::internal::traits; - typedef typename Eigen::NumTraits::Scalar>::Real RealScalar; + using Eigen::internal::traits; + using RealScalar = + typename Eigen::NumTraits::Scalar>::Real; const RealScalar b[] = {64764752532480000.L, 32382376266240000.L, 7771770303897600.L, 1187353796428800.L, 129060195264000.L, 10559470521600.L, 670442572800.L, 33522128640.L, 1323241920.L, 40840800.L, 960960.L, 16380.L, 182.L, 1.L}; diff --git a/stan/math/prim/mat/fun/append_col.hpp b/stan/math/prim/mat/fun/append_col.hpp index 88e15f3e6f8..eee2d3fc417 100644 --- a/stan/math/prim/mat/fun/append_col.hpp +++ b/stan/math/prim/mat/fun/append_col.hpp @@ -178,7 +178,7 @@ inline Eigen::Matrix, 1, Eigen::Dynamic> append_col( const T1& A, const Eigen::Matrix& B) { using Eigen::Dynamic; using Eigen::Matrix; - typedef return_type_t return_type; + using return_type = return_type_t; Matrix result(B.size() + 1); result << A, B.template cast(); @@ -204,7 +204,7 @@ inline Eigen::Matrix, 1, Eigen::Dynamic> append_col( const Eigen::Matrix& A, const T2& B) { using Eigen::Dynamic; using Eigen::Matrix; - typedef return_type_t return_type; + using return_type = return_type_t; Matrix result(A.size() + 1); result << A.template cast(), B; diff --git a/stan/math/prim/mat/fun/append_row.hpp b/stan/math/prim/mat/fun/append_row.hpp index a9e0f6ee7ce..91261943fa3 100644 --- a/stan/math/prim/mat/fun/append_row.hpp +++ b/stan/math/prim/mat/fun/append_row.hpp @@ -169,7 +169,7 @@ inline Eigen::Matrix, Eigen::Dynamic, 1> append_row( const T1& A, const Eigen::Matrix& B) { using Eigen::Dynamic; using Eigen::Matrix; - typedef return_type_t return_type; + using return_type = return_type_t; Matrix result(B.size() + 1); result << A, B.template cast(); @@ -194,7 +194,7 @@ inline Eigen::Matrix, Eigen::Dynamic, 1> append_row( const Eigen::Matrix& A, const T2& B) { using Eigen::Dynamic; using Eigen::Matrix; - typedef return_type_t return_type; + using return_type = return_type_t; Matrix result(A.size() + 1); result << A.template cast(), B; diff --git a/stan/math/prim/mat/fun/chol2inv.hpp b/stan/math/prim/mat/fun/chol2inv.hpp index ee6b35cd743..929e2dd34da 100644 --- a/stan/math/prim/mat/fun/chol2inv.hpp +++ b/stan/math/prim/mat/fun/chol2inv.hpp @@ -26,7 +26,7 @@ Eigen::Matrix chol2inv( check_square("chol2inv", "L", L); check_lower_triangular("chol2inv", "L", L); int K = L.rows(); - typedef Eigen::Matrix matrix_t; + using matrix_t = Eigen::Matrix; if (K == 0) { return L; } diff --git a/stan/math/prim/mat/fun/common_type.hpp b/stan/math/prim/mat/fun/common_type.hpp index 5b136197ed2..34596758da3 100644 --- a/stan/math/prim/mat/fun/common_type.hpp +++ b/stan/math/prim/mat/fun/common_type.hpp @@ -18,7 +18,7 @@ namespace math { */ template struct common_type, Eigen::Matrix > { - typedef Eigen::Matrix::type, R, C> type; + using type = Eigen::Matrix::type, R, C>; }; } // namespace math diff --git a/stan/math/prim/mat/fun/corr_matrix_constrain.hpp b/stan/math/prim/mat/fun/corr_matrix_constrain.hpp index baec681b8a4..2f1f3bc5810 100644 --- a/stan/math/prim/mat/fun/corr_matrix_constrain.hpp +++ b/stan/math/prim/mat/fun/corr_matrix_constrain.hpp @@ -41,7 +41,7 @@ Eigen::Matrix corr_matrix_constrain( typename math::index_type >::type k) { using Eigen::Dynamic; using Eigen::Matrix; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; size_type k_choose_2 = (k * (k - 1)) / 2; check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", @@ -80,7 +80,7 @@ Eigen::Matrix corr_matrix_constrain( using Eigen::Array; using Eigen::Dynamic; using Eigen::Matrix; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; size_type k_choose_2 = (k * (k - 1)) / 2; check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", diff --git a/stan/math/prim/mat/fun/corr_matrix_free.hpp b/stan/math/prim/mat/fun/corr_matrix_free.hpp index 6793be2efa7..6b2bd991972 100644 --- a/stan/math/prim/mat/fun/corr_matrix_free.hpp +++ b/stan/math/prim/mat/fun/corr_matrix_free.hpp @@ -42,7 +42,7 @@ Eigen::Matrix corr_matrix_free( using Eigen::Array; using Eigen::Dynamic; using Eigen::Matrix; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; size_type k = y.rows(); size_type k_choose_2 = (k * (k - 1)) / 2; diff --git a/stan/math/prim/mat/fun/cov_matrix_constrain.hpp b/stan/math/prim/mat/fun/cov_matrix_constrain.hpp index 2f7237d560d..2eb30b6fec6 100644 --- a/stan/math/prim/mat/fun/cov_matrix_constrain.hpp +++ b/stan/math/prim/mat/fun/cov_matrix_constrain.hpp @@ -31,7 +31,7 @@ Eigen::Matrix cov_matrix_constrain( using Eigen::Dynamic; using Eigen::Matrix; using std::exp; - typedef typename index_type >::type index_t; + using index_t = typename index_type>::type; Matrix L(K, K); check_size_match("cov_matrix_constrain", "x.size()", x.size(), @@ -71,7 +71,7 @@ Eigen::Matrix cov_matrix_constrain( using Eigen::Matrix; using std::exp; using std::log; - typedef typename index_type >::type index_t; + using index_t = typename index_type>::type; check_size_match("cov_matrix_constrain", "x.size()", x.size(), "K + (K choose 2)", (K * (K + 1)) / 2); Matrix L(K, K); diff --git a/stan/math/prim/mat/fun/cov_matrix_free_lkj.hpp b/stan/math/prim/mat/fun/cov_matrix_free_lkj.hpp index 8a926d81edd..1a6c547cc3c 100644 --- a/stan/math/prim/mat/fun/cov_matrix_free_lkj.hpp +++ b/stan/math/prim/mat/fun/cov_matrix_free_lkj.hpp @@ -34,7 +34,7 @@ Eigen::Matrix cov_matrix_free_lkj( using Eigen::Array; using Eigen::Dynamic; using Eigen::Matrix; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; check_nonzero_size("cov_matrix_free_lkj", "y", y); check_square("cov_matrix_free_lkj", "y", y); diff --git a/stan/math/prim/mat/fun/csr_matrix_times_vector.hpp b/stan/math/prim/mat/fun/csr_matrix_times_vector.hpp index fff7befa4f8..957014936f5 100644 --- a/stan/math/prim/mat/fun/csr_matrix_times_vector.hpp +++ b/stan/math/prim/mat/fun/csr_matrix_times_vector.hpp @@ -78,7 +78,7 @@ csr_matrix_times_vector(int m, int n, const Eigen::Matrix& w, const std::vector& v, const std::vector& u, const Eigen::Matrix& b) { - typedef return_type_t result_t; + using result_t = return_type_t; check_positive("csr_matrix_times_vector", "m", m); check_positive("csr_matrix_times_vector", "n", n); diff --git a/stan/math/prim/mat/fun/log_mix.hpp b/stan/math/prim/mat/fun/log_mix.hpp index e53f0e22b80..322586761aa 100644 --- a/stan/math/prim/mat/fun/log_mix.hpp +++ b/stan/math/prim/mat/fun/log_mix.hpp @@ -41,9 +41,9 @@ template return_type_t log_mix(const T_theta& theta, const T_lam& lambda) { static const char* function = "log_mix"; - typedef partials_return_type_t T_partials_return; + using T_partials_return = partials_return_type_t; - typedef typename Eigen::Matrix T_partials_vec; + using T_partials_vec = typename Eigen::Matrix; const int N = length(theta); @@ -125,11 +125,11 @@ return_type_t > > log_mix( T_theta, std::vector > >::type T_partials_return; - typedef typename Eigen::Matrix T_partials_vec; + using T_partials_vec = typename Eigen::Matrix; - typedef typename Eigen::Matrix T_partials_mat; + using T_partials_mat = typename Eigen::Matrix; - typedef typename std::vector > T_lamvec_type; + using T_lamvec_type = typename std::vector>; const int N = length(lambda); const int M = theta.size(); @@ -219,11 +219,11 @@ return_type_t > > log_mix( typedef typename stan::partials_return_type< T_theta, std::vector > >::type T_partials_return; - typedef typename Eigen::Matrix T_partials_vec; + using T_partials_vec = typename Eigen::Matrix; - typedef typename Eigen::Matrix T_partials_mat; + using T_partials_mat = typename Eigen::Matrix; - typedef typename std::vector > T_lamvec_type; + using T_lamvec_type = typename std::vector >; const int N = length(lambda); const int M = theta.size(); diff --git a/stan/math/prim/mat/fun/make_nu.hpp b/stan/math/prim/mat/fun/make_nu.hpp index 8c597cb6c22..6b4db295bcf 100644 --- a/stan/math/prim/mat/fun/make_nu.hpp +++ b/stan/math/prim/mat/fun/make_nu.hpp @@ -19,7 +19,7 @@ Eigen::Array make_nu(const T& eta, size_t K) { using Eigen::Array; using Eigen::Dynamic; using Eigen::Matrix; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; // Best (1978) implies nu = 2 * alpha for the dof in a t // distribution that generates a beta variate on (-1, 1) diff --git a/stan/math/prim/mat/fun/ordered_constrain.hpp b/stan/math/prim/mat/fun/ordered_constrain.hpp index a7691c49b2b..a2981eba2c7 100644 --- a/stan/math/prim/mat/fun/ordered_constrain.hpp +++ b/stan/math/prim/mat/fun/ordered_constrain.hpp @@ -24,7 +24,7 @@ Eigen::Matrix ordered_constrain( using Eigen::Matrix; using std::exp; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; size_type k = x.size(); Matrix y(k); @@ -56,7 +56,7 @@ inline Eigen::Matrix ordered_constrain( using Eigen::Dynamic; using Eigen::Matrix; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; for (size_type i = 1; i < x.size(); ++i) { lp += x(i); diff --git a/stan/math/prim/mat/fun/ordered_free.hpp b/stan/math/prim/mat/fun/ordered_free.hpp index c4ec2159aa4..b9932477075 100644 --- a/stan/math/prim/mat/fun/ordered_free.hpp +++ b/stan/math/prim/mat/fun/ordered_free.hpp @@ -28,7 +28,7 @@ Eigen::Matrix ordered_free( using Eigen::Dynamic; using Eigen::Matrix; using std::log; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; size_type k = y.size(); Matrix x(k); diff --git a/stan/math/prim/mat/fun/positive_ordered_constrain.hpp b/stan/math/prim/mat/fun/positive_ordered_constrain.hpp index e20a0902cb2..672a604a0e2 100644 --- a/stan/math/prim/mat/fun/positive_ordered_constrain.hpp +++ b/stan/math/prim/mat/fun/positive_ordered_constrain.hpp @@ -23,7 +23,7 @@ Eigen::Matrix positive_ordered_constrain( using Eigen::Dynamic; using Eigen::Matrix; using std::exp; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; size_type k = x.size(); Matrix y(k); @@ -54,7 +54,7 @@ inline Eigen::Matrix positive_ordered_constrain( const Eigen::Matrix& x, T& lp) { using Eigen::Dynamic; using Eigen::Matrix; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; for (size_type i = 0; i < x.size(); ++i) { lp += x(i); diff --git a/stan/math/prim/mat/fun/positive_ordered_free.hpp b/stan/math/prim/mat/fun/positive_ordered_free.hpp index f92ae0e2d04..65ab12e2deb 100644 --- a/stan/math/prim/mat/fun/positive_ordered_free.hpp +++ b/stan/math/prim/mat/fun/positive_ordered_free.hpp @@ -27,7 +27,7 @@ Eigen::Matrix positive_ordered_free( using Eigen::Dynamic; using Eigen::Matrix; using std::log; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; check_positive_ordered("stan::math::positive_ordered_free", "Positive ordered variable", y); diff --git a/stan/math/prim/mat/fun/qr_Q.hpp b/stan/math/prim/mat/fun/qr_Q.hpp index a1bc3d522d5..dc80f9203cd 100644 --- a/stan/math/prim/mat/fun/qr_Q.hpp +++ b/stan/math/prim/mat/fun/qr_Q.hpp @@ -18,7 +18,7 @@ namespace math { template Eigen::Matrix qr_Q( const Eigen::Matrix& m) { - typedef Eigen::Matrix matrix_t; + using matrix_t = Eigen::Matrix; check_nonzero_size("qr_Q", "m", m); Eigen::HouseholderQR qr(m.rows(), m.cols()); qr.compute(m); diff --git a/stan/math/prim/mat/fun/qr_R.hpp b/stan/math/prim/mat/fun/qr_R.hpp index 92643739794..1ab2cf3e0f4 100644 --- a/stan/math/prim/mat/fun/qr_R.hpp +++ b/stan/math/prim/mat/fun/qr_R.hpp @@ -18,7 +18,7 @@ namespace math { template Eigen::Matrix qr_R( const Eigen::Matrix& m) { - typedef Eigen::Matrix matrix_t; + using matrix_t = Eigen::Matrix; check_nonzero_size("qr_R", "m", m); Eigen::HouseholderQR qr(m.rows(), m.cols()); qr.compute(m); diff --git a/stan/math/prim/mat/fun/qr_thin_Q.hpp b/stan/math/prim/mat/fun/qr_thin_Q.hpp index 211912cb2a0..ffc1c55b8d1 100644 --- a/stan/math/prim/mat/fun/qr_thin_Q.hpp +++ b/stan/math/prim/mat/fun/qr_thin_Q.hpp @@ -18,7 +18,7 @@ namespace math { template Eigen::Matrix qr_thin_Q( const Eigen::Matrix& m) { - typedef Eigen::Matrix matrix_t; + using matrix_t = Eigen::Matrix; check_nonzero_size("qr_thin_Q", "m", m); Eigen::HouseholderQR qr(m.rows(), m.cols()); qr.compute(m); diff --git a/stan/math/prim/mat/fun/qr_thin_R.hpp b/stan/math/prim/mat/fun/qr_thin_R.hpp index 5516806c507..79575f2cac4 100644 --- a/stan/math/prim/mat/fun/qr_thin_R.hpp +++ b/stan/math/prim/mat/fun/qr_thin_R.hpp @@ -18,7 +18,7 @@ namespace math { template Eigen::Matrix qr_thin_R( const Eigen::Matrix& m) { - typedef Eigen::Matrix matrix_t; + using matrix_t = Eigen::Matrix; check_nonzero_size("qr_thin_R", "m", m); Eigen::HouseholderQR qr(m.rows(), m.cols()); qr.compute(m); diff --git a/stan/math/prim/mat/fun/simplex_constrain.hpp b/stan/math/prim/mat/fun/simplex_constrain.hpp index e5ab094933e..55c1c7d3da9 100644 --- a/stan/math/prim/mat/fun/simplex_constrain.hpp +++ b/stan/math/prim/mat/fun/simplex_constrain.hpp @@ -31,7 +31,7 @@ Eigen::Matrix simplex_constrain( using Eigen::Dynamic; using Eigen::Matrix; using std::log; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; int Km1 = y.size(); Matrix x(Km1 + 1); @@ -65,7 +65,7 @@ Eigen::Matrix simplex_constrain( using Eigen::Matrix; using std::log; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; int Km1 = y.size(); // K = Km1 + 1 Matrix x(Km1 + 1); diff --git a/stan/math/prim/mat/fun/simplex_free.hpp b/stan/math/prim/mat/fun/simplex_free.hpp index 768436bd3d6..b55311bedf9 100644 --- a/stan/math/prim/mat/fun/simplex_free.hpp +++ b/stan/math/prim/mat/fun/simplex_free.hpp @@ -31,7 +31,7 @@ Eigen::Matrix simplex_free( using Eigen::Matrix; using std::log; - typedef typename index_type >::type size_type; + using size_type = typename index_type>::type; check_simplex("stan::math::simplex_free", "Simplex variable", x); int Km1 = x.size() - 1; diff --git a/stan/math/prim/mat/fun/sort_indices.hpp b/stan/math/prim/mat/fun/sort_indices.hpp index f212bbc04ba..5d970d7d8a8 100644 --- a/stan/math/prim/mat/fun/sort_indices.hpp +++ b/stan/math/prim/mat/fun/sort_indices.hpp @@ -61,7 +61,7 @@ class index_comparator { */ template std::vector sort_indices(const C& xs) { - typedef typename index_type::type idx_t; + using idx_t = typename index_type::type; idx_t size = xs.size(); std::vector idxs; idxs.resize(size); diff --git a/stan/math/prim/mat/fun/tail.hpp b/stan/math/prim/mat/fun/tail.hpp index b481ad94f88..69561779313 100644 --- a/stan/math/prim/mat/fun/tail.hpp +++ b/stan/math/prim/mat/fun/tail.hpp @@ -61,7 +61,7 @@ inline Eigen::Matrix tail( */ template std::vector tail(const std::vector& sv, size_t n) { - typedef typename index_type >::type idx_t; + using idx_t = typename index_type >::type; if (n != 0) { check_std_vector_index("tail", "n", sv, n); } diff --git a/stan/math/prim/mat/fun/typedefs.hpp b/stan/math/prim/mat/fun/typedefs.hpp index 8ace5f07efd..1ff4802995f 100644 --- a/stan/math/prim/mat/fun/typedefs.hpp +++ b/stan/math/prim/mat/fun/typedefs.hpp @@ -16,17 +16,17 @@ typedef index_type >::type /** * Type for matrix of double values. */ -typedef Eigen::Matrix matrix_d; +using matrix_d = Eigen::Matrix; /** * Type for (column) vector of double values. */ -typedef Eigen::Matrix vector_d; +using vector_d = Eigen::Matrix; /** * Type for (row) vector of double values. */ -typedef Eigen::Matrix row_vector_d; +using row_vector_d = Eigen::Matrix; } // namespace math } // namespace stan diff --git a/stan/math/prim/mat/functor/map_rect_mpi.hpp b/stan/math/prim/mat/functor/map_rect_mpi.hpp index b141f4b5bfd..71cb8ad65a3 100644 --- a/stan/math/prim/mat/functor/map_rect_mpi.hpp +++ b/stan/math/prim/mat/functor/map_rect_mpi.hpp @@ -25,8 +25,8 @@ map_rect_mpi( job_params, const std::vector>& x_r, const std::vector>& x_i, std::ostream* msgs = nullptr) { - typedef internal::map_rect_reduce ReduceF; - typedef internal::map_rect_combine CombineF; + using ReduceF = internal::map_rect_reduce; + using CombineF = internal::map_rect_combine; // whenever the cluster is already busy with some command we fall // back to serial execution (possible if map_rect calls are nested diff --git a/stan/math/prim/mat/meta/append_return_type.hpp b/stan/math/prim/mat/meta/append_return_type.hpp index d173eb5c824..02bb1591a3e 100644 --- a/stan/math/prim/mat/meta/append_return_type.hpp +++ b/stan/math/prim/mat/meta/append_return_type.hpp @@ -20,7 +20,7 @@ namespace math { */ template struct append_return_type { - typedef return_type_t type; + using type = return_type_t; }; /** @@ -34,7 +34,7 @@ struct append_return_type { */ template <> struct append_return_type { - typedef int type; + using type = int; }; /** @@ -53,7 +53,7 @@ struct append_return_type { */ template struct append_return_type, Eigen::Matrix > { - typedef typename Eigen::Matrix, R, C> type; + using type = typename Eigen::Matrix, R, C>; }; /** @@ -69,7 +69,7 @@ struct append_return_type, Eigen::Matrix > { */ template struct append_return_type, std::vector > { - typedef typename std::vector::type> type; + using type = typename std::vector::type>; }; } // namespace math } // namespace stan diff --git a/stan/math/prim/mat/meta/is_vector.hpp b/stan/math/prim/mat/meta/is_vector.hpp index cd848789547..b2668e5d235 100644 --- a/stan/math/prim/mat/meta/is_vector.hpp +++ b/stan/math/prim/mat/meta/is_vector.hpp @@ -12,17 +12,17 @@ namespace stan { template struct is_vector > { enum { value = 1 }; - typedef T type; + using type = T; }; template struct is_vector > { enum { value = 1 }; - typedef T type; + using type = T; }; template struct is_vector > { enum { value = 1 }; - typedef T type; + using type = T; }; } // namespace stan #endif diff --git a/stan/math/prim/mat/meta/operands_and_partials.hpp b/stan/math/prim/mat/meta/operands_and_partials.hpp index 6aaca072513..c8f8e2d963c 100644 --- a/stan/math/prim/mat/meta/operands_and_partials.hpp +++ b/stan/math/prim/mat/meta/operands_and_partials.hpp @@ -18,7 +18,7 @@ namespace internal { template class ops_partials_edge> { public: - typedef empty_broadcast_array> partials_t; + using partials_t = empty_broadcast_array>; partials_t partials_; empty_broadcast_array> partials_vec_; ops_partials_edge() {} @@ -37,7 +37,7 @@ class ops_partials_edge> { template class ops_partials_edge>> { public: - typedef empty_broadcast_array> partials_t; + using partials_t = empty_broadcast_array>; empty_broadcast_array> partials_vec_; ops_partials_edge() {} explicit ops_partials_edge( diff --git a/stan/math/prim/mat/meta/scalar_type.hpp b/stan/math/prim/mat/meta/scalar_type.hpp index 3af731a7344..effcd1a9ea3 100644 --- a/stan/math/prim/mat/meta/scalar_type.hpp +++ b/stan/math/prim/mat/meta/scalar_type.hpp @@ -15,7 +15,7 @@ namespace stan { */ template struct scalar_type > { - typedef typename scalar_type::type type; + using type = typename scalar_type::type; }; /** @@ -28,7 +28,7 @@ struct scalar_type > { */ template struct scalar_type > { - typedef typename scalar_type::type type; + using type = typename scalar_type::type; }; /** @@ -41,7 +41,7 @@ struct scalar_type > { */ template struct scalar_type&> { - typedef typename scalar_type::type type; + using type = typename scalar_type::type; }; /** @@ -54,7 +54,7 @@ struct scalar_type&> { */ template struct scalar_type&> { - typedef typename scalar_type::type type; + using type = typename scalar_type::type; }; /** @@ -65,7 +65,7 @@ struct scalar_type&> { */ template struct scalar_type > { - typedef typename scalar_type::type type; + using type = typename scalar_type::type; }; } // namespace stan #endif diff --git a/stan/math/prim/mat/meta/seq_view.hpp b/stan/math/prim/mat/meta/seq_view.hpp index d208e921039..8f50a204fd1 100644 --- a/stan/math/prim/mat/meta/seq_view.hpp +++ b/stan/math/prim/mat/meta/seq_view.hpp @@ -9,28 +9,28 @@ namespace math { template struct store_type { - typedef const T& type; + using type = const T &; }; template <> struct store_type { - typedef const double type; + using type = const double; }; template <> struct store_type { - typedef const int type; + using type = const int; }; template struct pass_type { - typedef const T& type; + using type = const T &; }; template <> struct pass_type { - typedef double type; + using type = double; }; template <> struct pass_type { - typedef int type; + using type = int; }; // S assignable to T diff --git a/stan/math/prim/mat/meta/value_type.hpp b/stan/math/prim/mat/meta/value_type.hpp index 076f89e421e..8f55208adc2 100644 --- a/stan/math/prim/mat/meta/value_type.hpp +++ b/stan/math/prim/mat/meta/value_type.hpp @@ -17,7 +17,7 @@ namespace math { */ template struct value_type > { - typedef T type; + using type = T; }; } // namespace math diff --git a/stan/math/prim/mat/prob/bernoulli_logit_glm_lpmf.hpp b/stan/math/prim/mat/prob/bernoulli_logit_glm_lpmf.hpp index bbf0ac5b965..5d3776b2823 100644 --- a/stan/math/prim/mat/prob/bernoulli_logit_glm_lpmf.hpp +++ b/stan/math/prim/mat/prob/bernoulli_logit_glm_lpmf.hpp @@ -47,7 +47,7 @@ template bernoulli_logit_glm_lpmf( const T_y &y, const T_x &x, const T_alpha &alpha, const T_beta &beta) { static const char *function = "bernoulli_logit_glm_lpmf"; - typedef partials_return_type_t T_partials_return; + using T_partials_return = partials_return_type_t; typedef typename std::conditional_t< is_vector::value, Eigen::Matrix, -1, 1>, partials_return_type_t> diff --git a/stan/math/prim/mat/prob/dirichlet_lpmf.hpp b/stan/math/prim/mat/prob/dirichlet_lpmf.hpp index 3866b9d2cf1..d3cced02416 100644 --- a/stan/math/prim/mat/prob/dirichlet_lpmf.hpp +++ b/stan/math/prim/mat/prob/dirichlet_lpmf.hpp @@ -50,8 +50,8 @@ return_type_t dirichlet_lpmf(const T_prob& theta, const T_prior_size& alpha) { static const char* function = "dirichlet_lpmf"; - typedef partials_return_type_t T_partials_return; - typedef typename Eigen::Matrix T_partials_vec; + using T_partials_return = partials_return_type_t; + using T_partials_vec = typename Eigen::Matrix; check_consistent_sizes(function, "probabilities", theta, "prior sample sizes", alpha); diff --git a/stan/math/prim/mat/prob/lkj_corr_cholesky_lpdf.hpp b/stan/math/prim/mat/prob/lkj_corr_cholesky_lpdf.hpp index ab24fdfa1e8..ff15b7ba0e0 100644 --- a/stan/math/prim/mat/prob/lkj_corr_cholesky_lpdf.hpp +++ b/stan/math/prim/mat/prob/lkj_corr_cholesky_lpdf.hpp @@ -20,7 +20,7 @@ return_type_t lkj_corr_cholesky_lpdf( using boost::math::tools::promote_args; - typedef return_type_t lp_ret; + using lp_ret = return_type_t; lp_ret lp(0.0); check_positive(function, "Shape parameter", eta); check_lower_triangular(function, "Random variable", L); diff --git a/stan/math/prim/mat/prob/multi_gp_cholesky_lpdf.hpp b/stan/math/prim/mat/prob/multi_gp_cholesky_lpdf.hpp index 265db5df237..c201364b9ff 100644 --- a/stan/math/prim/mat/prob/multi_gp_cholesky_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_gp_cholesky_lpdf.hpp @@ -40,7 +40,7 @@ return_type_t multi_gp_cholesky_lpdf( const Eigen::Matrix& L, const Eigen::Matrix& w) { static const char* function = "multi_gp_cholesky_lpdf"; - typedef return_type_t T_lp; + using T_lp = return_type_t; check_size_match(function, "Size of random variable (rows y)", y.rows(), "Size of kernel scales (w)", w.size()); diff --git a/stan/math/prim/mat/prob/multi_gp_lpdf.hpp b/stan/math/prim/mat/prob/multi_gp_lpdf.hpp index 65712fad205..0bbdfacb0c4 100644 --- a/stan/math/prim/mat/prob/multi_gp_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_gp_lpdf.hpp @@ -40,7 +40,7 @@ return_type_t multi_gp_lpdf( const Eigen::Matrix& Sigma, const Eigen::Matrix& w) { static const char* function = "multi_gp_lpdf"; - typedef return_type_t T_lp; + using T_lp = return_type_t; T_lp lp(0.0); check_positive(function, "Kernel rows", Sigma.rows()); diff --git a/stan/math/prim/mat/prob/multi_normal_cholesky_lpdf.hpp b/stan/math/prim/mat/prob/multi_normal_cholesky_lpdf.hpp index 273179b5578..29dcb510bbc 100644 --- a/stan/math/prim/mat/prob/multi_normal_cholesky_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_normal_cholesky_lpdf.hpp @@ -41,12 +41,12 @@ template return_type_t multi_normal_cholesky_lpdf( const T_y& y, const T_loc& mu, const T_covar& L) { static const char* function = "multi_normal_cholesky_lpdf"; - typedef typename scalar_type::type T_covar_elem; - typedef return_type_t T_return; - typedef partials_return_type_t T_partials_return; + using T_covar_elem = typename scalar_type::type; + using T_return = return_type_t; + using T_partials_return = partials_return_type_t; typedef Eigen::Matrix matrix_partials_t; - typedef Eigen::Matrix vector_partials_t; + using vector_partials_t = Eigen::Matrix; typedef Eigen::Matrix row_vector_partials_t; diff --git a/stan/math/prim/mat/prob/multi_normal_lpdf.hpp b/stan/math/prim/mat/prob/multi_normal_lpdf.hpp index 16a6e374e0d..fc78bf4c3d1 100644 --- a/stan/math/prim/mat/prob/multi_normal_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_normal_lpdf.hpp @@ -21,8 +21,8 @@ return_type_t multi_normal_lpdf(const T_y& y, const T_loc& mu, const T_covar& Sigma) { static const char* function = "multi_normal_lpdf"; - typedef typename scalar_type::type T_covar_elem; - typedef return_type_t lp_type; + using T_covar_elem = typename scalar_type::type; + using lp_type = return_type_t; using Eigen::Dynamic; diff --git a/stan/math/prim/mat/prob/multi_normal_prec_lpdf.hpp b/stan/math/prim/mat/prob/multi_normal_prec_lpdf.hpp index e7436da969f..e58df44a094 100644 --- a/stan/math/prim/mat/prob/multi_normal_prec_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_normal_prec_lpdf.hpp @@ -21,8 +21,8 @@ template return_type_t multi_normal_prec_lpdf( const T_y& y, const T_loc& mu, const T_covar& Sigma) { static const char* function = "multi_normal_prec_lpdf"; - typedef typename scalar_type::type T_covar_elem; - typedef return_type_t lp_type; + using T_covar_elem = typename scalar_type::type; + using lp_type = return_type_t; check_positive(function, "Precision matrix rows", Sigma.rows()); check_symmetric(function, "Precision matrix", Sigma); diff --git a/stan/math/prim/mat/prob/multi_student_t_lpdf.hpp b/stan/math/prim/mat/prob/multi_student_t_lpdf.hpp index 51869ed33ba..d504bb41563 100644 --- a/stan/math/prim/mat/prob/multi_student_t_lpdf.hpp +++ b/stan/math/prim/mat/prob/multi_student_t_lpdf.hpp @@ -32,8 +32,8 @@ return_type_t multi_student_t_lpdf( const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& Sigma) { static const char* function = "multi_student_t"; using std::log; - typedef typename scalar_type::type T_scale_elem; - typedef return_type_t lp_type; + using T_scale_elem = typename scalar_type::type; + using lp_type = return_type_t; check_not_nan(function, "Degrees of freedom parameter", nu); check_positive(function, "Degrees of freedom parameter", nu); diff --git a/stan/math/prim/mat/prob/ordered_logistic_lpmf.hpp b/stan/math/prim/mat/prob/ordered_logistic_lpmf.hpp index 10ee376f787..62abc917614 100644 --- a/stan/math/prim/mat/prob/ordered_logistic_lpmf.hpp +++ b/stan/math/prim/mat/prob/ordered_logistic_lpmf.hpp @@ -74,8 +74,8 @@ return_type_t ordered_logistic_lpmf(const T_y& y, const T_cut& c) { static const char* function = "ordered_logistic"; - typedef partials_return_type_t T_partials_return; - typedef typename Eigen::Matrix T_partials_vec; + using T_partials_return = partials_return_type_t; + using T_partials_vec = typename Eigen::Matrix; scalar_seq_view lam_vec(lambda); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/mat/prob/poisson_log_glm_lpmf.hpp b/stan/math/prim/mat/prob/poisson_log_glm_lpmf.hpp index ffce3b853ca..7360a140b1f 100644 --- a/stan/math/prim/mat/prob/poisson_log_glm_lpmf.hpp +++ b/stan/math/prim/mat/prob/poisson_log_glm_lpmf.hpp @@ -48,7 +48,7 @@ return_type_t poisson_log_glm_lpmf(const T_y& y, const T_alpha& alpha, const T_beta& beta) { static const char* function = "poisson_log_glm_lpmf"; - typedef partials_return_type_t T_partials_return; + using T_partials_return = partials_return_type_t; typedef typename std::conditional_t< is_vector::value, Eigen::Array, -1, 1>, diff --git a/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp b/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp index 864a45cddeb..e36fef6a8b1 100644 --- a/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp +++ b/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp @@ -36,7 +36,7 @@ struct apply_scalar_unary { /** * Type of underlying scalar for the matrix type T. */ - typedef typename Eigen::internal::traits::Scalar scalar_t; + using scalar_t = typename Eigen::internal::traits::Scalar; /** * Return type for applying the function elementwise to a matrix @@ -70,7 +70,7 @@ struct apply_scalar_unary { /** * The return type, double. */ - typedef double return_t; + using return_t = double; /** * Apply the function specified by F to the specified argument. @@ -97,7 +97,7 @@ struct apply_scalar_unary { /** * The return type, double. */ - typedef double return_t; + using return_t = double; /** * Apply the function specified by F to the specified argument. diff --git a/stan/math/prim/scal/fun/common_type.hpp b/stan/math/prim/scal/fun/common_type.hpp index 41e9a67af68..32388944681 100644 --- a/stan/math/prim/scal/fun/common_type.hpp +++ b/stan/math/prim/scal/fun/common_type.hpp @@ -23,7 +23,7 @@ namespace math { */ template struct common_type { - typedef return_type_t type; + using type = return_type_t; }; } // namespace math diff --git a/stan/math/prim/scal/fun/fdim.hpp b/stan/math/prim/scal/fun/fdim.hpp index f0776feb1ca..1c5e6db7ada 100644 --- a/stan/math/prim/scal/fun/fdim.hpp +++ b/stan/math/prim/scal/fun/fdim.hpp @@ -23,7 +23,7 @@ namespace math { */ template inline return_type_t fdim(T1 x, T2 y) { - typedef return_type_t return_t; + using return_t = return_type_t; using std::numeric_limits; if (is_any_nan(x, y)) { return NOT_A_NUMBER; diff --git a/stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp b/stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp index ae22af892d4..4dd837077d5 100644 --- a/stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp +++ b/stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp @@ -52,7 +52,7 @@ return_type_t grad_reg_inc_gamma(T1 a, T2 z, T1 g, T1 dig, using std::exp; using std::fabs; using std::log; - typedef return_type_t TP; + using TP = return_type_t; if (is_any_nan(a, z, g, dig)) { return std::numeric_limits::quiet_NaN(); diff --git a/stan/math/prim/scal/fun/grad_reg_lower_inc_gamma.hpp b/stan/math/prim/scal/fun/grad_reg_lower_inc_gamma.hpp index d12af3a94c9..2cc194e2f2a 100644 --- a/stan/math/prim/scal/fun/grad_reg_lower_inc_gamma.hpp +++ b/stan/math/prim/scal/fun/grad_reg_lower_inc_gamma.hpp @@ -109,7 +109,7 @@ return_type_t grad_reg_lower_inc_gamma(const T1& a, const T2& z, using std::exp; using std::log; using std::pow; - typedef return_type_t TP; + using TP = return_type_t; if (is_any_nan(a, z)) { return std::numeric_limits::quiet_NaN(); diff --git a/stan/math/prim/scal/fun/log_modified_bessel_first_kind.hpp b/stan/math/prim/scal/fun/log_modified_bessel_first_kind.hpp index 079c3cbc825..cc5788f6e92 100644 --- a/stan/math/prim/scal/fun/log_modified_bessel_first_kind.hpp +++ b/stan/math/prim/scal/fun/log_modified_bessel_first_kind.hpp @@ -50,7 +50,7 @@ inline return_type_t log_modified_bessel_first_kind( using std::log; using std::sqrt; - typedef return_type_t T; + using T = return_type_t; if (z == 0) { if (v == 0) { diff --git a/stan/math/prim/scal/fun/promote_scalar_type.hpp b/stan/math/prim/scal/fun/promote_scalar_type.hpp index ee3edf210c2..1f8cc7841ef 100644 --- a/stan/math/prim/scal/fun/promote_scalar_type.hpp +++ b/stan/math/prim/scal/fun/promote_scalar_type.hpp @@ -17,7 +17,7 @@ struct promote_scalar_type { /** * The promoted type. */ - typedef T type; + using type = T; }; } // namespace math diff --git a/stan/math/prim/scal/meta/StdVectorBuilder.hpp b/stan/math/prim/scal/meta/StdVectorBuilder.hpp index 22f3af93095..9a3249e5523 100644 --- a/stan/math/prim/scal/meta/StdVectorBuilder.hpp +++ b/stan/math/prim/scal/meta/StdVectorBuilder.hpp @@ -39,7 +39,7 @@ class StdVectorBuilder { helper; public: - typedef typename helper::type type; + using type = typename helper::type; helper a; explicit StdVectorBuilder(size_t n) : a(n) {} diff --git a/stan/math/prim/scal/meta/VectorBuilder.hpp b/stan/math/prim/scal/meta/VectorBuilder.hpp index 8dde92059cd..ad8ca9bd950 100644 --- a/stan/math/prim/scal/meta/VectorBuilder.hpp +++ b/stan/math/prim/scal/meta/VectorBuilder.hpp @@ -32,7 +32,7 @@ class VectorBuilder { helper; public: - typedef typename helper::type type; + using type = typename helper::type; helper a; explicit VectorBuilder(size_t n) : a(n) {} diff --git a/stan/math/prim/scal/meta/VectorBuilderHelper.hpp b/stan/math/prim/scal/meta/VectorBuilderHelper.hpp index a79e0f6a0f6..b1cdb0cc68c 100644 --- a/stan/math/prim/scal/meta/VectorBuilderHelper.hpp +++ b/stan/math/prim/scal/meta/VectorBuilderHelper.hpp @@ -29,7 +29,7 @@ class VectorBuilderHelper { throw std::logic_error("used is false. this should never be called"); } - typedef T1 type; + using type = T1; inline type& data() { throw std::logic_error("used is false. this should never be called"); @@ -45,7 +45,7 @@ class VectorBuilderHelper { explicit VectorBuilderHelper(size_t /* n */) : x_(0) {} T1& operator[](size_t /* i */) { return x_; } - typedef T1 type; + using type = T1; inline type& data() { return x_; } }; diff --git a/stan/math/prim/scal/meta/child_type.hpp b/stan/math/prim/scal/meta/child_type.hpp index 9788cfb1cad..741d0c5255f 100644 --- a/stan/math/prim/scal/meta/child_type.hpp +++ b/stan/math/prim/scal/meta/child_type.hpp @@ -16,7 +16,7 @@ namespace math { template struct child_type { - typedef double type; + using type = double; }; /** @@ -31,7 +31,7 @@ struct child_type { template