forked from johnmyleswhite/SimilarityMetrics.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosine.jl
47 lines (42 loc) · 987 Bytes
/
cosine.jl
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
35
36
37
38
39
40
41
42
43
44
45
46
47
function cosine(a::AbstractSet, b::AbstractSet)
sA, sB, sI = 0, length(b), 0
for el in a
sA += 1
if contains(b, el)
sI += 1
end
end
return sI / sqrt(sA * sB)
end
function cosine(a::AbstractVector, b::AbstractVector)
sA, sB, sI = 0.0, 0.0, 0.0
for i in 1:length(a)
sA += a[i]^2
sI += a[i] * b[i]
end
for i in 1:length(b)
sB += b[i]^2
end
return sI / sqrt(sA * sB)
end
function norm_sparse(v::AbstractSparseMatrix)
non_0_vals = nonzeros(v)
m = length(non_0_vals)
ret = zero(eltype(v))
for i in 1:m
ret += non_0_vals[i]^2
end
ret
end
rowvals(S::SparseMatrixCSC) = S.rowval
function dot_sparse(v::AbstractSparseMatrix,w::AbstractSparseMatrix)
non_0_idx = intersect(rowvals(w), rowvals(v))
ret = zero(eltype(v))
for i in non_0_idx
ret += v[i] * w[i]
end
ret
end
function cosine_sparse(i::AbstractSparseMatrix, j::AbstractSparseMatrix)
return dot_sparse(i,j)/sqrt(norm_sparse(i)*norm_sparse(j))
end