diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-06-25 17:34:09 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-06-25 17:34:09 +0000 |
commit | 2984d5cc1cedb1621f6fa8d9dda0c515441f92e1 (patch) | |
tree | 85e155bd77644c26e265996f9cfecd7de70e2450 /lib/GSL/Compat.hs | |
parent | 1871acb835b4fc164bcff3f6e7467884b87fbd0f (diff) |
old tests passed
Diffstat (limited to 'lib/GSL/Compat.hs')
-rw-r--r-- | lib/GSL/Compat.hs | 370 |
1 files changed, 370 insertions, 0 deletions
diff --git a/lib/GSL/Compat.hs b/lib/GSL/Compat.hs new file mode 100644 index 0000000..6a94191 --- /dev/null +++ b/lib/GSL/Compat.hs | |||
@@ -0,0 +1,370 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : GSL.Compat | ||
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 GSL.Compat( | ||
18 | Mul,(<>), fromFile, readMatrix, size, dispR, dispC, format, gmap | ||
19 | ) where | ||
20 | |||
21 | import Data.Packed.Internal hiding (dsp) | ||
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 | import Numeric(showGFloat) | ||
29 | import Data.List(transpose,intersperse) | ||
30 | |||
31 | |||
32 | adaptScalar f1 f2 f3 x y | ||
33 | | dim x == 1 = f1 (x@>0) y | ||
34 | | dim y == 1 = f3 x (y@>0) | ||
35 | | otherwise = f2 x y | ||
36 | |||
37 | instance (Eq a, Field a) => Eq (Vector a) where | ||
38 | a == b = dim a == dim b && toList a == toList b | ||
39 | |||
40 | instance (Num a, Field a) => Num (Vector a) where | ||
41 | (+) = adaptScalar addConstant add (flip addConstant) | ||
42 | negate = scale (-1) | ||
43 | (*) = adaptScalar scale mul (flip scale) | ||
44 | signum = liftVector signum | ||
45 | abs = liftVector abs | ||
46 | fromInteger = fromList . return . fromInteger | ||
47 | |||
48 | instance (Eq a, Field a) => Eq (Matrix a) where | ||
49 | a == b = rows a == rows b && cols a == cols b && cdat a == cdat b && fdat a == fdat b | ||
50 | |||
51 | instance (Num a, Field a) => Num (Matrix a) where | ||
52 | (+) = liftMatrix2 (+) | ||
53 | negate = liftMatrix negate | ||
54 | (*) = liftMatrix2 (*) | ||
55 | signum = liftMatrix signum | ||
56 | abs = liftMatrix abs | ||
57 | fromInteger = (1><1) . return . fromInteger | ||
58 | |||
59 | --------------------------------------------------- | ||
60 | |||
61 | instance Fractional (Vector Double) where | ||
62 | fromRational n = fromList [fromRational n] | ||
63 | (/) = adaptScalar f (vectorZipR Div) g where | ||
64 | r `f` v = vectorMapValR Recip r v | ||
65 | v `g` r = scale (recip r) v | ||
66 | |||
67 | ------------------------------------------------------- | ||
68 | |||
69 | instance Fractional (Vector (Complex Double)) where | ||
70 | fromRational n = fromList [fromRational n] | ||
71 | (/) = adaptScalar f (vectorZipC Div) g where | ||
72 | r `f` v = vectorMapValC Recip r v | ||
73 | v `g` r = scale (recip r) v | ||
74 | |||
75 | ------------------------------------------------------ | ||
76 | |||
77 | instance Fractional (Matrix Double) where | ||
78 | fromRational n = (1><1) [fromRational n] | ||
79 | (/) = liftMatrix2 (/) | ||
80 | |||
81 | ------------------------------------------------------- | ||
82 | |||
83 | instance Fractional (Matrix (Complex Double)) where | ||
84 | fromRational n = (1><1) [fromRational n] | ||
85 | (/) = liftMatrix2 (/) | ||
86 | |||
87 | --------------------------------------------------------- | ||
88 | |||
89 | instance Floating (Vector Double) where | ||
90 | sin = vectorMapR Sin | ||
91 | cos = vectorMapR Cos | ||
92 | tan = vectorMapR Tan | ||
93 | asin = vectorMapR ASin | ||
94 | acos = vectorMapR ACos | ||
95 | atan = vectorMapR ATan | ||
96 | sinh = vectorMapR Sinh | ||
97 | cosh = vectorMapR Cosh | ||
98 | tanh = vectorMapR Tanh | ||
99 | asinh = vectorMapR ASinh | ||
100 | acosh = vectorMapR ACosh | ||
101 | atanh = vectorMapR ATanh | ||
102 | exp = vectorMapR Exp | ||
103 | log = vectorMapR Log | ||
104 | sqrt = vectorMapR Sqrt | ||
105 | (**) = adaptScalar (vectorMapValR PowSV) (vectorZipR Pow) (flip (vectorMapValR PowVS)) | ||
106 | pi = fromList [pi] | ||
107 | |||
108 | ----------------------------------------------------------- | ||
109 | |||
110 | instance Floating (Matrix Double) where | ||
111 | sin = liftMatrix sin | ||
112 | cos = liftMatrix cos | ||
113 | tan = liftMatrix tan | ||
114 | asin = liftMatrix asin | ||
115 | acos = liftMatrix acos | ||
116 | atan = liftMatrix atan | ||
117 | sinh = liftMatrix sinh | ||
118 | cosh = liftMatrix cosh | ||
119 | tanh = liftMatrix tanh | ||
120 | asinh = liftMatrix asinh | ||
121 | acosh = liftMatrix acosh | ||
122 | atanh = liftMatrix atanh | ||
123 | exp = liftMatrix exp | ||
124 | log = liftMatrix log | ||
125 | (**) = liftMatrix2 (**) | ||
126 | sqrt = liftMatrix sqrt | ||
127 | pi = (1><1) [pi] | ||
128 | ------------------------------------------------------------- | ||
129 | |||
130 | instance Floating (Vector (Complex Double)) where | ||
131 | sin = vectorMapC Sin | ||
132 | cos = vectorMapC Cos | ||
133 | tan = vectorMapC Tan | ||
134 | asin = vectorMapC ASin | ||
135 | acos = vectorMapC ACos | ||
136 | atan = vectorMapC ATan | ||
137 | sinh = vectorMapC Sinh | ||
138 | cosh = vectorMapC Cosh | ||
139 | tanh = vectorMapC Tanh | ||
140 | asinh = vectorMapC ASinh | ||
141 | acosh = vectorMapC ACosh | ||
142 | atanh = vectorMapC ATanh | ||
143 | exp = vectorMapC Exp | ||
144 | log = vectorMapC Log | ||
145 | sqrt = vectorMapC Sqrt | ||
146 | (**) = adaptScalar (vectorMapValC PowSV) (vectorZipC Pow) (flip (vectorMapValC PowVS)) | ||
147 | pi = fromList [pi] | ||
148 | |||
149 | --------------------------------------------------------------- | ||
150 | |||
151 | instance Floating (Matrix (Complex Double)) where | ||
152 | sin = liftMatrix sin | ||
153 | cos = liftMatrix cos | ||
154 | tan = liftMatrix tan | ||
155 | asin = liftMatrix asin | ||
156 | acos = liftMatrix acos | ||
157 | atan = liftMatrix atan | ||
158 | sinh = liftMatrix sinh | ||
159 | cosh = liftMatrix cosh | ||
160 | tanh = liftMatrix tanh | ||
161 | asinh = liftMatrix asinh | ||
162 | acosh = liftMatrix acosh | ||
163 | atanh = liftMatrix atanh | ||
164 | exp = liftMatrix exp | ||
165 | log = liftMatrix log | ||
166 | (**) = liftMatrix2 (**) | ||
167 | sqrt = liftMatrix sqrt | ||
168 | pi = (1><1) [pi] | ||
169 | |||
170 | --------------------------------------------------------------- | ||
171 | |||
172 | |||
173 | class Mul a b c | a b -> c where | ||
174 | infixl 7 <> | ||
175 | {- | 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. | ||
176 | |||
177 | @v = 'fromList' [1,2,3] :: Vector Double | ||
178 | cv = 'fromList' [1+'i',2] | ||
179 | m = 'fromLists' [[1,2,3], | ||
180 | [4,5,7]] :: Matrix Double | ||
181 | cm = 'fromLists' [[ 1, 2], | ||
182 | [3+'i',7*'i'], | ||
183 | [ 'i', 1]] | ||
184 | \ | ||
185 | \> m \<\> v | ||
186 | 14. 35. | ||
187 | \ | ||
188 | \> cv \<\> m | ||
189 | 9.+1.i 12.+2.i 17.+3.i | ||
190 | \ | ||
191 | \> m \<\> cm | ||
192 | 7.+5.i 5.+14.i | ||
193 | 19.+12.i 15.+35.i | ||
194 | \ | ||
195 | \> v \<\> 'i' | ||
196 | 1.i 2.i 3.i | ||
197 | \ | ||
198 | \> v \<\> v | ||
199 | 14.0 | ||
200 | \ | ||
201 | \> cv \<\> cv | ||
202 | 4.0 :+ 2.0@ | ||
203 | |||
204 | -} | ||
205 | (<>) :: a -> b -> c | ||
206 | |||
207 | |||
208 | instance Mul Double Double Double where | ||
209 | (<>) = (*) | ||
210 | |||
211 | instance Mul Double (Complex Double) (Complex Double) where | ||
212 | a <> b = (a:+0) * b | ||
213 | |||
214 | instance Mul (Complex Double) Double (Complex Double) where | ||
215 | a <> b = a * (b:+0) | ||
216 | |||
217 | instance Mul (Complex Double) (Complex Double) (Complex Double) where | ||
218 | (<>) = (*) | ||
219 | |||
220 | --------------------------------- matrix matrix | ||
221 | |||
222 | instance Mul (Matrix Double) (Matrix Double) (Matrix Double) where | ||
223 | (<>) = mXm | ||
224 | |||
225 | instance Mul (Matrix (Complex Double)) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
226 | (<>) = mXm | ||
227 | |||
228 | instance Mul (Matrix (Complex Double)) (Matrix Double) (Matrix (Complex Double)) where | ||
229 | c <> r = c <> liftMatrix comp r | ||
230 | |||
231 | instance Mul (Matrix Double) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
232 | r <> c = liftMatrix comp r <> c | ||
233 | |||
234 | --------------------------------- (Matrix Double) (Vector Double) | ||
235 | |||
236 | instance Mul (Matrix Double) (Vector Double) (Vector Double) where | ||
237 | (<>) = mXv | ||
238 | |||
239 | instance Mul (Matrix (Complex Double)) (Vector (Complex Double)) (Vector (Complex Double)) where | ||
240 | (<>) = mXv | ||
241 | |||
242 | instance Mul (Matrix (Complex Double)) (Vector Double) (Vector (Complex Double)) where | ||
243 | m <> v = m <> comp v | ||
244 | |||
245 | instance Mul (Matrix Double) (Vector (Complex Double)) (Vector (Complex Double)) where | ||
246 | m <> v = liftMatrix comp m <> v | ||
247 | |||
248 | --------------------------------- (Vector Double) (Matrix Double) | ||
249 | |||
250 | instance Mul (Vector Double) (Matrix Double) (Vector Double) where | ||
251 | (<>) = vXm | ||
252 | |||
253 | instance Mul (Vector (Complex Double)) (Matrix (Complex Double)) (Vector (Complex Double)) where | ||
254 | (<>) = vXm | ||
255 | |||
256 | instance Mul (Vector (Complex Double)) (Matrix Double) (Vector (Complex Double)) where | ||
257 | v <> m = v <> liftMatrix comp m | ||
258 | |||
259 | instance Mul (Vector Double) (Matrix (Complex Double)) (Vector (Complex Double)) where | ||
260 | v <> m = comp v <> m | ||
261 | |||
262 | --------------------------------- dot product | ||
263 | |||
264 | instance Mul (Vector Double) (Vector Double) Double where | ||
265 | (<>) = dot | ||
266 | |||
267 | instance Mul (Vector (Complex Double)) (Vector (Complex Double)) (Complex Double) where | ||
268 | (<>) = dot | ||
269 | |||
270 | instance Mul (Vector Double) (Vector (Complex Double)) (Complex Double) where | ||
271 | a <> b = comp a <> b | ||
272 | |||
273 | instance Mul (Vector (Complex Double)) (Vector Double) (Complex Double) where | ||
274 | (<>) = flip (<>) | ||
275 | |||
276 | --------------------------------- scaling vectors | ||
277 | |||
278 | instance Mul Double (Vector Double) (Vector Double) where | ||
279 | (<>) = scale | ||
280 | |||
281 | instance Mul (Vector Double) Double (Vector Double) where | ||
282 | (<>) = flip (<>) | ||
283 | |||
284 | instance Mul (Complex Double) (Vector (Complex Double)) (Vector (Complex Double)) where | ||
285 | (<>) = scale | ||
286 | |||
287 | instance Mul (Vector (Complex Double)) (Complex Double) (Vector (Complex Double)) where | ||
288 | (<>) = flip (<>) | ||
289 | |||
290 | instance Mul Double (Vector (Complex Double)) (Vector (Complex Double)) where | ||
291 | a <> v = (a:+0) <> v | ||
292 | |||
293 | instance Mul (Vector (Complex Double)) Double (Vector (Complex Double)) where | ||
294 | (<>) = flip (<>) | ||
295 | |||
296 | instance Mul (Complex Double) (Vector Double) (Vector (Complex Double)) where | ||
297 | a <> v = a <> comp v | ||
298 | |||
299 | instance Mul (Vector Double) (Complex Double) (Vector (Complex Double)) where | ||
300 | (<>) = flip (<>) | ||
301 | |||
302 | --------------------------------- scaling matrices | ||
303 | |||
304 | instance Mul Double (Matrix Double) (Matrix Double) where | ||
305 | (<>) a = liftMatrix (a <>) | ||
306 | |||
307 | instance Mul (Matrix Double) Double (Matrix Double) where | ||
308 | (<>) = flip (<>) | ||
309 | |||
310 | instance Mul (Complex Double) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
311 | (<>) a = liftMatrix (a <>) | ||
312 | |||
313 | instance Mul (Matrix (Complex Double)) (Complex Double) (Matrix (Complex Double)) where | ||
314 | (<>) = flip (<>) | ||
315 | |||
316 | instance Mul Double (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
317 | a <> m = (a:+0) <> m | ||
318 | |||
319 | instance Mul (Matrix (Complex Double)) Double (Matrix (Complex Double)) where | ||
320 | (<>) = flip (<>) | ||
321 | |||
322 | instance Mul (Complex Double) (Matrix Double) (Matrix (Complex Double)) where | ||
323 | a <> m = a <> liftMatrix comp m | ||
324 | |||
325 | instance Mul (Matrix Double) (Complex Double) (Matrix (Complex Double)) where | ||
326 | (<>) = flip (<>) | ||
327 | |||
328 | ----------------------------------------------------------------------------------- | ||
329 | |||
330 | size :: Vector a -> Int | ||
331 | size = dim | ||
332 | |||
333 | gmap f v = liftVector f v | ||
334 | |||
335 | |||
336 | -- shows a Double with n digits after the decimal point | ||
337 | shf :: (RealFloat a) => Int -> a -> String | ||
338 | shf dec n | abs n < 1e-10 = "0." | ||
339 | | abs (n - (fromIntegral.round $ n)) < 1e-10 = show (round n) ++"." | ||
340 | | otherwise = showGFloat (Just dec) n "" | ||
341 | -- shows a Complex Double as a pair, with n digits after the decimal point | ||
342 | shfc n z@ (a:+b) | ||
343 | | magnitude z <1e-10 = "0." | ||
344 | | abs b < 1e-10 = shf n a | ||
345 | | abs a < 1e-10 = shf n b ++"i" | ||
346 | | b > 0 = shf n a ++"+"++shf n b ++"i" | ||
347 | | otherwise = shf n a ++shf n b ++"i" | ||
348 | |||
349 | dsp :: String -> [[String]] -> String | ||
350 | dsp sep as = unlines . map unwords' $ transpose mtp where | ||
351 | mt = transpose as | ||
352 | longs = map (maximum . map length) mt | ||
353 | mtp = zipWith (\a b -> map (pad a) b) longs mt | ||
354 | pad n str = replicate (n - length str) ' ' ++ str | ||
355 | unwords' = concat . intersperse sep | ||
356 | |||
357 | format :: (Field t) => String -> (t -> String) -> Matrix t -> String | ||
358 | format sep f m = dsp sep . map (map f) . toLists $ m | ||
359 | |||
360 | disp m f = putStrLn $ "matrix ("++show (rows m) ++"x"++ show (cols m) ++")\n"++format " | " f m | ||
361 | |||
362 | dispR :: Int -> Matrix Double -> IO () | ||
363 | dispR d m = disp m (shf d) | ||
364 | |||
365 | dispC :: Int -> Matrix (Complex Double) -> IO () | ||
366 | dispC d m = disp m (shfc d) | ||
367 | |||
368 | -- | creates a matrix from a table of numbers. | ||
369 | readMatrix :: String -> Matrix Double | ||
370 | readMatrix = fromLists . map (map read). map words . filter (not.null) . lines \ No newline at end of file | ||