summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Matrix.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-04-30 16:10:20 +0200
committerAlberto Ruiz <aruiz@um.es>2014-04-30 16:10:20 +0200
commitcf1379fed234cf99b2ccce6d9311bfc5c58ef4a3 (patch)
tree2532837500eb98af048404e33018fa88d8a7f535 /lib/Data/Packed/Matrix.hs
parentb8b7b47450a1007d4d8c77bb7d04e2744c3bfcd4 (diff)
improved documentation
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r--lib/Data/Packed/Matrix.hs81
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
116corresponding common row and column: 116corresponding common row and column:
117 117
118@ 118@
119> let disp = putStr . dispf 2 119disp = 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]]
1268x10 1238x10
1271 0 0 0 0 7 7 7 10 20 1241 0 0 0 0 7 7 7 10 20
1280 1 0 0 0 7 7 7 10 20 1250 1 0 0 0 7 7 7 10 20
@@ -132,7 +129,6 @@ corresponding common row and column:
1323 3 3 3 3 1 0 0 0 0 1293 3 3 3 3 1 0 0 0 0
1333 3 3 3 3 0 2 0 0 0 1303 3 3 3 3 0 2 0 0 0
1343 3 3 3 3 0 0 3 0 0 1313 3 3 3 3 0 0 3 0 0
135@
136 132
137-} 133-}
138fromBlocks :: Element t => [[Matrix t]] -> Matrix t 134fromBlocks :: 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]]
1657x8
1661 1 0 0 0 0 0 0
1671 1 0 0 0 0 0 0
1680 0 2 2 2 2 2 0
1690 0 2 2 2 2 2 0
1700 0 2 2 2 2 2 0
1710 0 0 0 0 0 0 5
1720 0 0 0 0 0 0 7
173
174-}
167diagBlock :: (Element t, Num t) => [Matrix t] -> Matrix t 175diagBlock :: (Element t, Num t) => [Matrix t] -> Matrix t
168diagBlock ms = fromBlocks $ zipWith f ms [0..] 176diagBlock 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-}
196diagRect :: (Storable t) => t -> Vector t -> Int -> Int -> Matrix t 205diagRect :: (Storable t) => t -> Vector t -> Int -> Int -> Matrix t
197diagRect z v r c = ST.runSTMatrix $ do 206diagRect 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
216This is the format produced by the instances of Show (Matrix a), which 225This is the format produced by the instances of Show (Matrix a), which
217can also be used for input. 226can also be used for input.
@@ -219,12 +228,11 @@ can also be used for input.
219The input list is explicitly truncated, so that it can 228The input list is explicitly truncated, so that it can
220safely be used with lists that are too long (like infinite lists). 229safely be used with lists that are too long (like infinite lists).
221 230
222Example: 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-}
262fromLists :: Element t => [[t]] -> Matrix t 271fromLists :: Element t => [[t]] -> Matrix t
263fromLists = fromRows . map fromList 272fromLists = 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--
266asRow :: Storable a => Vector a -> Matrix a 280asRow :: Storable a => Vector a -> Matrix a
267asRow v = reshape (dim v) v 281asRow 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--
270asColumn :: Storable a => Vector a -> Matrix a 293asColumn :: Storable a => Vector a -> Matrix a
271asColumn v = reshape 1 v 294asColumn 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-}
318repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t 341repmat :: (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 ..])
380m[0,0] = 1 403m[0,0] = 1
381m[0,1] = 2 404m[0,1] = 2
382m[0,2] = 3 405m[0,2] = 3
383m[1,0] = 4 406m[1,0] = 4
384m[1,1] = 5 407m[1,1] = 5
385m[1,2] = 6@ 408m[1,2] = 6
409
386-} 410-}
387mapMatrixWithIndexM_ 411mapMatrixWithIndexM_
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)
397Just (3><3) 421Just (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-}
402mapMatrixWithIndexM 427mapMatrixWithIndexM
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 -}
416mapMatrixWithIndex 443mapMatrixWithIndex
417 :: (Element a, Storable b) => 444 :: (Element a, Storable b) =>