summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/experiments/bigtests.hs60
-rw-r--r--examples/experiments/listlike.hs24
-rw-r--r--examples/experiments/speed.hs90
-rw-r--r--examples/experiments/speed.m21
-rw-r--r--examples/experiments/testmnist.m29
-rw-r--r--examples/parallel.hs (renamed from examples/experiments/parallel.hs)0
-rw-r--r--hmatrix.cabal3
7 files changed, 2 insertions, 225 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 @@
1module Main where
2
3import Numeric.LinearAlgebra
4import Numeric.LinearAlgebra.Tests
5import System.Random(randomRs,mkStdGen)
6import Test.HUnit hiding (test)
7import System(getArgs)
8
9
10pseudorandomR seed (n,m) = reshape m $ fromList $ take (n*m) $ randomRs (-100,100) $ mkStdGen seed
11
12pseudorandomC seed (n,m) = toComplex (pseudorandomR seed (n,m), pseudorandomR (seed+1) (n,m))
13
14bigmat = m + trans m
15 where m = pseudorandomR 18 (1000,1000) :: Matrix Double
16bigmatc = mc + ctrans mc
17 where mc = pseudorandomC 19 (1000,1000) :: Matrix (Complex Double)
18
19utest str b = TestCase $ assertBool str b
20
21feye n = flipud (ident n) :: Matrix Double
22
23infixl 4 |~|
24a |~| b = dist a b < 10^^(-10)
25
26dist 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
37square m = rows m == cols m
38
39unitary m = square m && m <> ctrans m |~| ident (rows m)
40
41eigProp m = complex m <> v |~| v <> diag s
42 where (s, v) = eig m
43
44eigSHProp 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
49bigtests = 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
60main = 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
3import qualified Data.ListLike as LL
4import Numeric.LinearAlgebra
5import Data.Monoid
6import Foreign
7
8instance 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
12instance 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
20v k = fromList [1..k] :: Vector Double
21
22f k = k+(3::Double)
23
24main = print $ (LL.map f [1..5] :: Vector Double) \ No newline at end of file
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
3GNU-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
99.69 seconds
10
11Mathematica:
12
13rot[a_]:={{ Cos[a], 0, Sin[a]},
14 { 0, 1, 0},
15 { -Sin[a],0,Cos[a]}}//N
16
17test := Timing[
18 n = 100000;
19 angles = Range[0.,n-1]/(n-1);
20 Fold[Dot,IdentityMatrix[3],rot/@angles] // MatrixForm
21]
22
232.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 ]
350.33 CPU seconds
36
37cos 50000 = -0.0178772559665563
38sin 50000 = -0.999840189089790
39
40-}
41
42import Numeric.LinearAlgebra
43import System
44import Data.List(foldl1')
45import System.CPUTime
46import Text.Printf
47import Debug.Trace
48
49debug x = trace (show x) x
50
51timing 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
57op a b = trans $ (trans a) <> (trans b)
58
59op2 a b = trans $ (trans a) + (trans b)
60
61rot' :: Double -> Matrix Double
62rot' 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
69rot :: Double -> Matrix Double
70rot a = (3><3) [ c,0,s
71 , 0,1,0
72 ,-s,0,c ]
73 where c = cos a
74 s = sin a
75
76fun n r = foldl1' (<>) (map r angles)
77 where angles = toList $ linspace n (0,1)
78
79main = 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
21; % measuring Octave computing times
3
4function r = rot(a)
5 c = cos(a);
6 s = sin(a);
7 r = [ c , 0, s;
8 0, 1, 0;
9 -s, 0, c];
10end
11
12t0=time();
13x = linspace(0,1,1E5);
14ac = eye(3);
15for a = x
16 ac = ac*rot(a);
17end
18
19format long
20disp(ac);
21printf("%.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
4t0=time();
5load mnist.txt
6disp("load");
7disp(time()-t0)
8
9
10x = mnist(:,1:784);
11d = mnist(:,785);
12
13
14t0=time();
15xc = x - repmat(mean(x),rows(x),1);
16disp("x - repmat(mean(x),rows(x),1)");
17disp(time()-t0)
18
19t0=time();
20mc = (xc'*xc)/rows(x);
21disp("(xc'*xc)/rows(x)");
22disp(time()-t0)
23
24t0=time();
25[v,l]=eig(mc);
26disp("eig");
27disp(time()-t0)
28
29disp(flipud(diag(l))(1:10)); \ No newline at end of file
diff --git a/examples/experiments/parallel.hs b/examples/parallel.hs
index dfbfd88..dfbfd88 100644
--- a/examples/experiments/parallel.hs
+++ b/examples/parallel.hs
diff --git a/hmatrix.cabal b/hmatrix.cabal
index e185d8a..5265451 100644
--- a/hmatrix.cabal
+++ b/hmatrix.cabal
@@ -1,5 +1,5 @@
1Name: hmatrix 1Name: hmatrix
2Version: 0.5.1.2 2Version: 0.5.2.0
3License: GPL 3License: GPL
4License-file: LICENSE 4License-file: LICENSE
5Author: Alberto Ruiz 5Author: Alberto Ruiz
@@ -27,6 +27,7 @@ extra-source-files: examples/tests.hs
27 examples/pca1.hs 27 examples/pca1.hs
28 examples/pca2.hs 28 examples/pca2.hs
29 examples/pinv.hs 29 examples/pinv.hs
30 examples/data.txt
30 examples/lie.hs 31 examples/lie.hs
31 examples/kalman.hs 32 examples/kalman.hs
32 examples/plot.hs 33 examples/plot.hs