diff options
Diffstat (limited to 'examples/experiments')
-rw-r--r-- | examples/experiments/bigtests.hs | 60 | ||||
-rw-r--r-- | examples/experiments/listlike.hs | 24 | ||||
-rw-r--r-- | examples/experiments/parallel.hs | 36 | ||||
-rw-r--r-- | examples/experiments/speed.hs | 90 | ||||
-rw-r--r-- | examples/experiments/speed.m | 21 | ||||
-rw-r--r-- | examples/experiments/testmnist.m | 29 |
6 files changed, 0 insertions, 260 deletions
diff --git a/examples/experiments/bigtests.hs b/examples/experiments/bigtests.hs deleted file mode 100644 index a3dfad2..0000000 --- a/examples/experiments/bigtests.hs +++ /dev/null | |||
@@ -1,60 +0,0 @@ | |||
1 | module Main where | ||
2 | |||
3 | import Numeric.LinearAlgebra | ||
4 | import Numeric.LinearAlgebra.Tests | ||
5 | import System.Random(randomRs,mkStdGen) | ||
6 | import Test.HUnit hiding (test) | ||
7 | import System(getArgs) | ||
8 | |||
9 | |||
10 | pseudorandomR seed (n,m) = reshape m $ fromList $ take (n*m) $ randomRs (-100,100) $ mkStdGen seed | ||
11 | |||
12 | pseudorandomC seed (n,m) = toComplex (pseudorandomR seed (n,m), pseudorandomR (seed+1) (n,m)) | ||
13 | |||
14 | bigmat = m + trans m | ||
15 | where m = pseudorandomR 18 (1000,1000) :: Matrix Double | ||
16 | bigmatc = mc + ctrans mc | ||
17 | where mc = pseudorandomC 19 (1000,1000) :: Matrix (Complex Double) | ||
18 | |||
19 | utest str b = TestCase $ assertBool str b | ||
20 | |||
21 | feye n = flipud (ident n) :: Matrix Double | ||
22 | |||
23 | infixl 4 |~| | ||
24 | a |~| b = dist a b < 10^^(-10) | ||
25 | |||
26 | dist a b = r | ||
27 | where norm = pnorm Infinity | ||
28 | na = norm a | ||
29 | nb = norm b | ||
30 | nab = norm (a-b) | ||
31 | mx = max na nb | ||
32 | mn = min na nb | ||
33 | r = if mn < eps | ||
34 | then mx | ||
35 | else nab/mx | ||
36 | |||
37 | square m = rows m == cols m | ||
38 | |||
39 | unitary m = square m && m <> ctrans m |~| ident (rows m) | ||
40 | |||
41 | eigProp m = complex m <> v |~| v <> diag s | ||
42 | where (s, v) = eig m | ||
43 | |||
44 | eigSHProp m = m <> v |~| v <> real (diag s) | ||
45 | && unitary v | ||
46 | && m |~| v <> real (diag s) <> ctrans v | ||
47 | where (s, v) = eigSH m | ||
48 | |||
49 | bigtests = do | ||
50 | putStrLn "--------- big matrices -----" | ||
51 | runTestTT $ TestList | ||
52 | [ utest "eigS" $ eigSHProp bigmat | ||
53 | , utest "eigH" $ eigSHProp bigmatc | ||
54 | , utest "eigR" $ eigProp bigmat | ||
55 | , utest "eigC" $ eigProp bigmatc | ||
56 | , utest "det" $ det (feye 1000) == 1 && det (feye 1002) == -1 | ||
57 | ] | ||
58 | return () | ||
59 | |||
60 | main = bigtests | ||
diff --git a/examples/experiments/listlike.hs b/examples/experiments/listlike.hs deleted file mode 100644 index 2d62447..0000000 --- a/examples/experiments/listlike.hs +++ /dev/null | |||
@@ -1,24 +0,0 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | ||
2 | |||
3 | import qualified Data.ListLike as LL | ||
4 | import Numeric.LinearAlgebra | ||
5 | import Data.Monoid | ||
6 | import Foreign | ||
7 | |||
8 | instance Storable a => LL.FoldableLL (Vector a) a where | ||
9 | foldl f x v = foldl f x (toList v) | ||
10 | foldr f x v = foldr f x (toList v) | ||
11 | |||
12 | instance Storable a => LL.ListLike (Vector a) a where | ||
13 | singleton a = fromList [a] | ||
14 | head a = a @> 0 | ||
15 | tail a | dim a == 1 = mempty | ||
16 | | otherwise = subVector 1 (dim a -1) a | ||
17 | genericLength = fromIntegral.dim | ||
18 | |||
19 | |||
20 | v k = fromList [1..k] :: Vector Double | ||
21 | |||
22 | f k = k+(3::Double) | ||
23 | |||
24 | main = print $ (LL.map f [1..5] :: Vector Double) \ No newline at end of file | ||
diff --git a/examples/experiments/parallel.hs b/examples/experiments/parallel.hs deleted file mode 100644 index dfbfd88..0000000 --- a/examples/experiments/parallel.hs +++ /dev/null | |||
@@ -1,36 +0,0 @@ | |||
1 | import System(getArgs) | ||
2 | import Numeric.LinearAlgebra | ||
3 | import Control.Parallel.Strategies | ||
4 | import System.Time | ||
5 | |||
6 | inParallel = parMap rwhnf id | ||
7 | |||
8 | parMul p x y = fromBlocks [ inParallel ( map (x <>) ys ) ] | ||
9 | where ys = splitColumns p y | ||
10 | |||
11 | |||
12 | main = do | ||
13 | n <- (read . head) `fmap` getArgs | ||
14 | let m = ident n :: Matrix Double | ||
15 | time $ print $ vectorMax $ takeDiag $ m <> m | ||
16 | time $ print $ vectorMax $ takeDiag $ parMul 2 m m | ||
17 | time $ print $ vectorMax $ takeDiag $ parMul 4 m m | ||
18 | time $ print $ vectorMax $ takeDiag $ parMul 8 m m | ||
19 | |||
20 | time act = do | ||
21 | t0 <- getClockTime | ||
22 | act | ||
23 | t1 <- getClockTime | ||
24 | print $ tdSec $ normalizeTimeDiff $ diffClockTimes t1 t0 | ||
25 | |||
26 | splitColumns n m = splitColumns' (f n (cols m)) m | ||
27 | where | ||
28 | splitColumns' [] m = [] | ||
29 | splitColumns' ((a,b):rest) m = subMatrix (0,a) (rows m, b-a+1) m : splitColumns' rest m | ||
30 | |||
31 | f :: Int -> Int -> [(Int,Int)] | ||
32 | f n c = zip ks (map pred $ tail ks) | ||
33 | where ks = map round $ toList $ linspace (fromIntegral n+1) (0,fromIntegral c) | ||
34 | |||
35 | splitRowsAt p m = (takeRows p m, dropRows p m) | ||
36 | splitColumnsAt p m = (takeColumns p m, dropColumns p m) | ||
diff --git a/examples/experiments/speed.hs b/examples/experiments/speed.hs deleted file mode 100644 index 22d7220..0000000 --- a/examples/experiments/speed.hs +++ /dev/null | |||
@@ -1,90 +0,0 @@ | |||
1 | {- speed tests | ||
2 | |||
3 | GNU-Octave (see speed.m in this folder): | ||
4 | |||
5 | ./speed.m | ||
6 | -0.017877255967426 0.000000000000000 -0.999840189089781 | ||
7 | 0.000000000000000 1.000000000000000 0.000000000000000 | ||
8 | 0.999840189089763 0.000000000000000 -0.017877255967417 | ||
9 | 9.69 seconds | ||
10 | |||
11 | Mathematica: | ||
12 | |||
13 | rot[a_]:={{ Cos[a], 0, Sin[a]}, | ||
14 | { 0, 1, 0}, | ||
15 | { -Sin[a],0,Cos[a]}}//N | ||
16 | |||
17 | test := Timing[ | ||
18 | n = 100000; | ||
19 | angles = Range[0.,n-1]/(n-1); | ||
20 | Fold[Dot,IdentityMatrix[3],rot/@angles] // MatrixForm | ||
21 | ] | ||
22 | |||
23 | 2.08013 Second | ||
24 | {{\(-0.017877255967432837`\), "0.`", \(-0.9998401890898042`\)}, | ||
25 | {"0.`", "1.`", "0.`"}, | ||
26 | {"0.9998401890898042`", "0.`", \(-0.017877255967432837`\)}} | ||
27 | |||
28 | $ ghc --make -O speed | ||
29 | |||
30 | $ ./speed 5 100000 1 | ||
31 | (3><3) | ||
32 | [ -1.7877255967425523e-2, 0.0, -0.9998401890897632 | ||
33 | , 0.0, 1.0, 0.0 | ||
34 | , 0.999840189089781, 0.0, -1.7877255967416586e-2 ] | ||
35 | 0.33 CPU seconds | ||
36 | |||
37 | cos 50000 = -0.0178772559665563 | ||
38 | sin 50000 = -0.999840189089790 | ||
39 | |||
40 | -} | ||
41 | |||
42 | import Numeric.LinearAlgebra | ||
43 | import System | ||
44 | import Data.List(foldl1') | ||
45 | import System.CPUTime | ||
46 | import Text.Printf | ||
47 | import Debug.Trace | ||
48 | |||
49 | debug x = trace (show x) x | ||
50 | |||
51 | timing act = do | ||
52 | t0 <- getCPUTime | ||
53 | act | ||
54 | t1 <- getCPUTime | ||
55 | printf "%.2f CPU seconds\n" $ (fromIntegral ((t1 - t0) `div` (10^10)) / 100 :: Double) :: IO () | ||
56 | |||
57 | op a b = trans $ (trans a) <> (trans b) | ||
58 | |||
59 | op2 a b = trans $ (trans a) + (trans b) | ||
60 | |||
61 | rot' :: Double -> Matrix Double | ||
62 | rot' a = matrix [[ c,0,s], | ||
63 | [ 0,1,0], | ||
64 | [-s,0,c]] | ||
65 | where c = cos a | ||
66 | s = sin a | ||
67 | matrix = fromLists | ||
68 | |||
69 | rot :: Double -> Matrix Double | ||
70 | rot a = (3><3) [ c,0,s | ||
71 | , 0,1,0 | ||
72 | ,-s,0,c ] | ||
73 | where c = cos a | ||
74 | s = sin a | ||
75 | |||
76 | fun n r = foldl1' (<>) (map r angles) | ||
77 | where angles = toList $ linspace n (0,1) | ||
78 | |||
79 | main = do | ||
80 | args <- getArgs | ||
81 | let [p,n,d] = map read args | ||
82 | let ms = replicate n ((ident d :: Matrix Double)) | ||
83 | let mz = replicate n (diag (constant (0::Double) d)) | ||
84 | timing $ case p of | ||
85 | 0 -> print $ foldl1' (<>) ms | ||
86 | 1 -> print $ foldl1' (<>) (map trans ms) | ||
87 | 2 -> print $ foldl1' op ms | ||
88 | 3 -> print $ foldl1' op2 mz | ||
89 | 4 -> print $ fun n rot' | ||
90 | 5 -> print $ fun n rot | ||
diff --git a/examples/experiments/speed.m b/examples/experiments/speed.m deleted file mode 100644 index 2f41665..0000000 --- a/examples/experiments/speed.m +++ /dev/null | |||
@@ -1,21 +0,0 @@ | |||
1 | #! /usr/bin/octave -qf | ||
2 | 1; % measuring Octave computing times | ||
3 | |||
4 | function r = rot(a) | ||
5 | c = cos(a); | ||
6 | s = sin(a); | ||
7 | r = [ c , 0, s; | ||
8 | 0, 1, 0; | ||
9 | -s, 0, c]; | ||
10 | end | ||
11 | |||
12 | t0=time(); | ||
13 | x = linspace(0,1,1E5); | ||
14 | ac = eye(3); | ||
15 | for a = x | ||
16 | ac = ac*rot(a); | ||
17 | end | ||
18 | |||
19 | format long | ||
20 | disp(ac); | ||
21 | printf("%.2f seconds\n",time()-t0) | ||
diff --git a/examples/experiments/testmnist.m b/examples/experiments/testmnist.m deleted file mode 100644 index 38625a7..0000000 --- a/examples/experiments/testmnist.m +++ /dev/null | |||
@@ -1,29 +0,0 @@ | |||
1 | #! /usr/bin/octave -qf | ||
2 | % measuring Octave computing times | ||
3 | |||
4 | t0=time(); | ||
5 | load mnist.txt | ||
6 | disp("load"); | ||
7 | disp(time()-t0) | ||
8 | |||
9 | |||
10 | x = mnist(:,1:784); | ||
11 | d = mnist(:,785); | ||
12 | |||
13 | |||
14 | t0=time(); | ||
15 | xc = x - repmat(mean(x),rows(x),1); | ||
16 | disp("x - repmat(mean(x),rows(x),1)"); | ||
17 | disp(time()-t0) | ||
18 | |||
19 | t0=time(); | ||
20 | mc = (xc'*xc)/rows(x); | ||
21 | disp("(xc'*xc)/rows(x)"); | ||
22 | disp(time()-t0) | ||
23 | |||
24 | t0=time(); | ||
25 | [v,l]=eig(mc); | ||
26 | disp("eig"); | ||
27 | disp(time()-t0) | ||
28 | |||
29 | disp(flipud(diag(l))(1:10)); \ No newline at end of file | ||