|
libDAI
|
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. | |
| typedef TProb< T > | this_type |
| Shorthand. | |
| typedef container_type::const_iterator | const_iterator |
| Constant iterator over the elements. | |
| typedef container_type::iterator | iterator |
| Iterator over the elements. | |
| typedef container_type::const_reverse_iterator | const_reverse_iterator |
| Constant reverse iterator over the elements. | |
| typedef container_type::reverse_iterator | reverse_iterator |
| Reverse iterator over the elements. | |
Public Member Functions | |
| template<typename binOp1 , typename binOp2 > | |
| T | innerProduct (const this_type &q, T init, binOp1 binaryOp1, binOp2 binaryOp2) const |
| Performs a generalized inner product, similar to std::inner_product. | |
Constructors and destructors | |
| TProb () | |
| Default constructor (constructs empty vector) | |
| TProb (size_t n) | |
Construct uniform probability distribution over n outcomes (i.e., a vector of length n with each entry set to ) | |
| TProb (size_t n, T p) | |
| Construct vector of length n with each entry set to p. | |
| template<typename TIterator > | |
| TProb (TIterator begin, TIterator end, size_t sizeHint) | |
| Construct vector from a range. | |
| template<typename S > | |
| TProb (const std::vector< S > &v) | |
| Construct vector from another vector. | |
Iterator interface | |
| iterator | begin () |
| Returns iterator that points to the first element. | |
| const_iterator | begin () const |
| Returns constant iterator that points to the first element. | |
| iterator | end () |
| Returns iterator that points beyond the last element. | |
| const_iterator | end () const |
| Returns constant iterator that points beyond the last element. | |
| reverse_iterator | rbegin () |
| Returns reverse iterator that points to the last element. | |
| const_reverse_iterator | rbegin () const |
| Returns constant reverse iterator that points to the last element. | |
| reverse_iterator | rend () |
| Returns reverse iterator that points beyond the first element. | |
| const_reverse_iterator | rend () const |
| Returns constant reverse iterator that points beyond the first element. | |
Miscellaneous operations | |
| void | resize (size_t sz) |
Get/set individual entries | |
| T | get (size_t i) const |
| Gets i 'th entry. | |
| void | set (size_t i, T val) |
| Sets i 'th entry to val. | |
Queries | |
| const container_type & | p () const |
| Returns a const reference to the wrapped container. | |
| container_type & | p () |
| Returns a reference to the wrapped container. | |
| T | operator[] (size_t i) const |
| Returns a copy of the i 'th entry. | |
| size_t | size () const |
| Returns length of the vector (i.e., the number of entries) | |
| template<typename unOp > | |
| T | accumulateSum (T init, unOp op) const |
| Accumulate all values (similar to std::accumulate) by summing. | |
| template<typename unOp > | |
| T | accumulateMax (T init, unOp op, bool minimize) const |
| Accumulate all values (similar to std::accumulate) by maximization/minimization. | |
| T | entropy () const |
Returns the Shannon entropy of *this, . | |
| T | max () const |
| Returns maximum value of all entries. | |
| T | min () const |
| Returns minimum value of all entries. | |
| T | sum () const |
| Returns sum of all entries. | |
| T | sumAbs () const |
| Return sum of absolute value of all entries. | |
| T | maxAbs () const |
| Returns maximum absolute value of all entries. | |
| bool | hasNaNs () const |
Returns true if one or more entries are NaN. | |
| bool | hasNegatives () const |
Returns true if one or more entries are negative. | |
| std::pair< size_t, T > | argmax () const |
| Returns a pair consisting of the index of the maximum value and the maximum value itself. | |
| size_t | draw () |
| Returns a random index, according to the (normalized) distribution described by *this. | |
| bool | operator< (const this_type &q) const |
| Lexicographical comparison. | |
| bool | operator== (const this_type &q) const |
| Comparison. | |
Unary transformations | |
| template<typename unaryOp > | |
| this_type | pwUnaryTr (unaryOp op) const |
Returns the result of applying operation op pointwise on *this. | |
| this_type | operator- () const |
Returns negative of *this. | |
| this_type | abs () const |
| Returns pointwise absolute value. | |
| this_type | exp () const |
| Returns pointwise exponent. | |
| this_type | log (bool zero=false) const |
| Returns pointwise logarithm. | |
| this_type | inverse (bool zero=true) const |
| Returns pointwise inverse. | |
| this_type | normalized (ProbNormType norm=dai::NORMPROB) const |
Returns normalized copy of *this, using the specified norm. | |
Unary operations | |
| template<typename unaryOp > | |
| this_type & | pwUnaryOp (unaryOp op) |
| Applies unary operation op pointwise. | |
| this_type & | randomize () |
| Draws all entries i.i.d. from a uniform distribution on [0,1) | |
| this_type & | setUniform () |
Sets all entries to where n is the length of the vector. | |
| this_type & | takeAbs () |
| Applies absolute value pointwise. | |
| this_type & | takeExp () |
| Applies exponent pointwise. | |
| this_type & | takeLog (bool zero=false) |
| Applies logarithm pointwise. | |
| T | normalize (ProbNormType norm=dai::NORMPROB) |
| Normalizes vector using the specified norm. | |
Operations with scalars | |
| this_type & | fill (T x) |
| Sets all entries to x. | |
| this_type & | operator+= (T x) |
| Adds scalar x to each entry. | |
| this_type & | operator-= (T x) |
| Subtracts scalar x from each entry. | |
| this_type & | operator*= (T x) |
| Multiplies each entry with scalar x. | |
| this_type & | operator/= (T x) |
| Divides each entry by scalar x, where division by 0 yields 0. | |
| this_type & | operator^= (T x) |
| Raises entries to the power x. | |
Transformations with scalars | |
| this_type | operator+ (T x) const |
Returns sum of *this and scalar x. | |
| this_type | operator- (T x) const |
Returns difference of *this and scalar x. | |
| this_type | operator* (T x) const |
Returns product of *this with scalar x. | |
| this_type | operator/ (T x) const |
Returns quotient of *this and scalar x, where division by 0 yields 0. | |
| this_type | operator^ (T x) const |
Returns *this raised to the power x. | |
Operations with other equally-sized vectors | |
| template<typename binaryOp > | |
| this_type & | pwBinaryOp (const this_type &q, binaryOp op) |
| Applies binary operation pointwise on two vectors. | |
| this_type & | operator+= (const this_type &q) |
| Pointwise addition with q. | |
| this_type & | operator-= (const this_type &q) |
| Pointwise subtraction of q. | |
| this_type & | operator*= (const this_type &q) |
| Pointwise multiplication with q. | |
| this_type & | operator/= (const this_type &q) |
| Pointwise division by q, where division by 0 yields 0. | |
| this_type & | divide (const this_type &q) |
| Pointwise division by q, where division by 0 yields +Inf. | |
| this_type & | operator^= (const this_type &q) |
| Pointwise power. | |
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. | |
| this_type | operator+ (const this_type &q) const |
Returns sum of *this and q. | |
| this_type | operator- (const this_type &q) const |
Return *this minus q. | |
| this_type | operator* (const this_type &q) const |
Return product of *this with q. | |
| this_type | operator/ (const this_type &q) const |
Returns quotient of *this with q, where division by 0 yields 0. | |
| this_type | divided_by (const this_type &q) const |
| Pointwise division by q, where division by 0 yields +Inf. | |
| this_type | operator^ (const this_type &q) const |
Returns *this to the power q. | |
Private Attributes | |
| container_type | _p |
| The data structure that stores the values. | |
Related Functions | |
(Note that these are not member functions.) | |
| template<typename T > | |
| T | dist (const TProb< T > &p, const TProb< T > &q, ProbDistType dt) |
| Returns distance between p and q, measured using distance measure dt. | |
| template<typename T > | |
| std::ostream & | operator<< (std::ostream &os, const TProb< T > &p) |
| Writes a TProb<T> to an output stream. | |
| template<typename T > | |
| TProb< T > | min (const TProb< T > &a, const TProb< T > &b) |
| Returns the pointwise minimum of a and b. | |
| template<typename T > | |
| TProb< T > | max (const TProb< T > &a, const TProb< T > &b) |
| Returns the pointwise maximum of a and b. | |
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.
| T | Should be a scalar that is castable from and to dai::Real and should support elementary arithmetic operations. |
| typedef std::vector<T> dai::TProb< T >::container_type |
Type of data structure used for storing the values.
| typedef TProb<T> dai::TProb< T >::this_type |
Shorthand.
| typedef container_type::const_iterator dai::TProb< T >::const_iterator |
Constant iterator over the elements.
| typedef container_type::iterator dai::TProb< T >::iterator |
Iterator over the elements.
| typedef container_type::const_reverse_iterator dai::TProb< T >::const_reverse_iterator |
Constant reverse iterator over the elements.
| typedef container_type::reverse_iterator dai::TProb< T >::reverse_iterator |
Reverse iterator over the elements.
| dai::TProb< T >::TProb | ( | ) | [inline] |
Default constructor (constructs empty vector)
| dai::TProb< T >::TProb | ( | size_t | n | ) | [inline, explicit] |
Construct uniform probability distribution over n outcomes (i.e., a vector of length n with each entry set to
)
| dai::TProb< T >::TProb | ( | size_t | n, |
| T | p | ||
| ) | [inline, explicit] |
Construct vector of length n with each entry set to p.
| dai::TProb< T >::TProb | ( | TIterator | begin, |
| TIterator | end, | ||
| size_t | sizeHint | ||
| ) | [inline] |
Construct vector from a range.
| TIterator | Iterates over instances that can be cast to T |
| begin | Points to first instance to be added. |
| end | Points just beyond last instance to be added. |
| sizeHint | For 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. |
| dai::TProb< T >::TProb | ( | const std::vector< S > & | v | ) | [inline] |
Construct vector from another vector.
| S | type of elements in v (should be castable to type T) |
| v | vector used for initialization. |
| iterator dai::TProb< T >::begin | ( | ) | [inline] |
Returns iterator that points to the first element.
| const_iterator dai::TProb< T >::begin | ( | ) | const [inline] |
Returns constant iterator that points to the first element.
| iterator dai::TProb< T >::end | ( | ) | [inline] |
Returns iterator that points beyond the last element.
| const_iterator dai::TProb< T >::end | ( | ) | const [inline] |
Returns constant iterator that points beyond the last element.
| reverse_iterator dai::TProb< T >::rbegin | ( | ) | [inline] |
Returns reverse iterator that points to the last element.
| const_reverse_iterator dai::TProb< T >::rbegin | ( | ) | const [inline] |
Returns constant reverse iterator that points to the last element.
| reverse_iterator dai::TProb< T >::rend | ( | ) | [inline] |
Returns reverse iterator that points beyond the first element.
| const_reverse_iterator dai::TProb< T >::rend | ( | ) | const [inline] |
Returns constant reverse iterator that points beyond the first element.
| T dai::TProb< T >::get | ( | size_t | i | ) | const [inline] |
Gets i 'th entry.
| void dai::TProb< T >::set | ( | size_t | i, |
| T | val | ||
| ) | [inline] |
Sets i 'th entry to val.
| const container_type& dai::TProb< T >::p | ( | ) | const [inline] |
Returns a const reference to the wrapped container.
| container_type& dai::TProb< T >::p | ( | ) | [inline] |
Returns a reference to the wrapped container.
| T dai::TProb< T >::operator[] | ( | size_t | i | ) | const [inline] |
Returns a copy of the i 'th entry.
| size_t dai::TProb< T >::size | ( | ) | const [inline] |
Returns length of the vector (i.e., the number of entries)
| T dai::TProb< T >::accumulateSum | ( | T | 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;
| T dai::TProb< T >::accumulateMax | ( | T | 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;
| T dai::TProb< T >::entropy | ( | ) | const [inline] |
Returns the Shannon entropy of *this,
.
| T dai::TProb< T >::max | ( | ) | const [inline] |
Returns maximum value of all entries.
| T dai::TProb< T >::min | ( | ) | const [inline] |
Returns minimum value of all entries.
| T dai::TProb< T >::sum | ( | ) | const [inline] |
Returns sum of all entries.
| T dai::TProb< T >::sumAbs | ( | ) | const [inline] |
Return sum of absolute value of all entries.
| T dai::TProb< T >::maxAbs | ( | ) | const [inline] |
Returns maximum absolute value of all entries.
| bool dai::TProb< T >::hasNaNs | ( | ) | const [inline] |
Returns true if one or more entries are NaN.
| bool dai::TProb< T >::hasNegatives | ( | ) | const [inline] |
Returns true if one or more entries are negative.
| 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.
| size_t dai::TProb< T >::draw | ( | ) | [inline] |
Returns a random index, according to the (normalized) distribution described by *this.
| bool dai::TProb< T >::operator< | ( | const this_type & | q | ) | const [inline] |
Lexicographical comparison.
this->size() == q.size() | bool dai::TProb< T >::operator== | ( | const this_type & | q | ) | const [inline] |
Comparison.
| this_type dai::TProb< T >::pwUnaryTr | ( | unaryOp | op | ) | const [inline] |
Returns the result of applying operation op pointwise on *this.
| this_type dai::TProb< T >::operator- | ( | ) | const [inline] |
Returns negative of *this.
| this_type dai::TProb< T >::abs | ( | ) | const [inline] |
Returns pointwise absolute value.
| this_type dai::TProb< T >::exp | ( | ) | const [inline] |
Returns pointwise exponent.
| 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.
| 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.
| this_type dai::TProb< T >::normalized | ( | ProbNormType | norm = dai::NORMPROB | ) | const [inline] |
Returns normalized copy of *this, using the specified norm.
| NOT_NORMALIZABLE | if the norm is zero |
| this_type& dai::TProb< T >::pwUnaryOp | ( | unaryOp | op | ) | [inline] |
Applies unary operation op pointwise.
| this_type& dai::TProb< T >::randomize | ( | ) | [inline] |
Draws all entries i.i.d. from a uniform distribution on [0,1)
| this_type& dai::TProb< T >::setUniform | ( | ) | [inline] |
Sets all entries to
where n is the length of the vector.
| this_type& dai::TProb< T >::takeAbs | ( | ) | [inline] |
Applies absolute value pointwise.
| this_type& dai::TProb< T >::takeExp | ( | ) | [inline] |
Applies exponent pointwise.
| this_type& dai::TProb< T >::takeLog | ( | bool | zero = false | ) | [inline] |
Applies logarithm pointwise.
If zero == true, uses log(0)==0; otherwise, log(0)==-Inf.
| T dai::TProb< T >::normalize | ( | ProbNormType | norm = dai::NORMPROB | ) | [inline] |
Normalizes vector using the specified norm.
| NOT_NORMALIZABLE | if the norm is zero |
| this_type& dai::TProb< T >::fill | ( | T | x | ) | [inline] |
Sets all entries to x.
| this_type& dai::TProb< T >::operator+= | ( | T | x | ) | [inline] |
Adds scalar x to each entry.
| this_type& dai::TProb< T >::operator-= | ( | T | x | ) | [inline] |
Subtracts scalar x from each entry.
| this_type& dai::TProb< T >::operator*= | ( | T | x | ) | [inline] |
Multiplies each entry with scalar x.
| this_type& dai::TProb< T >::operator/= | ( | T | x | ) | [inline] |
Divides each entry by scalar x, where division by 0 yields 0.
| this_type& dai::TProb< T >::operator^= | ( | T | x | ) | [inline] |
Raises entries to the power x.
| this_type dai::TProb< T >::operator+ | ( | T | x | ) | const [inline] |
Returns sum of *this and scalar x.
| this_type dai::TProb< T >::operator- | ( | T | x | ) | const [inline] |
Returns difference of *this and scalar x.
| this_type dai::TProb< T >::operator* | ( | T | x | ) | const [inline] |
Returns product of *this with scalar x.
| this_type dai::TProb< T >::operator/ | ( | T | x | ) | const [inline] |
Returns quotient of *this and scalar x, where division by 0 yields 0.
| this_type dai::TProb< T >::operator^ | ( | T | x | ) | const [inline] |
Returns *this raised to the power x.
| this_type& dai::TProb< T >::pwBinaryOp | ( | const this_type & | q, |
| binaryOp | op | ||
| ) | [inline] |
Applies binary operation pointwise on two vectors.
| binaryOp | Type of function object that accepts two arguments of type T and outputs a type T |
| q | Right operand |
| op | Operation of type binaryOp |
| this_type& dai::TProb< T >::operator+= | ( | const this_type & | q | ) | [inline] |
Pointwise addition with q.
this->size() == q.size() | this_type& dai::TProb< T >::operator-= | ( | const this_type & | q | ) | [inline] |
Pointwise subtraction of q.
this->size() == q.size() | this_type& dai::TProb< T >::operator*= | ( | const this_type & | q | ) | [inline] |
Pointwise multiplication with q.
this->size() == q.size() | this_type& dai::TProb< T >::operator/= | ( | const this_type & | q | ) | [inline] |
Pointwise division by q, where division by 0 yields 0.
this->size() == q.size() | this_type& dai::TProb< T >::divide | ( | const this_type & | q | ) | [inline] |
Pointwise division by q, where division by 0 yields +Inf.
this->size() == q.size() | this_type& dai::TProb< T >::operator^= | ( | const this_type & | q | ) | [inline] |
Pointwise power.
this->size() == q.size() | 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.
| binaryOp | Type of function object that accepts two arguments of type T and outputs a type T |
| q | Right operand |
| op | Operation of type binaryOp |
| this_type dai::TProb< T >::operator+ | ( | const this_type & | q | ) | const [inline] |
Returns sum of *this and q.
this->size() == q.size() | this_type dai::TProb< T >::operator- | ( | const this_type & | q | ) | const [inline] |
Return *this minus q.
this->size() == q.size() | this_type dai::TProb< T >::operator* | ( | const this_type & | q | ) | const [inline] |
Return product of *this with q.
this->size() == q.size() | this_type dai::TProb< T >::operator/ | ( | const this_type & | q | ) | const [inline] |
Returns quotient of *this with q, where division by 0 yields 0.
this->size() == q.size() | this_type dai::TProb< T >::divided_by | ( | const this_type & | q | ) | const [inline] |
Pointwise division by q, where division by 0 yields +Inf.
this->size() == q.size() | this_type dai::TProb< T >::operator^ | ( | const this_type & | q | ) | const [inline] |
Returns *this to the power q.
this->size() == q.size() | T dai::TProb< T >::innerProduct | ( | const this_type & | q, |
| T | init, | ||
| binOp1 | binaryOp1, | ||
| binOp2 | binaryOp2 | ||
| ) | const [inline] |
Performs a generalized inner product, similar to std::inner_product.
this->size() == q.size() | T dist | ( | const TProb< T > & | p, |
| const TProb< T > & | q, | ||
| ProbDistType | dt | ||
| ) | [related] |
Returns distance between p and q, measured using distance measure dt.
this->size() == q.size() | std::ostream & operator<< | ( | std::ostream & | os, |
| const TProb< T > & | p | ||
| ) | [related] |
Writes a TProb<T> to an output stream.
Returns the pointwise minimum of a and b.
this->size() == q.size() Returns the pointwise maximum of a and b.
this->size() == q.size() container_type dai::TProb< T >::_p [private] |
The data structure that stores the values.
1.7.4