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.hs25
1 files changed, 24 insertions, 1 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index e204a83..d91a089 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -22,7 +22,8 @@ module Data.Packed.Matrix (
22 (@@>), 22 (@@>),
23 asRow, asColumn, 23 asRow, asColumn,
24 fromRows, toRows, fromColumns, toColumns, 24 fromRows, toRows, fromColumns, toColumns,
25 fromBlocks, repmat, 25 fromBlocks, toBlocks, toBlocksEvery,
26 repmat,
26 flipud, fliprl, 27 flipud, fliprl,
27 subMatrix, takeRows, dropRows, takeColumns, dropColumns, 28 subMatrix, takeRows, dropRows, takeColumns, dropColumns,
28 extractRows, 29 extractRows,
@@ -413,3 +414,25 @@ compat' m1 m2 = rows m1 == 1 && cols m1 == 1
413 || rows m2 == 1 && cols m2 == 1 414 || rows m2 == 1 && cols m2 == 1
414 || rows m1 == rows m2 && cols m1 == cols m2 415 || rows m1 == rows m2 && cols m1 == cols m2
415 416
417------------------------------------------------------------
418
419toBlockRows [r] m | r == rows m = [m]
420toBlockRows rs m = map (reshape (cols m)) (takesV szs (flatten m))
421 where szs = map (* cols m) rs
422
423toBlockCols [c] m | c == cols m = [m]
424toBlockCols cs m = map trans . toBlockRows cs . trans $ m
425
426-- | Partition a matrix into blocks with the given numbers of rows and columns.
427-- The remaining rows and columns are discarded.
428toBlocks :: (Element t) => [Int] -> [Int] -> Matrix t -> [[Matrix t]]
429toBlocks rs cs m = map (toBlockCols cs) . toBlockRows rs $ m
430
431-- | Fully partition a matrix into blocks of the same size. If the dimensions are not
432-- a multiple of the given size the last blocks will be smaller.
433toBlocksEvery :: (Element t) => Int -> Int -> Matrix t -> [[Matrix t]]
434toBlocksEvery r c m = toBlocks rs cs m where
435 (qr,rr) = rows m `divMod` r
436 (qc,rc) = cols m `divMod` c
437 rs = replicate qr r ++ if rr > 0 then [rr] else []
438 cs = replicate qc c ++ if rc > 0 then [rc] else []