Skip to content

Commit

Permalink
Merge pull request #1340 from stan-dev/refactor/clang-tidy-cleanup
Browse files Browse the repository at this point in the history
Refactor/clang tidy cleanup
  • Loading branch information
rok-cesnovar authored Sep 9, 2019
2 parents 54277ff + 028ab67 commit d4e9332
Show file tree
Hide file tree
Showing 537 changed files with 4,121 additions and 2,444 deletions.
3 changes: 2 additions & 1 deletion stan/math/fwd/arr/fun/log_sum_exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ template <typename T>
fvar<T> log_sum_exp(const std::vector<fvar<T> >& v) {
using std::exp;
std::vector<T> 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) {
Expand Down
3 changes: 2 additions & 1 deletion stan/math/fwd/arr/fun/sum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ namespace math {
*/
template <typename T>
inline fvar<T> sum(const std::vector<fvar<T> >& m) {
if (m.size() == 0)
if (m.size() == 0) {
return 0.0;
}
std::vector<T> vals(m.size());
std::vector<T> tans(m.size());
for (size_t i = 0; i < m.size(); ++i) {
Expand Down
6 changes: 4 additions & 2 deletions stan/math/fwd/arr/fun/to_fvar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ namespace math {
template <typename T>
inline std::vector<fvar<T>> to_fvar(const std::vector<T>& v) {
std::vector<fvar<T>> 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;
}

template <typename T>
inline std::vector<fvar<T>> to_fvar(const std::vector<T>& v,
const std::vector<T>& d) {
std::vector<fvar<T>> x(v.size());
for (size_t i = 0; i < v.size(); ++i)
for (size_t i = 0; i < v.size(); ++i) {
x[i] = fvar<T>(v[i], d[i]);
}
return x;
}

Expand Down
12 changes: 8 additions & 4 deletions stan/math/fwd/core/fvar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,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;
}
}

/**
Expand All @@ -104,10 +105,12 @@ struct fvar {
*/
template <typename V>
fvar(const V& v,
typename std::enable_if<ad_promotable<V, T>::value>::type* dummy = 0)
typename std::enable_if<ad_promotable<V, T>::value>::type* dummy
= nullptr)
: val_(v), d_(0.0) {
if (unlikely(is_nan(v)))
if (unlikely(is_nan(v))) {
d_ = v;
}
}

/**
Expand All @@ -123,8 +126,9 @@ struct fvar {
*/
template <typename V, typename D>
fvar(const V& v, const D& d) : val_(v), d_(d) {
if (unlikely(is_nan(v)))
if (unlikely(is_nan(v))) {
d_ = v;
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions stan/math/fwd/mat/fun/Eigen_NumTraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct NumTraits<stan::math::fvar<T>> : GenericNumTraits<stan::math::fvar<T>> {
*/
template <typename T, typename BinaryOp>
struct ScalarBinaryOpTraits<stan::math::fvar<T>, double, BinaryOp> {
typedef stan::math::fvar<T> ReturnType;
using ReturnType = stan::math::fvar<T>;
};

/**
Expand All @@ -67,7 +67,7 @@ struct ScalarBinaryOpTraits<stan::math::fvar<T>, double, BinaryOp> {
*/
template <typename T, typename BinaryOp>
struct ScalarBinaryOpTraits<double, stan::math::fvar<T>, BinaryOp> {
typedef stan::math::fvar<T> ReturnType;
using ReturnType = stan::math::fvar<T>;
};

} // namespace Eigen
Expand Down
3 changes: 2 additions & 1 deletion stan/math/fwd/mat/fun/crossprod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ namespace math {
template <typename T, int R, int C>
inline Eigen::Matrix<fvar<T>, C, C> crossprod(
const Eigen::Matrix<fvar<T>, R, C>& m) {
if (m.rows() == 0)
if (m.rows() == 0) {
return Eigen::Matrix<fvar<T>, C, C>(0, 0);
}
return multiply(transpose(m), m);
}

Expand Down
9 changes: 6 additions & 3 deletions stan/math/fwd/mat/fun/divide.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ inline Eigen::Matrix<fvar<T>, R, C> divide(
const Eigen::Matrix<fvar<T>, R, C>& v, const fvar<T>& c) {
Eigen::Matrix<fvar<T>, 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;
}
Expand All @@ -23,8 +24,9 @@ inline Eigen::Matrix<fvar<T>, R, C> divide(
const Eigen::Matrix<fvar<T>, R, C>& v, double c) {
Eigen::Matrix<fvar<T>, 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;
}
Expand All @@ -34,8 +36,9 @@ inline Eigen::Matrix<fvar<T>, R, C> divide(const Eigen::Matrix<double, R, C>& v,
const fvar<T>& c) {
Eigen::Matrix<fvar<T>, 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;
}
Expand Down
36 changes: 24 additions & 12 deletions stan/math/fwd/mat/fun/dot_product.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ inline fvar<T> dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
check_matching_sizes("dot_product", "v1", v1, "v2", v2);

fvar<T> 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;
}

Expand All @@ -31,8 +32,9 @@ inline fvar<T> dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
check_matching_sizes("dot_product", "v1", v1, "v2", v2);

fvar<T> 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;
}

Expand All @@ -44,8 +46,9 @@ inline fvar<T> dot_product(const Eigen::Matrix<double, R1, C1>& v1,
check_matching_sizes("dot_product", "v1", v1, "v2", v2);

fvar<T> 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;
}

Expand All @@ -57,8 +60,9 @@ inline fvar<T> dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
check_vector("dot_product", "v2", v2);

fvar<T> 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;
}

Expand All @@ -70,8 +74,9 @@ inline fvar<T> dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
check_vector("dot_product", "v2", v2);

fvar<T> 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;
}

Expand All @@ -83,8 +88,9 @@ inline fvar<T> dot_product(const Eigen::Matrix<double, R1, C1>& v1,
check_vector("dot_product", "v2", v2);

fvar<T> 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;
}

Expand All @@ -93,8 +99,9 @@ inline fvar<T> dot_product(const std::vector<fvar<T> >& v1,
const std::vector<fvar<T> >& v2) {
check_matching_sizes("dot_product", "v1", v1, "v2", v2);
fvar<T> 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;
}

Expand All @@ -103,8 +110,9 @@ inline fvar<T> dot_product(const std::vector<double>& v1,
const std::vector<fvar<T> >& v2) {
check_matching_sizes("dot_product", "v1", v1, "v2", v2);
fvar<T> 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;
}

Expand All @@ -113,35 +121,39 @@ inline fvar<T> dot_product(const std::vector<fvar<T> >& v1,
const std::vector<double>& v2) {
check_matching_sizes("dot_product", "v1", v1, "v2", v2);
fvar<T> 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;
}

template <typename T>
inline fvar<T> dot_product(const std::vector<fvar<T> >& v1,
const std::vector<fvar<T> >& v2, size_type& length) {
fvar<T> 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;
}

template <typename T>
inline fvar<T> dot_product(const std::vector<double>& v1,
const std::vector<fvar<T> >& v2, size_type& length) {
fvar<T> 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;
}

template <typename T>
inline fvar<T> dot_product(const std::vector<fvar<T> >& v1,
const std::vector<double>& v2, size_type& length) {
fvar<T> 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;
}

Expand Down
8 changes: 5 additions & 3 deletions stan/math/fwd/mat/fun/log_softmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ inline Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1> log_softmax(
using Eigen::Matrix;

Matrix<T, Dynamic, 1> 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<T, Dynamic, 1> softmax_alpha_t = softmax(alpha_t);
Matrix<T, Dynamic, 1> log_softmax_alpha_t = log_softmax(alpha_t);
Expand All @@ -33,11 +34,12 @@ inline Eigen::Matrix<fvar<T>, 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;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion stan/math/fwd/mat/fun/mdivide_left_ldlt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ inline Eigen::Matrix<fvar<T2>, R1, C2> mdivide_left_ldlt(

Eigen::Matrix<T2, R2, C2> b_val(b.rows(), b.cols());
Eigen::Matrix<T2, R2, C2> 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));
}
Expand Down
9 changes: 6 additions & 3 deletions stan/math/fwd/mat/fun/multiply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ inline Eigen::Matrix<fvar<T>, R1, C1> multiply(
const Eigen::Matrix<fvar<T>, R1, C1>& m, const fvar<T>& c) {
Eigen::Matrix<fvar<T>, 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;
}
Expand All @@ -26,8 +27,9 @@ inline Eigen::Matrix<fvar<T>, R2, C2> multiply(
const Eigen::Matrix<fvar<T>, R2, C2>& m, double c) {
Eigen::Matrix<fvar<T>, 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;
}
Expand All @@ -37,8 +39,9 @@ inline Eigen::Matrix<fvar<T>, R1, C1> multiply(
const Eigen::Matrix<double, R1, C1>& m, const fvar<T>& c) {
Eigen::Matrix<fvar<T>, 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;
}
Expand Down
6 changes: 4 additions & 2 deletions stan/math/fwd/mat/fun/multiply_lower_tri_self_transpose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ namespace math {
template <typename T, int R, int C>
inline Eigen::Matrix<fvar<T>, R, R> multiply_lower_tri_self_transpose(
const Eigen::Matrix<fvar<T>, R, C>& m) {
if (m.rows() == 0)
if (m.rows() == 0) {
return Eigen::Matrix<fvar<T>, R, R>(0, 0);
}
Eigen::Matrix<fvar<T>, 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));
}
Expand Down
8 changes: 5 additions & 3 deletions stan/math/fwd/mat/fun/qr_Q.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ namespace math {
template <typename T>
Eigen::Matrix<fvar<T>, Eigen::Dynamic, Eigen::Dynamic> qr_Q(
const Eigen::Matrix<fvar<T>, Eigen::Dynamic, Eigen::Dynamic>& m) {
typedef Eigen::Matrix<fvar<T>, Eigen::Dynamic, Eigen::Dynamic> matrix_fwd_t;
using matrix_fwd_t = Eigen::Matrix<fvar<T>, 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<matrix_fwd_t> 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;
}

Expand Down
Loading

0 comments on commit d4e9332

Please sign in to comment.