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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
{-# LANGUAGE FlexibleContexts, UndecidableInstances, CPP, FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
-----------------------------------------------------------------------------
{- |
Module : Numeric.LinearAlgebra.Tests.Instances
Copyright : (c) Alberto Ruiz 2008
License : BSD3
Maintainer : Alberto Ruiz
Stability : provisional
Arbitrary instances for vectors, matrices.
-}
module Numeric.LinearAlgebra.Tests.Instances(
Sq(..), rSq,cSq,
Rot(..), rRot,cRot,
Her(..), rHer,cHer,
WC(..), rWC,cWC,
SqWC(..), rSqWC, cSqWC,
PosDef(..), rPosDef, cPosDef,
Consistent(..), rConsist, cConsist,
RM,CM, rM,cM,
FM,ZM, fM,zM
) where
import System.Random
import Numeric.LinearAlgebra
import Numeric.LinearAlgebra.Devel
import Numeric.Container
import Control.Monad(replicateM)
import Test.QuickCheck(Arbitrary,arbitrary,coarbitrary,choose,vector
,sized,classify,Testable,Property
,quickCheckWith,maxSize,stdArgs,shrink)
#if MIN_VERSION_QuickCheck(2,0,0)
shrinkListElementwise :: (Arbitrary a) => [a] -> [[a]]
shrinkListElementwise [] = []
shrinkListElementwise (x:xs) = [ y:xs | y <- shrink x ]
++ [ x:ys | ys <- shrinkListElementwise xs ]
shrinkPair :: (Arbitrary a, Arbitrary b) => (a,b) -> [(a,b)]
shrinkPair (a,b) = [ (a,x) | x <- shrink b ] ++ [ (x,b) | x <- shrink a ]
#endif
#if MIN_VERSION_QuickCheck(2,1,1)
#else
instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where
arbitrary = do
re <- arbitrary
im <- arbitrary
return (re :+ im)
#if MIN_VERSION_QuickCheck(2,0,0)
shrink (re :+ im) =
[ u :+ v | (u,v) <- shrinkPair (re,im) ]
#else
-- this has been moved to the 'Coarbitrary' class in QuickCheck 2
coarbitrary = undefined
#endif
#endif
chooseDim = sized $ \m -> choose (1,max 1 m)
instance (Field a, Arbitrary a) => Arbitrary (Vector a) where
arbitrary = do m <- chooseDim
l <- vector m
return $ fromList l
#if MIN_VERSION_QuickCheck(2,0,0)
-- shrink any one of the components
shrink = map fromList . shrinkListElementwise . toList
#else
coarbitrary = undefined
#endif
instance (Element a, Arbitrary a) => Arbitrary (Matrix a) where
arbitrary = do
m <- chooseDim
n <- chooseDim
l <- vector (m*n)
return $ (m><n) l
#if MIN_VERSION_QuickCheck(2,0,0)
-- shrink any one of the components
shrink a = map (rows a >< cols a)
. shrinkListElementwise
. concat . toLists
$ a
#else
coarbitrary = undefined
#endif
-- a square matrix
newtype (Sq a) = Sq (Matrix a) deriving Show
instance (Element a, Arbitrary a) => Arbitrary (Sq a) where
arbitrary = do
n <- chooseDim
l <- vector (n*n)
return $ Sq $ (n><n) l
#if MIN_VERSION_QuickCheck(2,0,0)
shrink (Sq a) = [ Sq b | b <- shrink a ]
#else
coarbitrary = undefined
#endif
-- a unitary matrix
newtype (Rot a) = Rot (Matrix a) deriving Show
instance (Field a, Arbitrary a) => Arbitrary (Rot a) where
arbitrary = do
Sq m <- arbitrary
let (q,_) = qr m
return (Rot q)
#if MIN_VERSION_QuickCheck(2,0,0)
#else
coarbitrary = undefined
#endif
-- a complex hermitian or real symmetric matrix
newtype (Her a) = Her (Matrix a) deriving Show
instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (Her a) where
arbitrary = do
Sq m <- arbitrary
let m' = m/2
return $ Her (m' + ctrans m')
#if MIN_VERSION_QuickCheck(2,0,0)
#else
coarbitrary = undefined
#endif
class (Field a, Arbitrary a, Element (RealOf a), Random (RealOf a)) => ArbitraryField a
instance ArbitraryField Double
instance ArbitraryField (Complex Double)
-- a well-conditioned general matrix (the singular values are between 1 and 100)
newtype (WC a) = WC (Matrix a) deriving Show
instance (ArbitraryField a) => Arbitrary (WC a) where
arbitrary = do
m <- arbitrary
let (u,_,v) = svd m
r = rows m
c = cols m
n = min r c
sv' <- replicateM n (choose (1,100))
let s = diagRect 0 (fromList sv') r c
return $ WC (u `mXm` real s `mXm` trans v)
#if MIN_VERSION_QuickCheck(2,0,0)
#else
coarbitrary = undefined
#endif
-- a well-conditioned square matrix (the singular values are between 1 and 100)
newtype (SqWC a) = SqWC (Matrix a) deriving Show
instance (ArbitraryField a) => Arbitrary (SqWC a) where
arbitrary = do
Sq m <- arbitrary
let (u,_,v) = svd m
n = rows m
sv' <- replicateM n (choose (1,100))
let s = diag (fromList sv')
return $ SqWC (u `mXm` real s `mXm` trans v)
#if MIN_VERSION_QuickCheck(2,0,0)
#else
coarbitrary = undefined
#endif
-- a positive definite square matrix (the eigenvalues are between 0 and 100)
newtype (PosDef a) = PosDef (Matrix a) deriving Show
instance (ArbitraryField a, Num (Vector a))
=> Arbitrary (PosDef a) where
arbitrary = do
Her m <- arbitrary
let (_,v) = eigSH m
n = rows m
l <- replicateM n (choose (0,100))
let s = diag (fromList l)
p = v `mXm` real s `mXm` ctrans v
return $ PosDef (0.5 * p + 0.5 * ctrans p)
#if MIN_VERSION_QuickCheck(2,0,0)
#else
coarbitrary = undefined
#endif
-- a pair of matrices that can be multiplied
newtype (Consistent a) = Consistent (Matrix a, Matrix a) deriving Show
instance (Field a, Arbitrary a) => Arbitrary (Consistent a) where
arbitrary = do
n <- chooseDim
k <- chooseDim
m <- chooseDim
la <- vector (n*k)
lb <- vector (k*m)
return $ Consistent ((n><k) la, (k><m) lb)
#if MIN_VERSION_QuickCheck(2,0,0)
shrink (Consistent (x,y)) = [ Consistent (u,v) | (u,v) <- shrinkPair (x,y) ]
#else
coarbitrary = undefined
#endif
type RM = Matrix Double
type CM = Matrix (Complex Double)
type FM = Matrix Float
type ZM = Matrix (Complex Float)
rM m = m :: RM
cM m = m :: CM
fM m = m :: FM
zM m = m :: ZM
rHer (Her m) = m :: RM
cHer (Her m) = m :: CM
rRot (Rot m) = m :: RM
cRot (Rot m) = m :: CM
rSq (Sq m) = m :: RM
cSq (Sq m) = m :: CM
rWC (WC m) = m :: RM
cWC (WC m) = m :: CM
rSqWC (SqWC m) = m :: RM
cSqWC (SqWC m) = m :: CM
rPosDef (PosDef m) = m :: RM
cPosDef (PosDef m) = m :: CM
rConsist (Consistent (a,b)) = (a,b::RM)
cConsist (Consistent (a,b)) = (a,b::CM)
|