summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Matrix.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r--lib/Data/Packed/Matrix.hs44
1 files changed, 29 insertions, 15 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index c1a9b24..1b67820 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -30,7 +30,7 @@ module Data.Packed.Matrix (
30 (@@>), 30 (@@>),
31 asRow, asColumn, 31 asRow, asColumn,
32 fromRows, toRows, fromColumns, toColumns, 32 fromRows, toRows, fromColumns, toColumns,
33 fromBlocks, toBlocks, toBlocksEvery, 33 fromBlocks, diagBlock, toBlocks, toBlocksEvery,
34 repmat, 34 repmat,
35 flipud, fliprl, 35 flipud, fliprl,
36 subMatrix, takeRows, dropRows, takeColumns, dropColumns, 36 subMatrix, takeRows, dropRows, takeColumns, dropColumns,
@@ -46,6 +46,7 @@ import Data.Array
46 46
47import Data.List(transpose,intersperse) 47import Data.List(transpose,intersperse)
48import Foreign.Storable(Storable) 48import Foreign.Storable(Storable)
49import Control.Monad(liftM)
49 50
50------------------------------------------------------------------- 51-------------------------------------------------------------------
51 52
@@ -155,7 +156,19 @@ adaptBlocks ms = ms' where
155 x = m@@>(0,0) 156 x = m@@>(0,0)
156 g _ _ = error "inconsistent dimensions in fromBlocks" 157 g _ _ = error "inconsistent dimensions in fromBlocks"
157 158
158----------------------------------------------------------- 159
160--------------------------------------------------------------------------------
161
162-- | create a block diagonal matrix
163diagBlock :: (Element t, Num t) => [Matrix t] -> Matrix t
164diagBlock ms = fromBlocks $ zipWith f ms [0..]
165 where
166 f m k = take n $ replicate k z ++ m : repeat z
167 n = length ms
168 z = (1><1) [0]
169
170--------------------------------------------------------------------------------
171
159 172
160-- | Reverse rows 173-- | Reverse rows
161flipud :: Element t => Matrix t -> Matrix t 174flipud :: Element t => Matrix t -> Matrix t
@@ -351,7 +364,11 @@ toBlocksEvery r c m = toBlocks rs cs m where
351 364
352------------------------------------------------------------------- 365-------------------------------------------------------------------
353 366
354mk c g = \k v -> g (divMod k c) v 367-- Given a column number and a function taking matrix indexes, returns
368-- a function which takes vector indexes (that can be used on the
369-- flattened matrix).
370mk :: Int -> ((Int, Int) -> t) -> (Int -> t)
371mk c g = \k -> g (divMod k c)
355 372
356{- | 373{- |
357 374
@@ -364,9 +381,8 @@ m[1,1] = 5
364m[1,2] = 6@ 381m[1,2] = 6@
365-} 382-}
366mapMatrixWithIndexM_ 383mapMatrixWithIndexM_
367 :: (Element a, Num a, 384 :: (Element a, Num a, Monad m) =>
368 Functor f, Monad f) => 385 ((Int, Int) -> a -> m ()) -> Matrix a -> m ()
369 ((Int, Int) -> a -> f ()) -> Matrix a -> f ()
370mapMatrixWithIndexM_ g m = mapVectorWithIndexM_ (mk c g) . flatten $ m 386mapMatrixWithIndexM_ g m = mapVectorWithIndexM_ (mk c g) . flatten $ m
371 where 387 where
372 c = cols m 388 c = cols m
@@ -380,11 +396,9 @@ Just (3><3)
380 , 20.0, 21.0, 122.0 ]@ 396 , 20.0, 21.0, 122.0 ]@
381-} 397-}
382mapMatrixWithIndexM 398mapMatrixWithIndexM
383 :: (Foreign.Storable.Storable t, 399 :: (Element a, Storable b, Monad m) =>
384 Element a, Num a, 400 ((Int, Int) -> a -> m b) -> Matrix a -> m (Matrix b)
385 Functor f, Monad f) => 401mapMatrixWithIndexM g m = liftM (reshape c) . mapVectorWithIndexM (mk c g) . flatten $ m
386 ((Int, Int) -> a -> f t) -> Matrix a -> f (Matrix t)
387mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flatten $ m
388 where 402 where
389 c = cols m 403 c = cols m
390 404
@@ -395,10 +409,10 @@ mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flat
395 , 10.0, 111.0, 12.0 409 , 10.0, 111.0, 12.0
396 , 20.0, 21.0, 122.0 ]@ 410 , 20.0, 21.0, 122.0 ]@
397 -} 411 -}
398mapMatrixWithIndex :: (Foreign.Storable.Storable t, 412mapMatrixWithIndex
399 Element a, Num a) => 413 :: (Element a, Storable b) =>
400 ((Int, Int) -> a -> t) -> Matrix a -> Matrix t 414 ((Int, Int) -> a -> b) -> Matrix a -> Matrix b
401mapMatrixWithIndex g m = reshape c $ mapVectorWithIndex (mk c g) $ flatten m 415mapMatrixWithIndex g m = reshape c . mapVectorWithIndex (mk c g) . flatten $ m
402 where 416 where
403 c = cols m 417 c = cols m
404 418