Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clang tidy cleanup and using std algorithms #1373

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use auto in gp_exp_quad_cov, add rule of 5 to accumulator, and pass b…
…y value for log_sum_exp's accumulator with max_val
  • Loading branch information
SteveBronder committed Nov 3, 2019
commit 32663498befabd3272f13fd5e1b67af7e1cfa924
2 changes: 1 addition & 1 deletion stan/math/prim/arr/fun/log_sum_exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ inline double log_sum_exp(const std::vector<double>& x) {
double max_val = *std::max_element(x.begin(), x.end());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very neat!

double sum = std::accumulate(
x.begin(), x.end(), 0.0,
[&max_val](auto& acc, auto&& x_i) { return acc + exp(x_i - max_val); });
[max_val](auto& acc, auto&& x_i) { return acc + exp(x_i - max_val); });
return max_val + log(sum);
}

Expand Down
4 changes: 4 additions & 0 deletions stan/math/prim/mat/fun/accumulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class accumulator {
* Destroy an accumulator.
*/
~accumulator() = default;
accumulator(const accumulator& other) = default;
accumulator(accumulator&& other) = default;
accumulator& operator=(const accumulator& other) = default;
accumulator& operator=(accumulator&& other) = default;

/**
* Add the specified arithmetic type value to the buffer after
Expand Down
36 changes: 14 additions & 22 deletions stan/math/prim/mat/fun/gp_exp_quad_cov.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ namespace internal {
* @return squared distance
*/
template <typename T_x, typename T_sigma, typename T_l>
inline typename Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
Eigen::Dynamic>
inline auto
gp_exp_quad_cov(const std::vector<T_x> &x, const T_sigma &sigma_sq,
const T_l &neg_half_inv_l_sq) {
using std::exp;
Expand Down Expand Up @@ -70,8 +69,7 @@ gp_exp_quad_cov(const std::vector<T_x> &x, const T_sigma &sigma_sq,
* x is nan or infinite
*/
template <typename T_x, typename T_sigma>
inline typename Eigen::Matrix<return_type_t<T_x, T_sigma>, Eigen::Dynamic,
Eigen::Dynamic>
inline auto
gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x, -1, 1>> &x,
const T_sigma &sigma_sq) {
using std::exp;
Expand Down Expand Up @@ -107,8 +105,7 @@ gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x, -1, 1>> &x,
* @return squared distance
*/
template <typename T_x1, typename T_x2, typename T_sigma, typename T_l>
inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l>,
Eigen::Dynamic, Eigen::Dynamic>
inline auto
gp_exp_quad_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2,
const T_sigma &sigma_sq, const T_l &neg_half_inv_l_sq) {
using std::exp;
Expand Down Expand Up @@ -140,8 +137,7 @@ gp_exp_quad_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2,
* @return squared distance
*/
template <typename T_x1, typename T_x2, typename T_s>
inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_s>, Eigen::Dynamic,
Eigen::Dynamic>
inline auto
gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x1, -1, 1>> &x1,
const std::vector<Eigen::Matrix<T_x2, -1, 1>> &x2,
const T_s &sigma_sq) {
Expand Down Expand Up @@ -173,8 +169,7 @@ gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x1, -1, 1>> &x1,
* x is nan or infinite
*/
template <typename T_x, typename T_sigma, typename T_l>
inline typename Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
Eigen::Dynamic>
inline auto
gp_exp_quad_cov(const std::vector<T_x> &x, const T_sigma &sigma,
const T_l &length_scale) {
check_positive("gp_exp_quad_cov", "magnitude", sigma);
Expand Down Expand Up @@ -213,8 +208,7 @@ gp_exp_quad_cov(const std::vector<T_x> &x, const T_sigma &sigma,
* x is nan or infinite
*/
template <typename T_x, typename T_sigma, typename T_l>
inline typename Eigen::Matrix<return_type_t<T_x, T_sigma, T_l>, Eigen::Dynamic,
Eigen::Dynamic>
inline auto
gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x, -1, 1>> &x,
const T_sigma &sigma, const std::vector<T_l> &length_scale) {
check_positive_finite("gp_exp_quad_cov", "magnitude", sigma);
Expand Down Expand Up @@ -256,8 +250,7 @@ gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x, -1, 1>> &x,
* x is nan or infinite
*/
template <typename T_x1, typename T_x2, typename T_sigma, typename T_l>
inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_sigma, T_l>,
Eigen::Dynamic, Eigen::Dynamic>
inline auto
gp_exp_quad_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2,
const T_sigma &sigma, const T_l &length_scale) {
const char *function_name = "gp_exp_quad_cov";
Expand Down Expand Up @@ -305,8 +298,7 @@ gp_exp_quad_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2,
* x is nan or infinite
*/
template <typename T_x1, typename T_x2, typename T_s, typename T_l>
inline typename Eigen::Matrix<return_type_t<T_x1, T_x2, T_s, T_l>,
Eigen::Dynamic, Eigen::Dynamic>
inline auto
gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x1, -1, 1>> &x1,
const std::vector<Eigen::Matrix<T_x2, -1, 1>> &x2,
const T_s &sigma, const std::vector<T_l> &length_scale) {
Expand Down Expand Up @@ -353,7 +345,7 @@ gp_exp_quad_cov(const std::vector<Eigen::Matrix<T_x1, -1, 1>> &x1,
* x is nan or infinite
*/
template <>
inline Eigen::MatrixXd gp_exp_quad_cov(const std::vector<double> &x,
inline auto gp_exp_quad_cov(const std::vector<double> &x,
const double &sigma,
const double &length_scale) {
const char *function_name = "gp_exp_quad_cov";
Expand Down Expand Up @@ -396,7 +388,7 @@ inline Eigen::MatrixXd gp_exp_quad_cov(const std::vector<double> &x,
* x is nan or infinite
*/
template <>
inline Eigen::MatrixXd gp_exp_quad_cov(const std::vector<Eigen::VectorXd> &x,
inline auto gp_exp_quad_cov(const std::vector<Eigen::VectorXd> &x,
const double &sigma,
const double &length_scale) {
const char *function_name = "gp_exp_quad_cov";
Expand Down Expand Up @@ -440,7 +432,7 @@ inline Eigen::MatrixXd gp_exp_quad_cov(const std::vector<Eigen::VectorXd> &x,
* x is nan or infinite
*/
template <>
inline Eigen::MatrixXd gp_exp_quad_cov(
inline auto gp_exp_quad_cov(
const std::vector<Eigen::VectorXd> &x, const double &sigma,
const std::vector<double> &length_scale) {
const char *function_name = "gp_exp_quad_cov";
Expand Down Expand Up @@ -487,7 +479,7 @@ inline Eigen::MatrixXd gp_exp_quad_cov(
* x is nan or infinite
*/
template <>
inline typename Eigen::MatrixXd gp_exp_quad_cov(const std::vector<double> &x1,
inline auto gp_exp_quad_cov(const std::vector<double> &x1,
const std::vector<double> &x2,
const double &sigma,
const double &length_scale) {
Expand Down Expand Up @@ -538,7 +530,7 @@ inline typename Eigen::MatrixXd gp_exp_quad_cov(const std::vector<double> &x1,
* x is nan or infinite
*/
template <>
inline typename Eigen::MatrixXd gp_exp_quad_cov(
inline auto gp_exp_quad_cov(
const std::vector<Eigen::VectorXd> &x1,
const std::vector<Eigen::VectorXd> &x2, const double &sigma,
const double &length_scale) {
Expand Down Expand Up @@ -594,7 +586,7 @@ inline typename Eigen::MatrixXd gp_exp_quad_cov(
* x is nan or infinite
*/
template <>
inline typename Eigen::MatrixXd gp_exp_quad_cov(
inline auto gp_exp_quad_cov(
const std::vector<Eigen::VectorXd> &x1,
const std::vector<Eigen::VectorXd> &x2, const double &sigma,
const std::vector<double> &length_scale) {
Expand Down
1 change: 1 addition & 0 deletions stan/math/rev/arr/fun/log_sum_exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace stan {
namespace math {

namespace internal {
// If we make a vectorized value_of we can just use the prim version here
inline double log_sum_exp_as_double(const std::vector<var>& x) {
using std::exp;
using std::log;
Expand Down
2 changes: 1 addition & 1 deletion stan/math/rev/mat/fun/gp_exp_quad_cov.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class gp_exp_quad_cov_vari<T_x, double, T_l> : public vari {
* @throw std::domain_error if sigma <= 0, l <= 0, or
* x is nan or infinite
*/
template <typename T_x, typename = enable_if_arithmetic<scalar_type_t<T_x>>>
template <typename T_x, typename = require_arithmetic_t<scalar_type_t<T_x>>>
inline Eigen::Matrix<var, -1, -1> gp_exp_quad_cov(const std::vector<T_x> &x,
const var &sigma,
const var &length_scale) {
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.