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
145
Issues
145
List
Board
Labels
Milestones
Merge Requests
5
Merge Requests
5
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
35e3d393
Commit
35e3d393
authored
Mar 27, 2025
by
Grégoire Locqueville
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
A bit of cleanup. Also make all imports explicit or qualified
parent
06ca7c6f
Pipeline
#7496
passed with stages
in 47 minutes and 39 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
51 deletions
+53
-51
LinearAlgebra.hs
test/Test/Core/LinearAlgebra.hs
+53
-51
No files found.
test/Test/Core/LinearAlgebra.hs
View file @
35e3d393
...
...
@@ -12,16 +12,17 @@ import Data.Array.Accelerate.Interpreter qualified as Naive
import
Data.Array.Accelerate
qualified
as
A
import
Data.Functor
((
<&>
))
import
Data.Massiv.Array
qualified
as
Massiv
import
Data.Proxy
import
Data.Scientific
import
Data.Proxy
(
Proxy
(
Proxy
))
import
Data.Scientific
(
Scientific
,
fromFloatDigits
)
import
Gargantext.Core.LinearAlgebra
qualified
as
LA
import
Gargantext.Core.Methods.Matrix.Accelerate.Utils
qualified
as
A
import
Gargantext.Core.Methods.Matrix.Accelerate.Utils
qualified
as
Legacy
import
Gargantext.Core.Methods.Similarities.Accelerate.Distributional
qualified
as
Legacy
import
Gargantext.Orphans.Accelerate
(
sliceArray
)
import
Prelude
hiding
((
^
))
import
Test.Tasty
import
Test.Tasty.QuickCheck
import
Test.Tasty
(
TestTree
,
testGroup
)
import
qualified
Test.Tasty.QuickCheck
as
QC
import
Test.Tasty.QuickCheck
((
===
),
(
.&&.
))
--
...
...
@@ -31,11 +32,12 @@ import Test.Tasty.QuickCheck
newtype
SquareMatrix
a
=
SquareMatrix
{
_SquareMatrix
::
Matrix
a
}
deriving
newtype
(
Show
,
Eq
)
instance
(
Elt
a
,
Show
a
,
Prelude
.
Num
a
,
Ord
a
,
Arbitrary
a
)
=>
Arbitrary
(
SquareMatrix
a
)
where
instance
(
Elt
a
,
Show
a
,
Prelude
.
Num
a
,
Ord
a
,
QC
.
Arbitrary
a
)
=>
QC
.
Arbitrary
(
SquareMatrix
a
)
where
arbitrary
=
do
x
<-
choose
(
1
,
30
)
x
<-
QC
.
choose
(
1
,
30
)
let
sh
=
Z
:.
x
:.
x
SquareMatrix
.
A
.
fromList
sh
<$>
vectorOf
(
x
*
x
)
arbitrary
SquareMatrix
.
A
.
fromList
sh
<$>
QC
.
vectorOf
(
x
*
x
)
QC
.
arbitrary
shrink
=
map
SquareMatrix
.
sliceArray
.
_SquareMatrix
...
...
@@ -45,16 +47,16 @@ instance (Elt a, Show a, Prelude.Num a, Ord a, Arbitrary a) => Arbitrary (Square
newtype
CoocMatrix
=
CoocMatrix
{
_CoocMatrix
::
Matrix
Int
}
deriving
newtype
(
Show
,
Eq
)
instance
Arbitrary
CoocMatrix
where
instance
QC
.
Arbitrary
CoocMatrix
where
-- | We simulate the creation of a cooccurence matrix: there are `numContexts`
-- "virtual contexts" and `numTerms` "virtual terms"; for each context and
-- each term, the term has probability `probAppearance` to appear in the
-- context. The generated matrix is the cooccurence matrix of the resulting
-- "virtual corpus"
arbitrary
=
do
numContexts
<-
choose
(
1
,
30
)
numTerms
<-
choose
(
1
,
30
)
probAppearance
<-
(
choose
(
0
,
1
)
::
Gen
Double
)
numContexts
<-
QC
.
choose
(
1
,
30
)
numTerms
<-
QC
.
choose
(
1
,
30
)
probAppearance
<-
(
QC
.
choose
(
0
,
1
)
::
QC
.
Gen
Double
)
-- `appearances` is a list of lists of integers encoding which virtual terms
-- appear in which virtual contexts. More specifically, the integer j
-- appears in the i-th list iff the virtual term j appears in the i-th
...
...
@@ -63,7 +65,7 @@ instance Arbitrary CoocMatrix where
-- In a given virtual context, iterate over all terms and pick whether
-- they appear in the context with probability `probAppearance`
foldM
(
\
termsSoFar
candidateTerm
->
do
randomVar
<-
choose
(
0
,
1
)
randomVar
<-
QC
.
choose
(
0
,
1
)
return
$
if
randomVar
<
probAppearance
then
candidateTerm
:
termsSoFar
else
termsSoFar
)
[]
[
1
..
numTerms
]
...
...
@@ -71,7 +73,7 @@ instance Arbitrary CoocMatrix where
[
1
..
numTerms
]
<&>
(
\
j
->
(
i
,
j
)
))
-- For each pair of virtual terms, iterate over all virtual contexts and add
-- 1 each time both term appear in the context:
-- 1 each time both term
s
appear in the context:
let
coocMatrix
=
(
fmap
.
fmap
)
(
\
(
i
,
j
)
->
foldr
(
\
contextTerms
currentCoocCount
->
if
i
`
elem
`
contextTerms
&&
j
`
elem
`
contextTerms
...
...
@@ -90,7 +92,7 @@ twoByTwo :: SquareMatrix Int
twoByTwo
=
SquareMatrix
$
fromList
(
Z
:.
2
:.
2
)
(
Prelude
.
replicate
4
5
)
testMatrix_01
::
SquareMatrix
Int
testMatrix_01
=
SquareMatrix
$
fromList
(
Z
:.
14
:.
14
)
$
testMatrix_01
=
SquareMatrix
$
fromList
(
Z
:.
14
:.
14
)
[
30
,
36
,
-
36
,
-
16
,
0
,
7
,
34
,
-
7
,
5
,
-
4
,
0
,
21
,
6
,
-
35
,
0
,
-
31
,
20
,
-
15
,
-
22
,
-
7
,
-
22
,
-
37
,
-
29
,
-
29
,
23
,
-
31
,
-
29
,
-
23
,
-
24
,
-
29
,
19
,
-
6
,
16
,
7
,
15
,
-
27
,
-
27
,
-
30
,
-
9
,
-
33
,
18
,
-
23
,
...
...
@@ -107,7 +109,7 @@ testMatrix_01 = SquareMatrix $ fromList (Z :. 14 :. 14) $
13
,
-
37
,
-
16
,
2
,
7
,
-
13
,
21
,
-
10
,
-
33
,
-
33
,
-
26
,
-
19
,
-
1
,
29
]
testMatrix_02
::
SquareMatrix
Int
testMatrix_02
=
SquareMatrix
$
fromList
(
Z
:.
7
:.
7
)
$
testMatrix_02
=
SquareMatrix
$
fromList
(
Z
:.
7
:.
7
)
[
30
,
36
,
-
36
,
-
16
,
0
,
7
,
34
,
0
,
-
31
,
20
,
-
15
,
-
22
,
-
7
,
-
22
,
-
24
,
-
29
,
19
,
-
6
,
16
,
7
,
15
,
...
...
@@ -117,7 +119,7 @@ testMatrix_02 = SquareMatrix $ fromList (Z :. 7 :. 7) $
13
,
-
37
,
-
16
,
2
,
7
,
-
13
,
21
]
testMatrix_03
::
SquareMatrix
Int
testMatrix_03
=
SquareMatrix
$
fromList
(
Z
:.
11
:.
11
)
$
testMatrix_03
=
SquareMatrix
$
fromList
(
Z
:.
11
:.
11
)
[
1
,
-
1
,
1
,
0
,
1
,
-
1
,
0
,
1
,
1
,
0
,
0
,
1
,
1
,
1
,
1
,
1
,
0
,
1
,
-
1
,
1
,
0
,
0
,
-
1
,
1
,
0
,
-
1
,
0
,
-
1
,
0
,
1
,
0
,
-
1
,
0
,
...
...
@@ -131,7 +133,7 @@ testMatrix_03 = SquareMatrix $ fromList (Z :. 11 :. 11) $
1
,
1
,
-
1
,
0
,
-
1
,
-
1
,
1
,
0
,
1
,
0
,
-
1
]
testMatrix_04
::
SquareMatrix
Int
testMatrix_04
=
SquareMatrix
$
fromList
(
Z
:.
8
:.
8
)
$
testMatrix_04
=
SquareMatrix
$
fromList
(
Z
:.
8
:.
8
)
[
3
,
-
1
,
0
,
1
,
-
1
,
1
,
1
,
-
3
,
-
2
,
-
2
,
2
,
1
,
1
,
-
2
,
1
,
-
1
,
-
2
,
-
3
,
-
1
,
1
,
1
,
-
3
,
-
2
,
-
1
,
...
...
@@ -146,28 +148,28 @@ testMatrix_04 = SquareMatrix $ fromList (Z :. 8 :. 8) $
tests
::
TestTree
tests
=
testGroup
"LinearAlgebra"
[
testProperty
"termDivNan"
compareTermDivNan
,
testProperty
"diag"
compareDiag
,
testProperty
"sumRows"
compareSumRows
,
testProperty
"matMaxMini"
compareMatMaxMini
,
testProperty
"sumM_go"
compareSumM_go
,
testProperty
"sumMin_go"
compareSumMin_go
,
testProperty
"matrixEye"
compareMatrixEye
,
testProperty
"diagNull"
compareDiagNull
QC
.
testProperty
"termDivNan"
compareTermDivNan
,
QC
.
testProperty
"diag"
compareDiag
,
QC
.
testProperty
"sumRows"
compareSumRows
,
QC
.
testProperty
"matMaxMini"
compareMatMaxMini
,
QC
.
testProperty
"sumM_go"
compareSumM_go
,
QC
.
testProperty
"sumMin_go"
compareSumMin_go
,
QC
.
testProperty
"matrixEye"
compareMatrixEye
,
QC
.
testProperty
"diagNull"
compareDiagNull
,
testGroup
"distributional"
[
testProperty
"reference implementation roundtrips"
compareDistributionalImplementations
,
testProperty
"2x2"
(
compareDistributional
(
Proxy
@
Double
)
twoByTwo
)
,
testProperty
"7x7"
(
compareDistributional
(
Proxy
@
Double
)
testMatrix_02
)
,
testProperty
"14x14"
(
compareDistributional
(
Proxy
@
Double
)
testMatrix_01
)
,
testProperty
"roundtrips"
(
compareDistributional
(
Proxy
@
Double
))
QC
.
testProperty
"reference implementation roundtrips"
compareDistributionalImplementations
,
QC
.
testProperty
"2x2"
(
compareDistributional
(
Proxy
@
Double
)
twoByTwo
)
,
QC
.
testProperty
"7x7"
(
compareDistributional
(
Proxy
@
Double
)
testMatrix_02
)
,
QC
.
testProperty
"14x14"
(
compareDistributional
(
Proxy
@
Double
)
testMatrix_01
)
,
QC
.
testProperty
"roundtrips"
(
compareDistributional
(
Proxy
@
Double
))
]
,
testGroup
"logDistributional2"
[
testProperty
"2x2"
(
compareLogDistributional2
(
Proxy
@
Double
)
twoByTwo
)
,
testProperty
"7x7"
(
compareLogDistributional2
(
Proxy
@
Double
)
testMatrix_02
)
,
testProperty
"8x8"
(
compareLogDistributional2
(
Proxy
@
Double
)
testMatrix_04
)
,
testProperty
"11x11"
(
compareLogDistributional2
(
Proxy
@
Double
)
testMatrix_03
)
,
testProperty
"14x14"
(
compareLogDistributional2
(
Proxy
@
Double
)
testMatrix_01
)
,
testProperty
"roundtrips"
(
compareLogDistributional2
(
Proxy
@
Double
))
QC
.
testProperty
"2x2"
(
compareLogDistributional2
(
Proxy
@
Double
)
twoByTwo
)
,
QC
.
testProperty
"7x7"
(
compareLogDistributional2
(
Proxy
@
Double
)
testMatrix_02
)
,
QC
.
testProperty
"8x8"
(
compareLogDistributional2
(
Proxy
@
Double
)
testMatrix_04
)
,
QC
.
testProperty
"11x11"
(
compareLogDistributional2
(
Proxy
@
Double
)
testMatrix_03
)
,
QC
.
testProperty
"14x14"
(
compareLogDistributional2
(
Proxy
@
Double
)
testMatrix_01
)
,
QC
.
testProperty
"roundtrips"
(
compareLogDistributional2
(
Proxy
@
Double
))
]
]
...
...
@@ -177,27 +179,27 @@ tests = testGroup "LinearAlgebra" [
compareTermDivNan
::
(
Array
TermDivNanShape
Double
)
->
(
Array
TermDivNanShape
Double
)
->
Property
->
QC
.
Property
compareTermDivNan
i1
i2
=
let
massiv
=
LA
.
termDivNan
@
Massiv
.
U
(
LA
.
accelerate2MassivMatrix
i1
)
(
LA
.
accelerate2MassivMatrix
i2
)
accelerate
=
Naive
.
run
(
Legacy
.
termDivNan
(
use
i1
)
(
use
i2
))
in
accelerate
===
LA
.
massiv2AccelerateMatrix
massiv
compareDiag
::
SquareMatrix
Int
->
Property
compareDiag
::
SquareMatrix
Int
->
QC
.
Property
compareDiag
(
SquareMatrix
i1
)
=
let
massiv
=
LA
.
diag
(
LA
.
accelerate2MassivMatrix
i1
)
accelerate
=
Naive
.
run
(
Legacy
.
diag
(
use
i1
))
in
accelerate
===
LA
.
massiv2AccelerateVector
massiv
compareSumRows
::
Array
(
Z
:.
Int
:.
Int
:.
Int
)
Int
->
Property
compareSumRows
::
Array
(
Z
:.
Int
:.
Int
:.
Int
)
Int
->
QC
.
Property
compareSumRows
i1
=
let
massiv
=
LA
.
sumRows
@
_
@
Massiv
.
U
(
LA
.
accelerate2Massiv3DMatrix
i1
)
massiv'
=
LA
.
sumRowsReferenceImplementation
@
Massiv
.
U
(
LA
.
accelerate2Massiv3DMatrix
i1
)
accelerate
=
Naive
.
run
(
A
.
sum
(
use
i1
))
in
counterexample
"sumRows and reference implementation do not agree"
(
massiv
===
massiv'
)
.&&.
in
QC
.
counterexample
"sumRows and reference implementation do not agree"
(
massiv
===
massiv'
)
.&&.
accelerate
===
LA
.
massiv2AccelerateMatrix
massiv
compareDistributionalImplementations
::
SquareMatrix
Int
->
Property
compareDistributionalImplementations
::
SquareMatrix
Int
->
QC
.
Property
compareDistributionalImplementations
(
SquareMatrix
i1
)
=
let
ma
=
LA
.
accelerate2MassivMatrix
i1
in
LA
.
distributional
@
Massiv
.
U
@
Double
ma
===
LA
.
distributionalReferenceImplementation
ma
...
...
@@ -215,13 +217,13 @@ compareDistributional :: forall e.
,
Monoid
e
)
=>
Proxy
e
->
SquareMatrix
Int
->
Property
->
QC
.
Property
compareDistributional
Proxy
(
SquareMatrix
i1
)
=
let
massiv
=
Massiv
.
computeAs
Massiv
.
B
$
LA
.
distributional
@
_
@
e
(
LA
.
accelerate2MassivMatrix
i1
)
accelerate
=
Legacy
.
distributionalWith
Naive
.
run
i1
expected
=
map
conv
(
A
.
toList
accelerate
)
actual
=
map
conv
(
mconcat
(
Massiv
.
toLists2
massiv
))
in
counterexample
"size not equal"
(
Prelude
.
length
expected
===
Prelude
.
length
actual
)
.&&.
expected
===
actual
in
QC
.
counterexample
"size not equal"
(
Prelude
.
length
expected
===
Prelude
.
length
actual
)
.&&.
expected
===
actual
where
conv
::
e
->
Scientific
conv
=
fromFloatDigits
...
...
@@ -241,42 +243,42 @@ compareLogDistributional2 :: forall e.
,
Monoid
e
)
=>
Proxy
e
->
SquareMatrix
Int
->
Property
->
QC
.
Property
compareLogDistributional2
Proxy
(
SquareMatrix
i1
)
=
let
massiv
=
Massiv
.
computeAs
Massiv
.
B
$
LA
.
logDistributional2
@
_
@
e
(
LA
.
accelerate2MassivMatrix
i1
)
accelerate
=
Legacy
.
logDistributional2With
Naive
.
run
i1
expected
=
map
conv
(
A
.
toList
accelerate
)
actual
=
map
conv
(
mconcat
(
Massiv
.
toLists2
massiv
))
in
counterexample
"size not equal"
(
Prelude
.
length
expected
===
Prelude
.
length
actual
)
.&&.
expected
===
actual
in
QC
.
counterexample
"size not equal"
(
Prelude
.
length
expected
===
Prelude
.
length
actual
)
.&&.
expected
===
actual
where
conv
::
e
->
Scientific
conv
=
fromFloatDigits
compareMatMaxMini
::
SquareMatrix
Int
->
Property
compareMatMaxMini
::
SquareMatrix
Int
->
QC
.
Property
compareMatMaxMini
(
SquareMatrix
i1
)
=
let
massiv
=
LA
.
matMaxMini
(
LA
.
accelerate2MassivMatrix
i1
)
accelerate
=
Naive
.
run
(
A
.
matMaxMini
(
use
i1
))
in
accelerate
===
LA
.
massiv2AccelerateMatrix
massiv
compareSumMin_go
::
SquareMatrix
Int
->
Property
compareSumMin_go
::
SquareMatrix
Int
->
QC
.
Property
compareSumMin_go
(
SquareMatrix
i1
)
=
let
massiv
=
LA
.
sumMin_go
(
A
.
dim
i1
)
(
LA
.
accelerate2MassivMatrix
i1
)
accelerate
=
Naive
.
run
(
Legacy
.
sumMin_go
(
A
.
dim
i1
)
(
use
i1
))
in
accelerate
===
LA
.
massiv2AccelerateMatrix
massiv
compareSumM_go
::
SquareMatrix
Int
->
Property
compareSumM_go
::
SquareMatrix
Int
->
QC
.
Property
compareSumM_go
(
SquareMatrix
i1
)
=
let
massiv
=
LA
.
sumM_go
(
A
.
dim
i1
)
(
LA
.
accelerate2MassivMatrix
i1
)
accelerate
=
Naive
.
run
(
Legacy
.
sumM_go
(
A
.
dim
i1
)
(
use
i1
))
in
accelerate
===
LA
.
massiv2AccelerateMatrix
massiv
compareMatrixEye
::
Positive
Int
->
Property
compareMatrixEye
(
getPositive
->
n
)
compareMatrixEye
::
QC
.
Positive
Int
->
QC
.
Property
compareMatrixEye
(
QC
.
getPositive
->
n
)
=
let
massiv
=
Massiv
.
compute
@
Massiv
.
U
$
LA
.
matrixEye
@
Int
n
accelerate
=
Naive
.
run
(
Legacy
.
matrixEye
n
)
in
accelerate
===
LA
.
massiv2AccelerateMatrix
massiv
compareDiagNull
::
SquareMatrix
Int
->
Property
compareDiagNull
::
SquareMatrix
Int
->
QC
.
Property
compareDiagNull
(
SquareMatrix
i1
)
=
let
massiv
=
Massiv
.
compute
@
Massiv
.
U
$
LA
.
diagNull
(
A
.
dim
i1
)
(
LA
.
accelerate2MassivMatrix
i1
)
accelerate
=
Naive
.
run
(
Legacy
.
diagNull
(
A
.
dim
i1
)
(
use
i1
))
...
...
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