Skip to content

Instantly share code, notes, and snippets.

@Voker57
Created April 28, 2009 20:24
Show Gist options
  • Save Voker57/103379 to your computer and use it in GitHub Desktop.
Save Voker57/103379 to your computer and use it in GitHub Desktop.
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) )
This file has been truncated, but you can view the full file.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment