summaryrefslogtreecommitdiff
path: root/lib/Data/Packed
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-06-25 17:34:09 +0000
committerAlberto Ruiz <aruiz@um.es>2007-06-25 17:34:09 +0000
commit2984d5cc1cedb1621f6fa8d9dda0c515441f92e1 (patch)
tree85e155bd77644c26e265996f9cfecd7de70e2450 /lib/Data/Packed
parent1871acb835b4fc164bcff3f6e7467884b87fbd0f (diff)
old tests passed
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r--lib/Data/Packed/Instances.hs391
-rw-r--r--lib/Data/Packed/Internal/Vector.hs3
2 files changed, 3 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{- |
4Module : Data.Packed.Instances
5Copyright : (c) Alberto Ruiz 2006
6License : GPL-style
7
8Maintainer : Alberto Ruiz (aruiz at um dot es)
9Stability : provisional
10Portability : uses -fffi and -fglasgow-exts
11
12Creates 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
17module Data.Packed.Instances(
18 Contractible(..)
19) where
20
21import Data.Packed.Internal
22import Data.Packed.Vector
23import Data.Packed.Matrix
24import GSL.Vector
25import GSL.Matrix
26import LinearAlgebra.Algorithms
27import Complex
28
29instance (Eq a, Field a) => Eq (Vector a) where
30 a == b = dim a == dim b && toList a == toList b
31
32instance (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
40instance (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
43instance (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
53adaptScalar 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{-
59subvv = vectorZip 4
60subvc v c = addConstant (-c) v
61subcv c v = addConstant c (scale (-1) v)
62
63mul = vectorZip 1
64
65instance 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
77subCvv = vectorZipComplex 4 -- faster?
78subCvc v c = addConstantC (-c) v
79subCcv c v = addConstantC c (scale (-1) v)
80
81
82instance 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
92liftMatrix2' :: (Vector a -> Vector b -> Vector c) -> Matrix a -> Matrix b -> Matrix c
93liftMatrix2' 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
100instance (Eq a, Field a) => Eq (Matrix a) where
101 a == b = rows a == rows b && cdat a == cdat b
102
103instance 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
113instance 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
123instance 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
131instance 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
139instance Fractional (Matrix Double) where
140 fromRational n = fromLists [[fromRational n]]
141 (/) = liftMatrix2' (/)
142
143-------------------------------------------------------
144
145instance Fractional (Matrix (Complex Double)) where
146 fromRational n = fromLists [[fromRational n]]
147 (/) = liftMatrix2' (/)
148
149---------------------------------------------------------
150
151instance 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
173instance 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
194instance 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
216instance 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
238class 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
243cv = 'fromList' [1+'i',2]
244m = 'fromLists' [[1,2,3],
245 [4,5,7]] :: Matrix Double
246cm = 'fromLists' [[ 1, 2],
247 [3+'i',7*'i'],
248 [ 'i', 1]]
249\
250\> m \<\> v
25114. 35.
252\
253\> cv \<\> m
2549.+1.i 12.+2.i 17.+3.i
255\
256\> m \<\> cm
257 7.+5.i 5.+14.i
25819.+12.i 15.+35.i
259\
260\> v \<\> 'i'
2611.i 2.i 3.i
262\
263\> v \<\> v
26414.0
265\
266\> cv \<\> cv
2674.0 :+ 2.0@
268
269-}
270 (<>) :: a -> b -> c
271
272
273instance Contractible Double Double Double where
274 (<>) = (*)
275
276instance Contractible Double (Complex Double) (Complex Double) where
277 a <> b = (a:+0) * b
278
279instance Contractible (Complex Double) Double (Complex Double) where
280 a <> b = a * (b:+0)
281
282instance Contractible (Complex Double) (Complex Double) (Complex Double) where
283 (<>) = (*)
284
285--------------------------------- matrix matrix
286
287instance Contractible (Matrix Double) (Matrix Double) (Matrix Double) where
288 (<>) = mXm
289
290instance Contractible (Matrix (Complex Double)) (Matrix (Complex Double)) (Matrix (Complex Double)) where
291 (<>) = mXm
292
293instance Contractible (Matrix (Complex Double)) (Matrix Double) (Matrix (Complex Double)) where
294 c <> r = c <> liftMatrix comp r
295
296instance Contractible (Matrix Double) (Matrix (Complex Double)) (Matrix (Complex Double)) where
297 r <> c = liftMatrix comp r <> c
298
299--------------------------------- (Matrix Double) (Vector Double)
300
301instance Contractible (Matrix Double) (Vector Double) (Vector Double) where
302 (<>) = mXv
303
304instance Contractible (Matrix (Complex Double)) (Vector (Complex Double)) (Vector (Complex Double)) where
305 (<>) = mXv
306
307instance Contractible (Matrix (Complex Double)) (Vector Double) (Vector (Complex Double)) where
308 m <> v = m <> comp v
309
310instance Contractible (Matrix Double) (Vector (Complex Double)) (Vector (Complex Double)) where
311 m <> v = liftMatrix comp m <> v
312
313--------------------------------- (Vector Double) (Matrix Double)
314
315instance Contractible (Vector Double) (Matrix Double) (Vector Double) where
316 (<>) = vXm
317
318instance Contractible (Vector (Complex Double)) (Matrix (Complex Double)) (Vector (Complex Double)) where
319 (<>) = vXm
320
321instance Contractible (Vector (Complex Double)) (Matrix Double) (Vector (Complex Double)) where
322 v <> m = v <> liftMatrix comp m
323
324instance Contractible (Vector Double) (Matrix (Complex Double)) (Vector (Complex Double)) where
325 v <> m = comp v <> m
326
327--------------------------------- dot product
328
329instance Contractible (Vector Double) (Vector Double) Double where
330 (<>) = dot
331
332instance Contractible (Vector (Complex Double)) (Vector (Complex Double)) (Complex Double) where
333 (<>) = dot
334
335instance Contractible (Vector Double) (Vector (Complex Double)) (Complex Double) where
336 a <> b = comp a <> b
337
338instance Contractible (Vector (Complex Double)) (Vector Double) (Complex Double) where
339 (<>) = flip (<>)
340
341--------------------------------- scaling vectors
342
343instance Contractible Double (Vector Double) (Vector Double) where
344 (<>) = scale
345
346instance Contractible (Vector Double) Double (Vector Double) where
347 (<>) = flip (<>)
348
349instance Contractible (Complex Double) (Vector (Complex Double)) (Vector (Complex Double)) where
350 (<>) = scale
351
352instance Contractible (Vector (Complex Double)) (Complex Double) (Vector (Complex Double)) where
353 (<>) = flip (<>)
354
355instance Contractible Double (Vector (Complex Double)) (Vector (Complex Double)) where
356 a <> v = (a:+0) <> v
357
358instance Contractible (Vector (Complex Double)) Double (Vector (Complex Double)) where
359 (<>) = flip (<>)
360
361instance Contractible (Complex Double) (Vector Double) (Vector (Complex Double)) where
362 a <> v = a <> comp v
363
364instance Contractible (Vector Double) (Complex Double) (Vector (Complex Double)) where
365 (<>) = flip (<>)
366
367--------------------------------- scaling matrices
368
369instance Contractible Double (Matrix Double) (Matrix Double) where
370 (<>) a = liftMatrix (a <>)
371
372instance Contractible (Matrix Double) Double (Matrix Double) where
373 (<>) = flip (<>)
374
375instance Contractible (Complex Double) (Matrix (Complex Double)) (Matrix (Complex Double)) where
376 (<>) a = liftMatrix (a <>)
377
378instance Contractible (Matrix (Complex Double)) (Complex Double) (Matrix (Complex Double)) where
379 (<>) = flip (<>)
380
381instance Contractible Double (Matrix (Complex Double)) (Matrix (Complex Double)) where
382 a <> m = (a:+0) <> m
383
384instance Contractible (Matrix (Complex Double)) Double (Matrix (Complex Double)) where
385 (<>) = flip (<>)
386
387instance Contractible (Complex Double) (Matrix Double) (Matrix (Complex Double)) where
388 a <> m = a <> liftMatrix comp m
389
390instance Contractible (Matrix Double) (Complex Double) (Matrix (Complex Double)) where
391 (<>) = flip (<>)
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index f1addf4..ab93577 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -129,5 +129,8 @@ constant x n | isReal id x = scast $ constantR (scast x) n
129 | isComp id x = scast $ constantC (scast x) n 129 | isComp id x = scast $ constantC (scast x) n
130 | otherwise = constantG x n 130 | otherwise = constantG x n
131 131
132liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b
132liftVector f = fromList . map f . toList 133liftVector f = fromList . map f . toList
134
135liftVector2 :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c
133liftVector2 f u v = fromList $ zipWith f (toList u) (toList v) 136liftVector2 f u v = fromList $ zipWith f (toList u) (toList v)