summaryrefslogtreecommitdiff
path: root/packages/tests/src/Numeric
diff options
context:
space:
mode:
Diffstat (limited to 'packages/tests/src/Numeric')
-rw-r--r--packages/tests/src/Numeric/LinearAlgebra/Tests.hs48
-rw-r--r--packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs18
-rw-r--r--packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs12
3 files changed, 41 insertions, 37 deletions
diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests.hs
index 2ff1580..30480d7 100644
--- a/packages/tests/src/Numeric/LinearAlgebra/Tests.hs
+++ b/packages/tests/src/Numeric/LinearAlgebra/Tests.hs
@@ -127,8 +127,8 @@ expmTest2 = expm nd2 :~15~: (2><2)
127mbCholTest = utest "mbCholTest" (ok1 && ok2) where 127mbCholTest = utest "mbCholTest" (ok1 && ok2) where
128 m1 = (2><2) [2,5,5,8 :: Double] 128 m1 = (2><2) [2,5,5,8 :: Double]
129 m2 = (2><2) [3,5,5,9 :: Complex Double] 129 m2 = (2><2) [3,5,5,9 :: Complex Double]
130 ok1 = mbCholSH m1 == Nothing 130 ok1 = mbChol (trustSym m1) == Nothing
131 ok2 = mbCholSH m2 == Just (chol m2) 131 ok2 = mbChol (trustSym m2) == Just (chol $ trustSym m2)
132 132
133--------------------------------------------------------------------- 133---------------------------------------------------------------------
134 134
@@ -403,8 +403,8 @@ indexProp g f x = a1 == g a2 && a2 == a3 && b1 == g b2 && b2 == b3
403-------------------------------------------------------------------------------- 403--------------------------------------------------------------------------------
404 404
405sliceTest = utest "slice test" $ and 405sliceTest = utest "slice test" $ and
406 [ testSlice chol (gen 5 :: Matrix R) 406 [ testSlice (chol . trustSym) (gen 5 :: Matrix R)
407 , testSlice chol (gen 5 :: Matrix C) 407 , testSlice (chol . trustSym) (gen 5 :: Matrix C)
408 , testSlice qr (rec :: Matrix R) 408 , testSlice qr (rec :: Matrix R)
409 , testSlice qr (rec :: Matrix C) 409 , testSlice qr (rec :: Matrix C)
410 , testSlice hess (agen 5 :: Matrix R) 410 , testSlice hess (agen 5 :: Matrix R)
@@ -420,12 +420,12 @@ sliceTest = utest "slice test" $ and
420 420
421 , testSlice eig (agen 5 :: Matrix R) 421 , testSlice eig (agen 5 :: Matrix R)
422 , testSlice eig (agen 5 :: Matrix C) 422 , testSlice eig (agen 5 :: Matrix C)
423 , testSlice eigSH (gen 5 :: Matrix R) 423 , testSlice (eigSH . trustSym) (gen 5 :: Matrix R)
424 , testSlice eigSH (gen 5 :: Matrix C) 424 , testSlice (eigSH . trustSym) (gen 5 :: Matrix C)
425 , testSlice eigenvalues (agen 5 :: Matrix R) 425 , testSlice eigenvalues (agen 5 :: Matrix R)
426 , testSlice eigenvalues (agen 5 :: Matrix C) 426 , testSlice eigenvalues (agen 5 :: Matrix C)
427 , testSlice eigenvaluesSH (gen 5 :: Matrix R) 427 , testSlice (eigenvaluesSH . trustSym) (gen 5 :: Matrix R)
428 , testSlice eigenvaluesSH (gen 5 :: Matrix C) 428 , testSlice (eigenvaluesSH . trustSym) (gen 5 :: Matrix C)
429 429
430 , testSlice svd (rec :: Matrix R) 430 , testSlice svd (rec :: Matrix R)
431 , testSlice thinSVD (rec :: Matrix R) 431 , testSlice thinSVD (rec :: Matrix R)
@@ -489,10 +489,10 @@ sliceTest = utest "slice test" $ and
489 , testSlice ((<>) (ogen 5:: Matrix (Z ./. 7))) (gen 5) 489 , testSlice ((<>) (ogen 5:: Matrix (Z ./. 7))) (gen 5)
490 , testSlice (flip (<>) (gen 5:: Matrix (Z ./. 7))) (ogen 5) 490 , testSlice (flip (<>) (gen 5:: Matrix (Z ./. 7))) (ogen 5)
491 491
492 , testSlice (flip cholSolve (agen 5:: Matrix R)) (chol $ gen 5) 492 , testSlice (flip cholSolve (agen 5:: Matrix R)) (chol $ trustSym $ gen 5)
493 , testSlice (flip cholSolve (agen 5:: Matrix C)) (chol $ gen 5) 493 , testSlice (flip cholSolve (agen 5:: Matrix C)) (chol $ trustSym $ gen 5)
494 , testSlice (cholSolve (chol $ gen 5:: Matrix R)) (agen 5) 494 , testSlice (cholSolve (chol $ trustSym $ gen 5:: Matrix R)) (agen 5)
495 , testSlice (cholSolve (chol $ gen 5:: Matrix C)) (agen 5) 495 , testSlice (cholSolve (chol $ trustSym $ gen 5:: Matrix C)) (agen 5)
496 496
497 , ok_qrgr (rec :: Matrix R) 497 , ok_qrgr (rec :: Matrix R)
498 , ok_qrgr (rec :: Matrix C) 498 , ok_qrgr (rec :: Matrix C)
@@ -515,8 +515,8 @@ sliceTest = utest "slice test" $ and
515 515
516 test_lus m = testSlice f lup 516 test_lus m = testSlice f lup
517 where 517 where
518 f x = luSolve (x,p) m 518 f x = luSolve (LU x p) m
519 (lup,p) = luPacked m 519 (LU lup p) = luPacked m
520 520
521 gen :: Numeric t => Int -> Matrix t 521 gen :: Numeric t => Int -> Matrix t
522 gen n = diagRect 1 (konst 5 n) n n 522 gen n = diagRect 1 (konst 5 n) n n
@@ -588,11 +588,11 @@ runTests n = do
588 test (linearSolveProp (luSolve.luPacked) . rSqWC) 588 test (linearSolveProp (luSolve.luPacked) . rSqWC)
589 test (linearSolveProp (luSolve.luPacked) . cSqWC) 589 test (linearSolveProp (luSolve.luPacked) . cSqWC)
590 putStrLn "------ ldlSolve" 590 putStrLn "------ ldlSolve"
591 test (linearSolveProp (ldlSolve.ldlPacked) . rSymWC) 591 test (linearSolvePropH (ldlSolve.ldlPacked) . rSymWC)
592 test (linearSolveProp (ldlSolve.ldlPacked) . cSymWC) 592 test (linearSolvePropH (ldlSolve.ldlPacked) . cSymWC)
593 putStrLn "------ cholSolve" 593 putStrLn "------ cholSolve"
594 test (linearSolveProp (cholSolve.chol) . rPosDef) 594 test (linearSolveProp (cholSolve.chol.trustSym) . rPosDef)
595 test (linearSolveProp (cholSolve.chol) . cPosDef) 595 test (linearSolveProp (cholSolve.chol.trustSym) . cPosDef)
596 putStrLn "------ luSolveLS" 596 putStrLn "------ luSolveLS"
597 test (linearSolveProp linearSolveLS . rSqWC) 597 test (linearSolveProp linearSolveLS . rSqWC)
598 test (linearSolveProp linearSolveLS . cSqWC) 598 test (linearSolveProp linearSolveLS . cSqWC)
@@ -865,8 +865,8 @@ eigBench = do
865 let m = reshape 1000 (randomVector 777 Uniform (1000*1000)) 865 let m = reshape 1000 (randomVector 777 Uniform (1000*1000))
866 s = m + tr m 866 s = m + tr m
867 m `seq` s `seq` putStrLn "" 867 m `seq` s `seq` putStrLn ""
868 time "eigenvalues symmetric 1000x1000" (eigenvaluesSH' m) 868 time "eigenvalues symmetric 1000x1000" (eigenvaluesSH (trustSym m))
869 time "eigenvectors symmetric 1000x1000" (snd $ eigSH' m) 869 time "eigenvectors symmetric 1000x1000" (snd $ eigSH (trustSym m))
870 time "eigenvalues general 1000x1000" (eigenvalues m) 870 time "eigenvalues general 1000x1000" (eigenvalues m)
871 time "eigenvectors general 1000x1000" (snd $ eig m) 871 time "eigenvectors general 1000x1000" (snd $ eig m)
872 872
@@ -893,12 +893,14 @@ solveBenchN n = do
893 time ("svd solve " ++ show n) (linearSolveSVD a b) 893 time ("svd solve " ++ show n) (linearSolveSVD a b)
894 time (" ls solve " ++ show n) (linearSolveLS a b) 894 time (" ls solve " ++ show n) (linearSolveLS a b)
895 time (" solve " ++ show n) (linearSolve a b) 895 time (" solve " ++ show n) (linearSolve a b)
896 time ("cholSolve " ++ show n) (cholSolve (chol a) b) 896-- time (" LU solve " ++ show n) (luSolve (luPacked a) b)
897 time ("LDL solve " ++ show n) (ldlSolve (ldlPacked (trustSym a)) b)
898 time ("cholSolve " ++ show n) (cholSolve (chol $ trustSym a) b)
897 899
898solveBench = do 900solveBench = do
899 solveBenchN 500 901 solveBenchN 500
900 solveBenchN 1000 902 solveBenchN 1000
901 -- solveBenchN 1500 903 solveBenchN 1500
902 904
903-------------------------------- 905--------------------------------
904 906
@@ -906,7 +908,7 @@ cholBenchN n = do
906 let x = uniformSample 777 (2*n) (replicate n (-1,1)) 908 let x = uniformSample 777 (2*n) (replicate n (-1,1))
907 a = tr x <> x 909 a = tr x <> x
908 a `seq` putStr "" 910 a `seq` putStr ""
909 time ("chol " ++ show n) (chol a) 911 time ("chol " ++ show n) (chol $ trustSym a)
910 912
911cholBench = do 913cholBench = do
912 putStrLn "" 914 putStrLn ""
diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs
index 7c54535..4704989 100644
--- a/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs
+++ b/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs
@@ -14,7 +14,7 @@ Arbitrary instances for vectors, matrices.
14module Numeric.LinearAlgebra.Tests.Instances( 14module Numeric.LinearAlgebra.Tests.Instances(
15 Sq(..), rSq,cSq, 15 Sq(..), rSq,cSq,
16 Rot(..), rRot,cRot, 16 Rot(..), rRot,cRot,
17 Her(..), rHer,cHer, 17 rHer,cHer,
18 WC(..), rWC,cWC, 18 WC(..), rWC,cWC,
19 SqWC(..), rSqWC, cSqWC, rSymWC, cSymWC, 19 SqWC(..), rSqWC, cSqWC, rSymWC, cSymWC,
20 PosDef(..), rPosDef, cPosDef, 20 PosDef(..), rPosDef, cPosDef,
@@ -81,12 +81,12 @@ instance (Field a, Arbitrary a) => Arbitrary (Rot a) where
81 81
82 82
83-- a complex hermitian or real symmetric matrix 83-- a complex hermitian or real symmetric matrix
84newtype (Her a) = Her (Matrix a) deriving Show 84--newtype (Her a) = Her (Matrix a) deriving Show
85instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (Her a) where 85instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (Her a) where
86 arbitrary = do 86 arbitrary = do
87 Sq m <- arbitrary 87 Sq m <- arbitrary
88 let m' = m/2 88 let m' = m/2
89 return $ Her (m' + tr m') 89 return $ sym m'
90 90
91 91
92class (Field a, Arbitrary a, Element (RealOf a), Random (RealOf a)) => ArbitraryField a 92class (Field a, Arbitrary a, Element (RealOf a), Random (RealOf a)) => ArbitraryField a
@@ -125,9 +125,9 @@ newtype (PosDef a) = PosDef (Matrix a) deriving Show
125instance (Numeric a, ArbitraryField a, Num (Vector a)) 125instance (Numeric a, ArbitraryField a, Num (Vector a))
126 => Arbitrary (PosDef a) where 126 => Arbitrary (PosDef a) where
127 arbitrary = do 127 arbitrary = do
128 Her m <- arbitrary 128 m <- arbitrary
129 let (_,v) = eigSH m 129 let (_,v) = eigSH m
130 n = rows m 130 n = rows (her m)
131 l <- replicateM n (choose (0,100)) 131 l <- replicateM n (choose (0,100))
132 let s = diag (fromList l) 132 let s = diag (fromList l)
133 p = v <> real s <> tr v 133 p = v <> real s <> tr v
@@ -161,8 +161,8 @@ fM m = m :: FM
161zM m = m :: ZM 161zM m = m :: ZM
162 162
163 163
164rHer (Her m) = m :: RM 164rHer m = her m :: RM
165cHer (Her m) = m :: CM 165cHer m = her m :: CM
166 166
167rRot (Rot m) = m :: RM 167rRot (Rot m) = m :: RM
168cRot (Rot m) = m :: CM 168cRot (Rot m) = m :: CM
@@ -176,8 +176,8 @@ cWC (WC m) = m :: CM
176rSqWC (SqWC m) = m :: RM 176rSqWC (SqWC m) = m :: RM
177cSqWC (SqWC m) = m :: CM 177cSqWC (SqWC m) = m :: CM
178 178
179rSymWC (SqWC m) = m + tr m :: RM 179rSymWC (SqWC m) = sym m :: Her R
180cSymWC (SqWC m) = m + tr m :: CM 180cSymWC (SqWC m) = sym m :: Her C
181 181
182rPosDef (PosDef m) = m :: RM 182rPosDef (PosDef m) = m :: RM
183cPosDef (PosDef m) = m :: CM 183cPosDef (PosDef m) = m :: CM
diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs
index 207a303..2ac3588 100644
--- a/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs
+++ b/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs
@@ -39,7 +39,7 @@ module Numeric.LinearAlgebra.Tests.Properties (
39 expmDiagProp, 39 expmDiagProp,
40 multProp1, multProp2, 40 multProp1, multProp2,
41 subProp, 41 subProp,
42 linearSolveProp, linearSolveProp2 42 linearSolveProp, linearSolvePropH, linearSolveProp2
43) where 43) where
44 44
45import Numeric.LinearAlgebra.HMatrix hiding (Testable,unitary) 45import Numeric.LinearAlgebra.HMatrix hiding (Testable,unitary)
@@ -209,11 +209,11 @@ eigProp m = complex m <> v |~| v <> diag s
209eigSHProp m = m <> v |~| v <> real (diag s) 209eigSHProp m = m <> v |~| v <> real (diag s)
210 && unitary v 210 && unitary v
211 && m |~| v <> real (diag s) <> tr v 211 && m |~| v <> real (diag s) <> tr v
212 where (s, v) = eigSH m 212 where (s, v) = eigSH' m
213 213
214eigProp2 m = fst (eig m) |~| eigenvalues m 214eigProp2 m = fst (eig m) |~| eigenvalues m
215 215
216eigSHProp2 m = fst (eigSH m) |~| eigenvaluesSH m 216eigSHProp2 m = fst (eigSH' m) |~| eigenvaluesSH' m
217 217
218------------------------------------------------------------------ 218------------------------------------------------------------------
219 219
@@ -246,9 +246,9 @@ schurProp2 m = m |~| u <> s <> tr u && unitary u && upperHessenberg s -- fixme
246 where (u,s) = schur m 246 where (u,s) = schur m
247 247
248cholProp m = m |~| tr c <> c && upperTriang c 248cholProp m = m |~| tr c <> c && upperTriang c
249 where c = chol m 249 where c = chol (trustSym m)
250 250
251exactProp m = chol m == chol (m+0) 251exactProp m = chol (trustSym m) == chol (trustSym (m+0))
252 252
253expmDiagProp m = expm (logm m) :~ 7 ~: complex m 253expmDiagProp m = expm (logm m) :~ 7 ~: complex m
254 where logm = matFunc log 254 where logm = matFunc log
@@ -263,6 +263,8 @@ multProp2 p (a,b) = (tr (a <> b)) :~p~: (tr b <> tr a)
263 263
264linearSolveProp f m = f m m |~| ident (rows m) 264linearSolveProp f m = f m m |~| ident (rows m)
265 265
266linearSolvePropH f m = f m (her m) |~| ident (rows (her m))
267
266linearSolveProp2 f (a,x) = not wc `trivial` (not wc || a <> f a b |~| b) 268linearSolveProp2 f (a,x) = not wc `trivial` (not wc || a <> f a b |~| b)
267 where q = min (rows a) (cols a) 269 where q = min (rows a) (cols a)
268 b = a <> x 270 b = a <> x