Created
April 28, 2009 20:24
-
-
Save Voker57/103379 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
module Clusterizer (clusterValues, Pair, Cluster, massCenter) where | |
import Data.List | |
type Pair = (Int, Int) | |
type Cluster = [Pair] | |
compareFirst a b = compare (fst a) (fst b) | |
box :: a -> [a] | |
box a = [a] | |
clusterValues :: [Pair] -> [Cluster] | |
clusterValues values = | |
snd (iterateValues values (map (box) ((inits values) !! 2)) ) | |
iterateValues :: [Pair] -> [Cluster] -> ([Pair], [Cluster]) | |
iterateValues [] clusters = ([], clusters) | |
iterateValues values clusters = | |
let mp = map (grabValue values) (clusters); -- [ (Cluster, Pair) ] | |
vs = map (snd) mp; | |
pairs = values \\ vs | |
in iterateValues pairs (map (fst) mp) | |
massCenter :: Cluster -> Pair | |
massCenter [] = (0, 0) | |
massCenter points = | |
( ((sum (map (fst) points)) `div` (length points)), ((sum (map (snd) points)) `div` (length points))) | |
dist :: (Floating f) => Pair -> Pair -> f | |
dist a b = sqrt (realToFrac (sum (map (\x -> x * x) [((fst a) - (fst b)), ((snd a) - (snd b))]))) | |
grabValue :: [Pair] -> Cluster -> (Cluster, Pair) | |
grabValue values cluster = | |
let dists = map (dist (massCenter cluster)) values; | |
distMap = zip dists values; | |
sortedMap = sortBy (compareFirst) distMap; | |
sortedValues = map (snd) sortedMap | |
in ( (cluster ++ (box (head sortedValues))), (head sortedValues) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment