Ember
Loading...
Searching...
No Matches
mathUtils Namespace Reference

A collection of general-purpose mathematical functions for working with vectors. More...

Functions

double maxval (const dvector &v)
 Returns the value of the maximum element of a vector.
 
double minval (const dvector &v)
 Returns the value of the minimum element of a vector.
 
double range (const dvector &v)
 Returns the range (maximum - minimum) of a vector.
 
double sum (const dvector &v)
 Returns the sum of all the elements in a vector.
 
double mean (const dvector &v)
 Returns the arithemetic mean of the elements in a vector.
 
double maxval (const dvector &v, size_t iStart, size_t iEnd)
 Returns the value of the maximum element of the sub-vector of v with indices on the closed interval [iStart, iEnd]
 
double minval (const dvector &v, size_t iStart, size_t iEnd)
 Returns the value of the minimum element of the sub-vector of v with indices on the closed interval [iStart, iEnd]
 
double range (const dvector &v, size_t iStart, size_t iEnd)
 Returns the range (maximum - minimum) of the sub-vector of v with indices on the closed interval [iStart, iEnd]
 
double sum (const dvector &v, size_t iStart, size_t iEnd)
 Returns the sum of all the elements in the sub-vector of v with indices on the closed interval [iStart, iEnd]
 
double mean (const dvector &v, size_t iStart, size_t iEnd)
 Returns the arithemetic mean of the sub-vector of v with indices on the closed interval [iStart, iEnd]
 
size_t minloc (const dvector &v)
 Returns the index of the minimum element of v.
 
size_t maxloc (const dvector &v)
 Returns the index of the maximum element of v.
 
template<typename Derived>
bool notnan (const Eigen::EigenBase< Derived > &v)
 Returns true if v does not contain any NaNs.
 
template<typename T>
bool notnan (const vector< T > &v)
 Returns true if v does not contain any NaNs.
 
bool notnan (const double &v)
 Returns true if v is not NaN.
 
bool notnan (const sdVector &v)
 Returns true if v does not contain any NaNs.
 
size_t nanloc (const dvector &v)
 Returns the index of first NaN component of v.
 
bool almostEqual (double a, double b, double rtol=1e-10, double atol=1e-18)
 Returns true if a and b are equal to within the specified relative (rtol) and absolute (atol) tolerances.
 
dvector abs (const dvector &v)
 Returns a vector containing the absolute values of the elements of v.
 
template<class T>
int findFirst (const T &v)
 Returns the index of the first element of v which is true.
 
template<class T>
int findLast (const T &v)
 Returns the index of the last element of v which is true.
 
vector< int > find (vector< bool > &v)
 Returns a vector containing the indices of the elements of v which are true.
 
template<class T>
void smooth (T &v)
 Applies a simple smoothing function to v which effectively filters out high-frequency components.
 
dvector linspace (const double x1, const double x2, const int n)
 Returns a vector of n elements linearly spaced along the closed interval [x1, x2]
 
template<class T1, class T2>
vector< T1 > computeSplines (const T1 &xIn, const T2 &yIn)
 Internal function for calculating cubic splines for a sequence of data points.
 
template<class T>
interp1 (const T &xIn, const T &yIn, const T &xOut, bool extrap=true)
 Linear interpolation for a vector of desired outputs.
 
template<class T>
double interp1 (const T &xIn, const T &yIn, const double xOut, bool extrap=true)
 Linear interpolation for a single output point.
 
dvec splines (const dvec &xIn, const dvec &yIn, const dvec &xOut)
 Cubic spline interpolation for a vector of desired outputs.
 
double splines (const dvec &xIn, const dvec &yIn, const double xOut)
 Cubic spline interpolation for a single output point.
 
template<class T1, class T2>
double integrate (const T1 &x, const T2 &y)
 Numerical integration using the cubic spline fit for the given data.
 
template<class T1, class T2>
double trapz (const T1 &x, const T2 &y)
 Trapezoidal rule integration.
 
void vectorVectorToArray2D (const vector< dvector > &v, dmatrix &a)
 
void array2DToVectorVector (const dmatrix &a, vector< dvector > &v)
 
std::string stringify (double x)
 
std::string stringify (double x, int nDigits)
 
std::string stringify (int x)
 
int sign (const double x)
 
int sign (const int x)
 
template<class Tx, class Ty>
void uniqueSort (vector< Tx > &keys, vector< vector< Ty > > &values)
 Sort & remove duplicate entries from keys, and perform the same permutation on values Requires that keys.size() == values[i].size()
 

Detailed Description

A collection of general-purpose mathematical functions for working with vectors.

Function Documentation

◆ maxval() [1/2]

double mathUtils::maxval ( const dvector & v)

Returns the value of the maximum element of a vector.

◆ minval() [1/2]

double mathUtils::minval ( const dvector & v)

Returns the value of the minimum element of a vector.

◆ range() [1/2]

double mathUtils::range ( const dvector & v)

Returns the range (maximum - minimum) of a vector.

◆ sum() [1/2]

double mathUtils::sum ( const dvector & v)

Returns the sum of all the elements in a vector.

◆ mean() [1/2]

double mathUtils::mean ( const dvector & v)

Returns the arithemetic mean of the elements in a vector.

◆ maxval() [2/2]

double mathUtils::maxval ( const dvector & v,
size_t iStart,
size_t iEnd )

Returns the value of the maximum element of the sub-vector of v with indices on the closed interval [iStart, iEnd]

◆ minval() [2/2]

double mathUtils::minval ( const dvector & v,
size_t iStart,
size_t iEnd )

Returns the value of the minimum element of the sub-vector of v with indices on the closed interval [iStart, iEnd]

◆ range() [2/2]

double mathUtils::range ( const dvector & v,
size_t iStart,
size_t iEnd )

Returns the range (maximum - minimum) of the sub-vector of v with indices on the closed interval [iStart, iEnd]

◆ sum() [2/2]

double mathUtils::sum ( const dvector & v,
size_t iStart,
size_t iEnd )

Returns the sum of all the elements in the sub-vector of v with indices on the closed interval [iStart, iEnd]

◆ mean() [2/2]

double mathUtils::mean ( const dvector & v,
size_t iStart,
size_t iEnd )

Returns the arithemetic mean of the sub-vector of v with indices on the closed interval [iStart, iEnd]

◆ minloc()

size_t mathUtils::minloc ( const dvector & v)

Returns the index of the minimum element of v.

◆ maxloc()

size_t mathUtils::maxloc ( const dvector & v)

Returns the index of the maximum element of v.

◆ notnan() [1/4]

template<typename Derived>
bool mathUtils::notnan ( const Eigen::EigenBase< Derived > & v)

Returns true if v does not contain any NaNs.

◆ notnan() [2/4]

template<typename T>
bool mathUtils::notnan ( const vector< T > & v)

Returns true if v does not contain any NaNs.

◆ notnan() [3/4]

bool mathUtils::notnan ( const double & v)
inline

Returns true if v is not NaN.

◆ notnan() [4/4]

bool mathUtils::notnan ( const sdVector & v)

Returns true if v does not contain any NaNs.

◆ nanloc()

size_t mathUtils::nanloc ( const dvector & v)

Returns the index of first NaN component of v.

Returns -1 if no elements are NaN.

◆ almostEqual()

bool mathUtils::almostEqual ( double a,
double b,
double rtol = 1e-10,
double atol = 1e-18 )
inline

Returns true if a and b are equal to within the specified relative (rtol) and absolute (atol) tolerances.

◆ abs()

dvector mathUtils::abs ( const dvector & v)

Returns a vector containing the absolute values of the elements of v.

◆ findFirst()

template<class T>
int mathUtils::findFirst ( const T & v)

Returns the index of the first element of v which is true.

Returns -1 if all elements of v are false.

◆ findLast()

template<class T>
int mathUtils::findLast ( const T & v)

Returns the index of the last element of v which is true.

Returns -1 if all elements of v are false.

◆ find()

vector< int > mathUtils::find ( vector< bool > & v)

Returns a vector containing the indices of the elements of v which are true.

◆ smooth()

template<class T>
void mathUtils::smooth ( T & v)

Applies a simple smoothing function to v which effectively filters out high-frequency components.

◆ linspace()

dvector mathUtils::linspace ( const double x1,
const double x2,
const int n )

Returns a vector of n elements linearly spaced along the closed interval [x1, x2]

◆ computeSplines()

template<class T1, class T2>
vector< T1 > mathUtils::computeSplines ( const T1 & xIn,
const T2 & yIn )

Internal function for calculating cubic splines for a sequence of data points.

◆ interp1() [1/2]

template<class T>
T mathUtils::interp1 ( const T & xIn,
const T & yIn,
const T & xOut,
bool extrap = true )

Linear interpolation for a vector of desired outputs.

T is a container of doubles, e.g. vector<double> or Eigen::ArrayXd.

Parameters
xInInput x-coordinates, length N. Must be monotonically increasing.
yInInput y-coordinates, length N
xOutTarget x-coordinates, length M
extrapLinearly extrapolate outside the input interval. If false, clip to the nearest value.
Returns
Computed y-coordinates at the points defined in xOut, length M

◆ interp1() [2/2]

template<class T>
double mathUtils::interp1 ( const T & xIn,
const T & yIn,
const double xOut,
bool extrap = true )

Linear interpolation for a single output point.

T is a container of dobules, e.g. vector<double> or Eigen::ArrayXd.

Parameters
xInInput x-coordinates, length N. Must be monotonically increasing.
yInInput y-coordinates, length N
xOutTarget x-coordinate
extrapLinearly extrapolate outside the input interval. If false, clip to the nearest value.
Returns
Computed y-coordinate at xOut

◆ splines() [1/2]

dvec mathUtils::splines ( const dvec & xIn,
const dvec & yIn,
const dvec & xOut )

Cubic spline interpolation for a vector of desired outputs.

Parameters
xInInput x-coordinates, length N. Must be monotonically increasing.
yInInput y-coordinates, length N
xOutTarget x-coordinates, length M
Returns
Computed y-coordinates at the points defined in xOut, length M

◆ splines() [2/2]

double mathUtils::splines ( const dvec & xIn,
const dvec & yIn,
const double xOut )

Cubic spline interpolation for a single output point.

Parameters
xInInput x-coordinates, length N. Must be monotonically increasing.
yInInput y-coordinates, length N
xOutTarget x-coordinate
Returns
Computed y-coordinate at xOut

◆ integrate()

template<class T1, class T2>
double mathUtils::integrate ( const T1 & x,
const T2 & y )

Numerical integration using the cubic spline fit for the given data.

◆ trapz()

template<class T1, class T2>
double mathUtils::trapz ( const T1 & x,
const T2 & y )

Trapezoidal rule integration.

◆ vectorVectorToArray2D()

void mathUtils::vectorVectorToArray2D ( const vector< dvector > & v,
dmatrix & a )

◆ array2DToVectorVector()

void mathUtils::array2DToVectorVector ( const dmatrix & a,
vector< dvector > & v )

◆ stringify() [1/3]

std::string mathUtils::stringify ( double x)

◆ stringify() [2/3]

std::string mathUtils::stringify ( double x,
int nDigits )

◆ stringify() [3/3]

std::string mathUtils::stringify ( int x)

◆ sign() [1/2]

int mathUtils::sign ( const double x)

◆ sign() [2/2]

int mathUtils::sign ( const int x)

◆ uniqueSort()

template<class Tx, class Ty>
void mathUtils::uniqueSort ( vector< Tx > & keys,
vector< vector< Ty > > & values )

Sort & remove duplicate entries from keys, and perform the same permutation on values Requires that keys.size() == values[i].size()