diff options
Diffstat (limited to 'packages/hmatrix/src/Numeric/Chain.hs')
-rw-r--r-- | packages/hmatrix/src/Numeric/Chain.hs | 140 |
1 files changed, 0 insertions, 140 deletions
diff --git a/packages/hmatrix/src/Numeric/Chain.hs b/packages/hmatrix/src/Numeric/Chain.hs deleted file mode 100644 index de6a86f..0000000 --- a/packages/hmatrix/src/Numeric/Chain.hs +++ /dev/null | |||
@@ -1,140 +0,0 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Numeric.Chain | ||
4 | -- Copyright : (c) Vivian McPhail 2010 | ||
5 | -- License : GPL-style | ||
6 | -- | ||
7 | -- Maintainer : Vivian McPhail <haskell.vivian.mcphail <at> gmail.com> | ||
8 | -- Stability : provisional | ||
9 | -- Portability : portable | ||
10 | -- | ||
11 | -- optimisation of association order for chains of matrix multiplication | ||
12 | -- | ||
13 | ----------------------------------------------------------------------------- | ||
14 | |||
15 | module Numeric.Chain ( | ||
16 | optimiseMult, | ||
17 | ) where | ||
18 | |||
19 | import Data.Maybe | ||
20 | |||
21 | import Data.Packed.Matrix | ||
22 | import Data.Packed.Numeric | ||
23 | |||
24 | import qualified Data.Array.IArray as A | ||
25 | |||
26 | ----------------------------------------------------------------------------- | ||
27 | {- | | ||
28 | Provide optimal association order for a chain of matrix multiplications | ||
29 | and apply the multiplications. | ||
30 | |||
31 | The algorithm is the well-known O(n\^3) dynamic programming algorithm | ||
32 | that builds a pyramid of optimal associations. | ||
33 | |||
34 | > m1, m2, m3, m4 :: Matrix Double | ||
35 | > m1 = (10><15) [1..] | ||
36 | > m2 = (15><20) [1..] | ||
37 | > m3 = (20><5) [1..] | ||
38 | > m4 = (5><10) [1..] | ||
39 | |||
40 | > >>> optimiseMult [m1,m2,m3,m4] | ||
41 | |||
42 | will perform @((m1 `multiply` (m2 `multiply` m3)) `multiply` m4)@ | ||
43 | |||
44 | The naive left-to-right multiplication would take @4500@ scalar multiplications | ||
45 | whereas the optimised version performs @2750@ scalar multiplications. The complexity | ||
46 | in this case is 32 (= 4^3/2) * (2 comparisons, 3 scalar multiplications, 3 scalar additions, | ||
47 | 5 lookups, 2 updates) + a constant (= three table allocations) | ||
48 | -} | ||
49 | optimiseMult :: Product t => [Matrix t] -> Matrix t | ||
50 | optimiseMult = chain | ||
51 | |||
52 | ----------------------------------------------------------------------------- | ||
53 | |||
54 | type Matrices a = A.Array Int (Matrix a) | ||
55 | type Sizes = A.Array Int (Int,Int) | ||
56 | type Cost = A.Array Int (A.Array Int (Maybe Int)) | ||
57 | type Indexes = A.Array Int (A.Array Int (Maybe ((Int,Int),(Int,Int)))) | ||
58 | |||
59 | update :: A.Array Int (A.Array Int a) -> (Int,Int) -> a -> A.Array Int (A.Array Int a) | ||
60 | update a (r,c) e = a A.// [(r,(a A.! r) A.// [(c,e)])] | ||
61 | |||
62 | newWorkSpaceCost :: Int -> A.Array Int (A.Array Int (Maybe Int)) | ||
63 | newWorkSpaceCost n = A.array (1,n) $ map (\i -> (i, subArray i)) [1..n] | ||
64 | where subArray i = A.listArray (1,i) (repeat Nothing) | ||
65 | |||
66 | newWorkSpaceIndexes :: Int -> A.Array Int (A.Array Int (Maybe ((Int,Int),(Int,Int)))) | ||
67 | newWorkSpaceIndexes n = A.array (1,n) $ map (\i -> (i, subArray i)) [1..n] | ||
68 | where subArray i = A.listArray (1,i) (repeat Nothing) | ||
69 | |||
70 | matricesToSizes :: [Matrix a] -> Sizes | ||
71 | matricesToSizes ms = A.listArray (1,length ms) $ map (\m -> (rows m,cols m)) ms | ||
72 | |||
73 | chain :: Product a => [Matrix a] -> Matrix a | ||
74 | chain [] = error "chain: zero matrices to multiply" | ||
75 | chain [m] = m | ||
76 | chain [ml,mr] = ml `multiply` mr | ||
77 | chain ms = let ln = length ms | ||
78 | ma = A.listArray (1,ln) ms | ||
79 | mz = matricesToSizes ms | ||
80 | i = chain_cost mz | ||
81 | in chain_paren (ln,ln) i ma | ||
82 | |||
83 | chain_cost :: Sizes -> Indexes | ||
84 | chain_cost mz = let (_,u) = A.bounds mz | ||
85 | cost = newWorkSpaceCost u | ||
86 | ixes = newWorkSpaceIndexes u | ||
87 | (_,_,i) = foldl chain_cost' (mz,cost,ixes) (order u) | ||
88 | in i | ||
89 | |||
90 | chain_cost' :: (Sizes,Cost,Indexes) -> (Int,Int) -> (Sizes,Cost,Indexes) | ||
91 | chain_cost' sci@(mz,cost,ixes) (r,c) | ||
92 | | c == 1 = let cost' = update cost (r,c) (Just 0) | ||
93 | ixes' = update ixes (r,c) (Just ((r,c),(r,c))) | ||
94 | in (mz,cost',ixes') | ||
95 | | otherwise = minimum_cost sci (r,c) | ||
96 | |||
97 | minimum_cost :: (Sizes,Cost,Indexes) -> (Int,Int) -> (Sizes,Cost,Indexes) | ||
98 | minimum_cost sci fu = foldl (smaller_cost fu) sci (fulcrum_order fu) | ||
99 | |||
100 | smaller_cost :: (Int,Int) -> (Sizes,Cost,Indexes) -> ((Int,Int),(Int,Int)) -> (Sizes,Cost,Indexes) | ||
101 | smaller_cost (r,c) (mz,cost,ixes) ix@((lr,lc),(rr,rc)) = let op_cost = fromJust ((cost A.! lr) A.! lc) | ||
102 | + fromJust ((cost A.! rr) A.! rc) | ||
103 | + fst (mz A.! (lr-lc+1)) | ||
104 | * snd (mz A.! lc) | ||
105 | * snd (mz A.! rr) | ||
106 | cost' = (cost A.! r) A.! c | ||
107 | in case cost' of | ||
108 | Nothing -> let cost'' = update cost (r,c) (Just op_cost) | ||
109 | ixes'' = update ixes (r,c) (Just ix) | ||
110 | in (mz,cost'',ixes'') | ||
111 | Just ct -> if op_cost < ct then | ||
112 | let cost'' = update cost (r,c) (Just op_cost) | ||
113 | ixes'' = update ixes (r,c) (Just ix) | ||
114 | in (mz,cost'',ixes'') | ||
115 | else (mz,cost,ixes) | ||
116 | |||
117 | |||
118 | fulcrum_order (r,c) = let fs' = zip (repeat r) [1..(c-1)] | ||
119 | in map (partner (r,c)) fs' | ||
120 | |||
121 | partner (r,c) (a,b) = ((r-b, c-b), (a,b)) | ||
122 | |||
123 | order 0 = [] | ||
124 | order n = order (n-1) ++ zip (repeat n) [1..n] | ||
125 | |||
126 | chain_paren :: Product a => (Int,Int) -> Indexes -> Matrices a -> Matrix a | ||
127 | chain_paren (r,c) ixes ma = let ((lr,lc),(rr,rc)) = fromJust $ (ixes A.! r) A.! c | ||
128 | in if lr == rr && lc == rc then (ma A.! lr) | ||
129 | else (chain_paren (lr,lc) ixes ma) `multiply` (chain_paren (rr,rc) ixes ma) | ||
130 | |||
131 | -------------------------------------------------------------------------- | ||
132 | |||
133 | {- TESTS -} | ||
134 | |||
135 | -- optimal association is ((m1*(m2*m3))*m4) | ||
136 | m1, m2, m3, m4 :: Matrix Double | ||
137 | m1 = (10><15) [1..] | ||
138 | m2 = (15><20) [1..] | ||
139 | m3 = (20><5) [1..] | ||
140 | m4 = (5><10) [1..] | ||