libDAI
Public Types | Public Member Functions | Private Attributes | Related Functions | List of all members
dai::TProb< T > Class Template Reference

Represents a vector with entries of type T. More...

#include <dai/prob.h>

Public Types

typedef std::vector< T > container_type
 Type of data structure used for storing the values. More...
 
typedef TProb< T > this_type
 Shorthand. More...
 
typedef container_type::const_iterator const_iterator
 Constant iterator over the elements. More...
 
typedef container_type::iterator iterator
 Iterator over the elements. More...
 
typedef container_type::const_reverse_iterator const_reverse_iterator
 Constant reverse iterator over the elements. More...
 
typedef container_type::reverse_iterator reverse_iterator
 Reverse iterator over the elements. More...
 

Public Member Functions

template<typename binOp1 , typename binOp2 >
innerProduct (const this_type &q, T init, binOp1 binaryOp1, binOp2 binaryOp2) const
 Performs a generalized inner product, similar to std::inner_product. More...
 
Constructors and destructors
 TProb ()
 Default constructor (constructs empty vector) More...
 
 TProb (size_t n)
 Construct uniform probability distribution over n outcomes (i.e., a vector of length n with each entry set to $1/n$) More...
 
 TProb (size_t n, T p)
 Construct vector of length n with each entry set to p. More...
 
template<typename TIterator >
 TProb (TIterator begin, TIterator end, size_t sizeHint)
 Construct vector from a range. More...
 
template<typename S >
 TProb (const std::vector< S > &v)
 Construct vector from another vector. More...
 
Iterator interface
iterator begin ()
 Returns iterator that points to the first element. More...
 
const_iterator begin () const
 Returns constant iterator that points to the first element. More...
 
iterator end ()
 Returns iterator that points beyond the last element. More...
 
const_iterator end () const
 Returns constant iterator that points beyond the last element. More...
 
reverse_iterator rbegin ()
 Returns reverse iterator that points to the last element. More...
 
const_reverse_iterator rbegin () const
 Returns constant reverse iterator that points to the last element. More...
 
reverse_iterator rend ()
 Returns reverse iterator that points beyond the first element. More...
 
const_reverse_iterator rend () const
 Returns constant reverse iterator that points beyond the first element. More...
 
Miscellaneous operations
void resize (size_t sz)
 
Get/set individual entries
get (size_t i) const
 Gets i 'th entry. More...
 
void set (size_t i, T val)
 Sets i 'th entry to val. More...
 
Queries
const container_typep () const
 Returns a const reference to the wrapped container. More...
 
container_typep ()
 Returns a reference to the wrapped container. More...
 
operator[] (size_t i) const
 Returns a copy of the i 'th entry. More...
 
size_t size () const
 Returns length of the vector (i.e., the number of entries) More...
 
template<typename unOp >
accumulateSum (T init, unOp op) const
 Accumulate all values (similar to std::accumulate) by summing. More...
 
template<typename unOp >
accumulateMax (T init, unOp op, bool minimize) const
 Accumulate all values (similar to std::accumulate) by maximization/minimization. More...
 
entropy () const
 Returns the Shannon entropy of *this, $-\sum_i p_i \log p_i$. More...
 
max () const
 Returns maximum value of all entries. More...
 
min () const
 Returns minimum value of all entries. More...
 
sum () const
 Returns sum of all entries. More...
 
sumAbs () const
 Return sum of absolute value of all entries. More...
 
maxAbs () const
 Returns maximum absolute value of all entries. More...
 
bool hasNaNs () const
 Returns true if one or more entries are NaN. More...
 
bool hasNegatives () const
 Returns true if one or more entries are negative. More...
 
std::pair< size_t, T > argmax () const
 Returns a pair consisting of the index of the maximum value and the maximum value itself. More...
 
size_t draw ()
 Returns a random index, according to the (normalized) distribution described by *this. More...
 
bool operator< (const this_type &q) const
 Lexicographical comparison. More...
 
bool operator== (const this_type &q) const
 Comparison. More...
 
std::string toString () const
 Formats a TProb as a string. More...
 
Unary transformations
template<typename unaryOp >
this_type pwUnaryTr (unaryOp op) const
 Returns the result of applying operation op pointwise on *this. More...
 
this_type operator- () const
 Returns negative of *this. More...
 
this_type abs () const
 Returns pointwise absolute value. More...
 
this_type exp () const
 Returns pointwise exponent. More...
 
this_type log (bool zero=false) const
 Returns pointwise logarithm. More...
 
this_type inverse (bool zero=true) const
 Returns pointwise inverse. More...
 
this_type normalized (ProbNormType norm=dai::NORMPROB) const
 Returns normalized copy of *this, using the specified norm. More...
 
Unary operations
template<typename unaryOp >
this_typepwUnaryOp (unaryOp op)
 Applies unary operation op pointwise. More...
 
this_typerandomize ()
 Draws all entries i.i.d. from a uniform distribution on [0,1) More...
 
this_typesetUniform ()
 Sets all entries to $1/n$ where n is the length of the vector. More...
 
this_typetakeAbs ()
 Applies absolute value pointwise. More...
 
this_typetakeExp ()
 Applies exponent pointwise. More...
 
this_typetakeLog (bool zero=false)
 Applies logarithm pointwise. More...
 
normalize (ProbNormType norm=dai::NORMPROB)
 Normalizes vector using the specified norm. More...
 
Operations with scalars
this_typefill (T x)
 Sets all entries to x. More...
 
this_typeoperator+= (T x)
 Adds scalar x to each entry. More...
 
this_typeoperator-= (T x)
 Subtracts scalar x from each entry. More...
 
this_typeoperator*= (T x)
 Multiplies each entry with scalar x. More...
 
this_typeoperator/= (T x)
 Divides each entry by scalar x, where division by 0 yields 0. More...
 
this_typeoperator^= (T x)
 Raises entries to the power x. More...
 
Transformations with scalars
this_type operator+ (T x) const
 Returns sum of *this and scalar x. More...
 
this_type operator- (T x) const
 Returns difference of *this and scalar x. More...
 
this_type operator* (T x) const
 Returns product of *this with scalar x. More...
 
this_type operator/ (T x) const
 Returns quotient of *this and scalar x, where division by 0 yields 0. More...
 
this_type operator^ (T x) const
 Returns *this raised to the power x. More...
 
Operations with other equally-sized vectors
template<typename binaryOp >
this_typepwBinaryOp (const this_type &q, binaryOp op)
 Applies binary operation pointwise on two vectors. More...
 
this_typeoperator+= (const this_type &q)
 Pointwise addition with q. More...
 
this_typeoperator-= (const this_type &q)
 Pointwise subtraction of q. More...
 
this_typeoperator*= (const this_type &q)
 Pointwise multiplication with q. More...
 
this_typeoperator/= (const this_type &q)
 Pointwise division by q, where division by 0 yields 0. More...
 
this_typedivide (const this_type &q)
 Pointwise division by q, where division by 0 yields +Inf. More...
 
this_typeoperator^= (const this_type &q)
 Pointwise power. More...
 
Transformations with other equally-sized vectors
template<typename binaryOp >
this_type pwBinaryTr (const this_type &q, binaryOp op) const
 Returns the result of applying binary operation op pointwise on *this and q. More...
 
this_type operator+ (const this_type &q) const
 Returns sum of *this and q. More...
 
this_type operator- (const this_type &q) const
 Return *this minus q. More...
 
this_type operator* (const this_type &q) const
 Return product of *this with q. More...
 
this_type operator/ (const this_type &q) const
 Returns quotient of *this with q, where division by 0 yields 0. More...
 
this_type divided_by (const this_type &q) const
 Pointwise division by q, where division by 0 yields +Inf. More...
 
this_type operator^ (const this_type &q) const
 Returns *this to the power q. More...
 

Private Attributes

container_type _p
 The data structure that stores the values. More...
 

Related Functions

(Note that these are not member functions.)

template<typename T >
dist (const TProb< T > &p, const TProb< T > &q, ProbDistType dt)
 Returns distance between p and q, measured using distance measure dt. More...
 
template<typename T >
std::ostream & operator<< (std::ostream &os, const TProb< T > &p)
 Writes a TProb<T> to an output stream. More...
 
template<typename T >
TProb< T > min (const TProb< T > &a, const TProb< T > &b)
 Returns the pointwise minimum of a and b. More...
 
template<typename T >
TProb< T > max (const TProb< T > &a, const TProb< T > &b)
 Returns the pointwise maximum of a and b. More...
 

Detailed Description

template<typename T>
class dai::TProb< T >

Represents a vector with entries of type T.

It is simply a std::vector<T> with an interface designed for dealing with probability mass functions.

It is mainly used for representing measures on a finite outcome space, for example, the probability distribution of a discrete random variable. However, entries are not necessarily non-negative; it is also used to represent logarithms of probability mass functions.

Template Parameters
TShould be a scalar that is castable from and to dai::Real and should support elementary arithmetic operations.

Member Typedef Documentation

template<typename T>
typedef std::vector<T> dai::TProb< T >::container_type

Type of data structure used for storing the values.

template<typename T>
typedef TProb<T> dai::TProb< T >::this_type

Shorthand.

template<typename T>
typedef container_type::const_iterator dai::TProb< T >::const_iterator

Constant iterator over the elements.

template<typename T>
typedef container_type::iterator dai::TProb< T >::iterator

Iterator over the elements.

template<typename T>
typedef container_type::const_reverse_iterator dai::TProb< T >::const_reverse_iterator

Constant reverse iterator over the elements.

template<typename T>
typedef container_type::reverse_iterator dai::TProb< T >::reverse_iterator

Reverse iterator over the elements.

Constructor & Destructor Documentation

template<typename T>
dai::TProb< T >::TProb ( )
inline

Default constructor (constructs empty vector)

template<typename T>
dai::TProb< T >::TProb ( size_t  n)
inlineexplicit

Construct uniform probability distribution over n outcomes (i.e., a vector of length n with each entry set to $1/n$)

template<typename T>
dai::TProb< T >::TProb ( size_t  n,
p 
)
inlineexplicit

Construct vector of length n with each entry set to p.

template<typename T>
template<typename TIterator >
dai::TProb< T >::TProb ( TIterator  begin,
TIterator  end,
size_t  sizeHint 
)
inline

Construct vector from a range.

Template Parameters
TIteratorIterates over instances that can be cast to T
Parameters
beginPoints to first instance to be added.
endPoints just beyond last instance to be added.
sizeHintFor efficiency, the number of entries can be speficied by sizeHint; the value 0 can be given if the size is unknown, but this will result in a performance penalty.
template<typename T>
template<typename S >
dai::TProb< T >::TProb ( const std::vector< S > &  v)
inline

Construct vector from another vector.

Template Parameters
Stype of elements in v (should be castable to type T)
Parameters
vvector used for initialization.

Member Function Documentation

template<typename T>
iterator dai::TProb< T >::begin ( )
inline

Returns iterator that points to the first element.

template<typename T>
const_iterator dai::TProb< T >::begin ( ) const
inline

Returns constant iterator that points to the first element.

template<typename T>
iterator dai::TProb< T >::end ( )
inline

Returns iterator that points beyond the last element.

template<typename T>
const_iterator dai::TProb< T >::end ( ) const
inline

Returns constant iterator that points beyond the last element.

template<typename T>
reverse_iterator dai::TProb< T >::rbegin ( )
inline

Returns reverse iterator that points to the last element.

template<typename T>
const_reverse_iterator dai::TProb< T >::rbegin ( ) const
inline

Returns constant reverse iterator that points to the last element.

template<typename T>
reverse_iterator dai::TProb< T >::rend ( )
inline

Returns reverse iterator that points beyond the first element.

template<typename T>
const_reverse_iterator dai::TProb< T >::rend ( ) const
inline

Returns constant reverse iterator that points beyond the first element.

template<typename T>
T dai::TProb< T >::get ( size_t  i) const
inline

Gets i 'th entry.

template<typename T>
void dai::TProb< T >::set ( size_t  i,
val 
)
inline

Sets i 'th entry to val.

template<typename T>
const container_type& dai::TProb< T >::p ( ) const
inline

Returns a const reference to the wrapped container.

template<typename T>
container_type& dai::TProb< T >::p ( )
inline

Returns a reference to the wrapped container.

template<typename T>
T dai::TProb< T >::operator[] ( size_t  i) const
inline

Returns a copy of the i 'th entry.

template<typename T>
size_t dai::TProb< T >::size ( ) const
inline

Returns length of the vector (i.e., the number of entries)

template<typename T>
template<typename unOp >
T dai::TProb< T >::accumulateSum ( init,
unOp  op 
) const
inline

Accumulate all values (similar to std::accumulate) by summing.

The following calculation is done:

T t = op(init);
for( const_iterator it = begin(); it != end(); it++ )
t += op(*it);
return t;
template<typename T>
template<typename unOp >
T dai::TProb< T >::accumulateMax ( init,
unOp  op,
bool  minimize 
) const
inline

Accumulate all values (similar to std::accumulate) by maximization/minimization.

The following calculation is done (with "max" replaced by "min" if minimize == true):

T t = op(init);
for( const_iterator it = begin(); it != end(); it++ )
t = std::max( t, op(*it) );
return t;
template<typename T>
T dai::TProb< T >::entropy ( ) const
inline

Returns the Shannon entropy of *this, $-\sum_i p_i \log p_i$.

template<typename T>
T dai::TProb< T >::max ( ) const
inline

Returns maximum value of all entries.

template<typename T>
T dai::TProb< T >::min ( ) const
inline

Returns minimum value of all entries.

template<typename T>
T dai::TProb< T >::sum ( ) const
inline

Returns sum of all entries.

template<typename T>
T dai::TProb< T >::sumAbs ( ) const
inline

Return sum of absolute value of all entries.

template<typename T>
T dai::TProb< T >::maxAbs ( ) const
inline

Returns maximum absolute value of all entries.

template<typename T>
bool dai::TProb< T >::hasNaNs ( ) const
inline

Returns true if one or more entries are NaN.

template<typename T>
bool dai::TProb< T >::hasNegatives ( ) const
inline

Returns true if one or more entries are negative.

template<typename T>
std::pair<size_t,T> dai::TProb< T >::argmax ( ) const
inline

Returns a pair consisting of the index of the maximum value and the maximum value itself.

template<typename T>
size_t dai::TProb< T >::draw ( )
inline

Returns a random index, according to the (normalized) distribution described by *this.

template<typename T>
bool dai::TProb< T >::operator< ( const this_type q) const
inline

Lexicographical comparison.

Precondition
this->size() == q.size()
template<typename T>
bool dai::TProb< T >::operator== ( const this_type q) const
inline

Comparison.

template<typename T>
std::string dai::TProb< T >::toString ( ) const
inline

Formats a TProb as a string.

template<typename T>
template<typename unaryOp >
this_type dai::TProb< T >::pwUnaryTr ( unaryOp  op) const
inline

Returns the result of applying operation op pointwise on *this.

template<typename T>
this_type dai::TProb< T >::operator- ( ) const
inline

Returns negative of *this.

template<typename T>
this_type dai::TProb< T >::abs ( ) const
inline

Returns pointwise absolute value.

template<typename T>
this_type dai::TProb< T >::exp ( ) const
inline

Returns pointwise exponent.

template<typename T>
this_type dai::TProb< T >::log ( bool  zero = false) const
inline

Returns pointwise logarithm.

If zero == true, uses log(0)==0; otherwise, log(0)==-Inf.

template<typename T>
this_type dai::TProb< T >::inverse ( bool  zero = true) const
inline

Returns pointwise inverse.

If zero == true, uses 1/0==0; otherwise, 1/0==Inf.

template<typename T>
this_type dai::TProb< T >::normalized ( ProbNormType  norm = dai::NORMPROB) const
inline

Returns normalized copy of *this, using the specified norm.

Exceptions
NOT_NORMALIZABLEif the norm is zero
template<typename T>
template<typename unaryOp >
this_type& dai::TProb< T >::pwUnaryOp ( unaryOp  op)
inline

Applies unary operation op pointwise.

template<typename T>
this_type& dai::TProb< T >::randomize ( )
inline

Draws all entries i.i.d. from a uniform distribution on [0,1)

template<typename T>
this_type& dai::TProb< T >::setUniform ( )
inline

Sets all entries to $1/n$ where n is the length of the vector.

template<typename T>
this_type& dai::TProb< T >::takeAbs ( )
inline

Applies absolute value pointwise.

template<typename T>
this_type& dai::TProb< T >::takeExp ( )
inline

Applies exponent pointwise.

template<typename T>
this_type& dai::TProb< T >::takeLog ( bool  zero = false)
inline

Applies logarithm pointwise.

If zero == true, uses log(0)==0; otherwise, log(0)==-Inf.

template<typename T>
T dai::TProb< T >::normalize ( ProbNormType  norm = dai::NORMPROB)
inline

Normalizes vector using the specified norm.

Exceptions
NOT_NORMALIZABLEif the norm is zero
template<typename T>
this_type& dai::TProb< T >::fill ( x)
inline

Sets all entries to x.

template<typename T>
this_type& dai::TProb< T >::operator+= ( x)
inline

Adds scalar x to each entry.

template<typename T>
this_type& dai::TProb< T >::operator-= ( x)
inline

Subtracts scalar x from each entry.

template<typename T>
this_type& dai::TProb< T >::operator*= ( x)
inline

Multiplies each entry with scalar x.

template<typename T>
this_type& dai::TProb< T >::operator/= ( x)
inline

Divides each entry by scalar x, where division by 0 yields 0.

template<typename T>
this_type& dai::TProb< T >::operator^= ( x)
inline

Raises entries to the power x.

template<typename T>
this_type dai::TProb< T >::operator+ ( x) const
inline

Returns sum of *this and scalar x.

template<typename T>
this_type dai::TProb< T >::operator- ( x) const
inline

Returns difference of *this and scalar x.

template<typename T>
this_type dai::TProb< T >::operator* ( x) const
inline

Returns product of *this with scalar x.

template<typename T>
this_type dai::TProb< T >::operator/ ( x) const
inline

Returns quotient of *this and scalar x, where division by 0 yields 0.

template<typename T>
this_type dai::TProb< T >::operator^ ( x) const
inline

Returns *this raised to the power x.

template<typename T>
template<typename binaryOp >
this_type& dai::TProb< T >::pwBinaryOp ( const this_type q,
binaryOp  op 
)
inline

Applies binary operation pointwise on two vectors.

Template Parameters
binaryOpType of function object that accepts two arguments of type T and outputs a type T
Parameters
qRight operand
opOperation of type binaryOp
template<typename T>
this_type& dai::TProb< T >::operator+= ( const this_type q)
inline

Pointwise addition with q.

Precondition
this->size() == q.size()
template<typename T>
this_type& dai::TProb< T >::operator-= ( const this_type q)
inline

Pointwise subtraction of q.

Precondition
this->size() == q.size()
template<typename T>
this_type& dai::TProb< T >::operator*= ( const this_type q)
inline

Pointwise multiplication with q.

Precondition
this->size() == q.size()
template<typename T>
this_type& dai::TProb< T >::operator/= ( const this_type q)
inline

Pointwise division by q, where division by 0 yields 0.

Precondition
this->size() == q.size()
See also
divide(const TProb<T> &)
template<typename T>
this_type& dai::TProb< T >::divide ( const this_type q)
inline

Pointwise division by q, where division by 0 yields +Inf.

Precondition
this->size() == q.size()
See also
operator/=(const TProb<T> &)
template<typename T>
this_type& dai::TProb< T >::operator^= ( const this_type q)
inline

Pointwise power.

Precondition
this->size() == q.size()
template<typename T>
template<typename binaryOp >
this_type dai::TProb< T >::pwBinaryTr ( const this_type q,
binaryOp  op 
) const
inline

Returns the result of applying binary operation op pointwise on *this and q.

Template Parameters
binaryOpType of function object that accepts two arguments of type T and outputs a type T
Parameters
qRight operand
opOperation of type binaryOp
template<typename T>
this_type dai::TProb< T >::operator+ ( const this_type q) const
inline

Returns sum of *this and q.

Precondition
this->size() == q.size()
template<typename T>
this_type dai::TProb< T >::operator- ( const this_type q) const
inline

Return *this minus q.

Precondition
this->size() == q.size()
template<typename T>
this_type dai::TProb< T >::operator* ( const this_type q) const
inline

Return product of *this with q.

Precondition
this->size() == q.size()
template<typename T>
this_type dai::TProb< T >::operator/ ( const this_type q) const
inline

Returns quotient of *this with q, where division by 0 yields 0.

Precondition
this->size() == q.size()
See also
divided_by(const TProb<T> &)
template<typename T>
this_type dai::TProb< T >::divided_by ( const this_type q) const
inline

Pointwise division by q, where division by 0 yields +Inf.

Precondition
this->size() == q.size()
See also
operator/(const TProb<T> &)
template<typename T>
this_type dai::TProb< T >::operator^ ( const this_type q) const
inline

Returns *this to the power q.

Precondition
this->size() == q.size()
template<typename T>
template<typename binOp1 , typename binOp2 >
T dai::TProb< T >::innerProduct ( const this_type q,
init,
binOp1  binaryOp1,
binOp2  binaryOp2 
) const
inline

Performs a generalized inner product, similar to std::inner_product.

Precondition
this->size() == q.size()

Friends And Related Function Documentation

template<typename T >
T dist ( const TProb< T > &  p,
const TProb< T > &  q,
ProbDistType  dt 
)
related

Returns distance between p and q, measured using distance measure dt.

Precondition
this->size() == q.size()
template<typename T >
std::ostream & operator<< ( std::ostream &  os,
const TProb< T > &  p 
)
related

Writes a TProb<T> to an output stream.

template<typename T >
TProb< T > min ( const TProb< T > &  a,
const TProb< T > &  b 
)
related

Returns the pointwise minimum of a and b.

Precondition
this->size() == q.size()
template<typename T >
TProb< T > max ( const TProb< T > &  a,
const TProb< T > &  b 
)
related

Returns the pointwise maximum of a and b.

Precondition
this->size() == q.size()

Member Data Documentation

template<typename T>
container_type dai::TProb< T >::_p
private

The data structure that stores the values.


The documentation for this class was generated from the following file: