From 3ac854b2ffa6fdd401b738eb8b4b857dd27cc9ad Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Mon, 28 May 2012 21:27:52 +0100 Subject: [PATCH] use more Util::sizeExpected --- packages/CLPBN/horus/BpSolver.cpp | 5 ++--- packages/CLPBN/horus/CbpSolver.cpp | 3 +-- packages/CLPBN/horus/Indexer.h | 5 ++--- packages/CLPBN/horus/Util.cpp | 16 +++++++++------- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/CLPBN/horus/BpSolver.cpp b/packages/CLPBN/horus/BpSolver.cpp index 667cbe31f..ea4ba10f3 100644 --- a/packages/CLPBN/horus/BpSolver.cpp +++ b/packages/CLPBN/horus/BpSolver.cpp @@ -264,9 +264,8 @@ BpSolver::calcFactorToVarMsg (SpLink* link) const SpLinkSet& links = ninf(src)->getLinks(); // calculate the product of messages that were sent // to factor `src', except from var `dst' - unsigned msgSize = std::accumulate (src->factor().ranges().begin(), - src->factor().ranges().end(), 1, std::multiplies()); - unsigned reps = 1; + unsigned reps = 1; + unsigned msgSize = Util::sizeExpected (src->factor().ranges()); Params msgProduct (msgSize, LogAware::multIdenty()); if (Globals::logDomain) { for (size_t i = links.size(); i-- > 0; ) { diff --git a/packages/CLPBN/horus/CbpSolver.cpp b/packages/CLPBN/horus/CbpSolver.cpp index 65992d75d..04e08f124 100644 --- a/packages/CLPBN/horus/CbpSolver.cpp +++ b/packages/CLPBN/horus/CbpSolver.cpp @@ -207,9 +207,8 @@ CbpSolver::calcFactorToVarMsg (SpLink* _link) const SpLinkSet& links = ninf(src)->getLinks(); // calculate the product of messages that were sent // to factor `src', except from var `dst' - unsigned msgSize = std::accumulate (src->factor().ranges().begin(), - src->factor().ranges().end(), 1, std::multiplies()); unsigned reps = 1; + unsigned msgSize = Util::sizeExpected (src->factor().ranges()); Params msgProduct (msgSize, LogAware::multIdenty()); if (Globals::logDomain) { for (size_t i = links.size(); i-- > 0; ) { diff --git a/packages/CLPBN/horus/Indexer.h b/packages/CLPBN/horus/Indexer.h index 79f6cbb0a..db99cf1a7 100644 --- a/packages/CLPBN/horus/Indexer.h +++ b/packages/CLPBN/horus/Indexer.h @@ -14,10 +14,9 @@ class Indexer { public: Indexer (const Ranges& ranges, bool calcOffsets = true) - : index_(0), indices_(ranges.size(), 0), ranges_(ranges) + : index_(0), indices_(ranges.size(), 0), ranges_(ranges), + size_(Util::sizeExpected (ranges)) { - size_ = std::accumulate (ranges.begin(), ranges.end(), 1, - std::multiplies()); if (calcOffsets) { calculateOffsets(); } diff --git a/packages/CLPBN/horus/Util.cpp b/packages/CLPBN/horus/Util.cpp index f88ceb9cc..63ef64426 100644 --- a/packages/CLPBN/horus/Util.cpp +++ b/packages/CLPBN/horus/Util.cpp @@ -18,7 +18,6 @@ InfAlgorithms infAlgorithm = InfAlgorithms::VE; - namespace BpOptions { Schedule schedule = BpOptions::Schedule::SEQ_FIXED; //Schedule schedule = BpOptions::Schedule::SEQ_RANDOM; @@ -71,7 +70,6 @@ stringToDouble (string str) - double factorial (unsigned num) { @@ -128,8 +126,8 @@ nrCombinations (unsigned n, unsigned k) size_t sizeExpected (const Ranges& ranges) { - return std::accumulate ( - ranges.begin(), ranges.end(), 1, multiplies()); + return std::accumulate (ranges.begin(), + ranges.end(), 1, multiplies()); } @@ -392,7 +390,9 @@ getMaxNorm (const Params& v1, const Params& v2) double pow (double base, unsigned iexp) { - return Globals::logDomain ? base * iexp : std::pow (base, iexp); + return Globals::logDomain + ? base * iexp + : std::pow (base, iexp); } @@ -400,8 +400,10 @@ pow (double base, unsigned iexp) double pow (double base, double exp) { - // assumes that `expoent' is never in log domain - return Globals::logDomain ? base * exp : std::pow (base, exp); + // `expoent' should not be in log domain + return Globals::logDomain + ? base * exp + : std::pow (base, exp); }