mirror of https://github.com/ogoun/Zero.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
728 B
29 lines
728 B
using System;
|
|
|
|
namespace ZeroLevel.HNSW.PHNSW
|
|
{
|
|
internal static class PHNSWMetric
|
|
{
|
|
internal static float CosineDistance(float[] u, float[] v)
|
|
{
|
|
if (u.Length != v.Length)
|
|
{
|
|
throw new ArgumentException("Vectors have non-matching dimensions");
|
|
}
|
|
|
|
float dot = 0.0f;
|
|
float nru = 0.0f;
|
|
float nrv = 0.0f;
|
|
for (int i = 0; i < u.Length; ++i)
|
|
{
|
|
dot += u[i] * v[i];
|
|
nru += u[i] * u[i];
|
|
nrv += v[i] * v[i];
|
|
}
|
|
|
|
var similarity = dot / (float)(Math.Sqrt(nru) * Math.Sqrt(nrv));
|
|
return 1 - similarity;
|
|
}
|
|
}
|
|
}
|