summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Matrix.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-02-01 10:29:20 +0000
committerAlberto Ruiz <aruiz@um.es>2010-02-01 10:29:20 +0000
commit2a3baa18b508851a9d30e4abc7d7de7978146557 (patch)
treeadc4a32b9461678bb468b1ce48cb9759679d5a61 /lib/Data/Packed/Matrix.hs
parent6f4137cabbc16fa616e823db3d1b2cf90c03e5c9 (diff)
export liftMatrix2Auto
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r--lib/Data/Packed/Matrix.hs27
1 files changed, 26 insertions, 1 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 4cb7a88..66e5082 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -27,7 +27,7 @@ module Data.Packed.Matrix (
27 subMatrix, takeRows, dropRows, takeColumns, dropColumns, 27 subMatrix, takeRows, dropRows, takeColumns, dropColumns,
28 extractRows, 28 extractRows,
29 ident, diag, diagRect, takeDiag, 29 ident, diag, diagRect, takeDiag,
30 liftMatrix, liftMatrix2, 30 liftMatrix, liftMatrix2, liftMatrix2Auto,
31 format, dispf, disps, 31 format, dispf, disps,
32 loadMatrix, saveMatrix, fromFile, fileDimensions, 32 loadMatrix, saveMatrix, fromFile, fileDimensions,
33 readMatrix, fromArray2D 33 readMatrix, fromArray2D
@@ -386,3 +386,28 @@ extractRows l m = fromRows $ extract (toRows $ m) l
386-} 386-}
387repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t 387repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t
388repmat m r c = fromBlocks $ partit c $ replicate (r*c) m 388repmat m r c = fromBlocks $ partit c $ replicate (r*c) m
389
390-- | A version of 'liftMatrix2' which automatically adapt matrices with a single row or column to match the dimensions of the other matrix.
391liftMatrix2Auto :: (Element t, Element a, Element b)
392 => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t
393liftMatrix2Auto f m1 m2 | compat' m1 m2 = lM f m1 m2
394 | rows m1 == rows m2 && cols m2 == 1 = lM f m1 (repCols (cols m1) m2)
395 | rows m1 == rows m2 && cols m1 == 1 = lM f (repCols (cols m2) m1) m2
396 | cols m1 == cols m2 && rows m2 == 1 = lM f m1 (repRows (rows m1) m2)
397 | cols m1 == cols m2 && cols m1 == 1 = lM f (repRows (rows m2) m1) m2
398 | rows m1 == 1 && cols m2 == 1 = lM f (repRows (rows m2) m1) (repCols (cols m1) m2)
399 | cols m1 == 1 && rows m2 == 1 = lM f (repCols (cols m2) m1) (repRows (rows m1) m2)
400 | otherwise = error $ "nonconformable matrices in liftMatrix2Auto: " ++ show (size m1) ++ ", " ++ show (size m2)
401
402size m = (rows m, cols m)
403
404lM f m1 m2 = reshape (max (cols m1) (cols m2)) (f (flatten m1) (flatten m2))
405
406repRows n x = fromRows (replicate n (flatten x))
407repCols n x = fromColumns (replicate n (flatten x))
408
409compat' :: Matrix a -> Matrix b -> Bool
410compat' m1 m2 = rows m1 == 1 && cols m1 == 1
411 || rows m2 == 1 && cols m2 == 1
412 || rows m1 == rows m2 && cols m1 == cols m2
413