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. 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 > | |
T | 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 ) 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 | |
T | 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_type & | p () const |
Returns a const reference to the wrapped container. More... | |
container_type & | p () |
Returns a reference to the wrapped container. More... | |
T | 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 > | |
T | accumulateSum (T init, unOp op) const |
Accumulate all values (similar to std::accumulate) by summing. More... | |
template<typename unOp > | |
T | accumulateMax (T init, unOp op, bool minimize) const |
Accumulate all values (similar to std::accumulate) by maximization/minimization. More... | |
T | entropy () const |
Returns the Shannon entropy of *this , . More... | |
T | max () const |
Returns maximum value of all entries. More... | |
T | min () const |
Returns minimum value of all entries. More... | |
T | sum () const |
Returns sum of all entries. More... | |
T | sumAbs () const |
Return sum of absolute value of all entries. More... | |
T | 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_type & | pwUnaryOp (unaryOp op) |
Applies unary operation op pointwise. More... | |
this_type & | randomize () |
Draws all entries i.i.d. from a uniform distribution on [0,1) More... | |
this_type & | setUniform () |
Sets all entries to where n is the length of the vector. More... | |
this_type & | takeAbs () |
Applies absolute value pointwise. More... | |
this_type & | takeExp () |
Applies exponent pointwise. More... | |
this_type & | takeLog (bool zero=false) |
Applies logarithm pointwise. More... | |
T | normalize (ProbNormType norm=dai::NORMPROB) |
Normalizes vector using the specified norm. More... | |
Operations with scalars | |
this_type & | fill (T x) |
Sets all entries to x. More... | |
this_type & | operator+= (T x) |
Adds scalar x to each entry. More... | |
this_type & | operator-= (T x) |
Subtracts scalar x from each entry. More... | |
this_type & | operator*= (T x) |
Multiplies each entry with scalar x. More... | |
this_type & | operator/= (T x) |
Divides each entry by scalar x, where division by 0 yields 0. More... | |
this_type & | operator^= (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_type & | pwBinaryOp (const this_type &q, binaryOp op) |
Applies binary operation pointwise on two vectors. More... | |
this_type & | operator+= (const this_type &q) |
Pointwise addition with q. More... | |
this_type & | operator-= (const this_type &q) |
Pointwise subtraction of q. More... | |
this_type & | operator*= (const this_type &q) |
Pointwise multiplication with q. More... | |
this_type & | operator/= (const this_type &q) |
Pointwise division by q, where division by 0 yields 0. More... | |
this_type & | divide (const this_type &q) |
Pointwise division by q, where division by 0 yields +Inf. More... | |
this_type & | operator^= (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 > | |
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... | |
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.
|
inline |
Default constructor (constructs empty vector)
|
inlineexplicit |
Construct uniform probability distribution over n outcomes (i.e., a vector of length n with each entry set to )
|
inlineexplicit |
Construct vector of length n with each entry set to p.
|
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. |
|
inline |
Construct vector from another vector.
S | type of elements in v (should be castable to type T) |
v | vector used for initialization. |
|
inline |
Returns iterator that points to the first element.
|
inline |
Returns constant iterator that points to the first element.
|
inline |
Returns iterator that points beyond the last element.
|
inline |
Returns constant iterator that points beyond the last element.
|
inline |
Returns reverse iterator that points to the last element.
|
inline |
Returns constant reverse iterator that points to the last element.
|
inline |
Returns reverse iterator that points beyond the first element.
|
inline |
Returns constant reverse iterator that points beyond the first element.
|
inline |
Gets i 'th entry.
|
inline |
Sets i 'th entry to val.
|
inline |
Returns a const reference to the wrapped container.
|
inline |
Returns a reference to the wrapped container.
|
inline |
Returns a copy of the i 'th entry.
|
inline |
Returns length of the vector (i.e., the number of entries)
|
inline |
Accumulate all values (similar to std::accumulate) by summing.
The following calculation is done:
|
inline |
Accumulate all values (similar to std::accumulate) by maximization/minimization.
The following calculation is done (with "max" replaced by "min" if minimize == true
):
|
inline |
Returns the Shannon entropy of *this
, .
|
inline |
Returns maximum value of all entries.
|
inline |
Returns minimum value of all entries.
|
inline |
Returns sum of all entries.
|
inline |
Return sum of absolute value of all entries.
|
inline |
Returns maximum absolute value of all entries.
|
inline |
Returns true
if one or more entries are NaN.
|
inline |
Returns true
if one or more entries are negative.
|
inline |
Returns a pair consisting of the index of the maximum value and the maximum value itself.
|
inline |
Returns a random index, according to the (normalized) distribution described by *this.
|
inline |
Lexicographical comparison.
this->size() == q.size()
|
inline |
Comparison.
|
inline |
Formats a TProb as a string.
|
inline |
Returns the result of applying operation op pointwise on *this
.
|
inline |
Returns negative of *this
.
|
inline |
Returns pointwise absolute value.
|
inline |
Returns pointwise exponent.
|
inline |
Returns pointwise logarithm.
If zero == true
, uses log(0)==0
; otherwise, log(0)==-Inf
.
|
inline |
Returns pointwise inverse.
If zero == true
, uses 1/0==0
; otherwise, 1/0==Inf
.
|
inline |
Returns normalized copy of *this
, using the specified norm.
NOT_NORMALIZABLE | if the norm is zero |
|
inline |
Applies unary operation op pointwise.
|
inline |
Draws all entries i.i.d. from a uniform distribution on [0,1)
|
inline |
Sets all entries to where n is the length of the vector.
|
inline |
Applies absolute value pointwise.
|
inline |
Applies exponent pointwise.
|
inline |
Applies logarithm pointwise.
If zero == true
, uses log(0)==0
; otherwise, log(0)==-Inf
.
|
inline |
Normalizes vector using the specified norm.
NOT_NORMALIZABLE | if the norm is zero |
|
inline |
Sets all entries to x.
|
inline |
Adds scalar x to each entry.
|
inline |
Subtracts scalar x from each entry.
|
inline |
Multiplies each entry with scalar x.
|
inline |
Divides each entry by scalar x, where division by 0 yields 0.
|
inline |
Raises entries to the power x.
|
inline |
Returns sum of *this
and scalar x.
|
inline |
Returns difference of *this
and scalar x.
|
inline |
Returns product of *this
with scalar x.
|
inline |
Returns quotient of *this
and scalar x, where division by 0 yields 0.
|
inline |
Returns *this
raised to the power x.
|
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 |
|
inline |
Pointwise addition with q.
this->size() == q.size()
|
inline |
Pointwise subtraction of q.
this->size() == q.size()
|
inline |
Pointwise multiplication with q.
this->size() == q.size()
|
inline |
Pointwise division by q, where division by 0 yields 0.
this->size() == q.size()
|
inline |
Pointwise division by q, where division by 0 yields +Inf.
this->size() == q.size()
|
inline |
Pointwise power.
this->size() == q.size()
|
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 |
|
inline |
Returns sum of *this
and q.
this->size() == q.size()
|
inline |
Return *this
minus q.
this->size() == q.size()
|
inline |
Return product of *this
with q.
this->size() == q.size()
|
inline |
Returns quotient of *this
with q, where division by 0 yields 0.
this->size() == q.size()
|
inline |
Pointwise division by q, where division by 0 yields +Inf.
this->size() == q.size()
|
inline |
Returns *this
to the power q.
this->size() == q.size()
|
inline |
Performs a generalized inner product, similar to std::inner_product.
this->size() == q.size()
|
related |
Returns distance between p and q, measured using distance measure dt.
this->size() == q.size()
|
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()
|
private |
The data structure that stores the values.