summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2008-01-28 19:31:59 +0000
committerAlberto Ruiz <aruiz@um.es>2008-01-28 19:31:59 +0000
commit240ae9be06380814fc1e223c3c53c746e5b1e6ba (patch)
tree7d69620b120fe8fe5552f6a586422d24921b2f0c
parent003b8db7a864bbea3772cd70906153bd36d1f7ac (diff)
added the Testing module (and minor changes in instance definitions)
-rw-r--r--hmatrix.cabal11
-rw-r--r--lib/Numeric/LinearAlgebra/Algorithms.hs2
-rw-r--r--lib/Numeric/LinearAlgebra/Linear.hs15
-rw-r--r--lib/Numeric/LinearAlgebra/Testing.hs98
4 files changed, 107 insertions, 19 deletions
diff --git a/hmatrix.cabal b/hmatrix.cabal
index 049036c..17aba9e 100644
--- a/hmatrix.cabal
+++ b/hmatrix.cabal
@@ -1,5 +1,5 @@
1Name: hmatrix 1Name: hmatrix
2Version: 0.2.0.1 2Version: 0.2.0.0
3License: GPL 3License: GPL
4License-file: LICENSE 4License-file: LICENSE
5Author: Alberto Ruiz 5Author: Alberto Ruiz
@@ -13,7 +13,7 @@ Description: A purely functional interface to basic linear algebra comput
13 . 13 .
14 More information: <http://alberrto.googlepages.com/gslhaskell> 14 More information: <http://alberrto.googlepages.com/gslhaskell>
15Category: Numerical, Math 15Category: Numerical, Math
16tested-with: GHC ==6.6.1, GHC ==6.8.1, GHC ==6.8.2 16tested-with: GHC ==6.8.2
17 17
18cabal-version: >=1.2 18cabal-version: >=1.2
19build-type: Simple 19build-type: Simple
@@ -23,9 +23,9 @@ flag splitBase
23 23
24library 24library
25 if flag(splitBase) 25 if flag(splitBase)
26 build-depends: base >= 3, array, storable-complex 26 build-depends: base >= 3, array
27 else 27 else
28 build-depends: base < 3, storable-complex 28 build-depends: base < 3
29 29
30 ghc-options: -O 30 ghc-options: -O
31 31
@@ -85,7 +85,8 @@ library
85 Numeric.LinearAlgebra.Instances, 85 Numeric.LinearAlgebra.Instances,
86 Numeric.LinearAlgebra.Interface, 86 Numeric.LinearAlgebra.Interface,
87 Numeric.LinearAlgebra.Algorithms, 87 Numeric.LinearAlgebra.Algorithms,
88 Graphics.Plot 88 Graphics.Plot,
89 Numeric.LinearAlgebra.Testing
89 other-modules: Data.Packed.Internal, 90 other-modules: Data.Packed.Internal,
90 Data.Packed.Internal.Common, 91 Data.Packed.Internal.Common,
91 Data.Packed.Internal.Vector, 92 Data.Packed.Internal.Vector,
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