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/LinearAlgebra | |
parent | 92de588b82945bb251a056c34a8ef0c00cb00e5a (diff) |
Numeric.Container and Numeric.LinearAlgebra moved to base
Diffstat (limited to 'packages/hmatrix/src/Numeric/LinearAlgebra')
-rw-r--r-- | packages/hmatrix/src/Numeric/LinearAlgebra/GSL.hs | 136 | ||||
-rw-r--r-- | packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs | 42 | ||||
-rw-r--r-- | packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs | 84 |
3 files changed, 0 insertions, 262 deletions
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/GSL.hs b/packages/hmatrix/src/Numeric/LinearAlgebra/GSL.hs deleted file mode 100644 index 1fde621..0000000 --- a/packages/hmatrix/src/Numeric/LinearAlgebra/GSL.hs +++ /dev/null | |||
@@ -1,136 +0,0 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Numeric.LinearAlgebra.GSL | ||
4 | -- Copyright : (c) Alberto Ruiz 2007-14 | ||
5 | -- License : GPL | ||
6 | -- Maintainer : Alberto Ruiz | ||
7 | -- Stability : provisional | ||
8 | -- | ||
9 | ----------------------------------------------------------------------------- | ||
10 | |||
11 | module Numeric.LinearAlgebra.GSL ( | ||
12 | RandDist(..), randomVector, | ||
13 | saveMatrix, | ||
14 | fwriteVector, freadVector, fprintfVector, fscanfVector, | ||
15 | fileDimensions, loadMatrix, fromFile | ||
16 | ) where | ||
17 | |||
18 | import Data.Packed | ||
19 | import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM) | ||
20 | |||
21 | import Data.Complex | ||
22 | import Foreign.Marshal.Alloc(free) | ||
23 | import Foreign.Ptr(Ptr) | ||
24 | import Foreign.C.Types | ||
25 | import Foreign.C.String(newCString) | ||
26 | import System.IO.Unsafe(unsafePerformIO) | ||
27 | import System.Process(readProcess) | ||
28 | |||
29 | fromei x = fromIntegral (fromEnum x) :: CInt | ||
30 | |||
31 | ----------------------------------------------------------------------- | ||
32 | |||
33 | data RandDist = Uniform -- ^ uniform distribution in [0,1) | ||
34 | | Gaussian -- ^ normal distribution with mean zero and standard deviation one | ||
35 | deriving Enum | ||
36 | |||
37 | -- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed. | ||
38 | randomVector :: Int -- ^ seed | ||
39 | -> RandDist -- ^ distribution | ||
40 | -> Int -- ^ vector size | ||
41 | -> Vector Double | ||
42 | randomVector seed dist n = unsafePerformIO $ do | ||
43 | r <- createVector n | ||
44 | app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector" | ||
45 | return r | ||
46 | |||
47 | foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV | ||
48 | |||
49 | -------------------------------------------------------------------------------- | ||
50 | |||
51 | -- | Saves a matrix as 2D ASCII table. | ||
52 | saveMatrix :: FilePath | ||
53 | -> String -- ^ format (%f, %g, %e) | ||
54 | -> Matrix Double | ||
55 | -> IO () | ||
56 | saveMatrix filename fmt m = do | ||
57 | charname <- newCString filename | ||
58 | charfmt <- newCString fmt | ||
59 | let o = if orderOf m == RowMajor then 1 else 0 | ||
60 | app1 (matrix_fprintf charname charfmt o) mat m "matrix_fprintf" | ||
61 | free charname | ||
62 | free charfmt | ||
63 | |||
64 | foreign import ccall unsafe "matrix_fprintf" matrix_fprintf :: Ptr CChar -> Ptr CChar -> CInt -> TM | ||
65 | |||
66 | -------------------------------------------------------------------------------- | ||
67 | |||
68 | -- | Loads a vector from an ASCII file (the number of elements must be known in advance). | ||
69 | fscanfVector :: FilePath -> Int -> IO (Vector Double) | ||
70 | fscanfVector filename n = do | ||
71 | charname <- newCString filename | ||
72 | res <- createVector n | ||
73 | app1 (gsl_vector_fscanf charname) vec res "gsl_vector_fscanf" | ||
74 | free charname | ||
75 | return res | ||
76 | |||
77 | foreign import ccall unsafe "vector_fscanf" gsl_vector_fscanf:: Ptr CChar -> TV | ||
78 | |||
79 | -- | Saves the elements of a vector, with a given format (%f, %e, %g), to an ASCII file. | ||
80 | fprintfVector :: FilePath -> String -> Vector Double -> IO () | ||
81 | fprintfVector filename fmt v = do | ||
82 | charname <- newCString filename | ||
83 | charfmt <- newCString fmt | ||
84 | app1 (gsl_vector_fprintf charname charfmt) vec v "gsl_vector_fprintf" | ||
85 | free charname | ||
86 | free charfmt | ||
87 | |||
88 | foreign import ccall unsafe "vector_fprintf" gsl_vector_fprintf :: Ptr CChar -> Ptr CChar -> TV | ||
89 | |||
90 | -- | Loads a vector from a binary file (the number of elements must be known in advance). | ||
91 | freadVector :: FilePath -> Int -> IO (Vector Double) | ||
92 | freadVector filename n = do | ||
93 | charname <- newCString filename | ||
94 | res <- createVector n | ||
95 | app1 (gsl_vector_fread charname) vec res "gsl_vector_fread" | ||
96 | free charname | ||
97 | return res | ||
98 | |||
99 | foreign import ccall unsafe "vector_fread" gsl_vector_fread:: Ptr CChar -> TV | ||
100 | |||
101 | -- | Saves the elements of a vector to a binary file. | ||
102 | fwriteVector :: FilePath -> Vector Double -> IO () | ||
103 | fwriteVector filename v = do | ||
104 | charname <- newCString filename | ||
105 | app1 (gsl_vector_fwrite charname) vec v "gsl_vector_fwrite" | ||
106 | free charname | ||
107 | |||
108 | foreign import ccall unsafe "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV | ||
109 | |||
110 | type PD = Ptr Double -- | ||
111 | type TV = CInt -> PD -> IO CInt -- | ||
112 | type TM = CInt -> CInt -> PD -> IO CInt -- | ||
113 | |||
114 | -------------------------------------------------------------------------------- | ||
115 | |||
116 | {- | obtains the number of rows and columns in an ASCII data file | ||
117 | (provisionally using unix's wc). | ||
118 | -} | ||
119 | fileDimensions :: FilePath -> IO (Int,Int) | ||
120 | fileDimensions fname = do | ||
121 | wcres <- readProcess "wc" ["-w",fname] "" | ||
122 | contents <- readFile fname | ||
123 | let tot = read . head . words $ wcres | ||
124 | c = length . head . dropWhile null . map words . lines $ contents | ||
125 | if tot > 0 | ||
126 | then return (tot `div` c, c) | ||
127 | else return (0,0) | ||
128 | |||
129 | -- | Loads a matrix from an ASCII file formatted as a 2D table. | ||
130 | loadMatrix :: FilePath -> IO (Matrix Double) | ||
131 | loadMatrix file = fromFile file =<< fileDimensions file | ||
132 | |||
133 | -- | Loads a matrix from an ASCII file (the number of rows and columns must be known in advance). | ||
134 | fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) | ||
135 | fromFile filename (r,c) = reshape c `fmap` fscanfVector filename (r*c) | ||
136 | |||
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs b/packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs deleted file mode 100644 index d2278ee..0000000 --- a/packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs +++ /dev/null | |||
@@ -1,42 +0,0 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Numeric.LinearAlgebra.IO | ||
4 | -- Copyright : (c) Alberto Ruiz 2007-14 | ||
5 | -- License : GPL | ||
6 | -- Maintainer : Alberto Ruiz | ||
7 | -- Stability : provisional | ||
8 | -- | ||
9 | ----------------------------------------------------------------------------- | ||
10 | |||
11 | module Numeric.LinearAlgebra.IO ( | ||
12 | saveMatrix, | ||
13 | fwriteVector, freadVector, fprintfVector, fscanfVector, | ||
14 | fileDimensions, loadMatrix, fromFile | ||
15 | ) where | ||
16 | |||
17 | import Data.Packed | ||
18 | import Numeric.GSL.LinearAlgebra | ||
19 | import System.Process(readProcess) | ||
20 | |||
21 | |||
22 | {- | obtains the number of rows and columns in an ASCII data file | ||
23 | (provisionally using unix's wc). | ||
24 | -} | ||
25 | fileDimensions :: FilePath -> IO (Int,Int) | ||
26 | fileDimensions fname = do | ||
27 | wcres <- readProcess "wc" ["-w",fname] "" | ||
28 | contents <- readFile fname | ||
29 | let tot = read . head . words $ wcres | ||
30 | c = length . head . dropWhile null . map words . lines $ contents | ||
31 | if tot > 0 | ||
32 | then return (tot `div` c, c) | ||
33 | else return (0,0) | ||
34 | |||
35 | -- | Loads a matrix from an ASCII file formatted as a 2D table. | ||
36 | loadMatrix :: FilePath -> IO (Matrix Double) | ||
37 | loadMatrix file = fromFile file =<< fileDimensions file | ||
38 | |||
39 | -- | Loads a matrix from an ASCII file (the number of rows and columns must be known in advance). | ||
40 | fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double) | ||
41 | fromFile filename (r,c) = reshape c `fmap` fscanfVector filename (r*c) | ||
42 | |||
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs b/packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs deleted file mode 100644 index fa125a0..0000000 --- a/packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs +++ /dev/null | |||
@@ -1,84 +0,0 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- | | ||
3 | -- Module : Numeric.LinearAlgebra.Random | ||
4 | -- Copyright : (c) Alberto Ruiz 2009-14 | ||
5 | -- License : GPL | ||
6 | -- | ||
7 | -- Maintainer : Alberto Ruiz | ||
8 | -- Stability : provisional | ||
9 | -- | ||
10 | -- Random vectors and matrices. | ||
11 | -- | ||
12 | ----------------------------------------------------------------------------- | ||
13 | |||
14 | module Numeric.LinearAlgebra.Random ( | ||
15 | Seed, | ||
16 | RandDist(..), | ||
17 | randomVector, | ||
18 | gaussianSample, | ||
19 | uniformSample, | ||
20 | rand, randn | ||
21 | ) where | ||
22 | |||
23 | import Numeric.GSL.LinearAlgebra | ||
24 | import Data.Packed.Numeric | ||
25 | import Numeric.LinearAlgebra.Base(Seed,RandDist(..),cholSH) | ||
26 | import System.Random(randomIO) | ||
27 | |||
28 | |||
29 | |||
30 | |||
31 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariate | ||
32 | -- Gaussian distribution. | ||
33 | gaussianSample :: Seed | ||
34 | -> Int -- ^ number of rows | ||
35 | -> Vector Double -- ^ mean vector | ||
36 | -> Matrix Double -- ^ covariance matrix | ||
37 | -> Matrix Double -- ^ result | ||
38 | gaussianSample seed n med cov = m where | ||
39 | c = dim med | ||
40 | meds = konst' 1 n `outer` med | ||
41 | rs = reshape c $ randomVector seed Gaussian (c * n) | ||
42 | m = rs `mXm` cholSH cov `add` meds | ||
43 | |||
44 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariate | ||
45 | -- uniform distribution. | ||
46 | uniformSample :: Seed | ||
47 | -> Int -- ^ number of rows | ||
48 | -> [(Double,Double)] -- ^ ranges for each column | ||
49 | -> Matrix Double -- ^ result | ||
50 | uniformSample seed n rgs = m where | ||
51 | (as,bs) = unzip rgs | ||
52 | a = fromList as | ||
53 | cs = zipWith subtract as bs | ||
54 | d = dim a | ||
55 | dat = toRows $ reshape n $ randomVector seed Uniform (n*d) | ||
56 | am = konst' 1 n `outer` a | ||
57 | m = fromColumns (zipWith scale cs dat) `add` am | ||
58 | |||
59 | -- | pseudorandom matrix with uniform elements between 0 and 1 | ||
60 | randm :: RandDist | ||
61 | -> Int -- ^ rows | ||
62 | -> Int -- ^ columns | ||
63 | -> IO (Matrix Double) | ||
64 | randm d r c = do | ||
65 | seed <- randomIO | ||
66 | return (reshape c $ randomVector seed d (r*c)) | ||
67 | |||
68 | -- | pseudorandom matrix with uniform elements between 0 and 1 | ||
69 | rand :: Int -> Int -> IO (Matrix Double) | ||
70 | rand = randm Uniform | ||
71 | |||
72 | {- | pseudorandom matrix with normal elements | ||
73 | |||
74 | >>> x <- randn 3 5 | ||
75 | >>> disp 3 x | ||
76 | 3x5 | ||
77 | 0.386 -1.141 0.491 -0.510 1.512 | ||
78 | 0.069 -0.919 1.022 -0.181 0.745 | ||
79 | 0.313 -0.670 -0.097 -1.575 -0.583 | ||
80 | |||
81 | -} | ||
82 | randn :: Int -> Int -> IO (Matrix Double) | ||
83 | randn = randm Gaussian | ||
84 | |||