Browse Source

Distance: add Minkowski distance (generalized p-norm)

pull/184/head
Christoph Ruegg 13 years ago
parent
commit
233b4d4bcc
  1. 106
      src/Numerics/Distance.cs

106
src/Numerics/Distance.cs

@ -202,36 +202,6 @@ namespace MathNet.Numerics
return SAD(a, b);
}
/// <summary>
/// Canberra Distance, a weighted version of the L1-norm of the difference.
/// </summary>
public static double Canberra(double[] a, double[] b)
{
if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength);
double sum = 0d;
for (var i = 0; i < a.Length; i++)
{
sum += Math.Abs(a[i] - b[i])/(Math.Abs(a[i]) + Math.Abs(b[i]));
}
return sum;
}
/// <summary>
/// Canberra Distance, a weighted version of the L1-norm of the difference.
/// </summary>
public static double Canberra(float[] a, float[] b)
{
if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength);
float sum = 0f;
for (var i = 0; i < a.Length; i++)
{
sum += Math.Abs(a[i] - b[i]) / (Math.Abs(a[i]) + Math.Abs(b[i]));
}
return sum;
}
/// <summary>
/// Chebyshev Distance, i.e. the Infinity-norm of the difference.
/// </summary>
@ -276,6 +246,82 @@ namespace MathNet.Numerics
return max;
}
/// <summary>
/// Minkowski Distance, i.e. the generalized p-norm of the difference.
/// </summary>
public static double Minkowski<T>(double p, Vector<T> a, Vector<T> b) where T : struct, IEquatable<T>, IFormattable
{
return (a - b).Norm(p);
}
/// <summary>
/// Minkowski Distance, i.e. the generalized p-norm of the difference.
/// </summary>
public static double Minkowski(double p, double[] a, double[] b)
{
if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength);
if (p < 0d) throw new ArgumentOutOfRangeException("p");
if (p == 1d) return Manhattan(a, b);
if (p == 2d) return Euclidean(a, b);
if (double.IsPositiveInfinity(p)) return Chebyshev(a, b);
double sum = 0d;
for (var i = 0; i < a.Length; i++)
{
sum += Math.Pow(Math.Abs(a[i] - b[i]), p);
}
return Math.Pow(sum, 1.0 / p);
}
/// <summary>
/// Minkowski Distance, i.e. the generalized p-norm of the difference.
/// </summary>
public static float Minkowski(double p, float[] a, float[] b)
{
if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength);
if (p < 0d) throw new ArgumentOutOfRangeException("p");
if (p == 1d) return Manhattan(a, b);
if (p == 2d) return Euclidean(a, b);
if (double.IsPositiveInfinity(p)) return Chebyshev(a, b);
double sum = 0d;
for (var i = 0; i < a.Length; i++)
{
sum += Math.Pow(Math.Abs(a[i] - b[i]), p);
}
return (float) Math.Pow(sum, 1.0/p);
}
/// <summary>
/// Canberra Distance, a weighted version of the L1-norm of the difference.
/// </summary>
public static double Canberra(double[] a, double[] b)
{
if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength);
double sum = 0d;
for (var i = 0; i < a.Length; i++)
{
sum += Math.Abs(a[i] - b[i]) / (Math.Abs(a[i]) + Math.Abs(b[i]));
}
return sum;
}
/// <summary>
/// Canberra Distance, a weighted version of the L1-norm of the difference.
/// </summary>
public static float Canberra(float[] a, float[] b)
{
if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength);
float sum = 0f;
for (var i = 0; i < a.Length; i++)
{
sum += Math.Abs(a[i] - b[i]) / (Math.Abs(a[i]) + Math.Abs(b[i]));
}
return sum;
}
/// <summary>
/// Hamming Distance, i.e. the number of positions that have different values in the vectors.
/// </summary>

Loading…
Cancel
Save