summaryrefslogtreecommitdiff
path: root/examples/experiments/bigtests.hs
blob: a3dfad2a0135ef2bc6f2fe75c5e9e93c8c86fbc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Main where

import Numeric.LinearAlgebra
import Numeric.LinearAlgebra.Tests
import System.Random(randomRs,mkStdGen)
import Test.HUnit hiding (test)
import System(getArgs)


pseudorandomR seed (n,m) = reshape m $ fromList $ take (n*m) $ randomRs (-100,100) $ mkStdGen seed

pseudorandomC seed (n,m) = toComplex (pseudorandomR seed (n,m), pseudorandomR (seed+1) (n,m))

bigmat = m + trans m
    where m = pseudorandomR 18 (1000,1000) :: Matrix Double
bigmatc = mc + ctrans mc
    where mc = pseudorandomC 19 (1000,1000) :: Matrix (Complex Double)

utest str b = TestCase $ assertBool str b

feye n = flipud (ident n) :: Matrix Double

infixl 4 |~|
a |~| b = dist a b < 10^^(-10)

dist a b = r
    where norm = pnorm Infinity
          na = norm a
          nb = norm b
          nab = norm (a-b)
          mx = max na nb
          mn = min na nb
          r = if mn < eps
                then mx
                else nab/mx

square m = rows m == cols m

unitary m = square m && m <> ctrans m |~| ident (rows m)

eigProp m = complex m <> v |~| v <> diag s
    where (s, v) = eig m

eigSHProp m = m <> v |~| v <> real (diag s)
              && unitary v
              && m |~| v <> real (diag s) <> ctrans v
    where (s, v) = eigSH m

bigtests = do
    putStrLn "--------- big matrices -----"
    runTestTT $ TestList
     [ utest "eigS" $ eigSHProp bigmat
     , utest "eigH" $ eigSHProp bigmatc
     , utest "eigR" $ eigProp   bigmat
     , utest "eigC" $ eigProp   bigmatc
     , utest "det"  $ det (feye 1000) == 1 && det (feye 1002) == -1
     ]
    return ()

main = bigtests