summaryrefslogtreecommitdiff
path: root/lib/GSL/Compat.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-06-26 07:47:21 +0000
committerAlberto Ruiz <aruiz@um.es>2007-06-26 07:47:21 +0000
commit3019948b97ba1c177b21ab103823fabe561b3ffe (patch)
tree7dbd400d679a765cdc1389e092cafb4b91f4eb89 /lib/GSL/Compat.hs
parent2984d5cc1cedb1621f6fa8d9dda0c515441f92e1 (diff)
passed easyVision tests
Diffstat (limited to 'lib/GSL/Compat.hs')
-rw-r--r--lib/GSL/Compat.hs115
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
17module GSL.Compat( 17module 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
21import Data.Packed.Internal hiding (dsp) 22import Data.Packed.Internal hiding (dsp)
@@ -27,6 +28,8 @@ import LinearAlgebra.Algorithms
27import Complex 28import Complex
28import Numeric(showGFloat) 29import Numeric(showGFloat)
29import Data.List(transpose,intersperse) 30import Data.List(transpose,intersperse)
31import Foreign(Storable)
32import Data.Array
30 33
31 34
32adaptScalar f1 f2 f3 x y 35adaptScalar 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
40liftMatrix2' :: (Field t) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t
41liftMatrix2' 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
44compat' :: Matrix a -> Matrix b -> Bool
45compat' 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
37instance (Eq a, Field a) => Eq (Vector a) where 49instance (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
51instance (Num a, Field a) => Num (Matrix a) where 63instance (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
77instance Fractional (Matrix Double) where 89instance 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
83instance Fractional (Matrix (Complex Double)) where 95instance 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
330size :: Vector a -> Int 342size :: Vector a -> Int
331size = dim 343size = dim
332 344
345gmap :: (Storable a, Storable b) => (a->b) -> Vector a -> Vector b
333gmap f v = liftVector f v 346gmap f v = liftVector f v
334 347
348constant :: Double -> Int -> Vector Double
349constant = 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
337shf :: (RealFloat a) => Int -> a -> String 352shf :: (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.
369readMatrix :: String -> Matrix Double 384readMatrix :: String -> Matrix Double
370readMatrix = fromLists . map (map read). map words . filter (not.null) . lines \ No newline at end of file 385readMatrix = fromLists . map (map read). map words . filter (not.null) . lines
386
387-------------------------------------------------------------
388
389class Joinable a b c | a b -> c where
390 joinH :: a -> b -> c
391 joinV :: a -> b -> c
392
393instance 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
397instance 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
401instance Joinable (Matrix Double) (Matrix Double) (Matrix Double) where
402 joinH m1 m2 = fromBlocks [[m1,m2]]
403 joinV m1 m2 = fromBlocks [[m1],[m2]]
404
405instance 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
409instance 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
413instance 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
417infixl 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.
4251.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
430a <|> b = joinH a b
431
432-- | Vertical concatenation of matrices and vectors.
433(<->) :: (Joinable a b c) => a -> b -> c
434a <-> b = joinV a b
435
436----------------------------------------------------------
437
438vectorMax = toScalarR Max
439
440vectorMin = toScalarR Min
441
442fromArray2D 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
448toComplexV :: (Vector Double, Vector Double) -> Vector (Complex Double)
449toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i]
450
451-- | extracts the real and imaginary parts of a complex vector
452fromComplexV :: Vector (Complex Double) -> (Vector Double, Vector Double)
453fromComplexV m = (a,b) where [a,b] = toColumns $ reshape 2 $ asReal m
454
455-- | creates a complex matrix from matrices with real and imaginary parts
456toComplexM :: (Matrix Double, Matrix Double) -> Matrix (Complex Double)
457toComplexM (r,i) = reshape (cols r) $ asComplex $ flatten $ fromColumns [flatten r, flatten i]
458
459-- | extracts the real and imaginary parts of a complex matrix
460fromComplexM :: Matrix (Complex Double) -> (Matrix Double, Matrix Double)
461fromComplexM m = (reshape c a, reshape c b)
462 where c = cols m
463 [a,b] = toColumns $ reshape 2 $ asReal $ flatten m
464
465fromComplex = fromComplexM
466
467pnorm 0 = LinearAlgebra.Algorithms.pnorm Infinity
468pnorm 1 = LinearAlgebra.Algorithms.pnorm PNorm1
469pnorm 2 = LinearAlgebra.Algorithms.pnorm PNorm2 \ No newline at end of file