Public static double CosineBetweenVectors(double[] a, double[] b) { if (a.Length != b.Length || a.Length == 0) throw new Exception("не верные параметры");
double ab = 0; double a_length = 0; double b_length = 0; for (var i = 0; i < a.Length; i++) { ab += a[i] * b[i]; a_length = a[i] * a[i]; b_length = b[i] * b[i]; }
if (a_length * b_length == 0) throw new Exception("Нулевые вектора");
return ab / Math.Sqrt(a_length) / Math.Sqrt(b_length); }
{
if (a.Length != b.Length || a.Length == 0) throw new Exception("не верные параметры");
double ab = 0;
double a_length = 0;
double b_length = 0;
for (var i = 0; i < a.Length; i++)
{
ab += a[i] * b[i];
a_length = a[i] * a[i];
b_length = b[i] * b[i];
}
if (a_length * b_length == 0) throw new Exception("Нулевые вектора");
return ab / Math.Sqrt(a_length) / Math.Sqrt(b_length);
}