diff options
Diffstat (limited to 'lib/Numeric')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests/Instances.hs (renamed from lib/Numeric/LinearAlgebra/Testing.hs) | 44 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests/Properties.hs | 50 |
2 files changed, 58 insertions, 36 deletions
diff --git a/lib/Numeric/LinearAlgebra/Testing.hs b/lib/Numeric/LinearAlgebra/Tests/Instances.hs index dcf1d8e..fececdb 100644 --- a/lib/Numeric/LinearAlgebra/Testing.hs +++ b/lib/Numeric/LinearAlgebra/Tests/Instances.hs | |||
@@ -1,32 +1,26 @@ | |||
1 | {-# OPTIONS_GHC -XPatternSignatures #-} | 1 | {-# OPTIONS #-} |
2 | ----------------------------------------------------------------------------- | 2 | ----------------------------------------------------------------------------- |
3 | {- | | 3 | {- | |
4 | Module : Numeric.LinearAlgebra.Testing | 4 | Module : Numeric.LinearAlgebra.Tests.Instances |
5 | Copyright : (c) Alberto Ruiz 2007 | 5 | Copyright : (c) Alberto Ruiz 2008 |
6 | License : GPL-style | 6 | License : GPL-style |
7 | 7 | ||
8 | Maintainer : Alberto Ruiz (aruiz at um dot es) | 8 | Maintainer : Alberto Ruiz (aruiz at um dot es) |
9 | Stability : provisional | 9 | Stability : provisional |
10 | Portability : portable | 10 | Portability : portable |
11 | 11 | ||
12 | Some consistency tests. | 12 | Arbitrary instances for vectors, matrices. |
13 | 13 | ||
14 | -} | 14 | -} |
15 | 15 | ||
16 | module Numeric.LinearAlgebra.Testing( | 16 | module Numeric.LinearAlgebra.Tests.Instances( |
17 | runTests, runBigTests | 17 | Sq(..), |
18 | Rot(..), | ||
19 | Her(..) | ||
18 | ) where | 20 | ) where |
19 | 21 | ||
20 | import Numeric.LinearAlgebra | 22 | import Numeric.LinearAlgebra |
21 | import Test.QuickCheck | 23 | 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 | 24 | ||
31 | instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where | 25 | instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where |
32 | arbitrary = do | 26 | arbitrary = do |
@@ -51,10 +45,7 @@ instance (Element a, Arbitrary a) => Arbitrary (Matrix a) where | |||
51 | return $ (m><n) l | 45 | return $ (m><n) l |
52 | coarbitrary = undefined | 46 | coarbitrary = undefined |
53 | 47 | ||
54 | |||
55 | newtype (Sq a) = Sq (Matrix a) deriving Show | 48 | newtype (Sq a) = Sq (Matrix a) deriving Show |
56 | sq (Sq m) = m | ||
57 | |||
58 | instance (Element a, Arbitrary a) => Arbitrary (Sq a) where | 49 | instance (Element a, Arbitrary a) => Arbitrary (Sq a) where |
59 | arbitrary = do | 50 | arbitrary = do |
60 | n <- chooseDim | 51 | n <- chooseDim |
@@ -63,8 +54,6 @@ instance (Element a, Arbitrary a) => Arbitrary (Sq a) where | |||
63 | coarbitrary = undefined | 54 | coarbitrary = undefined |
64 | 55 | ||
65 | newtype (Rot a) = Rot (Matrix a) deriving Show | 56 | newtype (Rot a) = Rot (Matrix a) deriving Show |
66 | rot (Rot a) = a | ||
67 | |||
68 | instance (Field a, Arbitrary a) => Arbitrary (Rot a) where | 57 | instance (Field a, Arbitrary a) => Arbitrary (Rot a) where |
69 | arbitrary = do | 58 | arbitrary = do |
70 | Sq m <- arbitrary | 59 | Sq m <- arbitrary |
@@ -73,26 +62,9 @@ instance (Field a, Arbitrary a) => Arbitrary (Rot a) where | |||
73 | coarbitrary = undefined | 62 | coarbitrary = undefined |
74 | 63 | ||
75 | newtype (Her a) = Her (Matrix a) deriving Show | 64 | newtype (Her a) = Her (Matrix a) deriving Show |
76 | her (Her a) = a | ||
77 | |||
78 | instance (Field a, Arbitrary a) => Arbitrary (Her a) where | 65 | instance (Field a, Arbitrary a) => Arbitrary (Her a) where |
79 | arbitrary = do | 66 | arbitrary = do |
80 | Sq m <- arbitrary | 67 | Sq m <- arbitrary |
81 | let m' = m/2 | 68 | let m' = m/2 |
82 | return $ Her (m' + ctrans m') | 69 | return $ Her (m' + ctrans m') |
83 | coarbitrary = undefined | 70 | 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 | ||
diff --git a/lib/Numeric/LinearAlgebra/Tests/Properties.hs b/lib/Numeric/LinearAlgebra/Tests/Properties.hs new file mode 100644 index 0000000..ad31454 --- /dev/null +++ b/lib/Numeric/LinearAlgebra/Tests/Properties.hs | |||
@@ -0,0 +1,50 @@ | |||
1 | {-# OPTIONS #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : Numeric.LinearAlgebra.Tests.Properties | ||
5 | Copyright : (c) Alberto Ruiz 2008 | ||
6 | License : GPL-style | ||
7 | |||
8 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
9 | Stability : provisional | ||
10 | Portability : portable | ||
11 | |||
12 | Arbitrary instances for vectors, matrices. | ||
13 | |||
14 | -} | ||
15 | |||
16 | module Numeric.LinearAlgebra.Tests.Properties | ||
17 | |||
18 | where | ||
19 | |||
20 | import Numeric.LinearAlgebra | ||
21 | import Numeric.LinearAlgebra.Tests.Instances(Sq(..),Her(..),Rot(..)) | ||
22 | |||
23 | -- relative error | ||
24 | dist :: (Normed t, Num t) => t -> t -> Double | ||
25 | dist a b = r | ||
26 | where norm = pnorm Infinity | ||
27 | na = norm a | ||
28 | nb = norm b | ||
29 | nab = norm (a-b) | ||
30 | mx = max na nb | ||
31 | mn = min na nb | ||
32 | r = if mn < eps | ||
33 | then mx | ||
34 | else nab/mx | ||
35 | |||
36 | infixl 4 |~| | ||
37 | --a |~| b = a :~10~: b | ||
38 | a |~| b = dist a b < 10^^(-10) | ||
39 | |||
40 | --data Aprox a = (:~) a Int | ||
41 | --(~:) :: (Normed a, Num a) => Aprox a -> a -> Bool | ||
42 | --a :~n~: b = dist a b < 10^^(-n) | ||
43 | |||
44 | ------------------------------------------------------ | ||
45 | |||
46 | square m = rows m == cols m | ||
47 | |||
48 | unitary m = square m && m <> ctrans m |~| ident (rows m) | ||
49 | |||
50 | hermitian m = square m && m |~| ctrans m | ||