diff options
Diffstat (limited to 'lib/Numeric')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests/Instances.hs | 21 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests/Properties.hs | 14 |
2 files changed, 29 insertions, 6 deletions
diff --git a/lib/Numeric/LinearAlgebra/Tests/Instances.hs b/lib/Numeric/LinearAlgebra/Tests/Instances.hs index fececdb..583143a 100644 --- a/lib/Numeric/LinearAlgebra/Tests/Instances.hs +++ b/lib/Numeric/LinearAlgebra/Tests/Instances.hs | |||
@@ -16,11 +16,13 @@ Arbitrary instances for vectors, matrices. | |||
16 | module Numeric.LinearAlgebra.Tests.Instances( | 16 | module Numeric.LinearAlgebra.Tests.Instances( |
17 | Sq(..), | 17 | Sq(..), |
18 | Rot(..), | 18 | Rot(..), |
19 | Her(..) | 19 | Her(..), |
20 | WC(..) | ||
20 | ) where | 21 | ) where |
21 | 22 | ||
22 | import Numeric.LinearAlgebra | 23 | import Numeric.LinearAlgebra |
23 | import Test.QuickCheck | 24 | import Test.QuickCheck |
25 | import Control.Monad(replicateM) | ||
24 | 26 | ||
25 | instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where | 27 | instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where |
26 | arbitrary = do | 28 | arbitrary = do |
@@ -45,6 +47,7 @@ instance (Element a, Arbitrary a) => Arbitrary (Matrix a) where | |||
45 | return $ (m><n) l | 47 | return $ (m><n) l |
46 | coarbitrary = undefined | 48 | coarbitrary = undefined |
47 | 49 | ||
50 | -- a square matrix | ||
48 | newtype (Sq a) = Sq (Matrix a) deriving Show | 51 | newtype (Sq a) = Sq (Matrix a) deriving Show |
49 | instance (Element a, Arbitrary a) => Arbitrary (Sq a) where | 52 | instance (Element a, Arbitrary a) => Arbitrary (Sq a) where |
50 | arbitrary = do | 53 | arbitrary = do |
@@ -53,6 +56,7 @@ instance (Element a, Arbitrary a) => Arbitrary (Sq a) where | |||
53 | return $ Sq $ (n><n) l | 56 | return $ Sq $ (n><n) l |
54 | coarbitrary = undefined | 57 | coarbitrary = undefined |
55 | 58 | ||
59 | -- a unitary matrix | ||
56 | newtype (Rot a) = Rot (Matrix a) deriving Show | 60 | newtype (Rot a) = Rot (Matrix a) deriving Show |
57 | instance (Field a, Arbitrary a) => Arbitrary (Rot a) where | 61 | instance (Field a, Arbitrary a) => Arbitrary (Rot a) where |
58 | arbitrary = do | 62 | arbitrary = do |
@@ -61,6 +65,7 @@ instance (Field a, Arbitrary a) => Arbitrary (Rot a) where | |||
61 | return (Rot q) | 65 | return (Rot q) |
62 | coarbitrary = undefined | 66 | coarbitrary = undefined |
63 | 67 | ||
68 | -- a complex hermitian or real symmetric matrix | ||
64 | newtype (Her a) = Her (Matrix a) deriving Show | 69 | newtype (Her a) = Her (Matrix a) deriving Show |
65 | instance (Field a, Arbitrary a) => Arbitrary (Her a) where | 70 | instance (Field a, Arbitrary a) => Arbitrary (Her a) where |
66 | arbitrary = do | 71 | arbitrary = do |
@@ -68,3 +73,17 @@ instance (Field a, Arbitrary a) => Arbitrary (Her a) where | |||
68 | let m' = m/2 | 73 | let m' = m/2 |
69 | return $ Her (m' + ctrans m') | 74 | return $ Her (m' + ctrans m') |
70 | coarbitrary = undefined | 75 | coarbitrary = undefined |
76 | |||
77 | -- a well-conditioned matrix (the singular values are between 1 and 100) | ||
78 | newtype (WC a) = WC (Matrix a) deriving Show | ||
79 | instance (Field a, Arbitrary a) => Arbitrary (WC a) where | ||
80 | arbitrary = do | ||
81 | m <- arbitrary | ||
82 | let (u,_,v) = svd m | ||
83 | r = rows m | ||
84 | c = cols m | ||
85 | n = min r c | ||
86 | sv <- replicateM n (choose (1,100)) | ||
87 | let s = diagRect (fromList sv) r c | ||
88 | return $ WC (u <> real s <> trans v) | ||
89 | coarbitrary = undefined | ||
diff --git a/lib/Numeric/LinearAlgebra/Tests/Properties.hs b/lib/Numeric/LinearAlgebra/Tests/Properties.hs index ad31454..6686b97 100644 --- a/lib/Numeric/LinearAlgebra/Tests/Properties.hs +++ b/lib/Numeric/LinearAlgebra/Tests/Properties.hs | |||
@@ -34,12 +34,12 @@ dist a b = r | |||
34 | else nab/mx | 34 | else nab/mx |
35 | 35 | ||
36 | infixl 4 |~| | 36 | infixl 4 |~| |
37 | --a |~| b = a :~10~: b | 37 | a |~| b = a :~10~: b |
38 | a |~| b = dist a b < 10^^(-10) | 38 | --a |~| b = dist a b < 10^^(-10) |
39 | 39 | ||
40 | --data Aprox a = (:~) a Int | 40 | data Aprox a = (:~) a Int |
41 | --(~:) :: (Normed a, Num a) => Aprox a -> a -> Bool | 41 | (~:) :: (Normed a, Num a) => Aprox a -> a -> Bool |
42 | --a :~n~: b = dist a b < 10^^(-n) | 42 | a :~n~: b = dist a b < 10^^(-n) |
43 | 43 | ||
44 | ------------------------------------------------------ | 44 | ------------------------------------------------------ |
45 | 45 | ||
@@ -48,3 +48,7 @@ square m = rows m == cols m | |||
48 | unitary m = square m && m <> ctrans m |~| ident (rows m) | 48 | unitary m = square m && m <> ctrans m |~| ident (rows m) |
49 | 49 | ||
50 | hermitian m = square m && m |~| ctrans m | 50 | hermitian m = square m && m |~| ctrans m |
51 | |||
52 | degenerate m = rank m < min (rows m) (cols m) | ||
53 | |||
54 | wellCond m = rcond m > 1/100 | ||