Skip to content

Instantly share code, notes, and snippets.

@muhwyndhamhp
Created July 13, 2022 08:03
Show Gist options
  • Save muhwyndhamhp/b4224afdd5ac4040213e3891597f5bc2 to your computer and use it in GitHub Desktop.
Save muhwyndhamhp/b4224afdd5ac4040213e3891597f5bc2 to your computer and use it in GitHub Desktop.
private fun l2Norm(x1: FloatArray, x2: FloatArray): Float {
var sum = 0.0f
val mag1 = sqrt(x1.map { xi -> xi.pow(2) }.sum())
val mag2 = sqrt(x2.map { xi -> xi.pow(2) }.sum())
for (i in x1.indices) {
sum += ((x1[i] / mag1) - (x2[i] / mag2)).pow(2)
}
return sqrt(sum)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment