diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-21 09:57:03 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-21 09:57:03 +0200 |
commit | e07c3dee7235496b71a89233106d93f6cc94ada1 (patch) | |
tree | 1ad29c3fc93ee076ad68e3ee759c9a3357f9cd5b /packages/hmatrix/src/Numeric/GSL/Vector.hs | |
parent | 92de588b82945bb251a056c34a8ef0c00cb00e5a (diff) |
Numeric.Container and Numeric.LinearAlgebra moved to base
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/Vector.hs')
-rw-r--r-- | packages/hmatrix/src/Numeric/GSL/Vector.hs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/packages/hmatrix/src/Numeric/GSL/Vector.hs b/packages/hmatrix/src/Numeric/GSL/Vector.hs new file mode 100644 index 0000000..af79f32 --- /dev/null +++ b/packages/hmatrix/src/Numeric/GSL/Vector.hs | |||
@@ -0,0 +1,107 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Numeric.GSL.Vector | ||
4 | -- Copyright : (c) Alberto Ruiz 2007-14 | ||
5 | -- License : GPL | ||
6 | -- Maintainer : Alberto Ruiz | ||
7 | -- Stability : provisional | ||
8 | -- | ||
9 | ----------------------------------------------------------------------------- | ||
10 | |||
11 | module Numeric.GSL.Vector ( | ||
12 | randomVector, | ||
13 | saveMatrix, | ||
14 | fwriteVector, freadVector, fprintfVector, fscanfVector | ||
15 | ) where | ||
16 | |||
17 | import Data.Packed | ||
18 | import Numeric.LinearAlgebra(RandDist(..)) | ||
19 | import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM) | ||
20 | |||
21 | import Foreign.Marshal.Alloc(free) | ||
22 | import Foreign.Ptr(Ptr) | ||
23 | import Foreign.C.Types | ||
24 | import Foreign.C.String(newCString) | ||
25 | import System.IO.Unsafe(unsafePerformIO) | ||
26 | |||
27 | fromei x = fromIntegral (fromEnum x) :: CInt | ||
28 | |||
29 | ----------------------------------------------------------------------- | ||
30 | |||
31 | -- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed. | ||
32 | randomVector :: Int -- ^ seed | ||
33 | -> RandDist -- ^ distribution | ||
34 | -> Int -- ^ vector size | ||
35 | -> Vector Double | ||
36 | randomVector seed dist n = unsafePerformIO $ do | ||
37 | r <- createVector n | ||
38 | app1 (c_random_vector_GSL (fi seed) ((fi.fromEnum) dist)) vec r "randomVectorGSL" | ||
39 | return r | ||
40 | |||
41 | foreign import ccall unsafe "random_vector_GSL" c_random_vector_GSL :: CInt -> CInt -> TV | ||
42 | |||
43 | -------------------------------------------------------------------------------- | ||
44 | |||
45 | -- | Saves a matrix as 2D ASCII table. | ||
46 | saveMatrix :: FilePath | ||
47 | -> String -- ^ format (%f, %g, %e) | ||
48 | -> Matrix Double | ||
49 | -> IO () | ||
50 | saveMatrix filename fmt m = do | ||
51 | charname <- newCString filename | ||
52 | charfmt <- newCString fmt | ||
53 | let o = if orderOf m == RowMajor then 1 else 0 | ||
54 | app1 (matrix_fprintf charname charfmt o) mat m "matrix_fprintf" | ||
55 | free charname | ||
56 | free charfmt | ||
57 | |||
58 | foreign import ccall unsafe "matrix_fprintf" matrix_fprintf :: Ptr CChar -> Ptr CChar -> CInt -> TM | ||
59 | |||
60 | -------------------------------------------------------------------------------- | ||
61 | |||
62 | -- | Loads a vector from an ASCII file (the number of elements must be known in advance). | ||
63 | fscanfVector :: FilePath -> Int -> IO (Vector Double) | ||
64 | fscanfVector filename n = do | ||
65 | charname <- newCString filename | ||
66 | res <- createVector n | ||
67 | app1 (gsl_vector_fscanf charname) vec res "gsl_vector_fscanf" | ||
68 | free charname | ||
69 | return res | ||
70 | |||
71 | foreign import ccall unsafe "vector_fscanf" gsl_vector_fscanf:: Ptr CChar -> TV | ||
72 | |||
73 | -- | Saves the elements of a vector, with a given format (%f, %e, %g), to an ASCII file. | ||
74 | fprintfVector :: FilePath -> String -> Vector Double -> IO () | ||
75 | fprintfVector filename fmt v = do | ||
76 | charname <- newCString filename | ||
77 | charfmt <- newCString fmt | ||
78 | app1 (gsl_vector_fprintf charname charfmt) vec v "gsl_vector_fprintf" | ||
79 | free charname | ||
80 | free charfmt | ||
81 | |||
82 | foreign import ccall unsafe "vector_fprintf" gsl_vector_fprintf :: Ptr CChar -> Ptr CChar -> TV | ||
83 | |||
84 | -- | Loads a vector from a binary file (the number of elements must be known in advance). | ||
85 | freadVector :: FilePath -> Int -> IO (Vector Double) | ||
86 | freadVector filename n = do | ||
87 | charname <- newCString filename | ||
88 | res <- createVector n | ||
89 | app1 (gsl_vector_fread charname) vec res "gsl_vector_fread" | ||
90 | free charname | ||
91 | return res | ||
92 | |||
93 | foreign import ccall unsafe "vector_fread" gsl_vector_fread:: Ptr CChar -> TV | ||
94 | |||
95 | -- | Saves the elements of a vector to a binary file. | ||
96 | fwriteVector :: FilePath -> Vector Double -> IO () | ||
97 | fwriteVector filename v = do | ||
98 | charname <- newCString filename | ||
99 | app1 (gsl_vector_fwrite charname) vec v "gsl_vector_fwrite" | ||
100 | free charname | ||
101 | |||
102 | foreign import ccall unsafe "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV | ||
103 | |||
104 | type PD = Ptr Double -- | ||
105 | type TV = CInt -> PD -> IO CInt -- | ||
106 | type TM = CInt -> CInt -> PD -> IO CInt -- | ||
107 | |||