summaryrefslogtreecommitdiff
path: root/lib/Numeric
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric')
-rw-r--r--lib/Numeric/LinearAlgebra/Tests/Instances.hs21
-rw-r--r--lib/Numeric/LinearAlgebra/Tests/Properties.hs14
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.
16module Numeric.LinearAlgebra.Tests.Instances( 16module Numeric.LinearAlgebra.Tests.Instances(
17 Sq(..), 17 Sq(..),
18 Rot(..), 18 Rot(..),
19 Her(..) 19 Her(..),
20 WC(..)
20) where 21) where
21 22
22import Numeric.LinearAlgebra 23import Numeric.LinearAlgebra
23import Test.QuickCheck 24import Test.QuickCheck
25import Control.Monad(replicateM)
24 26
25instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where 27instance (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
48newtype (Sq a) = Sq (Matrix a) deriving Show 51newtype (Sq a) = Sq (Matrix a) deriving Show
49instance (Element a, Arbitrary a) => Arbitrary (Sq a) where 52instance (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
56newtype (Rot a) = Rot (Matrix a) deriving Show 60newtype (Rot a) = Rot (Matrix a) deriving Show
57instance (Field a, Arbitrary a) => Arbitrary (Rot a) where 61instance (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
64newtype (Her a) = Her (Matrix a) deriving Show 69newtype (Her a) = Her (Matrix a) deriving Show
65instance (Field a, Arbitrary a) => Arbitrary (Her a) where 70instance (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)
78newtype (WC a) = WC (Matrix a) deriving Show
79instance (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
36infixl 4 |~| 36infixl 4 |~|
37--a |~| b = a :~10~: b 37a |~| b = a :~10~: b
38a |~| b = dist a b < 10^^(-10) 38--a |~| b = dist a b < 10^^(-10)
39 39
40--data Aprox a = (:~) a Int 40data 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) 42a :~n~: b = dist a b < 10^^(-n)
43 43
44------------------------------------------------------ 44------------------------------------------------------
45 45
@@ -48,3 +48,7 @@ square m = rows m == cols m
48unitary m = square m && m <> ctrans m |~| ident (rows m) 48unitary m = square m && m <> ctrans m |~| ident (rows m)
49 49
50hermitian m = square m && m |~| ctrans m 50hermitian m = square m && m |~| ctrans m
51
52degenerate m = rank m < min (rows m) (cols m)
53
54wellCond m = rcond m > 1/100