diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-08-31 16:52:26 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-08-31 16:52:26 +0000 |
commit | 4486e93da02c7ef9e1fdf785c88f78986048c332 (patch) | |
tree | c0d84fce23a39a307fd12041fdd570be93aca15d /lib/Numeric/LinearAlgebra/Tests | |
parent | 0b48e6b34a1a4ec590f2d17833f713f42f5e0955 (diff) |
refactoring norms
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Tests')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests/Instances.hs | 20 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests/Properties.hs | 8 |
2 files changed, 13 insertions, 15 deletions
diff --git a/lib/Numeric/LinearAlgebra/Tests/Instances.hs b/lib/Numeric/LinearAlgebra/Tests/Instances.hs index aaaff28..21a6f88 100644 --- a/lib/Numeric/LinearAlgebra/Tests/Instances.hs +++ b/lib/Numeric/LinearAlgebra/Tests/Instances.hs | |||
@@ -27,13 +27,10 @@ module Numeric.LinearAlgebra.Tests.Instances( | |||
27 | ) where | 27 | ) where |
28 | 28 | ||
29 | 29 | ||
30 | import Numeric.LinearAlgebra hiding (real,complex) | 30 | import Numeric.LinearAlgebra |
31 | import Control.Monad(replicateM) | 31 | import Control.Monad(replicateM) |
32 | #include "quickCheckCompat.h" | 32 | #include "quickCheckCompat.h" |
33 | 33 | ||
34 | real x = real'' x | ||
35 | complex x = complex'' x | ||
36 | |||
37 | #if MIN_VERSION_QuickCheck(2,0,0) | 34 | #if MIN_VERSION_QuickCheck(2,0,0) |
38 | shrinkListElementwise :: (Arbitrary a) => [a] -> [[a]] | 35 | shrinkListElementwise :: (Arbitrary a) => [a] -> [[a]] |
39 | shrinkListElementwise [] = [] | 36 | shrinkListElementwise [] = [] |
@@ -72,7 +69,7 @@ instance (Field a, Arbitrary a) => Arbitrary (Vector a) where | |||
72 | #if MIN_VERSION_QuickCheck(2,0,0) | 69 | #if MIN_VERSION_QuickCheck(2,0,0) |
73 | -- shrink any one of the components | 70 | -- shrink any one of the components |
74 | shrink = map fromList . shrinkListElementwise . toList | 71 | shrink = map fromList . shrinkListElementwise . toList |
75 | 72 | ||
76 | #else | 73 | #else |
77 | coarbitrary = undefined | 74 | coarbitrary = undefined |
78 | #endif | 75 | #endif |
@@ -140,7 +137,7 @@ instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (Her a) where | |||
140 | 137 | ||
141 | -- a well-conditioned general matrix (the singular values are between 1 and 100) | 138 | -- a well-conditioned general matrix (the singular values are between 1 and 100) |
142 | newtype (WC a) = WC (Matrix a) deriving Show | 139 | newtype (WC a) = WC (Matrix a) deriving Show |
143 | instance (Field a, Arbitrary a) => Arbitrary (WC a) where | 140 | instance (AutoReal a, Field a, Arbitrary a) => Arbitrary (WC a) where |
144 | arbitrary = do | 141 | arbitrary = do |
145 | m <- arbitrary | 142 | m <- arbitrary |
146 | let (u,_,v) = svd m | 143 | let (u,_,v) = svd m |
@@ -149,7 +146,7 @@ instance (Field a, Arbitrary a) => Arbitrary (WC a) where | |||
149 | n = min r c | 146 | n = min r c |
150 | sv' <- replicateM n (choose (1,100)) | 147 | sv' <- replicateM n (choose (1,100)) |
151 | let s = diagRect (fromList sv') r c | 148 | let s = diagRect (fromList sv') r c |
152 | return $ WC (u <> real s <> trans v) | 149 | return $ WC (u <> real'' s <> trans v) |
153 | 150 | ||
154 | #if MIN_VERSION_QuickCheck(2,0,0) | 151 | #if MIN_VERSION_QuickCheck(2,0,0) |
155 | #else | 152 | #else |
@@ -159,14 +156,14 @@ instance (Field a, Arbitrary a) => Arbitrary (WC a) where | |||
159 | 156 | ||
160 | -- a well-conditioned square matrix (the singular values are between 1 and 100) | 157 | -- a well-conditioned square matrix (the singular values are between 1 and 100) |
161 | newtype (SqWC a) = SqWC (Matrix a) deriving Show | 158 | newtype (SqWC a) = SqWC (Matrix a) deriving Show |
162 | instance (Field a, Arbitrary a) => Arbitrary (SqWC a) where | 159 | instance (AutoReal a, Field a, Arbitrary a) => Arbitrary (SqWC a) where |
163 | arbitrary = do | 160 | arbitrary = do |
164 | Sq m <- arbitrary | 161 | Sq m <- arbitrary |
165 | let (u,_,v) = svd m | 162 | let (u,_,v) = svd m |
166 | n = rows m | 163 | n = rows m |
167 | sv' <- replicateM n (choose (1,100)) | 164 | sv' <- replicateM n (choose (1,100)) |
168 | let s = diag (fromList sv') | 165 | let s = diag (fromList sv') |
169 | return $ SqWC (u <> real s <> trans v) | 166 | return $ SqWC (u <> real'' s <> trans v) |
170 | 167 | ||
171 | #if MIN_VERSION_QuickCheck(2,0,0) | 168 | #if MIN_VERSION_QuickCheck(2,0,0) |
172 | #else | 169 | #else |
@@ -176,14 +173,14 @@ instance (Field a, Arbitrary a) => Arbitrary (SqWC a) where | |||
176 | 173 | ||
177 | -- a positive definite square matrix (the eigenvalues are between 0 and 100) | 174 | -- a positive definite square matrix (the eigenvalues are between 0 and 100) |
178 | newtype (PosDef a) = PosDef (Matrix a) deriving Show | 175 | newtype (PosDef a) = PosDef (Matrix a) deriving Show |
179 | instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (PosDef a) where | 176 | instance (AutoReal a, Field a, Arbitrary a, Num (Vector a)) => Arbitrary (PosDef a) where |
180 | arbitrary = do | 177 | arbitrary = do |
181 | Her m <- arbitrary | 178 | Her m <- arbitrary |
182 | let (_,v) = eigSH m | 179 | let (_,v) = eigSH m |
183 | n = rows m | 180 | n = rows m |
184 | l <- replicateM n (choose (0,100)) | 181 | l <- replicateM n (choose (0,100)) |
185 | let s = diag (fromList l) | 182 | let s = diag (fromList l) |
186 | p = v <> real s <> ctrans v | 183 | p = v <> real'' s <> ctrans v |
187 | return $ PosDef (0.5 * p + 0.5 * ctrans p) | 184 | return $ PosDef (0.5 * p + 0.5 * ctrans p) |
188 | 185 | ||
189 | #if MIN_VERSION_QuickCheck(2,0,0) | 186 | #if MIN_VERSION_QuickCheck(2,0,0) |
@@ -243,3 +240,4 @@ cPosDef (PosDef m) = m :: CM | |||
243 | 240 | ||
244 | rConsist (Consistent (a,b)) = (a,b::RM) | 241 | rConsist (Consistent (a,b)) = (a,b::RM) |
245 | cConsist (Consistent (a,b)) = (a,b::CM) | 242 | cConsist (Consistent (a,b)) = (a,b::CM) |
243 | |||
diff --git a/lib/Numeric/LinearAlgebra/Tests/Properties.hs b/lib/Numeric/LinearAlgebra/Tests/Properties.hs index 9891d8a..d6bb338 100644 --- a/lib/Numeric/LinearAlgebra/Tests/Properties.hs +++ b/lib/Numeric/LinearAlgebra/Tests/Properties.hs | |||
@@ -54,15 +54,15 @@ complex x = complex'' x | |||
54 | debug x = trace (show x) x | 54 | debug x = trace (show x) x |
55 | 55 | ||
56 | -- relative error | 56 | -- relative error |
57 | dist :: (Normed t, Num t) => t -> t -> Double | 57 | --dist :: (Normed t, Num t) => t -> t -> Double |
58 | dist a b = r | 58 | dist a b = r |
59 | where norm = pnorm Infinity | 59 | where norm = normInf |
60 | na = norm a | 60 | na = norm a |
61 | nb = norm b | 61 | nb = norm b |
62 | nab = norm (a-b) | 62 | nab = norm (a-b) |
63 | mx = max na nb | 63 | mx = max na nb |
64 | mn = min na nb | 64 | mn = min na nb |
65 | r = if mn < eps | 65 | r = if mn < peps |
66 | then mx | 66 | then mx |
67 | else nab/mx | 67 | else nab/mx |
68 | 68 | ||
@@ -71,7 +71,7 @@ a |~| b = a :~10~: b | |||
71 | --a |~| b = dist a b < 10^^(-10) | 71 | --a |~| b = dist a b < 10^^(-10) |
72 | 72 | ||
73 | data Aprox a = (:~) a Int | 73 | data Aprox a = (:~) a Int |
74 | (~:) :: (Normed a, Num a) => Aprox a -> a -> Bool | 74 | -- (~:) :: (Normed a, Num a) => Aprox a -> a -> Bool |
75 | a :~n~: b = dist a b < 10^^(-n) | 75 | a :~n~: b = dist a b < 10^^(-n) |
76 | 76 | ||
77 | ------------------------------------------------------ | 77 | ------------------------------------------------------ |