#ifndef YAP_PACKAGES_CLPBN_HORUS_UTIL_H_ #define YAP_PACKAGES_CLPBN_HORUS_UTIL_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include "Horus.h" namespace Horus { namespace { const double NEG_INF = -std::numeric_limits::infinity(); } namespace Util { template void addToVector (std::vector&, const std::vector&); template void addToSet (std::set&, const std::vector&); template void addToQueue (std::queue&, const std::vector&); template bool contains (const std::vector&, const T&); template bool contains (const std::set&, const T&); template bool contains (const std::unordered_map&, const K&); template size_t indexOf (const std::vector&, const T&); template void apply_n_times (Params& v1, const Params& v2, unsigned reps, Operation); template void log (std::vector&); template void exp (std::vector&); template std::string elementsToString (const std::vector& v, std::string sep = " "); template std::string toString (const T&); template <> std::string toString (const bool&); double logSum (double, double); unsigned maxUnsigned(); unsigned stringToUnsigned (std::string); double stringToDouble (std::string); double factorial (unsigned); double logFactorial (unsigned); unsigned nrCombinations (unsigned, unsigned); size_t sizeExpected (const Ranges&); unsigned nrDigits (int); bool isInteger (const std::string&); std::string parametersToString ( const Params&, unsigned = Constants::precision); std::vector getStateLines (const Vars&); bool setHorusFlag (std::string option, std::string value); void printHeader (std::string, std::ostream& os = std::cout); void printSubHeader (std::string, std::ostream& os = std::cout); void printAsteriskLine (std::ostream& os = std::cout); void printDashedLine (std::ostream& os = std::cout); } // namespace Util template void Util::addToVector (std::vector& v, const std::vector& elements) { v.insert (v.end(), elements.begin(), elements.end()); } template void Util::addToSet (std::set& s, const std::vector& elements) { s.insert (elements.begin(), elements.end()); } template void Util::addToQueue (std::queue& q, const std::vector& elements) { for (size_t i = 0; i < elements.size(); i++) { q.push (elements[i]); } } template bool Util::contains (const std::vector& v, const T& e) { return std::find (v.begin(), v.end(), e) != v.end(); } template bool Util::contains (const std::set& s, const T& e) { return s.find (e) != s.end(); } template bool Util::contains (const std::unordered_map& m, const K& k) { return m.find (k) != m.end(); } template size_t Util::indexOf (const std::vector& v, const T& e) { return std::distance (v.begin(), std::find (v.begin(), v.end(), e)); } template void Util::apply_n_times ( Params& v1, const Params& v2, unsigned repetitions, Operation unary_op) { Params::iterator first = v1.begin(); Params::const_iterator last = v1.end(); Params::const_iterator first2 = v2.begin(); Params::const_iterator last2 = v2.end(); while (first != last) { for (first2 = v2.begin(); first2 != last2; ++first2) { std::transform (first, first + repetitions, first, std::bind1st (unary_op, *first2)); first += repetitions; } } } template void Util::log (std::vector& v) { std::transform (v.begin(), v.end(), v.begin(), (double (*)(double))std::log); } template void Util::exp (std::vector& v) { std::transform (v.begin(), v.end(), v.begin(), (double (*)(double))std::exp); } template std::string Util::elementsToString (const std::vector& v, std::string sep) { std::stringstream ss; for (size_t i = 0; i < v.size(); i++) { ss << ((i != 0) ? sep : "") << v[i]; } return ss.str(); } template std::string Util::toString (const T& t) { std::stringstream ss; ss << t; return ss.str(); } 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 unsigned Util::maxUnsigned() { return std::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); } // namespace LogAware template void operator+=(std::vector& v, double val) { std::transform (v.begin(), v.end(), v.begin(), std::bind2nd (std::plus(), val)); } template void operator-=(std::vector& v, double val) { std::transform (v.begin(), v.end(), v.begin(), std::bind2nd (std::minus(), val)); } template void operator*=(std::vector& v, double val) { std::transform (v.begin(), v.end(), v.begin(), std::bind2nd (std::multiplies(), val)); } template void operator/=(std::vector& v, double val) { std::transform (v.begin(), v.end(), v.begin(), std::bind2nd (std::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(), std::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(), std::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(), std::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(), std::divides()); } template void operator^=(std::vector& v, double exp) { std::transform (v.begin(), v.end(), v.begin(), std::bind2nd (std::ptr_fun (std::pow), exp)); } template void operator^=(std::vector& v, int iexp) { std::transform (v.begin(), v.end(), v.begin(), std::bind2nd (std::ptr_fun (std::pow), iexp)); } template std::ostream& operator<< (std::ostream& os, const std::vector& v) { os << "[" ; os << Util::elementsToString (v, ", "); os << "]" ; return os; } namespace FuncObj { 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)); }}; } // namespace FuncObj } // namespace Horus #endif // YAP_PACKAGES_CLPBN_HORUS_UTIL_H_