-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmeans.hs
29 lines (22 loc) · 916 Bytes
/
kmeans.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Kmeans where
import Control.Monad.Par
data Point = Point {
x :: Double,
y :: Double
}
square :: Double -> Double
square a = a * a
distance :: Point -> Point -> Double
distance p q = square (x p - x q) + square (y p - y q)
closest :: [Double] -> Int
closest xs = snd . minimum $ zip xs [0..length xs]
assign :: [Point] -> [Point] -> [Int]
assign means = map (\p -> closest (map (`distance` p) means))
passign :: [Point] -> [Point] -> Int -> Par [Int]
passign means ps chunksize | length ps <= chunksize = return $ assign means ps
| otherwise = do
let (part, rest) = splitAt chunksize ps
ipart <- spawnP (assign means part)
crest <- passign means rest chunksize
cpart <- get ipart
return $ cpart ++ crest