diff options
author | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-09-05 08:11:17 +0000 |
---|---|---|
committer | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-09-05 08:11:17 +0000 |
commit | fa4e2233a873bbfee26939c013b56acc160bca7b (patch) | |
tree | ba2152dfd8ae8ffa6ead19c1924747c2134a3190 /lib/Numeric/Vector.hs | |
parent | b59a56c22f7e4aa518046c41e049e5bf1cdf8204 (diff) |
refactor Numeric Vector/Matrix and classes
Diffstat (limited to 'lib/Numeric/Vector.hs')
-rw-r--r-- | lib/Numeric/Vector.hs | 317 |
1 files changed, 317 insertions, 0 deletions
diff --git a/lib/Numeric/Vector.hs b/lib/Numeric/Vector.hs new file mode 100644 index 0000000..ced202f --- /dev/null +++ b/lib/Numeric/Vector.hs | |||
@@ -0,0 +1,317 @@ | |||
1 | {-# LANGUAGE TypeFamilies #-} | ||
2 | {-# LANGUAGE FlexibleContexts #-} | ||
3 | {-# LANGUAGE FlexibleInstances #-} | ||
4 | {-# LANGUAGE UndecidableInstances #-} | ||
5 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
6 | --{-# LANGUAGE FunctionalDependencies #-} | ||
7 | ----------------------------------------------------------------------------- | ||
8 | -- | | ||
9 | -- Module : Numeric.Vector | ||
10 | -- Copyright : (c) Alberto Ruiz 2007 | ||
11 | -- License : GPL-style | ||
12 | -- | ||
13 | -- Maintainer : Alberto Ruiz <aruiz@um.es> | ||
14 | -- Stability : provisional | ||
15 | -- Portability : portable | ||
16 | -- | ||
17 | -- Numeric instances and functions for 'Data.Packed.Vector's | ||
18 | -- | ||
19 | ----------------------------------------------------------------------------- | ||
20 | |||
21 | module Numeric.Vector ( | ||
22 | -- * Vector creation | ||
23 | constant, linspace, | ||
24 | module Data.Packed.Vector | ||
25 | ) where | ||
26 | |||
27 | import Data.Complex | ||
28 | |||
29 | import Control.Monad(ap) | ||
30 | |||
31 | import Data.Packed.Vector | ||
32 | import Data.Packed.Matrix(Element(..)) | ||
33 | import Numeric.GSL.Vector | ||
34 | |||
35 | import Numeric.Container | ||
36 | |||
37 | ------------------------------------------------------------------- | ||
38 | |||
39 | #ifndef VECTOR | ||
40 | import Foreign(Storable) | ||
41 | #endif | ||
42 | |||
43 | ------------------------------------------------------------------ | ||
44 | |||
45 | #ifndef VECTOR | ||
46 | |||
47 | instance (Show a, Storable a) => (Show (Vector a)) where | ||
48 | show v = (show (dim v))++" |> " ++ show (toList v) | ||
49 | |||
50 | #endif | ||
51 | |||
52 | #ifdef VECTOR | ||
53 | |||
54 | instance (Element a, Read a) => Read (Vector a) where | ||
55 | readsPrec _ s = [(fromList . read $ listnums, rest)] | ||
56 | where (thing,trest) = breakAt ']' s | ||
57 | (dims,listnums) = breakAt ' ' (dropWhile (==' ') thing) | ||
58 | rest = drop 31 trest | ||
59 | #else | ||
60 | |||
61 | instance (Element a, Read a) => Read (Vector a) where | ||
62 | readsPrec _ s = [((d |>) . read $ listnums, rest)] | ||
63 | where (thing,rest) = breakAt ']' s | ||
64 | (dims,listnums) = breakAt '>' thing | ||
65 | d = read . init . fst . breakAt '|' $ dims | ||
66 | |||
67 | #endif | ||
68 | |||
69 | breakAt c l = (a++[c],tail b) where | ||
70 | (a,b) = break (==c) l | ||
71 | |||
72 | ------------------------------------------------------------------ | ||
73 | |||
74 | {- | creates a vector with a given number of equal components: | ||
75 | |||
76 | @> constant 2 7 | ||
77 | 7 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ | ||
78 | -} | ||
79 | constant :: Element a => a -> Int -> Vector a | ||
80 | -- constant x n = runSTVector (newVector x n) | ||
81 | constant = constantD -- about 2x faster | ||
82 | |||
83 | {- | Creates a real vector containing a range of values: | ||
84 | |||
85 | @\> linspace 5 (-3,7) | ||
86 | 5 |> [-3.0,-0.5,2.0,4.5,7.0]@ | ||
87 | |||
88 | Logarithmic spacing can be defined as follows: | ||
89 | |||
90 | @logspace n (a,b) = 10 ** linspace n (a,b)@ | ||
91 | -} | ||
92 | linspace :: (Enum e, Linear Vector e) => Int -> (e, e) -> Vector e | ||
93 | linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] | ||
94 | where s = (b-a)/fromIntegral (n-1) | ||
95 | |||
96 | ------------------------------------------------------------------ | ||
97 | |||
98 | adaptScalar f1 f2 f3 x y | ||
99 | | dim x == 1 = f1 (x@>0) y | ||
100 | | dim y == 1 = f3 x (y@>0) | ||
101 | | otherwise = f2 x y | ||
102 | |||
103 | ------------------------------------------------------------------ | ||
104 | |||
105 | #ifndef VECTOR | ||
106 | |||
107 | instance Linear Vector a => Eq (Vector a) where | ||
108 | (==) = equal | ||
109 | |||
110 | #endif | ||
111 | |||
112 | instance Num (Vector Float) where | ||
113 | (+) = adaptScalar addConstant add (flip addConstant) | ||
114 | negate = scale (-1) | ||
115 | (*) = adaptScalar scale mul (flip scale) | ||
116 | signum = vectorMapF Sign | ||
117 | abs = vectorMapF Abs | ||
118 | fromInteger = fromList . return . fromInteger | ||
119 | |||
120 | instance Num (Vector Double) where | ||
121 | (+) = adaptScalar addConstant add (flip addConstant) | ||
122 | negate = scale (-1) | ||
123 | (*) = adaptScalar scale mul (flip scale) | ||
124 | signum = vectorMapR Sign | ||
125 | abs = vectorMapR Abs | ||
126 | fromInteger = fromList . return . fromInteger | ||
127 | |||
128 | instance Num (Vector (Complex Double)) where | ||
129 | (+) = adaptScalar addConstant add (flip addConstant) | ||
130 | negate = scale (-1) | ||
131 | (*) = adaptScalar scale mul (flip scale) | ||
132 | signum = vectorMapC Sign | ||
133 | abs = vectorMapC Abs | ||
134 | fromInteger = fromList . return . fromInteger | ||
135 | |||
136 | instance Num (Vector (Complex Float)) where | ||
137 | (+) = adaptScalar addConstant add (flip addConstant) | ||
138 | negate = scale (-1) | ||
139 | (*) = adaptScalar scale mul (flip scale) | ||
140 | signum = vectorMapQ Sign | ||
141 | abs = vectorMapQ Abs | ||
142 | fromInteger = fromList . return . fromInteger | ||
143 | |||
144 | --------------------------------------------------- | ||
145 | |||
146 | instance (Linear Vector a, Num (Vector a)) => Fractional (Vector a) where | ||
147 | fromRational n = fromList [fromRational n] | ||
148 | (/) = adaptScalar f divide g where | ||
149 | r `f` v = scaleRecip r v | ||
150 | v `g` r = scale (recip r) v | ||
151 | |||
152 | ------------------------------------------------------- | ||
153 | |||
154 | instance Floating (Vector Float) where | ||
155 | sin = vectorMapF Sin | ||
156 | cos = vectorMapF Cos | ||
157 | tan = vectorMapF Tan | ||
158 | asin = vectorMapF ASin | ||
159 | acos = vectorMapF ACos | ||
160 | atan = vectorMapF ATan | ||
161 | sinh = vectorMapF Sinh | ||
162 | cosh = vectorMapF Cosh | ||
163 | tanh = vectorMapF Tanh | ||
164 | asinh = vectorMapF ASinh | ||
165 | acosh = vectorMapF ACosh | ||
166 | atanh = vectorMapF ATanh | ||
167 | exp = vectorMapF Exp | ||
168 | log = vectorMapF Log | ||
169 | sqrt = vectorMapF Sqrt | ||
170 | (**) = adaptScalar (vectorMapValF PowSV) (vectorZipF Pow) (flip (vectorMapValF PowVS)) | ||
171 | pi = fromList [pi] | ||
172 | |||
173 | ------------------------------------------------------------- | ||
174 | |||
175 | instance Floating (Vector Double) where | ||
176 | sin = vectorMapR Sin | ||
177 | cos = vectorMapR Cos | ||
178 | tan = vectorMapR Tan | ||
179 | asin = vectorMapR ASin | ||
180 | acos = vectorMapR ACos | ||
181 | atan = vectorMapR ATan | ||
182 | sinh = vectorMapR Sinh | ||
183 | cosh = vectorMapR Cosh | ||
184 | tanh = vectorMapR Tanh | ||
185 | asinh = vectorMapR ASinh | ||
186 | acosh = vectorMapR ACosh | ||
187 | atanh = vectorMapR ATanh | ||
188 | exp = vectorMapR Exp | ||
189 | log = vectorMapR Log | ||
190 | sqrt = vectorMapR Sqrt | ||
191 | (**) = adaptScalar (vectorMapValR PowSV) (vectorZipR Pow) (flip (vectorMapValR PowVS)) | ||
192 | pi = fromList [pi] | ||
193 | |||
194 | ------------------------------------------------------------- | ||
195 | |||
196 | instance Floating (Vector (Complex Double)) where | ||
197 | sin = vectorMapC Sin | ||
198 | cos = vectorMapC Cos | ||
199 | tan = vectorMapC Tan | ||
200 | asin = vectorMapC ASin | ||
201 | acos = vectorMapC ACos | ||
202 | atan = vectorMapC ATan | ||
203 | sinh = vectorMapC Sinh | ||
204 | cosh = vectorMapC Cosh | ||
205 | tanh = vectorMapC Tanh | ||
206 | asinh = vectorMapC ASinh | ||
207 | acosh = vectorMapC ACosh | ||
208 | atanh = vectorMapC ATanh | ||
209 | exp = vectorMapC Exp | ||
210 | log = vectorMapC Log | ||
211 | sqrt = vectorMapC Sqrt | ||
212 | (**) = adaptScalar (vectorMapValC PowSV) (vectorZipC Pow) (flip (vectorMapValC PowVS)) | ||
213 | pi = fromList [pi] | ||
214 | |||
215 | ----------------------------------------------------------- | ||
216 | |||
217 | instance Floating (Vector (Complex Float)) where | ||
218 | sin = vectorMapQ Sin | ||
219 | cos = vectorMapQ Cos | ||
220 | tan = vectorMapQ Tan | ||
221 | asin = vectorMapQ ASin | ||
222 | acos = vectorMapQ ACos | ||
223 | atan = vectorMapQ ATan | ||
224 | sinh = vectorMapQ Sinh | ||
225 | cosh = vectorMapQ Cosh | ||
226 | tanh = vectorMapQ Tanh | ||
227 | asinh = vectorMapQ ASinh | ||
228 | acosh = vectorMapQ ACosh | ||
229 | atanh = vectorMapQ ATanh | ||
230 | exp = vectorMapQ Exp | ||
231 | log = vectorMapQ Log | ||
232 | sqrt = vectorMapQ Sqrt | ||
233 | (**) = adaptScalar (vectorMapValQ PowSV) (vectorZipQ Pow) (flip (vectorMapValQ PowVS)) | ||
234 | pi = fromList [pi] | ||
235 | |||
236 | ----------------------------------------------------------- | ||
237 | |||
238 | |||
239 | -- instance (Storable a, Num (Vector a)) => Monoid (Vector a) where | ||
240 | -- mempty = 0 { idim = 0 } | ||
241 | -- mappend a b = mconcat [a,b] | ||
242 | -- mconcat = j . filter ((>0).dim) | ||
243 | -- where j [] = mempty | ||
244 | -- j l = join l | ||
245 | |||
246 | --------------------------------------------------------------- | ||
247 | |||
248 | -- instance (NFData a, Storable a) => NFData (Vector a) where | ||
249 | -- rnf = rnf . (@>0) | ||
250 | -- | ||
251 | -- instance (NFData a, Element a) => NFData (Matrix a) where | ||
252 | -- rnf = rnf . flatten | ||
253 | |||
254 | --------------------------------------------------------------- | ||
255 | |||
256 | instance Linear Vector Float where | ||
257 | scale = vectorMapValF Scale | ||
258 | scaleRecip = vectorMapValF Recip | ||
259 | addConstant = vectorMapValF AddConstant | ||
260 | add = vectorZipF Add | ||
261 | sub = vectorZipF Sub | ||
262 | mul = vectorZipF Mul | ||
263 | divide = vectorZipF Div | ||
264 | equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0 | ||
265 | scalar x = fromList [x] | ||
266 | minIndex = round . toScalarF MinIdx | ||
267 | maxIndex = round . toScalarF MaxIdx | ||
268 | minElement = toScalarF Min | ||
269 | maxElement = toScalarF Max | ||
270 | |||
271 | |||
272 | instance Linear Vector Double where | ||
273 | scale = vectorMapValR Scale | ||
274 | scaleRecip = vectorMapValR Recip | ||
275 | addConstant = vectorMapValR AddConstant | ||
276 | add = vectorZipR Add | ||
277 | sub = vectorZipR Sub | ||
278 | mul = vectorZipR Mul | ||
279 | divide = vectorZipR Div | ||
280 | equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0 | ||
281 | scalar x = fromList [x] | ||
282 | minIndex = round . toScalarR MinIdx | ||
283 | maxIndex = round . toScalarR MaxIdx | ||
284 | minElement = toScalarR Min | ||
285 | maxElement = toScalarR Max | ||
286 | |||
287 | instance Linear Vector (Complex Double) where | ||
288 | scale = vectorMapValC Scale | ||
289 | scaleRecip = vectorMapValC Recip | ||
290 | addConstant = vectorMapValC AddConstant | ||
291 | add = vectorZipC Add | ||
292 | sub = vectorZipC Sub | ||
293 | mul = vectorZipC Mul | ||
294 | divide = vectorZipC Div | ||
295 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 | ||
296 | scalar x = fromList [x] | ||
297 | minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
298 | maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
299 | minElement = ap (@>) minIndex | ||
300 | maxElement = ap (@>) maxIndex | ||
301 | |||
302 | instance Linear Vector (Complex Float) where | ||
303 | scale = vectorMapValQ Scale | ||
304 | scaleRecip = vectorMapValQ Recip | ||
305 | addConstant = vectorMapValQ AddConstant | ||
306 | add = vectorZipQ Add | ||
307 | sub = vectorZipQ Sub | ||
308 | mul = vectorZipQ Mul | ||
309 | divide = vectorZipQ Div | ||
310 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 | ||
311 | scalar x = fromList [x] | ||
312 | minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
313 | maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
314 | minElement = ap (@>) minIndex | ||
315 | maxElement = ap (@>) maxIndex | ||
316 | |||
317 | --------------------------------------------------------------- | ||