summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Matrix.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-10-31 13:36:37 +0000
committerAlberto Ruiz <aruiz@um.es>2007-10-31 13:36:37 +0000
commitdb223fb5f9cd4adef54736812f796b48ecc289e6 (patch)
treef787f8d7c929f2b978bb8fd6aa83aa1b5db05339 /lib/Data/Packed/Matrix.hs
parentbf838323545fe0878382f8f4d41b0f36714afa43 (diff)
Field->Element, GenMat->Field
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r--lib/Data/Packed/Matrix.hs44
1 files changed, 22 insertions, 22 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index a705975..e96500f 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -14,7 +14,7 @@
14----------------------------------------------------------------------------- 14-----------------------------------------------------------------------------
15 15
16module Data.Packed.Matrix ( 16module Data.Packed.Matrix (
17 Field, 17 Element,
18 Matrix,rows,cols, 18 Matrix,rows,cols,
19 (><), 19 (><),
20 trans, 20 trans,
@@ -41,13 +41,13 @@ import Data.List(transpose,intersperse)
41import Data.Array 41import Data.Array
42 42
43-- | creates a matrix from a vertical list of matrices 43-- | creates a matrix from a vertical list of matrices
44joinVert :: Field t => [Matrix t] -> Matrix t 44joinVert :: Element t => [Matrix t] -> Matrix t
45joinVert ms = case common cols ms of 45joinVert ms = case common cols ms of
46 Nothing -> error "joinVert on matrices with different number of columns" 46 Nothing -> error "joinVert on matrices with different number of columns"
47 Just c -> reshape c $ join (map cdat ms) 47 Just c -> reshape c $ join (map cdat ms)
48 48
49-- | creates a matrix from a horizontal list of matrices 49-- | creates a matrix from a horizontal list of matrices
50joinHoriz :: Field t => [Matrix t] -> Matrix t 50joinHoriz :: Element t => [Matrix t] -> Matrix t
51joinHoriz ms = trans. joinVert . map trans $ ms 51joinHoriz ms = trans. joinVert . map trans $ ms
52 52
53{- | Creates a matrix from blocks given as a list of lists of matrices: 53{- | Creates a matrix from blocks given as a list of lists of matrices:
@@ -63,15 +63,15 @@ joinHoriz ms = trans. joinVert . map trans $ ms
63 , -1.0, -1.0, -1.0, -1.0, 0.0, 7.0, 0.0 63 , -1.0, -1.0, -1.0, -1.0, 0.0, 7.0, 0.0
64 , -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 2.0 ]@ 64 , -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 2.0 ]@
65-} 65-}
66fromBlocks :: Field t => [[Matrix t]] -> Matrix t 66fromBlocks :: Element t => [[Matrix t]] -> Matrix t
67fromBlocks = joinVert . map joinHoriz 67fromBlocks = joinVert . map joinHoriz
68 68
69-- | Reverse rows 69-- | Reverse rows
70flipud :: Field t => Matrix t -> Matrix t 70flipud :: Element t => Matrix t -> Matrix t
71flipud m = fromRows . reverse . toRows $ m 71flipud m = fromRows . reverse . toRows $ m
72 72
73-- | Reverse columns 73-- | Reverse columns
74fliprl :: Field t => Matrix t -> Matrix t 74fliprl :: Element t => Matrix t -> Matrix t
75fliprl m = fromColumns . reverse . toColumns $ m 75fliprl m = fromColumns . reverse . toColumns $ m
76 76
77------------------------------------------------------------ 77------------------------------------------------------------
@@ -84,7 +84,7 @@ fliprl m = fromColumns . reverse . toColumns $ m
84 , 0.0, 5.0, 0.0, 0.0 84 , 0.0, 5.0, 0.0, 0.0
85 , 0.0, 0.0, 5.0, 0.0 ]@ 85 , 0.0, 0.0, 5.0, 0.0 ]@
86-} 86-}
87diagRect :: (Field t, Num t) => Vector t -> Int -> Int -> Matrix t 87diagRect :: (Element t, Num t) => Vector t -> Int -> Int -> Matrix t
88diagRect s r c 88diagRect s r c
89 | dim s < min r c = error "diagRect" 89 | dim s < min r c = error "diagRect"
90 | r == c = diag s 90 | r == c = diag s
@@ -93,11 +93,11 @@ diagRect s r c
93 where zeros (r,c) = reshape c $ constantD 0 (r*c) 93 where zeros (r,c) = reshape c $ constantD 0 (r*c)
94 94
95-- | extracts the diagonal from a rectangular matrix 95-- | extracts the diagonal from a rectangular matrix
96takeDiag :: (Field t) => Matrix t -> Vector t 96takeDiag :: (Element t) => Matrix t -> Vector t
97takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] 97takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]]
98 98
99-- | creates the identity matrix of given dimension 99-- | creates the identity matrix of given dimension
100ident :: Field a => Int -> Matrix a 100ident :: Element a => Int -> Matrix a
101ident n = diag (constant 1 n) 101ident n = diag (constant 1 n)
102 102
103------------------------------------------------------------ 103------------------------------------------------------------
@@ -112,7 +112,7 @@ ident n = diag (constant 1 n)
112This is the format produced by the instances of Show (Matrix a), which 112This is the format produced by the instances of Show (Matrix a), which
113can also be used for input. 113can also be used for input.
114-} 114-}
115(><) :: (Field a) => Int -> Int -> [a] -> Matrix a 115(><) :: (Element a) => Int -> Int -> [a] -> Matrix a
116r >< c = f where 116r >< c = f where
117 f l | dim v == r*c = matrixFromVector RowMajor c v 117 f l | dim v == r*c = matrixFromVector RowMajor c v
118 | otherwise = error $ "inconsistent list size = " 118 | otherwise = error $ "inconsistent list size = "
@@ -122,16 +122,16 @@ r >< c = f where
122---------------------------------------------------------------- 122----------------------------------------------------------------
123 123
124-- | Creates a matrix with the first n rows of another matrix 124-- | Creates a matrix with the first n rows of another matrix
125takeRows :: Field t => Int -> Matrix t -> Matrix t 125takeRows :: Element t => Int -> Matrix t -> Matrix t
126takeRows n mat = subMatrix (0,0) (n, cols mat) mat 126takeRows n mat = subMatrix (0,0) (n, cols mat) mat
127-- | Creates a copy of a matrix without the first n rows 127-- | Creates a copy of a matrix without the first n rows
128dropRows :: Field t => Int -> Matrix t -> Matrix t 128dropRows :: Element t => Int -> Matrix t -> Matrix t
129dropRows n mat = subMatrix (n,0) (rows mat - n, cols mat) mat 129dropRows n mat = subMatrix (n,0) (rows mat - n, cols mat) mat
130-- |Creates a matrix with the first n columns of another matrix 130-- |Creates a matrix with the first n columns of another matrix
131takeColumns :: Field t => Int -> Matrix t -> Matrix t 131takeColumns :: Element t => Int -> Matrix t -> Matrix t
132takeColumns n mat = subMatrix (0,0) (rows mat, n) mat 132takeColumns n mat = subMatrix (0,0) (rows mat, n) mat
133-- | Creates a copy of a matrix without the first n columns 133-- | Creates a copy of a matrix without the first n columns
134dropColumns :: Field t => Int -> Matrix t -> Matrix t 134dropColumns :: Element t => Int -> Matrix t -> Matrix t
135dropColumns n mat = subMatrix (0,n) (rows mat, cols mat - n) mat 135dropColumns n mat = subMatrix (0,n) (rows mat, cols mat - n) mat
136 136
137---------------------------------------------------------------- 137----------------------------------------------------------------
@@ -141,7 +141,7 @@ dropColumns n mat = subMatrix (0,n) (rows mat, cols mat - n) mat
141@\> flatten ('ident' 3) 141@\> flatten ('ident' 3)
1429 |> [1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]@ 1429 |> [1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]@
143-} 143-}
144flatten :: Field t => Matrix t -> Vector t 144flatten :: Element t => Matrix t -> Vector t
145flatten = cdat 145flatten = cdat
146 146
147{- | Creates a 'Matrix' from a list of lists (considered as rows). 147{- | Creates a 'Matrix' from a list of lists (considered as rows).
@@ -152,20 +152,20 @@ flatten = cdat
152 , 3.0, 4.0 152 , 3.0, 4.0
153 , 5.0, 6.0 ]@ 153 , 5.0, 6.0 ]@
154-} 154-}
155fromLists :: Field t => [[t]] -> Matrix t 155fromLists :: Element t => [[t]] -> Matrix t
156fromLists = fromRows . map fromList 156fromLists = fromRows . map fromList
157 157
158-- | creates a 1-row matrix from a vector 158-- | creates a 1-row matrix from a vector
159asRow :: Field a => Vector a -> Matrix a 159asRow :: Element a => Vector a -> Matrix a
160asRow v = reshape (dim v) v 160asRow v = reshape (dim v) v
161 161
162-- | creates a 1-column matrix from a vector 162-- | creates a 1-column matrix from a vector
163asColumn :: Field a => Vector a -> Matrix a 163asColumn :: Element a => Vector a -> Matrix a
164asColumn v = reshape 1 v 164asColumn v = reshape 1 v
165 165
166----------------------------------------------------- 166-----------------------------------------------------
167 167
168fromArray2D :: (Field e) => Array (Int, Int) e -> Matrix e 168fromArray2D :: (Element e) => Array (Int, Int) e -> Matrix e
169fromArray2D m = (r><c) (elems m) 169fromArray2D m = (r><c) (elems m)
170 where ((r0,c0),(r1,c1)) = bounds m 170 where ((r0,c0),(r1,c1)) = bounds m
171 r = r1-r0+1 171 r = r1-r0+1
@@ -201,7 +201,7 @@ this function the user can easily define any desired display function:
201@disp = putStrLn . format \" \" (printf \"%.2f\")@ 201@disp = putStrLn . format \" \" (printf \"%.2f\")@
202 202
203-} 203-}
204format :: (Field t) => String -> (t -> String) -> Matrix t -> String 204format :: (Element t) => String -> (t -> String) -> Matrix t -> String
205format sep f m = dsp' sep . map (map f) . toLists $ m 205format sep f m = dsp' sep . map (map f) . toLists $ m
206 206
207disp m f = putStrLn $ "matrix ("++show (rows m) ++"x"++ show (cols m) ++")\n"++format " | " f m 207disp m f = putStrLn $ "matrix ("++show (rows m) ++"x"++ show (cols m) ++")\n"++format " | " f m
@@ -217,7 +217,7 @@ readMatrix :: String -> Matrix Double
217readMatrix = fromLists . map (map read). map words . filter (not.null) . lines 217readMatrix = fromLists . map (map read). map words . filter (not.null) . lines
218 218
219-- | rearranges the rows of a matrix according to the order given in a list of integers. 219-- | rearranges the rows of a matrix according to the order given in a list of integers.
220extractRows :: Field t => [Int] -> Matrix t -> Matrix t 220extractRows :: Element t => [Int] -> Matrix t -> Matrix t
221extractRows l m = fromRows $ extract (toRows $ m) l 221extractRows l m = fromRows $ extract (toRows $ m) l
222 where extract l is = [l!!i |i<-is] 222 where extract l is = [l!!i |i<-is]
223 223
@@ -231,5 +231,5 @@ extractRows l m = fromRows $ extract (toRows $ m) l
231 , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ]@ 231 , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ]@
232 232
233-} 233-}
234repmat :: (Field t) => Matrix t -> Int -> Int -> Matrix t 234repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t
235repmat m r c = fromBlocks $ partit c $ replicate (r*c) m \ No newline at end of file 235repmat m r c = fromBlocks $ partit c $ replicate (r*c) m \ No newline at end of file