I am using 2 nested SortedDictionaries to construct sparse matrix
Here is the custom simularity(sim) function I wrote . Now it has O(n^2) complexity. I looking for suggestions to improve robustness and efficiency.Thanks for any help.
double
a = 0, b = 0,
sqrta = 0,
sqrtb = 0,
sim = 0;
foreach (var word_i in dict)
{
foreach (var word_j in dict)
{
if (word_i.Key == word_j.Key) continue;
sim=a=b=sqrta=sqrtb=0;
foreach (var term in word_j.Value.Keys)
{
if (word_i.Value.ContainsKey(term))
{
word_i.Value.TryGetValue(term,out a);
word_j.Value.TryGetValue(term,out b);
sim += a * b;
sqrta += Math.Pow(a,2);
sqrtb += Math.Pow(b,2);
}
}
sim /= Math.Sqrt(sqrta) * Math.Sqrt(sqrtb);
}
}
sim
is set to 0 in every iteration of the middle loop; isn'tsim
supposed to be the output value? \$\endgroup\$dict
? Seeing the type will make it easier to follow. I am guessing its aDictionary<string, Dictionary<string, double>>
? Also what are the keys of the inner dictionary? And are most of them common to all words or present on only a few words each? \$\endgroup\$