diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-04-30 16:10:20 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-04-30 16:10:20 +0200 |
commit | cf1379fed234cf99b2ccce6d9311bfc5c58ef4a3 (patch) | |
tree | 2532837500eb98af048404e33018fa88d8a7f535 /lib/Data/Packed/Matrix.hs | |
parent | b8b7b47450a1007d4d8c77bb7d04e2744c3bfcd4 (diff) |
improved documentation
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 81 |
1 files changed, 54 insertions, 27 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 5365c1c..f72bd15 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -116,13 +116,10 @@ Single row-column components are automatically expanded to match the | |||
116 | corresponding common row and column: | 116 | corresponding common row and column: |
117 | 117 | ||
118 | @ | 118 | @ |
119 | > let disp = putStr . dispf 2 | 119 | disp = putStr . dispf 2 |
120 | > let vector xs = fromList xs :: Vector Double | 120 | @ |
121 | > let diagl = diag . vector | ||
122 | > let rowm = asRow . vector | ||
123 | |||
124 | > disp $ fromBlocks [[ident 5, 7, rowm[10,20]], [3, diagl[1,2,3], 0]] | ||
125 | 121 | ||
122 | >>> disp $ fromBlocks [[ident 5, 7, row[10,20]], [3, diagl[1,2,3], 0]] | ||
126 | 8x10 | 123 | 8x10 |
127 | 1 0 0 0 0 7 7 7 10 20 | 124 | 1 0 0 0 0 7 7 7 10 20 |
128 | 0 1 0 0 0 7 7 7 10 20 | 125 | 0 1 0 0 0 7 7 7 10 20 |
@@ -132,7 +129,6 @@ corresponding common row and column: | |||
132 | 3 3 3 3 3 1 0 0 0 0 | 129 | 3 3 3 3 3 1 0 0 0 0 |
133 | 3 3 3 3 3 0 2 0 0 0 | 130 | 3 3 3 3 3 0 2 0 0 0 |
134 | 3 3 3 3 3 0 0 3 0 0 | 131 | 3 3 3 3 3 0 0 3 0 0 |
135 | @ | ||
136 | 132 | ||
137 | -} | 133 | -} |
138 | fromBlocks :: Element t => [[Matrix t]] -> Matrix t | 134 | fromBlocks :: Element t => [[Matrix t]] -> Matrix t |
@@ -163,7 +159,19 @@ adaptBlocks ms = ms' where | |||
163 | 159 | ||
164 | -------------------------------------------------------------------------------- | 160 | -------------------------------------------------------------------------------- |
165 | 161 | ||
166 | -- | create a block diagonal matrix | 162 | {- | create a block diagonal matrix |
163 | |||
164 | >>> disp 2 $ diagBlock [konst 1 (2,2), konst 2 (3,5), col [5,7]] | ||
165 | 7x8 | ||
166 | 1 1 0 0 0 0 0 0 | ||
167 | 1 1 0 0 0 0 0 0 | ||
168 | 0 0 2 2 2 2 2 0 | ||
169 | 0 0 2 2 2 2 2 0 | ||
170 | 0 0 2 2 2 2 2 0 | ||
171 | 0 0 0 0 0 0 0 5 | ||
172 | 0 0 0 0 0 0 0 7 | ||
173 | |||
174 | -} | ||
167 | diagBlock :: (Element t, Num t) => [Matrix t] -> Matrix t | 175 | diagBlock :: (Element t, Num t) => [Matrix t] -> Matrix t |
168 | diagBlock ms = fromBlocks $ zipWith f ms [0..] | 176 | diagBlock ms = fromBlocks $ zipWith f ms [0..] |
169 | where | 177 | where |
@@ -186,12 +194,13 @@ fliprl m = fromColumns . reverse . toColumns $ m | |||
186 | 194 | ||
187 | {- | creates a rectangular diagonal matrix: | 195 | {- | creates a rectangular diagonal matrix: |
188 | 196 | ||
189 | @> diagRect 7 (fromList [10,20,30]) 4 5 :: Matrix Double | 197 | >>> diagRect 7 (fromList [10,20,30]) 4 5 :: Matrix Double |
190 | (4><5) | 198 | (4><5) |
191 | [ 10.0, 7.0, 7.0, 7.0, 7.0 | 199 | [ 10.0, 7.0, 7.0, 7.0, 7.0 |
192 | , 7.0, 20.0, 7.0, 7.0, 7.0 | 200 | , 7.0, 20.0, 7.0, 7.0, 7.0 |
193 | , 7.0, 7.0, 30.0, 7.0, 7.0 | 201 | , 7.0, 7.0, 30.0, 7.0, 7.0 |
194 | , 7.0, 7.0, 7.0, 7.0, 7.0 ]@ | 202 | , 7.0, 7.0, 7.0, 7.0, 7.0 ] |
203 | |||
195 | -} | 204 | -} |
196 | diagRect :: (Storable t) => t -> Vector t -> Int -> Int -> Matrix t | 205 | diagRect :: (Storable t) => t -> Vector t -> Int -> Int -> Matrix t |
197 | diagRect z v r c = ST.runSTMatrix $ do | 206 | diagRect z v r c = ST.runSTMatrix $ do |
@@ -208,10 +217,10 @@ takeDiag m = fromList [flatten m `at` (k*cols m+k) | k <- [0 .. min (rows m) (co | |||
208 | 217 | ||
209 | {- | An easy way to create a matrix: | 218 | {- | An easy way to create a matrix: |
210 | 219 | ||
211 | @\> (2><3)[1..6] | 220 | >>> (2><3)[2,4,7,-3,11,0] |
212 | (2><3) | 221 | (2><3) |
213 | [ 1.0, 2.0, 3.0 | 222 | [ 2.0, 4.0, 7.0 |
214 | , 4.0, 5.0, 6.0 ]@ | 223 | , -3.0, 11.0, 0.0 ] |
215 | 224 | ||
216 | This is the format produced by the instances of Show (Matrix a), which | 225 | This is the format produced by the instances of Show (Matrix a), which |
217 | can also be used for input. | 226 | can also be used for input. |
@@ -219,12 +228,11 @@ can also be used for input. | |||
219 | The input list is explicitly truncated, so that it can | 228 | The input list is explicitly truncated, so that it can |
220 | safely be used with lists that are too long (like infinite lists). | 229 | safely be used with lists that are too long (like infinite lists). |
221 | 230 | ||
222 | Example: | 231 | >>> (2><3)[1..] |
223 | |||
224 | @\> (2><3)[1..] | ||
225 | (2><3) | 232 | (2><3) |
226 | [ 1.0, 2.0, 3.0 | 233 | [ 1.0, 2.0, 3.0 |
227 | , 4.0, 5.0, 6.0 ]@ | 234 | , 4.0, 5.0, 6.0 ] |
235 | |||
228 | 236 | ||
229 | -} | 237 | -} |
230 | (><) :: (Storable a) => Int -> Int -> [a] -> Matrix a | 238 | (><) :: (Storable a) => Int -> Int -> [a] -> Matrix a |
@@ -253,20 +261,35 @@ dropColumns n mt = subMatrix (0,n) (rows mt, cols mt - n) mt | |||
253 | 261 | ||
254 | {- | Creates a 'Matrix' from a list of lists (considered as rows). | 262 | {- | Creates a 'Matrix' from a list of lists (considered as rows). |
255 | 263 | ||
256 | @\> fromLists [[1,2],[3,4],[5,6]] | 264 | >>> fromLists [[1,2],[3,4],[5,6]] |
257 | (3><2) | 265 | (3><2) |
258 | [ 1.0, 2.0 | 266 | [ 1.0, 2.0 |
259 | , 3.0, 4.0 | 267 | , 3.0, 4.0 |
260 | , 5.0, 6.0 ]@ | 268 | , 5.0, 6.0 ] |
269 | |||
261 | -} | 270 | -} |
262 | fromLists :: Element t => [[t]] -> Matrix t | 271 | fromLists :: Element t => [[t]] -> Matrix t |
263 | fromLists = fromRows . map fromList | 272 | fromLists = fromRows . map fromList |
264 | 273 | ||
265 | -- | creates a 1-row matrix from a vector | 274 | -- | creates a 1-row matrix from a vector |
275 | -- | ||
276 | -- >>> asRow (fromList [1..5]) | ||
277 | -- (1><5) | ||
278 | -- [ 1.0, 2.0, 3.0, 4.0, 5.0 ] | ||
279 | -- | ||
266 | asRow :: Storable a => Vector a -> Matrix a | 280 | asRow :: Storable a => Vector a -> Matrix a |
267 | asRow v = reshape (dim v) v | 281 | asRow v = reshape (dim v) v |
268 | 282 | ||
269 | -- | creates a 1-column matrix from a vector | 283 | -- | creates a 1-column matrix from a vector |
284 | -- | ||
285 | -- >>> asColumn (fromList [1..5]) | ||
286 | -- (5><1) | ||
287 | -- [ 1.0 | ||
288 | -- , 2.0 | ||
289 | -- , 3.0 | ||
290 | -- , 4.0 | ||
291 | -- , 5.0 ] | ||
292 | -- | ||
270 | asColumn :: Storable a => Vector a -> Matrix a | 293 | asColumn :: Storable a => Vector a -> Matrix a |
271 | asColumn v = reshape 1 v | 294 | asColumn v = reshape 1 v |
272 | 295 | ||
@@ -307,12 +330,12 @@ extractRows l m = fromRows $ extract (toRows m) l | |||
307 | 330 | ||
308 | {- | creates matrix by repetition of a matrix a given number of rows and columns | 331 | {- | creates matrix by repetition of a matrix a given number of rows and columns |
309 | 332 | ||
310 | @> repmat (ident 2) 2 3 :: Matrix Double | 333 | >>> repmat (ident 2) 2 3 |
311 | (4><6) | 334 | (4><6) |
312 | [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 | 335 | [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 |
313 | , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 | 336 | , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 |
314 | , 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 | 337 | , 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 |
315 | , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ]@ | 338 | , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ] |
316 | 339 | ||
317 | -} | 340 | -} |
318 | repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t | 341 | repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t |
@@ -376,13 +399,14 @@ mk c g = \k -> g (divMod k c) | |||
376 | 399 | ||
377 | {- | | 400 | {- | |
378 | 401 | ||
379 | @ghci> mapMatrixWithIndexM_ (\\(i,j) v -> printf \"m[%.0f,%.0f] = %.f\\n\" i j v :: IO()) ((2><3)[1 :: Double ..]) | 402 | >>> mapMatrixWithIndexM_ (\(i,j) v -> printf "m[%d,%d] = %.f\n" i j v :: IO()) ((2><3)[1 :: Double ..]) |
380 | m[0,0] = 1 | 403 | m[0,0] = 1 |
381 | m[0,1] = 2 | 404 | m[0,1] = 2 |
382 | m[0,2] = 3 | 405 | m[0,2] = 3 |
383 | m[1,0] = 4 | 406 | m[1,0] = 4 |
384 | m[1,1] = 5 | 407 | m[1,1] = 5 |
385 | m[1,2] = 6@ | 408 | m[1,2] = 6 |
409 | |||
386 | -} | 410 | -} |
387 | mapMatrixWithIndexM_ | 411 | mapMatrixWithIndexM_ |
388 | :: (Element a, Num a, Monad m) => | 412 | :: (Element a, Num a, Monad m) => |
@@ -393,11 +417,12 @@ mapMatrixWithIndexM_ g m = mapVectorWithIndexM_ (mk c g) . flatten $ m | |||
393 | 417 | ||
394 | {- | | 418 | {- | |
395 | 419 | ||
396 | @ghci> mapMatrixWithIndexM (\\(i,j) v -> Just $ 100*v + 10*i + j) (ident 3:: Matrix Double) | 420 | >>> mapMatrixWithIndexM (\(i,j) v -> Just $ 100*v + 10*fromIntegral i + fromIntegral j) (ident 3:: Matrix Double) |
397 | Just (3><3) | 421 | Just (3><3) |
398 | [ 100.0, 1.0, 2.0 | 422 | [ 100.0, 1.0, 2.0 |
399 | , 10.0, 111.0, 12.0 | 423 | , 10.0, 111.0, 12.0 |
400 | , 20.0, 21.0, 122.0 ]@ | 424 | , 20.0, 21.0, 122.0 ] |
425 | |||
401 | -} | 426 | -} |
402 | mapMatrixWithIndexM | 427 | mapMatrixWithIndexM |
403 | :: (Element a, Storable b, Monad m) => | 428 | :: (Element a, Storable b, Monad m) => |
@@ -407,11 +432,13 @@ mapMatrixWithIndexM g m = liftM (reshape c) . mapVectorWithIndexM (mk c g) . fla | |||
407 | c = cols m | 432 | c = cols m |
408 | 433 | ||
409 | {- | | 434 | {- | |
410 | @ghci> mapMatrixWithIndex (\\(i,j) v -> 100*v + 10*i + j) (ident 3:: Matrix Double) | 435 | |
436 | >>> mapMatrixWithIndex (\\(i,j) v -> 100*v + 10*fromIntegral i + fromIntegral j) (ident 3:: Matrix Double) | ||
411 | (3><3) | 437 | (3><3) |
412 | [ 100.0, 1.0, 2.0 | 438 | [ 100.0, 1.0, 2.0 |
413 | , 10.0, 111.0, 12.0 | 439 | , 10.0, 111.0, 12.0 |
414 | , 20.0, 21.0, 122.0 ]@ | 440 | , 20.0, 21.0, 122.0 ] |
441 | |||
415 | -} | 442 | -} |
416 | mapMatrixWithIndex | 443 | mapMatrixWithIndex |
417 | :: (Element a, Storable b) => | 444 | :: (Element a, Storable b) => |