-
Notifications
You must be signed in to change notification settings - Fork 0
/
cossim.js
34 lines (21 loc) · 954 Bytes
/
cossim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Function:: cosSim()
// Authors: Corey Devin Anderson and Kirankumar Batchu
//------------------------------------------------------------------------------
// Description:
// Vectorized function for calculating cosine similarity. Utilizes matrix
// algebra functions math.dot() and math.norm() from math.js (default norm type
// is L2, which is what we want here).
//------------------------------------------------------------------------------
// Parameters:
// a, b : Attribute arrays to be compared.
//
// Returns : cosine similarity estimate for the pair (a, b)
// For Adjusted Cosine Similarity, used adjusted() and then pass to cossim().
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Run from START to END:
// START
function cosSim(a, b) {
return math.dot(a, b) / (math.norm(a) * math.norm(b));
}
// END