Created
July 13, 2022 08:03
-
-
Save muhwyndhamhp/b4224afdd5ac4040213e3891597f5bc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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