diff options
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Tests/Instances.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests/Instances.hs | 91 |
1 files changed, 84 insertions, 7 deletions
diff --git a/lib/Numeric/LinearAlgebra/Tests/Instances.hs b/lib/Numeric/LinearAlgebra/Tests/Instances.hs index 4e829d2..9b18513 100644 --- a/lib/Numeric/LinearAlgebra/Tests/Instances.hs +++ b/lib/Numeric/LinearAlgebra/Tests/Instances.hs | |||
@@ -1,4 +1,5 @@ | |||
1 | {-# LANGUAGE FlexibleContexts, UndecidableInstances #-} | 1 | {-# LANGUAGE FlexibleContexts, UndecidableInstances, CPP #-} |
2 | {-# OPTIONS_GHC -fno-warn-unused-imports #-} | ||
2 | ----------------------------------------------------------------------------- | 3 | ----------------------------------------------------------------------------- |
3 | {- | | 4 | {- | |
4 | Module : Numeric.LinearAlgebra.Tests.Instances | 5 | Module : Numeric.LinearAlgebra.Tests.Instances |
@@ -24,24 +25,53 @@ module Numeric.LinearAlgebra.Tests.Instances( | |||
24 | RM,CM, rM,cM | 25 | RM,CM, rM,cM |
25 | ) where | 26 | ) where |
26 | 27 | ||
28 | |||
29 | |||
30 | |||
27 | import Numeric.LinearAlgebra | 31 | import Numeric.LinearAlgebra |
28 | import Test.QuickCheck | ||
29 | import Control.Monad(replicateM) | 32 | import Control.Monad(replicateM) |
33 | #include "quickCheckCompat.h" | ||
34 | |||
35 | |||
36 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
37 | shrinkListElementwise :: (Arbitrary a) => [a] -> [[a]] | ||
38 | shrinkListElementwise [] = [] | ||
39 | shrinkListElementwise (x:xs) = [ y:xs | y <- shrink x ] | ||
40 | ++ [ x:ys | ys <- shrinkListElementwise xs ] | ||
41 | |||
42 | shrinkPair :: (Arbitrary a, Arbitrary b) => (a,b) -> [(a,b)] | ||
43 | shrinkPair (a,b) = [ (a,x) | x <- shrink b ] ++ [ (x,b) | x <- shrink a ] | ||
44 | #endif | ||
45 | |||
30 | 46 | ||
31 | instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where | 47 | instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where |
32 | arbitrary = do | 48 | arbitrary = do |
33 | re <- arbitrary | 49 | re <- arbitrary |
34 | im <- arbitrary | 50 | im <- arbitrary |
35 | return (re :+ im) | 51 | return (re :+ im) |
36 | coarbitrary = undefined | 52 | |
53 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
54 | shrink (re :+ im) = | ||
55 | [ u :+ v | (u,v) <- shrinkPair (re,im) ] | ||
56 | #else | ||
57 | -- this has been moved to the 'Coarbitrary' class in QuickCheck 2 | ||
58 | coarbitrary = undefined | ||
59 | #endif | ||
37 | 60 | ||
38 | chooseDim = sized $ \m -> choose (1,max 1 m) | 61 | chooseDim = sized $ \m -> choose (1,max 1 m) |
39 | 62 | ||
40 | instance (Field a, Arbitrary a) => Arbitrary (Vector a) where | 63 | instance (Field a, Arbitrary a) => Arbitrary (Vector a) where |
41 | arbitrary = do m <- chooseDim | 64 | arbitrary = do m <- chooseDim |
42 | l <- vector m | 65 | l <- vector m |
43 | return $ fromList l | 66 | return $ fromList l |
44 | coarbitrary = undefined | 67 | |
68 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
69 | -- shrink any one of the components | ||
70 | shrink = map fromList . shrinkListElementwise . toList | ||
71 | |||
72 | #else | ||
73 | coarbitrary = undefined | ||
74 | #endif | ||
45 | 75 | ||
46 | instance (Element a, Arbitrary a) => Arbitrary (Matrix a) where | 76 | instance (Element a, Arbitrary a) => Arbitrary (Matrix a) where |
47 | arbitrary = do | 77 | arbitrary = do |
@@ -49,7 +79,17 @@ instance (Element a, Arbitrary a) => Arbitrary (Matrix a) where | |||
49 | n <- chooseDim | 79 | n <- chooseDim |
50 | l <- vector (m*n) | 80 | l <- vector (m*n) |
51 | return $ (m><n) l | 81 | return $ (m><n) l |
82 | |||
83 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
84 | -- shrink any one of the components | ||
85 | shrink a = map ((rows a) >< (cols a)) | ||
86 | . shrinkListElementwise | ||
87 | . concat . toLists | ||
88 | $ a | ||
89 | #else | ||
52 | coarbitrary = undefined | 90 | coarbitrary = undefined |
91 | #endif | ||
92 | |||
53 | 93 | ||
54 | -- a square matrix | 94 | -- a square matrix |
55 | newtype (Sq a) = Sq (Matrix a) deriving Show | 95 | newtype (Sq a) = Sq (Matrix a) deriving Show |
@@ -58,7 +98,13 @@ instance (Element a, Arbitrary a) => Arbitrary (Sq a) where | |||
58 | n <- chooseDim | 98 | n <- chooseDim |
59 | l <- vector (n*n) | 99 | l <- vector (n*n) |
60 | return $ Sq $ (n><n) l | 100 | return $ Sq $ (n><n) l |
101 | |||
102 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
103 | shrink (Sq a) = [ Sq b | b <- shrink a ] | ||
104 | #else | ||
61 | coarbitrary = undefined | 105 | coarbitrary = undefined |
106 | #endif | ||
107 | |||
62 | 108 | ||
63 | -- a unitary matrix | 109 | -- a unitary matrix |
64 | newtype (Rot a) = Rot (Matrix a) deriving Show | 110 | newtype (Rot a) = Rot (Matrix a) deriving Show |
@@ -67,7 +113,12 @@ instance (Field a, Arbitrary a) => Arbitrary (Rot a) where | |||
67 | Sq m <- arbitrary | 113 | Sq m <- arbitrary |
68 | let (q,_) = qr m | 114 | let (q,_) = qr m |
69 | return (Rot q) | 115 | return (Rot q) |
116 | |||
117 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
118 | #else | ||
70 | coarbitrary = undefined | 119 | coarbitrary = undefined |
120 | #endif | ||
121 | |||
71 | 122 | ||
72 | -- a complex hermitian or real symmetric matrix | 123 | -- a complex hermitian or real symmetric matrix |
73 | newtype (Her a) = Her (Matrix a) deriving Show | 124 | newtype (Her a) = Her (Matrix a) deriving Show |
@@ -76,7 +127,12 @@ instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (Her a) where | |||
76 | Sq m <- arbitrary | 127 | Sq m <- arbitrary |
77 | let m' = m/2 | 128 | let m' = m/2 |
78 | return $ Her (m' + ctrans m') | 129 | return $ Her (m' + ctrans m') |
130 | |||
131 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
132 | #else | ||
79 | coarbitrary = undefined | 133 | coarbitrary = undefined |
134 | #endif | ||
135 | |||
80 | 136 | ||
81 | -- a well-conditioned general matrix (the singular values are between 1 and 100) | 137 | -- a well-conditioned general matrix (the singular values are between 1 and 100) |
82 | newtype (WC a) = WC (Matrix a) deriving Show | 138 | newtype (WC a) = WC (Matrix a) deriving Show |
@@ -90,7 +146,12 @@ instance (Field a, Arbitrary a) => Arbitrary (WC a) where | |||
90 | sv <- replicateM n (choose (1,100)) | 146 | sv <- replicateM n (choose (1,100)) |
91 | let s = diagRect (fromList sv) r c | 147 | let s = diagRect (fromList sv) r c |
92 | return $ WC (u <> real s <> trans v) | 148 | return $ WC (u <> real s <> trans v) |
149 | |||
150 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
151 | #else | ||
93 | coarbitrary = undefined | 152 | coarbitrary = undefined |
153 | #endif | ||
154 | |||
94 | 155 | ||
95 | -- a well-conditioned square matrix (the singular values are between 1 and 100) | 156 | -- a well-conditioned square matrix (the singular values are between 1 and 100) |
96 | newtype (SqWC a) = SqWC (Matrix a) deriving Show | 157 | newtype (SqWC a) = SqWC (Matrix a) deriving Show |
@@ -102,7 +163,12 @@ instance (Field a, Arbitrary a) => Arbitrary (SqWC a) where | |||
102 | sv <- replicateM n (choose (1,100)) | 163 | sv <- replicateM n (choose (1,100)) |
103 | let s = diag (fromList sv) | 164 | let s = diag (fromList sv) |
104 | return $ SqWC (u <> real s <> trans v) | 165 | return $ SqWC (u <> real s <> trans v) |
166 | |||
167 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
168 | #else | ||
105 | coarbitrary = undefined | 169 | coarbitrary = undefined |
170 | #endif | ||
171 | |||
106 | 172 | ||
107 | -- a positive definite square matrix (the eigenvalues are between 0 and 100) | 173 | -- a positive definite square matrix (the eigenvalues are between 0 and 100) |
108 | newtype (PosDef a) = PosDef (Matrix a) deriving Show | 174 | newtype (PosDef a) = PosDef (Matrix a) deriving Show |
@@ -115,7 +181,12 @@ instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (PosDef a) where | |||
115 | let s = diag (fromList l) | 181 | let s = diag (fromList l) |
116 | p = v <> real s <> ctrans v | 182 | p = v <> real s <> ctrans v |
117 | return $ PosDef (0.5 .* p + 0.5 .* ctrans p) | 183 | return $ PosDef (0.5 .* p + 0.5 .* ctrans p) |
184 | |||
185 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
186 | #else | ||
118 | coarbitrary = undefined | 187 | coarbitrary = undefined |
188 | #endif | ||
189 | |||
119 | 190 | ||
120 | -- a pair of matrices that can be multiplied | 191 | -- a pair of matrices that can be multiplied |
121 | newtype (Consistent a) = Consistent (Matrix a, Matrix a) deriving Show | 192 | newtype (Consistent a) = Consistent (Matrix a, Matrix a) deriving Show |
@@ -127,7 +198,13 @@ instance (Field a, Arbitrary a) => Arbitrary (Consistent a) where | |||
127 | la <- vector (n*k) | 198 | la <- vector (n*k) |
128 | lb <- vector (k*m) | 199 | lb <- vector (k*m) |
129 | return $ Consistent ((n><k) la, (k><m) lb) | 200 | return $ Consistent ((n><k) la, (k><m) lb) |
201 | |||
202 | #if MIN_VERSION_QuickCheck(2,0,0) | ||
203 | shrink (Consistent (x,y)) = [ Consistent (u,v) | (u,v) <- shrinkPair (x,y) ] | ||
204 | #else | ||
130 | coarbitrary = undefined | 205 | coarbitrary = undefined |
206 | #endif | ||
207 | |||
131 | 208 | ||
132 | 209 | ||
133 | type RM = Matrix Double | 210 | type RM = Matrix Double |