Browse Source

Distance: add Canberra distance

pull/184/head
Christoph Ruegg 13 years ago
parent
commit
df9948b48f
  1. 30
      src/Numerics/Distance.cs

30
src/Numerics/Distance.cs

@ -202,6 +202,36 @@ 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>

Loading…
Cancel
Save