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 all commits
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
4 changes: 2 additions & 2 deletions stan/math/opencl/opencl_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class opencl_context {
int device_id = 0;

msg << "Number of Platforms: " << all_platforms.size() << "\n";
for (auto plat_iter : all_platforms) {
for (const auto& plat_iter : all_platforms) {
cl::Platform platform(plat_iter);

msg << "Platform ID: " << platform_id++ << "\n";
Expand All @@ -279,7 +279,7 @@ class opencl_context {
std::vector<cl::Device> all_devices;
platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices);

for (auto device_iter : all_devices) {
for (const auto& device_iter : all_devices) {
cl::Device device(device_iter);

msg << "\tDevice " << device_id++ << ": "
Expand Down
7 changes: 2 additions & 5 deletions stan/math/prim/arr/fun/dot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@

#include <stan/math/prim/meta.hpp>
#include <vector>
#include <numeric>
#include <cstddef>

namespace stan {
namespace math {

inline double dot(const std::vector<double>& x, const std::vector<double>& y) {
double sum = 0.0;
for (size_t i = 0; i < x.size(); ++i) {
sum += x[i] * y[i];
}
return sum;
return std::inner_product(x.begin(), x.end(), y.begin(), 0.0);
}

} // namespace math
Expand Down
7 changes: 2 additions & 5 deletions stan/math/prim/arr/fun/dot_self.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@

#include <stan/math/prim/meta.hpp>
#include <vector>
#include <numeric>
#include <cstddef>

namespace stan {
namespace math {

inline double dot_self(const std::vector<double>& x) {
double sum = 0.0;
for (double i : x) {
sum += i * i;
}
return sum;
return std::inner_product(x.begin(), x.end(), x.begin(), 0.0);
}

} // namespace math
Expand Down
26 changes: 9 additions & 17 deletions stan/math/prim/arr/fun/log_sum_exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define STAN_MATH_PRIM_ARR_FUN_LOG_SUM_EXP_HPP

#include <stan/math/prim/meta.hpp>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <limits>
#include <vector>
#include <cmath>
#include <cstdlib>

namespace stan {
namespace math {
Expand All @@ -27,21 +29,11 @@ inline double log_sum_exp(const std::vector<double>& x) {
using std::exp;
using std::log;
using std::numeric_limits;
double max = -numeric_limits<double>::infinity();
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<double>::infinity()) {
sum += exp(x[ii] - max);
}
}

return max + log(sum);
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); });
return max_val + log(sum);
}

} // namespace math
Expand Down
11 changes: 3 additions & 8 deletions stan/math/prim/arr/fun/promote_elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,15 @@ namespace math {
* @tparam S type of input elements, must be assignable to T
*/
template <typename T, typename S>
struct promote_elements<std::vector<T>, std::vector<S> > {
struct promote_elements<std::vector<T>, std::vector<S>> {
/**
* Return input vector of type S as vector of type T.
*
* @param u vector of type S, assignable to type T
* @returns vector of type T
*/
inline static std::vector<T> promote(const std::vector<S>& u) {
std::vector<T> t;
t.reserve(u.size());
for (size_t i = 0; i < u.size(); ++i) {
t.push_back(promote_elements<T, S>::promote(u[i]));
}
return t;
return {u.begin(), u.end()};
}
};

Expand All @@ -44,7 +39,7 @@ struct promote_elements<std::vector<T>, std::vector<S> > {
* @tparam T type of elements
*/
template <typename T>
struct promote_elements<std::vector<T>, std::vector<T> > {
struct promote_elements<std::vector<T>, std::vector<T>> {
/**
* Return input vector.
*
Expand Down
1 change: 1 addition & 0 deletions stan/math/prim/arr/fun/sum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stan/math/prim/meta.hpp>
#include <cstddef>
#include <vector>
#include <algorithm>
#include <numeric>

namespace stan {
Expand Down
6 changes: 5 additions & 1 deletion stan/math/prim/mat/fun/accumulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ class accumulator {
/**
* Destroy an accumulator.
*/
~accumulator() {}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is there a reason for defining the accumulator destructor as empty here? tmk this still calls the destructor for all the accumulates members

Copy link
Contributor

Choose a reason for hiding this comment

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

This one is OK to leave as default---as is, it's not virtual and breaks the rule of 3(5).

~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
Loading