Skip to content

Commit

Permalink
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-buildbot committed Nov 28, 2024
1 parent 1595aba commit 88b378f
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 124 deletions.
100 changes: 51 additions & 49 deletions stan/math/fwd/fun/log_add_exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,68 +49,70 @@ inline fvar<T> log_add_exp(double x1, const fvar<T>& x2) {

// Overload for matrices of fvar
template <typename T>
inline Eigen::Matrix<fvar<T>, -1, -1> log_add_exp(const Eigen::Matrix<fvar<T>, -1, -1>& a,
const Eigen::Matrix<fvar<T>, -1, -1>& b) {
using fvar_mat_type = Eigen::Matrix<fvar<T>, -1, -1>;
fvar_mat_type result(a.rows(), a.cols());

// Check for empty inputs
if (a.size() == 0 || b.size() == 0) {
throw std::invalid_argument("Input containers must not be empty.");
}
inline Eigen::Matrix<fvar<T>, -1, -1> log_add_exp(
const Eigen::Matrix<fvar<T>, -1, -1>& a,
const Eigen::Matrix<fvar<T>, -1, -1>& b) {
using fvar_mat_type = Eigen::Matrix<fvar<T>, -1, -1>;
fvar_mat_type result(a.rows(), a.cols());

// Check for NaN
if (a.array().isNaN().any() || b.array().isNaN().any()) {
result.setConstant(fvar<T>(std::numeric_limits<double>::quiet_NaN()));
return result;
}
// Check for empty inputs
if (a.size() == 0 || b.size() == 0) {
throw std::invalid_argument("Input containers must not be empty.");
}

// Check for infinity
if (a.array().isInf().any() || b.array().isInf().any()) {
result.setConstant(fvar<T>(std::numeric_limits<double>::quiet_NaN()));
return result;
}
// Check for NaN
if (a.array().isNaN().any() || b.array().isNaN().any()) {
result.setConstant(fvar<T>(std::numeric_limits<double>::quiet_NaN()));
return result;
}

// Check for infinity
if (a.array().isInf().any() || b.array().isInf().any()) {
result.setConstant(fvar<T>(std::numeric_limits<double>::quiet_NaN()));
return result;
}

// Apply the log_add_exp operation directly
for (int i = 0; i < a.rows(); ++i) {
for (int j = 0; j < a.cols(); ++j) {
result(i, j) = stan::math::log_add_exp(a(i, j), b(i, j));
}
// Apply the log_add_exp operation directly
for (int i = 0; i < a.rows(); ++i) {
for (int j = 0; j < a.cols(); ++j) {
result(i, j) = stan::math::log_add_exp(a(i, j), b(i, j));
}
}

return result; // Return the result matrix
return result; // Return the result matrix
}

// Overload for Eigen vectors
template <typename T>
inline Eigen::Matrix<fvar<T>, -1, 1> log_add_exp(const Eigen::Matrix<fvar<T>, -1, 1>& a,
const Eigen::Matrix<fvar<T>, -1, 1>& b) {
using fvar_vec_type = Eigen::Matrix<fvar<T>, -1, 1>;
fvar_vec_type result(a.rows());

// Check for empty inputs
if (a.size() == 0 || b.size() == 0) {
throw std::invalid_argument("Input containers must not be empty.");
}
inline Eigen::Matrix<fvar<T>, -1, 1> log_add_exp(
const Eigen::Matrix<fvar<T>, -1, 1>& a,
const Eigen::Matrix<fvar<T>, -1, 1>& b) {
using fvar_vec_type = Eigen::Matrix<fvar<T>, -1, 1>;
fvar_vec_type result(a.rows());

// Check for NaN
if (a.array().isNaN().any() || b.array().isNaN().any()) {
result.setConstant(fvar<T>(std::numeric_limits<double>::quiet_NaN()));
return result;
}
// Check for empty inputs
if (a.size() == 0 || b.size() == 0) {
throw std::invalid_argument("Input containers must not be empty.");
}

// Check for infinity
if (a.array().isInf().any() || b.array().isInf().any()) {
result.setConstant(fvar<T>(std::numeric_limits<double>::quiet_NaN()));
return result;
}
// Check for NaN
if (a.array().isNaN().any() || b.array().isNaN().any()) {
result.setConstant(fvar<T>(std::numeric_limits<double>::quiet_NaN()));
return result;
}

// Apply the log_add_exp operation directly
for (int i = 0; i < a.rows(); ++i) {
result(i) = stan::math::log_add_exp(a(i), b(i));
}
// Check for infinity
if (a.array().isInf().any() || b.array().isInf().any()) {
result.setConstant(fvar<T>(std::numeric_limits<double>::quiet_NaN()));
return result;
}

// Apply the log_add_exp operation directly
for (int i = 0; i < a.rows(); ++i) {
result(i) = stan::math::log_add_exp(a(i), b(i));
}

return result; // Return the result vector
return result; // Return the result vector
}

// Specialization for nested fvar types
Expand Down
141 changes: 72 additions & 69 deletions stan/math/prim/fun/log_add_exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace math {
template <typename T1, typename T2, require_all_not_st_var<T1, T2>* = nullptr,
require_all_stan_scalar_t<T1, T2>* = nullptr>
inline return_type_t<T1, T2> log_add_exp(const T2& a, const T1& b) {

if (a == NEGATIVE_INFTY) {
return b;
}
Expand Down Expand Up @@ -60,66 +59,69 @@ inline return_type_t<T1, T2> log_add_exp(const T2& a, const T1& b) {
*/
template <typename T, require_container_st<std::is_arithmetic, T>* = nullptr>
inline auto log_add_exp(const T& a, const T& b) {

// Check if sizes are compatible
if constexpr (stan::is_eigen<T>::value) {
// Check if both matrices/vectors have the same dimensions
stan::math::check_matching_dims("log_add_exp", "a", a, "b", b);

// Determine the number of rows and columns for the result
size_t rows = a.rows();
size_t cols = b.cols();
using return_t = return_type_t<T>;

Eigen::Matrix<return_t, Eigen::Dynamic, Eigen::Dynamic> result(rows, cols);

// Iterate over each element
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
double a_val = (a.cols() == 1) ? a(i, 0) : a(i, j); // Handle column vector or matrix
double b_val = (b.rows() == 1) ? b(0, j) : b(i, j); // Handle row vector or matrix

if (a_val == NEGATIVE_INFTY) {
result(i, j) = b_val;
} else if (b_val == NEGATIVE_INFTY) {
result(i, j) = a_val;
} else if (a_val == INFTY || b_val == INFTY) {
result(i, j) = INFTY;
} else {
result(i, j) = log_sum_exp(a_val, b_val);
}
}
}

return result;
} else if constexpr (std::is_same_v<T, std::vector<typename T::value_type>>) {
// Handle std::vector
if (a.size() != b.size()) {
throw std::invalid_argument("Sizes of x and y must match.");
// Check if sizes are compatible
if constexpr (stan::is_eigen<T>::value) {
// Check if both matrices/vectors have the same dimensions
stan::math::check_matching_dims("log_add_exp", "a", a, "b", b);

// Determine the number of rows and columns for the result
size_t rows = a.rows();
size_t cols = b.cols();
using return_t = return_type_t<T>;

Eigen::Matrix<return_t, Eigen::Dynamic, Eigen::Dynamic> result(rows, cols);

// Iterate over each element
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
double a_val = (a.cols() == 1)
? a(i, 0)
: a(i, j); // Handle column vector or matrix
double b_val = (b.rows() == 1)
? b(0, j)
: b(i, j); // Handle row vector or matrix

if (a_val == NEGATIVE_INFTY) {
result(i, j) = b_val;
} else if (b_val == NEGATIVE_INFTY) {
result(i, j) = a_val;
} else if (a_val == INFTY || b_val == INFTY) {
result(i, j) = INFTY;
} else {
result(i, j) = log_sum_exp(a_val, b_val);
}
}
}

using return_t = return_type_t<T>;
std::vector<return_t> result(a.size());

for (size_t i = 0; i < a.size(); ++i) {
double a_val = a[i];
double b_val = b[i];

if (a_val == NEGATIVE_INFTY) {
result[i] = b_val;
} else if (b_val == NEGATIVE_INFTY) {
result[i] = a_val;
} else if (a_val == INFTY || b_val == INFTY) {
result[i] = INFTY;
} else {
result[i] = log_sum_exp(a_val, b_val);
}
}
return result;
} else if constexpr (std::is_same_v<T, std::vector<typename T::value_type>>) {
// Handle std::vector
if (a.size() != b.size()) {
throw std::invalid_argument("Sizes of x and y must match.");
}

return result;
} else {
throw std::invalid_argument("Unsupported container type.");
using return_t = return_type_t<T>;
std::vector<return_t> result(a.size());

for (size_t i = 0; i < a.size(); ++i) {
double a_val = a[i];
double b_val = b[i];

if (a_val == NEGATIVE_INFTY) {
result[i] = b_val;
} else if (b_val == NEGATIVE_INFTY) {
result[i] = a_val;
} else if (a_val == INFTY || b_val == INFTY) {
result[i] = INFTY;
} else {
result[i] = log_sum_exp(a_val, b_val);
}
}

return result;
} else {
throw std::invalid_argument("Unsupported container type.");
}
}

/**
Expand All @@ -134,23 +136,24 @@ inline auto log_add_exp(const T& a, const T& b) {
*/
template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr>
inline auto log_add_exp(const T1& a, const T2& b) {
// Check if both are Eigen/vectors
if constexpr (stan::is_eigen<T1>::value && stan::is_eigen<T2>::value) {
// Check if both matrices/vectors have the same dimensions
stan::math::check_matching_dims("log_add_exp", "a", a, "b", b);
} else {
// Check if sizes are compatible for other types
if (a.size() != b.size()) {
throw std::invalid_argument("Sizes of x and y must match or be compatible.");
}
// Check if both are Eigen/vectors
if constexpr (stan::is_eigen<T1>::value && stan::is_eigen<T2>::value) {
// Check if both matrices/vectors have the same dimensions
stan::math::check_matching_dims("log_add_exp", "a", a, "b", b);
} else {
// Check if sizes are compatible for other types
if (a.size() != b.size()) {
throw std::invalid_argument(
"Sizes of x and y must match or be compatible.");
}
}

// If dimensions are verified to match, apply the operation
return apply_scalar_binary(
// If dimensions are verified to match, apply the operation
return apply_scalar_binary(
a, b, [](const auto& c, const auto& d) { return log_add_exp(c, d); });
}

} // namespace math
} // nfamespace stan
} // namespace stan

#endif
12 changes: 6 additions & 6 deletions test/unit/math/mix/fun/log_add_exp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TEST(MathMixMatFun, logAddExp) {
y1 << 3.0, 2.0;
stan::test::expect_ad(f, x1, y1);

//Test with negative infinity
// Test with negative infinity

stan::test::expect_ad(f, stan::math::NEGATIVE_INFTY, 1.0);
stan::test::expect_ad(f, 1.0, stan::math::NEGATIVE_INFTY);
Expand Down Expand Up @@ -50,7 +50,6 @@ TEST(MathMixMatFun, log_add_exp_elementwise_values) {
EXPECT_TRUE(std::isinf(result[1]));
}


TEST(MathMixMatFun, log_add_exp_mismatched_sizes) {
auto f = [](const auto& x, const auto& y) {
return stan::math::log_add_exp(x, y);
Expand All @@ -73,14 +72,15 @@ TEST(MathMixMatFun, log_add_exp_container_tests) {
x_row << 2.0, 1.0;
Eigen::MatrixXd y_row(1, 2);
y_row << 3.0, 2.0;

stan::test::expect_ad(f, x_row, y_row);

// Additional tests with mismatched sizes
Eigen::MatrixXd x_mismatch(2, 1);
x_mismatch << 0.5, -1.0;
Eigen::MatrixXd y_mismatch(1, 3);
y_mismatch << 1.0, 2.0, 3.0;

EXPECT_THROW(stan::math::log_add_exp(x_mismatch, y_mismatch), std::invalid_argument);

EXPECT_THROW(stan::math::log_add_exp(x_mismatch, y_mismatch),
std::invalid_argument);
}

0 comments on commit 88b378f

Please sign in to comment.