\section{Mahalanobis\+Distance$<$ Take\+Root $>$ Class Template Reference}
\label{classmlpack_1_1metric_1_1MahalanobisDistance}\index{Mahalanobis\+Distance$<$ Take\+Root $>$@{Mahalanobis\+Distance$<$ Take\+Root $>$}}


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


\subsection*{Public Member Functions}
\begin{DoxyCompactItemize}
\item 
\textbf{ Mahalanobis\+Distance} ()
\begin{DoxyCompactList}\small\item\em Initialize the Mahalanobis distance with the empty matrix as covariance. \end{DoxyCompactList}\item 
\textbf{ Mahalanobis\+Distance} (const size\+\_\+t dimensionality)
\begin{DoxyCompactList}\small\item\em Initialize the Mahalanobis distance with the identity matrix of the given dimensionality. \end{DoxyCompactList}\item 
\textbf{ Mahalanobis\+Distance} (arma\+::mat covariance)
\begin{DoxyCompactList}\small\item\em Initialize the Mahalanobis distance with the given covariance matrix. \end{DoxyCompactList}\item 
const arma\+::mat \& \textbf{ Covariance} () const
\begin{DoxyCompactList}\small\item\em Access the covariance matrix. \end{DoxyCompactList}\item 
arma\+::mat \& \textbf{ Covariance} ()
\begin{DoxyCompactList}\small\item\em Modify the covariance matrix. \end{DoxyCompactList}\item 
{\footnotesize template$<$typename Vec\+TypeA , typename Vec\+TypeB $>$ }\\double \textbf{ Evaluate} (const Vec\+TypeA \&a, const Vec\+TypeB \&b)
\begin{DoxyCompactList}\small\item\em Evaluate the distance between the two given points using this Mahalanobis distance. \end{DoxyCompactList}\item 
{\footnotesize template$<$typename Archive $>$ }\\void \textbf{ serialize} (Archive \&ar, const unsigned int version)
\begin{DoxyCompactList}\small\item\em Serialize the Mahalanobis distance. \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Detailed Description}
\subsubsection*{template$<$bool Take\+Root = true$>$\newline
class mlpack\+::metric\+::\+Mahalanobis\+Distance$<$ Take\+Root $>$}

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 $,

\[ d(x, y) = \sqrt{(x - y)^T Q (x - y)} \]

where Q is the covariance matrix.

Because each evaluation multiplies (x\+\_\+1 -\/ x\+\_\+2) by the covariance matrix, it is typically much quicker to use an \doxyref{L\+Metric}{p.}{classmlpack_1_1metric_1_1LMetric} and simply stretch the actual dataset itself before performing any evaluations. However, this class is provided for convenience.

If you wish to use the K\+NN class or other tree-\/based algorithms with this distance, it is recommended to instead stretch the dataset first, by decomposing Q = L$^\wedge$T L (perhaps via a Cholesky decomposition), and then multiply the data by L. If you still wish to use the K\+NN class with a custom distance anyway, you will need to use a different tree type than the default K\+D\+Tree, which only works with the \doxyref{L\+Metric}{p.}{classmlpack_1_1metric_1_1LMetric} class.

Similar to the \doxyref{L\+Metric}{p.}{classmlpack_1_1metric_1_1LMetric} class, this offers a template parameter Take\+Root which, when set to false, will instead evaluate the distance

\[ d(x, y) = (x - y)^T Q (x - y) \]

which is faster to evaluate.


\begin{DoxyTemplParams}{Template Parameters}
{\em Take\+Root} & If true, takes the root of the output. It is slightly faster to leave this at the default of false, but this means the metric may not satisfy the triangle inequality and may not be usable for methods that expect a true metric. \\
\hline
\end{DoxyTemplParams}


Definition at line 60 of file mahalanobis\+\_\+distance.\+hpp.



\subsection{Constructor \& Destructor Documentation}
\mbox{\label{classmlpack_1_1metric_1_1MahalanobisDistance_a00cbb536d89ebdafc57f3a481aa772e4}} 
\index{mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}!Mahalanobis\+Distance@{Mahalanobis\+Distance}}
\index{Mahalanobis\+Distance@{Mahalanobis\+Distance}!mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}}
\subsubsection{Mahalanobis\+Distance()\hspace{0.1cm}{\footnotesize\ttfamily [1/3]}}
{\footnotesize\ttfamily \textbf{ Mahalanobis\+Distance} (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [inline]}}



Initialize the Mahalanobis distance with the empty matrix as covariance. 

Don\textquotesingle{}t call \doxyref{Evaluate()}{p.}{classmlpack_1_1metric_1_1MahalanobisDistance_a7bc3cfc0ee3750ba84ee4dc7f62994ae} until you set the covariance with \doxyref{Covariance()}{p.}{classmlpack_1_1metric_1_1MahalanobisDistance_aa65080f46932c7155e87b017e8fa58f5}! 

Definition at line 67 of file mahalanobis\+\_\+distance.\+hpp.

\mbox{\label{classmlpack_1_1metric_1_1MahalanobisDistance_a68f439d98cd6d071ed45bd91f47ebfcb}} 
\index{mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}!Mahalanobis\+Distance@{Mahalanobis\+Distance}}
\index{Mahalanobis\+Distance@{Mahalanobis\+Distance}!mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}}
\subsubsection{Mahalanobis\+Distance()\hspace{0.1cm}{\footnotesize\ttfamily [2/3]}}
{\footnotesize\ttfamily \textbf{ Mahalanobis\+Distance} (\begin{DoxyParamCaption}\item[{const size\+\_\+t}]{dimensionality }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [inline]}}



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


\begin{DoxyParams}{Parameters}
{\em dimensionality} & Dimesnsionality of the covariance matrix. \\
\hline
\end{DoxyParams}


Definition at line 75 of file mahalanobis\+\_\+distance.\+hpp.

\mbox{\label{classmlpack_1_1metric_1_1MahalanobisDistance_a385234d4f1d914f7786c481fa805c31c}} 
\index{mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}!Mahalanobis\+Distance@{Mahalanobis\+Distance}}
\index{Mahalanobis\+Distance@{Mahalanobis\+Distance}!mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}}
\subsubsection{Mahalanobis\+Distance()\hspace{0.1cm}{\footnotesize\ttfamily [3/3]}}
{\footnotesize\ttfamily \textbf{ Mahalanobis\+Distance} (\begin{DoxyParamCaption}\item[{arma\+::mat}]{covariance }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [inline]}}



Initialize the Mahalanobis distance with the given covariance matrix. 

The given covariance matrix will be copied (this is not optimal).


\begin{DoxyParams}{Parameters}
{\em covariance} & The covariance matrix to use for this distance. \\
\hline
\end{DoxyParams}


Definition at line 84 of file mahalanobis\+\_\+distance.\+hpp.



References Mahalanobis\+Distance$<$ Take\+Root $>$\+::\+Evaluate().



\subsection{Member Function Documentation}
\mbox{\label{classmlpack_1_1metric_1_1MahalanobisDistance_a7cab8a0b8c0deac1833f3e489fa52daf}} 
\index{mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}!Covariance@{Covariance}}
\index{Covariance@{Covariance}!mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}}
\subsubsection{Covariance()\hspace{0.1cm}{\footnotesize\ttfamily [1/2]}}
{\footnotesize\ttfamily const arma\+::mat\& Covariance (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption}) const\hspace{0.3cm}{\ttfamily [inline]}}



Access the covariance matrix. 

\begin{DoxyReturn}{Returns}
Constant reference to the covariance matrix. 
\end{DoxyReturn}


Definition at line 104 of file mahalanobis\+\_\+distance.\+hpp.

\mbox{\label{classmlpack_1_1metric_1_1MahalanobisDistance_aa65080f46932c7155e87b017e8fa58f5}} 
\index{mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}!Covariance@{Covariance}}
\index{Covariance@{Covariance}!mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}}
\subsubsection{Covariance()\hspace{0.1cm}{\footnotesize\ttfamily [2/2]}}
{\footnotesize\ttfamily arma\+::mat\& Covariance (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [inline]}}



Modify the covariance matrix. 

\begin{DoxyReturn}{Returns}
Reference to the covariance matrix. 
\end{DoxyReturn}


Definition at line 111 of file mahalanobis\+\_\+distance.\+hpp.



References Mahalanobis\+Distance$<$ Take\+Root $>$\+::serialize().

\mbox{\label{classmlpack_1_1metric_1_1MahalanobisDistance_a7bc3cfc0ee3750ba84ee4dc7f62994ae}} 
\index{mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}!Evaluate@{Evaluate}}
\index{Evaluate@{Evaluate}!mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}}
\subsubsection{Evaluate()}
{\footnotesize\ttfamily double Evaluate (\begin{DoxyParamCaption}\item[{const Vec\+TypeA \&}]{a,  }\item[{const Vec\+TypeB \&}]{b }\end{DoxyParamCaption})}



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.


\begin{DoxyParams}{Parameters}
{\em a} & First vector. \\
\hline
{\em b} & Second vector. \\
\hline
\end{DoxyParams}


Referenced by Mahalanobis\+Distance$<$ Take\+Root $>$\+::\+Mahalanobis\+Distance().

\mbox{\label{classmlpack_1_1metric_1_1MahalanobisDistance_a68e832cb064e3b7ca978d8e5911cf700}} 
\index{mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}!serialize@{serialize}}
\index{serialize@{serialize}!mlpack\+::metric\+::\+Mahalanobis\+Distance@{mlpack\+::metric\+::\+Mahalanobis\+Distance}}
\subsubsection{serialize()}
{\footnotesize\ttfamily void serialize (\begin{DoxyParamCaption}\item[{Archive \&}]{ar,  }\item[{const unsigned int}]{version }\end{DoxyParamCaption})}



Serialize the Mahalanobis distance. 



Referenced by Mahalanobis\+Distance$<$ Take\+Root $>$\+::\+Covariance().



The documentation for this class was generated from the following file\+:\begin{DoxyCompactItemize}
\item 
/var/www/mlpack.\+ratml.\+org/mlpack.\+org/\+\_\+src/mlpack-\/3.\+3.\+1/src/mlpack/core/metrics/\textbf{ mahalanobis\+\_\+distance.\+hpp}\end{DoxyCompactItemize}
