bpp-core3  3.0.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)
 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)
 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< size_t > &v2)
 Takes elements of a vector given a vector of positions. More...
 
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 T sumProd (const std::vector< T > &v1, const std::vector< T > &v2)
 
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 printRange (const std::vector< T > &v1, OutputStream &out= *ApplicationTools::message, const std::string &delim=",", const std::string &rangeDelim="-")
 
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)
 
template<class InputType , class OutputType >
static OutputType scalar (const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w)
 
template<class T >
static std::vector< T > kroneckerMult (const std::vector< T > &v1, const std::vector< T > &v2)
 
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)
 
template<class InputType , class OutputType >
static OutputType cos (const std::vector< InputType > &v1, const std::vector< InputType > &v2)
 
template<class InputType , class OutputType >
static OutputType cos (const std::vector< InputType > &v1, const std::vector< InputType > &v2, const std::vector< InputType > &w)
 
template<class T >
static std::vector< T > abs (const std::vector< T > &v)
 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)
 
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)
 
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)
 
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)
 
template<class InputType , class OutputType >
static OutputType cor (const std::vector< InputType > &v1, const std::vector< InputType > &v2)
 
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)
 
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)
 
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)
 
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 , class U >
static bool contains (const std::vector< T > &vec, U 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 , class U >
static std::vector< T > vectorIntersection (const std::vector< T > &vec1, const std::vector< U > &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.
static void resize2 (VVdouble &vv, size_t n1, size_t n2)
 
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< T > log (const std::vector< T > &v1)
 
template<class T >
static double log (const T &x)
 
template<class T >
static std::vector< double > log (const std::vector< T > &v1, double base)
 
template<class T >
static double exp (const T &x)
 
template<class T >
static std::vector< T > 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)
 Template function to get the minimum value of a std::vector. More...
 
template<class T >
static T max (const std::vector< T > &v)
 Template function to get the maximum value of a std::vector. More...
 
template<class T >
static size_t whichMax (const std::vector< T > &v)
 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)
 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)
 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)
 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)
 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)
 Template function to get the order of elements in the input vector. More...
 

Detailed Description

Definition at line 308 of file VectorTools.h.

Constructor & Destructor Documentation

◆ VectorTools()

bpp::VectorTools::VectorTools ( )
inline

Definition at line 311 of file VectorTools.h.

◆ ~VectorTools()

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

Definition at line 312 of file VectorTools.h.

Member Function Documentation

◆ abs()

template<class T >
static std::vector<T> bpp::VectorTools::abs ( const std::vector< T > &  v)
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 1298 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 1899 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 1921 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 18 of file VectorTools.cpp.

References range().

Referenced by countValues().

◆ 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 1369 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 1388 of file VectorTools.h.

◆ contains() [1/2]

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 1764 of file VectorTools.h.

Referenced by extend(), bpp::DataTable::setRowName(), vectorIntersection(), and vectorUnion().

◆ contains() [2/2]

template<class T , class U >
static bool bpp::VectorTools::contains ( const std::vector< T > &  vec,
el 
)
inlinestatic

Definition at line 1774 of file VectorTools.h.

◆ 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 1792 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 
)
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 1515 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 
)
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 1530 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 801 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 
)
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 1054 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 
)
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 1068 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 510 of file VectorTools.h.

References breaks().

◆ 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 
)
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 1407 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 
)
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 1429 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 573 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 624 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 1977 of file VectorTools.h.

References append(), and test().

◆ exp() [1/2]

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

Definition at line 787 of file VectorTools.h.

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

◆ exp() [2/2]

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

Definition at line 793 of file VectorTools.h.

References 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 1939 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< size_t > &  v2 
)
inlinestatic

Takes elements of a vector given a vector of positions.

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 493 of file VectorTools.h.

◆ fact()

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

Definition at line 825 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 1731 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 1750 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 471 of file VectorTools.h.

Referenced by bpp::DataTable::setColumnNames(), bpp::Table< T >::setColumnNames(), bpp::DataTable::setRowNames(), and bpp::Table< T >::setRowNames().

◆ kroneckerMult()

template<class T >
static std::vector<T> bpp::VectorTools::kroneckerMult ( const std::vector< T > &  v1,
const std::vector< T > &  v2 
)
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 994 of file VectorTools.h.

◆ log() [1/3]

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

Definition at line 762 of file VectorTools.h.

◆ log() [2/3]

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

Definition at line 773 of file VectorTools.h.

◆ log() [3/3]

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

Definition at line 779 of file VectorTools.h.

◆ log10()

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

Definition at line 817 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 698 of file VectorTools.h.

References logSumExp().

◆ 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 638 of file VectorTools.h.

References logSumExp().

◆ 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 649 of file VectorTools.h.

References max().

Referenced by logMeanExp(), and logNorm().

◆ 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 673 of file VectorTools.h.

References max().

◆ max()

template<class T >
static T bpp::VectorTools::max ( const std::vector< T > &  v)
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 1111 of file VectorTools.h.

Referenced by bpp::NumCalcApplicationTools::getParameterGrid(), 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 1313 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 1324 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 1342 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 
)
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 1697 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 
)
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 1618 of file VectorTools.h.

◆ min()

template<class T >
static T bpp::VectorTools::min ( const std::vector< T > &  v)
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 1090 of file VectorTools.h.

Referenced by bpp::NumCalcApplicationTools::getParameterGrid(), and 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 543 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 1015 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 
)
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 1033 of file VectorTools.h.

◆ order()

template<class T >
static std::vector<size_t> bpp::VectorTools::order ( const std::vector< T > &  v)
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 1274 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 856 of file VectorTools.h.

Referenced by bpp::TreeGraphImpl< GraphImpl >::getFatherOfNode().

◆ pow()

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

Definition at line 841 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 1910 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 876 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 919 of file VectorTools.h.

◆ printRange()

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

Definition at line 888 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 557 of file VectorTools.h.

◆ range()

template<class T >
static std::vector<T> bpp::VectorTools::range ( const std::vector< T > &  v)
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 1239 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 1954 of file VectorTools.h.

◆ resize2()

static void bpp::VectorTools::resize2 ( VVdouble vv,
size_t  n1,
size_t  n2 
)
inlinestatic

Definition at line 320 of file VectorTools.h.

◆ resize3()

static void bpp::VectorTools::resize3 ( VVVdouble vvv,
size_t  n1,
size_t  n2,
size_t  n3 
)
inlinestatic

Definition at line 326 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 339 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 
)
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 939 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 
)
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 969 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 1489 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 
)
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 1503 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 (included).
toThe end (included)
byThe step.
Returns
A std::vector containing the sequence.

Definition at line 377 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 1560 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 1659 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 1586 of file VectorTools.h.

◆ sin()

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

Definition at line 809 of file VectorTools.h.

◆ sqr()

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

Definition at line 833 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 710 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 733 of file VectorTools.h.

References max().

◆ sumProd()

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

Definition at line 601 of file VectorTools.h.

◆ test()

bool VectorTools::test ( )
static

Test function.

Returns
true if all tests are passed.

Definition at line 33 of file VectorTools.cpp.

Referenced by diff(), and 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 446 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 1465 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 
)
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 1478 of file VectorTools.h.

◆ vectorIntersection() [1/3]

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. The order of elements is the order of the first vector.
Parameters
vec1Vector 1.
vec2Vector 2.

Definition at line 1850 of file VectorTools.h.

References contains().

◆ vectorIntersection() [2/3]

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

Definition at line 1861 of file VectorTools.h.

References contains().

◆ vectorIntersection() [3/3]

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 1876 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 1813 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 1830 of file VectorTools.h.

References contains().

◆ which()

template<class T >
static size_t bpp::VectorTools::which ( const std::vector< T > &  v,
const T &  which 
)
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 402 of file VectorTools.h.

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

◆ whichAll()

template<class T >
static std::vector<size_t> bpp::VectorTools::whichAll ( const std::vector< T > &  v,
const T &  which 
)
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 422 of file VectorTools.h.

References which().

◆ whichMax()

template<class T >
static size_t bpp::VectorTools::whichMax ( const std::vector< T > &  v)
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 1133 of file VectorTools.h.

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

◆ whichMaxAll()

template<class T >
static std::vector<size_t> bpp::VectorTools::whichMaxAll ( const std::vector< T > &  v)
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 1187 of file VectorTools.h.

References max().

◆ whichMin()

template<class T >
static size_t bpp::VectorTools::whichMin ( const std::vector< T > &  v)
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 1160 of file VectorTools.h.

◆ whichMinAll()

template<class T >
static std::vector<size_t> bpp::VectorTools::whichMinAll ( const std::vector< T > &  v)
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 1213 of file VectorTools.h.

References min().


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