Tuesday, July 14, 2009

Euclidean Distance

So, I'm packing things up and got to thinking about my copy of "Programming Collective Intelligence" from which I've been making notes on a port of the code to Ruby from Python as a way of building my Python-Fu.

But, I confirmed something that Rich Hickey talks about in his Clojure presentations. That "thing" is that the increase in power one receives from every expression being evaluated is far superior to most other languages vs. a Lisp. (Ex Clojure, Kawa, PocketScheme, PLT, CLISP etc)

He calls it "ceremony" and talks a bit about how Clojure removes 90% of the nonsense that goes on when building a Java program by abstracting it all away. (I mean, realisitically if Groovy can get away with being dynamically typed, what's the deal with Java remaining static? How 20th century can one be?)

Anywho - I had a few minutes break in the morning while my daughter was trying to take her mornig nap (all sounds must cease in the universe for her to find her "happy place") and so I huddled up in the living room with my PocketPC and a copy of PCI went to work.

The first example in the book is how to classify various data as a relation to the other. The example given is of the user trying to find how close two critics are (which inferentially speaking could mean the user would provide ratings for a set of movies and then the system would match them up with a critic they would agree with but, I digress) using the Euclidean Distance.

For the sake of not being viewed as a copyright infringer, I won't quote the book but, you can look at the Python code on page 10 where the formula is presented as an interaction with the Python REPL.

Here's my translation of that into Scheme:


; square function
(define (square n)
(expt n 2))

; euclidean distance
(define (euc-distance x y x1 y1)
(/ 1
(+
(sqrt (+ (square (- x x1)
(square (- y y1)))) 1)))

Essentially one takes the 1/(square root of the (sum of the squares of the (distances between vertices)) + 1)

Which is pretty much how I wrote this function - by working inside out.

It was a nice way to spend about twenty minutes.

Now what's interesting to me is that while my formula is only applicable to two dimensions - one could easily abstract it to be applicable for n dimensions in alignment with the actual theorem.


\sqrt{(p_1-q_1)^2 + (p_2-q_2)^2+...+(p_n-q_n)^2}.

Meaning one would need to iterate over the whole set of differences between the two items along their dimensions.

For someone coming from a liberal arts background - all this n dimension stuff is exciting.

No comments:

Post a Comment