diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-06-25 17:34:09 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-06-25 17:34:09 +0000 |
commit | 2984d5cc1cedb1621f6fa8d9dda0c515441f92e1 (patch) | |
tree | 85e155bd77644c26e265996f9cfecd7de70e2450 /examples/oldtests.hs | |
parent | 1871acb835b4fc164bcff3f6e7467884b87fbd0f (diff) |
old tests passed
Diffstat (limited to 'examples/oldtests.hs')
-rw-r--r-- | examples/oldtests.hs | 121 |
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 @@ | |||
1 | import Test.HUnit | ||
2 | |||
3 | import GSL | ||
4 | import GSL.Matrix | ||
5 | import System.Random(randomRs,mkStdGen) | ||
6 | |||
7 | realMatrix = fromLists :: [[Double]] -> Matrix Double | ||
8 | realVector = fromList :: [Double] -> Vector Double | ||
9 | |||
10 | toComplexM = uncurry $ liftMatrix2 (curry toComplex) | ||
11 | |||
12 | infixl 2 =~= | ||
13 | a =~= b = pnorm PNorm1 (flatten (a - b)) < 1E-6 | ||
14 | |||
15 | randomMatrix seed (n,m) = reshape m $ realVector $ take (n*m) $ randomRs (-100,100) $ mkStdGen seed | ||
16 | |||
17 | randomMatrixC seed (n,m) = toComplexM (randomMatrix seed (n,m), randomMatrix (seed+1) (n,m)) | ||
18 | |||
19 | besselTest = 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 | |||
24 | exponentialTest = 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 | |||
29 | disp m = putStrLn (format " " show m) | ||
30 | |||
31 | ms = realMatrix [[1,2,3] | ||
32 | ,[-4,1,7]] | ||
33 | |||
34 | ms' = randomMatrix 27 (50,100) | ||
35 | |||
36 | ms'' = toComplexM (randomMatrix 100 (50,100),randomMatrix 101 (50,100)) | ||
37 | |||
38 | fullsvdTest method mat msg = do | ||
39 | let (u,s,vt) = method mat | ||
40 | assertBool msg (u <> s <> trans vt =~= mat) | ||
41 | |||
42 | svdg' m = (u, diag s, v) where (u,s,v) = svdg m | ||
43 | |||
44 | full_svd_Rd = svdRdd | ||
45 | |||
46 | -------------------------------------------------------------------- | ||
47 | |||
48 | mcu = toComplexM (randomMatrix 33 (20,20),randomMatrix 34 (20,20)) | ||
49 | |||
50 | mcur = randomMatrix 35 (40,40) | ||
51 | |||
52 | -- eigenvectors are columns | ||
53 | eigTest method m msg = do | ||
54 | let (s,v) = method m | ||
55 | assertBool msg $ m <> v =~= v <> diag s | ||
56 | |||
57 | bigmat = m + trans m where m = randomMatrix 18 (1000,1000) | ||
58 | bigmatc = mc + conjTrans mc where mc = toComplexM(m,m) | ||
59 | m = randomMatrix 19 (1000,1000) | ||
60 | |||
61 | -------------------------------------------------------------------- | ||
62 | |||
63 | invTest msg m = do | ||
64 | assertBool msg $ m <> inv m =~= ident (rows m) | ||
65 | |||
66 | invComplexTest msg m = do | ||
67 | assertBool msg $ m <> invC m =~= ident (rows m) | ||
68 | |||
69 | invC m = linearSolveC m (ident (rows m)) | ||
70 | |||
71 | --identC n = toComplexM(ident n, (0::Double) <>ident n) | ||
72 | |||
73 | -------------------------------------------------------------------- | ||
74 | |||
75 | pinvTest f msg m = do | ||
76 | assertBool msg $ m <> f m <> m =~= m | ||
77 | |||
78 | pinvC m = linearSolveLSC m (ident (rows m)) | ||
79 | |||
80 | pinvSVDR m = linearSolveSVDR Nothing m (ident (rows m)) | ||
81 | |||
82 | pinvSVDC m = linearSolveSVDC Nothing m (ident (rows m)) | ||
83 | |||
84 | -------------------------------------------------------------------- | ||
85 | |||
86 | |||
87 | tests = 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 | |||
121 | main = runTestTT tests | ||