]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Viz/Graph/Utils.hs
Merge branch 'dev' into dev-doc-table-optimization
[gargantext.git] / src / Gargantext / Viz / Graph / Utils.hs
1 {-|
2 Module : Gargantext.Viz.Graph.Utils
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 These functions are used for Vector.Matrix only.
11
12 -}
13
14 {-# LANGUAGE BangPatterns #-}
15 {-# LANGUAGE Strict #-}
16
17 module Gargantext.Viz.Graph.Utils
18 where
19
20 import Data.Matrix hiding (identity)
21
22 import Data.Vector (Vector)
23 import qualified Data.Vector as V
24
25 import qualified Data.List as L
26 import Gargantext.Prelude
27
28 ------------------------------------------------------------------------
29 -- | Some utils to build the matrix from cooccurrence results
30
31 -- | For tests only, to be removed
32 -- m1 :: Matrix Double
33 -- m1 = fromList 300 300 [1..]
34 ------------------------------------------------------------------------
35 ------------------------------------------------------------------------
36 data Axis = Col | Row
37 ------------------------------------------------------------------------
38 -- | Matrix functions
39 type AxisId = Int
40
41 -- Data.Vector.Additions
42 dropAt :: Int -> Vector a -> Vector a
43 dropAt n v = debut <> (V.tail fin)
44 where
45 debut = V.take n v
46 fin = V.drop n v
47
48 total :: Num a => Matrix a -> a
49 total m = V.sum $ V.map (\c -> V.sum (getCol c m)) (V.enumFromTo 1 (nOf Col m))
50
51 nOf :: Axis -> Matrix a -> Int
52 nOf Row = nrows
53 nOf Col = ncols
54
55 axis :: Axis -> AxisId -> Matrix a -> Vector a
56 axis Col = getCol
57 axis Row = getRow
58
59
60 toListsWithIndex :: Matrix a -> [((Int, Int), a)]
61 toListsWithIndex m = concat' $ zip [1..] $ map (\c -> zip [1..] c) $ toLists m
62 where
63 concat' :: [(Int, [(Int, a)])] -> [((Int, Int), a)]
64 concat' xs = L.concat $ map (\(x, ys) -> map (\(y, a) -> ((x,y), a)) ys ) xs
65
66