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

# mlpack\_metric\_MahalanobisDistance(3)

The Mahalanobis distance, which is essentially a stretched Euclidean distance.

## NAME

mlpack::metric::MahalanobisDistance\< TakeRoot \> -

The Mahalanobis distance, which is essentially a stretched Euclidean distance.

## SYNOPSIS

### Public Member Functions

**MahalanobisDistance** ()\
 *Initialize the Mahalanobis distance with the empty matrix as covariance.*  **MahalanobisDistance** (const size\_t dimensionality)\
 *Initialize the Mahalanobis distance with the identity matrix of the given dimensionality.*  **MahalanobisDistance** (const arma::mat &**covariance**)\
 *Initialize the Mahalanobis distance with the given covariance matrix.*  const arma::mat & **Covariance** () const \
 *Access the covariance matrix.*  arma::mat & **Covariance** ()\
 *Modify the covariance matrix.*  template\<typename VecType1 , typename VecType2 \> double **Evaluate** (const VecType1 &a, const VecType2 &b)\
 std::string **ToString** () const \
 *Evaluate the distance between the two given points using this Mahalanobis distance.*

### Private Attributes

arma::mat **covariance**\
 *The covariance matrix associated with this distance.*

## Detailed Description

### template\<bool TakeRoot = false\>class mlpack::metric::MahalanobisDistance\< TakeRoot \>

The Mahalanobis distance, which is essentially a stretched Euclidean distance.

Given a square covariance matrix $ Q $ of size $ d $ x $ d $, where $ d $ is the dimensionality of the points it will be evaluating, and given two vectors $ x $ and $ y $ also of dimensionality $ d $,

.PP where Q is the covariance matrix.

Because each evaluation multiplies (x\_1 - x\_2) by the covariance matrix, it may be much quicker to use an **LMetric** and simply stretch the actual dataset itself before performing any evaluations. However, this class is provided for convenience.

Similar to the **LMetric** class, this offers a template parameter TakeRoot which, when set to false, will instead evaluate the distance

.PP which is faster to evaluate.

**Template Parameters:**

*TakeRoot* If true, takes the root of the output. It is slightly faster to leave this at the default of false.

Definition at line 61 of file mahalanobis\_distance.hpp.

## Constructor & Destructor Documentation

### template\<bool TakeRoot = false\> mlpack::metric::MahalanobisDistance\< TakeRoot \>::MahalanobisDistance ()` [inline]`

Initialize the Mahalanobis distance with the empty matrix as covariance. Don't call **Evaluate()** until you set the covariance with **Covariance()**!

Definition at line 68 of file mahalanobis\_distance.hpp.

### template\<bool TakeRoot = false\> mlpack::metric::MahalanobisDistance\< TakeRoot \>::MahalanobisDistance (const size\_tdimensionality)` [inline]`

Initialize the Mahalanobis distance with the identity matrix of the given dimensionality.

**Parameters:**

*dimensionality* Dimesnsionality of the covariance matrix.

Definition at line 76 of file mahalanobis\_distance.hpp.

### template\<bool TakeRoot = false\> mlpack::metric::MahalanobisDistance\< TakeRoot \>::MahalanobisDistance (const arma::mat &covariance)` [inline]`

Initialize the Mahalanobis distance with the given covariance matrix. The given covariance matrix will be copied (this is not optimal).

**Parameters:**

*covariance* The covariance matrix to use for this distance.

Definition at line 85 of file mahalanobis\_distance.hpp.

## Member Function Documentation

### template\<bool TakeRoot = false\> const arma::mat& mlpack::metric::MahalanobisDistance\< TakeRoot \>::Covariance () const` [inline]`

Access the covariance matrix.

**Returns:**

Constant reference to the covariance matrix.

Definition at line 107 of file mahalanobis\_distance.hpp.

References mlpack::metric::MahalanobisDistance\< TakeRoot \>::covariance.

### template\<bool TakeRoot = false\> arma::mat& mlpack::metric::MahalanobisDistance\< TakeRoot \>::Covariance ()` [inline]`

Modify the covariance matrix.

**Returns:**

Reference to the covariance matrix.

Definition at line 114 of file mahalanobis\_distance.hpp.

References mlpack::metric::MahalanobisDistance\< TakeRoot \>::covariance.

### template\<bool TakeRoot = false\> template\<typename VecType1 , typename VecType2 \> double mlpack::metric::MahalanobisDistance\< TakeRoot \>::Evaluate (const VecType1 &a, const VecType2 &b)

### template\<bool TakeRoot = false\> std::string mlpack::metric::MahalanobisDistance\< TakeRoot \>::ToString () const

Evaluate the distance between the two given points using this Mahalanobis distance. If the covariance matrix has not been set (i.e. if you used the empty constructor and did not later modify the covariance matrix), calling this method will probably result in a crash.

**Parameters:**

*a* First vector. \
 *b* Second vector.

## Member Data Documentation

### template\<bool TakeRoot = false\> arma::mat mlpack::metric::MahalanobisDistance\< TakeRoot \>::covariance` [private]`

The covariance matrix associated with this distance.

Definition at line 117 of file mahalanobis\_distance.hpp.

Referenced by mlpack::metric::MahalanobisDistance\< TakeRoot \>::Covariance().

## Author

Generated automatically by Doxygen for MLPACK from the source code.
