When implementing collaborative filtering algorithms in a project, I couldn’t find any decent implementations of the Euclidean Distance algorithm.
For those unfamiliar:
In Cartesian coordinates, if x = (x1, x2,…, xn) and y = (y1, y2,…, yn) are two points in Euclidean n-space, then the distance from x to y, or from y to x is given by:
So I decided to be that reference I was looking for earlier. Here is the implementation in Clojure:
(defn square [x] (* x x)) (defn euclidean-distance [x y] (Math/sqrt (->> (map - y x) (map square) (reduce +)))) (/ 1 (+ 1 (euclidean-distance [0 0 0 0 0] [10 10 10 1 1])))