bpp-core  2.2.0
bpp::VectorTools Class Reference

#include <Bpp/Numeric/VectorTools.h>

Classes

class  order_Cmp_
 

Public Member Functions

 VectorTools ()
 
virtual ~VectorTools ()
 

Static Public Member Functions

template<class T >
static void fill (std::vector< T > &v, T value)
 
template<class T >
static std::vector< T > seq (T from, T to, T by)
 Build a sequence std::vector. More...
 
template<class T >
static size_t which (const std::vector< T > &v, const T &which) throw (ElementNotFoundException<T> )
 Send the position of the first occurence of 'which'. More...
 
template<class T >
static std::vector< size_t > whichAll (const std::vector< T > &v, const T &which) throw (ElementNotFoundException<T> )
 Send the positions of all occurences of 'which'. More...
 
template<class T >
static std::vector< T > unique (const std::vector< T > &v)
 Send a new std::vector with unique elements. More...
 
template<class T >
static bool isUnique (const std::vector< T > &v)
 Tell if the std::vector as unique elements. More...
 
template<class T >
static std::vector< T > extract (const std::vector< T > &v1, const std::vector< int > &v2)
 
template<class T >
static std::map< T, size_t > countValues (const std::vector< T > &v)
 Count each element of a std::vector. More...
 
static std::vector< double > breaks (const std::vector< double > &v, unsigned int n)
 Get the break points for a given number of classes. More...
 
template<class T >
static size_t nclassScott (const std::vector< T > &v)
 Get the optimal class number following Scott's method. More...
 
template<class T >
static T prod (const std::vector< T > &v1)
 
template<class T >
static std::vector< T > cumProd (const std::vector< T > &v1)
 
template<class T >
static T sum (const std::vector< T > &v1)
 
template<class T >
static std::vector< T > cumSum (const std::vector< T > &v1)
 
template<class T >
static void logNorm (std::vector< T > &v)
 
template<class T >
static T logSumExp (const std::vector< T > &v1)
 
template<class T >
static T logSumExp (const std::vector< T > &v1, const std::vector< T > &v2)
 
template<class T >
static T logMeanExp (const std::vector< T > &v1)
 
template<class T >
static T sumExp (const std::vector< T > &v1)
 
template<class T >
static T sumExp (const std::vector< T > &v1, const std::vector< T > &v2)
 
template<class T >
static std::string paste (const std::vector< T > &v, const std::string &delim=" ")
 Concatenate a std::vector after converting to string. More...
 
template<class T >
static void print (const std::vector< T > &v1, OutputStream &out= *ApplicationTools::message, const std::string &delim=" ")
 Print a std::vector to a stream. More...
 
template<class T >
static void printForR (const std::vector< T > &v1, std::string variableName="x", std::ostream &out=std::cout)
 Print a std::vector to a stream in R format. More...
 
template<class InputType , class OutputType >
static OutputType scalar (const std::vector< InputType > &v1, const std::vector< InputType > &v2) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType scalar (const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w) throw (DimensionException)
 
template<class T >
static std::vector< T > kroneckerMult (const std::vector< T > &v1, const std::vector< T > &v2) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType norm (const std::vector< InputType > &v1)
 
template<class InputType , class OutputType >
static OutputType norm (const std::vector< InputType > &v1, const std::vector< InputType > &w) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType cos (const std::vector< InputType > &v1, const std::vector< InputType > &v2) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType cos (const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w) throw (DimensionException)
 
template<class T >
static std::vector< T > abs (const std::vector< T > &v) throw (EmptyVectorException<T> )
 Template function to get the absolute value of all elements of a std::vector. More...
 
template<class InputType , class OutputType >
static OutputType mean (const std::vector< InputType > &v1)
 
template<class InputType , class OutputType >
static OutputType mean (const std::vector< InputType > &v1, const std::vector< InputType > &w, bool normalizeWeights=true)
 
template<class InputType >
static InputType median (std::vector< InputType > &v1)
 
template<class InputType , class OutputType >
static std::vector< OutputType > center (const std::vector< InputType > &v1)
 Set the mean of a std::vector to be 0. More...
 
template<class InputType , class OutputType >
static std::vector< OutputType > center (const std::vector< InputType > &v1, const std::vector< InputType > &w, bool normalizeWeights=true)
 Set the weighted mean of a std::vector to be 0. More...
 
template<class InputType , class OutputType >
static OutputType cov (const std::vector< InputType > &v1, const std::vector< InputType > &v2, bool unbiased=true) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType cov (const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w, bool unbiased=true, bool normalizeWeights=true) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType var (const std::vector< InputType > &v1, bool unbiased=true)
 
template<class InputType , class OutputType >
static OutputType var (const std::vector< InputType > &v1, const std::vector< InputType > &w, bool unbiased=true, bool normalizeWeights=true) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType sd (const std::vector< InputType > &v1, bool unbiased=true)
 
template<class InputType , class OutputType >
static OutputType sd (const std::vector< InputType > &v1, const std::vector< InputType > &w, bool unbiased=true, bool normalizeWeights=true) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType cor (const std::vector< InputType > &v1, const std::vector< InputType > &v2) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType cor (const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w, bool normalizeWeights=true) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType shannon (const std::vector< InputType > &v, double base=2.7182818)
 
template<class InputType , class OutputType >
static OutputType shannonDiscrete (const std::vector< InputType > &v, double base=2.7182818)
 
template<class InputType , class OutputType >
static OutputType miDiscrete (const std::vector< InputType > &v1, const std::vector< InputType > &v2, double base=2.7182818) throw (DimensionException)
 
template<class InputType , class OutputType >
static OutputType shannonContinuous (const std::vector< InputType > &v, double base=2.7182818)
 
template<class InputType , class OutputType >
static OutputType miContinuous (const std::vector< InputType > &v1, const std::vector< InputType > &v2, double base=2.7182818) throw (DimensionException)
 
template<class T >
static bool haveSameElements (const std::vector< T > &v1, const std::vector< T > &v2)
 
template<class T >
static bool haveSameElements (std::vector< T > &v1, std::vector< T > &v2)
 
template<class T >
static bool contains (const std::vector< T > &vec, T el)
 
template<class T >
static bool containsAll (std::vector< T > &v1, std::vector< T > &v2)
 
template<class T >
static std::vector< T > vectorUnion (const std::vector< T > &vec1, const std::vector< T > &vec2)
 
template<class T >
static std::vector< T > vectorUnion (const std::vector< std::vector< T > > &vecElementL)
 
template<class T >
static std::vector< T > vectorIntersection (const std::vector< T > &vec1, const std::vector< T > &vec2)
 
template<class T >
static std::vector< T > vectorIntersection (const std::vector< std::vector< T > > &vecElementL)
 
template<class T >
static void append (std::vector< T > &vec1, const std::vector< T > &vec2)
 Append the content of a std::vector to another one. More...
 
template<class T >
static void prepend (std::vector< T > &vec1, const std::vector< T > &vec2)
 Prepend the content of a std::vector to another one. More...
 
template<class T >
static std::vector< T > append (const std::vector< std::vector< T > > &vecElementL)
 
template<class T >
static void extend (std::vector< T > &vec1, const std::vector< T > &vec2)
 Extend the content of a std::vector with another one. Only the elements not present in the first vector will be added. More...
 
template<class T >
static std::vector< T > rep (const std::vector< T > &vec, size_t n)
 
template<class T >
static void diff (std::vector< T > &v1, std::vector< T > &v2, std::vector< T > &v3)
 This function returns the difference of two std::vectors. More...
 
static bool test ()
 Test function. More...
 
Matrix-like functions to resize arrays.
template<class T >
static void resize2 (VVdouble &vv, size_t n1, size_t n2)
 
template<class T >
static void resize3 (VVVdouble &vvv, size_t n1, size_t n2, size_t n3)
 
static void resize4 (VVVVdouble &vvvv, size_t n1, size_t n2, size_t n3, size_t n4)
 
These methods apply the corresponding function to each element

and return the result in a new std::vector.

template<class T >
static std::vector< double > log (const std::vector< T > &v1)
 
template<class T >
static std::vector< double > log (const std::vector< T > &v1, double base)
 
template<class T >
static std::vector< double > exp (const std::vector< T > &v1)
 
template<class T >
static std::vector< double > cos (const std::vector< T > &v1)
 
template<class T >
static std::vector< double > sin (const std::vector< T > &v1)
 
template<class T >
static std::vector< double > log10 (const std::vector< T > &v1)
 
template<class T >
static std::vector< T > fact (const std::vector< T > &v1)
 
template<class T >
static std::vector< T > sqr (const std::vector< T > &v1)
 
template<class T >
static std::vector< T > pow (const std::vector< T > &v1, T &b)
 
Extrema.
template<class T >
static T min (const std::vector< T > &v) throw (EmptyVectorException<T> )
 Template function to get the minimum value of a std::vector. More...
 
template<class T >
static T max (const std::vector< T > &v) throw (EmptyVectorException<T> )
 Template function to get the maximum value of a std::vector. More...
 
template<class T >
static size_t whichMax (const std::vector< T > &v) throw (EmptyVectorException<T> )
 Template function to get the index of the maximum value of a std::vector. More...
 
template<class T >
static size_t whichMin (const std::vector< T > &v) throw (EmptyVectorException<T> )
 Template function to get the index of the minimum value of a std::vector. More...
 
template<class T >
static std::vector< size_t > whichMaxAll (const std::vector< T > &v) throw (EmptyVectorException<T> )
 Template function to get the indices of the maximum value of a std::vector. More...
 
template<class T >
static std::vector< size_t > whichMinAll (const std::vector< T > &v) throw (EmptyVectorException<T> )
 Template function to get the indices of the minimum value of a std::vector. More...
 
template<class T >
static std::vector< T > range (const std::vector< T > &v) throw (EmptyVectorException<T> )
 Template function to get both extrema of a std::vector. More...
 
template<class T >
static std::vector< size_t > order (const std::vector< T > &v) throw (EmptyVectorException<T> )
 Template function to get the order of elements in the input vector. More...
 

Detailed Description

Definition at line 329 of file VectorTools.h.

Constructor & Destructor Documentation

◆ VectorTools()

bpp::VectorTools::VectorTools ( )
inline

Definition at line 332 of file VectorTools.h.

◆ ~VectorTools()

virtual bpp::VectorTools::~VectorTools ( )
inlinevirtual

Definition at line 333 of file VectorTools.h.

Member Function Documentation

◆ abs()

template<class T >
static std::vector<T> bpp::VectorTools::abs ( const std::vector< T > &  v)
throw (EmptyVectorException< T >
)
inlinestatic

Template function to get the absolute value of all elements of a std::vector.

Parameters
vThe input std::vector.
Returns
A vector with all absolute values.

Definition at line 1256 of file VectorTools.h.

◆ append() [1/2]

template<class T >
static void bpp::VectorTools::append ( std::vector< T > &  vec1,
const std::vector< T > &  vec2 
)
inlinestatic

Append the content of a std::vector to another one.

Parameters
vec1Vector 1.
vec2Vector 2.

Definition at line 1835 of file VectorTools.h.

Referenced by diff(), bpp::AbstractParameterAliasable::getAlias(), and bpp::NumCalcApplicationTools::seqFromString().

◆ append() [2/2]

template<class T >
static std::vector<T> bpp::VectorTools::append ( const std::vector< std::vector< T > > &  vecElementL)
inlinestatic
Returns
A single std::vector made of the concatenation of the std::vectors passed as input.
Parameters
vecElementLA std::vector of std::vectors.

Definition at line 1861 of file VectorTools.h.

◆ breaks()

vector< double > VectorTools::breaks ( const std::vector< double > &  v,
unsigned int  n 
)
static

Get the break points for a given number of classes.

Given a std::vector of values, return the values that cut the range of values in a given number of classes.

Parameters
vThe std::vector to parse.
nThe expected number of classes.
Returns
a std::vector of size = n + 1 containing the breaking points.

Definition at line 53 of file VectorTools.cpp.

References range().

◆ center() [1/2]

template<class InputType , class OutputType >
static std::vector<OutputType> bpp::VectorTools::center ( const std::vector< InputType > &  v1)
inlinestatic

Set the mean of a std::vector to be 0.

Returns
A std::vector with mean 0.
Parameters
v1A std::vector.

Definition at line 1327 of file VectorTools.h.

◆ center() [2/2]

template<class InputType , class OutputType >
static std::vector<OutputType> bpp::VectorTools::center ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  w,
bool  normalizeWeights = true 
)
inlinestatic

Set the weighted mean of a std::vector to be 0.

Returns
A std::vector with mean 0.
Parameters
v1A std::vector.
wA std::vector of weights.
normalizeWeightsTell if weights should be normalized so that they sum to 1.

Definition at line 1346 of file VectorTools.h.

◆ contains()

template<class T >
static bool bpp::VectorTools::contains ( const std::vector< T > &  vec,
el 
)
inlinestatic
Returns
'true' if a the input std::vector contains the given element.
Parameters
vecThe std::vector to check.
elThe element to look for.

Definition at line 1721 of file VectorTools.h.

Referenced by extend(), vectorIntersection(), and vectorUnion().

◆ containsAll()

template<class T >
static bool bpp::VectorTools::containsAll ( std::vector< T > &  v1,
std::vector< T > &  v2 
)
inlinestatic
Returns
'true' if a the first std::vector contains all elements of the second std::vector.
Warning
The two input std::vectors will be sorted.
Parameters
v1The first std::vector to check.
v2The second std::vector to check.

Definition at line 1739 of file VectorTools.h.

◆ cor() [1/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::cor ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  v2 
)
throw (DimensionException
)
inlinestatic
Returns
The Pearson correlation coefficient of two std::vectors.
Parameters
v1First std::vector.
v2Second std::vector.
Exceptions
DimensionExceptionIf the two std::vector do not have the same length.

Definition at line 1473 of file VectorTools.h.

◆ cor() [2/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::cor ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  v2,
const std::vector< InputType > &  w,
bool  normalizeWeights = true 
)
throw (DimensionException
)
inlinestatic
Returns
The weighted Pearson correlation coefficient of two std::vectors.
Parameters
v1First std::vector.
v2Second std::vector.
wA std::vector of weights.
normalizeWeightsTell if weights should be normalized so that they sum to 1.
Exceptions
DimensionExceptionIf the two std::vector do not have the same length.

Definition at line 1488 of file VectorTools.h.

References sum().

◆ cos() [1/3]

template<class T >
static std::vector<double> bpp::VectorTools::cos ( const std::vector< T > &  v1)
inlinestatic

Definition at line 784 of file VectorTools.h.

◆ cos() [2/3]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::cos ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  v2 
)
throw (DimensionException
)
inlinestatic
Returns
The cosinus of the angle of two std::vectors.
Parameters
v1First std::vector.
v2Second std::vector.
Exceptions
DimensionExceptionIf the two std::vector do not have the same length.

Definition at line 1012 of file VectorTools.h.

◆ cos() [3/3]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::cos ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  v2,
const std::vector< InputType > &  w 
)
throw (DimensionException
)
inlinestatic
Returns
The weighted cosinus of the angle of two std::vectors.
Parameters
v1First std::vector.
v2Second std::vector.
wA std::vector of weights.
Exceptions
DimensionExceptionIf the two std::vector do not have the same length.

Definition at line 1026 of file VectorTools.h.

◆ countValues()

template<class T >
static std::map<T, size_t> bpp::VectorTools::countValues ( const std::vector< T > &  v)
inlinestatic

Count each element of a std::vector.

Returns
A map with keys = unique std::vector values and values = count for each std::vector value.
Parameters
vthe std::vector to parse.

Definition at line 540 of file VectorTools.h.

◆ cov() [1/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::cov ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  v2,
bool  unbiased = true 
)
throw (DimensionException
)
inlinestatic
Returns
The covariance of two std::vectors.
Parameters
v1First std::vector.
v2Second std::vector.
unbiasedTell if an unbiased estimate must be computed.
Exceptions
DimensionExceptionIf the two std::vector do not have the same length.

Definition at line 1365 of file VectorTools.h.

◆ cov() [2/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::cov ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  v2,
const std::vector< InputType > &  w,
bool  unbiased = true,
bool  normalizeWeights = true 
)
throw (DimensionException
)
inlinestatic
Returns
The weighted covariance of two std::vectors. To have a population estimate you have to multiply by $\frac{n}{n-1}$.
Parameters
v1First std::vector.
v2Second std::vector.
wA std::vector of weights.
unbiasedTell if an unbiased estimate must be computed.
normalizeWeightsTell if weights should be normalized so that they sum to 1.
Exceptions
DimensionExceptionIf the two std::vector do not have the same length.

Definition at line 1387 of file VectorTools.h.

References sqr(), and sum().

◆ cumProd()

template<class T >
static std::vector<T> bpp::VectorTools::cumProd ( const std::vector< T > &  v1)
inlinestatic
Returns
The cumulative product of all elements in a std::vector.
Parameters
v1A std::vector.
Author
Julien Dutheil

Definition at line 600 of file VectorTools.h.

◆ cumSum()

template<class T >
static std::vector<T> bpp::VectorTools::cumSum ( const std::vector< T > &  v1)
inlinestatic
Returns
The cumulative sum of all elements in a std::vector.
Parameters
v1A std::vector.
Author
Julien Dutheil

Definition at line 627 of file VectorTools.h.

Referenced by bpp::RandomTools::pickOne().

◆ diff()

template<class T >
static void bpp::VectorTools::diff ( std::vector< T > &  v1,
std::vector< T > &  v2,
std::vector< T > &  v3 
)
inlinestatic

This function returns the difference of two std::vectors.

Warning
The two input std::vectors will be sorted. As a consequence, the output std::vector will be also sorted.
Parameters
v1First std::vector.
v2Second std::vector.
v3A std::vector to be populated with all elements in v1 that are not found in v2.

Definition at line 1917 of file VectorTools.h.

References append().

◆ exp()

template<class T >
static std::vector<double> bpp::VectorTools::exp ( const std::vector< T > &  v1)
inlinestatic

Definition at line 776 of file VectorTools.h.

Referenced by bpp::MatrixTools::exp().

◆ extend()

template<class T >
static void bpp::VectorTools::extend ( std::vector< T > &  vec1,
const std::vector< T > &  vec2 
)
inlinestatic

Extend the content of a std::vector with another one. Only the elements not present in the first vector will be added.

Parameters
vec1Vector 1.
vec2Vector 2.

Definition at line 1879 of file VectorTools.h.

References contains().

◆ extract()

template<class T >
static std::vector<T> bpp::VectorTools::extract ( const std::vector< T > &  v1,
const std::vector< int > &  v2 
)
inlinestatic
Author
Laurent Gueguen
Parameters
v1the std::vector of elements,
v2the std::vector of the selected positions
Returns
the std::vector of the selected elements, in the order of the required positions

Definition at line 523 of file VectorTools.h.

◆ fact()

template<class T >
static std::vector<T> bpp::VectorTools::fact ( const std::vector< T > &  v1)
inlinestatic

Definition at line 808 of file VectorTools.h.

◆ fill()

template<class T >
static void bpp::VectorTools::fill ( std::vector< T > &  v,
value 
)
inlinestatic

◆ haveSameElements() [1/2]

template<class T >
static bool bpp::VectorTools::haveSameElements ( const std::vector< T > &  v1,
const std::vector< T > &  v2 
)
inlinestatic
Returns
'true' if the two std::vectors contains the same elements, whatever their order in the container.
Parameters
v1First std::vector.
v2Second std::vector.

Definition at line 1688 of file VectorTools.h.

◆ haveSameElements() [2/2]

template<class T >
static bool bpp::VectorTools::haveSameElements ( std::vector< T > &  v1,
std::vector< T > &  v2 
)
inlinestatic
Returns
'true' if the two std::vectors contains the same elements, in the same frequency, whatever their order in the container.
Warning
The two input std::vectors will be sorted.
Parameters
v1First std::vector.
v2Second std::vector.

Definition at line 1707 of file VectorTools.h.

◆ isUnique()

template<class T >
static bool bpp::VectorTools::isUnique ( const std::vector< T > &  v)
inlinestatic

Tell if the std::vector as unique elements.

The input std::vector is copied, and the copy is sorted using QuickSort algorithm. A one-pass loop then look for duplicates.

If v is empty, the method returns 'true'.

Parameters
vthe std::vector to parse.

Definition at line 503 of file VectorTools.h.

Referenced by bpp::DataTable::setColumnNames(), and bpp::DataTable::setRowNames().

◆ kroneckerMult()

template<class T >
static std::vector<T> bpp::VectorTools::kroneckerMult ( const std::vector< T > &  v1,
const std::vector< T > &  v2 
)
throw (DimensionException
)
inlinestatic
Returns
The scalar Kronecker product of two std::vectors.
Parameters
v1First std::vector.
v2Second std::vector.
Exceptions
DimensionExceptionIf the two std::vector do not have the same length.

Definition at line 952 of file VectorTools.h.

◆ log() [1/2]

template<class T >
static std::vector<double> bpp::VectorTools::log ( const std::vector< T > &  v1)
inlinestatic

Definition at line 761 of file VectorTools.h.

◆ log() [2/2]

template<class T >
static std::vector<double> bpp::VectorTools::log ( const std::vector< T > &  v1,
double  base 
)
inlinestatic

Definition at line 768 of file VectorTools.h.

◆ log10()

template<class T >
static std::vector<double> bpp::VectorTools::log10 ( const std::vector< T > &  v1)
inlinestatic

Definition at line 800 of file VectorTools.h.

◆ logMeanExp()

template<class T >
static T bpp::VectorTools::logMeanExp ( const std::vector< T > &  v1)
inlinestatic
Author
Laurent Gueguen
Returns
From std::vector v1, return $\log(\textrm{mean}_i(\exp(v1_i)))$.
Parameters
v1a std::vector.

Definition at line 701 of file VectorTools.h.

References max().

◆ logNorm()

template<class T >
static void bpp::VectorTools::logNorm ( std::vector< T > &  v)
inlinestatic
Author
Laurent Gueguen
Returns
Log-normalize std::vector v1, ie add a constant to the elements of v such that $\sum_i(\exp(v_i)) = 1$.
Parameters
vstd::vector.

Definition at line 643 of file VectorTools.h.

References max().

◆ logSumExp() [1/2]

template<class T >
static T bpp::VectorTools::logSumExp ( const std::vector< T > &  v1)
inlinestatic
Author
Laurent Gueguen
Returns
From std::vector v1, return $\log(\sum_i(\exp(v1_i)))$.
Parameters
v1a std::vector.

Definition at line 660 of file VectorTools.h.

References max().

◆ logSumExp() [2/2]

template<class T >
static T bpp::VectorTools::logSumExp ( const std::vector< T > &  v1,
const std::vector< T > &  v2 
)
inlinestatic
Author
Laurent Gueguen
Returns
From std::vector v1, return $\log(\sum_i(v2_i * \exp(v1_i)))$.
Parameters
v1a std::vector.
v2another std::vector.

Definition at line 678 of file VectorTools.h.

References max().

◆ max()

template<class T >
static T bpp::VectorTools::max ( const std::vector< T > &  v)
throw (EmptyVectorException< T >
)
inlinestatic

Template function to get the maximum value of a std::vector.

The > operator must be defined for the specified class.

Parameters
vThe input std::vector.
Returns
The maximum value in the std::vector.
Exceptions
EmptyVectorExceptionIf the input std::vector is empty.

Definition at line 1069 of file VectorTools.h.

Referenced by logMeanExp(), logNorm(), logSumExp(), sumExp(), and whichMaxAll().

◆ mean() [1/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::mean ( const std::vector< InputType > &  v1)
inlinestatic
Returns
The mean value of the std::vector.
Parameters
v1A std::vector.

Definition at line 1271 of file VectorTools.h.

◆ mean() [2/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::mean ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  w,
bool  normalizeWeights = true 
)
inlinestatic
Returns
The weighted mean value of the std::vector.
Parameters
v1A std::vector.
wA std::vector of weights.
normalizeWeightsTell if weights should be normalized so that they sum to 1.

Definition at line 1282 of file VectorTools.h.

References sum().

◆ median()

template<class InputType >
static InputType bpp::VectorTools::median ( std::vector< InputType > &  v1)
inlinestatic
Returns
The median value of the std::vector.
Parameters
v1A std::vector.

Definition at line 1300 of file VectorTools.h.

◆ miContinuous()

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::miContinuous ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  v2,
double  base = 2.7182818 
)
throw (DimensionException
)
inlinestatic
Returns
Compute mutual information index from two samples from continuous variables.

This is the continuous version. Each vector is supposed to be a finite sample from a continuous distribution. The density is of the distribution is estimated using a kernel method, as well as the joint density, and are used to compute the continuous mutual information.

Reference: Ivan Kojadinovic (2004) Computational Statistics & Data Analysis, 46:269-294

Author
Julien Dutheil
See also
miDiscrete For the discrete version.
Warning
This function is experimental. Notably, bad kernel density estimates may lead to negative MI :(
Parameters
v1The first input vector.
v2The second input vector.
baseThe base of the logarithm to use.
Exceptions
DimensionExceptionif the two vectors do not have the same lengths.

Definition at line 1654 of file VectorTools.h.

References bpp::AdaptiveKernelDensityEstimation::kDensity().

◆ miDiscrete()

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::miDiscrete ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  v2,
double  base = 2.7182818 
)
throw (DimensionException
)
inlinestatic
Returns
Compute mutual information index from two samples from discrete variables.

This is the discrete version. Each vector is supposed to be a finite sample from a discrete distribution. The counts of each (joint) observed state are computed and used to compute the discrete mutual information.

Author
Julien Dutheil
See also
miContinuous.
Parameters
v1The first input vector.
v2The second input vector.
baseThe base of the logarithm to use.
Exceptions
DimensionExceptionif the two vectors do not have the same lengths.

Definition at line 1575 of file VectorTools.h.

◆ min()

template<class T >
static T bpp::VectorTools::min ( const std::vector< T > &  v)
throw (EmptyVectorException< T >
)
inlinestatic

Template function to get the minimum value of a std::vector.

The < operator must be defined for the specified class.

Parameters
vThe input std::vector.
Returns
The minimum value in the std::vector.
Exceptions
EmptyVectorExceptionIf the input std::vector is empty.

Definition at line 1048 of file VectorTools.h.

Referenced by whichMinAll().

◆ nclassScott()

template<class T >
static size_t bpp::VectorTools::nclassScott ( const std::vector< T > &  v)
inlinestatic

Get the optimal class number following Scott's method.

Use Scott's (1979) method to compute the optimal class number for histogram.

Scott, D.W. (1979) On optimal and data-based histograms. Biometrika, 66, 605¿610.

Parameters
vThe std::vector to parse.
Returns
The number of classes.

Definition at line 573 of file VectorTools.h.

References range().

◆ norm() [1/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::norm ( const std::vector< InputType > &  v1)
inlinestatic
Returns
The norm of a std::vector ( $\sqrt{\sum_i^n x_i^2}$).
Parameters
v1A std::vector.

Definition at line 973 of file VectorTools.h.

◆ norm() [2/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::norm ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  w 
)
throw (DimensionException
)
inlinestatic
Returns
The "weighted" norm of a std::vector ( $\sqrt{\sum_i^n x_i^2}$).
Parameters
v1A std::vector.
wA std::vector of weights.
Exceptions
DimensionExceptionIf v1 and w do not have the same length.
See also
scalar.

Definition at line 991 of file VectorTools.h.

◆ order()

template<class T >
static std::vector<size_t> bpp::VectorTools::order ( const std::vector< T > &  v)
throw (EmptyVectorException< T >
)
inlinestatic

Template function to get the order of elements in the input vector.

This function is equivalent to the R 'order' function. It will sort elements by increasing size, so that [0] in the resulting outptu vector is the indice of the minimum element in the input vector, [1] is the second minimum, etc.

Parameters
vThe input std::vector.
Returns
Vector with sorted positions, according to input vector. throw EmptyVectorException If the input std::vector is empty.

Definition at line 1232 of file VectorTools.h.

◆ paste()

template<class T >
static std::string bpp::VectorTools::paste ( const std::vector< T > &  v,
const std::string &  delim = " " 
)
inlinestatic

Concatenate a std::vector after converting to string.

Parameters
vThe std::vector to concatenate.
delimA string which is used to separate the values (default is " ").

Definition at line 839 of file VectorTools.h.

◆ pow()

template<class T >
static std::vector<T> bpp::VectorTools::pow ( const std::vector< T > &  v1,
T &  b 
)
inlinestatic

Definition at line 824 of file VectorTools.h.

Referenced by bpp::MatrixTools::pow().

◆ prepend()

template<class T >
static void bpp::VectorTools::prepend ( std::vector< T > &  vec1,
const std::vector< T > &  vec2 
)
inlinestatic

Prepend the content of a std::vector to another one.

Parameters
vec1Vector 1.
vec2Vector 2.

Definition at line 1850 of file VectorTools.h.

◆ print()

template<class T >
static void bpp::VectorTools::print ( const std::vector< T > &  v1,
OutputStream out = ApplicationTools::message,
const std::string &  delim = " " 
)
inlinestatic

Print a std::vector to a stream.

Parameters
v1A std::vector.
outA stream.
delimA string which is used to separate the values (default is " ").

Definition at line 859 of file VectorTools.h.

◆ printForR()

template<class T >
static void bpp::VectorTools::printForR ( const std::vector< T > &  v1,
std::string  variableName = "x",
std::ostream &  out = std::cout 
)
inlinestatic

Print a std::vector to a stream in R format.

Parameters
v1A std::vector.
variableNamethe variable name (default "x").
outA stream (default cout).

Definition at line 877 of file VectorTools.h.

◆ prod()

template<class T >
static T bpp::VectorTools::prod ( const std::vector< T > &  v1)
inlinestatic
Returns
The product of all elements in a std::vector.
Parameters
v1A std::vector.

Definition at line 587 of file VectorTools.h.

◆ range()

template<class T >
static std::vector<T> bpp::VectorTools::range ( const std::vector< T > &  v)
throw (EmptyVectorException< T >
)
inlinestatic

Template function to get both extrema of a std::vector.

Both < and > operators must be defined for the specified class.

Parameters
vThe input std::vector.
Returns
A std::vector of size 2 which values are min(v) and max(v). throw EmptyVectorException If the input std::vector is empty.

Definition at line 1197 of file VectorTools.h.

Referenced by breaks(), and nclassScott().

◆ rep()

template<class T >
static std::vector<T> bpp::VectorTools::rep ( const std::vector< T > &  vec,
size_t  n 
)
inlinestatic
Returns
A single std::vector made of the repetion of the std::vectors passed as input.
Parameters
vecA std::vector.
nthe number of repetitions

Definition at line 1894 of file VectorTools.h.

◆ resize2()

template<class T >
static void bpp::VectorTools::resize2 ( VVdouble vv,
size_t  n1,
size_t  n2 
)
inlinestatic

Definition at line 342 of file VectorTools.h.

◆ resize3()

template<class T >
static void bpp::VectorTools::resize3 ( VVVdouble vvv,
size_t  n1,
size_t  n2,
size_t  n3 
)
inlinestatic

Definition at line 349 of file VectorTools.h.

◆ resize4()

static void bpp::VectorTools::resize4 ( VVVVdouble vvvv,
size_t  n1,
size_t  n2,
size_t  n3,
size_t  n4 
)
inlinestatic

Definition at line 362 of file VectorTools.h.

◆ scalar() [1/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::scalar ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  v2 
)
throw (DimensionException
)
inlinestatic
Returns
The scalar product of two std::vectors.
Parameters
v1First std::vector.
v2Second std::vector.
Exceptions
DimensionExceptionIf the two std::vector do not have the same length.

Definition at line 897 of file VectorTools.h.

◆ scalar() [2/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::scalar ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  v2,
const std::vector< InputType > &  w 
)
throw (DimensionException
)
inlinestatic

This dt product correspond to the dot product <v1,v2> in the space defined by

\[ M = \begin{pmatrix} w_1 & \ldots & \\ \vdots & w_2 & \ldots\\ & \vdots & \ddots\\ \end{pmatrix} \]

Returns
The "weighted" scalar product of two std::vectors.
Parameters
v1First std::vector.
v2Second std::vector.
wA std::vector of weights.
Exceptions
DimensionExceptionIf the two std::vector do not have the same length or do not match the length of the weights.

Definition at line 927 of file VectorTools.h.

◆ sd() [1/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::sd ( const std::vector< InputType > &  v1,
bool  unbiased = true 
)
inlinestatic
Returns
The standard deviation of the std::vector.
Parameters
v1The sample std::vector.
unbiasedTell if an unbiased estimate must be computed.

Definition at line 1447 of file VectorTools.h.

◆ sd() [2/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::sd ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  w,
bool  unbiased = true,
bool  normalizeWeights = true 
)
throw (DimensionException
)
inlinestatic
Returns
The weighted standard deviation of the std::vector.
Parameters
v1The sample std::vector.
wA std::vector of weights.
unbiasedTell if an unbiased estimate must be computed.
normalizeWeightsTell if weights should be normalized so that they sum to 1.
Exceptions
DimensionExceptionIf v1 and w do not have the same length.

Definition at line 1461 of file VectorTools.h.

◆ seq()

template<class T >
static std::vector<T> bpp::VectorTools::seq ( from,
to,
by 
)
inlinestatic

Build a sequence std::vector.

Build a std::vector from a value to another with a specified step. This works for numerical values for which additions, subtractions and division makes sens.

Parameters
fromThe begining.
toThe end.
byThe step.
Returns
A std::vector containing the sequence.

Definition at line 402 of file VectorTools.h.

Referenced by bpp::NumCalcApplicationTools::seqFromString().

◆ shannon()

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::shannon ( const std::vector< InputType > &  v,
double  base = 2.7182818 
)
inlinestatic
Returns
Compute the Shannon entropy indice of a vector of frequencies.

The elements of the vector should contain frequencies of each modality of the variable and sum to one. This verification is left to the user though.

Author
Julien Dutheil
See also
shannonDiscrete for computing the shannon indice of a sample, discrete version.
shannonContinuous for computing the shannon indice of a sample, continuous version.
Parameters
vThe input std::vector.
baseThe base of the logarithm to use.

Definition at line 1518 of file VectorTools.h.

◆ shannonContinuous()

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::shannonContinuous ( const std::vector< InputType > &  v,
double  base = 2.7182818 
)
inlinestatic
Returns
Compute the Shannon entropy indice of a sample from a continuous variable.

This is the continuous version. The vector is supposed to be a finite sample from a continuous distribution. The density is of the distribution is estimated using a kernel method, and is used to compute the continuous entropy.

Reference: Ivan Kojadinovic (2004) Computational Statistics & Data Analysis, 46:269-294

Author
Julien Dutheil
See also
shannon For the discrete version.
Parameters
vThe input std::vector.
baseThe base of the logarithm to use.

Definition at line 1616 of file VectorTools.h.

References bpp::AdaptiveKernelDensityEstimation::kDensity().

◆ shannonDiscrete()

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::shannonDiscrete ( const std::vector< InputType > &  v,
double  base = 2.7182818 
)
inlinestatic
Returns
Compute the Shannon entropy indice of a vector.

This is the discrete version. The vector is supposed to be a finite sample from a discrete distribution. The counts of each observed state are computed and used to compute the discrete entropy.

Author
Julien Dutheil
See also
shannonContinuous for the continuous version.
shannon for direct computation from frequencies.
Parameters
vThe input std::vector.
baseThe base of the logarithm to use.

Definition at line 1543 of file VectorTools.h.

◆ sin()

template<class T >
static std::vector<double> bpp::VectorTools::sin ( const std::vector< T > &  v1)
inlinestatic

Definition at line 792 of file VectorTools.h.

◆ sqr()

template<class T >
static std::vector<T> bpp::VectorTools::sqr ( const std::vector< T > &  v1)
inlinestatic

Definition at line 816 of file VectorTools.h.

Referenced by cov().

◆ sum()

◆ sumExp() [1/2]

template<class T >
static T bpp::VectorTools::sumExp ( const std::vector< T > &  v1)
inlinestatic
Author
Laurent Gueguen
Returns
From std::vector v1, return $\sum_i(\exp(v1_i))$.
Parameters
v1a std::vector.

Definition at line 719 of file VectorTools.h.

References max().

Referenced by bpp::LogsumHmmLikelihood::computeD2Forward_(), and bpp::LogsumHmmLikelihood::computeDForward_().

◆ sumExp() [2/2]

template<class T >
static T bpp::VectorTools::sumExp ( const std::vector< T > &  v1,
const std::vector< T > &  v2 
)
inlinestatic
Author
Laurent Gueguen
Returns
From std::vector v1, return $\sum_i(v2_i * \exp(v1_i))$.
Parameters
v1a std::vector.
v2another std::vector.

Definition at line 737 of file VectorTools.h.

References max().

◆ test()

bool VectorTools::test ( )
static

Test function.

Returns
true if all tests are passed.

Definition at line 68 of file VectorTools.cpp.

Referenced by vectorIntersection().

◆ unique()

template<class T >
static std::vector<T> bpp::VectorTools::unique ( const std::vector< T > &  v)
inlinestatic

Send a new std::vector with unique elements.

The input std::vector is copied, and the copy is sorted using QuickSort algorithm. A one-pass loop then look for duplicates and copy unique element to a result std::vector. The output std::vector is hence sorted.

If v is empty, it is passed 'as is' in return (after being copied).

Parameters
vthe std::vector to parse.

Definition at line 478 of file VectorTools.h.

◆ var() [1/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::var ( const std::vector< InputType > &  v1,
bool  unbiased = true 
)
inlinestatic
Returns
The variance of the std::vector.
Parameters
v1The sample std::vector.
unbiasedTell if an unbiased estimate must be computed.

Definition at line 1423 of file VectorTools.h.

◆ var() [2/2]

template<class InputType , class OutputType >
static OutputType bpp::VectorTools::var ( const std::vector< InputType > &  v1,
const std::vector< InputType > &  w,
bool  unbiased = true,
bool  normalizeWeights = true 
)
throw (DimensionException
)
inlinestatic
Returns
The weighted variance of the std::vector.
Parameters
v1The sample std::vector.
wA std::vector of weights.
unbiasedTell if an unbiased estimate must be computed.
normalizeWeightsTell if weights should be normalized so that they sum to 1.
Exceptions
DimensionExceptionIf v1 and w do not have the same length.

Definition at line 1436 of file VectorTools.h.

◆ vectorIntersection() [1/2]

template<class T >
static std::vector<T> bpp::VectorTools::vectorIntersection ( const std::vector< T > &  vec1,
const std::vector< T > &  vec2 
)
inlinestatic
Returns
A std::vector which is the intersection of two std::vectors passed as input.
Parameters
vec1Vector 1.
vec2Vector 2.

Definition at line 1797 of file VectorTools.h.

References contains().

◆ vectorIntersection() [2/2]

template<class T >
static std::vector<T> bpp::VectorTools::vectorIntersection ( const std::vector< std::vector< T > > &  vecElementL)
inlinestatic
Returns
A std::vector which is the intersection of all std::vectors passed as input.
Parameters
vecElementLA std::vector of std::vectors.

Definition at line 1812 of file VectorTools.h.

References contains(), and test().

◆ vectorUnion() [1/2]

template<class T >
static std::vector<T> bpp::VectorTools::vectorUnion ( const std::vector< T > &  vec1,
const std::vector< T > &  vec2 
)
inlinestatic
Returns
A std::vector which is the union of two std::vectors passed as input. Duplicate element will be removed.
Parameters
vec1Vector 1.
vec2Vector 2.

Definition at line 1760 of file VectorTools.h.

References contains().

◆ vectorUnion() [2/2]

template<class T >
static std::vector<T> bpp::VectorTools::vectorUnion ( const std::vector< std::vector< T > > &  vecElementL)
inlinestatic
Returns
A std::vector which is the union of all std::vectors passed as input. Duplicate element will be removed.
Parameters
vecElementLA std::vector of std::vectors.

Definition at line 1777 of file VectorTools.h.

References contains().

◆ which()

template<class T >
static size_t bpp::VectorTools::which ( const std::vector< T > &  v,
const T &  which 
)
throw (ElementNotFoundException< T >
)
inlinestatic

Send the position of the first occurence of 'which'.

Comparisons are performed using the == operator. Maximum complexity: O(v.size()).

Parameters
vThe std::vector to search.
whichThe element to search.
Returns
The position of which in v.

Definition at line 434 of file VectorTools.h.

Referenced by bpp::DataTable::deleteColumn(), bpp::DataTable::deleteRow(), bpp::DataTable::getColumn(), bpp::DataTable::getRow(), bpp::DataTable::operator()(), and whichAll().

◆ whichAll()

template<class T >
static std::vector<size_t> bpp::VectorTools::whichAll ( const std::vector< T > &  v,
const T &  which 
)
throw (ElementNotFoundException< T >
)
inlinestatic

Send the positions of all occurences of 'which'.

Comparisons are performed using the == operator. Complexity: O(v.size()).

Parameters
vThe std::vector to search.
whichThe element to search.
Returns
A std::vector containing the positions of which in v.

Definition at line 454 of file VectorTools.h.

References which().

◆ whichMax()

template<class T >
static size_t bpp::VectorTools::whichMax ( const std::vector< T > &  v)
throw (EmptyVectorException< T >
)
inlinestatic

Template function to get the index of the maximum value of a std::vector.

The > operator must be defined for the specified class. The position sent is the first one matching the maximum value.

Parameters
vThe input std::vector.
Returns
The position of the maximum value in the std::vector.
Exceptions
EmptyVectorExceptionIf the input std::vector is empty.

Definition at line 1091 of file VectorTools.h.

◆ whichMaxAll()

template<class T >
static std::vector<size_t> bpp::VectorTools::whichMaxAll ( const std::vector< T > &  v)
throw (EmptyVectorException< T >
)
inlinestatic

Template function to get the indices of the maximum value of a std::vector.

The > and == operator must be defined for the specified class. All positions matching the maximum value are returned.

Parameters
vThe input std::vector.
Returns
A vector of positions matching the maximum value in the std::vector.
Exceptions
EmptyVectorExceptionIf the input std::vector is empty.

Definition at line 1145 of file VectorTools.h.

References max().

◆ whichMin()

template<class T >
static size_t bpp::VectorTools::whichMin ( const std::vector< T > &  v)
throw (EmptyVectorException< T >
)
inlinestatic

Template function to get the index of the minimum value of a std::vector.

The < operator must be defined for the specified class. The position sent is the first one matching the maximum value.

Parameters
vThe input std::vector.
Returns
The position of the minimum value in the std::vector.
Exceptions
EmptyVectorExceptionIf the input std::vector is empty.

Definition at line 1118 of file VectorTools.h.

◆ whichMinAll()

template<class T >
static std::vector<size_t> bpp::VectorTools::whichMinAll ( const std::vector< T > &  v)
throw (EmptyVectorException< T >
)
inlinestatic

Template function to get the indices of the minimum value of a std::vector.

The < and == operator must be defined for the specified class. All positions matching the maximum value are returned.

Parameters
vThe input std::vector.
Returns
A vector of positions matching the minimum value in the std::vector.
Exceptions
EmptyVectorExceptionIf the input std::vector is empty.

Definition at line 1171 of file VectorTools.h.

References min().


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