diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-02-01 10:29:20 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-02-01 10:29:20 +0000 |
commit | 2a3baa18b508851a9d30e4abc7d7de7978146557 (patch) | |
tree | adc4a32b9461678bb468b1ce48cb9759679d5a61 /lib/Data | |
parent | 6f4137cabbc16fa616e823db3d1b2cf90c03e5c9 (diff) |
export liftMatrix2Auto
Diffstat (limited to 'lib/Data')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 27 |
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 | -} |
387 | repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t | 387 | repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t |
388 | repmat m r c = fromBlocks $ partit c $ replicate (r*c) m | 388 | repmat 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. | ||
391 | liftMatrix2Auto :: (Element t, Element a, Element b) | ||
392 | => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | ||
393 | liftMatrix2Auto 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 | |||
402 | size m = (rows m, cols m) | ||
403 | |||
404 | lM f m1 m2 = reshape (max (cols m1) (cols m2)) (f (flatten m1) (flatten m2)) | ||
405 | |||
406 | repRows n x = fromRows (replicate n (flatten x)) | ||
407 | repCols n x = fromColumns (replicate n (flatten x)) | ||
408 | |||
409 | compat' :: Matrix a -> Matrix b -> Bool | ||
410 | compat' 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 | |||