2 Module : Gargantext.Core.Methods.Distances
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Motivation and definition of the @Conditional@ distance.
13 {-# LANGUAGE BangPatterns #-}
14 {-# LANGUAGE Strict #-}
15 module Gargantext.Core.Methods.Distances.Conditional
18 import Control.Parallel.Strategies (parList, rdeepseq, using)
19 import Data.Hashable (Hashable)
20 import Data.List (unzip)
21 import Data.Maybe (catMaybes)
22 import Gargantext.Prelude
23 import qualified Data.HashMap.Strict as Map
24 import qualified Data.Set as Set
25 import Control.DeepSeq (NFData)
26 type HashMap = Map.HashMap
28 ------------------------------------------------------------------------
29 -- First version as first implementation
30 -- - qualitatively verified
31 -- - parallized as main optimization
32 conditional :: (Ord a, Hashable a, NFData a)
34 -> HashMap (a,a) Double
35 conditional m' = Map.fromList $ ((catMaybes results') `using` parList rdeepseq)
38 ij = (/) <$> Map.lookup (i,j) m <*> Map.lookup (i,i) m
39 ji = (/) <$> Map.lookup (j,i) m <*> Map.lookup (j,j) m
46 -- Converting from Int to Double
47 m = Map.map fromIntegral m'
49 -- Get the matrix coordinates, removing duplicates
50 keys = Set.toList $ Set.fromList (x <> y)
51 (x,y) = unzip $ Map.keys m
57 -> Maybe ((a,a), Double)
58 getMax (i,j) (Just d) Nothing = Just ((i,j), d)
59 getMax (i,j) Nothing (Just d) = Just ((j,i), d)
60 getMax ij (Just di) (Just dj) = if di >= dj then getMax ij (Just di) Nothing
61 else getMax ij Nothing (Just dj)
62 getMax _ _ _ = Nothing