From 6feb74641265bfdad22bc6dc36e75dd707445a40 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Tue, 29 May 2012 13:48:08 +0100 Subject: [PATCH] use STL to calculate L1 distance and max norm --- packages/CLPBN/horus/Util.cpp | 44 +++++++++++------------------------ packages/CLPBN/horus/Util.h | 36 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/packages/CLPBN/horus/Util.cpp b/packages/CLPBN/horus/Util.cpp index 63ef64426..8bb6c933e 100644 --- a/packages/CLPBN/horus/Util.cpp +++ b/packages/CLPBN/horus/Util.cpp @@ -321,23 +321,15 @@ namespace LogAware { void normalize (Params& v) { - double sum = LogAware::addIdenty(); if (Globals::logDomain) { - for (size_t i = 0; i < v.size(); i++) { - sum = Util::logSum (sum, v[i]); - } + double sum = std::accumulate (v.begin(), v.end(), + LogAware::addIdenty(), Util::logSum); assert (sum != -numeric_limits::infinity()); - for (size_t i = 0; i < v.size(); i++) { - v[i] -= sum; - } + v -= sum; } else { - for (size_t i = 0; i < v.size(); i++) { - sum += v[i]; - } + double sum = std::accumulate (v.begin(), v.end(), 0); assert (sum != 0.0); - for (size_t i = 0; i < v.size(); i++) { - v[i] /= sum; - } + v /= sum; } } @@ -349,13 +341,11 @@ getL1Distance (const Params& v1, const Params& v2) assert (v1.size() == v2.size()); double dist = 0.0; if (Globals::logDomain) { - for (size_t i = 0; i < v1.size(); i++) { - dist += abs (exp(v1[i]) - exp(v2[i])); - } + dist = std::inner_product (v1.begin(), v1.end(), v2.begin(), 0.0, + std::plus(), FuncObject::abs_diff_exp()); } else { - for (size_t i = 0; i < v1.size(); i++) { - dist += abs (v1[i] - v2[i]); - } + dist = std::inner_product (v1.begin(), v1.end(), v2.begin(), 0.0, + std::plus(), FuncObject::abs_diff()); } return dist; } @@ -368,19 +358,11 @@ getMaxNorm (const Params& v1, const Params& v2) assert (v1.size() == v2.size()); double max = 0.0; if (Globals::logDomain) { - for (size_t i = 0; i < v1.size(); i++) { - double diff = abs (exp(v1[i]) - exp(v2[i])); - if (diff > max) { - max = diff; - } - } + max = std::inner_product (v1.begin(), v1.end(), v2.begin(), 0.0, + FuncObject::max(), FuncObject::abs_diff_exp()); } else { - for (size_t i = 0; i < v1.size(); i++) { - double diff = abs (v1[i] - v2[i]); - if (diff > max) { - max = diff; - } - } + max = std::inner_product (v1.begin(), v1.end(), v2.begin(), 0.0, + FuncObject::max(), FuncObject::abs_diff()); } return max; } diff --git a/packages/CLPBN/horus/Util.h b/packages/CLPBN/horus/Util.h index 5925ec908..9d9f39596 100644 --- a/packages/CLPBN/horus/Util.h +++ b/packages/CLPBN/horus/Util.h @@ -382,5 +382,41 @@ std::ostream& operator << (std::ostream& os, const vector& v) return os; } + +namespace FuncObject { + +template +struct max : public std::binary_function +{ + T operator() (const T& x, const T& y) const + { + return x < y ? y : x; + } +}; + + + +template +struct abs_diff : public std::binary_function +{ + T operator() (const T& x, const T& y) const + { + return std::abs (x - y); + } +}; + + + +template +struct abs_diff_exp : public std::binary_function +{ + T operator() (const T& x, const T& y) const + { + return std::abs (std::exp (x) - std::exp (y)); + } +}; + +} + #endif // HORUS_UTIL_H