summaryrefslogtreecommitdiff
path: root/examples/oldtests.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-06-25 17:34:09 +0000
committerAlberto Ruiz <aruiz@um.es>2007-06-25 17:34:09 +0000
commit2984d5cc1cedb1621f6fa8d9dda0c515441f92e1 (patch)
tree85e155bd77644c26e265996f9cfecd7de70e2450 /examples/oldtests.hs
parent1871acb835b4fc164bcff3f6e7467884b87fbd0f (diff)
old tests passed
Diffstat (limited to 'examples/oldtests.hs')
-rw-r--r--examples/oldtests.hs121
1 files changed, 121 insertions, 0 deletions
diff --git a/examples/oldtests.hs b/examples/oldtests.hs
new file mode 100644
index 0000000..987ef98
--- /dev/null
+++ b/examples/oldtests.hs
@@ -0,0 +1,121 @@
1import Test.HUnit
2
3import GSL
4import GSL.Matrix
5import System.Random(randomRs,mkStdGen)
6
7realMatrix = fromLists :: [[Double]] -> Matrix Double
8realVector = fromList :: [Double] -> Vector Double
9
10toComplexM = uncurry $ liftMatrix2 (curry toComplex)
11
12infixl 2 =~=
13a =~= b = pnorm PNorm1 (flatten (a - b)) < 1E-6
14
15randomMatrix seed (n,m) = reshape m $ realVector $ take (n*m) $ randomRs (-100,100) $ mkStdGen seed
16
17randomMatrixC seed (n,m) = toComplexM (randomMatrix seed (n,m), randomMatrix (seed+1) (n,m))
18
19besselTest = do
20 let (r,e) = bessel_J0_e 5.0
21 let expected = -0.17759677131433830434739701
22 assertBool "bessel_J0_e" ( abs (r-expected) < e )
23
24exponentialTest = do
25 let (v,e,err) = exp_e10_e 30.0
26 let expected = exp 30.0
27 assertBool "exp_e10_e" ( abs (v*10^e - expected) < 4E-2 )
28
29disp m = putStrLn (format " " show m)
30
31ms = realMatrix [[1,2,3]
32 ,[-4,1,7]]
33
34ms' = randomMatrix 27 (50,100)
35
36ms'' = toComplexM (randomMatrix 100 (50,100),randomMatrix 101 (50,100))
37
38fullsvdTest method mat msg = do
39 let (u,s,vt) = method mat
40 assertBool msg (u <> s <> trans vt =~= mat)
41
42svdg' m = (u, diag s, v) where (u,s,v) = svdg m
43
44full_svd_Rd = svdRdd
45
46--------------------------------------------------------------------
47
48mcu = toComplexM (randomMatrix 33 (20,20),randomMatrix 34 (20,20))
49
50mcur = randomMatrix 35 (40,40)
51
52-- eigenvectors are columns
53eigTest method m msg = do
54 let (s,v) = method m
55 assertBool msg $ m <> v =~= v <> diag s
56
57bigmat = m + trans m where m = randomMatrix 18 (1000,1000)
58bigmatc = mc + conjTrans mc where mc = toComplexM(m,m)
59 m = randomMatrix 19 (1000,1000)
60
61--------------------------------------------------------------------
62
63invTest msg m = do
64 assertBool msg $ m <> inv m =~= ident (rows m)
65
66invComplexTest msg m = do
67 assertBool msg $ m <> invC m =~= ident (rows m)
68
69invC m = linearSolveC m (ident (rows m))
70
71--identC n = toComplexM(ident n, (0::Double) <>ident n)
72
73--------------------------------------------------------------------
74
75pinvTest f msg m = do
76 assertBool msg $ m <> f m <> m =~= m
77
78pinvC m = linearSolveLSC m (ident (rows m))
79
80pinvSVDR m = linearSolveSVDR Nothing m (ident (rows m))
81
82pinvSVDC m = linearSolveSVDC Nothing m (ident (rows m))
83
84--------------------------------------------------------------------
85
86
87tests = TestList [
88 TestCase $ besselTest
89 , TestCase $ exponentialTest
90 , TestCase $ invTest "inv 100x100" (randomMatrix 18 (100,100))
91 , TestCase $ invComplexTest "complex inv 100x100" (randomMatrixC 18 (100,100))
92 , TestCase $ pinvTest (pinvTolg 1) "pinvg 100x50" (randomMatrix 18 (100,50))
93 , TestCase $ pinvTest pinv "pinv 100x50" (randomMatrix 18 (100,50))
94 , TestCase $ pinvTest pinv "pinv 50x100" (randomMatrix 18 (50,100))
95 , TestCase $ pinvTest pinvSVDR "pinvSVDR 100x50" (randomMatrix 18 (100,50))
96 , TestCase $ pinvTest pinvSVDR "pinvSVDR 50x100" (randomMatrix 18 (50,100))
97 , TestCase $ pinvTest pinvC "pinvC 100x50" (randomMatrixC 18 (100,50))
98 , TestCase $ pinvTest pinvC "pinvC 50x100" (randomMatrixC 18 (50,100))
99 , TestCase $ pinvTest pinvSVDC "pinvSVDC 100x50" (randomMatrixC 18 (100,50))
100 , TestCase $ pinvTest pinvSVDC "pinvSVDC 50x100" (randomMatrixC 18 (50,100))
101 , TestCase $ eigTest eigC mcu "eigC"
102 , TestCase $ eigTest eigR mcur "eigR"
103 , TestCase $ eigTest eigS (mcur+trans mcur) "eigS"
104 , TestCase $ eigTest eigSg (mcur+trans mcur) "eigSg"
105 , TestCase $ eigTest eigH (mcu+ (conjTrans) mcu) "eigH"
106 , TestCase $ eigTest eigHg (mcu+ (conjTrans) mcu) "eigHg"
107 , TestCase $ fullsvdTest svdg' ms "GSL svd small"
108 , TestCase $ fullsvdTest svdR ms "fullsvdR small"
109 , TestCase $ fullsvdTest svdR (trans ms) "fullsvdR small"
110 , TestCase $ fullsvdTest svdR ms' "fullsvdR"
111 , TestCase $ fullsvdTest svdR (trans ms') "fullsvdR"
112 , TestCase $ fullsvdTest full_svd_Rd ms' "fullsvdRd"
113 , TestCase $ fullsvdTest full_svd_Rd (trans ms') "fullsvdRd"
114 , TestCase $ fullsvdTest svdC ms'' "fullsvdC"
115 , TestCase $ fullsvdTest svdC (trans ms'') "fullsvdC"
116 , TestCase $ eigTest eigS bigmat "big eigS"
117 , TestCase $ eigTest eigH bigmatc "big eigH"
118 , TestCase $ eigTest eigR bigmat "big eigR"
119 ]
120
121main = runTestTT tests