diff options
Diffstat (limited to 'lib/Data/Packed/Instances.hs')
-rw-r--r-- | lib/Data/Packed/Instances.hs | 391 |
1 files changed, 0 insertions, 391 deletions
diff --git a/lib/Data/Packed/Instances.hs b/lib/Data/Packed/Instances.hs deleted file mode 100644 index 4478469..0000000 --- a/lib/Data/Packed/Instances.hs +++ /dev/null | |||
@@ -1,391 +0,0 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : Data.Packed.Instances | ||
5 | Copyright : (c) Alberto Ruiz 2006 | ||
6 | License : GPL-style | ||
7 | |||
8 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
9 | Stability : provisional | ||
10 | Portability : uses -fffi and -fglasgow-exts | ||
11 | |||
12 | Creates reasonable numeric instances for Vectors and Matrices. In the context of the standard numeric operators, one-component vectors and matrices automatically expand to match the dimensions of the other operand. | ||
13 | |||
14 | -} | ||
15 | ----------------------------------------------------------------------------- | ||
16 | |||
17 | module Data.Packed.Instances( | ||
18 | Contractible(..) | ||
19 | ) where | ||
20 | |||
21 | import Data.Packed.Internal | ||
22 | import Data.Packed.Vector | ||
23 | import Data.Packed.Matrix | ||
24 | import GSL.Vector | ||
25 | import GSL.Matrix | ||
26 | import LinearAlgebra.Algorithms | ||
27 | import Complex | ||
28 | |||
29 | instance (Eq a, Field a) => Eq (Vector a) where | ||
30 | a == b = dim a == dim b && toList a == toList b | ||
31 | |||
32 | instance (Num a, Field a) => Num (Vector a) where | ||
33 | (+) = add | ||
34 | (-) = sub | ||
35 | (*) = mul | ||
36 | signum = liftVector signum | ||
37 | abs = liftVector abs | ||
38 | fromInteger = fromList . return . fromInteger | ||
39 | |||
40 | instance (Eq a, Field a) => Eq (Matrix a) where | ||
41 | a == b = rows a == rows b && cols a == cols b && cdat a == cdat b && fdat a == fdat b | ||
42 | |||
43 | instance (Num a, Field a) => Num (Matrix a) where | ||
44 | (+) = liftMatrix2 add | ||
45 | (-) = liftMatrix2 sub | ||
46 | (*) = liftMatrix2 mul | ||
47 | signum = liftMatrix signum | ||
48 | abs = liftMatrix abs | ||
49 | fromInteger = (1><1) . return . fromInteger | ||
50 | |||
51 | --------------------------------------------------- | ||
52 | |||
53 | adaptScalar f1 f2 f3 x y | ||
54 | | dim x == 1 = f1 (x@>0) y | ||
55 | | dim y == 1 = f3 x (y@>0) | ||
56 | | otherwise = f2 x y | ||
57 | |||
58 | {- | ||
59 | subvv = vectorZip 4 | ||
60 | subvc v c = addConstant (-c) v | ||
61 | subcv c v = addConstant c (scale (-1) v) | ||
62 | |||
63 | mul = vectorZip 1 | ||
64 | |||
65 | instance Num (Vector Double) where | ||
66 | (+) = adaptScalar addConstant add (flip addConstant) | ||
67 | (-) = adaptScalar subcv subvv subvc | ||
68 | (*) = adaptScalar scale mul (flip scale) | ||
69 | abs = vectorMap 3 | ||
70 | signum = vectorMap 15 | ||
71 | fromInteger n = fromList [fromInteger n] | ||
72 | |||
73 | ---------------------------------------------------- | ||
74 | |||
75 | --addConstantC a = gmap (+a) | ||
76 | --subCvv u v = u `add` scale (-1) v | ||
77 | subCvv = vectorZipComplex 4 -- faster? | ||
78 | subCvc v c = addConstantC (-c) v | ||
79 | subCcv c v = addConstantC c (scale (-1) v) | ||
80 | |||
81 | |||
82 | instance Num (Vector (Complex Double)) where | ||
83 | (+) = adaptScalar addConstantC add (flip addConstantC) | ||
84 | (-) = adaptScalar subCcv subCvv subCvc | ||
85 | (*) = adaptScalar scale (vectorZipComplex 1) (flip scale) | ||
86 | abs = gmap abs | ||
87 | signum = gmap signum | ||
88 | fromInteger n = fromList [fromInteger n] | ||
89 | |||
90 | |||
91 | -- | adapts a function on two vectors to work on all the elements of two matrices | ||
92 | liftMatrix2' :: (Vector a -> Vector b -> Vector c) -> Matrix a -> Matrix b -> Matrix c | ||
93 | liftMatrix2' f m1@(M r1 c1 _) m2@(M r2 c2 _) | ||
94 | | sameShape m1 m2 || r1*c1==1 || r2*c2==1 | ||
95 | = reshape (max c1 c2) $ f (flatten m1) (flatten m2) | ||
96 | | otherwise = error "inconsistent matrix dimensions" | ||
97 | |||
98 | --------------------------------------------------- | ||
99 | |||
100 | instance (Eq a, Field a) => Eq (Matrix a) where | ||
101 | a == b = rows a == rows b && cdat a == cdat b | ||
102 | |||
103 | instance Num (Matrix Double) where | ||
104 | (+) = liftMatrix2' (+) | ||
105 | (-) = liftMatrix2' (-) | ||
106 | (*) = liftMatrix2' (*) | ||
107 | abs = liftMatrix abs | ||
108 | signum = liftMatrix signum | ||
109 | fromInteger n = fromLists [[fromInteger n]] | ||
110 | |||
111 | ---------------------------------------------------- | ||
112 | |||
113 | instance Num (Matrix (Complex Double)) where | ||
114 | (+) = liftMatrix2' (+) | ||
115 | (-) = liftMatrix2' (-) | ||
116 | (*) = liftMatrix2' (*) | ||
117 | abs = liftMatrix abs | ||
118 | signum = liftMatrix signum | ||
119 | fromInteger n = fromLists [[fromInteger n]] | ||
120 | |||
121 | ------------------------------------------------------ | ||
122 | |||
123 | instance Fractional (Vector Double) where | ||
124 | fromRational n = fromList [fromRational n] | ||
125 | (/) = adaptScalar f (vectorZip 2) g where | ||
126 | r `f` v = vectorZip 2 (constant r (dim v)) v | ||
127 | v `g` r = scale (recip r) v | ||
128 | |||
129 | ------------------------------------------------------- | ||
130 | |||
131 | instance Fractional (Vector (Complex Double)) where | ||
132 | fromRational n = fromList [fromRational n] | ||
133 | (/) = adaptScalar f (vectorZipComplex 2) g where | ||
134 | r `f` v = gmap ((*r).recip) v | ||
135 | v `g` r = gmap (/r) v | ||
136 | |||
137 | ------------------------------------------------------ | ||
138 | |||
139 | instance Fractional (Matrix Double) where | ||
140 | fromRational n = fromLists [[fromRational n]] | ||
141 | (/) = liftMatrix2' (/) | ||
142 | |||
143 | ------------------------------------------------------- | ||
144 | |||
145 | instance Fractional (Matrix (Complex Double)) where | ||
146 | fromRational n = fromLists [[fromRational n]] | ||
147 | (/) = liftMatrix2' (/) | ||
148 | |||
149 | --------------------------------------------------------- | ||
150 | |||
151 | instance Floating (Vector Double) where | ||
152 | sin = vectorMap 0 | ||
153 | cos = vectorMap 1 | ||
154 | tan = vectorMap 2 | ||
155 | asin = vectorMap 4 | ||
156 | acos = vectorMap 5 | ||
157 | atan = vectorMap 6 | ||
158 | sinh = vectorMap 7 | ||
159 | cosh = vectorMap 8 | ||
160 | tanh = vectorMap 9 | ||
161 | asinh = vectorMap 10 | ||
162 | acosh = vectorMap 11 | ||
163 | atanh = vectorMap 12 | ||
164 | exp = vectorMap 13 | ||
165 | log = vectorMap 14 | ||
166 | sqrt = vectorMap 16 | ||
167 | (**) = adaptScalar f (vectorZip 5) g where f s v = constant s (dim v) ** v | ||
168 | g v s = v ** constant s (dim v) | ||
169 | pi = fromList [pi] | ||
170 | |||
171 | ----------------------------------------------------------- | ||
172 | |||
173 | instance Floating (Matrix Double) where | ||
174 | sin = liftMatrix sin | ||
175 | cos = liftMatrix cos | ||
176 | tan = liftMatrix tan | ||
177 | asin = liftMatrix asin | ||
178 | acos = liftMatrix acos | ||
179 | atan = liftMatrix atan | ||
180 | sinh = liftMatrix sinh | ||
181 | cosh = liftMatrix cosh | ||
182 | tanh = liftMatrix tanh | ||
183 | asinh = liftMatrix asinh | ||
184 | acosh = liftMatrix acosh | ||
185 | atanh = liftMatrix atanh | ||
186 | exp = liftMatrix exp | ||
187 | log = liftMatrix log | ||
188 | sqrt = liftMatrix sqrt | ||
189 | (**) = liftMatrix2 (**) | ||
190 | pi = fromLists [[pi]] | ||
191 | |||
192 | ------------------------------------------------------------- | ||
193 | |||
194 | instance Floating (Vector (Complex Double)) where | ||
195 | sin = vectorMapComplex 0 | ||
196 | cos = vectorMapComplex 1 | ||
197 | tan = vectorMapComplex 2 | ||
198 | asin = vectorMapComplex 4 | ||
199 | acos = vectorMapComplex 5 | ||
200 | atan = vectorMapComplex 6 | ||
201 | sinh = vectorMapComplex 7 | ||
202 | cosh = vectorMapComplex 8 | ||
203 | tanh = vectorMapComplex 9 | ||
204 | asinh = vectorMapComplex 10 | ||
205 | acosh = vectorMapComplex 11 | ||
206 | atanh = vectorMapComplex 12 | ||
207 | exp = vectorMapComplex 13 | ||
208 | log = vectorMapComplex 14 | ||
209 | sqrt = vectorMapComplex 16 | ||
210 | (**) = adaptScalar f (vectorZipComplex 5) g where f s v = constantC s (dim v) ** v | ||
211 | g v s = v ** constantC s (dim v) | ||
212 | pi = fromList [pi] | ||
213 | |||
214 | --------------------------------------------------------------- | ||
215 | |||
216 | instance Floating (Matrix (Complex Double)) where | ||
217 | sin = liftMatrix sin | ||
218 | cos = liftMatrix cos | ||
219 | tan = liftMatrix tan | ||
220 | asin = liftMatrix asin | ||
221 | acos = liftMatrix acos | ||
222 | atan = liftMatrix atan | ||
223 | sinh = liftMatrix sinh | ||
224 | cosh = liftMatrix cosh | ||
225 | tanh = liftMatrix tanh | ||
226 | asinh = liftMatrix asinh | ||
227 | acosh = liftMatrix acosh | ||
228 | atanh = liftMatrix atanh | ||
229 | exp = liftMatrix exp | ||
230 | log = liftMatrix log | ||
231 | (**) = liftMatrix2 (**) | ||
232 | sqrt = liftMatrix sqrt | ||
233 | pi = fromLists [[pi]] | ||
234 | |||
235 | --------------------------------------------------------------- | ||
236 | -} | ||
237 | |||
238 | class Contractible a b c | a b -> c where | ||
239 | infixl 7 <> | ||
240 | {- | An overloaded operator for matrix products, matrix-vector and vector-matrix products, dot products and scaling of vectors and matrices. Type consistency is statically checked. Alternatively, you can use the specific functions described below, but using this operator you can automatically combine real and complex objects. | ||
241 | |||
242 | @v = 'fromList' [1,2,3] :: Vector Double | ||
243 | cv = 'fromList' [1+'i',2] | ||
244 | m = 'fromLists' [[1,2,3], | ||
245 | [4,5,7]] :: Matrix Double | ||
246 | cm = 'fromLists' [[ 1, 2], | ||
247 | [3+'i',7*'i'], | ||
248 | [ 'i', 1]] | ||
249 | \ | ||
250 | \> m \<\> v | ||
251 | 14. 35. | ||
252 | \ | ||
253 | \> cv \<\> m | ||
254 | 9.+1.i 12.+2.i 17.+3.i | ||
255 | \ | ||
256 | \> m \<\> cm | ||
257 | 7.+5.i 5.+14.i | ||
258 | 19.+12.i 15.+35.i | ||
259 | \ | ||
260 | \> v \<\> 'i' | ||
261 | 1.i 2.i 3.i | ||
262 | \ | ||
263 | \> v \<\> v | ||
264 | 14.0 | ||
265 | \ | ||
266 | \> cv \<\> cv | ||
267 | 4.0 :+ 2.0@ | ||
268 | |||
269 | -} | ||
270 | (<>) :: a -> b -> c | ||
271 | |||
272 | |||
273 | instance Contractible Double Double Double where | ||
274 | (<>) = (*) | ||
275 | |||
276 | instance Contractible Double (Complex Double) (Complex Double) where | ||
277 | a <> b = (a:+0) * b | ||
278 | |||
279 | instance Contractible (Complex Double) Double (Complex Double) where | ||
280 | a <> b = a * (b:+0) | ||
281 | |||
282 | instance Contractible (Complex Double) (Complex Double) (Complex Double) where | ||
283 | (<>) = (*) | ||
284 | |||
285 | --------------------------------- matrix matrix | ||
286 | |||
287 | instance Contractible (Matrix Double) (Matrix Double) (Matrix Double) where | ||
288 | (<>) = mXm | ||
289 | |||
290 | instance Contractible (Matrix (Complex Double)) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
291 | (<>) = mXm | ||
292 | |||
293 | instance Contractible (Matrix (Complex Double)) (Matrix Double) (Matrix (Complex Double)) where | ||
294 | c <> r = c <> liftMatrix comp r | ||
295 | |||
296 | instance Contractible (Matrix Double) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
297 | r <> c = liftMatrix comp r <> c | ||
298 | |||
299 | --------------------------------- (Matrix Double) (Vector Double) | ||
300 | |||
301 | instance Contractible (Matrix Double) (Vector Double) (Vector Double) where | ||
302 | (<>) = mXv | ||
303 | |||
304 | instance Contractible (Matrix (Complex Double)) (Vector (Complex Double)) (Vector (Complex Double)) where | ||
305 | (<>) = mXv | ||
306 | |||
307 | instance Contractible (Matrix (Complex Double)) (Vector Double) (Vector (Complex Double)) where | ||
308 | m <> v = m <> comp v | ||
309 | |||
310 | instance Contractible (Matrix Double) (Vector (Complex Double)) (Vector (Complex Double)) where | ||
311 | m <> v = liftMatrix comp m <> v | ||
312 | |||
313 | --------------------------------- (Vector Double) (Matrix Double) | ||
314 | |||
315 | instance Contractible (Vector Double) (Matrix Double) (Vector Double) where | ||
316 | (<>) = vXm | ||
317 | |||
318 | instance Contractible (Vector (Complex Double)) (Matrix (Complex Double)) (Vector (Complex Double)) where | ||
319 | (<>) = vXm | ||
320 | |||
321 | instance Contractible (Vector (Complex Double)) (Matrix Double) (Vector (Complex Double)) where | ||
322 | v <> m = v <> liftMatrix comp m | ||
323 | |||
324 | instance Contractible (Vector Double) (Matrix (Complex Double)) (Vector (Complex Double)) where | ||
325 | v <> m = comp v <> m | ||
326 | |||
327 | --------------------------------- dot product | ||
328 | |||
329 | instance Contractible (Vector Double) (Vector Double) Double where | ||
330 | (<>) = dot | ||
331 | |||
332 | instance Contractible (Vector (Complex Double)) (Vector (Complex Double)) (Complex Double) where | ||
333 | (<>) = dot | ||
334 | |||
335 | instance Contractible (Vector Double) (Vector (Complex Double)) (Complex Double) where | ||
336 | a <> b = comp a <> b | ||
337 | |||
338 | instance Contractible (Vector (Complex Double)) (Vector Double) (Complex Double) where | ||
339 | (<>) = flip (<>) | ||
340 | |||
341 | --------------------------------- scaling vectors | ||
342 | |||
343 | instance Contractible Double (Vector Double) (Vector Double) where | ||
344 | (<>) = scale | ||
345 | |||
346 | instance Contractible (Vector Double) Double (Vector Double) where | ||
347 | (<>) = flip (<>) | ||
348 | |||
349 | instance Contractible (Complex Double) (Vector (Complex Double)) (Vector (Complex Double)) where | ||
350 | (<>) = scale | ||
351 | |||
352 | instance Contractible (Vector (Complex Double)) (Complex Double) (Vector (Complex Double)) where | ||
353 | (<>) = flip (<>) | ||
354 | |||
355 | instance Contractible Double (Vector (Complex Double)) (Vector (Complex Double)) where | ||
356 | a <> v = (a:+0) <> v | ||
357 | |||
358 | instance Contractible (Vector (Complex Double)) Double (Vector (Complex Double)) where | ||
359 | (<>) = flip (<>) | ||
360 | |||
361 | instance Contractible (Complex Double) (Vector Double) (Vector (Complex Double)) where | ||
362 | a <> v = a <> comp v | ||
363 | |||
364 | instance Contractible (Vector Double) (Complex Double) (Vector (Complex Double)) where | ||
365 | (<>) = flip (<>) | ||
366 | |||
367 | --------------------------------- scaling matrices | ||
368 | |||
369 | instance Contractible Double (Matrix Double) (Matrix Double) where | ||
370 | (<>) a = liftMatrix (a <>) | ||
371 | |||
372 | instance Contractible (Matrix Double) Double (Matrix Double) where | ||
373 | (<>) = flip (<>) | ||
374 | |||
375 | instance Contractible (Complex Double) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
376 | (<>) a = liftMatrix (a <>) | ||
377 | |||
378 | instance Contractible (Matrix (Complex Double)) (Complex Double) (Matrix (Complex Double)) where | ||
379 | (<>) = flip (<>) | ||
380 | |||
381 | instance Contractible Double (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
382 | a <> m = (a:+0) <> m | ||
383 | |||
384 | instance Contractible (Matrix (Complex Double)) Double (Matrix (Complex Double)) where | ||
385 | (<>) = flip (<>) | ||
386 | |||
387 | instance Contractible (Complex Double) (Matrix Double) (Matrix (Complex Double)) where | ||
388 | a <> m = a <> liftMatrix comp m | ||
389 | |||
390 | instance Contractible (Matrix Double) (Complex Double) (Matrix (Complex Double)) where | ||
391 | (<>) = flip (<>) | ||