diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-06-26 07:47:21 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-06-26 07:47:21 +0000 |
commit | 3019948b97ba1c177b21ab103823fabe561b3ffe (patch) | |
tree | 7dbd400d679a765cdc1389e092cafb4b91f4eb89 /lib/GSL/Compat.hs | |
parent | 2984d5cc1cedb1621f6fa8d9dda0c515441f92e1 (diff) |
passed easyVision tests
Diffstat (limited to 'lib/GSL/Compat.hs')
-rw-r--r-- | lib/GSL/Compat.hs | 115 |
1 files changed, 107 insertions, 8 deletions
diff --git a/lib/GSL/Compat.hs b/lib/GSL/Compat.hs index 6a94191..2cae0c4 100644 --- a/lib/GSL/Compat.hs +++ b/lib/GSL/Compat.hs | |||
@@ -15,7 +15,8 @@ Creates reasonable numeric instances for Vectors and Matrices. In the context of | |||
15 | ----------------------------------------------------------------------------- | 15 | ----------------------------------------------------------------------------- |
16 | 16 | ||
17 | module GSL.Compat( | 17 | module GSL.Compat( |
18 | Mul,(<>), fromFile, readMatrix, size, dispR, dispC, format, gmap | 18 | Mul,(<>), readMatrix, size, dispR, dispC, format, gmap, Joinable, (<|>),(<->), GSL.Compat.constant, |
19 | vectorMax, vectorMin, fromArray2D, fromComplex, GSL.Compat.pnorm, scale | ||
19 | ) where | 20 | ) where |
20 | 21 | ||
21 | import Data.Packed.Internal hiding (dsp) | 22 | import Data.Packed.Internal hiding (dsp) |
@@ -27,6 +28,8 @@ import LinearAlgebra.Algorithms | |||
27 | import Complex | 28 | import Complex |
28 | import Numeric(showGFloat) | 29 | import Numeric(showGFloat) |
29 | import Data.List(transpose,intersperse) | 30 | import Data.List(transpose,intersperse) |
31 | import Foreign(Storable) | ||
32 | import Data.Array | ||
30 | 33 | ||
31 | 34 | ||
32 | adaptScalar f1 f2 f3 x y | 35 | adaptScalar f1 f2 f3 x y |
@@ -34,6 +37,15 @@ adaptScalar f1 f2 f3 x y | |||
34 | | dim y == 1 = f3 x (y@>0) | 37 | | dim y == 1 = f3 x (y@>0) |
35 | | otherwise = f2 x y | 38 | | otherwise = f2 x y |
36 | 39 | ||
40 | liftMatrix2' :: (Field t) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | ||
41 | liftMatrix2' f m1 m2 | compat' m1 m2 = reshape (max (cols m1) (cols m2)) (f (cdat m1) (cdat m2)) | ||
42 | | otherwise = error "nonconformant matrices in liftMatrix2'" | ||
43 | |||
44 | compat' :: Matrix a -> Matrix b -> Bool | ||
45 | compat' m1 m2 = rows m1 == 1 && cols m1 == 1 | ||
46 | || rows m2 == 1 && cols m2 == 1 | ||
47 | || rows m1 == rows m2 && cols m1 == cols m2 | ||
48 | |||
37 | instance (Eq a, Field a) => Eq (Vector a) where | 49 | instance (Eq a, Field a) => Eq (Vector a) where |
38 | a == b = dim a == dim b && toList a == toList b | 50 | a == b = dim a == dim b && toList a == toList b |
39 | 51 | ||
@@ -49,9 +61,9 @@ 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 | 61 | a == b = rows a == rows b && cols a == cols b && cdat a == cdat b && fdat a == fdat b |
50 | 62 | ||
51 | instance (Num a, Field a) => Num (Matrix a) where | 63 | instance (Num a, Field a) => Num (Matrix a) where |
52 | (+) = liftMatrix2 (+) | 64 | (+) = liftMatrix2' (+) |
53 | negate = liftMatrix negate | 65 | negate = liftMatrix negate |
54 | (*) = liftMatrix2 (*) | 66 | (*) = liftMatrix2' (*) |
55 | signum = liftMatrix signum | 67 | signum = liftMatrix signum |
56 | abs = liftMatrix abs | 68 | abs = liftMatrix abs |
57 | fromInteger = (1><1) . return . fromInteger | 69 | fromInteger = (1><1) . return . fromInteger |
@@ -76,13 +88,13 @@ instance Fractional (Vector (Complex Double)) where | |||
76 | 88 | ||
77 | instance Fractional (Matrix Double) where | 89 | instance Fractional (Matrix Double) where |
78 | fromRational n = (1><1) [fromRational n] | 90 | fromRational n = (1><1) [fromRational n] |
79 | (/) = liftMatrix2 (/) | 91 | (/) = liftMatrix2' (/) |
80 | 92 | ||
81 | ------------------------------------------------------- | 93 | ------------------------------------------------------- |
82 | 94 | ||
83 | instance Fractional (Matrix (Complex Double)) where | 95 | instance Fractional (Matrix (Complex Double)) where |
84 | fromRational n = (1><1) [fromRational n] | 96 | fromRational n = (1><1) [fromRational n] |
85 | (/) = liftMatrix2 (/) | 97 | (/) = liftMatrix2' (/) |
86 | 98 | ||
87 | --------------------------------------------------------- | 99 | --------------------------------------------------------- |
88 | 100 | ||
@@ -122,7 +134,7 @@ instance Floating (Matrix Double) where | |||
122 | atanh = liftMatrix atanh | 134 | atanh = liftMatrix atanh |
123 | exp = liftMatrix exp | 135 | exp = liftMatrix exp |
124 | log = liftMatrix log | 136 | log = liftMatrix log |
125 | (**) = liftMatrix2 (**) | 137 | (**) = liftMatrix2' (**) |
126 | sqrt = liftMatrix sqrt | 138 | sqrt = liftMatrix sqrt |
127 | pi = (1><1) [pi] | 139 | pi = (1><1) [pi] |
128 | ------------------------------------------------------------- | 140 | ------------------------------------------------------------- |
@@ -163,7 +175,7 @@ instance Floating (Matrix (Complex Double)) where | |||
163 | atanh = liftMatrix atanh | 175 | atanh = liftMatrix atanh |
164 | exp = liftMatrix exp | 176 | exp = liftMatrix exp |
165 | log = liftMatrix log | 177 | log = liftMatrix log |
166 | (**) = liftMatrix2 (**) | 178 | (**) = liftMatrix2' (**) |
167 | sqrt = liftMatrix sqrt | 179 | sqrt = liftMatrix sqrt |
168 | pi = (1><1) [pi] | 180 | pi = (1><1) [pi] |
169 | 181 | ||
@@ -330,8 +342,11 @@ instance Mul (Matrix Double) (Complex Double) (Matrix (Complex Double)) where | |||
330 | size :: Vector a -> Int | 342 | size :: Vector a -> Int |
331 | size = dim | 343 | size = dim |
332 | 344 | ||
345 | gmap :: (Storable a, Storable b) => (a->b) -> Vector a -> Vector b | ||
333 | gmap f v = liftVector f v | 346 | gmap f v = liftVector f v |
334 | 347 | ||
348 | constant :: Double -> Int -> Vector Double | ||
349 | constant = constantR | ||
335 | 350 | ||
336 | -- shows a Double with n digits after the decimal point | 351 | -- shows a Double with n digits after the decimal point |
337 | shf :: (RealFloat a) => Int -> a -> String | 352 | shf :: (RealFloat a) => Int -> a -> String |
@@ -367,4 +382,88 @@ dispC d m = disp m (shfc d) | |||
367 | 382 | ||
368 | -- | creates a matrix from a table of numbers. | 383 | -- | creates a matrix from a table of numbers. |
369 | readMatrix :: String -> Matrix Double | 384 | readMatrix :: String -> Matrix Double |
370 | readMatrix = fromLists . map (map read). map words . filter (not.null) . lines \ No newline at end of file | 385 | readMatrix = fromLists . map (map read). map words . filter (not.null) . lines |
386 | |||
387 | ------------------------------------------------------------- | ||
388 | |||
389 | class Joinable a b c | a b -> c where | ||
390 | joinH :: a -> b -> c | ||
391 | joinV :: a -> b -> c | ||
392 | |||
393 | instance Joinable (Matrix Double) (Vector Double) (Matrix Double) where | ||
394 | joinH m v = fromBlocks [[m,reshape 1 v]] | ||
395 | joinV m v = fromBlocks [[m],[reshape (size v) v]] | ||
396 | |||
397 | instance Joinable (Vector Double) (Matrix Double) (Matrix Double) where | ||
398 | joinH v m = fromBlocks [[reshape 1 v,m]] | ||
399 | joinV v m = fromBlocks [[reshape (size v) v],[m]] | ||
400 | |||
401 | instance Joinable (Matrix Double) (Matrix Double) (Matrix Double) where | ||
402 | joinH m1 m2 = fromBlocks [[m1,m2]] | ||
403 | joinV m1 m2 = fromBlocks [[m1],[m2]] | ||
404 | |||
405 | instance Joinable (Matrix (Complex Double)) (Vector (Complex Double)) (Matrix (Complex Double)) where | ||
406 | joinH m v = fromBlocks [[m,reshape 1 v]] | ||
407 | joinV m v = fromBlocks [[m],[reshape (size v) v]] | ||
408 | |||
409 | instance Joinable (Vector (Complex Double)) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
410 | joinH v m = fromBlocks [[reshape 1 v,m]] | ||
411 | joinV v m = fromBlocks [[reshape (size v) v],[m]] | ||
412 | |||
413 | instance Joinable (Matrix (Complex Double)) (Matrix (Complex Double)) (Matrix (Complex Double)) where | ||
414 | joinH m1 m2 = fromBlocks [[m1,m2]] | ||
415 | joinV m1 m2 = fromBlocks [[m1],[m2]] | ||
416 | |||
417 | infixl 3 <|>, <-> | ||
418 | |||
419 | {- | Horizontal concatenation of matrices and vectors: | ||
420 | |||
421 | @\> 'ident' 3 \<-\> i\<\>'ident' 3 \<|\> 'fromList' [1..6] | ||
422 | 1. 0. 0. 1. | ||
423 | 0. 1. 0. 2. | ||
424 | 0. 0. 1. 3. | ||
425 | 1.i 0. 0. 4. | ||
426 | 0. 1.i 0. 5. | ||
427 | 0. 0. 1.i 6.@ | ||
428 | -} | ||
429 | (<|>) :: (Joinable a b c) => a -> b -> c | ||
430 | a <|> b = joinH a b | ||
431 | |||
432 | -- | Vertical concatenation of matrices and vectors. | ||
433 | (<->) :: (Joinable a b c) => a -> b -> c | ||
434 | a <-> b = joinV a b | ||
435 | |||
436 | ---------------------------------------------------------- | ||
437 | |||
438 | vectorMax = toScalarR Max | ||
439 | |||
440 | vectorMin = toScalarR Min | ||
441 | |||
442 | fromArray2D m = (r><c) (elems m) | ||
443 | where ((r0,c0),(r1,c1)) = bounds m | ||
444 | r = r1-r0+1 | ||
445 | c = c1-c0+1 | ||
446 | |||
447 | -- | creates a complex vector from vectors with real and imaginary parts | ||
448 | toComplexV :: (Vector Double, Vector Double) -> Vector (Complex Double) | ||
449 | toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i] | ||
450 | |||
451 | -- | extracts the real and imaginary parts of a complex vector | ||
452 | fromComplexV :: Vector (Complex Double) -> (Vector Double, Vector Double) | ||
453 | fromComplexV m = (a,b) where [a,b] = toColumns $ reshape 2 $ asReal m | ||
454 | |||
455 | -- | creates a complex matrix from matrices with real and imaginary parts | ||
456 | toComplexM :: (Matrix Double, Matrix Double) -> Matrix (Complex Double) | ||
457 | toComplexM (r,i) = reshape (cols r) $ asComplex $ flatten $ fromColumns [flatten r, flatten i] | ||
458 | |||
459 | -- | extracts the real and imaginary parts of a complex matrix | ||
460 | fromComplexM :: Matrix (Complex Double) -> (Matrix Double, Matrix Double) | ||
461 | fromComplexM m = (reshape c a, reshape c b) | ||
462 | where c = cols m | ||
463 | [a,b] = toColumns $ reshape 2 $ asReal $ flatten m | ||
464 | |||
465 | fromComplex = fromComplexM | ||
466 | |||
467 | pnorm 0 = LinearAlgebra.Algorithms.pnorm Infinity | ||
468 | pnorm 1 = LinearAlgebra.Algorithms.pnorm PNorm1 | ||
469 | pnorm 2 = LinearAlgebra.Algorithms.pnorm PNorm2 \ No newline at end of file | ||