---
name: "mlpack_tree_CosineTree"
section: "3"
platform: "linux"
package: "mlpack-doc"
version: "1.0.10-1"
release: "Debian 8 (Jessie)"
platforms: ["linux"]
url: "https://new.carta.tech/mlpack_tree_CosineTree"
---

# mlpack\_tree\_CosineTree(3)

## NAME

mlpack::tree::CosineTree -

## SYNOPSIS

### Public Member Functions

**CosineTree** (const arma::mat &**dataset**)\
 **CosineTree** *constructor for the root node of the tree.*  **CosineTree** (**CosineTree** &parentNode, const std::vector\< size\_t \> &subIndices)\
 **CosineTree** *constructor for nodes other than the root node of the tree.*  **CosineTree** (const arma::mat &**dataset**, const double **epsilon**, const double **delta**)\
 *Construct the* **CosineTree** *and the basis for the given matrix, and passed 'epsilon' and 'delta' parameters.*  void **BasisVector** (arma::vec &bVector)\
 *Set the basis vector of the node.*  arma::vec & **BasisVector** ()\
 *Get the basis vector of the node.*  size\_t **BinarySearch** (arma::vec &cDistribution, double value, size\_t start, size\_t end)\
 *Sample a column based on the cumulative Length-Squared distribution of the cosine node, and a randomly generated value in the range \[0, 1\].*  void **CalculateCentroid** ()\
 *Calculate centroid of the columns present in the node.*  void **CalculateCosines** (arma::vec &cosines)\
 *Calculate cosines of the columns present in the node, with respect to the sampled splitting point.*  arma::vec & **Centroid** ()\
 *Get pointer to the centroid vector.*  size\_t **ColumnSampleLS** ()\
 *Sample a point from the Length-Squared distribution of the cosine node.*  void **ColumnSamplesLS** (std::vector\< size\_t \> &sampledIndices, arma::vec &probabilities, size\_t numSamples)\
 *Sample 'numSamples' points from the Length-Squared distribution of the cosine node.*  void **ConstructBasis** (**CosineNodeQueue** &treeQueue)\
 *Constructs the final basis matrix, after the cosine tree construction.*  void **CosineNodeSplit** ()\
 *This function splits the cosine node into two children based on the cosines of the columns contained in the node, with respect to the sampled splitting point.*  double **FrobNormSquared** () const \
 *Get the Frobenius norm squared of columns in the node.*  const arma::mat & **GetDataset** () const \
 *Get pointer to the dataset matrix.*  void **GetFinalBasis** (arma::mat &finalBasis)\
 *Returns the basis of the constructed subspace.*  void **L2Error** (const double error)\
 *Set the Monte Carlo error.*  double **L2Error** () const \
 *Get the Monte Carlo error.*  **CosineTree** \* **Left** ()\
 *Get pointer to the left child of the node.*  void **ModifiedGramSchmidt** (**CosineNodeQueue** &treeQueue, arma::vec &**centroid**, arma::vec &newBasisVector, arma::vec \*addBasisVector=NULL)\
 *Calculates the orthonormalization of the passed centroid, with respect to the current vector subspace.*  double **MonteCarloError** (**CosineTree** \*node, **CosineNodeQueue** &treeQueue, arma::vec \*addBasisVector1=NULL, arma::vec \*addBasisVector2=NULL)\
 *Estimates the squared error of the projection of the input node's matrix onto the current vector subspace.*  size\_t **NumColumns** () const \
 *Get number of columns of input matrix in the node.*  **CosineTree** \* **Right** ()\
 *Get pointer to the right child of the node.*  size\_t **SplitPointIndex** () const \
 *Get the column index of split point of the node.*  std::vector\< size\_t \> & **VectorIndices** ()\
 *Get the indices of columns in the node.*

### Private Attributes

arma::mat **basis**\
 *Subspace basis of the input dataset.*  arma::vec **basisVector**\
 *Orthonormalized basis vector of the node.*  arma::vec **centroid**\
 *Centroid of columns of input matrix in the node.*  const arma::mat & **dataset**\
 *Matrix for which cosine tree is constructed.*  double **delta**\
 *Cumulative probability for Monte Carlo error lower bound.*  double **epsilon**\
 *Error tolerance fraction for calculated subspace.*  double **frobNormSquared**\
 *Frobenius norm squared of columns in the node.*  std::vector\< size\_t \> **indices**\
 *Indices of columns of input matrix in the node.*  double **l2Error**\
 *Monte Carlo error for this node.*  arma::vec **l2NormsSquared**\
 *L2-norm squared of columns in the node.*  **CosineTree** \* **left**\
 *Left child of the node.*  size\_t **numColumns**\
 *Number of columns of input matrix in the node.*  **CosineTree** \* **parent**\
 *Parent of the node.*  **CosineTree** \* **right**\
 *Right child of the node.*  size\_t **splitPointIndex**\
 *Index of split point of cosine node.*

## Detailed Description

Definition at line 40 of file cosine\_tree.hpp.

## Constructor & Destructor Documentation

### mlpack::tree::CosineTree::CosineTree (const arma::mat &dataset)

**CosineTree** constructor for the root node of the tree. It initializes the necessary variables required for splitting of the node, and building the tree further. It takes a pointer to the input matrix and calculates the relevant variables using it.

**Parameters:**

*dataset* Matrix for which cosine tree is constructed.

### mlpack::tree::CosineTree::CosineTree (CosineTree &parentNode, const std::vector\< size\_t \> &subIndices)

**CosineTree** constructor for nodes other than the root node of the tree. It takes in a pointer to the parent node and a list of column indices which mentions the columns to be included in the node. The function calculate the relevant variables just like the constructor above.

**Parameters:**

*parentNode* Pointer to the parent cosine node. \
 *subIndices* Pointer to vector of column indices to be included.

### mlpack::tree::CosineTree::CosineTree (const arma::mat &dataset, const doubleepsilon, const doubledelta)

Construct the **CosineTree** and the basis for the given matrix, and passed 'epsilon' and 'delta' parameters. The **CosineTree** is constructed by splitting nodes in the direction of maximum error, stored using a priority queue. Basis vectors are added from the left and right children of the split node. The basis vector from a node is the orthonormalized centroid of its columns. The splitting continues till the Monte Carlo estimate of the input matrix's projection on the obtained subspace is less than a fraction of the norm of the input matrix.

**Parameters:**

*dataset* Matrix for which the **CosineTree** is constructed. \
 *epsilon* Error tolerance fraction for calculated subspace. \
 *delta* Cumulative probability for Monte Carlo error lower bound.

## Member Function Documentation

### void mlpack::tree::CosineTree::BasisVector (arma::vec &bVector)` [inline]`

Set the basis vector of the node.

Definition at line 194 of file cosine\_tree.hpp.

References basisVector.

### arma::vec& mlpack::tree::CosineTree::BasisVector ()` [inline]`

Get the basis vector of the node.

Definition at line 197 of file cosine\_tree.hpp.

References basisVector.

### size\_t mlpack::tree::CosineTree::BinarySearch (arma::vec &cDistribution, doublevalue, size\_tstart, size\_tend)

Sample a column based on the cumulative Length-Squared distribution of the cosine node, and a randomly generated value in the range \[0, 1\]. Binary search is more efficient than searching linearly for the same. This leads a significant speedup when there are large number of columns to choose from and when a number of samples are to be drawn from the distribution.

**Parameters:**

*cDistribution* Cumulative LS distibution of columns in the node. \
 *value* Randomly generated value in the range \[0, 1\]. \
 *start* Starting index of the distribution interval to search in. \
 *end* Ending index of the distribution interval to search in.

### void mlpack::tree::CosineTree::CalculateCentroid ()

Calculate centroid of the columns present in the node. The calculated centroid is used as a basis vector for the cosine tree being constructed.

### void mlpack::tree::CosineTree::CalculateCosines (arma::vec &cosines)

Calculate cosines of the columns present in the node, with respect to the sampled splitting point. The calculated cosine values are useful for splitting the node into its children.

**Parameters:**

*cosines* Vector to store the cosine values in.

### arma::vec& mlpack::tree::CosineTree::Centroid ()` [inline]`

Get pointer to the centroid vector.

Definition at line 191 of file cosine\_tree.hpp.

References centroid.

### size\_t mlpack::tree::CosineTree::ColumnSampleLS ()

Sample a point from the Length-Squared distribution of the cosine node. The function uses 'l2NormsSquared' to calculate the cumulative probability distribution of the column vectors. The sampling is based on a randomly generated value in the range \[0, 1\].

### void mlpack::tree::CosineTree::ColumnSamplesLS (std::vector\< size\_t \> &sampledIndices, arma::vec &probabilities, size\_tnumSamples)

Sample 'numSamples' points from the Length-Squared distribution of the cosine node. The function uses 'l2NormsSquared' to calculate the cumulative probability distribution of the column vectors. The sampling is based on a randomly generated values in the range \[0, 1\].

### void mlpack::tree::CosineTree::ConstructBasis (CosineNodeQueue &treeQueue)

Constructs the final basis matrix, after the cosine tree construction.

**Parameters:**

*treeQueue* Priority queue of cosine nodes.

### void mlpack::tree::CosineTree::CosineNodeSplit ()

This function splits the cosine node into two children based on the cosines of the columns contained in the node, with respect to the sampled splitting point. The function also calls the **CosineTree** constructor for the children.

### double mlpack::tree::CosineTree::FrobNormSquared () const` [inline]`

Get the Frobenius norm squared of columns in the node.

Definition at line 209 of file cosine\_tree.hpp.

References frobNormSquared.

### const arma::mat& mlpack::tree::CosineTree::GetDataset () const` [inline]`

Get pointer to the dataset matrix.

Definition at line 179 of file cosine\_tree.hpp.

References dataset.

### void mlpack::tree::CosineTree::GetFinalBasis (arma::mat &finalBasis)` [inline]`

Returns the basis of the constructed subspace.

Definition at line 176 of file cosine\_tree.hpp.

References basis.

### void mlpack::tree::CosineTree::L2Error (const doubleerror)` [inline]`

Set the Monte Carlo error.

Definition at line 185 of file cosine\_tree.hpp.

References l2Error.

Referenced by mlpack::tree::CompareCosineNode::operator()().

### double mlpack::tree::CosineTree::L2Error () const` [inline]`

Get the Monte Carlo error.

Definition at line 188 of file cosine\_tree.hpp.

References l2Error.

### CosineTree\* mlpack::tree::CosineTree::Left ()` [inline]`

Get pointer to the left child of the node.

Definition at line 200 of file cosine\_tree.hpp.

References left.

### void mlpack::tree::CosineTree::ModifiedGramSchmidt (CosineNodeQueue &treeQueue, arma::vec &centroid, arma::vec &newBasisVector, arma::vec \*addBasisVector = `NULL`)

Calculates the orthonormalization of the passed centroid, with respect to the current vector subspace.

**Parameters:**

*treeQueue* Priority queue of cosine nodes. \
 *centroid* Centroid of the node being added to the basis. \
 *newBasisVector* Orthonormalized centroid of the node. \
 *addBasisVector* Address to additional basis vector.

### double mlpack::tree::CosineTree::MonteCarloError (CosineTree \*node, CosineNodeQueue &treeQueue, arma::vec \*addBasisVector1 = `NULL`, arma::vec \*addBasisVector2 = `NULL`)

Estimates the squared error of the projection of the input node's matrix onto the current vector subspace. A normal distribution is fit using weighted norms of projections of samples drawn from the input node's matrix columns. The error is calculated as the difference between the Frobenius norm of the input node's matrix and lower bound of the normal distribution.

**Parameters:**

*node* Node for which Monte Carlo estimate is calculated. \
 *treeQueue* Priority queue of cosine nodes. \
 *addBasisVector1* Address to first additional basis vector. \
 *addBasisVector2* Address to second additional basis vector.

### size\_t mlpack::tree::CosineTree::NumColumns () const` [inline]`

Get number of columns of input matrix in the node.

Definition at line 206 of file cosine\_tree.hpp.

References numColumns.

### CosineTree\* mlpack::tree::CosineTree::Right ()` [inline]`

Get pointer to the right child of the node.

Definition at line 203 of file cosine\_tree.hpp.

References right.

### size\_t mlpack::tree::CosineTree::SplitPointIndex () const` [inline]`

Get the column index of split point of the node.

Definition at line 212 of file cosine\_tree.hpp.

References indices, and splitPointIndex.

### std::vector\<size\_t\>& mlpack::tree::CosineTree::VectorIndices ()` [inline]`

Get the indices of columns in the node.

Definition at line 182 of file cosine\_tree.hpp.

References indices.

## Member Data Documentation

### arma::mat mlpack::tree::CosineTree::basis` [private]`

Subspace basis of the input dataset.

Definition at line 222 of file cosine\_tree.hpp.

Referenced by GetFinalBasis().

### arma::vec mlpack::tree::CosineTree::basisVector` [private]`

Orthonormalized basis vector of the node.

Definition at line 236 of file cosine\_tree.hpp.

Referenced by BasisVector().

### arma::vec mlpack::tree::CosineTree::centroid` [private]`

Centroid of columns of input matrix in the node.

Definition at line 234 of file cosine\_tree.hpp.

Referenced by Centroid().

### const arma::mat& mlpack::tree::CosineTree::dataset` [private]`

Matrix for which cosine tree is constructed.

Definition at line 216 of file cosine\_tree.hpp.

Referenced by GetDataset().

### double mlpack::tree::CosineTree::delta` [private]`

Cumulative probability for Monte Carlo error lower bound.

Definition at line 220 of file cosine\_tree.hpp.

### double mlpack::tree::CosineTree::epsilon` [private]`

Error tolerance fraction for calculated subspace.

Definition at line 218 of file cosine\_tree.hpp.

### double mlpack::tree::CosineTree::frobNormSquared` [private]`

Frobenius norm squared of columns in the node.

Definition at line 244 of file cosine\_tree.hpp.

Referenced by FrobNormSquared().

### std::vector\<size\_t\> mlpack::tree::CosineTree::indices` [private]`

Indices of columns of input matrix in the node.

Definition at line 230 of file cosine\_tree.hpp.

Referenced by SplitPointIndex(), and VectorIndices().

### double mlpack::tree::CosineTree::l2Error` [private]`

Monte Carlo error for this node.

Definition at line 242 of file cosine\_tree.hpp.

Referenced by L2Error().

### arma::vec mlpack::tree::CosineTree::l2NormsSquared` [private]`

L2-norm squared of columns in the node.

Definition at line 232 of file cosine\_tree.hpp.

### CosineTree\* mlpack::tree::CosineTree::left` [private]`

Left child of the node.

Definition at line 228 of file cosine\_tree.hpp.

Referenced by Left().

### size\_t mlpack::tree::CosineTree::numColumns` [private]`

Number of columns of input matrix in the node.

Definition at line 240 of file cosine\_tree.hpp.

Referenced by NumColumns().

### CosineTree\* mlpack::tree::CosineTree::parent` [private]`

Parent of the node.

Definition at line 224 of file cosine\_tree.hpp.

### CosineTree\* mlpack::tree::CosineTree::right` [private]`

Right child of the node.

Definition at line 226 of file cosine\_tree.hpp.

Referenced by Right().

### size\_t mlpack::tree::CosineTree::splitPointIndex` [private]`

Index of split point of cosine node.

Definition at line 238 of file cosine\_tree.hpp.

Referenced by SplitPointIndex().

## Author

Generated automatically by Doxygen for MLPACK from the source code.
