Classes | Functions
utils.hpp File Reference

Various useful functions. More...

#include <vector>
#include <algorithm>
#include "universal_error.hpp"
#include <cassert>
#include "boost/container/flat_map.hpp"
#include <iostream>
Include dependency graph for utils.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  BinaryOperation< T >
 BinaryOperation Binary operator. More...
 
class  UnaryOperation< T >
 Abstract class for an unary operation. More...
 

Functions

bool is_nan (double x)
 Checks whether a number is a nan. More...
 
vector< double > linspace (double xl, double xh, int n)
 Uniformly spaced array (like the matlab function with the same name) More...
 
vector< double > arange (double x_min, double x_max, double dx)
 Uniformly spaced array (line numpy function with the same name) More...
 
template<class T >
vector< T > operator+ (vector< T > const &v1, vector< T > const &v2)
 Term by term addition for vectors. More...
 
template<class T >
vector< T > operator+ (const vector< T > &v, const T &t)
 Adds the same thing to all terms in a vector. More...
 
template<class T >
vector< T > operator+ (const T &t, const vector< T > &v)
 Adds the same thing to all terms in a vector. More...
 
template<class T >
vector< T > & operator+= (vector< T > &v, const T &t)
 Adds the same thing to all terms in a vector. More...
 
template<class T >
vector< T > operator- (vector< T > const &v1, vector< T > const &v2)
 Term by term vector subtraction. More...
 
template<class T >
vector< T > operator* (double d, vector< T > const &v)
 Multiplies all terms of a vector by a scalar. More...
 
template<typename T >
BiLinearInterpolation (const vector< T > &x, const vector< T > &y, const std::vector< std::vector< T > > &z, T xi, T yi)
 BiLinear Interpolation. More...
 
template<typename T >
LinearInterpolation (const vector< T > &x, const vector< T > &y, T xi)
 Linear Interpolation. More...
 
template<typename T >
LinearInterpolation (typename vector< T >::const_iterator itx_begin, typename vector< T >::const_iterator itx_end, typename vector< T >::const_iterator ity_begin, T xi)
 Linear Interpolation. More...
 
double min (vector< double > const &v)
 Returns the minimal term in a vector. More...
 
double max (vector< double > const &v)
 returns the maximal term in a vector More...
 
template<class T >
void RemoveVector (vector< T > &v, vector< int > &indeces)
 Removes the elements in v given by indeces. More...
 
template<class T >
void RemoveVector (vector< T > &v, vector< size_t > &indeces)
 Removes the elements in v given by indeces. More...
 
template<class T >
vector< T > VectorValues (vector< T > const &v, vector< int > const &index)
 Returns only the values with indeces in index. More...
 
template<class T >
vector< T > VectorValues (vector< T > const &v, vector< size_t > const &index)
 Returns only the values with indeces in index. More...
 
template<class T >
VectorSum (vector< T > const &v)
 Returns the sum of the vector. More...
 
template<class T >
vector< T > unique (vector< T > const &v)
 Returns a vector containing only unique elements. More...
 
template<class T >
vector< int > unique_index (vector< T > const &v)
 Returns a vector containing only indeces of unique elements. More...
 
template<class T >
vector< T > RemoveList (vector< T > const &v, vector< T > const &list)
 Returns only elements from vector v which are not in vector list, assumes list is sorted. More...
 
template<class T >
void RemoveVal (vector< T > &vec, T val)
 Removes the first occurence of val inside a vector. More...
 
template<class T >
bool InVector (vector< T > const &vec, T val)
 checks if val is in the vector More...
 
template<class T >
int IndexInVector (vector< T > const &vec, T val)
 Returns the index of val in the vector. More...
 
template<class T >
void ReArrangeVector (vector< T > &v, vector< int > const &indeces)
 Rearranges the vector according to the indeces. More...
 
template<class T >
void sort_index (const vector< T > &arr, vector< int > &res)
 Returns the indeces of a sort. More...
 
template<class T >
void sort_index (const vector< T > &arr, vector< size_t > &res)
 Returns the indeces of a sort. More...
 
template<class T >
vector< size_t > sort_index (const vector< T > &arr)
 Returns the indeces of a sort. More...
 
template<class RAIter , class Compare >
void sort_index (RAIter iterBegin, RAIter iterEnd, Compare comp, std::vector< size_t > &indexes)
 Returns the indeces of a sort. More...
 
template<class T >
vector< T > join (vector< T > const &v1, vector< T > const &v2)
 Concatenates two vectors. More...
 
template<class T >
vector< T > binary_unite (vector< T > const &v1, vector< T > const &v2, BinaryOperation< T > const &bin_op)
 Applies a binary operation on every pair of values from two vectors. More...
 
template<class T >
vector< T > apply_to_each_term (vector< T > const &v, UnaryOperation< T > const &un_op)
 Applies an unary operator to all terms in a std::vector. More...
 
template<class T >
pair_member (const std::pair< T, T > &p, int index)
 Selects a member of std::pair. More...
 
template<class T >
void set_pair_member (std::pair< T, T > &p, int index, const T &val)
 Sets a member of std::pair. More...
 
template<class T , class S >
vector< T > list_static_cast (const vector< S > &source)
 Performs type casting for an entire vector. More...
 
template<class T >
void insert_all_to_back (vector< T > &subject, const vector< T > &addendum)
 Inserts all elements from one vector to the end of another. More...
 
template<class Iter , class T >
Iter binary_find (Iter begin, Iter end, T val)
 Binary search that return iterator of found object or end if not found. More...
 
template<class S , class T >
vector< T >::const_reference safe_retrieve (vector< T > const &data, vector< S > const &keys, const S &key)
 Checks for existence and retrieves entry from flat map. More...
 
template<class S , class T >
vector< T >::reference safe_retrieve (vector< T > &data, vector< S > const &keys, const S &key)
 Checks for existence and retrieves entry from flat map. More...
 
template<class S , class T >
const T & safe_retrieve (const boost::container::flat_map< S, T > &m, const S &s)
 Checks for existence and retrieves entry from flat map. More...
 
template<class T >
vector< vector< T > > CombineVectors (vector< vector< vector< T > > > const &data)
 Reduces the dimension of the input vector. More...
 
template<class T >
vector< T > CombineVectors (vector< vector< T > > const &data)
 Reduces the dimension of the input vector. More...
 
template<class S , class T >
T & safe_retrieve (boost::container::flat_map< S, T > &m, const S &s)
 Non constant version of safe retrieve. More...
 
double fastsqrt (double x)
 Fast approximate sqrt. More...
 

Detailed Description

Various useful functions.

Author
Almog Yalinewich

Definition in file utils.hpp.

Function Documentation

◆ apply_to_each_term()

template<class T >
vector<T> apply_to_each_term ( vector< T > const &  v,
UnaryOperation< T > const &  un_op 
)

Applies an unary operator to all terms in a std::vector.

Parameters
vVector
un_opUnary operator
Returns
Vector

Definition at line 646 of file utils.hpp.

◆ arange()

vector<double> arange ( double  x_min,
double  x_max,
double  dx 
)

Uniformly spaced array (line numpy function with the same name)

Parameters
x_minLower bound
x_maxUpper bound
dxDifference between consecutive terms
Returns
Uniformly spaced array

Definition at line 35 of file utils.cpp.

◆ BiLinearInterpolation()

template<typename T >
T BiLinearInterpolation ( const vector< T > &  x,
const vector< T > &  y,
const std::vector< std::vector< T > > &  z,
xi,
yi 
)

BiLinear Interpolation.

Parameters
xThe x vector, assumed sorted
yThe y vector, assumed sorted
zz=f(x,y) matrix, first index is in x
xiThe x interpolation location
yiThe y interpolation location
Returns
f(xi)

Definition at line 137 of file utils.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ binary_find()

template<class Iter , class T >
Iter binary_find ( Iter  begin,
Iter  end,
val 
)

Binary search that return iterator of found object or end if not found.

Parameters
beginbegin iterator
endend iterator
valThe value to search for
Returns
iterator of found object or end if not found

Definition at line 709 of file utils.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ binary_unite()

template<class T >
vector<T> binary_unite ( vector< T > const &  v1,
vector< T > const &  v2,
BinaryOperation< T > const &  bin_op 
)

Applies a binary operation on every pair of values from two vectors.

Parameters
v1First vector
v2Second vector
bin_opBinary operation
Returns
Vector

Definition at line 615 of file utils.hpp.

◆ CombineVectors() [1/2]

template<class T >
vector<vector<T> > CombineVectors ( vector< vector< vector< T > > > const &  data)

Reduces the dimension of the input vector.

Parameters
dataThe vector to reduce its dimension
Returns
The reduced vector

Definition at line 770 of file utils.hpp.

Here is the caller graph for this function:

◆ CombineVectors() [2/2]

template<class T >
vector<T> CombineVectors ( vector< vector< T > > const &  data)

Reduces the dimension of the input vector.

Parameters
dataThe vector to reduce its dimension
Returns
The reduced vector

Definition at line 794 of file utils.hpp.

Here is the call graph for this function:

◆ fastsqrt()

double fastsqrt ( double  x)

Fast approximate sqrt.

Parameters
xThe value to calculate the sqrt of
Returns
Sqrt(x)

Definition at line 11 of file utils.cpp.

Here is the caller graph for this function:

◆ IndexInVector()

template<class T >
int IndexInVector ( vector< T > const &  vec,
val 
)

Returns the index of val in the vector.

Parameters
vecThe vector to check
valThe value to look for
Returns
The index of the vector which first equals to val, throws exception if not found

Definition at line 464 of file utils.hpp.

◆ insert_all_to_back()

template<class T >
void insert_all_to_back ( vector< T > &  subject,
const vector< T > &  addendum 
)

Inserts all elements from one vector to the end of another.

Parameters
subjectVector that will be modifies
addendumVector that will be added

Definition at line 696 of file utils.hpp.

◆ InVector()

template<class T >
bool InVector ( vector< T > const &  vec,
val 
)

checks if val is in the vector

Parameters
vecThe vector to check
valThe value to check inside the vector
Returns
True if the value is in the vector, false otherwise

Definition at line 449 of file utils.hpp.

◆ is_nan()

bool is_nan ( double  x)

Checks whether a number is a nan.

Parameters
xA number
Returns
True if nan false otherwise

Definition at line 19 of file utils.cpp.

Here is the caller graph for this function:

◆ join()

template<class T >
vector<T> join ( vector< T > const &  v1,
vector< T > const &  v2 
)

Concatenates two vectors.

Parameters
v1vector
v2vector
Returns
vector

Definition at line 581 of file utils.hpp.

◆ LinearInterpolation() [1/2]

template<typename T >
T LinearInterpolation ( const vector< T > &  x,
const vector< T > &  y,
xi 
)

Linear Interpolation.

Parameters
xThe x vector, assumed sorted
yy=f(x) vector
xiThe interpolation location
Returns
f(xi)

Definition at line 197 of file utils.hpp.

Here is the caller graph for this function:

◆ LinearInterpolation() [2/2]

template<typename T >
T LinearInterpolation ( typename vector< T >::const_iterator  itx_begin,
typename vector< T >::const_iterator  itx_end,
typename vector< T >::const_iterator  ity_begin,
xi 
)

Linear Interpolation.

Parameters
itx_beginThe iterator to the beginining of the X sequence, assumed sorted
itx_endThe iterator to the end of the X sequence
ity_beginThe iterator to the beginining of the Y sequence, assumed sorted
xiThe interpolation location
Returns
y(xi)

Definition at line 233 of file utils.hpp.

Here is the call graph for this function:

◆ linspace()

vector<double> linspace ( double  xl,
double  xh,
int  n 
)

Uniformly spaced array (like the matlab function with the same name)

Parameters
xlLower bound
xhHigher bound
nNumber of elements
Returns
The uniformly spaced array

Definition at line 26 of file utils.cpp.

◆ list_static_cast()

template<class T , class S >
vector<T> list_static_cast ( const vector< S > &  source)

Performs type casting for an entire vector.

Parameters
sourceSource vector
Returns
Source vector converted to new type

Definition at line 684 of file utils.hpp.

◆ max()

double max ( vector< double > const &  v)

returns the maximal term in a vector

Parameters
vVector
Returns
The maximum of the vector

Definition at line 52 of file utils.cpp.

Here is the caller graph for this function:

◆ min()

double min ( vector< double > const &  v)

Returns the minimal term in a vector.

Parameters
vVector
Returns
The minimum of the vector

Definition at line 44 of file utils.cpp.

Here is the caller graph for this function:

◆ operator*()

template<class T >
vector<T> operator* ( double  d,
vector< T > const &  v 
)

Multiplies all terms of a vector by a scalar.

Parameters
dScalar
vvector
Returns
The mulitplication of the vector

Definition at line 116 of file utils.hpp.

Here is the call graph for this function:

◆ operator+() [1/3]

template<class T >
vector<T> operator+ ( vector< T > const &  v1,
vector< T > const &  v2 
)

Term by term addition for vectors.

Parameters
v1Right argument
v2Left argument
Returns
The addition of the two vectors

Definition at line 46 of file utils.hpp.

◆ operator+() [2/3]

template<class T >
vector<T> operator+ ( const vector< T > &  v,
const T &  t 
)

Adds the same thing to all terms in a vector.

Parameters
vVector
tAddition
Returns
New vector with sums as terms

Definition at line 61 of file utils.hpp.

◆ operator+() [3/3]

template<class T >
vector<T> operator+ ( const T &  t,
const vector< T > &  v 
)

Adds the same thing to all terms in a vector.

Parameters
vVector
tAddition
Returns
New vector with sums as terms

Definition at line 75 of file utils.hpp.

◆ operator+=()

template<class T >
vector<T>& operator+= ( vector< T > &  v,
const T &  t 
)

Adds the same thing to all terms in a vector.

Parameters
vVector
tAddition
Returns
Reference to united vector

Definition at line 86 of file utils.hpp.

◆ operator-()

template<class T >
vector<T> operator- ( vector< T > const &  v1,
vector< T > const &  v2 
)

Term by term vector subtraction.

Parameters
v1Left argument
v2Right argument
Returns
The subtraction between the vectors

Definition at line 99 of file utils.hpp.

◆ pair_member()

template<class T >
T pair_member ( const std::pair< T, T > &  p,
int  index 
)

Selects a member of std::pair.

Parameters
pA pair
index0 for first member, 1 for second, error otherwise
Returns
Either first or second members of pair

Definition at line 660 of file utils.hpp.

Here is the caller graph for this function:

◆ ReArrangeVector()

template<class T >
void ReArrangeVector ( vector< T > &  v,
vector< int > const &  indeces 
)

Rearranges the vector according to the indeces.

Parameters
vThe vector to rearrange
indecesThe rearrangement indeces

Definition at line 477 of file utils.hpp.

◆ RemoveList()

template<class T >
vector<T> RemoveList ( vector< T > const &  v,
vector< T > const &  list 
)

Returns only elements from vector v which are not in vector list, assumes list is sorted.

Parameters
vThe vector to change
listThe values to be taken out of v
Returns
The new vector

Definition at line 416 of file utils.hpp.

Here is the caller graph for this function:

◆ RemoveVal()

template<class T >
void RemoveVal ( vector< T > &  vec,
val 
)

Removes the first occurence of val inside a vector.

Parameters
vecThe vector to change
valThe value to remove

Definition at line 431 of file utils.hpp.

Here is the caller graph for this function:

◆ RemoveVector() [1/2]

template<class T >
void RemoveVector ( vector< T > &  v,
vector< int > &  indeces 
)

Removes the elements in v given by indeces.

Parameters
vThe vector to change
indecesThe cells to remove

Definition at line 275 of file utils.hpp.

Here is the caller graph for this function:

◆ RemoveVector() [2/2]

template<class T >
void RemoveVector ( vector< T > &  v,
vector< size_t > &  indeces 
)

Removes the elements in v given by indeces.

Parameters
vThe vector to change
indecesThe cells to remove

Definition at line 301 of file utils.hpp.

◆ safe_retrieve() [1/4]

template<class S , class T >
vector<T>::const_reference safe_retrieve ( vector< T > const &  data,
vector< S > const &  keys,
const S &  key 
)

Checks for existence and retrieves entry from flat map.

Parameters
dataData vector
keyKey to look for
keysSorted vector of keys
Returns
Value corresponding to key

Definition at line 727 of file utils.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ safe_retrieve() [2/4]

template<class S , class T >
vector<T>::reference safe_retrieve ( vector< T > &  data,
vector< S > const &  keys,
const S &  key 
)

Checks for existence and retrieves entry from flat map.

Parameters
dataData vector
keyKey to look for
keysSorted vector of keys
Returns
Value corresponding to key

Definition at line 743 of file utils.hpp.

Here is the call graph for this function:

◆ safe_retrieve() [3/4]

template<class S , class T >
const T& safe_retrieve ( const boost::container::flat_map< S, T > &  m,
const S &  s 
)

Checks for existence and retrieves entry from flat map.

Parameters
mflat map
sKey
Returns
Value corresponding to key

Definition at line 757 of file utils.hpp.

◆ safe_retrieve() [4/4]

template<class S , class T >
T& safe_retrieve ( boost::container::flat_map< S, T > &  m,
const S &  s 
)

Non constant version of safe retrieve.

Parameters
mflat map
sKey
Returns
Value corresponding to key

Definition at line 808 of file utils.hpp.

Here is the call graph for this function:

◆ set_pair_member()

template<class T >
void set_pair_member ( std::pair< T, T > &  p,
int  index,
const T &  val 
)

Sets a member of std::pair.

Parameters
pPair
index0 for first, 1 for second, error otherwise
valValue to be written

Definition at line 671 of file utils.hpp.

◆ sort_index() [1/4]

template<class T >
void sort_index ( const vector< T > &  arr,
vector< int > &  res 
)

Returns the indeces of a sort.

Parameters
arrThe array to sort
resThe indeces of the sort that is given as the output

Definition at line 500 of file utils.hpp.

Here is the caller graph for this function:

◆ sort_index() [2/4]

template<class T >
void sort_index ( const vector< T > &  arr,
vector< size_t > &  res 
)

Returns the indeces of a sort.

Parameters
arrThe array to sort
resThe indeces of the sort that is given as the output

Definition at line 512 of file utils.hpp.

◆ sort_index() [3/4]

template<class T >
vector<size_t> sort_index ( const vector< T > &  arr)

Returns the indeces of a sort.

Parameters
arrThe array to sort
Returns
The indeces of the sort that is given as the output

Definition at line 525 of file utils.hpp.

◆ sort_index() [4/4]

template<class RAIter , class Compare >
void sort_index ( RAIter  iterBegin,
RAIter  iterEnd,
Compare  comp,
std::vector< size_t > &  indexes 
)

Returns the indeces of a sort.

Parameters
iterBeginStarting iterator
iterEndEnd iterator
compThe compare function
indexesOutput

Definition at line 556 of file utils.hpp.

◆ unique()

template<class T >
vector<T> unique ( vector< T > const &  v)

Returns a vector containing only unique elements.

Parameters
vThe input vector, must be SORTED!!
Returns
The unique vector

Definition at line 376 of file utils.hpp.

Here is the caller graph for this function:

◆ unique_index()

template<class T >
vector<int> unique_index ( vector< T > const &  v)

Returns a vector containing only indeces of unique elements.

Parameters
vThe input vector, must be SORTED!!
Returns
The unique vector indeces

Definition at line 397 of file utils.hpp.

Here is the caller graph for this function:

◆ VectorSum()

template<class T >
T VectorSum ( vector< T > const &  v)

Returns the sum of the vector.

Parameters
vThe vector to sum
Returns
The sum

Definition at line 361 of file utils.hpp.

◆ VectorValues() [1/2]

template<class T >
vector<T> VectorValues ( vector< T > const &  v,
vector< int > const &  index 
)

Returns only the values with indeces in index.

Parameters
vThe vector to check
indexThe indeces to return
Returns
The reduced vector

Definition at line 326 of file utils.hpp.

Here is the caller graph for this function:

◆ VectorValues() [2/2]

template<class T >
vector<T> VectorValues ( vector< T > const &  v,
vector< size_t > const &  index 
)

Returns only the values with indeces in index.

Parameters
vThe vector to check
indexThe indeces to return
Returns
The reduced vector

Definition at line 344 of file utils.hpp.