Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
haskell-gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
148
Issues
148
List
Board
Labels
Milestones
Merge Requests
11
Merge Requests
11
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gargantext
haskell-gargantext
Commits
99a93da5
Commit
99a93da5
authored
Oct 26, 2020
by
Alexandre Delanoë
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Clean] Core.Methods.Matrix.Accelerate.Utils created
parent
3b7f4b9f
Pipeline
#1166
canceled with stage
Changes
4
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
364 additions
and
303 deletions
+364
-303
Matrice.hs
src/Gargantext/Core/Methods/Distances/Matrice.hs
+13
-301
Utils.hs
src/Gargantext/Core/Methods/Matrix/Accelerate/Utils.hs
+349
-0
Prelude.hs
src/Gargantext/Database/Admin/Types/Hyperdata/Prelude.hs
+1
-1
Prelude.hs
src/Gargantext/Database/Schema/Prelude.hs
+1
-1
No files found.
src/Gargantext/Core/Methods/Distances/Matrice.hs
View file @
99a93da5
...
@@ -10,15 +10,8 @@ Portability : POSIX
...
@@ -10,15 +10,8 @@ Portability : POSIX
This module aims at implementig distances of terms context by context is
This module aims at implementig distances of terms context by context is
the same referential of corpus.
the same referential of corpus.
Implementation use Accelerate library which enables GPU and CPU computation:
Implementation use Accelerate library which enables GPU and CPU computation
See Gargantext.Core.Methods.Graph.Accelerate)
* Manuel M. T. Chakravarty, Gabriele Keller, Sean Lee, Trevor L. McDonell, and Vinod Grover.
[Accelerating Haskell Array Codes with Multicore GPUs][CKLM+11].
In _DAMP '11: Declarative Aspects of Multicore Programming_, ACM, 2011.
* Trevor L. McDonell, Manuel M. T. Chakravarty, Vinod Grover, and Ryan R. Newton.
[Type-safe Runtime Code Generation: Accelerate to LLVM][MCGN15].
In _Haskell '15: The 8th ACM SIGPLAN Symposium on Haskell_, ACM, 2015.
-}
-}
...
@@ -30,140 +23,14 @@ Implementation use Accelerate library which enables GPU and CPU computation:
...
@@ -30,140 +23,14 @@ Implementation use Accelerate library which enables GPU and CPU computation:
module
Gargantext.Core.Methods.Distances.Matrice
module
Gargantext.Core.Methods.Distances.Matrice
where
where
import
qualified
Data.Foldable
as
P
(
foldl1
)
--
import qualified Data.Foldable as P (foldl1)
import
Debug.Trace
(
trace
)
--
import Debug.Trace (trace)
import
Data.Array.Accelerate
import
Data.Array.Accelerate
import
Data.Array.Accelerate.Interpreter
(
run
)
import
Data.Array.Accelerate.Interpreter
(
run
)
import
Gargantext.Core.Methods.Matrix.Accelerate.Utils
import
qualified
Gargantext.Prelude
as
P
import
qualified
Gargantext.Prelude
as
P
-----------------------------------------------------------------------
-- | Define a vector
--
-- >>> vector 3
-- Vector (Z :. 3) [0,1,2]
vector
::
Elt
c
=>
Int
->
[
c
]
->
(
Array
(
Z
:.
Int
)
c
)
vector
n
l
=
fromList
(
Z
:.
n
)
l
-- | Define a matrix
--
-- >>> matrix 3 ([1..] :: [Double])
-- Matrix (Z :. 3 :. 3)
-- [ 1.0, 2.0, 3.0,
-- 4.0, 5.0, 6.0,
-- 7.0, 8.0, 9.0]
matrix
::
Elt
c
=>
Int
->
[
c
]
->
Matrix
c
matrix
n
l
=
fromList
(
Z
:.
n
:.
n
)
l
-- | Two ways to get the rank (as documentation)
--
-- >>> rank (matrix 3 ([1..] :: [Int]))
-- 2
rank
::
(
Matrix
a
)
->
Int
rank
m
=
arrayRank
$
arrayShape
m
-----------------------------------------------------------------------
-- | Dimension of a square Matrix
-- How to force use with SquareMatrix ?
type
Dim
=
Int
-- | Get Dimension of a square Matrix
--
-- >>> dim (matrix 3 ([1..] :: [Int]))
-- 3
dim
::
Matrix
a
->
Dim
dim
m
=
n
where
Z
:.
_
:.
n
=
arrayShape
m
-- indexTail (arrayShape m)
-----------------------------------------------------------------------
-- TODO move to Utils
runExp
::
Elt
e
=>
Exp
e
->
e
runExp
e
=
indexArray
(
run
(
unit
e
))
Z
-----------------------------------------------------------------------
-- | Sum of a Matrix by Column
--
-- >>> run $ matSumCol 3 (use $ matrix 3 [1..])
-- Matrix (Z :. 3 :. 3)
-- [ 12.0, 15.0, 18.0,
-- 12.0, 15.0, 18.0,
-- 12.0, 15.0, 18.0]
matSumCol
::
Dim
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
matSumCol
r
mat
=
replicate
(
constant
(
Z
:.
(
r
::
Int
)
:.
All
))
$
sum
$
transpose
mat
matSumCol'
::
Matrix
Double
->
Matrix
Double
matSumCol'
m
=
run
$
matSumCol
n
m'
where
n
=
dim
m
m'
=
use
m
-- | Proba computes de probability matrix: all cells divided by thee sum of its column
-- if you need get the probability on the lines, just transpose it
--
-- >>> run $ matProba 3 (use $ matrix 3 [1..])
-- Matrix (Z :. 3 :. 3)
-- [ 8.333333333333333e-2, 0.13333333333333333, 0.16666666666666666,
-- 0.3333333333333333, 0.3333333333333333, 0.3333333333333333,
-- 0.5833333333333334, 0.5333333333333333, 0.5]
matProba
::
Dim
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
matProba
r
mat
=
zipWith
(
/
)
mat
(
matSumCol
r
mat
)
-- | Diagonal of the matrix
--
-- >>> run $ diag (use $ matrix 3 ([1..] :: [Int]))
-- Vector (Z :. 3) [1,5,9]
diag
::
Elt
e
=>
Acc
(
Matrix
e
)
->
Acc
(
Vector
e
)
diag
m
=
backpermute
(
indexTail
(
shape
m
))
(
lift1
(
\
(
Z
:.
x
)
->
(
Z
:.
x
:.
(
x
::
Exp
Int
))))
m
-- | Divide by the Diagonal of the matrix
--
-- >>> run $ divByDiag 3 (use $ matrix 3 ([1..] :: [Double]))
-- Matrix (Z :. 3 :. 3)
-- [ 1.0, 0.4, 0.3333333333333333,
-- 4.0, 1.0, 0.6666666666666666,
-- 7.0, 1.6, 1.0]
divByDiag
::
Dim
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
divByDiag
d
mat
=
zipWith
(
/
)
mat
(
replicate
(
constant
(
Z
:.
(
d
::
Int
)
:.
All
))
$
diag
mat
)
-----------------------------------------------------------------------
-- | Filters the matrix with the minimum of maximums
--
-- >>> run $ matMiniMax $ use $ matrix 3 [1..]
-- Matrix (Z :. 3 :. 3)
-- [ 0.0, 4.0, 7.0,
-- 0.0, 5.0, 8.0,
-- 0.0, 6.0, 9.0]
matMiniMax
::
(
Elt
a
,
Ord
a
,
P
.
Num
a
)
=>
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
matMiniMax
m
=
filterWith'
miniMax'
(
constant
0
)
m
where
miniMax'
=
the
$
minimum
$
maximum
m
-- | Filters the matrix with a constant
--
-- >>> run $ matFilter 5 $ use $ matrix 3 [1..]
-- Matrix (Z :. 3 :. 3)
-- [ 0.0, 0.0, 7.0,
-- 0.0, 0.0, 8.0,
-- 0.0, 6.0, 9.0]
filter'
::
Double
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
filter'
t
m
=
filterWith
t
0
m
filterWith
::
Double
->
Double
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
filterWith
t
v
m
=
map
(
\
x
->
ifThenElse
(
x
>
(
constant
t
))
x
(
constant
v
))
(
transpose
m
)
filterWith'
::
(
Elt
a
,
Ord
a
)
=>
Exp
a
->
Exp
a
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
filterWith'
t
v
m
=
map
(
\
x
->
ifThenElse
(
x
>
t
)
x
v
)
m
-----------------------------------------------------------------------
-- * Metrics of proximity
-- * Metrics of proximity
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-- ** Conditional distance
-- ** Conditional distance
...
@@ -281,50 +148,6 @@ distributional m = -- run {- $ matMiniMax -}
...
@@ -281,50 +148,6 @@ distributional m = -- run {- $ matMiniMax -}
n
::
Dim
n
::
Dim
n
=
dim
m
n
=
dim
m
-- run $ (identityMatrix (DAA.constant (10::Int)) :: DAA.Acc (DAA.Matrix Int)) Matrix (Z :. 10 :. 10)
identityMatrix
::
Num
a
=>
Exp
Int
->
Acc
(
Matrix
a
)
identityMatrix
n
=
let
zeros
=
fill
(
index2
n
n
)
0
ones
=
fill
(
index1
n
)
1
in
permute
const
zeros
(
\
(
unindex1
->
i
)
->
index2
i
i
)
ones
eyeMatrix
::
Num
a
=>
Dim
->
Acc
(
Matrix
a
)
eyeMatrix
n'
=
let
ones
=
fill
(
index2
n
n
)
1
zeros
=
fill
(
index1
n
)
0
n
=
constant
n'
in
permute
const
ones
(
\
(
unindex1
->
i
)
->
index2
i
i
)
zeros
-- | TODO use Lenses
data
Direction
=
MatCol
(
Exp
Int
)
|
MatRow
(
Exp
Int
)
|
Diag
nullOf
::
Num
a
=>
Dim
->
Direction
->
Acc
(
Matrix
a
)
nullOf
n'
dir
=
let
ones
=
fill
(
index2
n
n
)
1
zeros
=
fill
(
index2
n
n
)
0
n
=
constant
n'
in
permute
const
ones
(
lift1
(
\
(
Z
:.
(
i
::
Exp
Int
)
:.
(
_j
::
Exp
Int
))
->
case
dir
of
MatCol
m
->
(
Z
:.
i
:.
m
)
MatRow
m
->
(
Z
:.
m
:.
i
)
Diag
->
(
Z
:.
i
:.
i
)
)
)
zeros
nullOfWithDiag
::
Num
a
=>
Dim
->
Direction
->
Acc
(
Matrix
a
)
nullOfWithDiag
n
dir
=
zipWith
(
*
)
(
nullOf
n
dir
)
(
nullOf
n
Diag
)
rIJ'
::
Matrix
Int
->
Matrix
Double
rIJ'
m
=
run
$
sumRowMin
(
dim
m
)
m'
where
m'
=
(
map
fromIntegral
$
use
m
)
rIJ
::
(
Elt
a
,
Ord
a
,
P
.
Fractional
(
Exp
a
),
P
.
Num
a
)
rIJ
::
(
Elt
a
,
Ord
a
,
P
.
Fractional
(
Exp
a
),
P
.
Num
a
)
=>
Dim
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
=>
Dim
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
rIJ
n
m
=
matMiniMax
$
divide
a
b
rIJ
n
m
=
matMiniMax
$
divide
a
b
...
@@ -332,102 +155,6 @@ rIJ n m = matMiniMax $ divide a b
...
@@ -332,102 +155,6 @@ rIJ n m = matMiniMax $ divide a b
a
=
sumRowMin
n
m
a
=
sumRowMin
n
m
b
=
sumColMin
n
m
b
=
sumColMin
n
m
divide
::
(
Elt
a
,
Ord
a
,
P
.
Fractional
(
Exp
a
),
P
.
Num
a
)
=>
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
divide
=
zipWith
divide'
where
divide'
a
b
=
ifThenElse
(
b
>
(
constant
0
))
(
a
/
b
)
(
constant
0
)
-- | Nominator
sumRowMin
::
(
Num
a
,
Ord
a
)
=>
Dim
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
sumRowMin
n
m
=
{-trace (P.show $ run m') $-}
m'
where
m'
=
reshape
(
shape
m
)
vs
vs
=
P
.
foldl1
(
++
)
$
P
.
map
(
\
z
->
sumRowMin1
n
(
constant
z
)
m
)
[
0
..
n
-
1
]
sumRowMin1
::
(
Num
a
,
Ord
a
)
=>
Dim
->
Exp
Int
->
Acc
(
Matrix
a
)
->
Acc
(
Vector
a
)
sumRowMin1
n
x
m
=
trace
(
P
.
show
(
run
m
,
run
$
transpose
m
))
$
m''
where
m''
=
sum
$
zipWith
min
(
transpose
m
)
m
_m'
=
zipWith
(
*
)
(
zipWith
(
*
)
(
nullOf
n
(
MatCol
x
))
$
nullOfWithDiag
n
(
MatRow
x
))
m
-- | Denominator
sumColMin
::
(
Num
a
,
Ord
a
)
=>
Dim
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
sumColMin
n
m
=
reshape
(
shape
m
)
vs
where
vs
=
P
.
foldl1
(
++
)
$
P
.
map
(
\
z
->
sumColMin1
n
(
constant
z
)
m
)
[
0
..
n
-
1
]
sumColMin1
::
(
Num
a
)
=>
Dim
->
Exp
Int
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
sumColMin1
n
x
m
=
zipWith
(
*
)
(
nullOfWithDiag
n
(
MatCol
x
))
m
{- | WIP fun with indexes
selfMatrix :: Num a => Dim -> Acc (Matrix a)
selfMatrix n' =
let zeros = fill (index2 n n) 0
ones = fill (index2 n n) 1
n = constant n'
in
permute const ones ( lift1 ( \(Z :. (i :: Exp Int) :. (_j:: Exp Int))
-> -- ifThenElse (i /= j)
-- (Z :. i :. j)
(Z :. i :. i)
)) zeros
selfMatrix' :: (Elt a, P.Num (Exp a)) => Array DIM2 a -> Matrix a
selfMatrix' m' = run $ selfMatrix n
where
n = dim m'
m = use m'
-}
-------------------------------------------------
diagNull
::
Num
a
=>
Dim
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
diagNull
n
m
=
zipWith
(
*
)
m
eye
where
eye
=
eyeMatrix
n
-------------------------------------------------
crossProduct
::
Dim
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
crossProduct
n
m
=
{-trace (P.show (run m',run m'')) $-}
zipWith
(
*
)
m'
m''
where
m'
=
cross
n
m
m''
=
transpose
$
cross
n
m
crossT
::
Matrix
Double
->
Matrix
Double
crossT
=
run
.
transpose
.
use
crossProduct'
::
Matrix
Double
->
Matrix
Double
crossProduct'
m
=
run
$
crossProduct
n
m'
where
n
=
dim
m
m'
=
use
m
runWith
::
(
Arrays
c
,
Elt
a1
)
=>
(
Dim
->
Acc
(
Matrix
a1
)
->
a2
->
Acc
c
)
->
Matrix
a1
->
a2
->
c
runWith
f
m
=
run
.
f
(
dim
m
)
(
use
m
)
-- | cross
cross
::
Dim
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
cross
n
mat
=
diagNull
n
(
matSumCol
n
$
diagNull
n
mat
)
cross'
::
Matrix
Double
->
Matrix
Double
cross'
mat
=
run
$
cross
n
mat'
where
mat'
=
use
mat
n
=
dim
mat
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-- * Specificity and Genericity
-- * Specificity and Genericity
...
@@ -458,7 +185,10 @@ type SymetricMatrix = Matrix
...
@@ -458,7 +185,10 @@ type SymetricMatrix = Matrix
type
NonSymetricMatrix
=
Matrix
type
NonSymetricMatrix
=
Matrix
incExcSpeGen
::
Matrix
Int
->
(
Vector
GenericityInclusion
,
Vector
SpecificityExclusion
)
incExcSpeGen
::
Matrix
Int
->
(
Vector
GenericityInclusion
,
Vector
SpecificityExclusion
)
incExcSpeGen
m
=
(
run'
inclusionExclusion
m
,
run'
specificityGenericity
m
)
incExcSpeGen
m
=
(
run'
inclusionExclusion
m
,
run'
specificityGenericity
m
)
where
where
run'
fun
mat
=
run
$
fun
$
map
fromIntegral
$
use
mat
run'
fun
mat
=
run
$
fun
$
map
fromIntegral
$
use
mat
...
@@ -497,7 +227,9 @@ p_ij m = zipWith (/) m (n_jj m)
...
@@ -497,7 +227,9 @@ p_ij m = zipWith (/) m (n_jj m)
-- | P(j|i) = Nij /N(ii) Probability to get i given j
-- | P(j|i) = Nij /N(ii) Probability to get i given j
-- to test
-- to test
p_ji
::
(
Elt
e
,
P
.
Fractional
(
Exp
e
))
=>
Acc
(
Array
DIM2
e
)
->
Acc
(
Array
DIM2
e
)
p_ji
::
(
Elt
e
,
P
.
Fractional
(
Exp
e
))
=>
Acc
(
Array
DIM2
e
)
->
Acc
(
Array
DIM2
e
)
p_ji
=
transpose
.
p_ij
p_ji
=
transpose
.
p_ij
...
@@ -529,24 +261,6 @@ p_ m = zipWith (/) m (n_ m)
...
@@ -529,24 +261,6 @@ p_ m = zipWith (/) m (n_ m)
distriTest
::
Int
->
Matrix
Double
distriTest
::
Int
->
Matrix
Double
distriTest
n
=
distributional
(
theMatrix
n
)
distriTest
n
=
distributional
(
theMatrix
n
)
theMatrix
::
Int
->
Matrix
Int
theMatrix
n
=
matrix
n
(
dataMatrix
n
)
where
dataMatrix
::
Int
->
[
Int
]
dataMatrix
x
|
(
P
.==
)
x
2
=
[
1
,
1
,
1
,
2
]
|
(
P
.==
)
x
3
=
[
1
,
1
,
2
,
1
,
2
,
3
,
2
,
3
,
4
]
|
(
P
.==
)
x
4
=
[
1
,
1
,
2
,
3
,
1
,
2
,
3
,
4
,
2
,
3
,
4
,
5
,
3
,
4
,
5
,
6
]
|
P
.
otherwise
=
P
.
undefined
{-
{-
theResult :: Int -> Matrix Double
theResult :: Int -> Matrix Double
...
@@ -561,5 +275,3 @@ colMatrix n ns = replicate (constant (Z :. (n :: Int) :. All)) v
...
@@ -561,5 +275,3 @@ colMatrix n ns = replicate (constant (Z :. (n :: Int) :. All)) v
where
where
v
=
use
$
vector
(
P
.
length
ns
)
ns
v
=
use
$
vector
(
P
.
length
ns
)
ns
-----------------------------------------------------------------------
src/Gargantext/Core/Methods/Matrix/Accelerate/Utils.hs
0 → 100644
View file @
99a93da5
{-|
Module : Gargantext.Core.Methods.Matrix.Accelerate.Utils
Description :
Copyright : (c) CNRS, 2017-Present
License : AGPL + CECILL v3
Maintainer : team@gargantext.org
Stability : experimental
Portability : POSIX
This module aims at implementig distances of terms context by context is
the same referential of corpus.
Implementation use Accelerate library which enables GPU and CPU computation:
* Manuel M. T. Chakravarty, Gabriele Keller, Sean Lee, Trevor L. McDonell, and Vinod Grover.
[Accelerating Haskell Array Codes with Multicore GPUs][CKLM+11].
In _DAMP '11: Declarative Aspects of Multicore Programming_, ACM, 2011.
* Trevor L. McDonell, Manuel M. T. Chakravarty, Vinod Grover, and Ryan R. Newton.
[Type-safe Runtime Code Generation: Accelerate to LLVM][MCGN15].
In _Haskell '15: The 8th ACM SIGPLAN Symposium on Haskell_, ACM, 2015.
-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
module
Gargantext.Core.Methods.Matrix.Accelerate.Utils
where
import
qualified
Data.Foldable
as
P
(
foldl1
)
import
Debug.Trace
(
trace
)
import
Data.Array.Accelerate
import
Data.Array.Accelerate.Interpreter
(
run
)
import
qualified
Gargantext.Prelude
as
P
-----------------------------------------------------------------------
runExp
::
Elt
e
=>
Exp
e
->
e
runExp
e
=
indexArray
(
run
(
unit
e
))
Z
-----------------------------------------------------------------------
-- | Define a vector
--
-- >>> vector 3
-- Vector (Z :. 3) [0,1,2]
vector
::
Elt
c
=>
Int
->
[
c
]
->
(
Array
(
Z
:.
Int
)
c
)
vector
n
l
=
fromList
(
Z
:.
n
)
l
-- | Define a matrix
--
-- >>> matrix 3 ([1..] :: [Double])
-- Matrix (Z :. 3 :. 3)
-- [ 1.0, 2.0, 3.0,
-- 4.0, 5.0, 6.0,
-- 7.0, 8.0, 9.0]
matrix
::
Elt
c
=>
Int
->
[
c
]
->
Matrix
c
matrix
n
l
=
fromList
(
Z
:.
n
:.
n
)
l
-- | Two ways to get the rank (as documentation)
--
-- >>> rank (matrix 3 ([1..] :: [Int]))
-- 2
rank
::
(
Matrix
a
)
->
Int
rank
m
=
arrayRank
$
arrayShape
m
-----------------------------------------------------------------------
-- | Dimension of a square Matrix
-- How to force use with SquareMatrix ?
type
Dim
=
Int
-- | Get Dimension of a square Matrix
--
-- >>> dim (matrix 3 ([1..] :: [Int]))
-- 3
dim
::
Matrix
a
->
Dim
dim
m
=
n
where
Z
:.
_
:.
n
=
arrayShape
m
-- indexTail (arrayShape m)
-----------------------------------------------------------------------
-- | Sum of a Matrix by Column
--
-- >>> run $ matSumCol 3 (use $ matrix 3 [1..])
-- Matrix (Z :. 3 :. 3)
-- [ 12.0, 15.0, 18.0,
-- 12.0, 15.0, 18.0,
-- 12.0, 15.0, 18.0]
matSumCol
::
Dim
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
matSumCol
r
mat
=
replicate
(
constant
(
Z
:.
(
r
::
Int
)
:.
All
))
$
sum
$
transpose
mat
matSumCol'
::
Matrix
Double
->
Matrix
Double
matSumCol'
m
=
run
$
matSumCol
n
m'
where
n
=
dim
m
m'
=
use
m
-- | Proba computes de probability matrix: all cells divided by thee sum of its column
-- if you need get the probability on the lines, just transpose it
--
-- >>> run $ matProba 3 (use $ matrix 3 [1..])
-- Matrix (Z :. 3 :. 3)
-- [ 8.333333333333333e-2, 0.13333333333333333, 0.16666666666666666,
-- 0.3333333333333333, 0.3333333333333333, 0.3333333333333333,
-- 0.5833333333333334, 0.5333333333333333, 0.5]
matProba
::
Dim
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
matProba
r
mat
=
zipWith
(
/
)
mat
(
matSumCol
r
mat
)
-- | Diagonal of the matrix
--
-- >>> run $ diag (use $ matrix 3 ([1..] :: [Int]))
-- Vector (Z :. 3) [1,5,9]
diag
::
Elt
e
=>
Acc
(
Matrix
e
)
->
Acc
(
Vector
e
)
diag
m
=
backpermute
(
indexTail
(
shape
m
))
(
lift1
(
\
(
Z
:.
x
)
->
(
Z
:.
x
:.
(
x
::
Exp
Int
))))
m
-- | Divide by the Diagonal of the matrix
--
-- >>> run $ divByDiag 3 (use $ matrix 3 ([1..] :: [Double]))
-- Matrix (Z :. 3 :. 3)
-- [ 1.0, 0.4, 0.3333333333333333,
-- 4.0, 1.0, 0.6666666666666666,
-- 7.0, 1.6, 1.0]
divByDiag
::
Dim
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
divByDiag
d
mat
=
zipWith
(
/
)
mat
(
replicate
(
constant
(
Z
:.
(
d
::
Int
)
:.
All
))
$
diag
mat
)
-----------------------------------------------------------------------
-- | Filters the matrix with the minimum of maximums
--
-- >>> run $ matMiniMax $ use $ matrix 3 [1..]
-- Matrix (Z :. 3 :. 3)
-- [ 0.0, 4.0, 7.0,
-- 0.0, 5.0, 8.0,
-- 0.0, 6.0, 9.0]
matMiniMax
::
(
Elt
a
,
Ord
a
,
P
.
Num
a
)
=>
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
matMiniMax
m
=
filterWith'
miniMax'
(
constant
0
)
m
where
miniMax'
=
the
$
minimum
$
maximum
m
-- | Filters the matrix with a constant
--
-- >>> run $ matFilter 5 $ use $ matrix 3 [1..]
-- Matrix (Z :. 3 :. 3)
-- [ 0.0, 0.0, 7.0,
-- 0.0, 0.0, 8.0,
-- 0.0, 6.0, 9.0]
filter'
::
Double
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
filter'
t
m
=
filterWith
t
0
m
filterWith
::
Double
->
Double
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
filterWith
t
v
m
=
map
(
\
x
->
ifThenElse
(
x
>
(
constant
t
))
x
(
constant
v
))
(
transpose
m
)
filterWith'
::
(
Elt
a
,
Ord
a
)
=>
Exp
a
->
Exp
a
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
filterWith'
t
v
m
=
map
(
\
x
->
ifThenElse
(
x
>
t
)
x
v
)
m
-- run $ (identityMatrix (DAA.constant (10::Int)) :: DAA.Acc (DAA.Matrix Int)) Matrix (Z :. 10 :. 10)
identityMatrix
::
Num
a
=>
Exp
Int
->
Acc
(
Matrix
a
)
identityMatrix
n
=
let
zeros
=
fill
(
index2
n
n
)
0
ones
=
fill
(
index1
n
)
1
in
permute
const
zeros
(
\
(
unindex1
->
i
)
->
index2
i
i
)
ones
eyeMatrix
::
Num
a
=>
Dim
->
Acc
(
Matrix
a
)
eyeMatrix
n'
=
let
ones
=
fill
(
index2
n
n
)
1
zeros
=
fill
(
index1
n
)
0
n
=
constant
n'
in
permute
const
ones
(
\
(
unindex1
->
i
)
->
index2
i
i
)
zeros
-- | TODO use Lenses
data
Direction
=
MatCol
(
Exp
Int
)
|
MatRow
(
Exp
Int
)
|
Diag
nullOf
::
Num
a
=>
Dim
->
Direction
->
Acc
(
Matrix
a
)
nullOf
n'
dir
=
let
ones
=
fill
(
index2
n
n
)
1
zeros
=
fill
(
index2
n
n
)
0
n
=
constant
n'
in
permute
const
ones
(
lift1
(
\
(
Z
:.
(
i
::
Exp
Int
)
:.
(
_j
::
Exp
Int
))
->
case
dir
of
MatCol
m
->
(
Z
:.
i
:.
m
)
MatRow
m
->
(
Z
:.
m
:.
i
)
Diag
->
(
Z
:.
i
:.
i
)
)
)
zeros
nullOfWithDiag
::
Num
a
=>
Dim
->
Direction
->
Acc
(
Matrix
a
)
nullOfWithDiag
n
dir
=
zipWith
(
*
)
(
nullOf
n
dir
)
(
nullOf
n
Diag
)
divide
::
(
Elt
a
,
Ord
a
,
P
.
Fractional
(
Exp
a
),
P
.
Num
a
)
=>
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
divide
=
zipWith
divide'
where
divide'
a
b
=
ifThenElse
(
b
>
(
constant
0
))
(
a
/
b
)
(
constant
0
)
-- | Nominator
sumRowMin
::
(
Num
a
,
Ord
a
)
=>
Dim
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
sumRowMin
n
m
=
{-trace (P.show $ run m') $-}
m'
where
m'
=
reshape
(
shape
m
)
vs
vs
=
P
.
foldl1
(
++
)
$
P
.
map
(
\
z
->
sumRowMin1
n
(
constant
z
)
m
)
[
0
..
n
-
1
]
sumRowMin1
::
(
Num
a
,
Ord
a
)
=>
Dim
->
Exp
Int
->
Acc
(
Matrix
a
)
->
Acc
(
Vector
a
)
sumRowMin1
n
x
m
=
trace
(
P
.
show
(
run
m
,
run
$
transpose
m
))
$
m''
where
m''
=
sum
$
zipWith
min
(
transpose
m
)
m
_m'
=
zipWith
(
*
)
(
zipWith
(
*
)
(
nullOf
n
(
MatCol
x
))
$
nullOfWithDiag
n
(
MatRow
x
))
m
-- | Denominator
sumColMin
::
(
Num
a
,
Ord
a
)
=>
Dim
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
sumColMin
n
m
=
reshape
(
shape
m
)
vs
where
vs
=
P
.
foldl1
(
++
)
$
P
.
map
(
\
z
->
sumColMin1
n
(
constant
z
)
m
)
[
0
..
n
-
1
]
sumColMin1
::
(
Num
a
)
=>
Dim
->
Exp
Int
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
sumColMin1
n
x
m
=
zipWith
(
*
)
(
nullOfWithDiag
n
(
MatCol
x
))
m
{- | WIP fun with indexes
selfMatrix :: Num a => Dim -> Acc (Matrix a)
selfMatrix n' =
let zeros = fill (index2 n n) 0
ones = fill (index2 n n) 1
n = constant n'
in
permute const ones ( lift1 ( \(Z :. (i :: Exp Int) :. (_j:: Exp Int))
-> -- ifThenElse (i /= j)
-- (Z :. i :. j)
(Z :. i :. i)
)) zeros
selfMatrix' :: (Elt a, P.Num (Exp a)) => Array DIM2 a -> Matrix a
selfMatrix' m' = run $ selfMatrix n
where
n = dim m'
m = use m'
-}
-------------------------------------------------
diagNull
::
Num
a
=>
Dim
->
Acc
(
Matrix
a
)
->
Acc
(
Matrix
a
)
diagNull
n
m
=
zipWith
(
*
)
m
eye
where
eye
=
eyeMatrix
n
-------------------------------------------------
crossProduct
::
Dim
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
crossProduct
n
m
=
{-trace (P.show (run m',run m'')) $-}
zipWith
(
*
)
m'
m''
where
m'
=
cross
n
m
m''
=
transpose
$
cross
n
m
crossT
::
Matrix
Double
->
Matrix
Double
crossT
=
run
.
transpose
.
use
crossProduct'
::
Matrix
Double
->
Matrix
Double
crossProduct'
m
=
run
$
crossProduct
n
m'
where
n
=
dim
m
m'
=
use
m
runWith
::
(
Arrays
c
,
Elt
a1
)
=>
(
Dim
->
Acc
(
Matrix
a1
)
->
a2
->
Acc
c
)
->
Matrix
a1
->
a2
->
c
runWith
f
m
=
run
.
f
(
dim
m
)
(
use
m
)
-- | cross
cross
::
Dim
->
Acc
(
Matrix
Double
)
->
Acc
(
Matrix
Double
)
cross
n
mat
=
diagNull
n
(
matSumCol
n
$
diagNull
n
mat
)
cross'
::
Matrix
Double
->
Matrix
Double
cross'
mat
=
run
$
cross
n
mat'
where
mat'
=
use
mat
n
=
dim
mat
{-
-- | Hypothesis to test maybe later (or not)
-- TODO ask accelerate for instances to ease such writtings:
p_ :: (Elt e, P.Fractional (Exp e)) => Acc (Array DIM2 e) -> Acc (Array DIM2 e)
p_ m = zipWith (/) m (n_ m)
where
n_ :: Elt e => Acc (SymetricMatrix e) -> Acc (Matrix e)
n_ m = backpermute (shape m)
(lift1 ( \(Z :. (i :: Exp Int) :. (j:: Exp Int))
-> (ifThenElse (i < j) (lift (Z :. j :. j)) (lift (Z :. i :. i)) :: Exp DIM2)
)
) m
-}
theMatrix
::
Int
->
Matrix
Int
theMatrix
n
=
matrix
n
(
dataMatrix
n
)
where
dataMatrix
::
Int
->
[
Int
]
dataMatrix
x
|
(
P
.==
)
x
2
=
[
1
,
1
,
1
,
2
]
|
(
P
.==
)
x
3
=
[
1
,
1
,
2
,
1
,
2
,
3
,
2
,
3
,
4
]
|
(
P
.==
)
x
4
=
[
1
,
1
,
2
,
3
,
1
,
2
,
3
,
4
,
2
,
3
,
4
,
5
,
3
,
4
,
5
,
6
]
|
P
.
otherwise
=
P
.
undefined
{-
theResult :: Int -> Matrix Double
theResult n | (P.==) n 2 = let r = 1.6094379124341003 in [ 0, r, r, 0]
| P.otherwise = [ 1, 1 ]
-}
colMatrix
::
Elt
e
=>
Int
->
[
e
]
->
Acc
(
Array
((
Z
:.
Int
)
:.
Int
)
e
)
colMatrix
n
ns
=
replicate
(
constant
(
Z
:.
(
n
::
Int
)
:.
All
))
v
where
v
=
use
$
vector
(
P
.
length
ns
)
ns
-----------------------------------------------------------------------
src/Gargantext/Database/Admin/Types/Hyperdata/Prelude.hs
View file @
99a93da5
...
@@ -50,7 +50,7 @@ import Gargantext.Database.Prelude (fromField')
...
@@ -50,7 +50,7 @@ import Gargantext.Database.Prelude (fromField')
import
Gargantext.Prelude
import
Gargantext.Prelude
import
Opaleye
(
QueryRunnerColumnDefault
,
queryRunnerColumnDefault
,
PGJsonb
,
fieldQueryRunnerColumn
,
Nullable
)
import
Opaleye
(
QueryRunnerColumnDefault
,
queryRunnerColumnDefault
,
PGJsonb
,
fieldQueryRunnerColumn
,
Nullable
)
import
Test.QuickCheck
(
elements
)
import
Test.QuickCheck
(
elements
)
import
Test.QuickCheck.Arbitrary
import
Test.QuickCheck.Arbitrary
hiding
(
vector
)
------------------------------------------------------------------------
------------------------------------------------------------------------
-- Only Hyperdata types should be member of this type class.
-- Only Hyperdata types should be member of this type class.
...
...
src/Gargantext/Database/Schema/Prelude.hs
View file @
99a93da5
...
@@ -39,7 +39,7 @@ import GHC.Generics (Generic)
...
@@ -39,7 +39,7 @@ import GHC.Generics (Generic)
import
Gargantext.Core.Utils.Prefix
(
unPrefix
,
unPrefixSwagger
)
import
Gargantext.Core.Utils.Prefix
(
unPrefix
,
unPrefixSwagger
)
import
Opaleye
hiding
(
FromField
,
readOnly
)
import
Opaleye
hiding
(
FromField
,
readOnly
)
import
Opaleye.Internal.QueryArr
(
Query
)
import
Opaleye.Internal.QueryArr
(
Query
)
import
Test.QuickCheck.Arbitrary
import
Test.QuickCheck.Arbitrary
hiding
(
vector
)
import
Database.PostgreSQL.Simple.FromField
(
FromField
,
fromField
)
import
Database.PostgreSQL.Simple.FromField
(
FromField
,
fromField
)
import
Database.PostgreSQL.Simple.FromRow
(
FromRow
,
fromRow
,
field
)
import
Database.PostgreSQL.Simple.FromRow
(
FromRow
,
fromRow
,
field
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment