Commit 8f4d901a authored by Alfredo Di Napoli's avatar Alfredo Di Napoli Committed by Alfredo Di Napoli

Introduce the Gargantext.Core.LinearAlgebra module

The main idea is trying to refactor/improve the existing linear algebra
functions one function at the time, using reference implementations and
benchmarks along the way.
parent 945fd8d0
......@@ -204,6 +204,7 @@ library
Gargantext.Core.Notifications.Dispatcher.Types
Gargantext.Core.Notifications.Dispatcher.WebSocket
Gargantext.Core.Notifications.Nanomsg
Gargantext.Core.LinearAlgebra
Gargantext.Core.Text
Gargantext.Core.Text.Context
Gargantext.Core.Text.Corpus.API
......@@ -795,6 +796,7 @@ test-suite garg-test-tasty
Test.API.Setup
Test.API.Prelude
Test.API.UpdateList
Test.Core.LinearAlgebra
Test.Core.Notifications
Test.Core.Orchestrator
Test.Core.Similarity
......
{-# LANGUAGE DerivingStrategies #-}
{-|
Module : Gargantext.Core.LinearAlgebra
Description : Linear Algebra utility functions
Copyright : (c) CNRS, 2017-Present
License : AGPL + CECILL v3
Maintainer : team@gargantext.org
Stability : experimental
Portability : POSIX
Linear algebra utility functions to be used across all the Gargantext modules requiring it.
-}
module Gargantext.Core.LinearAlgebra (
-- * Types
Index(..)
-- * Functions
, createIndices
) where
import Prelude
import Data.Map.Strict qualified as M
import Data.Set qualified as S
import Data.Set (Set)
import Data.Map.Strict (Map)
import Data.Foldable (foldl')
newtype Index = Index { _Index :: Int }
deriving newtype (Eq, Show, Ord, Num, Enum)
createIndices :: Ord t => Map (t, t) b -> (Map t Index, Map Index t)
createIndices = set2indices . map2set
where
map2set :: Ord t => Map (t, t) a -> Set t
map2set cs' = foldl' (\s ((t1,t2),_) -> insert [t1,t2] s ) S.empty (M.toList cs')
where
insert as s = foldl' (\s' t -> S.insert t s') s as
set2indices :: Ord t => Set t -> (Map t Index, Map Index t)
set2indices s = (M.fromList toIndex', M.fromList fromIndex')
where
fromIndex' = zip [0..] xs
toIndex' = zip xs [0..]
xs = S.toList s
......@@ -20,6 +20,7 @@ TODO:
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
module Gargantext.Core.Viz.Graph.Index
where
......
{-# LANGUAGE TypeApplications #-}
module Test.Core.LinearAlgebra where
import Prelude
import Test.Tasty.QuickCheck
import Gargantext.Core.LinearAlgebra qualified as LA
import Test.Tasty
import Gargantext.Core.Viz.Graph.Index qualified as Legacy
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as M
compareImplementations :: (Arbitrary a, Eq b, Show b)
=> (a -> b)
-> (a -> c)
-> (c -> b)
-> a
-> Property
compareImplementations implementation1 implementation2 mapResults inputData
= implementation1 inputData === mapResults (implementation2 inputData)
mapCreateIndices :: (Map t Legacy.Index, Map Legacy.Index t) -> (Map t LA.Index, Map LA.Index t)
mapCreateIndices (m1, m2) = (M.map LA.Index m1, M.mapKeys LA.Index m2)
tests :: TestTree
tests = testGroup "LinearAlgebra" [
testProperty "createIndices roundtrip" (compareImplementations (LA.createIndices @Int @Int) Legacy.createIndices mapCreateIndices)
]
......@@ -30,6 +30,9 @@ import qualified Test.Offline.Phylo as Phylo
import qualified Test.Offline.Stemming.Lancaster as Lancaster
import qualified Test.Parsers.Date as PD
import qualified Test.Utils.Crypto as Crypto
import qualified Test.Core.LinearAlgebra as LinearAlgebra
import qualified Test.Core.Notifications as Notifications
import qualified Test.Core.Similarity as Similarity
import qualified Test.Utils.Jobs as Jobs
import System.IO (hGetBuffering, hSetBuffering)
......@@ -78,4 +81,5 @@ main = do
, Notifications.qcTests
, Orchestrator.qcTests
, NgramsTerms.tests
, LinearAlgebra.tests
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment