|
1 | 1 | {-# LANGUAGE TypeSynonymInstances #-} |
2 | | -module SComplex where |
| 2 | +module SComplex (Simplex, SComplex, boundary, empty, insert, fromList, union, faces, star) |
| 3 | +where |
3 | 4 |
|
4 | 5 | import qualified Data.Map.Strict as Map |
5 | 6 | import qualified Data.Set as Set |
@@ -35,23 +36,32 @@ iterateBoundary s = |
35 | 36 | b ++ (>>=) b iterateBoundary |
36 | 37 |
|
37 | 38 | -- A simplical complex is a collection of simplices, plus all of their boundaries |
38 | | -data SimplicalComplex a = SimplicalComplex { parents::Map.Map (Simplex a) (Set.Set (Simplex a)) } |
| 39 | +data SComplex a = SComplex { parents::Map.Map (Simplex a) (Set.Set (Simplex a)) } |
39 | 40 | deriving (Show) |
40 | 41 |
|
41 | | -empty = SimplicalComplex {parents=Map.empty} |
| 42 | +empty = SComplex { parents=Map.empty } |
42 | 43 |
|
43 | | -insert :: (Ord a) => Simplex a -> SimplicalComplex a -> SimplicalComplex a |
| 44 | +insert :: (Ord a) => Simplex a -> SComplex a -> SComplex a |
44 | 45 | insert [] sc = sc |
45 | 46 | insert s sc = |
46 | 47 | let b = boundary s |
47 | | - sc' = foldl (\x y -> insert y x) sc b |
| 48 | + sc' = foldl (flip insert) sc b |
48 | 49 | inserter m k = |
49 | 50 | Map.insertWith Set.union k (Set.singleton s) m |
50 | 51 | in |
51 | 52 | -- sc' has all of the children of s inserted properly. |
52 | 53 | -- Now, let's link up the direct children with s |
53 | | - SimplicalComplex { parents=Map.insert s Set.empty $ foldl inserter (parents sc') b} |
| 54 | + SComplex { parents=Map.insert s Set.empty $ foldl inserter (parents sc') b } |
54 | 55 |
|
55 | | -fromList :: (Ord a) => [Simplex a] -> SimplicalComplex a |
56 | | -fromList = foldl (\x y -> insert y x) $ empty |
| 56 | +fromList :: (Ord a, Foldable t) => t (Simplex a) -> SComplex a |
| 57 | +fromList = foldl (flip insert) empty |
57 | 58 |
|
| 59 | +union :: (Ord a) => SComplex a -> SComplex a -> SComplex a |
| 60 | +union sc1 sc2 = |
| 61 | + foldl (flip insert) sc1 (Map.keys $ parents sc2) |
| 62 | + |
| 63 | +faces :: SComplex a -> Int -> [Simplex a] |
| 64 | +faces sc o = filter ((==) o . length) $ Map.keys . parents $ sc |
| 65 | + |
| 66 | +star :: (Ord a) => SComplex a -> Simplex a -> Maybe [Simplex a] |
| 67 | +star sc s = fmap Set.toList $ Map.lookup s $ parents sc |
0 commit comments