diff options
author | Alberto Ruiz <aruiz@um.es> | 2015-06-05 21:26:24 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2015-06-05 21:26:24 +0200 |
commit | 3e4cdbadb0277010b0e089615e3053f6705332cb (patch) | |
tree | 0cb23e3bd5ec500bbf2bd3e669175332668e6eb0 /packages/base/src | |
parent | ee2f0b36ff35d2190abf488977315c03ca86f8df (diff) |
simplify extract{Rows,Columns} using ??
Diffstat (limited to 'packages/base/src')
-rw-r--r-- | packages/base/src/Internal/Element.hs | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/packages/base/src/Internal/Element.hs b/packages/base/src/Internal/Element.hs index 0f61370..61a75d8 100644 --- a/packages/base/src/Internal/Element.hs +++ b/packages/base/src/Internal/Element.hs | |||
@@ -90,10 +90,19 @@ data Extractor | |||
90 | | TakeLast Int | 90 | | TakeLast Int |
91 | | Drop Int | 91 | | Drop Int |
92 | | DropLast Int | 92 | | DropLast Int |
93 | deriving Show | 93 | deriving Show |
94 | |||
95 | ppext All = ":" | ||
96 | ppext (Range a 1 c) = printf "%d:%d" a c | ||
97 | ppext (Range a b c) = printf "%d:%d:%d" a b c | ||
98 | ppext (Pos v) = show (toList v) | ||
99 | ppext (PosCyc v) = "Cyclic"++show (toList v) | ||
100 | ppext (Take n) = printf "Take %d" n | ||
101 | ppext (Drop n) = printf "Drop %d" n | ||
102 | ppext (TakeLast n) = printf "TakeLast %d" n | ||
103 | ppext (DropLast n) = printf "DropLast %d" n | ||
94 | 104 | ||
95 | 105 | ||
96 | -- | ||
97 | infixl 9 ?? | 106 | infixl 9 ?? |
98 | (??) :: Element t => Matrix t -> (Extractor,Extractor) -> Matrix t | 107 | (??) :: Element t => Matrix t -> (Extractor,Extractor) -> Matrix t |
99 | 108 | ||
@@ -101,7 +110,7 @@ minEl = toScalarI Min | |||
101 | maxEl = toScalarI Max | 110 | maxEl = toScalarI Max |
102 | cmodi = vectorMapValI ModVS | 111 | cmodi = vectorMapValI ModVS |
103 | 112 | ||
104 | extractError m e = error $ printf "can't extract %s from matrix %dx%d" (show e) (rows m) (cols m) | 113 | extractError m (e1,e2)= error $ printf "can't extract (%s,%s) from matrix %dx%d" (ppext e1::String) (ppext e2::String) (rows m) (cols m) |
105 | 114 | ||
106 | m ?? (Range a s b,e) | s /= 1 = m ?? (Pos (idxs [a,a+s .. b]), e) | 115 | m ?? (Range a s b,e) | s /= 1 = m ?? (Pos (idxs [a,a+s .. b]), e) |
107 | m ?? (e,Range a s b) | s /= 1 = m ?? (e, Pos (idxs [a,a+s .. b])) | 116 | m ?? (e,Range a s b) | s /= 1 = m ?? (e, Pos (idxs [a,a+s .. b])) |
@@ -403,24 +412,11 @@ fromArray2D m = (r><c) (elems m) | |||
403 | 412 | ||
404 | -- | rearranges the rows of a matrix according to the order given in a list of integers. | 413 | -- | rearranges the rows of a matrix according to the order given in a list of integers. |
405 | extractRows :: Element t => [Int] -> Matrix t -> Matrix t | 414 | extractRows :: Element t => [Int] -> Matrix t -> Matrix t |
406 | extractRows [] m = emptyM 0 (cols m) | 415 | extractRows l m = m ?? (Pos (idxs l), All) |
407 | extractRows l m = fromRows $ extract (toRows m) l | ||
408 | where | ||
409 | extract l' is = [l'!!i | i<- map verify is] | ||
410 | verify k | ||
411 | | k >= 0 && k < rows m = k | ||
412 | | otherwise = error $ "can't extract row " | ||
413 | ++show k++" in list " ++ show l ++ " from matrix " ++ shSize m | ||
414 | 416 | ||
415 | -- | rearranges the rows of a matrix according to the order given in a list of integers. | 417 | -- | rearranges the rows of a matrix according to the order given in a list of integers. |
416 | extractColumns :: Element t => [Int] -> Matrix t -> Matrix t | 418 | extractColumns :: Element t => [Int] -> Matrix t -> Matrix t |
417 | extractColumns l m = trans . extractRows (map verify l) . trans $ m | 419 | extractColumns l m = m ?? (All, Pos (idxs l)) |
418 | where | ||
419 | verify k | ||
420 | | k >= 0 && k < cols m = k | ||
421 | | otherwise = error $ "can't extract column " | ||
422 | ++show k++" in list " ++ show l ++ " from matrix " ++ shSize m | ||
423 | |||
424 | 420 | ||
425 | 421 | ||
426 | {- | creates matrix by repetition of a matrix a given number of rows and columns | 422 | {- | creates matrix by repetition of a matrix a given number of rows and columns |