mlpack  2.2.5
mlpack::math Namespace Reference

Miscellaneous math routines. More...

Classes

class  ColumnsToBlocks
 Transform the columns of the given matrix into a block format. More...

 
class  RangeType
 Simple real-valued range. More...

 

Typedefs

typedef RangeType< double > Range
 3.0.0 TODO: break reverse-compatibility by changing RangeType to Range. More...

 

Functions

void Center (const arma::mat &x, arma::mat &xCentered)
 Creates a centered matrix, where centering is done by subtracting the sum over the columns (a column vector) from each column of the matrix. More...

 
double ClampNonNegative (const double d)
 Forces a number to be non-negative, turning negative numbers into zero. More...

 
double ClampNonPositive (const double d)
 Forces a number to be non-positive, turning positive numbers into zero. More...

 
double ClampRange (double value, const double rangeMin, const double rangeMax)
 Clamp a number between a particular range. More...

 
void ObtainDistinctSamples (const size_t loInclusive, const size_t hiExclusive, const size_t maxNumSamples, arma::uvec &distinctSamples)
 Obtains no more than maxNumSamples distinct samples. More...

 
void Orthogonalize (const arma::mat &x, arma::mat &W)
 Orthogonalize x and return the result in W, using eigendecomposition. More...

 
void Orthogonalize (arma::mat &x)
 Orthogonalize x in-place. More...

 
int RandInt (const int hiExclusive)
 Generates a uniform random integer. More...

 
int RandInt (const int lo, const int hiExclusive)
 Generates a uniform random integer. More...

 
double RandNormal ()
 Generates a normally distributed random number with mean 0 and variance 1. More...

 
double RandNormal (const double mean, const double variance)
 Generates a normally distributed random number with specified mean and variance. More...

 
double Random ()
 Generates a uniform random number between 0 and 1. More...

 
double Random (const double lo, const double hi)
 Generates a uniform random number in the specified range. More...

 
void RandomBasis (arma::mat &basis, const size_t d)
 Create a random d-dimensional orthogonal basis, storing it in the given matrix. More...

 
void RandomSeed (const size_t seed)
 Set the random seed used by the random functions (Random() and RandInt()). More...

 
void RandVector (arma::vec &v)
 Overwrites a dimension-N vector to a random vector on the unit sphere in R^N. More...

 
void RemoveRows (const arma::mat &input, const std::vector< size_t > &rowsToRemove, arma::mat &output)
 Remove a certain set of rows in a matrix while copying to a second matrix. More...

 
void Smat (const arma::vec &input, arma::mat &output)
 The inverse of Svec. More...

 
void Svec (const arma::mat &input, arma::vec &output)
 Upper triangular representation of a symmetric matrix, scaled such that, dot(Svec(A), Svec(B)) == dot(A, B) for symmetric A, B. More...

 
void Svec (const arma::sp_mat &input, arma::sp_vec &output)
 
size_t SvecIndex (size_t i, size_t j, size_t n)
 Return the index such that A[i,j] == factr(i, j) * svec(A)[pos(i, j)], where factr(i, j) = sqrt(2) if i != j and 1 otherwise. More...

 
void SymKronId (const arma::mat &A, arma::mat &op)
 If A is a symmetric matrix, then SymKronId returns an operator Op such that. More...

 
void VectorPower (arma::vec &vec, const double power)
 Auxiliary function to raise vector elements to a specific power. More...

 
void WhitenUsingEig (const arma::mat &x, arma::mat &xWhitened, arma::mat &whiteningMatrix)
 Whitens a matrix using the eigendecomposition of the covariance matrix. More...

 
void WhitenUsingSVD (const arma::mat &x, arma::mat &xWhitened, arma::mat &whiteningMatrix)
 Whitens a matrix using the singular value decomposition of the covariance matrix. More...

 

Variables

MLPACK_EXPORT std::mt19937 randGen
 MLPACK_EXPORT is required for global variables; it exports the symbols correctly on Windows. More...

 
MLPACK_EXPORT std::normal_distribution randNormalDist
 
MLPACK_EXPORT std::uniform_real_distribution randUniformDist
 

Detailed Description

Miscellaneous math routines.

Typedef Documentation

◆ Range

typedef RangeType<double> Range

3.0.0 TODO: break reverse-compatibility by changing RangeType to Range.

Definition at line 19 of file range.hpp.

Function Documentation

◆ Center()

void mlpack::math::Center ( const arma::mat &  x,
arma::mat &  xCentered 
)

Creates a centered matrix, where centering is done by subtracting the sum over the columns (a column vector) from each column of the matrix.

Parameters
xInput matrix
xCenteredMatrix to write centered output into

Referenced by NystroemKernelRule< KernelType, PointSelectionPolicy >::ApplyKernelMatrix(), and HRectBound< MetricType >::MinWidth().

◆ ClampNonNegative()

double mlpack::math::ClampNonNegative ( const double  d)
inline

Forces a number to be non-negative, turning negative numbers into zero.

Avoids branching costs (this is a measurable improvement).

Parameters
dDouble to clamp.
Returns
0 if d < 0, d otherwise.

Definition at line 28 of file clamp.hpp.

Referenced by ClampRange().

◆ ClampNonPositive()

double mlpack::math::ClampNonPositive ( const double  d)
inline

Forces a number to be non-positive, turning positive numbers into zero.

Avoids branching costs (this is a measurable improvement).

Parameters
dDouble to clamp.
0if d > 0, d otherwise.

Definition at line 40 of file clamp.hpp.

Referenced by ClampRange().

◆ ClampRange()

double mlpack::math::ClampRange ( double  value,
const double  rangeMin,
const double  rangeMax 
)
inline

Clamp a number between a particular range.

Parameters
valueThe number to clamp.
rangeMinThe first of the range.
rangeMaxThe last of the range.
Returns
max(rangeMin, min(rangeMax, d)).

Definition at line 53 of file clamp.hpp.

References ClampNonNegative(), and ClampNonPositive().

◆ ObtainDistinctSamples()

void mlpack::math::ObtainDistinctSamples ( const size_t  loInclusive,
const size_t  hiExclusive,
const size_t  maxNumSamples,
arma::uvec &  distinctSamples 
)
inline

Obtains no more than maxNumSamples distinct samples.

Each sample belongs to [loInclusive, hiExclusive).

Parameters
loInclusiveThe lower bound (inclusive).
hiExclusiveThe high bound (exclusive).
maxNumSamplesThe maximum number of samples to obtain.
distinctSamplesThe samples that will be obtained.

Definition at line 114 of file random.hpp.

References RandInt().

◆ Orthogonalize() [1/2]

void mlpack::math::Orthogonalize ( const arma::mat &  x,
arma::mat &  W 
)

Orthogonalize x and return the result in W, using eigendecomposition.

We will be using the formula $ W = x (x^T x)^{-0.5} $.

◆ Orthogonalize() [2/2]

void mlpack::math::Orthogonalize ( arma::mat &  x)

Orthogonalize x in-place.

This could be sped up by a custom implementation.

◆ RandInt() [1/2]

int mlpack::math::RandInt ( const int  hiExclusive)
inline

◆ RandInt() [2/2]

int mlpack::math::RandInt ( const int  lo,
const int  hiExclusive 
)
inline

Generates a uniform random integer.

Definition at line 79 of file random.hpp.

References randUniformDist.

◆ RandNormal() [1/2]

double mlpack::math::RandNormal ( )
inline

Generates a normally distributed random number with mean 0 and variance 1.

Definition at line 88 of file random.hpp.

References randNormalDist.

◆ RandNormal() [2/2]

double mlpack::math::RandNormal ( const double  mean,
const double  variance 
)
inline

Generates a normally distributed random number with specified mean and variance.

Parameters
meanMean of distribution.
varianceVariance of distribution.

Definition at line 100 of file random.hpp.

References randNormalDist.

◆ Random() [1/2]

double mlpack::math::Random ( )
inline

Generates a uniform random number between 0 and 1.

Definition at line 55 of file random.hpp.

References randUniformDist.

◆ Random() [2/2]

double mlpack::math::Random ( const double  lo,
const double  hi 
)
inline

Generates a uniform random number in the specified range.

Definition at line 63 of file random.hpp.

References randUniformDist.

◆ RandomBasis()

void mlpack::math::RandomBasis ( arma::mat &  basis,
const size_t  d 
)

Create a random d-dimensional orthogonal basis, storing it in the given matrix.

Parameters
basisMatrix to store basis in.
dDesired number of dimensions in the basis.

◆ RandomSeed()

void mlpack::math::RandomSeed ( const size_t  seed)
inline

Set the random seed used by the random functions (Random() and RandInt()).

The seed is casted to a 32-bit integer before being given to the random number generator, but a size_t is taken as a parameter for API consistency.

Parameters
seedSeed for the random number generator.

Definition at line 40 of file random.hpp.

◆ RandVector()

void mlpack::math::RandVector ( arma::vec &  v)

Overwrites a dimension-N vector to a random vector on the unit sphere in R^N.

◆ RemoveRows()

void mlpack::math::RemoveRows ( const arma::mat &  input,
const std::vector< size_t > &  rowsToRemove,
arma::mat &  output 
)

Remove a certain set of rows in a matrix while copying to a second matrix.

Parameters
inputInput matrix to copy.
rowsToRemoveVector containing indices of rows to be removed.
outputMatrix to copy non-removed rows into.

◆ Smat()

void mlpack::math::Smat ( const arma::vec &  input,
arma::mat &  output 
)

The inverse of Svec.

That is, Smat(Svec(A)) == A.

Parameters
input
outputA symmetric matrix

◆ Svec() [1/2]

void mlpack::math::Svec ( const arma::mat &  input,
arma::vec &  output 
)

Upper triangular representation of a symmetric matrix, scaled such that, dot(Svec(A), Svec(B)) == dot(A, B) for symmetric A, B.

Specifically,

Svec(K) = [ K_11, sqrt(2) K_12, ..., sqrt(2) K_1n, K_22, ..., sqrt(2) K_2n, ..., K_nn ]^T

Parameters
inputA symmetric matrix
output

◆ Svec() [2/2]

void mlpack::math::Svec ( const arma::sp_mat &  input,
arma::sp_vec &  output 
)

◆ SvecIndex()

size_t mlpack::math::SvecIndex ( size_t  i,
size_t  j,
size_t  n 
)
inline

Return the index such that A[i,j] == factr(i, j) * svec(A)[pos(i, j)], where factr(i, j) = sqrt(2) if i != j and 1 otherwise.

Parameters
i
j
n

◆ SymKronId()

void mlpack::math::SymKronId ( const arma::mat &  A,
arma::mat &  op 
)

If A is a symmetric matrix, then SymKronId returns an operator Op such that.

Op * svec(X) == svec(0.5 * (AX + XA))

for every symmetric matrix X

Parameters
A
op

◆ VectorPower()

void mlpack::math::VectorPower ( arma::vec &  vec,
const double  power 
)

Auxiliary function to raise vector elements to a specific power.

The sign is ignored in the power operation and then re-added. Useful for eigenvalues.

◆ WhitenUsingEig()

void mlpack::math::WhitenUsingEig ( const arma::mat &  x,
arma::mat &  xWhitened,
arma::mat &  whiteningMatrix 
)

Whitens a matrix using the eigendecomposition of the covariance matrix.

Whitening means the covariance matrix of the result is the identity matrix.

◆ WhitenUsingSVD()

void mlpack::math::WhitenUsingSVD ( const arma::mat &  x,
arma::mat &  xWhitened,
arma::mat &  whiteningMatrix 
)

Whitens a matrix using the singular value decomposition of the covariance matrix.

Whitening means the covariance matrix of the result is the identity matrix.

Variable Documentation

◆ randGen

MLPACK_EXPORT std::mt19937 randGen

MLPACK_EXPORT is required for global variables; it exports the symbols correctly on Windows.

◆ randNormalDist

MLPACK_EXPORT std::normal_distribution randNormalDist

Referenced by RandNormal().

◆ randUniformDist

MLPACK_EXPORT std::uniform_real_distribution randUniformDist

Referenced by RandInt(), and Random().