diff options
Diffstat (limited to 'lib/Data')
-rw-r--r-- | lib/Data/Packed.hs | 70 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 12 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/aux.h | 2 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 8 |
4 files changed, 91 insertions, 1 deletions
diff --git a/lib/Data/Packed.hs b/lib/Data/Packed.hs new file mode 100644 index 0000000..668d2f7 --- /dev/null +++ b/lib/Data/Packed.hs | |||
@@ -0,0 +1,70 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : Data.Packed | ||
5 | Copyright : (c) Alberto Ruiz 2006-7 | ||
6 | License : GPL-style | ||
7 | |||
8 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
9 | Stability : provisional | ||
10 | Portability : uses ffi | ||
11 | |||
12 | The Vector and Matrix types and some utilities. | ||
13 | |||
14 | -} | ||
15 | ----------------------------------------------------------------------------- | ||
16 | |||
17 | module Data.Packed ( | ||
18 | module Data.Packed.Vector, | ||
19 | module Data.Packed.Matrix, | ||
20 | module Data.Complex, | ||
21 | Container(..) | ||
22 | ) where | ||
23 | |||
24 | import Data.Packed.Vector | ||
25 | import Data.Packed.Matrix | ||
26 | import Data.Complex | ||
27 | import Data.Packed.Internal | ||
28 | |||
29 | -- | conversion utilities | ||
30 | class (Field e) => Container c e where | ||
31 | toComplex :: RealFloat e => (c e, c e) -> c (Complex e) | ||
32 | fromComplex :: RealFloat e => c (Complex e) -> (c e, c e) | ||
33 | comp :: RealFloat e => c e -> c (Complex e) | ||
34 | conj :: RealFloat e => c (Complex e) -> c (Complex e) | ||
35 | real :: c Double -> c e | ||
36 | complex :: c e -> c (Complex Double) | ||
37 | |||
38 | instance Container Vector Double where | ||
39 | toComplex = Data.Packed.Internal.toComplex | ||
40 | fromComplex = Data.Packed.Internal.fromComplex | ||
41 | comp = Data.Packed.Internal.comp | ||
42 | conj = Data.Packed.Internal.conj | ||
43 | real = id | ||
44 | complex = Data.Packed.comp | ||
45 | |||
46 | instance Container Vector (Complex Double) where | ||
47 | toComplex = undefined -- can't match | ||
48 | fromComplex = undefined | ||
49 | comp = undefined | ||
50 | conj = undefined | ||
51 | real = Data.Packed.comp | ||
52 | complex = id | ||
53 | |||
54 | instance Container Matrix Double where | ||
55 | toComplex = uncurry $ liftMatrix2 $ curry Data.Packed.toComplex | ||
56 | fromComplex z = (reshape c r, reshape c i) | ||
57 | where (r,i) = Data.Packed.fromComplex (cdat z) | ||
58 | c = cols z | ||
59 | comp = liftMatrix Data.Packed.Internal.comp | ||
60 | conj = liftMatrix Data.Packed.Internal.conj | ||
61 | real = id | ||
62 | complex = Data.Packed.comp | ||
63 | |||
64 | instance Container Matrix (Complex Double) where | ||
65 | toComplex = undefined | ||
66 | fromComplex = undefined | ||
67 | comp = undefined | ||
68 | conj = undefined | ||
69 | real = Data.Packed.comp | ||
70 | complex = id | ||
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 63ebddf..e76500b 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -24,6 +24,8 @@ import Complex | |||
24 | import Control.Monad(when) | 24 | import Control.Monad(when) |
25 | import Data.List(transpose,intersperse) | 25 | import Data.List(transpose,intersperse) |
26 | import Data.Maybe(fromJust) | 26 | import Data.Maybe(fromJust) |
27 | import Foreign.C.String | ||
28 | import Foreign.C.Types | ||
27 | 29 | ||
28 | ----------------------------------------------------------------- | 30 | ----------------------------------------------------------------- |
29 | 31 | ||
@@ -371,6 +373,16 @@ fromComplex z = (r,i) where | |||
371 | comp :: Vector Double -> Vector (Complex Double) | 373 | comp :: Vector Double -> Vector (Complex Double) |
372 | comp v = toComplex (v,constant 0 (dim v)) | 374 | comp v = toComplex (v,constant 0 (dim v)) |
373 | 375 | ||
376 | -- | loads a matrix efficiently from formatted ASCII text file (the number of rows and columns must be known in advance). | ||
377 | fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) | ||
378 | fromFile filename (r,c) = do | ||
379 | charname <- newCString filename | ||
380 | res <- createMatrix RowMajor r c | ||
381 | c_gslReadMatrix charname // mat dat res // check "gslReadMatrix" [] | ||
382 | --free charname -- TO DO: free the auxiliary CString | ||
383 | return res | ||
384 | foreign import ccall "aux.h matrix_fscanf" c_gslReadMatrix:: Ptr CChar -> TM | ||
385 | |||
374 | ------------------------------------------------------------------------- | 386 | ------------------------------------------------------------------------- |
375 | 387 | ||
376 | -- Generic definitions | 388 | -- Generic definitions |
diff --git a/lib/Data/Packed/Internal/aux.h b/lib/Data/Packed/Internal/aux.h index 83111e5..73334e3 100644 --- a/lib/Data/Packed/Internal/aux.h +++ b/lib/Data/Packed/Internal/aux.h | |||
@@ -26,3 +26,5 @@ int diagR(KRVEC(d),RMAT(r)); | |||
26 | int diagC(KCVEC(d),CMAT(r)); | 26 | int diagC(KCVEC(d),CMAT(r)); |
27 | 27 | ||
28 | const char * gsl_strerror (const int gsl_errno); | 28 | const char * gsl_strerror (const int gsl_errno); |
29 | |||
30 | int matrix_fscanf(char*filename, RMAT(a)); | ||
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 404fde7..2b93348 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -25,9 +25,10 @@ module Data.Packed.Matrix ( | |||
25 | fromBlocks, | 25 | fromBlocks, |
26 | flipud, fliprl, | 26 | flipud, fliprl, |
27 | subMatrix, takeRows, dropRows, takeColumns, dropColumns, | 27 | subMatrix, takeRows, dropRows, takeColumns, dropColumns, |
28 | extractRows, | ||
28 | ident, diag, diagRect, takeDiag, | 29 | ident, diag, diagRect, takeDiag, |
29 | liftMatrix, liftMatrix2, | 30 | liftMatrix, liftMatrix2, |
30 | format, dispR, readMatrix, fromArray2D | 31 | format, dispR, readMatrix, fromFile, fromArray2D |
31 | ) where | 32 | ) where |
32 | 33 | ||
33 | import Data.Packed.Internal | 34 | import Data.Packed.Internal |
@@ -209,3 +210,8 @@ dispC d m = disp m (shfc d) | |||
209 | -- | creates a matrix from a table of numbers. | 210 | -- | creates a matrix from a table of numbers. |
210 | readMatrix :: String -> Matrix Double | 211 | readMatrix :: String -> Matrix Double |
211 | readMatrix = fromLists . map (map read). map words . filter (not.null) . lines | 212 | readMatrix = fromLists . map (map read). map words . filter (not.null) . lines |
213 | |||
214 | -- | rearranges the rows of a matrix according to the order given in a list of integers. | ||
215 | extractRows :: Field t => [Int] -> Matrix t -> Matrix t | ||
216 | extractRows l m = fromRows $ extract (toRows $ m) l | ||
217 | where extract l is = [l!!i |i<-is] | ||