#ifndef HORUS_UTIL_H #define HORUS_UTIL_H #include #include #include #include #include #include #include #include #include #include #include "Horus.h" using namespace std; template void operator+=(std::vector& v, double val) { std::transform (v.begin(), v.end(), v.begin(), std::bind1st (plus(), val)); } template void operator-=(std::vector& v, double val) { std::transform (v.begin(), v.end(), v.begin(), std::bind1st (minus(), val)); } template void operator*=(std::vector& v, double val) { std::transform (v.begin(), v.end(), v.begin(), std::bind1st (multiplies(), val)); } template void operator/=(std::vector& v, double val) { std::transform (v.begin(), v.end(), v.begin(), std::bind1st (divides(), val)); } template void operator+=(std::vector& a, const std::vector& b) { assert (a.size() == b.size()); std::transform (a.begin(), a.end(), b.begin(), a.begin(), plus()); } template void operator-=(std::vector& a, const std::vector& b) { assert (a.size() == b.size()); std::transform (a.begin(), a.end(), b.begin(), a.begin(), minus()); } template void operator*=(std::vector& a, const std::vector& b) { assert (a.size() == b.size()); std::transform (a.begin(), a.end(), b.begin(), a.begin(), multiplies()); } template void operator/=(std::vector& a, const std::vector& b) { assert (a.size() == b.size()); std::transform (a.begin(), a.end(), b.begin(), a.begin(), divides()); } template void operator^=(std::vector& v, double exp) { std::transform (v.begin(), v.end(), v.begin(), std::bind2nd (ptr_fun (std::pow), exp)); } template void operator^=(std::vector& v, int iexp) { std::transform (v.begin(), v.end(), v.begin(), std::bind2nd (ptr_fun (std::pow), iexp)); } namespace { const double NEG_INF = -numeric_limits::infinity(); }; namespace Util { template void addToVector (vector&, const vector&); template void addToSet (set&, const vector&); template void addToQueue (queue&, const vector&); template bool contains (const vector&, const T&); template bool contains (const set&, const T&); template bool contains ( const unordered_map&, const K&); template size_t indexOf (const vector&, const T&); template std::string toString (const T&); template <> std::string toString (const bool&); template void log (vector&); template void exp (vector&); double logSum (double, double); void add (Params&, const Params&, unsigned); void multiply (Params&, const Params&, unsigned); unsigned maxUnsigned (void); unsigned stringToUnsigned (string); double stringToDouble (string); double factorial (unsigned); double logFactorial (unsigned); unsigned nrCombinations (unsigned, unsigned); size_t sizeExpected (const Ranges&); unsigned nrDigits (int); bool isInteger (const string&); string parametersToString (const Params&, unsigned = Constants::PRECISION); vector getStateLines (const Vars&); bool setHorusFlag (string key, string value); void printHeader (string, std::ostream& os = std::cout); void printSubHeader (string, std::ostream& os = std::cout); void printAsteriskLine (std::ostream& os = std::cout); void printDashedLine (std::ostream& os = std::cout); }; template void Util::addToVector (vector& v, const vector& elements) { v.insert (v.end(), elements.begin(), elements.end()); } template void Util::addToSet (set& s, const vector& elements) { s.insert (elements.begin(), elements.end()); } template void Util::addToQueue (queue& q, const vector& elements) { for (size_t i = 0; i < elements.size(); i++) { q.push (elements[i]); } } template bool Util::contains (const vector& v, const T& e) { return std::find (v.begin(), v.end(), e) != v.end(); } template bool Util::contains (const set& s, const T& e) { return s.find (e) != s.end(); } template bool Util::contains (const unordered_map& m, const K& k) { return m.find (k) != m.end(); } template size_t Util::indexOf (const vector& v, const T& e) { return std::distance (v.begin(), std::find (v.begin(), v.end(), e)); } template std::string Util::toString (const T& t) { std::stringstream ss; ss << t; return ss.str(); } template std::ostream& operator << (std::ostream& os, const vector& v) { os << "[" ; for (size_t i = 0; i < v.size(); i++) { os << ((i != 0) ? ", " : "") << v[i]; } os << "]" ; return os; } template void Util::log (vector& v) { transform (v.begin(), v.end(), v.begin(), ::log); } template void Util::exp (vector& v) { transform (v.begin(), v.end(), v.begin(), ::exp); } inline double Util::logSum (double x, double y) { // std::log (std::exp (x) + std::exp (y)) can overflow! assert (std::isnan (x) == false); assert (std::isnan (y) == false); if (x == NEG_INF) { return y; } if (y == NEG_INF) { return x; } // if one value is much smaller than the other, // keep the larger value const double tol = 460.517; // log (1e200) if (x < y - tol) { return y; } if (y < x - tol) { return x; } assert (std::isnan (x - y) == false); const double exp_diff = std::exp (x - y); if (std::isfinite (exp_diff) == false) { // difference is too large return x > y ? x : y; } // otherwise return the sum return y + std::log (static_cast(1.0) + exp_diff); } inline void Util::add (Params& v1, const Params& v2, unsigned repetitions) { for (size_t count = 0; count < v1.size(); ) { for (size_t i = 0; i < v2.size(); i++) { for (unsigned r = 0; r < repetitions; r++) { v1[count] += v2[i]; count ++; } } } } inline void Util::multiply (Params& v1, const Params& v2, unsigned repetitions) { for (size_t count = 0; count < v1.size(); ) { for (size_t i = 0; i < v2.size(); i++) { for (unsigned r = 0; r < repetitions; r++) { v1[count] *= v2[i]; count ++; } } } } inline unsigned Util::maxUnsigned (void) { return numeric_limits::max(); } namespace LogAware { inline double one() { return Globals::logDomain ? 0.0 : 1.0; } inline double zero() { return Globals::logDomain ? NEG_INF : 0.0; } inline double addIdenty() { return Globals::logDomain ? NEG_INF : 0.0; } inline double multIdenty() { return Globals::logDomain ? 0.0 : 1.0; } inline double withEvidence() { return Globals::logDomain ? 0.0 : 1.0; } inline double noEvidence() { return Globals::logDomain ? NEG_INF : 0.0; } inline double log (double v) { return Globals::logDomain ? ::log (v) : v; } inline double exp (double v) { return Globals::logDomain ? ::exp (v) : v; } void normalize (Params&); double getL1Distance (const Params&, const Params&); double getMaxNorm (const Params&, const Params&); double pow (double, unsigned); double pow (double, double); void pow (Params&, unsigned); void pow (Params&, double); }; #endif // HORUS_UTIL_H