Skip to content

Instantly share code, notes, and snippets.

@muhwyndhamhp
Created July 13, 2022 08:03
Show Gist options
  • Save muhwyndhamhp/f28b05cea0840f5c0def3d0e6d8ee5e8 to your computer and use it in GitHub Desktop.
Save muhwyndhamhp/f28b05cea0840f5c0def3d0e6d8ee5e8 to your computer and use it in GitHub Desktop.
private fun cosineSim(x1: FloatArray, x2: FloatArray): Float {
var dotProduct = 0.0f
var normA = 0.0f
var normB = 0.0f
for (i in x1.indices) {
dotProduct += x1[i] * x2[i]
normA += x1[i].pow(2)
normB += x2[i].pow(2)
}
return dotProduct / (sqrt(normA) * sqrt(normB))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment