mlpack
|
The L_p metric for arbitrary integer p, with an option to take the root. More...
#include <lmetric.hpp>
Public Member Functions | |
LMetric () | |
Default constructor does nothing, but is required to satisfy the Metric policy. | |
template<typename Archive > | |
void | serialize (Archive &, const uint32_t) |
Serialize the metric (nothing to do). | |
template<> | |
VecTypeA::elem_type | Evaluate (const VecTypeA &a, const VecTypeB &b) |
template<> | |
VecTypeA::elem_type | Evaluate (const VecTypeA &a, const VecTypeB &b) |
template<> | |
VecTypeA::elem_type | Evaluate (const VecTypeA &a, const VecTypeB &b) |
template<> | |
VecTypeA::elem_type | Evaluate (const VecTypeA &a, const VecTypeB &b) |
template<> | |
VecTypeA::elem_type | Evaluate (const VecTypeA &a, const VecTypeB &b) |
template<> | |
VecTypeA::elem_type | Evaluate (const VecTypeA &a, const VecTypeB &b) |
template<> | |
VecTypeA::elem_type | Evaluate (const VecTypeA &a, const VecTypeB &b) |
Static Public Member Functions | |
template<typename VecTypeA , typename VecTypeB > | |
static VecTypeA::elem_type | Evaluate (const VecTypeA &a, const VecTypeB &b) |
Computes the distance between two points. More... | |
Static Public Attributes | |
static const int | Power = TPower |
The power of the metric. | |
static const bool | TakeRoot = TTakeRoot |
Whether or not the root is taken. | |
The L_p metric for arbitrary integer p, with an option to take the root.
This class implements the standard L_p metric for two arbitrary vectors \( x \) and \( y \) of dimensionality \( n \):
\[ d(x, y) = \left( \sum_{i = 1}^{n} | x_i - y_i |^p \right)^{\frac{1}{p}}. \]
The value of p is given as a template parameter.
In addition, the function \( d(x, y) \) can be simplified, neglecting the p-root calculation. This is done by specifying the TakeRoot template parameter to be false. Then,
\[ d(x, y) = \sum_{i = 1}^{n} | x_i - y_i |^p \]
It is faster to compute that distance, so TakeRoot is by default off. However, when TakeRoot is false, the distance given is not actually a true metric – it does not satisfy the triangle inequality. Some mlpack methods do not require the triangle inequality to operate correctly (such as the BinarySpaceTree), but setting TakeRoot = false in some cases will cause incorrect results.
A few convenience typedefs are given:
Power | Power of metric; i.e. Power = 1 gives the L1-norm (Manhattan distance). |
TakeRoot | If true, the Power'th root of the result is taken before it is returned. Setting this to false causes the metric to not satisfy the Triangle Inequality (be careful!). |
|
static |
Computes the distance between two points.
VecTypeA | Type of first vector (generally arma::vec or arma::sp_vec). |
VecTypeB | Type of second vector. |
a | First vector. |
b | Second vector. |