summaryrefslogtreecommitdiff
path: root/examples/experiments
diff options
context:
space:
mode:
Diffstat (limited to 'examples/experiments')
-rw-r--r--examples/experiments/Static.hs123
-rw-r--r--examples/experiments/error.hs16
-rw-r--r--examples/experiments/listlike.hs32
-rw-r--r--examples/experiments/parallel.hs28
-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/experiments/useStatic.hs36
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
3module Static where
4
5import Language.Haskell.TH
6import Numeric.LinearAlgebra
7import Foreign
8import Language.Haskell.TH.Syntax
9import Data.Packed.Internal(Vector(..),Matrix(..))
10
11instance Lift Double where
12 lift x = return (LitE (RationalL (toRational x)))
13
14instance Lift (Ptr Double) where
15 lift p = [e| p |]
16
17instance Lift (ForeignPtr Double) where
18 lift p = [e| p |]
19
20instance (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
23instance (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
27tdim :: Int -> ExpQ
28tdim 0 = [| Z |]
29tdim n = [| S $(tdim (n-1)) |]
30
31
32data Z = Z deriving Show
33data S a = S a deriving Show
34
35class Dim a
36
37instance Dim Z
38instance Dim a => Dim (S a)
39
40class Sum a b c | a b -> c -- , a c -> b, b c -> a
41
42instance Sum Z a a
43instance Sum a Z a
44instance Sum a b c => Sum a (S b) (S c)
45
46newtype SVec d t = SVec (Vector t) deriving Show
47newtype SMat r c t = SMat (Matrix t) deriving Show
48
49createl :: d -> [Double] -> SVec d Double
50createl d l = SVec (fromList l)
51
52createv :: Storable t => d -> Vector t -> SVec d t
53createv d v = SVec v
54
55--vec'' v = [|createv ($(tdim (dim v))) v|]
56
57vec' :: [Double] -> ExpQ
58vec' d = [| createl ($(tdim (length d))) d |]
59
60
61createm :: (Dim r, Dim c) => r -> c -> (Matrix Double) -> SMat r c Double
62createm _ _ m = SMat m
63
64createml :: (Dim r, Dim c) => r -> c -> Int -> Int -> [Double] -> SMat r c Double
65createml _ _ r c l = SMat ((r><c) l)
66
67mat :: Int -> Int -> [Double] -> ExpQ
68mat r c l = [| createml ($(tdim r)) ($(tdim c)) r c l |]
69
70vec :: [Double] -> ExpQ
71vec d = mat (length d) 1 d
72
73
74mat' :: Matrix Double -> ExpQ
75mat' m = [| createm ($(tdim (rows m))) ($(tdim (cols m))) m |]
76
77covec :: [Double] -> ExpQ
78covec d = mat 1 (length d) d
79
80scalar :: SMat (S Z) (S Z) Double -> Double
81scalar (SMat m) = flatten m @> 0
82
83v = fromList [1..5] :: Vector Double
84l = [1,1.5..5::Double]
85
86k = [11..30::Int]
87
88rawv (SVec v) = v
89raw (SMat m) = m
90
91liftStatic :: (Matrix a -> Matrix b -> Matrix c) -> SMat dr dc a -> SMat dr dc b -> SMat dr dc c
92liftStatic f a b = SMat (f (raw a) (raw b))
93
94a |+| b = liftStatic (+) a b
95
96prod :: SMat r k Double -> SMat k c Double -> SMat r c Double
97prod a b = SMat (raw a <> raw b)
98
99strans :: SMat r c Double -> SMat c r Double
100strans = SMat . trans . raw
101
102sdot a b = scalar (prod a b)
103
104jv :: (Field t, Sum r1 r2 r3) => SMat r1 c t -> SMat r2 c t -> SMat r3 c t
105jv 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
109jh a b = strans (jv (strans a) (strans b))
110
111
112homog :: (Field t) => SMat r c t -> SMat (S r) c t
113homog m = SMat (raw m <-> constant 1 (cols (raw m)))
114
115inhomog :: (Linear Vector t) => SMat (S (S r)) c t -> SMat r c t
116inhomog (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
122ht 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 @@
1import Numeric.GSL
2import Prelude hiding (catch)
3import Control.Exception
4
5test x = catch
6 (print x)
7 (\e -> putStrLn $ "captured ["++ show e++"]")
8
9main = 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
3import qualified Data.ListLike as LL
4import Numeric.LinearAlgebra
5import Data.Monoid
6import Data.Packed.Internal.Vector
7import Foreign
8
9instance (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
16instance 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
20instance 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
28v k = fromList [1..k] :: Vector Double
29
30f k = k+(3::Double)
31
32main = 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 @@
1import System(getArgs)
2import Numeric.LinearAlgebra
3import Control.Parallel.Strategies
4import System.Time
5
6inParallel = parMap rwhnf id
7
8parMul x y = fromBlocks [inParallel[x <> y1, x <> y2]]
9 where p = cols y `div` 2
10 (y1,y2) = splitColumnsAt p y
11
12main = 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
18a = (2><3) [1..6::Double]
19b = (3><4) [1..12::Double]
20
21splitRowsAt p m = (takeRows p m, dropRows p m)
22splitColumnsAt p m = (takeColumns p m, dropColumns p m)
23
24time 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
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
new file mode 100644
index 0000000..2f41665
--- /dev/null
+++ b/examples/experiments/speed.m
@@ -0,0 +1,21 @@
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
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
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/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
3import Static
4import Numeric.LinearAlgebra
5
6
7x = $(vec [1,2])
8
9y = $(vec [5,7])
10
11z a = vec [a,a]
12
13w = $(vec [1,2,3])
14
15cx = $(covec [1,2,3])
16
17
18t3 = $(tdim 3)
19
20crm33 = createml t3 t3 3 3
21
22rot a = crm33 [a,0,0,0,a,0,0,0,1]
23
24--q = x |+| y |+| $(z 5)
25
26m = $(mat 2 3 [1..6])
27
28n = $(mat 3 5 [1..15])
29
30infixl 7 <*>
31(<*>) = prod
32
33r1 = m <*> n
34r2 = strans (strans n <*> strans m)
35
36--r' = prod n m