diff options
author | Alberto Ruiz <aruiz@um.es> | 2008-01-28 19:31:59 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2008-01-28 19:31:59 +0000 |
commit | 240ae9be06380814fc1e223c3c53c746e5b1e6ba (patch) | |
tree | 7d69620b120fe8fe5552f6a586422d24921b2f0c /lib/Numeric/LinearAlgebra/Testing.hs | |
parent | 003b8db7a864bbea3772cd70906153bd36d1f7ac (diff) |
added the Testing module (and minor changes in instance definitions)
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Testing.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Testing.hs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/Numeric/LinearAlgebra/Testing.hs b/lib/Numeric/LinearAlgebra/Testing.hs new file mode 100644 index 0000000..dcf1d8e --- /dev/null +++ b/lib/Numeric/LinearAlgebra/Testing.hs | |||
@@ -0,0 +1,98 @@ | |||
1 | {-# OPTIONS_GHC -XPatternSignatures #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : Numeric.LinearAlgebra.Testing | ||
5 | Copyright : (c) Alberto Ruiz 2007 | ||
6 | License : GPL-style | ||
7 | |||
8 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
9 | Stability : provisional | ||
10 | Portability : portable | ||
11 | |||
12 | Some consistency tests. | ||
13 | |||
14 | -} | ||
15 | |||
16 | module Numeric.LinearAlgebra.Testing( | ||
17 | runTests, runBigTests | ||
18 | ) where | ||
19 | |||
20 | import Numeric.LinearAlgebra | ||
21 | import Test.QuickCheck | ||
22 | import Debug.Trace | ||
23 | |||
24 | qCheck n = check defaultConfig {configSize = const n} | ||
25 | |||
26 | debug x = trace (show x) x | ||
27 | |||
28 | type RM = Matrix Double | ||
29 | type CM = Matrix (Complex Double) | ||
30 | |||
31 | instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where | ||
32 | arbitrary = do | ||
33 | r <- arbitrary | ||
34 | i <- arbitrary | ||
35 | return (r:+i) | ||
36 | coarbitrary = undefined | ||
37 | |||
38 | chooseDim = sized $ \m -> choose (1,max 1 m) | ||
39 | |||
40 | instance (Field a, Arbitrary a) => Arbitrary (Vector a) where | ||
41 | arbitrary = do m <- chooseDim | ||
42 | l <- vector m | ||
43 | return $ fromList l | ||
44 | coarbitrary = undefined | ||
45 | |||
46 | instance (Element a, Arbitrary a) => Arbitrary (Matrix a) where | ||
47 | arbitrary = do | ||
48 | m <- chooseDim | ||
49 | n <- chooseDim | ||
50 | l <- vector (m*n) | ||
51 | return $ (m><n) l | ||
52 | coarbitrary = undefined | ||
53 | |||
54 | |||
55 | newtype (Sq a) = Sq (Matrix a) deriving Show | ||
56 | sq (Sq m) = m | ||
57 | |||
58 | instance (Element a, Arbitrary a) => Arbitrary (Sq a) where | ||
59 | arbitrary = do | ||
60 | n <- chooseDim | ||
61 | l <- vector (n*n) | ||
62 | return $ Sq $ (n><n) l | ||
63 | coarbitrary = undefined | ||
64 | |||
65 | newtype (Rot a) = Rot (Matrix a) deriving Show | ||
66 | rot (Rot a) = a | ||
67 | |||
68 | instance (Field a, Arbitrary a) => Arbitrary (Rot a) where | ||
69 | arbitrary = do | ||
70 | Sq m <- arbitrary | ||
71 | let (q,_) = qr m | ||
72 | return (Rot q) | ||
73 | coarbitrary = undefined | ||
74 | |||
75 | newtype (Her a) = Her (Matrix a) deriving Show | ||
76 | her (Her a) = a | ||
77 | |||
78 | instance (Field a, Arbitrary a) => Arbitrary (Her a) where | ||
79 | arbitrary = do | ||
80 | Sq m <- arbitrary | ||
81 | let m' = m/2 | ||
82 | return $ Her (m' + ctrans m') | ||
83 | coarbitrary = undefined | ||
84 | |||
85 | ------------------------------------------------------------------- | ||
86 | |||
87 | herR x = her x :: RM | ||
88 | |||
89 | -- | It runs all the tests. | ||
90 | runTests :: Int -- ^ maximum dimension | ||
91 | -> IO () | ||
92 | runTests n = do | ||
93 | qCheck n (\(Her (m::CM))-> m == ctrans m) | ||
94 | qCheck n $ (\m->m==ctrans m) . herR | ||
95 | |||
96 | -- | Some additional tests on big matrices. They take a few minutes. | ||
97 | runBigTests :: IO () | ||
98 | runBigTests = undefined | ||