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.hs53
1 files changed, 50 insertions, 3 deletions
diff --git a/packages/base/src/Data/Packed/Internal/Numeric.hs b/packages/base/src/Data/Packed/Internal/Numeric.hs
index 9b6b55b..f1b4898 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, 42 CInt, Extractor(..), (??),(¿¿),
43 module Data.Complex 43 module Data.Complex
44) where 44) where
45 45
@@ -53,6 +53,7 @@ import Data.Complex
53import Numeric.LinearAlgebra.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ) 53import Numeric.LinearAlgebra.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ)
54import Data.Packed.Internal 54import Data.Packed.Internal
55import Foreign.C.Types(CInt) 55import Foreign.C.Types(CInt)
56import Text.Printf(printf)
56 57
57------------------------------------------------------------------- 58-------------------------------------------------------------------
58 59
@@ -66,8 +67,53 @@ type family ArgOf (c :: * -> *) a
66type instance ArgOf Vector a = a -> a 67type instance ArgOf Vector a = a -> a
67type instance ArgOf Matrix a = a -> a -> a 68type instance ArgOf Matrix a = a -> a -> a
68 69
70--------------------------------------------------------------------------
71
72data Extractor = All | Range Int Int | At [Int] | AtCyc [Int] | Take Int | Drop Int
73
74idxs js = fromList (map fromIntegral js) :: Idxs
75
76infixl 9 ??, ¿¿
77(??),(¿¿) :: Element t => Matrix t -> Extractor -> Matrix t
78
79m ?? All = m
80m ?? Take 0 = (0><cols m) []
81m ?? Take n | abs n >= rows m = m
82m ?? Drop 0 = m
83m ?? Drop n | abs n >= rows m = (0><cols m) []
84m ?? Range a b | a > b = m ?? Take 0
85m ?? Range a b | a < 0 || b >= cols m = error $
86 printf "can't extract rows %d to %d from matrix %dx%d" a b (rows m) (cols m)
87m ?? At ps | minimum ps < 0 || maximum ps >= rows m = error $
88 printf "can't extract rows %s from matrix %dx%d" (show ps) (rows m) (cols m)
89
90m ?? er = extractR m mode js
91 where
92 (mode,js) = mkExt (rows m) er
93 ran a b = (0, idxs [a,b])
94 pos ks = (1, idxs ks)
95 mkExt _ (At ks) = pos ks
96 mkExt n (AtCyc ks) = pos (map (`mod` n) ks)
97 mkExt n All = ran 0 (n-1)
98 mkExt _ (Range mn mx) = ran mn mx
99 mkExt n (Take k)
100 | k >= 0 = ran 0 (k-1)
101 | otherwise = mkExt n (Drop (n+k))
102 mkExt n (Drop k)
103 | k >= 0 = ran k (n-1)
104 | otherwise = mkExt n (Take (n+k))
105
106
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
69------------------------------------------------------------------- 114-------------------------------------------------------------------
70 115
116
71-- | Basic element-by-element functions for numeric containers 117-- | Basic element-by-element functions for numeric containers
72class Element e => SContainer c e 118class Element e => SContainer c e
73 where 119 where
@@ -123,6 +169,7 @@ class (Complexable c, Fractional e, SContainer c e) => Container c e
123 -- element by element inverse tangent 169 -- element by element inverse tangent
124 arctan2' :: c e -> c e -> c e 170 arctan2' :: c e -> c e -> c e
125 171
172
126-------------------------------------------------------------------------- 173--------------------------------------------------------------------------
127 174
128instance SContainer Vector CInt 175instance SContainer Vector CInt
@@ -245,14 +292,14 @@ instance SContainer Vector (Complex Double)
245 accum' = accumV 292 accum' = accumV
246 cond' = undefined -- cannot match 293 cond' = undefined -- cannot match
247 294
248 295
249instance Container Vector (Complex Double) 296instance Container Vector (Complex Double)
250 where 297 where
251 scaleRecip = vectorMapValC Recip 298 scaleRecip = vectorMapValC Recip
252 divide = vectorZipC Div 299 divide = vectorZipC Div
253 arctan2' = vectorZipC ATan2 300 arctan2' = vectorZipC ATan2
254 conj' = conjugateC 301 conj' = conjugateC
255 302
256 303
257instance SContainer Vector (Complex Float) 304instance SContainer Vector (Complex Float)
258 where 305 where