summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/LinearAlgebra')
-rw-r--r--lib/Numeric/LinearAlgebra/Algorithms.hs2
-rw-r--r--lib/Numeric/LinearAlgebra/Linear.hs15
-rw-r--r--lib/Numeric/LinearAlgebra/Testing.hs98
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')
69import Data.Array 69import 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.
72class (Normed (Matrix t), Linear Matrix t) => Field t where 72class (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{- |
4Module : Numeric.LinearAlgebra.Linear 4Module : 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
63instance Linear Matrix Double where 63instance (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
74instance 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{- |
4Module : Numeric.LinearAlgebra.Testing
5Copyright : (c) Alberto Ruiz 2007
6License : GPL-style
7
8Maintainer : Alberto Ruiz (aruiz at um dot es)
9Stability : provisional
10Portability : portable
11
12Some consistency tests.
13
14-}
15
16module Numeric.LinearAlgebra.Testing(
17 runTests, runBigTests
18) where
19
20import Numeric.LinearAlgebra
21import Test.QuickCheck
22import Debug.Trace
23
24qCheck n = check defaultConfig {configSize = const n}
25
26debug x = trace (show x) x
27
28type RM = Matrix Double
29type CM = Matrix (Complex Double)
30
31instance (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
38chooseDim = sized $ \m -> choose (1,max 1 m)
39
40instance (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
46instance (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
55newtype (Sq a) = Sq (Matrix a) deriving Show
56sq (Sq m) = m
57
58instance (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
65newtype (Rot a) = Rot (Matrix a) deriving Show
66rot (Rot a) = a
67
68instance (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
75newtype (Her a) = Her (Matrix a) deriving Show
76her (Her a) = a
77
78instance (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
87herR x = her x :: RM
88
89-- | It runs all the tests.
90runTests :: Int -- ^ maximum dimension
91 -> IO ()
92runTests 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.
97runBigTests :: IO ()
98runBigTests = undefined