TProb<T> _p;
public:
+ /// Iterator over factor entries
+ typedef typename TProb<T>::iterator iterator;
+
+ /// Const iterator over factor entries
+ typedef typename TProb<T>::const_iterator const_iterator;
+
/// Constructs TFactor depending on no variables, with value p
TFactor ( Real p = 1.0 ) : _vs(), _p(1,p) {}
/// Constructs TFactor depending on variables in ns, with all values set to p
TFactor( const VarSet& ns, Real p ) : _vs(ns), _p(_vs.nrStates(),p) {}
- /// Constructs TFactor depending on variables in ns, copying the values from the array p
- TFactor( const VarSet& ns, const Real *p ) : _vs(ns), _p(_vs.nrStates(),p) {}
+ /// Constructs TFactor depending on variables in ns, copying the values from the range starting at begin
+ /** \param ns contains the variables that the new TFactor should depend on.
+ * \tparam Iterator Iterates over instances of type T; should support addition of size_t.
+ * \param begin Points to first element to be added.
+ */
+ template<typename TIterator>
+ TFactor( const VarSet& ns, TIterator begin ) : _vs(ns), _p(begin, begin + _vs.nrStates(), _vs.nrStates()) {}
/// Constructs TFactor depending on variables in ns, with values set to the TProb p
TFactor( const VarSet& ns, const TProb<T>& p ) : _vs(ns), _p(p) {
/// Constructs TFactor depending on the variable n, with uniform distribution
TFactor( const Var& n ) : _vs(n), _p(n.states()) {}
- /// Copy constructor
- TFactor( const TFactor<T> &x ) : _vs(x._vs), _p(x._p) {}
-
- /// Assignment operator
- TFactor<T> & operator= (const TFactor<T> &x) {
- if( this != &x ) {
- _vs = x._vs;
- _p = x._p;
- }
- return *this;
- }
-
/// Returns const reference to value vector
const TProb<T> & p() const { return _p; }
/// Returns reference to value vector
/// Returns a reference to the i'th entry of the value vector
T& operator[] (size_t i) { return _p[i]; }
+
+ /// Returns iterator pointing to first entry
+ iterator begin() { return _p.begin(); }
+ /// Returns const iterator pointing to first entry
+ const_iterator begin() const { return _p.begin(); }
+ /// Returns iterator pointing beyond last entry
+ iterator end() { return _p.end(); }
+ /// Returns const iterator pointing beyond last entry
+ const_iterator end() const { return _p.end(); }
/// Sets all values to p
TFactor<T> & fill (T p) { _p.fill( p ); return(*this); }
if( _vs == ns )
return *this;
else
- return (*this) * TFactor<T>(ns / _vs, 1);
+ return (*this) * TFactor<T>(ns / _vs, (T)1);
}
/// Returns true if *this has NaN values
bool hasNegatives() const { return _p.hasNegatives(); }
/// Returns total sum of values
- T totalSum() const { return _p.totalSum(); }
+ T sum() const { return _p.sum(); }
/// Returns maximum absolute value
T maxAbs() const { return _p.maxAbs(); }
/// Returns maximum value
- T maxVal() const { return _p.maxVal(); }
+ T max() const { return _p.max(); }
/// Returns minimum value
- T minVal() const { return _p.minVal(); }
+ T min() const { return _p.min(); }
/// Returns entropy of *this TFactor
Real entropy() const { return _p.entropy(); }
bs = i.states();
else
as = j.states();
- T f1 = slice( ij, alpha1 * as + beta1 * bs ).p().divide( slice( ij, alpha2 * as + beta1 * bs ).p() ).maxVal();
- T f2 = slice( ij, alpha2 * as + beta2 * bs ).p().divide( slice( ij, alpha1 * as + beta2 * bs ).p() ).maxVal();
+ T f1 = slice( ij, alpha1 * as + beta1 * bs ).p().divide( slice( ij, alpha2 * as + beta1 * bs ).p() ).max();
+ T f2 = slice( ij, alpha2 * as + beta2 * bs ).p().divide( slice( ij, alpha1 * as + beta2 * bs ).p() ).max();
T f = f1 * f2;
if( f > max )
max = f;
/** \relates TFactor
*/
template<typename T> std::ostream& operator<< (std::ostream& os, const TFactor<T>& P) {
- os << "(" << P.vars() << " <";
+ os << "(" << P.vars() << ", (";
for( size_t i = 0; i < P.states(); i++ )
- os << P[i] << " ";
- os << ">)";
+ os << (i == 0 ? "" : ", ") << P[i];
+ os << "))";
return os;
}