summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2009-04-17 11:04:08 +0000
committerAlberto Ruiz <aruiz@um.es>2009-04-17 11:04:08 +0000
commit71ed02d2728701130cf82e61a8633af0f6375812 (patch)
tree1f1c9ddc2643d20c79945cd1a56c53a9cf84c437 /examples
parentda07df1ef6edc8638c1caccfcff868d9a2fadca0 (diff)
removed static experiment
Diffstat (limited to 'examples')
-rw-r--r--examples/experiments/Static.hs115
-rw-r--r--examples/experiments/useStatic.hs36
2 files changed, 0 insertions, 151 deletions
diff --git a/examples/experiments/Static.hs b/examples/experiments/Static.hs
deleted file mode 100644
index 2e4ef4e..0000000
--- a/examples/experiments/Static.hs
+++ /dev/null
@@ -1,115 +0,0 @@
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
9
10instance Lift Double where
11 lift x = return (LitE (RationalL (toRational x)))
12
13instance Lift (Vector a ) where
14 lift v = [e| v |]
15
16instance Lift (Matrix a) where
17 lift m = [e| m |]
18
19tdim :: Int -> ExpQ
20tdim 0 = [| Z |]
21tdim n = [| S $(tdim (n-1)) |]
22
23
24data Z = Z deriving Show
25data S a = S a deriving Show
26
27class Dim a
28
29instance Dim Z
30instance Dim a => Dim (S a)
31
32class Sum a b c | a b -> c -- , a c -> b, b c -> a
33
34instance Sum Z a a
35instance Sum a Z a
36instance Sum a b c => Sum a (S b) (S c)
37
38newtype SVec d t = SVec (Vector t) deriving Show
39newtype SMat r c t = SMat (Matrix t) deriving Show
40
41createl :: d -> [Double] -> SVec d Double
42createl d l = SVec (fromList l)
43
44createv :: Storable t => d -> Vector t -> SVec d t
45createv d v = SVec v
46
47vec'' v = [|createv ($(tdim (dim v))) v|]
48
49vec' :: [Double] -> ExpQ
50vec' d = [| createl ($(tdim (length d))) d |]
51
52
53createm :: (Dim r, Dim c) => r -> c -> (Matrix Double) -> SMat r c Double
54createm _ _ m = SMat m
55
56createml :: (Dim r, Dim c) => r -> c -> Int -> Int -> [Double] -> SMat r c Double
57createml _ _ r c l = SMat ((r><c) l)
58
59mat :: Int -> Int -> [Double] -> ExpQ
60mat r c l = [| createml ($(tdim r)) ($(tdim c)) r c l |]
61
62vec :: [Double] -> ExpQ
63vec d = mat (length d) 1 d
64
65
66--mat' :: Matrix Double -> ExpQ
67--mat' m = [| createm ($(tdim (rows m))) ($(tdim (cols m))) m |]
68
69covec :: [Double] -> ExpQ
70covec d = mat 1 (length d) d
71
72scalar :: SMat (S Z) (S Z) Double -> Double
73scalar (SMat m) = flatten m @> 0
74
75v = fromList [1..5] :: Vector Double
76l = [1,1.5..5::Double]
77
78k = [11..30::Int]
79
80rawv (SVec v) = v
81raw (SMat m) = m
82
83liftStatic :: (Matrix a -> Matrix b -> Matrix c) -> SMat dr dc a -> SMat dr dc b -> SMat dr dc c
84liftStatic f a b = SMat (f (raw a) (raw b))
85
86a |+| b = liftStatic (+) a b
87
88prod :: SMat r k Double -> SMat k c Double -> SMat r c Double
89prod a b = SMat (raw a <> raw b)
90
91strans :: SMat r c Double -> SMat c r Double
92strans = SMat . trans . raw
93
94sdot a b = scalar (prod a b)
95
96jv :: (Field t, Sum r1 r2 r3) => SMat r1 c t -> SMat r2 c t -> SMat r3 c t
97jv a b = SMat ((raw a) <-> (raw b))
98
99-- curiously, we cannot easily fold jv because the matrics are not of the same type.
100
101jh a b = strans (jv (strans a) (strans b))
102
103
104homog :: (Field t) => SMat r c t -> SMat (S r) c t
105homog m = SMat (raw m <-> constant 1 (cols (raw m)))
106
107inhomog :: (Linear Vector t) => SMat (S (S r)) c t -> SMat r c t
108inhomog (SMat m) = SMat (sm <> d) where
109 sm = takeRows r' m
110 d = diag $ 1 / (flatten $ dropRows r' m)
111 r' = rows m -1
112
113
114ht t vs = inhomog (t `prod` homog vs)
115
diff --git a/examples/experiments/useStatic.hs b/examples/experiments/useStatic.hs
deleted file mode 100644
index 619af8f..0000000
--- a/examples/experiments/useStatic.hs
+++ /dev/null
@@ -1,36 +0,0 @@
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