summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/base/src/Internal/Element.hs32
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
95ppext All = ":"
96ppext (Range a 1 c) = printf "%d:%d" a c
97ppext (Range a b c) = printf "%d:%d:%d" a b c
98ppext (Pos v) = show (toList v)
99ppext (PosCyc v) = "Cyclic"++show (toList v)
100ppext (Take n) = printf "Take %d" n
101ppext (Drop n) = printf "Drop %d" n
102ppext (TakeLast n) = printf "TakeLast %d" n
103ppext (DropLast n) = printf "DropLast %d" n
94 104
95 105
96--
97infixl 9 ?? 106infixl 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
101maxEl = toScalarI Max 110maxEl = toScalarI Max
102cmodi = vectorMapValI ModVS 111cmodi = vectorMapValI ModVS
103 112
104extractError m e = error $ printf "can't extract %s from matrix %dx%d" (show e) (rows m) (cols m) 113extractError 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
106m ?? (Range a s b,e) | s /= 1 = m ?? (Pos (idxs [a,a+s .. b]), e) 115m ?? (Range a s b,e) | s /= 1 = m ?? (Pos (idxs [a,a+s .. b]), e)
107m ?? (e,Range a s b) | s /= 1 = m ?? (e, Pos (idxs [a,a+s .. b])) 116m ?? (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.
405extractRows :: Element t => [Int] -> Matrix t -> Matrix t 414extractRows :: Element t => [Int] -> Matrix t -> Matrix t
406extractRows [] m = emptyM 0 (cols m) 415extractRows l m = m ?? (Pos (idxs l), All)
407extractRows 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.
416extractColumns :: Element t => [Int] -> Matrix t -> Matrix t 418extractColumns :: Element t => [Int] -> Matrix t -> Matrix t
417extractColumns l m = trans . extractRows (map verify l) . trans $ m 419extractColumns 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