diff options
Diffstat (limited to 'examples/experiments')
-rw-r--r-- | examples/experiments/Static.hs | 123 | ||||
-rw-r--r-- | examples/experiments/error.hs | 16 | ||||
-rw-r--r-- | examples/experiments/listlike.hs | 32 | ||||
-rw-r--r-- | examples/experiments/parallel.hs | 28 | ||||
-rw-r--r-- | examples/experiments/speed.hs | 90 | ||||
-rw-r--r-- | examples/experiments/speed.m | 21 | ||||
-rw-r--r-- | examples/experiments/testmnist.m | 29 | ||||
-rw-r--r-- | examples/experiments/useStatic.hs | 36 |
8 files changed, 375 insertions, 0 deletions
diff --git a/examples/experiments/Static.hs b/examples/experiments/Static.hs new file mode 100644 index 0000000..c0cfcce --- /dev/null +++ b/examples/experiments/Static.hs | |||
@@ -0,0 +1,123 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts -fth -fallow-overlapping-instances -fallow-undecidable-instances #-} | ||
2 | |||
3 | module Static where | ||
4 | |||
5 | import Language.Haskell.TH | ||
6 | import Numeric.LinearAlgebra | ||
7 | import Foreign | ||
8 | import Language.Haskell.TH.Syntax | ||
9 | import Data.Packed.Internal(Vector(..),Matrix(..)) | ||
10 | |||
11 | instance Lift Double where | ||
12 | lift x = return (LitE (RationalL (toRational x))) | ||
13 | |||
14 | instance Lift (Ptr Double) where | ||
15 | lift p = [e| p |] | ||
16 | |||
17 | instance Lift (ForeignPtr Double) where | ||
18 | lift p = [e| p |] | ||
19 | |||
20 | instance (Lift a, Storable a, Lift (Ptr a), Lift (ForeignPtr a)) => Lift (Vector a ) where | ||
21 | lift (V n fp p) = [e| V $(lift n) $(lift fp) $(lift p) |] | ||
22 | |||
23 | instance (Lift (Vector a)) => Lift (Matrix a) where | ||
24 | lift (MC r c v vt) = [e| MC $(lift r) $(lift c) $(lift v) $(lift vt) |] | ||
25 | lift (MF r c v vt) = [e| MF $(lift r) $(lift c) $(lift v) $(lift vt) |] | ||
26 | |||
27 | tdim :: Int -> ExpQ | ||
28 | tdim 0 = [| Z |] | ||
29 | tdim n = [| S $(tdim (n-1)) |] | ||
30 | |||
31 | |||
32 | data Z = Z deriving Show | ||
33 | data S a = S a deriving Show | ||
34 | |||
35 | class Dim a | ||
36 | |||
37 | instance Dim Z | ||
38 | instance Dim a => Dim (S a) | ||
39 | |||
40 | class Sum a b c | a b -> c -- , a c -> b, b c -> a | ||
41 | |||
42 | instance Sum Z a a | ||
43 | instance Sum a Z a | ||
44 | instance Sum a b c => Sum a (S b) (S c) | ||
45 | |||
46 | newtype SVec d t = SVec (Vector t) deriving Show | ||
47 | newtype SMat r c t = SMat (Matrix t) deriving Show | ||
48 | |||
49 | createl :: d -> [Double] -> SVec d Double | ||
50 | createl d l = SVec (fromList l) | ||
51 | |||
52 | createv :: Storable t => d -> Vector t -> SVec d t | ||
53 | createv d v = SVec v | ||
54 | |||
55 | --vec'' v = [|createv ($(tdim (dim v))) v|] | ||
56 | |||
57 | vec' :: [Double] -> ExpQ | ||
58 | vec' d = [| createl ($(tdim (length d))) d |] | ||
59 | |||
60 | |||
61 | createm :: (Dim r, Dim c) => r -> c -> (Matrix Double) -> SMat r c Double | ||
62 | createm _ _ m = SMat m | ||
63 | |||
64 | createml :: (Dim r, Dim c) => r -> c -> Int -> Int -> [Double] -> SMat r c Double | ||
65 | createml _ _ r c l = SMat ((r><c) l) | ||
66 | |||
67 | mat :: Int -> Int -> [Double] -> ExpQ | ||
68 | mat r c l = [| createml ($(tdim r)) ($(tdim c)) r c l |] | ||
69 | |||
70 | vec :: [Double] -> ExpQ | ||
71 | vec d = mat (length d) 1 d | ||
72 | |||
73 | |||
74 | mat' :: Matrix Double -> ExpQ | ||
75 | mat' m = [| createm ($(tdim (rows m))) ($(tdim (cols m))) m |] | ||
76 | |||
77 | covec :: [Double] -> ExpQ | ||
78 | covec d = mat 1 (length d) d | ||
79 | |||
80 | scalar :: SMat (S Z) (S Z) Double -> Double | ||
81 | scalar (SMat m) = flatten m @> 0 | ||
82 | |||
83 | v = fromList [1..5] :: Vector Double | ||
84 | l = [1,1.5..5::Double] | ||
85 | |||
86 | k = [11..30::Int] | ||
87 | |||
88 | rawv (SVec v) = v | ||
89 | raw (SMat m) = m | ||
90 | |||
91 | liftStatic :: (Matrix a -> Matrix b -> Matrix c) -> SMat dr dc a -> SMat dr dc b -> SMat dr dc c | ||
92 | liftStatic f a b = SMat (f (raw a) (raw b)) | ||
93 | |||
94 | a |+| b = liftStatic (+) a b | ||
95 | |||
96 | prod :: SMat r k Double -> SMat k c Double -> SMat r c Double | ||
97 | prod a b = SMat (raw a <> raw b) | ||
98 | |||
99 | strans :: SMat r c Double -> SMat c r Double | ||
100 | strans = SMat . trans . raw | ||
101 | |||
102 | sdot a b = scalar (prod a b) | ||
103 | |||
104 | jv :: (Field t, Sum r1 r2 r3) => SMat r1 c t -> SMat r2 c t -> SMat r3 c t | ||
105 | jv a b = SMat ((raw a) <-> (raw b)) | ||
106 | |||
107 | -- curiously, we cannot easily fold jv because the matrics are not of the same type. | ||
108 | |||
109 | jh a b = strans (jv (strans a) (strans b)) | ||
110 | |||
111 | |||
112 | homog :: (Field t) => SMat r c t -> SMat (S r) c t | ||
113 | homog m = SMat (raw m <-> constant 1 (cols (raw m))) | ||
114 | |||
115 | inhomog :: (Linear Vector t) => SMat (S (S r)) c t -> SMat r c t | ||
116 | inhomog (SMat m) = SMat (sm <> d) where | ||
117 | sm = takeRows r' m | ||
118 | d = diag $ 1 / (flatten $ dropRows r' m) | ||
119 | r' = rows m -1 | ||
120 | |||
121 | |||
122 | ht t vs = inhomog (t `prod` homog vs) | ||
123 | |||
diff --git a/examples/experiments/error.hs b/examples/experiments/error.hs new file mode 100644 index 0000000..16305dc --- /dev/null +++ b/examples/experiments/error.hs | |||
@@ -0,0 +1,16 @@ | |||
1 | import Numeric.GSL | ||
2 | import Prelude hiding (catch) | ||
3 | import Control.Exception | ||
4 | |||
5 | test x = catch | ||
6 | (print x) | ||
7 | (\e -> putStrLn $ "captured ["++ show e++"]") | ||
8 | |||
9 | main = do | ||
10 | setErrorHandlerOff | ||
11 | |||
12 | test $ log_e (-1) | ||
13 | test $ 5 + (fst.exp_e) 1000 | ||
14 | test $ bessel_zero_Jnu_e (-0.3) 2 | ||
15 | |||
16 | putStrLn "Bye" \ No newline at end of file | ||
diff --git a/examples/experiments/listlike.hs b/examples/experiments/listlike.hs new file mode 100644 index 0000000..43216cb --- /dev/null +++ b/examples/experiments/listlike.hs | |||
@@ -0,0 +1,32 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | ||
2 | |||
3 | import qualified Data.ListLike as LL | ||
4 | import Numeric.LinearAlgebra | ||
5 | import Data.Monoid | ||
6 | import Data.Packed.Internal.Vector | ||
7 | import Foreign | ||
8 | |||
9 | instance (Storable a) => Monoid (Vector a) where | ||
10 | mempty = V { dim = 0, fptr = undefined, ptr = undefined } | ||
11 | mappend a b = mconcat [a,b] | ||
12 | mconcat = j . filter ((>0).dim) | ||
13 | where j [] = mempty | ||
14 | j l = join l | ||
15 | |||
16 | instance Storable a => LL.FoldableLL (Vector a) a where | ||
17 | foldl f x v = foldl f x (toList v) | ||
18 | foldr f x v = foldr f x (toList v) | ||
19 | |||
20 | instance Storable a => LL.ListLike (Vector a) a where | ||
21 | singleton a = fromList [a] | ||
22 | head a = a @> 0 | ||
23 | tail a | dim a == 1 = mempty | ||
24 | | otherwise = subVector 1 (dim a -1) a | ||
25 | genericLength = fromIntegral.dim | ||
26 | |||
27 | |||
28 | v k = fromList [1..k] :: Vector Double | ||
29 | |||
30 | f k = k+(3::Double) | ||
31 | |||
32 | 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 new file mode 100644 index 0000000..7256fb6 --- /dev/null +++ b/examples/experiments/parallel.hs | |||
@@ -0,0 +1,28 @@ | |||
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 x y = fromBlocks [inParallel[x <> y1, x <> y2]] | ||
9 | where p = cols y `div` 2 | ||
10 | (y1,y2) = splitColumnsAt p y | ||
11 | |||
12 | main = do | ||
13 | n <- (read . head) `fmap` getArgs | ||
14 | let m = ident n :: Matrix Double | ||
15 | time $ print $ vectorMax $ takeDiag $ parMul m m | ||
16 | time $ print $ vectorMax $ takeDiag $ m <> m | ||
17 | |||
18 | a = (2><3) [1..6::Double] | ||
19 | b = (3><4) [1..12::Double] | ||
20 | |||
21 | splitRowsAt p m = (takeRows p m, dropRows p m) | ||
22 | splitColumnsAt p m = (takeColumns p m, dropColumns p m) | ||
23 | |||
24 | time act = do | ||
25 | t0 <- getClockTime | ||
26 | act | ||
27 | t1 <- getClockTime | ||
28 | print $ tdSec $ normalizeTimeDiff $ diffClockTimes t1 t0 | ||
diff --git a/examples/experiments/speed.hs b/examples/experiments/speed.hs new file mode 100644 index 0000000..22d7220 --- /dev/null +++ b/examples/experiments/speed.hs | |||
@@ -0,0 +1,90 @@ | |||
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 new file mode 100644 index 0000000..2f41665 --- /dev/null +++ b/examples/experiments/speed.m | |||
@@ -0,0 +1,21 @@ | |||
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 new file mode 100644 index 0000000..38625a7 --- /dev/null +++ b/examples/experiments/testmnist.m | |||
@@ -0,0 +1,29 @@ | |||
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 | ||
diff --git a/examples/experiments/useStatic.hs b/examples/experiments/useStatic.hs new file mode 100644 index 0000000..619af8f --- /dev/null +++ b/examples/experiments/useStatic.hs | |||
@@ -0,0 +1,36 @@ | |||
1 | {-# OPTIONS -fno-monomorphism-restriction #-} | ||
2 | |||
3 | import Static | ||
4 | import Numeric.LinearAlgebra | ||
5 | |||
6 | |||
7 | x = $(vec [1,2]) | ||
8 | |||
9 | y = $(vec [5,7]) | ||
10 | |||
11 | z a = vec [a,a] | ||
12 | |||
13 | w = $(vec [1,2,3]) | ||
14 | |||
15 | cx = $(covec [1,2,3]) | ||
16 | |||
17 | |||
18 | t3 = $(tdim 3) | ||
19 | |||
20 | crm33 = createml t3 t3 3 3 | ||
21 | |||
22 | rot a = crm33 [a,0,0,0,a,0,0,0,1] | ||
23 | |||
24 | --q = x |+| y |+| $(z 5) | ||
25 | |||
26 | m = $(mat 2 3 [1..6]) | ||
27 | |||
28 | n = $(mat 3 5 [1..15]) | ||
29 | |||
30 | infixl 7 <*> | ||
31 | (<*>) = prod | ||
32 | |||
33 | r1 = m <*> n | ||
34 | r2 = strans (strans n <*> strans m) | ||
35 | |||
36 | --r' = prod n m | ||