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
cleanups for promote_elements
  • Loading branch information
SteveBronder committed Oct 8, 2019
commit bdadd8e75d689614c083402115eb18c513a5cbb2
7 changes: 3 additions & 4 deletions stan/math/prim/arr/fun/promote_elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +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({u.data(), u.data() + u.size()});
return t;
return {u.begin(), u.end()};
}
};

Expand All @@ -40,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
6 changes: 1 addition & 5 deletions stan/math/prim/mat/fun/promote_elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ struct promote_elements<Eigen::Matrix<T, R, C>, Eigen::Matrix<S, R, C> > {
*/
inline static Eigen::Matrix<T, R, C> promote(
const Eigen::Matrix<S, R, C>& u) {
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> t(u.rows(), u.cols());
for (int i = 0; i < u.size(); ++i) {
t(i) = promote_elements<T, S>::promote(u(i));
}
return t;
return u.template cast<T>();
}
};

Expand Down
12 changes: 7 additions & 5 deletions test/unit/math/rev/mat/fun/dot_product2_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,14 +996,16 @@ TEST(AgradRevMatrix, promoteElts) {
EXPECT_FLOAT_EQ(5.0, c.val());

vector<double> x(5);
for (int i = 0; i < 5; ++i)
x[i] = i * i;
vector<double> y = promote_common<vector<double>, vector<double> >(x);
for (int i = 0; i < 5; ++i) {
x[i] = i * i;
}
vector<double> y = promote_common<vector<double>, vector<double>>(x);
EXPECT_EQ(5U, y.size());
for (int i = 0; i < 5; ++i)
for (int i = 0; i < 5; ++i) {
EXPECT_FLOAT_EQ(x[i], y[i]);
}
std::vector<var> z
= promote_common<std::vector<double>, std::vector<var> >(x);
= promote_common<std::vector<double>, std::vector<var>>(x);
EXPECT_EQ(5U, z.size());
for (int i = 0; i < 5; ++i)
EXPECT_FLOAT_EQ(x[i], z[i].val());
Expand Down