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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-| Module : Graph.Tools.Random
Description :
Copyright : (c) CNRS, Alexandre Delanoë
License : AGPL + CECILL v3
Maintainer : alexandre+dev@delanoe.org
Stability : experimental
Portability : POSIX
-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Graph.Tools.Random where
import Data.Csv
import Data.Text (pack, splitOn, unpack)
import Data.Vector hiding (map, uniq)
import Prelude (read)
import Protolude
import Graph.Types
import qualified Data.Graph.Inductive.PatriciaTree as DGIP
import qualified Data.Set as Set
import qualified Data.Vector as Vector
import qualified Data.Matrix.Sparse.Static as SMatrix
import qualified Numeric.LinearAlgebra.Static as DMatrix
-- Random Matrix
-- | Random Matrix && Graph
-- TODO random matrix of any size for the tests
randomMatrix :: KnownNat n => IO (MatrixD n)
randomMatrix = DMatrix.rand
{-
matrix2graph :: forall n
. (KnownNat n)
=> MatrixS n
-> FiniteGraph n () ()
matrix2graph m = withG (mkGraphUfromEdges $ map (\(x,y,_) -> (x,y)) $ SMatrix.toList m)
identity
-}
randomAdjacency :: KnownNat n => IO (MatrixS n)
randomAdjacency = do
m1 <- randomMatrix
m2 <- randomMatrix
pure $ SMatrix.sparsify (\(i, j) v -> if i < j && v > 0.9 then Just 1 else Nothing)
$ DMatrix.mul m1 m2
-- pure $ SMatrix.fromMatrix
-- $ DMatrix.imapL (\(i, j) v -> if i < j && v > 0.9 then 1 else 0)
-- $ DMatrix.mul m1 m2