diff options
author | Alberto Ruiz <aruiz@um.es> | 2008-02-03 12:52:17 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2008-02-03 12:52:17 +0000 |
commit | 2747c2ba403dafcc52bac09d2fd51b6d66fa5e9e (patch) | |
tree | 8115445612f0c43f67649e9ed701e1d483bda9da /lib/Numeric/LinearAlgebra/Tests | |
parent | bac0db0bb1a25aa8be87a79492517aa8098670e6 (diff) |
test modules reorganized
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Tests')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests/Instances.hs | 70 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests/Properties.hs | 50 |
2 files changed, 120 insertions, 0 deletions
diff --git a/lib/Numeric/LinearAlgebra/Tests/Instances.hs b/lib/Numeric/LinearAlgebra/Tests/Instances.hs new file mode 100644 index 0000000..fececdb --- /dev/null +++ b/lib/Numeric/LinearAlgebra/Tests/Instances.hs | |||
@@ -0,0 +1,70 @@ | |||
1 | {-# OPTIONS #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : Numeric.LinearAlgebra.Tests.Instances | ||
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.Instances( | ||
17 | Sq(..), | ||
18 | Rot(..), | ||
19 | Her(..) | ||
20 | ) where | ||
21 | |||
22 | import Numeric.LinearAlgebra | ||
23 | import Test.QuickCheck | ||
24 | |||
25 | instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where | ||
26 | arbitrary = do | ||
27 | r <- arbitrary | ||
28 | i <- arbitrary | ||
29 | return (r:+i) | ||
30 | coarbitrary = undefined | ||
31 | |||
32 | chooseDim = sized $ \m -> choose (1,max 1 m) | ||
33 | |||
34 | instance (Field a, Arbitrary a) => Arbitrary (Vector a) where | ||
35 | arbitrary = do m <- chooseDim | ||
36 | l <- vector m | ||
37 | return $ fromList l | ||
38 | coarbitrary = undefined | ||
39 | |||
40 | instance (Element a, Arbitrary a) => Arbitrary (Matrix a) where | ||
41 | arbitrary = do | ||
42 | m <- chooseDim | ||
43 | n <- chooseDim | ||
44 | l <- vector (m*n) | ||
45 | return $ (m><n) l | ||
46 | coarbitrary = undefined | ||
47 | |||
48 | newtype (Sq a) = Sq (Matrix a) deriving Show | ||
49 | instance (Element a, Arbitrary a) => Arbitrary (Sq a) where | ||
50 | arbitrary = do | ||
51 | n <- chooseDim | ||
52 | l <- vector (n*n) | ||
53 | return $ Sq $ (n><n) l | ||
54 | coarbitrary = undefined | ||
55 | |||
56 | newtype (Rot a) = Rot (Matrix a) deriving Show | ||
57 | instance (Field a, Arbitrary a) => Arbitrary (Rot a) where | ||
58 | arbitrary = do | ||
59 | Sq m <- arbitrary | ||
60 | let (q,_) = qr m | ||
61 | return (Rot q) | ||
62 | coarbitrary = undefined | ||
63 | |||
64 | newtype (Her a) = Her (Matrix a) deriving Show | ||
65 | instance (Field a, Arbitrary a) => Arbitrary (Her a) where | ||
66 | arbitrary = do | ||
67 | Sq m <- arbitrary | ||
68 | let m' = m/2 | ||
69 | return $ Her (m' + ctrans m') | ||
70 | coarbitrary = 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 | ||