diff options
Diffstat (limited to 'lib/Numeric')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Algorithms.hs | 2 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 15 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Testing.hs | 98 |
3 files changed, 101 insertions, 14 deletions
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs index b19c0ec..79cc64d 100644 --- a/lib/Numeric/LinearAlgebra/Algorithms.hs +++ b/lib/Numeric/LinearAlgebra/Algorithms.hs | |||
@@ -69,7 +69,7 @@ import Data.List(foldl1') | |||
69 | import Data.Array | 69 | import Data.Array |
70 | 70 | ||
71 | -- | Auxiliary typeclass used to define generic computations for both real and complex matrices. | 71 | -- | Auxiliary typeclass used to define generic computations for both real and complex matrices. |
72 | class (Normed (Matrix t), Linear Matrix t) => Field t where | 72 | class (Normed (Matrix t), Linear Vector t, Linear Matrix t) => Field t where |
73 | -- | Singular value decomposition using lapack's dgesvd or zgesvd. | 73 | -- | Singular value decomposition using lapack's dgesvd or zgesvd. |
74 | svd :: Matrix t -> (Matrix t, Vector Double, Matrix t) | 74 | svd :: Matrix t -> (Matrix t, Vector Double, Matrix t) |
75 | luPacked :: Matrix t -> (Matrix t, [Int]) | 75 | luPacked :: Matrix t -> (Matrix t, [Int]) |
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs index a39df50..0ddbb55 100644 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ b/lib/Numeric/LinearAlgebra/Linear.hs | |||
@@ -1,4 +1,4 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | 1 | {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} |
2 | ----------------------------------------------------------------------------- | 2 | ----------------------------------------------------------------------------- |
3 | {- | | 3 | {- | |
4 | Module : Numeric.LinearAlgebra.Linear | 4 | Module : Numeric.LinearAlgebra.Linear |
@@ -60,18 +60,7 @@ instance Linear Vector (Complex Double) where | |||
60 | divide = vectorZipC Div | 60 | divide = vectorZipC Div |
61 | equal u v = dim u == dim v && vectorMax (liftVector magnitude (sub u v)) == 0.0 | 61 | equal u v = dim u == dim v && vectorMax (liftVector magnitude (sub u v)) == 0.0 |
62 | 62 | ||
63 | instance Linear Matrix Double where | 63 | instance (Linear Vector a, Container Matrix a) => (Linear Matrix a) where |
64 | scale x = liftMatrix (scale x) | ||
65 | scaleRecip x = liftMatrix (scaleRecip x) | ||
66 | addConstant x = liftMatrix (addConstant x) | ||
67 | add = liftMatrix2 add | ||
68 | sub = liftMatrix2 sub | ||
69 | mul = liftMatrix2 mul | ||
70 | divide = liftMatrix2 divide | ||
71 | equal a b = cols a == cols b && flatten a `equal` flatten b | ||
72 | |||
73 | |||
74 | instance Linear Matrix (Complex Double) where | ||
75 | scale x = liftMatrix (scale x) | 64 | scale x = liftMatrix (scale x) |
76 | scaleRecip x = liftMatrix (scaleRecip x) | 65 | scaleRecip x = liftMatrix (scaleRecip x) |
77 | addConstant x = liftMatrix (addConstant x) | 66 | addConstant x = liftMatrix (addConstant x) |
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 | ||