summaryrefslogtreecommitdiff
path: root/packages/base/src/Data/Packed/Internal/Numeric.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/Data/Packed/Internal/Numeric.hs')
-rw-r--r--packages/base/src/Data/Packed/Internal/Numeric.hs66
1 files changed, 41 insertions, 25 deletions
diff --git a/packages/base/src/Data/Packed/Internal/Numeric.hs b/packages/base/src/Data/Packed/Internal/Numeric.hs
index 00ec70c..353877a 100644
--- a/packages/base/src/Data/Packed/Internal/Numeric.hs
+++ b/packages/base/src/Data/Packed/Internal/Numeric.hs
@@ -39,7 +39,7 @@ module Data.Packed.Internal.Numeric (
39 roundVector, 39 roundVector,
40 RealOf, ComplexOf, SingleOf, DoubleOf, 40 RealOf, ComplexOf, SingleOf, DoubleOf,
41 IndexOf, 41 IndexOf,
42 CInt, Extractor(..), (??),(¿¿), 42 CInt, Extractor(..), (??),
43 module Data.Complex 43 module Data.Complex
44) where 44) where
45 45
@@ -69,27 +69,50 @@ type instance ArgOf Matrix a = a -> a -> a
69 69
70-------------------------------------------------------------------------- 70--------------------------------------------------------------------------
71 71
72data Extractor = All | Range Int Int | At [Int] | AtCyc [Int] | Take Int | Drop Int 72data Extractor
73 = All
74 | Range Int Int
75 | At [Int]
76 | AtCyc [Int]
77 | Take Int
78 | Drop Int
79 deriving Show
73 80
74idxs js = fromList (map fromIntegral js) :: Idxs 81idxs js = fromList (map fromIntegral js) :: Idxs
75 82
76infixl 9 ??, ¿¿ 83infixl 9 ??
77(??),(¿¿) :: Element t => Matrix t -> Extractor -> Matrix t 84(??) :: Element t => Matrix t -> (Extractor,Extractor) -> Matrix t
78 85
79m ?? All = m 86
80m ?? Take 0 = (0><cols m) [] 87extractError m e = error $ printf "can't extract %s from matrix %dx%d" (show e) (rows m) (cols m)
81m ?? Take n | abs n >= rows m = m 88
82m ?? Drop 0 = m 89m ?? e@(Range a b,_) | a < 0 || b >= rows m = extractError m e
83m ?? Drop n | abs n >= rows m = (0><cols m) [] 90m ?? e@(_,Range a b) | a < 0 || b >= cols m = extractError m e
84m ?? Range a b | a > b = m ?? Take 0 91m ?? e@(At ps,_) | minimum ps < 0 || maximum ps >= rows m = extractError m e
85m ?? Range a b | a < 0 || b >= cols m = error $ 92m ?? e@(_,At ps) | minimum ps < 0 || maximum ps >= cols m = extractError m e
86 printf "can't extract rows %d to %d from matrix %dx%d" a b (rows m) (cols m) 93
87m ?? At ps | minimum ps < 0 || maximum ps >= rows m = error $ 94m ?? (All,All) = m
88 printf "can't extract rows %s from matrix %dx%d" (show ps) (rows m) (cols m) 95
89 96m ?? (Range a b,e) | a > b = m ?? (Take 0,e)
90m ?? er = extractR m mode js 97m ?? (e,Range a b) | a > b = m ?? (e,Take 0)
98
99m ?? (Take 0,e) = (0><cols m) [] ?? (All,e)
100m ?? (e,Take 0) = (rows m><0) [] ?? (e,All)
101
102m ?? (Take n,e) | abs n > rows m = m ?? (All,e)
103m ?? (e,Take n) | abs n > cols m = m ?? (e,All)
104
105m ?? (Drop 0,e) = m ?? (All,e)
106m ?? (e,Drop 0) = m ?? (e,All)
107
108m ?? (Drop n,e) | abs n > rows m = m ?? (Take 0,e)
109m ?? (e,Drop n) | abs n > cols m = m ?? (e,Take 0)
110
111
112m ?? (er,ec) = extractR m moder rs modec cs
91 where 113 where
92 (mode,js) = mkExt (rows m) er 114 (moder,rs) = mkExt (rows m) er
115 (modec,cs) = mkExt (cols m) ec
93 ran a b = (0, idxs [a,b]) 116 ran a b = (0, idxs [a,b])
94 pos ks = (1, idxs ks) 117 pos ks = (1, idxs ks)
95 mkExt _ (At ks) = pos ks 118 mkExt _ (At ks) = pos ks
@@ -104,13 +127,6 @@ m ?? er = extractR m mode js
104 | otherwise = mkExt n (Take (n+k)) 127 | otherwise = mkExt n (Take (n+k))
105 128
106 129
107m ¿¿ Range a b | a < 0 || b > cols m -1 = error $
108 printf "can't extract columns %d to %d from matrix %dx%d" a b (rows m) (cols m)
109
110m ¿¿ At ps | minimum ps < 0 || maximum ps >= cols m = error $
111 printf "can't extract columns %s from matrix %dx%d" (show ps) (rows m) (cols m)
112m ¿¿ ec = trans (trans m ?? ec)
113
114------------------------------------------------------------------- 130-------------------------------------------------------------------
115 131
116 132